-
Notifications
You must be signed in to change notification settings - Fork 255
/
test_constructor.py
85 lines (68 loc) · 2.74 KB
/
test_constructor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
'''
Test creating an instance of a Java class and fetching its values.
'''
from __future__ import absolute_import
import unittest
from jnius import autoclass, JavaException
class TestConstructor(unittest.TestCase):
'''
TestCase for using constructors with PyJNIus.
'''
def test_constructor_none(self):
'''
Empty constructor, baked value in the .java file.
'''
ConstructorTest = autoclass('org.jnius.ConstructorTest')
inst = ConstructorTest()
self.assertEqual(inst.ret, 753)
self.assertTrue(ConstructorTest._class.isInstance(inst))
self.assertTrue(inst.getClass().isInstance(inst))
def test_constructor_int(self):
'''
Constructor expecting int and setting it as public 'ret' property.
'''
ConstructorTest = autoclass('org.jnius.ConstructorTest')
inst = ConstructorTest(123)
self.assertEqual(inst.ret, 123)
self.assertTrue(ConstructorTest._class.isInstance(inst))
self.assertTrue(inst.getClass().isInstance(inst))
def test_constructor_string(self):
'''
Constructor expecting char, casting it to int and setting it
as public 'ret' property.
'''
ConstructorTest = autoclass('org.jnius.ConstructorTest')
inst = ConstructorTest('a')
self.assertEqual(inst.ret, ord('a'))
self.assertTrue(ConstructorTest._class.isInstance(inst))
self.assertTrue(inst.getClass().isInstance(inst))
def test_constructor_multiobj(self):
'''
Constructor expecting String.
'''
outputStream = autoclass('java.lang.System').out
ConstructorTest = autoclass('org.jnius.ConstructorTest')
inst = ConstructorTest(outputStream, signature="(Ljava/io/OutputStream;)V")
self.assertEqual(inst.ret, 42)
self.assertTrue(ConstructorTest._class.isInstance(inst))
self.assertTrue(inst.getClass().isInstance(inst))
def test_constructor_int_string(self):
'''
Constructor expecting int and char, casting char to int and summing it
as public 'ret' property.
'''
ConstructorTest = autoclass('org.jnius.ConstructorTest')
inst = ConstructorTest(3, 'a')
self.assertEqual(inst.ret, 3 + ord('a'))
self.assertTrue(ConstructorTest._class.isInstance(inst))
self.assertTrue(inst.getClass().isInstance(inst))
def test_constructor_exception(self):
'''
No such constructor found (no such signature),
should raise JavaException.
'''
ConstructorTest = autoclass('org.jnius.ConstructorTest')
with self.assertRaises(JavaException):
ConstructorTest('1.', 2.0, False)
if __name__ == '__main__':
unittest.main()