Debian Codegraph @

test/jpypetest/test_coverage.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.imports
20 import common
21 import sys
22 import os
23 import importlib
24 import pytest
25 from unittest import mock
26 import _jpype
27
28 # Tests just for coverage
29 # These will be moved to the corresponding test file as needed
30
31
32 class CoverageCase(common.JPypeTestCase):
33 def setUp(self):
34 common.JPypeTestCase.setUp(self)
35 self.platform = sys.platform
36
37 def testWin32(self):
38 if sys.platform == "win32":
39 raise common.unittest.SkipTest("not tested on win32")
40 try:
41 sys.platform = "win32"
42 importlib.reload(jpype._classpath)
43 with self.assertRaises(jpype.JVMNotFoundException):
44 jpype.getDefaultJVMPath()
45
46 finally:
47 sys.platform = self.platform
48 importlib.reload(jpype._classpath)
49
50 def testHandleClassPath(self):
51 with self.assertRaises(TypeError):
52 jpype._core._expandClassPath([1])
53 jpype._core._expandClassPath(["./*.jar"])
54
55 def testRestart(self):
56 with self.assertRaises(OSError):
57 jpype.startJVM()
58
59 def testSynchronizeFail(self):
60 with self.assertRaises(TypeError):
61 jpype.synchronized("hello")
62
63 def testJArrayFail1(self):
64 with self.assertRaises(TypeError):
65 jpype.JArray(jpype.JInt, 2, 2)
66
67 def testJArrayFail2(self):
68 with self.assertRaises(TypeError):
69 jpype.JArray(jpype.JInt, 1)(1, 2, 3)
70
71 def testJArrayStr(self):
72 self.assertEqual(str(jpype.JArray(jpype.JInt)([1, 2])), str([1, 2]))
73
74 def testJArrayLength(self):
75 ja = jpype.JArray(jpype.JInt)([1, 2])
76 self.assertEqual(ja.length, len(ja))
77
78 def testJArrayGetItemSlice(self):
79 ja = jpype.JArray(jpype.JInt)([1, 2, 3, 4])
80 assert list(ja[0:2:-1]) == [1, 2, 3, 4][0:2:-1]
81
82 def testJArraySetItemSlice(self):
83 ja = jpype.JArray(jpype.JInt)([1, 2, 3, 4, 5, 6])
84 ja[0:-1:2] = [-1, -1, -1]
85 self.assertEqual(list(ja[:]), [-1, 2, -1, 4, -1, 6])
86 # FIXME ja[0:-1:2] = [-1] works but should not
87 # FIXME ja[0:-1:2] = 1 gives wierd error
88
89 def testJArrayGetItemSlice_2(self):
90 ja = jpype.JArray(jpype.JInt)([1, 2, 3, 4])
91 assert list(ja[1:]) == [2, 3, 4]
92
93 def testJArraySetItemSlice_2(self):
94 ja = jpype.JArray(jpype.JInt)([1, 2, 3, 4])
95 ja[1:] = [3, 4, 5]
96 self.assertEqual(list(ja[:]), [1, 3, 4, 5])
97
98 def testJArrayEQ(self):
99 ja1 = jpype.JArray(jpype.JInt)([1, 2, 3, 4])
100 ja2 = jpype.JArray(jpype.JInt)([1, 2, 3, 4])
101 ja1 == ja2
102 # FIXME: This test is failing, but shouldn't be.
103 # assert ja1 == ja2
104 assert (ja1 == [1, 2]) is False
105
106 def testJArrayNE(self):
107 ja = jpype.JArray(jpype.JInt)([1, 2, 3, 4])
108 assert ja != [1, 2]
109 assert (ja != ja) is False
110
111 def testJArrayIter(self):
112 ja = jpype.JArray(jpype.JInt)([1, 2, 3, 4])
113 for i in ja:
114 pass
115
116 def testJArrayBytes(self):
117 ja = jpype.JArray(jpype.JByte)([65, 66])
118 self.assertEqual(str(ja), "AB")
119
120 def testJArrayBytesFail(self):
121 ja = jpype.JArray(jpype.JByte)([65, 66])
122 self.assertFalse(ja == None)
123
124 def testJBooleanFail(self):
125 with self.assertRaises(TypeError):
126 jpype.java.lang.Boolean(1, 2)
127
128 def testJStringAppend(self):
129 js = jpype.JString("foo")
130 self.assertEqual(js + "bar", "foobar")
131 if not self._convertStrings:
132 self.assertIsInstance(js + "bar", jpype.java.lang.String)
133
134 def testJStringNE(self):
135 js = jpype.JString("foo")
136 self.assertFalse(js != "foo")
137 self.assertFalse(js != jpype.JString("foo"))
138
139 # FIXME review this for slices and other cases, we may need
140 # to improve this one
141 def testJStringGetItem(self):
142 js = jpype.JString("fred")
143 self.assertEqual(js[1], "r")
144
145 def testJStringLen(self):
146 js = jpype.JString("fred")
147 self.assertEqual(len(js), 4)
148
149 def testJStringLT(self):
150 js = jpype.JString("b")
151 self.assertTrue(js < 'c')
152 self.assertFalse(js < 'b')
153 self.assertFalse(js < 'a')
154
155 def testJStringLE(self):
156 js = jpype.JString("b")
157 self.assertTrue(js <= 'c')
158 self.assertTrue(js <= 'b')
159 self.assertFalse(js <= 'a')
160
161 def testJStringGT(self):
162 js = jpype.JString("b")
163 self.assertFalse(js > 'c')
164 self.assertFalse(js > 'b')
165 self.assertTrue(js > 'a')
166
167 def testJStringGE(self):
168 js = jpype.JString("b")
169 self.assertFalse(js >= 'c')
170 self.assertTrue(js >= 'b')
171 self.assertTrue(js >= 'a')
172
173 def testJStringContains(self):
174 js = jpype.JString("fred")
175 self.assertTrue("r" in js)
176 self.assertFalse("g" in js)
177
178 def testJStringRepr(self):
179 js = jpype.JString("fred")
180 self.assertTrue(repr(js), "fred")
181
182 # def testSetResourceFail(self):
183 # with self.assertRaises(RuntimeError):
184 # _jpype.setResource("NotAResource", None)
185
186 # FIXME this one is broken
187 # def testJPrimitiveSetAttr(self):
188 # ji = jpype.JInt(1)
189 # with self.assertRaises(AttributeError):
190 # ji.qq = "wow"
191
192 # FIXME this path seems like something outdated and is not working
193 # def testJBooleanFromBool(self):
194 # self.assertTrue(jpype.java.lang.Boolean(jpype.JInt(40))==True)
195
196 def testJArrayFail(self):
197 class JArray2(jpype.JArray, internal=True):
198 pass
199 with self.assertRaises(TypeError):
200 JArray2(jpype.JInt)
201
202 @common.requireInstrumentation
203 def testJStringFail(self):
204 _jpype.fault("PyJPClass_new::verify")
205
206 class JString2(jpype.JString, internal=True):
207 pass
208 with self.assertRaises(TypeError):
209 JString2("foo")
210
211 def testCustomizerLate(self):
212 with self.assertRaises(TypeError):
213 @jpype.JImplementationFor("java.lang.Object", base=True)
214 class Sally(object):
215 pass
216
217 def testCustomizerBadType(self):
218 with self.assertRaises(TypeError):
219 @jpype.JImplementationFor({})
220 class Sally(object):
221 pass
222
223 def testModuleHasClass(self):
224 self.assertTrue(_jpype._hasClass("java.lang.Object"))
225
226 def testJClassBadClass(self):
227 with self.assertRaises(Exception):
228 jpype.JClass("not.a.class")
229
230 def testJClassBadType(self):
231 with self.assertRaises(TypeError):
232 jpype.JClass({})
233
234 def testJClassFromClass(self):
235 self.assertIsInstance(jpype.JClass(jpype.java.lang.Class.forName(
236 "java.lang.StringBuilder")), jpype.JClass)
237
238 def testHints(self):
239 with self.assertRaises(AttributeError):
240 jpype.JObject._hints = object()
241
242 @pytest.mark.filterwarnings("ignore::DeprecationWarning")
243 def testDeprecated(self):
244 @jpype._core.deprecated
245 def foo():
246 pass
247
248 @jpype._core.deprecated("foo")
249 def bar():
250 pass
251 bar()
252 foo()
253
254 def testVersionPreStart(self):
255 with mock.patch('_jpype.isStarted') as func:
256 func.return_value = False
257 self.assertEqual(jpype.getJVMVersion(), (0, 0, 0))
258
259 def testGui(self):
260 def foo():
261 pass # this is executed in a thread which may start later
262 magic = mock.MagicMock()
263 with mock.patch("sys.platform", "other"), mock.patch.dict('sys.modules', {'PyObjCTools': magic}):
264 from PyObjCTools import AppHelper # type: ignore
265 self.assertEqual(sys.platform, "other")
266 jpype.setupGuiEnvironment(foo)
267 self.assertFalse(magic.AppHelper.runConsoleEventLoop.called)
268 jpype.shutdownGuiEnvironment()
269 self.assertFalse(magic.AppHelper.stopEventLoop.called)
270 with mock.patch("sys.platform", "darwin"), mock.patch.dict('sys.modules', {'PyObjCTools': magic}):
271 from PyObjCTools import AppHelper # type: ignore
272 self.assertEqual(sys.platform, "darwin")
273 jpype.setupGuiEnvironment(foo)
274 self.assertTrue(magic.AppHelper.runConsoleEventLoop.called)
275 jpype.shutdownGuiEnvironment()
276 self.assertTrue(magic.AppHelper.stopEventLoop.called)
277
278 def testImportKey(self):
279 self.assertEqual(jpype.imports._keywordUnwrap("with_"), "with")
280 self.assertEqual(jpype.imports._keywordUnwrap("func"), "func")
281 self.assertEqual(jpype.imports._keywordWrap("with"), "with_")
282 self.assertEqual(jpype.imports._keywordWrap("func"), "func")
283
284 def testImportNotStarted(self):
285 with mock.patch('_jpype.isStarted') as func:
286 func.return_value = False
287 with self.assertRaisesRegex(ImportError, "jvm"):
288 import mil.spec # type: ignore