Debian Codegraph @

test/jpypetest/test_classhints.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 common
20 import sys
21
22
23 class MyImpl(object):
24 def blah(self):
25 pass
26
27
28 class ClassProxy:
29 def __init__(self, proxy):
30 self.proxy = proxy
31
32
33 class ArrayProxy:
34 def __init__(self, proxy):
35 self.proxy = proxy
36
37
38 class StringProxy:
39 def __init__(self, proxy):
40 self.proxy = proxy
41
42
43 class ClassHintsTestCase(common.JPypeTestCase):
44
45 def setUp(self):
46 common.JPypeTestCase.setUp(self)
47 self.Custom = jpype.JClass("jpype.classhints.Custom")
48 self.ClassHintsTest = jpype.JClass("jpype.classhints.ClassHintsTest")
49
50 @jpype.JImplements("jpype.classhints.Custom")
51 class MyCustom(object):
52 def __init__(self, arg):
53 self.arg = arg
54 self.MyCustom = MyCustom
55
56 def testCharSequence(self):
57 Instant = jpype.JClass("java.time.Instant")
58 s = "2019-12-21T05:26:13.223189Z"
59 self.assertTrue(str(Instant.parse(s)), s)
60
61 def testInstant(self):
62 import datetime
63 if sys.version_info.major == 3 and sys.version_info.minor < 12:
64 now = datetime.datetime.utcnow()
65 else:
66 now = datetime.datetime.now(datetime.UTC)
67 Instant = jpype.JClass("java.time.Instant")
68 self.assertIsInstance(jpype.JObject(now, Instant), Instant)
69
70 def testPath(self):
71 import pathlib
72 JPath = jpype.JClass("java.nio.file.Path")
73 self.assertIsInstance(jpype.JObject(
74 pathlib.Path(__file__).absolute(), JPath), JPath)
75
76 def testFile(self):
77 import pathlib
78 JFile = jpype.JClass("java.io.File")
79 self.assertIsInstance(jpype.JObject(
80 pathlib.Path(__file__).absolute(), JFile), JFile)
81
82 def testConvertExact(self):
83 cht = self.ClassHintsTest
84 with self.assertRaises(TypeError):
85 cht.call("hello")
86
87 @jpype.JConversion(self.Custom, exact=str)
88 def StrToCustom(jcls, args):
89 return self.MyCustom(args)
90
91 cht.call("hello")
92 self.assertIsInstance(cht.input, self.MyCustom)
93 self.assertEqual(cht.input.arg, "hello")
94
95 def testConvertAttribute(self):
96 cht = self.ClassHintsTest
97 with self.assertRaises(TypeError):
98 cht.call(MyImpl())
99
100 @jpype.JConversion(self.Custom, attribute="blah")
101 def StrToCustom(jcls, args):
102 return self.MyCustom(args)
103
104 cht.call(MyImpl())
105 self.assertIsInstance(cht.input, self.MyCustom)
106 self.assertIsInstance(cht.input.arg, MyImpl)
107
108 def testClassCustomizer(self):
109
110 @jpype.JConversion("java.lang.Class", instanceof=ClassProxy)
111 def ClassCustomizer(jcls, obj):
112 return obj.proxy
113
114 hints = jpype.JClass('java.lang.Class')._hints
115 self.assertTrue(ClassProxy in hints.implicit)
116
117 def testArrayCustomizer(self):
118
119 @jpype.JConversion(jpype.JInt[:], instanceof=ArrayProxy)
120 def ArrayCustomizer(jcls, obj):
121 return obj.proxy
122
123 hints = jpype.JClass(jpype.JInt[:])._hints
124 self.assertTrue(ArrayProxy in hints.implicit)
125
126 def testStringCustomizer(self):
127
128 @jpype.JConversion("java.lang.String", instanceof=StringProxy)
129 def STringCustomizer(jcls, obj):
130 return obj.proxy
131
132 hints = jpype.JClass("java.lang.String")._hints
133 self.assertTrue(StringProxy in hints.implicit)