Debian Codegraph @

src/test/python/jpy_typeconv_test.py

raw symbols SCIP
1 import unittest
2 import array
3
4 import jpyutil
5
6 jpyutil.init_jvm(jvm_maxmem='512M', jvm_classpath=['target/test-classes'])
7 import jpy
8
9
10 class TestTypeConversions(unittest.TestCase):
11 """
12 This test covers type conversion, including:
13
14 - Automatic conversions of Python values to Java when calling Java methods from Python
15 - jpy.cast() (See JPy_cast)
16 - jpy.convert() (See JPy_convert_internal / JType_ConvertPythonToJavaObject)
17 """
18
19 def setUp(self):
20 self.Fixture = jpy.get_type('org.jpy.fixtures.TypeConversionTestFixture')
21 self.assertTrue('org.jpy.fixtures.TypeConversionTestFixture' in jpy.types)
22
23 def test_ToObjectConversion(self):
24 """
25 Test automatic conversion of Python values to Java objects (when passing Python values as arguments to Java methods).
26 """
27
28 fixture = self.Fixture()
29 self.assertEqual(fixture.stringifyObjectArg(12), 'Byte(12)')
30 self.assertEqual(fixture.stringifyObjectArg(0.34), 'Double(0.34)')
31 self.assertEqual(fixture.stringifyObjectArg('abc'), 'String(abc)')
32
33 with self.assertRaises(ValueError) as e:
34 fixture.stringifyObjectArg(1 + 2j)
35 self.assertEqual(str(e.exception), 'cannot convert a Python \'complex\' to a Java \'java.lang.Object\'')
36
37 def test_cast(self):
38 """
39 Test casts of Java objects using jpy.cast()
40 """
41 fixture = self.Fixture()
42
43 # Create a test String:
44 my_jstr = jpy.get_type('java.lang.String')('testStr')
45 self.assertEqual(type(my_jstr).jclassname, 'java.lang.String')
46 self.assertEqual(fixture.stringifyObjectArg(my_jstr), 'String(testStr)')
47
48 # Cast to String (this should be a no-op)
49 my_jcharseq1 = jpy.cast(my_jstr, 'java.lang.String')
50 self.assertTrue(fixture.isSameObject(my_jstr, my_jcharseq1)) # Should be same Java object...
51 # self.assertTrue(my_jcharseq1 is my_jstr) # and the same Python object. (But currently a new one is returned.)
52 self.assertEqual(type(my_jcharseq1).jclassname, 'java.lang.String')
53 self.assertEqual(fixture.stringifyObjectArg(my_jcharseq1), 'String(testStr)')
54
55 # Cast to CharSequence (using class name, not explicit jpy.get_type())
56 my_jcharseq1 = jpy.cast(my_jstr, 'java.lang.CharSequence')
57 self.assertTrue(fixture.isSameObject(my_jstr, my_jcharseq1)) # Should be same Java object...
58 self.assertFalse(my_jcharseq1 is my_jstr) # but a new Python object
59 self.assertEqual(type(my_jcharseq1).jclassname, 'java.lang.CharSequence')
60 self.assertEqual(fixture.stringifyObjectArg(my_jcharseq1), 'String(testStr)')
61
62 # Cast to CharSequence (using explicit jpy.get_type()):
63 my_jcharseq2 = jpy.cast(my_jstr, jpy.get_type('java.lang.CharSequence'))
64 self.assertTrue(fixture.isSameObject(my_jstr, my_jcharseq2)) # Should be same Java object...
65 self.assertFalse(my_jcharseq2 is my_jstr) # but a new Python object
66 self.assertEqual(type(my_jcharseq2).jclassname, 'java.lang.CharSequence')
67 self.assertEqual(fixture.stringifyObjectArg(my_jcharseq2), 'String(testStr)')
68
69 def test_convert_cast(self):
70 """
71 Test casts of Java objects using jpy.convert()
72 """
73 fixture = self.Fixture()
74
75 # Create a test String:
76 my_jstr = jpy.get_type('java.lang.String')('testStr')
77 self.assertEqual(type(my_jstr).jclassname, 'java.lang.String')
78 self.assertEqual(fixture.stringifyObjectArg(my_jstr), 'String(testStr)')
79
80 # Cast to String (this should be a no-op)
81 my_jcharseq1 = jpy.convert(my_jstr, 'java.lang.String')
82 self.assertTrue(fixture.isSameObject(my_jstr, my_jcharseq1)) # Should be same Java object...
83 # self.assertTrue(my_jcharseq1 is my_jstr) # and the same Python object. (But currently a new one is returned.)
84 self.assertEqual(type(my_jcharseq1).jclassname, 'java.lang.String')
85 self.assertEqual(fixture.stringifyObjectArg(my_jcharseq1), 'String(testStr)')
86
87 # Cast to CharSequence (using class name, not explicit jpy.get_type())
88 my_jcharseq1 = jpy.convert(my_jstr, 'java.lang.CharSequence')
89 self.assertTrue(fixture.isSameObject(my_jstr, my_jcharseq1)) # Should be same Java object...
90 self.assertFalse(my_jcharseq1 is my_jstr) # but a new Python object.
91 self.assertEqual(type(my_jcharseq1).jclassname, 'java.lang.CharSequence')
92 self.assertEqual(fixture.stringifyObjectArg(my_jcharseq1), 'String(testStr)')
93
94 # Cast to CharSequence (using explicit jpy.get_type()):
95 my_jcharseq2 = jpy.convert(my_jstr, jpy.get_type('java.lang.CharSequence'))
96 self.assertTrue(fixture.isSameObject(my_jstr, my_jcharseq2)) # Should be same Java object...
97 self.assertFalse(my_jcharseq2 is my_jstr) # but a new Python object.
98 self.assertEqual(type(my_jcharseq2).jclassname, 'java.lang.CharSequence')
99 self.assertEqual(fixture.stringifyObjectArg(my_jcharseq2), 'String(testStr)')
100
101 def test_convert_toBoxedPrimitive(self):
102 fixture = self.Fixture()
103
104 # Convert Python values to boxed types explicitly (using jpy.get_type() to retrieve the boxed type):
105 self.assertEqual(fixture.stringifyObjectArg(jpy.convert(65, jpy.get_type('java.lang.Character'))),
106 'Character(A)')
107 self.assertEqual(fixture.stringifyObjectArg(jpy.convert(12, jpy.get_type('java.lang.Byte'))), 'Byte(12)')
108 self.assertEqual(fixture.stringifyObjectArg(jpy.convert(12, jpy.get_type('java.lang.Short'))), 'Short(12)')
109 self.assertEqual(fixture.stringifyObjectArg(jpy.convert(12, jpy.get_type('java.lang.Integer'))), 'Integer(12)')
110 self.assertEqual(fixture.stringifyObjectArg(jpy.convert(12, jpy.get_type('java.lang.Long'))), 'Long(12)')
111 self.assertEqual(fixture.stringifyObjectArg(jpy.convert(12, jpy.get_type('java.lang.Float'))), 'Float(12.0)')
112 self.assertEqual(fixture.stringifyObjectArg(jpy.convert(12, jpy.get_type('java.lang.Double'))), 'Double(12.0)')
113
114 # Convert Python values to boxed types (using type name, not explicit jpy.get_type())
115 self.assertEqual(fixture.stringifyObjectArg(jpy.convert(65, 'java.lang.Character')), 'Character(A)')
116 self.assertEqual(fixture.stringifyObjectArg(jpy.convert(12, 'java.lang.Byte')), 'Byte(12)')
117 self.assertEqual(fixture.stringifyObjectArg(jpy.convert(12, 'java.lang.Short')), 'Short(12)')
118 self.assertEqual(fixture.stringifyObjectArg(jpy.convert(12, 'java.lang.Integer')), 'Integer(12)')
119 self.assertEqual(fixture.stringifyObjectArg(jpy.convert(12, 'java.lang.Long')), 'Long(12)')
120 self.assertEqual(fixture.stringifyObjectArg(jpy.convert(12, 'java.lang.Float')), 'Float(12.0)')
121 self.assertEqual(fixture.stringifyObjectArg(jpy.convert(12, 'java.lang.Double')), 'Double(12.0)')
122
123 # Convert Python values to boxed types (using primitive type names — but they still get boxed):
124 self.assertEqual(fixture.stringifyObjectArg(jpy.convert(65, jpy.get_type('char'))), 'Character(A)')
125 self.assertEqual(fixture.stringifyObjectArg(jpy.convert(12, jpy.get_type('byte'))), 'Byte(12)')
126 self.assertEqual(fixture.stringifyObjectArg(jpy.convert(12, jpy.get_type('short'))), 'Short(12)')
127 self.assertEqual(fixture.stringifyObjectArg(jpy.convert(12, jpy.get_type('int'))), 'Integer(12)')
128 self.assertEqual(fixture.stringifyObjectArg(jpy.convert(12, jpy.get_type('long'))), 'Long(12)')
129 self.assertEqual(fixture.stringifyObjectArg(jpy.convert(12, jpy.get_type('float'))), 'Float(12.0)')
130 self.assertEqual(fixture.stringifyObjectArg(jpy.convert(12, jpy.get_type('double'))), 'Double(12.0)')
131
132 def test_convert_toPrimitiveArray(self):
133 fixture = self.Fixture()
134
135 # Convert Python values to arrays of primitive types
136
137 target_type = type(jpy.array(jpy.get_type('char'), 0))
138 jobj = jpy.convert([65, 66, 67], target_type)
139 self.assertEqual(type(jobj), target_type)
140 self.assertEqual(fixture.stringifyObjectArg(jobj), 'char[](A,B,C)')
141
142 target_type = type(jpy.array(jpy.get_type('byte'), 0))
143 jobj = jpy.convert([12, 13, 14], target_type)
144 self.assertEqual(type(jobj), target_type)
145 self.assertEqual(fixture.stringifyObjectArg(jobj), 'byte[](12,13,14)')
146
147 target_type = type(jpy.array(jpy.get_type('short'), 0))
148 jobj = jpy.convert([12, 13, 14], target_type)
149 self.assertEqual(type(jobj), target_type)
150 self.assertEqual(fixture.stringifyObjectArg(jobj), 'short[](12,13,14)')
151
152 target_type = type(jpy.array(jpy.get_type('int'), 0))
153 jobj = jpy.convert([12, 13, 14], target_type)
154 self.assertEqual(type(jobj), target_type)
155 self.assertEqual(fixture.stringifyObjectArg(jobj), 'int[](12,13,14)')
156
157 target_type = type(jpy.array(jpy.get_type('long'), 0))
158 jobj = jpy.convert([12, 13, 14], target_type)
159 self.assertEqual(type(jobj), target_type)
160 self.assertEqual(fixture.stringifyObjectArg(jobj), 'long[](12,13,14)')
161
162 target_type = type(jpy.array(jpy.get_type('float'), 0))
163 jobj = jpy.convert([12, 13, 14], target_type)
164 self.assertEqual(type(jobj), target_type)
165 self.assertEqual(fixture.stringifyObjectArg(jobj), 'float[](12.0,13.0,14.0)')
166
167 target_type = type(jpy.array(jpy.get_type('double'), 0))
168 jobj = jpy.convert([12, 13, 14], target_type)
169 self.assertEqual(type(jobj), target_type)
170 self.assertEqual(fixture.stringifyObjectArg(jobj), 'double[](12.0,13.0,14.0)')
171
172 def test_convert_toBoxedPrimitiveArray(self):
173 fixture = self.Fixture()
174
175 # Convert Python values to arrays of boxed types
176
177 target_type = type(jpy.array(jpy.get_type('java.lang.Character'), 0))
178 jobj = jpy.convert([65, 66, 67], target_type)
179 self.assertEqual(type(jobj), target_type)
180 self.assertEqual(fixture.stringifyObjectArg(jobj), 'Character[](Character(A),Character(B),Character(C))')
181
182 target_type = type(jpy.array(jpy.get_type('java.lang.Byte'), 0))
183 jobj = jpy.convert([12, 13, 14], target_type)
184 self.assertEqual(type(jobj), target_type)
185 self.assertEqual(fixture.stringifyObjectArg(jobj), 'Byte[](Byte(12),Byte(13),Byte(14))')
186
187 target_type = type(jpy.array(jpy.get_type('java.lang.Short'), 0))
188 jobj = jpy.convert([12, 13, 14], target_type)
189 self.assertEqual(type(jobj), target_type)
190 self.assertEqual(fixture.stringifyObjectArg(jobj), 'Short[](Short(12),Short(13),Short(14))')
191
192 target_type = type(jpy.array(jpy.get_type('java.lang.Integer'), 0))
193 jobj = jpy.convert([12, 13, 14], target_type)
194 self.assertEqual(type(jobj), target_type)
195 self.assertEqual(fixture.stringifyObjectArg(jobj), 'Integer[](Integer(12),Integer(13),Integer(14))')
196
197 target_type = type(jpy.array(jpy.get_type('java.lang.Long'), 0))
198 jobj = jpy.convert([12, 13, 14], target_type)
199 self.assertEqual(type(jobj), target_type)
200 self.assertEqual(fixture.stringifyObjectArg(jobj), 'Long[](Long(12),Long(13),Long(14))')
201
202 target_type = type(jpy.array(jpy.get_type('java.lang.Float'), 0))
203 jobj = jpy.convert([12, 13, 14], target_type)
204 self.assertEqual(type(jobj), target_type)
205 self.assertEqual(fixture.stringifyObjectArg(jobj), 'Float[](Float(12.0),Float(13.0),Float(14.0))')
206
207 target_type = type(jpy.array(jpy.get_type('java.lang.Double'), 0))
208 jobj = jpy.convert([12, 13, 14], target_type)
209 self.assertEqual(type(jobj), target_type)
210 self.assertEqual(fixture.stringifyObjectArg(jobj), 'Double[](Double(12.0),Double(13.0),Double(14.0))')
211
212 def test_convert_toJavaLangObject(self):
213 """
214 Test converting values to java.lang.Object (and letting the jpy module determine what Java type to convert
215 them to).
216 """
217 java_lang_object_type = jpy.get_type('java.lang.Object')
218
219 jobj = jpy.convert('A', java_lang_object_type)
220 expected_type = jpy.get_type('java.lang.String')
221 self.assertTrue(type(jobj).jclass.equals(java_lang_object_type.jclass), f'Type is {type(jobj)}')
222 self.assertTrue(jobj.getClass().equals(expected_type.jclass), f'Type is {jobj.getClass()}')
223 self.assertEqual(jpy.cast(jobj, expected_type).toString(), 'A')
224
225 jobj = jpy.convert('ABCDE', java_lang_object_type)
226 expected_type = jpy.get_type('java.lang.String')
227 self.assertTrue(type(jobj).jclass.equals(java_lang_object_type.jclass), f'Type is {type(jobj)}')
228 self.assertTrue(jobj.getClass().equals(expected_type.jclass), f'Type is {jobj.getClass()}')
229 self.assertEqual(jpy.cast(jobj, expected_type).toString(), 'ABCDE')
230
231 jobj = jpy.convert(True, java_lang_object_type)
232 expected_type = jpy.get_type('java.lang.Boolean')
233 self.assertTrue(type(jobj).jclass.equals(java_lang_object_type.jclass), f'Type is {type(jobj)}')
234 self.assertTrue(jobj.getClass().equals(expected_type.jclass), f'Type is {jobj.getClass()}')
235 self.assertEqual(jpy.cast(jobj, expected_type).booleanValue(), True)
236
237 jobj = jpy.convert(False, java_lang_object_type)
238 expected_type = jpy.get_type('java.lang.Boolean')
239 self.assertTrue(type(jobj).jclass.equals(java_lang_object_type.jclass), f'Type is {type(jobj)}')
240 self.assertTrue(jobj.getClass().equals(expected_type.jclass), f'Type is {jobj.getClass()}')
241 self.assertEqual(jpy.cast(jobj, expected_type).booleanValue(), False)
242
243 jobj = jpy.convert(12, java_lang_object_type)
244 expected_type = jpy.get_type('java.lang.Byte')
245 self.assertTrue(type(jobj).jclass.equals(java_lang_object_type.jclass), f'Type is {type(jobj)}')
246 self.assertTrue(jobj.getClass().equals(expected_type.jclass), f'Type is {jobj.getClass()}')
247 self.assertEqual(jpy.cast(jobj, expected_type).byteValue(), 12)
248
249 jobj = jpy.convert(129, java_lang_object_type)
250 expected_type = jpy.get_type('java.lang.Short')
251 self.assertTrue(type(jobj).jclass.equals(java_lang_object_type.jclass), f'Type is {type(jobj)}')
252 self.assertTrue(jobj.getClass().equals(expected_type.jclass), f'Type is {jobj.getClass()}')
253 self.assertEqual(jpy.cast(jobj, expected_type).shortValue(), 129)
254
255 jobj = jpy.convert(100_000, java_lang_object_type)
256 expected_type = jpy.get_type('java.lang.Integer')
257 self.assertTrue(type(jobj).jclass.equals(java_lang_object_type.jclass), f'Type is {type(jobj)}')
258 self.assertTrue(jobj.getClass().equals(expected_type.jclass), f'Type is {jobj.getClass()}')
259 self.assertEqual(jpy.cast(jobj, expected_type).intValue(), 100_000)
260
261 jobj = jpy.convert(10_000_000_000, java_lang_object_type)
262 expected_type = jpy.get_type('java.lang.Long')
263 self.assertTrue(type(jobj).jclass.equals(java_lang_object_type.jclass), f'Type is {type(jobj)}')
264 self.assertTrue(jobj.getClass().equals(expected_type.jclass), f'Type is {jobj.getClass()}')
265 self.assertEqual(jpy.cast(jobj, expected_type).longValue(), 10_000_000_000)
266
267 jobj = jpy.convert(123.45, java_lang_object_type)
268 expected_type = jpy.get_type('java.lang.Double') # TODO: these go to Double, not Float ?
269 self.assertTrue(type(jobj).jclass.equals(java_lang_object_type.jclass), f'Type is {type(jobj)}')
270 self.assertTrue(jobj.getClass().equals(expected_type.jclass), f'Type is {jobj.getClass()}')
271 self.assertEqual(jpy.cast(jobj, expected_type).doubleValue(), 123.45)
272
273 too_big_for_float = jpy.get_type('java.lang.Double').MAX_VALUE
274 jobj = jpy.convert(too_big_for_float, java_lang_object_type)
275 expected_type = jpy.get_type('java.lang.Double')
276 self.assertTrue(type(jobj).jclass.equals(java_lang_object_type.jclass), f'Type is {type(jobj)}')
277 self.assertTrue(jobj.getClass().equals(expected_type.jclass), f'Type is {jobj.getClass()}')
278 self.assertEqual(jpy.cast(jobj, expected_type).doubleValue(), too_big_for_float)
279
280 def test_convert_toString(self):
281 jobj = jpy.convert('test string', jpy.get_type('java.lang.Object'))
282 self.assertTrue(type(jobj).jclass.equals(jpy.get_type('java.lang.Object').jclass), f'Type is {type(jobj)}')
283 self.assertTrue(jobj.getClass().equals(jpy.get_type('java.lang.String').jclass), f'Type is {jobj.getClass()}')
284 self.assertEqual(jpy.cast(jobj, jpy.get_type('java.lang.String')).toString(), 'test string')
285
286 # Invalid conversion:
287 with self.assertRaises(ValueError) as e:
288 jpy.convert(12, jpy.get_type('java.lang.String'))
289 actual_message = str(e.exception)
290 expected_message = "cannot convert a Python 'int' to a Java 'java.lang.String'"
291 self.assertEqual(actual_message, expected_message)
292
293 def test_ToPrimitiveArrayConversion(self):
294 fixture = self.Fixture()
295
296 # Python int array to Java int array
297 a = array.array('i', [1, 2, 3])
298 self.assertEqual(fixture.stringifyIntArrayArg(a), 'int[](1,2,3)')
299
300 # integer list
301 a = [4, 5, 6]
302 self.assertEqual(fixture.stringifyIntArrayArg(a), 'int[](4,5,6)')
303
304 # integer tuple
305 a = (7, 8, 9)
306 self.assertEqual(fixture.stringifyIntArrayArg(a), 'int[](7,8,9)')
307
308 with self.assertRaises(RuntimeError) as e:
309 fixture.stringifyIntArrayArg(1 + 2j)
310 self.assertEqual(str(e.exception), 'no matching Java method overloads found')
311
312 def test_ToObjectArrayConversion(self):
313 fixture = self.Fixture()
314
315 self.assertEqual(fixture.stringifyObjectArrayArg(('A', 12, 3.4)), 'Object[](String(A),Byte(12),Double(3.4))')
316 self.assertEqual(fixture.stringifyObjectArrayArg(['A', 12, 3.4]), 'Object[](String(A),Byte(12),Double(3.4))')
317
318 self.assertEqual(fixture.stringifyStringArrayArg(('A', 'B', 'C')), 'String[](String(A),String(B),String(C))')
319 self.assertEqual(fixture.stringifyStringArrayArg(['A', 'B', 'C']), 'String[](String(A),String(B),String(C))')
320
321 def test_ToBooleanConversion(self):
322 java_boolean_type = jpy.get_type("java.lang.Boolean")
323 expected_type = jpy.get_type('java.lang.Boolean')
324
325 jobj = jpy.convert(['abc'], java_boolean_type)
326 self.assertTrue(type(jobj).jclass.equals(java_boolean_type.jclass), f'Type is {type(jobj)}')
327 self.assertTrue(jobj.getClass().equals(expected_type.jclass), f'Type is {jobj.getClass()}')
328 self.assertEqual(jpy.cast(jobj, expected_type).booleanValue(), True)
329
330 jobj = jpy.convert([], java_boolean_type)
331 self.assertTrue(type(jobj).jclass.equals(java_boolean_type.jclass), f'Type is {type(jobj)}')
332 self.assertTrue(jobj.getClass().equals(expected_type.jclass), f'Type is {jobj.getClass()}')
333 self.assertEqual(jpy.cast(jobj, expected_type).booleanValue(), False)
334
335 try:
336 import numpy as np
337 except ImportError:
338 return
339
340 ba = np.array([True, False])
341 jobj = jpy.convert(ba[0], java_boolean_type)
342 self.assertEqual(jpy.cast(jobj, expected_type).booleanValue(), True)
343 jobj = jpy.convert(ba[1], java_boolean_type)
344 self.assertEqual(jpy.cast(jobj, expected_type).booleanValue(), False)
345 jobj = jpy.convert(np.bool_(True), java_boolean_type)
346 self.assertEqual(jpy.cast(jobj, expected_type).booleanValue(), True)
347 jobj = jpy.convert(np.bool_(False), java_boolean_type)
348 self.assertEqual(jpy.cast(jobj, expected_type).booleanValue(), False)
349
350
351 if __name__ == '__main__':
352 print('\nRunning ' + __file__)
353 unittest.main()