Debian Codegraph @

test/jpypetest/test_hints.py

raw symbols SCIP
1 # *****************************************************************************
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15 # See NOTICE file for details.
16 #
17 # *****************************************************************************
18 import _jpype
19 import jpype
20 from jpype import java
21 from jpype.types import *
22 from jpype import JConversion
23 import common
24 import jpype.protocol as proto
25
26
27 class HintsTestCase(common.JPypeTestCase):
28
29 def testCache(self):
30 cls = JClass('java.lang.Object')
31 hints = cls._hints
32 hints2 = cls._hints
33 self.assertEqual(hints, hints2)
34
35 def testProtocol(self):
36 protocol = jpype.protocol.SupportsFloat
37 annot = protocol.__float__.__annotations__
38 self.assertEqual(annot['return'], float)
39 protocol = jpype.protocol.SupportsIndex
40 annot = protocol.__index__.__annotations__
41 self.assertEqual(annot['return'], int)
42 self.assertTrue(hasattr(proto, "Sequence"))
43 self.assertTrue(hasattr(proto, "Mapping"))
44 self.assertTrue(hasattr(proto, "Protocol"))
45
46 def testObject(self):
47 cls = JClass('java.lang.Object')
48 hints = cls._hints
49 self.assertTrue(cls in hints.returns)
50 self.assertTrue(cls in hints.exact)
51 self.assertTrue(str in hints.implicit)
52 self.assertTrue(bool in hints.implicit)
53 self.assertTrue(proto.SupportsIndex in hints.implicit)
54 self.assertTrue(proto.SupportsFloat in hints.implicit)
55 self.assertTrue(proto._JClass in hints.implicit)
56 self.assertTrue(len(hints.explicit) == 0)
57 self.assertTrue(len(hints.none) == 0)
58
59 def testNumber(self):
60 cls = JClass('java.lang.Number')
61 hints = cls._hints
62 self.assertTrue(cls in hints.returns)
63 self.assertTrue(proto._JNumberLong in hints.implicit)
64 self.assertTrue(proto._JNumberFloat in hints.implicit)
65 self.assertTrue(proto.SupportsIndex in hints.implicit)
66 self.assertTrue(proto.SupportsFloat in hints.implicit)
67 self.assertTrue(len(hints.explicit) == 0)
68 self.assertTrue(len(hints.none) == 0)
69
70 def testString(self):
71 cls = JClass('java.lang.String')
72 hints = cls._hints
73 self.assertTrue(cls in hints.returns)
74 self.assertTrue(cls in hints.exact)
75 self.assertTrue(str in hints.implicit)
76 self.assertTrue(len(hints.explicit) == 0)
77 self.assertTrue(len(hints.none) == 0)
78
79 def testClass(self):
80 cls = JClass('java.lang.Class')
81 hints = cls._hints
82 self.assertTrue(cls in hints.returns)
83 self.assertTrue(cls in hints.exact)
84 self.assertTrue(proto._JClass in hints.implicit)
85 self.assertTrue(len(hints.explicit) == 0)
86 self.assertTrue(len(hints.none) == 0)
87
88 def testCollection(self):
89 cls = JClass('java.util.Collection')
90 hints = cls._hints
91 self.assertTrue(cls in hints.returns)
92 self.assertTrue(cls in hints.exact)
93 self.assertTrue(proto.Sequence in hints.implicit)
94 self.assertTrue(len(hints.explicit) == 0)
95 self.assertTrue(str in hints.none)
96
97 def testList(self):
98 cls = JClass('java.util.List')
99 hints = cls._hints
100 self.assertTrue(cls in hints.returns)
101 self.assertTrue(cls in hints.exact)
102 self.assertTrue(len(hints.implicit) == 0)
103 self.assertTrue(len(hints.explicit) == 0)
104 self.assertTrue(len(hints.none) == 0)
105
106 def testJBoolean(self):
107 cls = JBoolean
108 hints = cls._hints
109 self.assertTrue(bool in hints.returns)
110 self.assertTrue(bool in hints.exact)
111 self.assertTrue(cls in hints.exact)
112 self.assertTrue(JClass("java.lang.Boolean") in hints.implicit)
113 self.assertTrue(proto.SupportsIndex in hints.explicit)
114 self.assertTrue(proto.SupportsFloat in hints.explicit)
115 self.assertTrue(len(hints.none) == 0)
116
117 def testJChar(self):
118 cls = JChar
119 hints = cls._hints
120 self.assertTrue(cls in hints.returns)
121 self.assertTrue(cls in hints.exact)
122 self.assertTrue(JClass("java.lang.Character") in hints.implicit)
123 self.assertTrue(str in hints.implicit)
124 self.assertTrue(len(hints.explicit) == 0)
125 self.assertTrue(len(hints.none) == 0)
126
127 def testJShort(self):
128 cls = JShort
129 hints = cls._hints
130 self.assertTrue(cls in hints.returns)
131 self.assertTrue(cls in hints.exact)
132 self.assertTrue(JByte in hints.implicit)
133 self.assertTrue(JChar in hints.implicit)
134 self.assertTrue(JClass("java.lang.Short") in hints.implicit)
135 self.assertTrue(proto.SupportsIndex in hints.implicit)
136 self.assertTrue(proto.SupportsFloat in hints.explicit)
137 self.assertTrue(len(hints.none) == 0)
138
139 def testJInt(self):
140 cls = JInt
141 hints = cls._hints
142 self.assertTrue(cls in hints.returns)
143 self.assertTrue(cls in hints.exact)
144 self.assertTrue(JByte in hints.implicit)
145 self.assertTrue(JChar in hints.implicit)
146 self.assertTrue(JShort in hints.implicit)
147 self.assertTrue(JClass('java.lang.Integer') in hints.implicit)
148 self.assertTrue(proto.SupportsIndex in hints.implicit)
149 self.assertTrue(proto.SupportsFloat in hints.explicit)
150 self.assertTrue(len(hints.none) == 0)
151
152 def testJLong(self):
153 cls = JLong
154 hints = cls._hints
155 self.assertTrue(cls in hints.returns)
156 self.assertTrue(cls in hints.exact)
157 self.assertTrue(JByte in hints.implicit)
158 self.assertTrue(JChar in hints.implicit)
159 self.assertTrue(JShort in hints.implicit)
160 self.assertTrue(JInt in hints.implicit)
161 self.assertTrue(JClass('java.lang.Long') in hints.implicit)
162 self.assertTrue(proto.SupportsIndex in hints.implicit)
163 self.assertTrue(proto.SupportsFloat in hints.explicit)
164 self.assertTrue(len(hints.none) == 0)
165
166 def testJFloat(self):
167 cls = JFloat
168 hints = cls._hints
169 self.assertTrue(cls in hints.returns)
170 self.assertTrue(cls in hints.exact)
171 self.assertTrue(JByte in hints.implicit)
172 self.assertTrue(JChar in hints.implicit)
173 self.assertTrue(JShort in hints.implicit)
174 self.assertTrue(JInt in hints.implicit)
175 self.assertTrue(JLong in hints.implicit)
176 self.assertTrue(JClass('java.lang.Float') in hints.implicit)
177 self.assertTrue(int in hints.implicit)
178 self.assertTrue(proto.SupportsIndex in hints.implicit)
179 self.assertTrue(proto.SupportsFloat in hints.implicit)
180 self.assertTrue(len(hints.explicit) == 0)
181 self.assertTrue(len(hints.none) == 0)
182
183 def testJDouble(self):
184 cls = JDouble
185 hints = cls._hints
186 self.assertTrue(cls in hints.returns)
187 self.assertTrue(cls in hints.exact)
188 self.assertTrue(JByte in hints.implicit)
189 self.assertTrue(JChar in hints.implicit)
190 self.assertTrue(JShort in hints.implicit)
191 self.assertTrue(JInt in hints.implicit)
192 self.assertTrue(JLong in hints.implicit)
193 self.assertTrue(JFloat in hints.implicit)
194 self.assertTrue(JClass('java.lang.Double') in hints.implicit)
195 self.assertTrue(proto.SupportsIndex in hints.implicit)
196 self.assertTrue(proto.SupportsFloat in hints.implicit)
197 self.assertTrue(int in hints.implicit)
198 self.assertTrue(len(hints.explicit) == 0)
199 self.assertTrue(len(hints.none) == 0)
200
201 def testBoxedByte(self):
202 cls = JClass('java.lang.Byte')
203 hints = cls._hints
204 self.assertTrue(cls in hints.returns)
205 self.assertTrue(cls in hints.exact)
206 self.assertTrue(JByte in hints.implicit)
207 self.assertTrue(proto.SupportsFloat in hints.explicit)
208 self.assertTrue(JClass('java.lang.Byte') in hints.explicit)
209 self.assertTrue(proto.SupportsIndex in hints.explicit)
210 self.assertTrue(len(hints.none) == 0)
211
212 def testBoxedBoolean(self):
213 cls = JClass('java.lang.Boolean')
214 hints = cls._hints
215 self.assertTrue(cls in hints.returns)
216 self.assertTrue(cls in hints.exact)
217 self.assertTrue(bool in hints.implicit)
218 self.assertTrue(JBoolean in hints.implicit)
219 self.assertTrue(proto.SupportsIndex in hints.explicit)
220 self.assertTrue(proto.SupportsFloat in hints.explicit)
221 self.assertTrue(JClass('java.lang.Boolean') in hints.explicit)
222 self.assertTrue(len(hints.none) == 0)
223
224 def testBoxedCharacter(self):
225 cls = JClass('java.lang.Character')
226 hints = cls._hints
227 self.assertTrue(cls in hints.returns)
228 self.assertTrue(cls in hints.exact)
229 self.assertTrue(JChar in hints.implicit)
230 self.assertTrue(JClass('java.lang.Character') in hints.explicit)
231 self.assertTrue(str in hints.explicit)
232 self.assertTrue(len(hints.none) == 0)
233
234 def testBoxedShort(self):
235 cls = JClass('java.lang.Short')
236 hints = cls._hints
237 self.assertTrue(cls in hints.returns)
238 self.assertTrue(cls in hints.exact)
239 self.assertTrue(JShort in hints.implicit)
240 self.assertTrue(proto.SupportsFloat in hints.explicit)
241 self.assertTrue(JByte in hints.explicit)
242 self.assertTrue(JChar in hints.explicit)
243 self.assertTrue(JClass('java.lang.Short') in hints.explicit)
244 self.assertTrue(proto.SupportsIndex in hints.explicit)
245 self.assertTrue(len(hints.none) == 0)
246
247 def testBoxedInteger(self):
248 cls = JClass('java.lang.Integer')
249 hints = cls._hints
250 self.assertTrue(cls in hints.returns)
251 self.assertTrue(cls in hints.exact)
252 self.assertTrue(JInt in hints.implicit)
253 self.assertTrue(proto.SupportsFloat in hints.explicit)
254 self.assertTrue(JByte in hints.explicit)
255 self.assertTrue(JChar in hints.explicit)
256 self.assertTrue(JShort in hints.explicit)
257 self.assertTrue(JClass('java.lang.Integer') in hints.explicit)
258 self.assertTrue(proto.SupportsIndex in hints.explicit)
259 self.assertTrue(len(hints.none) == 0)
260
261 def testBoxedLong(self):
262 cls = JClass('java.lang.Long')
263 hints = cls._hints
264 self.assertTrue(cls in hints.returns)
265 self.assertTrue(cls in hints.exact)
266 self.assertTrue(JLong in hints.implicit)
267 self.assertTrue(proto.SupportsFloat in hints.explicit)
268 self.assertTrue(JByte in hints.explicit)
269 self.assertTrue(JChar in hints.explicit)
270 self.assertTrue(JShort in hints.explicit)
271 self.assertTrue(JInt in hints.explicit)
272 self.assertTrue(JClass('java.lang.Long') in hints.explicit)
273 self.assertTrue(proto.SupportsIndex in hints.explicit)
274 self.assertTrue(len(hints.none) == 0)
275
276 def testBoxedFloat(self):
277 cls = JClass('java.lang.Float')
278 hints = cls._hints
279 self.assertTrue(cls in hints.returns)
280 self.assertTrue(cls in hints.exact)
281 self.assertTrue(JFloat in hints.implicit)
282 self.assertTrue(JByte in hints.explicit)
283 self.assertTrue(JChar in hints.explicit)
284 self.assertTrue(JShort in hints.explicit)
285 self.assertTrue(JInt in hints.explicit)
286 self.assertTrue(JLong in hints.explicit)
287 self.assertTrue(JClass('java.lang.Float') in hints.explicit)
288 self.assertTrue(int in hints.explicit)
289 self.assertTrue(proto.SupportsIndex in hints.explicit)
290 self.assertTrue(proto.SupportsFloat in hints.explicit)
291 self.assertTrue(len(hints.none) == 0)
292
293 def testBoxedDouble(self):
294 cls = JClass('java.lang.Double')
295 hints = cls._hints
296 self.assertTrue(cls in hints.returns)
297 self.assertTrue(cls in hints.exact)
298 self.assertTrue(JDouble in hints.implicit)
299 self.assertTrue(JByte in hints.explicit)
300 self.assertTrue(JChar in hints.explicit)
301 self.assertTrue(JShort in hints.explicit)
302 self.assertTrue(JInt in hints.explicit)
303 self.assertTrue(JLong in hints.explicit)
304 self.assertTrue(JFloat in hints.explicit)
305 self.assertTrue(JClass('java.lang.Double') in hints.explicit)
306 self.assertTrue(proto.SupportsIndex in hints.explicit)
307 self.assertTrue(proto.SupportsFloat in hints.explicit)
308 self.assertTrue(int in hints.explicit)
309 self.assertTrue(len(hints.none) == 0)
310
311 def testObjectArray(self):
312 cls = JClass('java.lang.Object[]')
313 hints = cls._hints
314 self.assertTrue(cls in hints.returns)
315 self.assertTrue(cls in hints.exact)
316 self.assertTrue(proto.Sequence in hints.implicit)
317 self.assertTrue(len(hints.explicit) == 0)
318 self.assertTrue(str in hints.none)
319
320 def testBooleanArray(self):
321 cls = JClass('boolean[]')
322 hints = cls._hints
323 self.assertTrue(cls in hints.returns)
324 self.assertTrue(cls in hints.exact)
325 self.assertTrue(proto.Sequence in hints.implicit)
326 self.assertTrue(len(hints.explicit) == 0)
327 self.assertTrue(str in hints.none)
328
329 def testCharArray(self):
330 cls = JClass('char[]')
331 hints = cls._hints
332 self.assertTrue(cls in hints.returns)
333 self.assertTrue(cls in hints.exact)
334 self.assertTrue(str in hints.implicit)
335 self.assertTrue(proto.Sequence in hints.implicit)
336 self.assertTrue(len(hints.explicit) == 0)
337 self.assertTrue(len(hints.none) == 0)
338
339 def testShortArray(self):
340 cls = JClass('short[]')
341 hints = cls._hints
342 self.assertTrue(cls in hints.returns)
343 self.assertTrue(cls in hints.exact)
344 self.assertTrue(proto.Sequence in hints.implicit)
345 self.assertTrue(len(hints.explicit) == 0)
346 self.assertTrue(str in hints.none)
347
348 def testIntArray(self):
349 cls = JClass('int[]')
350 hints = cls._hints
351 self.assertTrue(cls in hints.returns)
352 self.assertTrue(cls in hints.exact)
353 self.assertTrue(proto.Sequence in hints.implicit)
354 self.assertTrue(len(hints.explicit) == 0)
355 self.assertTrue(str in hints.none)
356
357 def testLongArray(self):
358 cls = JClass('long[]')
359 hints = cls._hints
360 self.assertTrue(cls in hints.returns)
361 self.assertTrue(cls in hints.exact)
362 self.assertTrue(proto.Sequence in hints.implicit)
363 self.assertTrue(len(hints.explicit) == 0)
364 self.assertTrue(str in hints.none)
365
366 def testFloatArray(self):
367 cls = JClass('float[]')
368 hints = cls._hints
369 self.assertTrue(cls in hints.returns)
370 self.assertTrue(cls in hints.exact)
371 self.assertTrue(proto.Sequence in hints.implicit)
372 self.assertTrue(len(hints.explicit) == 0)
373 self.assertTrue(str in hints.none)
374
375 def testDoubleArray(self):
376 cls = JClass('double[]')
377 hints = cls._hints
378 self.assertTrue(cls in hints.returns)
379 self.assertTrue(cls in hints.exact)
380 self.assertTrue(proto.Sequence in hints.implicit)
381 self.assertTrue(len(hints.explicit) == 0)
382 self.assertTrue(str in hints.none)
383
384 def testPath(self):
385 cls = JClass('java.nio.file.Path')
386 hints = cls._hints
387 self.assertTrue(cls in hints.returns)
388 self.assertTrue(cls in hints.exact)
389 self.assertTrue(proto.SupportsPath in hints.implicit)
390 self.assertTrue(len(hints.explicit) == 0)
391 self.assertTrue(len(hints.none) == 0)
392
393 def testFile(self):
394 cls = JClass('java.io.File')
395 hints = cls._hints
396 self.assertTrue(cls in hints.returns)
397 self.assertTrue(cls in hints.exact)
398 self.assertTrue(proto.SupportsPath in hints.implicit)
399 self.assertTrue(len(hints.explicit) == 0)
400 self.assertTrue(len(hints.none) == 0)
401
402 def testInstant(self):
403 import datetime
404 cls = JClass('java.time.Instant')
405 hints = cls._hints
406 self.assertTrue(cls in hints.returns)
407 self.assertTrue(cls in hints.exact)
408 self.assertTrue(datetime.datetime in hints.implicit)
409 self.assertTrue(len(hints.explicit) == 0)
410 self.assertTrue(len(hints.none) == 0)
411
412 def testDate(self):
413 cls = JClass("java.sql.Date")
414 d = cls(120, 0, 5)
415 d2 = d._py()
416 d3 = JObject(d2, cls)
417 self.assertEqual(d, d2)
418 self.assertEqual(d, d3)
419
420 def testTimestamp(self):
421 cls = JClass("java.sql.Timestamp")
422 d = cls(120, 0, 5, 9, 22, 51, 123456000)
423 d2 = d._py()
424 d3 = JObject(d2, cls)
425 self.assertEqual(d, d2)
426 self.assertEqual(d, d3)
427
428 def testTime(self):
429 cls = JClass("java.sql.Time")
430 d = cls(11, 53, 1)
431 d2 = d._py()
432 d3 = JObject(d2, cls)
433 self.assertEqual(d, d2)
434 self.assertEqual(d, d3)
435
436 def testBigDecimal(self):
437 cls = JClass("java.math.BigDecimal")
438 d = cls('1000234600000000000000')
439 d2 = d._py()
440 d3 = JObject(d2, cls)
441 self.assertEqual(d, d2)
442 self.assertEqual(d, d3)
443
444 def testAddTypeBad(self):
445 cls = JClass('java.lang.Object')
446 with self.assertRaises(TypeError):
447 cls._hints._addTypeConversion()
448 with self.assertRaisesRegex(TypeError, "callable method is required"):
449 cls._hints._addTypeConversion(object, object(), True)
450 with self.assertRaisesRegex(TypeError, "type or protocol is required"):
451 def foo():
452 pass
453 cls._hints._addTypeConversion(object(), foo, True)
454
455 def testExcludeBad(self):
456 cls = JClass('java.lang.Object')
457 with self.assertRaisesRegex(TypeError, "type or protocol is required, not 'object'"):
458 cls._hints._excludeConversion(object())
459 with self.assertRaisesRegex(TypeError, "type or protocol is required, not 'object'"):
460 cls._hints._excludeConversion((object(),))
461
462 def testClassToClass(self):
463 fixture = JClass("jpype.common.Fixture")()
464 @JConversion(JByte[:], instanceof=java.lang.String)
465 def ToByteArray(cls, obj):
466 return obj.getBytes()
467 obj1 = java.lang.String("hello")
468 fixture.callByteArray(obj1)
469