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
|
# *****************************************************************************
#
# 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.imports
import common
import sys
import os
import importlib
import pytest
from unittest import mock
import _jpype
# Tests just for coverage
# These will be moved to the corresponding test file as needed
class CoverageCase(common.JPypeTestCase):
def setUp(self):
common.JPypeTestCase.setUp(self)
self.platform = sys.platform
def testWin32(self):
if sys.platform == "win32":
raise common.unittest.SkipTest("not tested on win32")
try:
sys.platform = "win32"
importlib.reload(jpype._classpath)
with self.assertRaises(jpype.JVMNotFoundException):
jpype.getDefaultJVMPath()
finally:
sys.platform = self.platform
importlib.reload(jpype._classpath)
def testHandleClassPath(self):
with self.assertRaises(TypeError):
jpype._core._handleClassPath([1])
jpype._core._handleClassPath(["./*.jar"])
def testRestart(self):
with self.assertRaises(OSError):
jpype.startJVM()
def testSynchronizeFail(self):
with self.assertRaises(TypeError):
jpype.synchronized("hello")
def testJArrayFail1(self):
with self.assertRaises(TypeError):
jpype.JArray(jpype.JInt, 2, 2)
def testJArrayFail2(self):
with self.assertRaises(TypeError):
jpype.JArray(jpype.JInt, 1)(1, 2, 3)
def testJArrayStr(self):
self.assertEqual(str(jpype.JArray(jpype.JInt)([1, 2])), str([1, 2]))
def testJArrayLength(self):
ja = jpype.JArray(jpype.JInt)([1, 2])
self.assertEqual(ja.length, len(ja))
def testJArrayGetItemSlice(self):
ja = jpype.JArray(jpype.JInt)([1, 2, 3, 4])
assert list(ja[0:2:-1]) == [1, 2, 3, 4][0:2:-1]
def testJArraySetItemSlice(self):
ja = jpype.JArray(jpype.JInt)([1, 2, 3, 4, 5, 6])
ja[0:-1:2] = [-1, -1, -1]
self.assertEqual(list(ja[:]), [-1, 2, -1, 4, -1, 6])
# FIXME ja[0:-1:2] = [-1] works but should not
# FIXME ja[0:-1:2] = 1 gives wierd error
def testJArrayGetItemSlice_2(self):
ja = jpype.JArray(jpype.JInt)([1, 2, 3, 4])
assert list(ja[1:]) == [2, 3, 4]
def testJArraySetItemSlice_2(self):
ja = jpype.JArray(jpype.JInt)([1, 2, 3, 4])
ja[1:] = [3, 4, 5]
self.assertEqual(list(ja[:]), [1, 3, 4, 5])
def testJArrayEQ(self):
ja1 = jpype.JArray(jpype.JInt)([1, 2, 3, 4])
ja2 = jpype.JArray(jpype.JInt)([1, 2, 3, 4])
ja1 == ja2
# FIXME: This test is failing, but shouldn't be.
# assert ja1 == ja2
assert (ja1 == [1, 2]) is False
def testJArrayNE(self):
ja = jpype.JArray(jpype.JInt)([1, 2, 3, 4])
assert ja != [1, 2]
assert (ja != ja) is False
def testJArrayIter(self):
ja = jpype.JArray(jpype.JInt)([1, 2, 3, 4])
for i in ja:
pass
def testJArrayBytes(self):
ja = jpype.JArray(jpype.JByte)([65, 66])
self.assertEqual(str(ja), "AB")
def testJArrayBytesFail(self):
ja = jpype.JArray(jpype.JByte)([65, 66])
self.assertFalse(ja == None)
def testJBooleanFail(self):
with self.assertRaises(TypeError):
jpype.java.lang.Boolean(1, 2)
def testJStringAppend(self):
js = jpype.JString("foo")
self.assertEqual(js + "bar", "foobar")
if not self._convertStrings:
self.assertIsInstance(js + "bar", jpype.java.lang.String)
def testJStringNE(self):
js = jpype.JString("foo")
self.assertFalse(js != "foo")
self.assertFalse(js != jpype.JString("foo"))
# FIXME review this for slices and other cases, we may need
# to improve this one
def testJStringGetItem(self):
js = jpype.JString("fred")
self.assertEqual(js[1], "r")
def testJStringLen(self):
js = jpype.JString("fred")
self.assertEqual(len(js), 4)
def testJStringLT(self):
js = jpype.JString("b")
self.assertTrue(js < 'c')
self.assertFalse(js < 'b')
self.assertFalse(js < 'a')
def testJStringLE(self):
js = jpype.JString("b")
self.assertTrue(js <= 'c')
self.assertTrue(js <= 'b')
self.assertFalse(js <= 'a')
def testJStringGT(self):
js = jpype.JString("b")
self.assertFalse(js > 'c')
self.assertFalse(js > 'b')
self.assertTrue(js > 'a')
def testJStringGE(self):
js = jpype.JString("b")
self.assertFalse(js >= 'c')
self.assertTrue(js >= 'b')
self.assertTrue(js >= 'a')
def testJStringContains(self):
js = jpype.JString("fred")
self.assertTrue("r" in js)
self.assertFalse("g" in js)
def testJStringRepr(self):
js = jpype.JString("fred")
self.assertTrue(repr(js), "fred")
# def testSetResourceFail(self):
# with self.assertRaises(RuntimeError):
# _jpype.setResource("NotAResource", None)
# FIXME this one is broken
# def testJPrimitiveSetAttr(self):
# ji = jpype.JInt(1)
# with self.assertRaises(AttributeError):
# ji.qq = "wow"
# FIXME this path seems like something outdated and is not working
# def testJBooleanFromBool(self):
# self.assertTrue(jpype.java.lang.Boolean(jpype.JInt(40))==True)
def testJArrayFail(self):
class JArray2(jpype.JArray, internal=True):
pass
with self.assertRaises(TypeError):
JArray2(jpype.JInt)
@common.requireInstrumentation
def testJStringFail(self):
_jpype.fault("PyJPClass_new::verify")
class JString2(jpype.JString, internal=True):
pass
with self.assertRaises(TypeError):
JString2("foo")
def testCustomizerLate(self):
with self.assertRaises(TypeError):
@jpype.JImplementationFor("java.lang.Object", base=True)
class Sally(object):
pass
def testCustomizerBadType(self):
with self.assertRaises(TypeError):
@jpype.JImplementationFor({})
class Sally(object):
pass
def testModuleHasClass(self):
self.assertTrue(_jpype._hasClass("java.lang.Object"))
def testJClassBadClass(self):
with self.assertRaises(Exception):
jpype.JClass("not.a.class")
def testJClassBadType(self):
with self.assertRaises(TypeError):
jpype.JClass({})
def testJClassFromClass(self):
self.assertIsInstance(jpype.JClass(jpype.java.lang.Class.forName(
"java.lang.StringBuilder")), jpype.JClass)
def testHints(self):
with self.assertRaises(AttributeError):
jpype.JObject._hints = object()
@pytest.mark.filterwarnings("ignore::DeprecationWarning")
def testDeprecated(self):
@jpype._core.deprecated
def foo():
pass
@jpype._core.deprecated("foo")
def bar():
pass
bar()
foo()
def testVersionPreStart(self):
with mock.patch('_jpype.isStarted') as func:
func.return_value = False
self.assertEqual(jpype.getJVMVersion(), (0, 0, 0))
def testGui(self):
def foo():
pass # this is executed in a thread which may start later
magic = mock.MagicMock()
with mock.patch("sys.platform", "other"), mock.patch.dict('sys.modules', {'PyObjCTools': magic}):
from PyObjCTools import AppHelper # type: ignore
self.assertEqual(sys.platform, "other")
jpype.setupGuiEnvironment(foo)
self.assertFalse(magic.AppHelper.runConsoleEventLoop.called)
jpype.shutdownGuiEnvironment()
self.assertFalse(magic.AppHelper.stopEventLoop.called)
with mock.patch("sys.platform", "darwin"), mock.patch.dict('sys.modules', {'PyObjCTools': magic}):
from PyObjCTools import AppHelper # type: ignore
self.assertEqual(sys.platform, "darwin")
jpype.setupGuiEnvironment(foo)
self.assertTrue(magic.AppHelper.runConsoleEventLoop.called)
jpype.shutdownGuiEnvironment()
self.assertTrue(magic.AppHelper.stopEventLoop.called)
def testImportKey(self):
self.assertEqual(jpype.imports._keywordUnwrap("with_"), "with")
self.assertEqual(jpype.imports._keywordUnwrap("func"), "func")
self.assertEqual(jpype.imports._keywordWrap("with"), "with_")
self.assertEqual(jpype.imports._keywordWrap("func"), "func")
def testImportNotStarted(self):
with mock.patch('_jpype.isStarted') as func:
func.return_value = False
with self.assertRaisesRegex(ImportError, "jvm"):
import mil.spec # type: ignore
|