pyjnius/tests/test_closeable.py

44 lines
1.4 KiB
Python
Raw Normal View History

'''
Test creating for java.io.Closeable dunder
'''
from __future__ import absolute_import
import unittest
from jnius import autoclass, JavaException, protocol_map
2020-04-09 08:51:33 +00:00
class TestCloseable(unittest.TestCase):
'''
TestCase for using java.io.Closeable dunder
'''
def test_stringreader_closeable(self):
2020-04-09 08:51:33 +00:00
'''
this checkes that java.lang.AutoCloseable instances gain
the correct dunder methods
'''
swCheck = autoclass("java.io.StringWriter")()
self.assertTrue("__enter__" in dir(swCheck))
self.assertTrue("__exit__" in dir(swCheck))
with autoclass("java.io.StringWriter")() as sw:
sw.write("Hello")
def test_our_closeable(self):
2020-04-09 08:51:33 +00:00
'''
this checkes that closeable dunder methods are actually called
org.jnius.CloseableClass has java.io.Closeable, to differ from
java.lang.AutoCloseable (which is what is in interface_map)
'''
ourcloseableClz = autoclass("org.jnius.CloseableClass")
self.assertTrue("__enter__" in dir(ourcloseableClz()))
self.assertTrue("__exit__" in dir(ourcloseableClz()))
self.assertTrue(ourcloseableClz.open)
with ourcloseableClz() as ourcloseable2:
self.assertTrue(ourcloseableClz.open)
self.assertFalse(ourcloseableClz.open)
2020-04-09 00:46:41 +00:00
def test_protocol_map(self):
self.assertTrue("java.util.List" in protocol_map)