File: test_jobject.py

package info (click to toggle)
python-jpype 1.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,984 kB
  • sloc: python: 18,767; cpp: 17,931; java: 8,448; xml: 1,305; makefile: 154; sh: 35
file content (310 lines) | stat: -rw-r--r-- 11,379 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# *****************************************************************************
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
#   See NOTICE file for details.
#
# *****************************************************************************
import _jpype
import jpype
import _jpype
from jpype.types import *
from jpype import java
import common
try:
    import numpy as np
except ImportError:
    pass


class JClassTestCase(common.JPypeTestCase):
    """ Test for methods of JObject

    Should test:
    - ``__getattribute__`` against methods, fields, python methods, and python properties
    - ``__setattr__`` against fields, final fields, python private fields
    - ``class_`` property
    - ``mro``
    """

    def setUp(self):
        common.JPypeTestCase.setUp(self)
        self.fixture = JClass('jpype.common.Fixture')()

    def testSetAttrPythonField(self):
        cls = JClass('java.lang.String')
        obj = cls('foo')
        # Setting a private field on a Java class is allowed
        obj._allowed = 1
        with self.assertRaises(AttributeError):
            # Setting a public field on a Java class is forbidden
            obj.forbidden = 1

    def testSetAttrFinal(self):
        cls = JClass('java.lang.Long')
        obj = cls(1)
        with self.assertRaises(AttributeError):
            # Setting a final field is forbidden
            obj.SIZE = 1

    def testClass(self):
        obj = JClass('java.lang.Long')
        clsType = JClass('java.lang.Class')
        # Get class must return a java.lang.Class instance belonging to the class
        self.assertIsInstance(obj.class_, clsType)
        self.assertEqual(obj.class_.getSimpleName(), "Long")

    def testGetAttrProperty(self):
        obj = JClass('java.lang.RuntimeException')('oo')
        value = obj.args
        self.assertEqual(value, ('oo',))

    def testSetAttrProperty(self):
        obj = JClass('java.lang.RuntimeException')('oo')
        with self.assertRaises(AttributeError):
            obj.args = 1

    def testAttrStaticField(self):
        self.fixture.static_object_field = "fred"
        self.assertEqual(self.fixture.static_object_field, "fred")

    def testGetAttrField(self):
        v = self.fixture.object_field

    def testSetAttrField(self):
        self.fixture.object_field = "fred"

    def testGetAttrPrivateField(self):
        with self.assertRaises(AttributeError):
            v = self.fixture.private_object_field

    def testSetAttrPrivateField(self):
        with self.assertRaises(AttributeError):
            self.fixture.private_object_field = "fred"

    def testGetAttrFinalField(self):
        v = self.fixture.final_object_field

    def testSetAttrFinalField(self):
        with self.assertRaises(AttributeError):
            self.fixture.final_object_field = "fred"

    def testGetAttrStaticFinalField(self):
        self.assertEqual(self.fixture.final_static_object_field,
                         "final static object field")

    def testSetAttrStaticFinalField(self):
        with self.assertRaises(AttributeError):
            self.fixture.finalStaticObjectField = "bar"

    def testStaticMethod(self):
        self.fixture.callStaticObject(JObject())

    def testPrivateStaticMethod(self):
        with self.assertRaises(AttributeError):
            self.fixture.callPrivateStaticObject(JObject())

    def testMethod(self):
        self.fixture.callObject(JObject())

    def testPrivateMethod(self):
        with self.assertRaises(AttributeError):
            self.fixture.callPrivateObject(JObject())

    def testProtectedMethod(self):
        with self.assertRaises(AttributeError):
            self.fixture.callProtectedObject(JObject())

    def testObjectBoolTrue(self):
        self.fixture.object_field = True
        self.assertIsInstance(self.fixture.object_field,
                              JClass('java.lang.Boolean'))
        self.assertEqual(str(self.fixture.object_field), str(True))
        self.assertEqual(self.fixture.object_field, True)

    def testObjectBoolFalse(self):
        self.fixture.object_field = False
        self.assertIsInstance(self.fixture.object_field,
                              JClass('java.lang.Boolean'))
        self.assertEqual(str(self.fixture.object_field), str(False))
        self.assertEqual(self.fixture.object_field, False)

    def testObjectBoolJValue(self):
        self.fixture.object_field = JBoolean(True)
        self.assertIsInstance(self.fixture.object_field,
                              JClass('java.lang.Boolean'))
        self.assertEqual(self.fixture.object_field, True)

    def testObjectShort(self):
        self.fixture.object_field = JShort(1)
        self.assertEqual(self.fixture.object_field, 1)
        self.assertIsInstance(self.fixture.object_field,
                              JClass('java.lang.Short'))

    def testObjectInteger(self):
        self.fixture.object_field = JInt(2)
        self.assertEqual(self.fixture.object_field, 2)
        self.assertIsInstance(self.fixture.object_field,
                              JClass('java.lang.Integer'))

    def testObjectLong(self):
        self.fixture.object_field = JLong(3)
        self.assertEqual(self.fixture.object_field, 3)
        self.assertIsInstance(self.fixture.object_field,
                              JClass('java.lang.Long'))

    def testObjectFloat(self):
        self.fixture.object_field = JFloat(1.125)
        self.assertEqual(self.fixture.object_field, 1.125)
        self.assertIsInstance(self.fixture.object_field,
                              JClass('java.lang.Float'))

    def testObjectDouble(self):
        self.fixture.object_field = JDouble(2.6125)
        self.assertEqual(self.fixture.object_field, 2.6125)
        self.assertIsInstance(self.fixture.object_field,
                              JClass('java.lang.Double'))

    def testObjectField(self):
        with self.assertRaises(TypeError):
            self.fixture.object_field = object()
        with self.assertRaises(TypeError):
            self.fixture.static_object_field = object()

    def testArraySetRangeFail(self):
        ja = JArray(JObject)(4)
        with self.assertRaises(TypeError):
            ja[:] = [1, 2, object(), 3]
        ja[:] = [1, 2, 3, 4]

    @common.requireInstrumentation
    def testArraySetRangeFault(self):
        _jpype.fault("JPClass::setArrayRange")
        ja = JArray(JObject)(4)
        with self.assertRaisesRegex(SystemError, "fault"):
            ja[:] = [1, 2, 3, 4]

    def testAssignClass(self):
        self.fixture.object_field = JClass("java.lang.StringBuilder")
        self.assertIsInstance(self.fixture.object_field, jpype.java.lang.Class)
        self.assertEqual(self.fixture.object_field,
                         JClass("java.lang.StringBuilder"))

    @common.requireInstrumentation
    def testSetFinalField(self):
        _jpype.fault("JPField::setStaticAttribute")
        with self.assertRaisesRegex(SystemError, "fault"):
            self.fixture.static_object_field = None

    def testHashNone(self):
        self.assertEqual(hash(JObject(None)), hash(None))

    def testStrPrimitive(self):
        with self.assertRaisesRegex(TypeError, "requires a Java object"):
            _jpype._JObject.__str__(JInt(1))

    def testGetAttrFail(self):
        jo = JClass("java.lang.Object")()
        with self.assertRaisesRegex(TypeError, "must be string"):
            getattr(jo, object())

    def testSetAttrFail(self):
        jo = JClass("java.lang.Object")()
        with self.assertRaisesRegex(TypeError, "must be string"):
            setattr(jo, object(), 1)

    def testSetAttrFail2(self):
        fixture = JClass("jpype.common.Fixture")()
        with self.assertRaisesRegex(AttributeError, "is not settable"):
            setattr(fixture, "callObject", 4)

    def testJavaPrimitives(self):
        self.assertIsInstance(
            self.fixture.callObject(JByte(1)), java.lang.Byte)
        self.assertIsInstance(
            self.fixture.callObject(JShort(1)), java.lang.Short)
        self.assertIsInstance(
            self.fixture.callObject(JInt(1)), java.lang.Integer)
        self.assertIsInstance(
            self.fixture.callObject(JLong(1)), java.lang.Long)
        self.assertIsInstance(
            self.fixture.callObject(JFloat(1)), java.lang.Float)
        self.assertIsInstance(self.fixture.callObject(
            JDouble(1)), java.lang.Double)

    def testPythonPrimitives(self):
        self.assertIsInstance(self.fixture.callObject(1), java.lang.Long)
        self.assertIsInstance(self.fixture.callObject(1.0), java.lang.Double)

    @common.requireNumpy
    def testNumpyPrimitives(self):
        self.assertIsInstance(
            self.fixture.callObject(np.int8(1)), java.lang.Byte)
        self.assertIsInstance(self.fixture.callObject(
            np.int16(1)), java.lang.Short)
        self.assertIsInstance(self.fixture.callObject(
            np.int32(1)), java.lang.Integer)
        self.assertIsInstance(self.fixture.callObject(
            np.int64(1)), java.lang.Long)
        self.assertIsInstance(self.fixture.callObject(
            np.float32(1)), java.lang.Float)
        self.assertIsInstance(self.fixture.callObject(
            np.float64(1)), java.lang.Double)

    def testCompare(self):
        jo = JClass("java.lang.Object")()
        with self.assertRaises(TypeError):
            jo < 0
        with self.assertRaises(TypeError):
            jo <= 0
        with self.assertRaises(TypeError):
            jo > 0
        with self.assertRaises(TypeError):
            jo >= 0

    def testCompareNull(self):
        jo = JClass("java.lang.Object")
        jv = JObject(None, jo)
        self.assertEqual(jv, None)
        self.assertEqual(None, jv)
        self.assertNotEqual(JInt(1), jv)
        self.assertNotEqual(jv, JInt(1))

    def testRepr(self):
        jo = JClass("java.lang.Object")
        jv = jo()
        jvn = JObject(None, jo)
        self.assertIsInstance(repr(jv), str)
        self.assertIsInstance(repr(jvn), str)
        self.assertEqual(repr(jv), "<java object 'java.lang.Object'>")
        self.assertEqual(repr(jvn), "<java object 'java.lang.Object'>")

    def testDeprecated(self):
        # this one should issue a warning
        jo = JClass("java.lang.Object")
        self.assertIsInstance(JObject(None, object), jo)

    def testGetSetBad(self):
        JS = JClass("java.lang.String")
        js = JS()
        with self.assertRaises(TypeError):
            JS.__getattribute__(js, object())
        with self.assertRaises(TypeError):
            setattr(js, object(), 1)

    def testGetSetBad_2(self):
        jo = JClass("java.lang.Object")()
        self.assertTrue(jo != JInt(0))
        self.assertFalse(jo == JInt(0))
        self.assertTrue(JInt(0) != jo)
        self.assertFalse(JInt(0) == jo)