| 1 | import unittest |
| 2 | import sys |
| 3 | |
| 4 | import jpyutil |
| 5 | |
| 6 | |
| 7 | jpyutil.init_jvm(jvm_maxmem='512M', jvm_classpath=['target/test-classes']) |
| 8 | import jpy |
| 9 | |
| 10 | |
| 11 | if sys.version_info >= (3, 0, 0): |
| 12 | TYPE_STR_PREFIX = '<class ' |
| 13 | else: |
| 14 | TYPE_STR_PREFIX = '<type ' |
| 15 | |
| 16 | |
| 17 | class TestGetClass(unittest.TestCase): |
| 18 | def test_get_class_of_primitive_array(self): |
| 19 | IntArray1D = jpy.get_type('[I') |
| 20 | self.assertEqual(str(IntArray1D), TYPE_STR_PREFIX + "'[I'>") |
| 21 | |
| 22 | IntArray2D = jpy.get_type('[[I') |
| 23 | self.assertEqual(str(IntArray2D), TYPE_STR_PREFIX + "'[[I'>") |
| 24 | |
| 25 | IntArray3D = jpy.get_type('[[[I') |
| 26 | self.assertEqual(str(IntArray3D), TYPE_STR_PREFIX + "'[[[I'>") |
| 27 | |
| 28 | with self.assertRaises(RuntimeError) as e: |
| 29 | IntArray1D() |
| 30 | self.assertEqual(str(e.exception), "no constructor found (missing JType attribute '__jinit__')") |
| 31 | |
| 32 | |
| 33 | def test_get_class_of_object_array(self): |
| 34 | StringArray1D = jpy.get_type('[Ljava.lang.String;') |
| 35 | self.assertEqual(str(StringArray1D), TYPE_STR_PREFIX + "'[Ljava.lang.String;'>") |
| 36 | |
| 37 | StringArray2D = jpy.get_type('[[Ljava.lang.String;') |
| 38 | self.assertEqual(str(StringArray2D), TYPE_STR_PREFIX + "'[[Ljava.lang.String;'>") |
| 39 | |
| 40 | StringArray3D = jpy.get_type('[[[Ljava.lang.String;') |
| 41 | self.assertEqual(str(StringArray3D), TYPE_STR_PREFIX + "'[[[Ljava.lang.String;'>") |
| 42 | |
| 43 | with self.assertRaises(RuntimeError) as e: |
| 44 | StringArray1D() |
| 45 | self.assertEqual(str(e.exception), "no constructor found (missing JType attribute '__jinit__')") |
| 46 | |
| 47 | def test_get_class_fromm_inner_class(self): |
| 48 | Point2D = jpy.get_type('java.awt.geom.Point2D') |
| 49 | self.assertEqual(str(Point2D), TYPE_STR_PREFIX + "'java.awt.geom.Point2D'>") |
| 50 | DoublePoint = jpy.get_type('java.awt.geom.Point2D$Double') |
| 51 | self.assertEqual(str(DoublePoint), TYPE_STR_PREFIX + "'java.awt.geom.Point2D$Double'>") |
| 52 | |
| 53 | |
| 54 | def test_get_class_of_unknown_type(self): |
| 55 | with self.assertRaises(ValueError) as e: |
| 56 | String = jpy.get_type('java.lang.Spring') |
| 57 | self.assertEqual(str(e.exception), "Java class 'java.lang.Spring' not found") |
| 58 | |
| 59 | with self.assertRaises(ValueError) as e: |
| 60 | IntArray = jpy.get_type('int[]') |
| 61 | self.assertEqual(str(e.exception), "Java class 'int[]' not found") |
| 62 | |
| 63 | def test_issue_74(self): |
| 64 | """ |
| 65 | Try to create enough references to trigger collection by Python. |
| 66 | """ |
| 67 | java_types = ['boolean', 'char', 'byte', 'short', 'int', 'long', |
| 68 | 'float', 'double', 'void', 'java.lang.String'] |
| 69 | |
| 70 | for java_type in java_types: |
| 71 | for i in range(200): |
| 72 | jpy.get_type(java_type) |
| 73 | |
| 74 | def test_cyclic_reference(self): |
| 75 | """ |
| 76 | Test if delaying resolving super classes breaks existing class reference pattern. |
| 77 | """ |
| 78 | j_child1_class = jpy.get_type("org.jpy.fixtures.CyclicReferenceChild1") |
| 79 | j_child2_class = jpy.get_type("org.jpy.fixtures.CyclicReferenceChild2") |
| 80 | j_child2 = j_child2_class() |
| 81 | |
| 82 | j_child1 = j_child1_class.of(8) |
| 83 | self.assertEqual(88, j_child1.parentMethod()) |
| 84 | self.assertEqual(888, j_child1.grandParentMethod()) |
| 85 | self.assertIsNone(j_child1.refChild2(j_child2)) |
| 86 | self.assertEqual(8, j_child1.get_x()) |
| 87 | self.assertEqual(10, j_child1.y) |
| 88 | self.assertEqual(100, j_child1.z) |
| 89 | |
| 90 | def test_component_type_resolution(self): |
| 91 | j_child1_class = jpy.get_type("org.jpy.fixtures.CyclicReferenceChild1") |
| 92 | j_child1 = j_child1_class.of(8) |
| 93 | j_child2s = j_child1.getChild2s() |
| 94 | self.assertIn("[Lorg.jpy.fixtures.CyclicReferenceChild2;", repr(type(j_child2s))) |
| 95 | for j_child2 in j_child2s: |
| 96 | self.assertTrue(j_child2.getName().startswith("Child2")) |
| 97 | |
| 98 | def test_fail_init_supertype(self): |
| 99 | with self.assertRaises(ValueError) as cm: |
| 100 | j_child_class = jpy.get_type("org.jpy.fixtures.GetTypeFailureChild") |
| 101 | |
| 102 | |
| 103 | if __name__ == '__main__': |
| 104 | print('\nRunning ' + __file__) |
| 105 | unittest.main() |