| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
import _jpype |
| 21 |
import jpype |
| 22 |
import unittest |
| 23 |
import subrun |
| 24 |
import common |
| 25 |
import unittest.mock as mock |
| 26 |
|
| 27 |
|
| 28 |
@subrun.TestCase(individual=True) |
| 29 |
class ModuleStartTestCase(unittest.TestCase): |
| 30 |
|
| 31 |
def testNoJObject(self): |
| 32 |
with self.assertRaises(RuntimeError): |
| 33 |
import jpype |
| 34 |
del _jpype.JObject |
| 35 |
jpype.startJVM() |
| 36 |
|
| 37 |
def testNoJInterface(self): |
| 38 |
with self.assertRaises(RuntimeError): |
| 39 |
import jpype |
| 40 |
del _jpype.JInterface |
| 41 |
jpype.startJVM() |
| 42 |
|
| 43 |
def testNoJArray(self): |
| 44 |
with self.assertRaises(RuntimeError): |
| 45 |
import jpype |
| 46 |
del _jpype.JArray |
| 47 |
jpype.startJVM() |
| 48 |
|
| 49 |
def testNoJException(self): |
| 50 |
with self.assertRaises(RuntimeError): |
| 51 |
import jpype |
| 52 |
del _jpype.JException |
| 53 |
jpype.startJVM() |
| 54 |
|
| 55 |
def testNoJClassPre(self): |
| 56 |
with self.assertRaises(RuntimeError): |
| 57 |
import jpype |
| 58 |
del _jpype._jclassPre |
| 59 |
jpype.startJVM() |
| 60 |
|
| 61 |
def testNoJClassPost(self): |
| 62 |
with self.assertRaises(RuntimeError): |
| 63 |
import jpype |
| 64 |
del _jpype._jclassPost |
| 65 |
jpype.startJVM() |
| 66 |
|
| 67 |
def testNoMethodAnnotations(self): |
| 68 |
with self.assertRaises(RuntimeError): |
| 69 |
import jpype |
| 70 |
del _jpype.getMethodAnnotations |
| 71 |
jpype.startJVM() |
| 72 |
|
| 73 |
def testNoMethodCode(self): |
| 74 |
with self.assertRaises(RuntimeError): |
| 75 |
import jpype |
| 76 |
del _jpype.getMethodCode |
| 77 |
jpype.startJVM() |
| 78 |
|
| 79 |
def testShutdown(self): |
| 80 |
import jpype |
| 81 |
jpype.startJVM(convertStrings=False) |
| 82 |
jpype.shutdownJVM() |
| 83 |
|
| 84 |
|
| 85 |
class ModuleTestCase(common.JPypeTestCase): |
| 86 |
|
| 87 |
def setUp(self): |
| 88 |
common.JPypeTestCase.setUp(self) |
| 89 |
|
| 90 |
def testIsPackage(self): |
| 91 |
self.assertTrue(_jpype.isPackage("java")) |
| 92 |
self.assertFalse(_jpype.isPackage("jva")) |
| 93 |
with self.assertRaises(TypeError): |
| 94 |
_jpype.isPackage(object()) |
| 95 |
|
| 96 |
def testGetClass(self): |
| 97 |
self.assertIsInstance(_jpype._getClass("java.lang.String"), _jpype._JClass) |
| 98 |
with self.assertRaises(TypeError): |
| 99 |
_jpype._getClass(object()) |
| 100 |
|
| 101 |
def testHasClass(self): |
| 102 |
self.assertTrue(_jpype._hasClass("java.lang.String")) |
| 103 |
with self.assertRaises(TypeError): |
| 104 |
_jpype._hasClass(object()) |
| 105 |
|
| 106 |
|
| 107 |
class JInitTestCase(common.JPypeTestCase): |
| 108 |
|
| 109 |
def setUp(self): |
| 110 |
common.JPypeTestCase.setUp(self) |
| 111 |
|
| 112 |
def testJInit(self): |
| 113 |
with mock.patch("_jpype.isStarted") as started: |
| 114 |
started.return_value = False |
| 115 |
self.assertEqual(len(jpype._jinit.JInitializers), 0) |
| 116 |
A = [] |
| 117 |
|
| 118 |
def func(): |
| 119 |
A.append(1) |
| 120 |
jpype.onJVMStart(func) |
| 121 |
self.assertEqual(len(A), 0) |
| 122 |
self.assertEqual(jpype._jinit.JInitializers[0], func) |
| 123 |
started.return_value = True |
| 124 |
jpype.onJVMStart(func) |
| 125 |
self.assertEqual(len(A), 1) |
| 126 |
jpype._jinit.runJVMInitializers() |
| 127 |
self.assertEqual(len(A), 2) |