Class 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 key PROP_KEY_NO_CLS_RELOAD. in a properties file EXCLUDED_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
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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 into excluded.
        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 form exlude.xxx and are read in excluded.
        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.
    • 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 class java.lang.ClassLoader
      • getResourceAsStream

        public java.io.InputStream getResourceAsStream​(java.lang.String name)
        Overrides:
        getResourceAsStream in class java.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 a String.
        Returns:
        a boolean which shows whether name starts with one of the prefixes given by excluded. 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 class java.lang.ClassLoader
        Throws:
        java.lang.ClassNotFoundException
      • lookupClassData

        private byte[] lookupClassData​(java.lang.String className)
                                throws java.lang.ClassNotFoundException
        Throws:
        java.lang.ClassNotFoundException