2020-04-04 09:02:05 +00:00
|
|
|
'''
|
|
|
|
Test creating for java.io.Closeable dunder
|
|
|
|
'''
|
|
|
|
|
|
|
|
from __future__ import absolute_import
|
|
|
|
import unittest
|
2020-04-08 20:46:37 +00:00
|
|
|
from jnius import autoclass, JavaException, protocol_map
|
2020-04-04 09:02:05 +00:00
|
|
|
|
|
|
|
|
2020-04-09 08:51:33 +00:00
|
|
|
class TestCloseable(unittest.TestCase):
|
2020-04-04 09:02:05 +00:00
|
|
|
'''
|
|
|
|
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
|
|
|
|
'''
|
2020-04-04 09:02:05 +00:00
|
|
|
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)
|
|
|
|
'''
|
2020-04-04 09:02:05 +00:00
|
|
|
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):
|
2020-04-08 20:46:37 +00:00
|
|
|
self.assertTrue("java.util.List" in protocol_map)
|