Package eu.simuline.testhelpers
Class TestCaseClassLoader
- java.lang.Object
-
- java.lang.ClassLoader
-
- eu.simuline.testhelpers.TestCaseClassLoader
-
public final class TestCaseClassLoader extends 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_FILEthat 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 String[]defaultExclusionsDefault excluded paths.private List<String>excludedHolds the excluded paths.(package private) static StringEXCLUDED_FILEName of excluded properties file.private JavaPathjPathThe scanned class path.private static intLEN_CLS_STREAMThe initial length of a stream to read class files from.private static StringPROP_KEY_NO_CLS_RELOADKey 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.privateTestCaseClassLoader(String classPath)Constructs a TestCaseLoader.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) Class<?>defineClass(String name)URLgetResource(String name)InputStreamgetResourceAsStream(String name)private booleanisExcluded(String name)Returns whether the name with the given name is excluded from being loaded.Class<?>loadClass(String name, boolean resolve)private byte[]lookupClassData(String className)private voidreadExcludedPackages()-
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 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 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.xxxand 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 JavaPath jPath
The scanned class path.
-
excluded
private List<String> excluded
Holds the excluded paths. This is initialized byreadExcludedPackages()and used byisExcluded(java.lang.String).
-
defaultExclusions
private final 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(String classPath)
Constructs a TestCaseLoader. It scans the class path and the excluded package paths.- Parameters:
classPath- the classpath.
-
-
Method Detail
-
getResource
public URL getResource(String name)
- Overrides:
getResourcein classClassLoader
-
getResourceAsStream
public InputStream getResourceAsStream(String name)
- Overrides:
getResourceAsStreamin classClassLoader
-
isExcluded
private boolean isExcluded(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
booleanwhich shows whethernamestarts with one of the prefixes given byexcluded. In this case the corresponding class is not loaded.
-
defineClass
Class<?> defineClass(String name) throws ClassNotFoundException
- Throws:
ClassNotFoundException
-
loadClass
public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException
- Overrides:
loadClassin classClassLoader- Throws:
ClassNotFoundException
-
lookupClassData
private byte[] lookupClassData(String className) throws ClassNotFoundException
- Throws:
ClassNotFoundException
-
readExcludedPackages
private void readExcludedPackages()
InitializesexcludedusingdefaultExclusions,PROP_KEY_NO_CLS_RELOADandEXCLUDED_FILE.-
First the entries of
defaultExclusionsare added toexcluded. -
Then
PROP_KEY_NO_CLS_RELOADis interpreted as a ":"-seprated list of entries each of which is added toexcluded. -
Finally,
EXCLUDED_FILEis 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
-
-