Debian Codegraph @

test/jpypetest/test_startup.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 subrun
20 import os
21 from pathlib import Path
22 import unittest
23 import common
24
25 root = os.path.dirname(os.path.abspath(os.path.dirname(__file__)))
26 cp = os.path.join(root, 'classes').replace('\\', '/')
27
28
29 @subrun.TestCase(individual=True)
30 class StartJVMCase(unittest.TestCase):
31 def setUp(self):
32 self.jvmpath = jpype.getDefaultJVMPath()
33
34 def testStartup(self):
35 with self.assertRaises(OSError):
36 jpype.startJVM(convertStrings=False)
37 jpype.startJVM(convertStrings=False)
38
39 def testRestart(self):
40 with self.assertRaises(OSError):
41 jpype.startJVM(convertStrings=False)
42 jpype.shutdownJVM()
43 jpype.startJVM(convertStrings=False)
44
45 def testInvalidArgsFalse(self):
46 with self.assertRaises(RuntimeError):
47 jpype.startJVM(
48 "-for_sure_InVaLiD",
49 ignoreUnrecognized=False, convertStrings=False,
50 )
51
52 def testInvalidArgsTrue(self):
53 jpype.startJVM(
54 "-for_sure_InVaLiD",
55 ignoreUnrecognized=True,
56 convertStrings=False,
57 )
58
59 def testClasspathArgKeyword(self):
60 jpype.startJVM(classpath=cp, convertStrings=False)
61 assert jpype.JClass('jpype.array.TestArray') is not None
62
63 def testClasspathArgList(self):
64 jpype.startJVM(
65 classpath=[cp],
66 convertStrings=False,
67 )
68 assert jpype.JClass('jpype.array.TestArray') is not None
69
70 def testClasspathArgListEmpty(self):
71 jpype.startJVM(
72 classpath=[cp, ''],
73 convertStrings=False,
74 )
75 assert jpype.JClass('jpype.array.TestArray') is not None
76
77 def testClasspathArgDef(self):
78 jpype.startJVM('-Djava.class.path=%s' % cp, convertStrings=False)
79 assert jpype.JClass('jpype.array.TestArray') is not None
80
81 def testClasspathArgPath(self):
82 jpype.startJVM(classpath=Path(cp), convertStrings=False)
83 assert jpype.JClass('jpype.array.TestArray') is not None
84
85 def testClasspathArgPathList(self):
86 jpype.startJVM(classpath=[Path(cp)], convertStrings=False)
87 assert jpype.JClass('jpype.array.TestArray') is not None
88
89 def testClasspathArgGlob(self):
90 jpype.startJVM(classpath=os.path.join(cp, '..', 'jar', 'mrjar*'))
91 assert jpype.JClass('org.jpype.mrjar.A') is not None
92
93 def testClasspathTwice(self):
94 with self.assertRaises(TypeError):
95 jpype.startJVM('-Djava.class.path=%s' %
96 cp, classpath=cp, convertStrings=False)
97
98 def testClasspathBadType(self):
99 with self.assertRaises(TypeError):
100 jpype.startJVM(classpath=1, convertStrings=False)
101
102 def testJVMPathArg_Str(self):
103 jpype.startJVM(self.jvmpath, classpath=cp, convertStrings=False)
104 assert jpype.JClass('jpype.array.TestArray') is not None
105
106 def testJVMPathArg_None(self):
107 # It is allowed to pass None as a JVM path
108 jpype.startJVM(
109 None, # type: ignore
110 classpath=cp,
111 )
112 assert jpype.JClass('jpype.array.TestArray') is not None
113
114 def testJVMPathArg_NoArgs(self):
115 jpype.startJVM(
116 classpath=cp,
117 )
118 assert jpype.JClass('jpype.array.TestArray') is not None
119
120 def testJVMPathArg_Path(self):
121 with self.assertRaises(TypeError):
122 jpype.startJVM(
123 # Pass a path as the first argument. This isn't supported (this is
124 # reflected in the type definition), but the fact that it "works"
125 # gives rise to this test.
126 Path(self.jvmpath), # type: ignore
127 convertStrings=False,
128 )
129
130 def testJVMPathKeyword_str(self):
131 jpype.startJVM(
132 classpath=cp,
133 jvmpath=self.jvmpath,
134 convertStrings=False,
135 )
136 assert jpype.JClass('jpype.array.TestArray') is not None
137
138 def testJVMPathKeyword_Path(self):
139 jpype.startJVM(jvmpath=Path(self.jvmpath), classpath=cp, convertStrings=False)
140 assert jpype.JClass('jpype.array.TestArray') is not None
141
142 def testPathTwice(self):
143 with self.assertRaises(TypeError):
144 jpype.startJVM(self.jvmpath, jvmpath=self.jvmpath)
145
146 def testBadKeyword(self):
147 with self.assertRaises(TypeError):
148 jpype.startJVM(invalid=True) # type: ignore
149
150 def testNonASCIIPath(self):
151 """Test that paths with non-ASCII characters are handled correctly.
152 Regression test for https://github.com/jpype-project/jpype/issues/1194
153 """
154 jpype.startJVM(jvmpath=Path(self.jvmpath), classpath="test/jar/unicode_à😎/sample_package.jar")
155 cl = jpype.JClass("java.lang.ClassLoader").getSystemClassLoader()
156 self.assertEqual(type(cl), jpype.JClass("org.jpype.JPypeClassLoader"))
157 assert dir(jpype.JPackage('org.jpype.sample_package')) == ['A', 'B']
158
159
160 def testOldStyleNonASCIIPath(self):
161 """Test that paths with non-ASCII characters are handled correctly.
162 Regression test for https://github.com/jpype-project/jpype/issues/1194
163 """
164 jpype.startJVM("-Djava.class.path=test/jar/unicode_à😎/sample_package.jar", jvmpath=Path(self.jvmpath))
165 cl = jpype.JClass("java.lang.ClassLoader").getSystemClassLoader()
166 self.assertEqual(type(cl), jpype.JClass("org.jpype.JPypeClassLoader"))
167 assert dir(jpype.JPackage('org.jpype.sample_package')) == ['A', 'B']
168
169 def testNonASCIIPathWithSystemClassLoader(self):
170 with self.assertRaises(ValueError):
171 jpype.startJVM(
172 "-Djava.system.class.loader=jpype.startup.TestSystemClassLoader",
173 jvmpath=Path(self.jvmpath),
174 classpath="test/jar/unicode_à😎/sample_package.jar"
175 )
176
177 def testOldStyleNonASCIIPathWithSystemClassLoader(self):
178 with self.assertRaises(ValueError):
179 jpype.startJVM(
180 self.jvmpath,
181 "-Djava.system.class.loader=jpype.startup.TestSystemClassLoader",
182 "-Djava.class.path=test/jar/unicode_à😎/sample_package.jar"
183 )
184
185 @common.requireAscii
186 def testASCIIPathWithSystemClassLoader(self):
187 jpype.startJVM(
188 "-Djava.system.class.loader=jpype.startup.TestSystemClassLoader",
189 jvmpath=Path(self.jvmpath),
190 classpath=f"test/classes"
191 )
192 classloader = jpype.JClass("java.lang.ClassLoader").getSystemClassLoader()
193 test_classLoader = jpype.JClass("jpype.startup.TestSystemClassLoader")
194 self.assertEqual(type(classloader), test_classLoader)
195 assert dir(jpype.JPackage('jpype.startup')) == ['TestSystemClassLoader']
196
197 @common.requireAscii
198 def testOldStyleASCIIPathWithSystemClassLoader(self):
199 jpype.startJVM(
200 self.jvmpath,
201 "-Djava.system.class.loader=jpype.startup.TestSystemClassLoader",
202 "-Djava.class.path=test/classes"
203 )
204 classloader = jpype.JClass("java.lang.ClassLoader").getSystemClassLoader()
205 test_classLoader = jpype.JClass("jpype.startup.TestSystemClassLoader")
206 self.assertEqual(type(classloader), test_classLoader)
207 assert dir(jpype.JPackage('jpype.startup')) == ['TestSystemClassLoader']
208
209 @common.requireAscii
210 def testDefaultSystemClassLoader(self):
211 # we introduce no behavior change unless absolutely necessary
212 jpype.startJVM(jvmpath=Path(self.jvmpath))
213 cl = jpype.JClass("java.lang.ClassLoader").getSystemClassLoader()
214 self.assertNotEqual(type(cl), jpype.JClass("org.jpype.JPypeClassLoader"))
215
216 def testServiceWithNonASCIIPath(self):
217 jpype.startJVM(
218 self.jvmpath,
219 "-Djava.locale.providers=SPI,CLDR",
220 classpath="test/jar/unicode_à😎/service.jar",
221 )
222 ZoneId = jpype.JClass("java.time.ZoneId")
223 ZoneRulesException = jpype.JClass("java.time.zone.ZoneRulesException")
224 try:
225 ZoneId.of("JpypeTest/Timezone")
226 except ZoneRulesException:
227 self.fail("JpypeZoneRulesProvider not loaded")