Skip to content

Commit

Permalink
Systematically check that jpype.startJVM is tested and correctly typed
Browse files Browse the repository at this point in the history
  • Loading branch information
pelson committed May 3, 2023
1 parent d847350 commit 507ede7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 33 deletions.
4 changes: 2 additions & 2 deletions jpype/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def interactive():
def startJVM(
*jvmargs: str,
jvmpath: typing.Optional[_PathOrStr] = None,
classpath: typing.Optional[_PathOrStr] = None,
classpath: typing.Optional[typing.Sequence[_PathOrStr], _PathOrStr] = None,
ignoreUnrecognized: bool = False,
convertStrings: bool = False,
interrupt: bool = not interactive(),
Expand Down Expand Up @@ -223,7 +223,7 @@ def startJVM(
# Allow the path to be a PathLike.
jvmpath = os.fspath(jvmpath)

extra_jvm_args = tuple()
extra_jvm_args: typing.Tuple[str, ...] = tuple()

# Classpath handling
if _hasClassPath(jvmargs):
Expand Down
2 changes: 0 additions & 2 deletions jpype/_core.pyi

This file was deleted.

73 changes: 44 additions & 29 deletions test/jpypetest/test_startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,6 @@
from pathlib import Path
import unittest


@functools.wraps(jpype.startJVM)
def runStartJVMTest(*args, **kwargs):
jpype.startJVM(*args, **kwargs)
try:
assert jpype.JClass('jpype.array.TestArray') is not None
except Exception as err:
raise RuntimeError("Test class not found") from err


root = os.path.dirname(os.path.abspath(os.path.dirname(__file__)))
cp = os.path.join(root, 'classes').replace('\\', '/')

Expand Down Expand Up @@ -62,72 +52,97 @@ def testInvalidArgsFalse(self):
def testInvalidArgsTrue(self):
jpype.startJVM(
"-for_sure_InVaLiD",
ignoreUnrecognized=True, convertStrings=False,
ignoreUnrecognized=True,
convertStrings=False,
)

def testClasspathArgKeyword(self):
runStartJVMTest(classpath=cp, convertStrings=False)
jpype.startJVM(classpath=cp, convertStrings=False)
assert jpype.JClass('jpype.array.TestArray') is not None

def testClasspathArgList(self):
runStartJVMTest(classpath=[cp], convertStrings=False)
jpype.startJVM(
classpath=[cp],
convertStrings=False,
)
assert jpype.JClass('jpype.array.TestArray') is not None

def testClasspathArgListEmpty(self):
runStartJVMTest(classpath=[cp, ''], convertStrings=False)
jpype.startJVM(
classpath=[cp, ''],
convertStrings=False,
)
assert jpype.JClass('jpype.array.TestArray') is not None

def testClasspathArgDef(self):
runStartJVMTest('-Djava.class.path=%s' % cp, convertStrings=False)
jpype.startJVM('-Djava.class.path=%s' % cp, convertStrings=False)
assert jpype.JClass('jpype.array.TestArray') is not None

def testClasspathArgPath(self):
runStartJVMTest(classpath=Path(cp), convertStrings=False)
jpype.startJVM(classpath=Path(cp), convertStrings=False)
assert jpype.JClass('jpype.array.TestArray') is not None

def testClasspathArgPathList(self):
runStartJVMTest(classpath=[Path(cp)], convertStrings=False)
jpype.startJVM(classpath=[Path(cp)], convertStrings=False)
assert jpype.JClass('jpype.array.TestArray') is not None

def testClasspathArgGlob(self):
jpype.startJVM(classpath=os.path.join(cp, '..', 'jar', 'mrjar*'))
assert jpype.JClass('org.jpype.mrjar.A') is not None

def testClasspathTwice(self):
with self.assertRaises(TypeError):
runStartJVMTest('-Djava.class.path=%s' %
jpype.startJVM('-Djava.class.path=%s' %
cp, classpath=cp, convertStrings=False)

def testClasspathBadType(self):
with self.assertRaises(TypeError):
runStartJVMTest(classpath=1, convertStrings=False)
jpype.startJVM(classpath=1, convertStrings=False)

def testJVMPathArg_Str(self):
runStartJVMTest(self.jvmpath, classpath=cp, convertStrings=False)
jpype.startJVM(self.jvmpath, classpath=cp, convertStrings=False)
assert jpype.JClass('jpype.array.TestArray') is not None

def testJVMPathArg_None(self):
# It is allowed to pass None as a JVM path
runStartJVMTest(None, classpath=cp, )
jpype.startJVM(
None, # type: ignore
classpath=cp,
)
assert jpype.JClass('jpype.array.TestArray') is not None

def testJVMPathArg_NoArgs(self):
runStartJVMTest(classpath=cp)
jpype.startJVM(
classpath=cp,
)
assert jpype.JClass('jpype.array.TestArray') is not None

def testJVMPathArg_Path(self):
with self.assertRaises(TypeError):
runStartJVMTest(
jpype.startJVM(
# Pass a path as the first argument. This isn't supported (this is
# reflected in the type definition), but the fact that it "works"
# gives rise to this test.
Path(self.jvmpath), # type: ignore
classpath=cp,
Path(self.jvmpath), # type: ignore
convertStrings=False,
)

def testJVMPathKeyword_str(self):
runStartJVMTest(classpath=cp, jvmpath=self.jvmpath,
convertStrings=False)
jpype.startJVM(
classpath=cp,
jvmpath=self.jvmpath,
convertStrings=False,
)
assert jpype.JClass('jpype.array.TestArray') is not None

def testJVMPathKeyword_Path(self):
runStartJVMTest(jvmpath=Path(self.jvmpath), classpath=cp, convertStrings=False)
jpype.startJVM(jvmpath=Path(self.jvmpath), classpath=cp, convertStrings=False)
assert jpype.JClass('jpype.array.TestArray') is not None

def testPathTwice(self):
with self.assertRaises(TypeError):
jpype.startJVM(self.jvmpath, jvmpath=self.jvmpath)

def testBadKeyword(self):
with self.assertRaises(TypeError):
jpype.startJVM(invalid=True)
jpype.startJVM(invalid=True) # type: ignore

0 comments on commit 507ede7

Please sign in to comment.