Debian Codegraph @

test/jpypetest/test_jclass.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.types import *
21 import common
22
23
24 class JClassTestCase(common.JPypeTestCase):
25 """ Test for methods of JClass
26
27 Should test:
28 - ``__getattribute__`` against methods, fields, python methods, and python properties
29 - ``__setattr__`` against fields, final fields, python private fields
30 - ``class_`` property
31 - ``mro``
32 """
33
34 def setUp(self):
35 common.JPypeTestCase.setUp(self)
36
37 def testGetAttrPyMethod(self):
38 cls = JClass('java.util.Iterator')
39 obj = JClass('java.util.ArrayList')()
40 obj.add(123)
41 # Static python methods should be the same when accessed as classes or objects
42 self.assertEqual(obj.iterator().next(), cls.next(obj.iterator()))
43
44 def testGetAttrStaticMethod(self):
45 cls = JClass('java.lang.Long')
46 obj = cls(10)
47 # Static java methods should be the same when accessed as classes or objects
48 self.assertEqual(cls.bitCount(123), obj.bitCount(123))
49
50 def testGetAttrStaticField(self):
51 cls = JClass('java.lang.String')
52 obj = cls("foo")
53 # Static fields should be the same when accessed as classes or objects
54 self.assertEqual(cls.CASE_INSENSITIVE_ORDER,
55 obj.CASE_INSENSITIVE_ORDER)
56
57 def testSetAttrPythonField(self):
58 cls = JClass('java.lang.String')
59 # Setting a private field on a Java class is allowed
60 cls._allowed = 1
61 with self.assertRaises(AttributeError):
62 # Setting a public field on a Java class is forbidden
63 cls.forbidden = 1
64
65 def testSetAttrFinal(self):
66 cls = JClass('java.lang.Long')
67 with self.assertRaises(AttributeError):
68 # Setting a final field is forbidden
69 cls.SIZE = 1
70
71 def testClass(self):
72 cls = JClass('java.lang.Long')
73 clsType = JClass('java.lang.Class')
74 # Get class must return a java.lang.Class instance belonging to the class
75 self.assertIsInstance(cls.class_, clsType)
76 self.assertEqual(cls.class_.getSimpleName(), "Long")
77
78 def testGetAttrProperty(self):
79 cls = JClass('java.lang.RuntimeException')
80 with self.assertRaises(AttributeError):
81 value = cls.args
82
83 def testSetAttrProperty(self):
84 cls = JClass('java.lang.RuntimeException')
85 with self.assertRaises(AttributeError):
86 cls.args = 1
87
88 def testGetAttrStaticField_2(self):
89 cls = JClass('jpype.common.Fixture')
90 cls.static_object_field = "fred"
91 self.assertEqual(cls.static_object_field, "fred")
92
93 def testSetAttrStaticField_3(self):
94 cls = JClass('jpype.common.Fixture')
95 cls.static_object_field = "fred"
96
97 def testGetAttrField(self):
98 cls = JClass('jpype.common.Fixture')
99 with self.assertRaises(AttributeError):
100 v = cls.object_field
101
102 def testSetAttrField(self):
103 cls = JClass('jpype.common.Fixture')
104 with self.assertRaises(AttributeError):
105 cls.object_field = "fred"
106
107 def testGetAttrPrivateField(self):
108 cls = JClass('jpype.common.Fixture')
109 with self.assertRaises(AttributeError):
110 v = cls.privateObjectField
111
112 def testSetAttrPrivateField(self):
113 cls = JClass('jpype.common.Fixture')
114 with self.assertRaises(AttributeError):
115 cls.private_object_field = "fred"
116
117 def testGetAttrFinalField(self):
118 cls = JClass('jpype.common.Fixture')
119 with self.assertRaises(AttributeError):
120 v = cls.final_object_field
121
122 def testSetAttrFinalField(self):
123 cls = JClass('jpype.common.Fixture')
124 with self.assertRaises(AttributeError):
125 cls.final_object_field = "fred"
126
127 def testGetAttrStaticFinalField(self):
128 cls = JClass('jpype.common.Fixture')
129 self.assertEqual(cls.final_static_object_field,
130 "final static object field")
131
132 def testSetAttrStaticFinalField(self):
133 cls = JClass('jpype.common.Fixture')
134 with self.assertRaises(AttributeError):
135 cls.final_static_object_field = "bar"
136
137 def testStaticMethod(self):
138 cls = JClass('jpype.common.Fixture')
139 cls.callStaticObject(JObject())
140
141 def testPrivateStaticMethod(self):
142 cls = JClass('jpype.common.Fixture')
143 with self.assertRaises(AttributeError):
144 cls.callPrivateStaticObject(JObject())
145
146 def testMethod(self):
147 cls = JClass('jpype.common.Fixture')
148 with self.assertRaises(TypeError):
149 cls.callObject(JObject())
150 cls.callObject(cls(), JObject())
151
152 def testPrivateMethod(self):
153 cls = JClass('jpype.common.Fixture')
154 with self.assertRaises(AttributeError):
155 cls.callPrivateObject(JObject())
156
157 def testProtectedMethod(self):
158 cls = JClass('jpype.common.Fixture')
159 with self.assertRaises(AttributeError):
160 cls.callProtectedObject(JObject())
161
162 def testJClassFail(self):
163 with self.assertRaises(TypeError):
164 cls = JClass("asdw.gqyr.jhnw")
165
166 def testGetClassFromClass(self):
167 cls = JClass('java.lang.Class')
168 self.assertIsInstance(cls.class_, cls)
169
170 def testGetClassFromInterface(self):
171 intr = JClass('java.io.Serializable')
172 cls = JClass('java.lang.Class')
173 self.assertIsInstance(intr.class_, cls)
174
175 def testInterfaceCtor(self):
176 intr = JClass('java.io.Serializable')
177 with self.assertRaises(TypeError):
178 intr()
179
180 def testJClassWithLoader(self):
181 cl = JClass('java.lang.Class').class_.getClassLoader()
182 self.assertIsInstance(
183 JClass('java.lang.StringBuilder', loader=cl), JClass)
184
185 @common.requireInstrumentation
186 def testJavaConversionFault(self):
187 _jpype.fault("JPClass::findJavaConversion")
188 with self.assertRaisesRegex(SystemError, "fault"):
189 print(jpype.java.lang.Class._canConvertToJava(None))
190
191 def testJavaConversion(self):
192 a = JString("a")
193 self.assertEqual(
194 jpype.java.lang.Class._canConvertToJava(object()), "none")
195 self.assertEqual(
196 jpype.java.lang.Class._canConvertToJava(a.getClass()), "exact")
197 self.assertEqual(
198 jpype.java.lang.Class._canConvertToJava(JString), "exact")
199
200 def testInnerClass(self):
201 # This tests for problems when the inner class implements the
202 # outer interface which creates a race condition. Success is
203 # not throwing an exception
204 test = JClass("jpype.types.InnerTest")()
205 test.test()
206
207 def testLookupGeneric(self):
208 self.assertEqual(JClass('java.util.ArrayList<>'),
209 JClass("java.util.ArrayList"))
210
211 def testLookupJNI(self):
212 self.assertEqual(JClass('java/lang/Object'),
213 JClass("java.lang.Object"))
214
215 def testLookupArray(self):
216 self.assertEqual(JClass('int[]'), JArray(JInt))
217 self.assertEqual(JClass('int[][]'), JArray(JInt, 2))
218 self.assertEqual(JClass('int[][][]'), JArray(JInt, 3))
219 self.assertEqual(JClass('java.lang.Object[][][]'), JArray(JObject, 3))
220
221 def testLookupArrayJNI(self):
222 self.assertEqual(JClass('[I'), JArray(JInt))
223 self.assertEqual(JClass('[[J'), JArray(JLong, 2))
224 self.assertEqual(JClass('[Ljava.lang.Object;'), JArray(JObject))
225 self.assertEqual(JClass('[Ljava/lang/Object;'), JArray(JObject))
226
227 def testClosed(self):
228 with self.assertRaises(TypeError):
229 class Q(JBoolean):
230 pass
231 with self.assertRaises(TypeError):
232 class Q(JChar):
233 pass
234 with self.assertRaises(TypeError):
235 class Q(JByte):
236 pass
237 with self.assertRaises(TypeError):
238 class Q(JShort):
239 pass
240 with self.assertRaises(TypeError):
241 class Q(JInt):
242 pass
243 with self.assertRaises(TypeError):
244 class Q(JLong):
245 pass
246 with self.assertRaises(TypeError):
247 class Q(JFloat):
248 pass
249 with self.assertRaises(TypeError):
250 class Q(JDouble):
251 pass
252
253 def testAsArray(self):
254 jo = JClass('java.lang.Object')
255 self.assertEqual(jo[:], JArray(jo))
256 self.assertEqual(jo[:][:], JArray(jo, 2))
257 self.assertIsInstance(jo[5], JArray(jo))
258 self.assertEqual(len(jo[5]), 5)
259 self.assertIsInstance(jo[5, :], JArray(jo, 2))
260 self.assertEqual(len(jo[5, :]), 5)
261 with self.assertRaises(TypeError):
262 self.assertEqual(jo[:, 5], JArray(jo, 2))
263 with self.assertRaises(TypeError):
264 jo['foo']
265
266 def testCastEqual(self):
267 jo = JClass('java.lang.Object')
268 with self.assertRaises(TypeError):
269 jo @= 5
270
271 def testCanCast(self):
272 String = JClass("java.lang.String")
273 self.assertFalse(String._canCast(1))
274 self.assertTrue(String._canCast("foo"))
275
276 def testUnsatisfied(self):
277 # This is testing what happens if a class fails to load properly due to
278 # failures. A partial loaded class can lead to crashes.
279 with self.assertRaises(JClass("java.lang.NoClassDefFoundError")):
280 JClass("org.jpype.unsatisfied.TestClass")