Package eu.simuline.testhelpers
Class TestCaseClassLoader
- java.lang.Object
-
- java.lang.ClassLoader
-
- eu.simuline.testhelpers.TestCaseClassLoader
-
public final class TestCaseClassLoader extends java.lang.ClassLoader
A custom class loader which allows to reload classes for each test run. The class loader can be configured with a list of package paths that should be excluded from loading. The loading of these packages is delegated to the system class loader. They will be shared across test runs.The list of excluded package paths is either hardcoded in
defaultExclusions
, specified as property with keyPROP_KEY_NO_CLS_RELOAD
. in a properties fileEXCLUDED_FILE
that is located in the same place as the TestCaseClassLoader class.Known limitation:
- the TestCaseClassLoader cannot load classes from jar files.
- service providers must be excluded from reloading.
- Version:
- 1.0
- Author:
- Ernst Reissner
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.String[]
defaultExclusions
Default excluded paths.private java.util.List<java.lang.String>
excluded
Holds the excluded paths.(package private) static java.lang.String
EXCLUDED_FILE
Name of excluded properties file.private eu.simuline.util.JavaPath
jPath
The scanned class path.private static int
LEN_CLS_STREAM
The initial length of a stream to read class files from.private static java.lang.String
PROP_KEY_NO_CLS_RELOAD
Key of system property containing a ":"-separated list of packages or classes to be excluded from reloading.
-
Constructor Summary
Constructors Modifier Constructor Description TestCaseClassLoader()
Constructs a TestCaseLoader.private
TestCaseClassLoader(java.lang.String classPath)
Constructs a TestCaseLoader.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) java.lang.Class<?>
defineClass(java.lang.String name)
java.net.URL
getResource(java.lang.String name)
java.io.InputStream
getResourceAsStream(java.lang.String name)
private boolean
isExcluded(java.lang.String name)
Returns whether the name with the given name is excluded from being loaded.java.lang.Class<?>
loadClass(java.lang.String name, boolean resolve)
private byte[]
lookupClassData(java.lang.String className)
private void
readExcludedPackages()
-
Methods inherited from class java.lang.ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findClass, findClass, findLibrary, findLoadedClass, findResource, findResource, findResources, findSystemClass, getClassLoadingLock, getDefinedPackage, getDefinedPackages, getName, getPackage, getPackages, getParent, getPlatformClassLoader, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, getUnnamedModule, isRegisteredAsParallelCapable, loadClass, registerAsParallelCapable, resolveClass, resources, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
-
-
-
-
Field Detail
-
PROP_KEY_NO_CLS_RELOAD
private static final java.lang.String PROP_KEY_NO_CLS_RELOAD
Key of system property containing a ":"-separated list of packages or classes to be excluded from reloading. Each is read intoexcluded
.- See Also:
- Constant Field Values
-
EXCLUDED_FILE
static final java.lang.String EXCLUDED_FILE
Name of excluded properties file. This shall be located in the same folder as this class loader. The property keys shall have the formexlude.xxx
and are read inexcluded
.- See Also:
- Constant Field Values
-
LEN_CLS_STREAM
private static final int LEN_CLS_STREAM
The initial length of a stream to read class files from.- See Also:
- Constant Field Values
-
jPath
private eu.simuline.util.JavaPath jPath
The scanned class path.
-
excluded
private java.util.List<java.lang.String> excluded
Holds the excluded paths. This is initialized byreadExcludedPackages()
and used byisExcluded(java.lang.String)
.
-
defaultExclusions
private final java.lang.String[] defaultExclusions
Default excluded paths.- See Also:
isExcluded(java.lang.String)
-
-
Constructor Detail
-
TestCaseClassLoader
public TestCaseClassLoader()
Constructs a TestCaseLoader. It scans the class path and the excluded package paths.
-
TestCaseClassLoader
private TestCaseClassLoader(java.lang.String classPath)
Constructs a TestCaseLoader. It scans the class path and the excluded package paths.- Parameters:
classPath
- the classpath.
-
-
Method Detail
-
getResource
public java.net.URL getResource(java.lang.String name)
- Overrides:
getResource
in classjava.lang.ClassLoader
-
getResourceAsStream
public java.io.InputStream getResourceAsStream(java.lang.String name)
- Overrides:
getResourceAsStream
in classjava.lang.ClassLoader
-
isExcluded
private boolean isExcluded(java.lang.String name)
Returns whether the name with the given name is excluded from being loaded.- Parameters:
name
- the fully qualified name of a class as aString
.- Returns:
- a
boolean
which shows whethername
starts with one of the prefixes given byexcluded
. In this case the corresponding class is not loaded.
-
defineClass
java.lang.Class<?> defineClass(java.lang.String name) throws java.lang.ClassNotFoundException
- Throws:
java.lang.ClassNotFoundException
-
loadClass
public java.lang.Class<?> loadClass(java.lang.String name, boolean resolve) throws java.lang.ClassNotFoundException
- Overrides:
loadClass
in classjava.lang.ClassLoader
- Throws:
java.lang.ClassNotFoundException
-
lookupClassData
private byte[] lookupClassData(java.lang.String className) throws java.lang.ClassNotFoundException
- Throws:
java.lang.ClassNotFoundException
-
readExcludedPackages
private void readExcludedPackages()
Initializesexcluded
usingdefaultExclusions
,PROP_KEY_NO_CLS_RELOAD
andEXCLUDED_FILE
.-
First the entries of
defaultExclusions
are added toexcluded
. -
Then
PROP_KEY_NO_CLS_RELOAD
is interpreted as a ":"-seprated list of entries each of which is added toexcluded
. -
Finally,
EXCLUDED_FILE
is interpreted as filename and the file is interpreted as Properties File with property keys starting withexcluded.
; all the other properties are ignored. The values are trimmed and a trailing*
is removed if present. If the remaining path is nontrivial, it is added toexcluded
.
-
First the entries of
-
-