pysysjava.javaplugin¶
Support for compiling and running Java applications from the run.py
of your PySys tests.
JavaPlugin¶
-
class
pysysjava.javaplugin.
JavaPlugin
[source]¶ Bases:
object
This is a PySys test plugin that for compiling and running Java applications from a PySys testcase (or from the runner).
You can access the methods of this class from any test using
self.java.XXX
. To enable this, just add it to your project configuration with an alias such asjava
:<test-plugin classname="pysysjava.javaplugin.JavaPlugin" alias="java"/>
For advanced cases (compiling or starting Java from a runner class or plugin) you can also use this class as a runner plugin:
<runner-plugin classname="pysysjava.javaplugin.JavaPlugin" alias="java"/>
This plugin assumes the existence of a project property named
javaHome
that contains the path to the JDK, with abin/
subdirectory containing executables such asjava
andjavac
.Since you often want the same JVM arguments (e.g. max heap size etc), compiler arguments and classpath for many of your tests, defaults for these can be configured via properties on the plugin (and in some cases also on a per-test/per-directory basis by adding
<user-data name="..." value="...">
to yourpysystest.xml
orpysysdirconfig.xml
file. For example:<test-plugin classname="pysysjava.javaplugin.JavaPlugin" alias="java"> <property name="defaultJVMArgs" value="-Xmx256m -XX:+HeapDumpOnOutOfMemoryError"/> </test-plugin>
Or:
<pysysdirconfig> <data> <user-data name="javaClasspath" value="${appHome}/target/logging-jars/*.jar"/> <user-data name="jvmArgs" value="-Xmx256M"/> </data> </pysysdirconfig>
See below for more details about these properties.
-
defaultCompilerArgs
= '-g'¶ A space-delimited string of
javac
arguments. By default we enable full debugging information.You may also want to add other options such as
-source
and-target
release version.
-
defaultJVMArgs
= '-Xmx512m -XX:+HeapDumpOnOutOfMemoryError -XX:-UsePerfData'¶ A space-delimited string of JVM arguments to use by default when running
java
processes, unless overridden by a per-test/dirconfigjvmArgs
or ajvmArgs=
keyword argument.By default the maximum heap size is limited to 512MB, but you may wish to set a larger heap limit if you are starting processes that require more memory - but be careful that the test machine has sufficient resources to cope with multiple tests running concurrently without risking out of memory conditions. Also by default the
-XX:-UsePerfData
option is used to avoid creation ofhsperfdata_XXX
files in the temp directory (typically the test output directory) which aren’t useful and can prevent test cleanup.This is converted to a
list[str]
when the plugin is setup ready for use by the test. If a key namedjvmArgs
exists in the test or directory descriptor’suser-data
that value takes precedence over the plugin property.
-
defaultClasspath
= ''¶ A default classpath that will be used for Java compilation and execution, unless overridden by a per-test/dirconfig
javaClasspath
user-data or aclasspath=
keyword argument.For details of how this plugin handles delimiters in the classpath string see
toClasspathList()
.This is converted to a
list[str]
when the plugin is setup ready for use by the test. If a key namedjavaClasspath
exists in the test or directory descriptor’suser-data
that value takes precedence over the plugin property.
-
compile
(input=None, output='javaclasses', classpath=None, arguments=None, **kwargs)[source]¶ Compile Java source files into classes. By default we compile Java files from the test’s input directory to
self.output/javaclasses
.For example:
self.java.compile(self.input, arguments=['--Werror'])
- Parameters
input (str or list[str]) – Typically a directory (relative to the test Input dir) in which to search for classpaths; alternatively a list of Java source files. By default we compile source files under the test Input directory.
output (str) – The path (relative to the test Output directory) where the output will be written.
classpath (str or list[str]) – The classpath to use, or None if the
self.defaultClasspath
should be used (which by default is empty). The classpath can be specified as a list or a single string delimited by;
, newline oros.pathsep
; seetoClasspathList()
for details.arguments (list[str]) – List of compiler arguments such as
--Werror
or-nowarn
(to control warn behaviour). If not specified, thedefaultCompilerArgs
plugin property is used.kwargs – Additional keyword arguments such as
timeout=
will be passed topysys.basetest.BaseTest.startProcess
.
- Returns
The process object, with the full path to the output dir in the
info
dictionary.- Return type
-
startJava
(classOrJar, arguments=[], classpath=None, jvmArgs=None, jvmProps={}, disableCoverage=False, stdouterr=None, **kwargs)[source]¶ Start a Java process to execute the specified class or .jar file.
For example:
myserver = self.java.startJava(self.project.appHome+'/my_server*.jar', ['127.0.0.1', self.getNextAvailableTCPPort()], stdouterr='my_server', background=True) self.java.defaultClasspath = [self.project.appHome+'/mydeps.jar'] self.java.compile(self.input, output=self.output+'/javaclasses', arguments=['--Werror']) self.java.startJava('myorg.MyHttpTestClient', ['127.0.0.1', port], stdouterr='httpclient', classpath=self.java.defaultClasspath+[self.output+'/javaclasses'], timeout=60)
If the project includes a writer with alias “javaCoverageWriter” then that writer is requested to add some JVM arguments to control code coverage (unless disableCoverage=True).
- Parameters
classOrJar (str) – Either a class (from the classpath) to execute, or the path to a
.jar
file (an absolute path or relative to the output directory) whose manifest indicates the main class. Since some jar names contain a version number, a*
glob expression can be used in the .jar file provided it matches exactly one jar and still ends with the.jar
suffix.arguments (list[str]) – Command line arguments for the specified class.
classpath (str or list[str]) – The classpath to use, or None if the
self.defaultClasspath
should be used (which by default is empty). The classpath can be specified as a list or a single string delimited by;
, newline oros.pathsep
; seetoClasspathList()
for details.jvmArgs (list[str]) – List of JVM arguments to pass before the class/jar name, such as
-Xmx512m
. If None is specified, thedefaultJVMArgs
plugin property is used.jvmProps (dict[str,str]) – System properties to be added to the jvmArgs (each entry results in a
-Dkey=value
jvmArg).disableCoverage (bool) – Set to True to ensure Java code coverage is not captured for this process. Code coverage can also be disabled on a per-test/directory basis by setting
self.disableCoverage
or adding thedisableCoverage
group to thepysystest.xml
.stdouterr (str) – The filename prefix to use for the stdout and stderr of the process (out/err will be appended), or a tuple of (stdout,stderr) as returned from
pysys.basetest.BaseTest.allocateUniqueStdOutErr
. The filenames can be accessed from the returned process object using .stdout/err from the returned process object.kwargs – Additional keyword arguments such as
stdouterr=
,timeout=
,onError=
andbackground=
will be passed topysys.basetest.BaseTest.startProcess
. It is strongly recommended to always include atstdouterr=
since otherwise any error messages from the process will not be captured.
- Returns
The process object.
- Return type
-
toClasspathList
(classpath)[source]¶ Converts the specified classpath string to a list of classpath entries (or just returns the input if it’s already a list). Glob expressions such as
*.jar
will be expanded if the parent directory exists and there is at least one match.If None is specified, the
defaultClasspath
is used (either from the test/dir descriptor’sclasspath
user-data or thedefaultClasspath
plugin property).In this PySys plugin classpath strings can be delimited with the usual OS separator
os.pathsep
(e.g.:
or;
), but to allow for platform-independence (given Windows uses:
for drive letters), if the string contains;
or a newline separator those will be used for splitting instead. Any whitespace or empty elements will be deleted.It is recommended to use absolute not relative paths for classpath entries.
>>> plugin = JavaPlugin()
>>> plugin.toClasspathList(['a.jar', 'b.jar']) ['a.jar', 'b.jar']
>>> plugin.toClasspathList(' a.jar ; b.jar ; c:/foo ']) ['a.jar', 'b.jar', 'c:/foo']
>>> plugin.toClasspathList(os.pathsep.join(['a.jar', 'b.jar']) ['a.jar', 'b.jar']
>>> plugin.toClasspathList(None) is None True
-