org.kuali.rice.test.runners
Class LoadTimeWeavableTestRunner

java.lang.Object
  extended by org.junit.runner.Runner
      extended by org.kuali.rice.test.runners.LoadTimeWeavableTestRunner
All Implemented Interfaces:
org.junit.runner.Describable, org.junit.runner.manipulation.Filterable, org.junit.runner.manipulation.Sortable

public class LoadTimeWeavableTestRunner
extends org.junit.runner.Runner
implements org.junit.runner.manipulation.Filterable, org.junit.runner.manipulation.Sortable

A JUnit test Runner which uses a custom classloader with a copy of the classpath and allows for transformers to be added to the ClassLoader for load-time weaving.

Useful when writing tests that use JPA with EclipseLink since it depends upon load-time weaving.

In order to use this class, you must have a BootstrapTest annotation available somewhere in the hierarchy of your test class (usually on the same class where the RunWith annotation is specified which references this runner class). This informs the runner about a test that it can run to execute any one-time initialization for the test suite. Ideally, this bootstrap test will execute code which loads JPA persistence units and any associated ClassFileTransformers for load-time weaving. This is necessary because it is common for an integration test to have references in the test class itself to JPA entities which need to be weaved. When this occurs, if the persistence units and ClassFileTransformers are not properly loaded before the entity classes are loaded by the classloader, then instrumentation will (silently!) fail to occur.

Much of the code in this class was copied from the JUnit ParentRunner, BlockJUnit4ClassRunner, and TomcatInstrumentableClassLoader.

Author:
Kuali Rice Team (rice.collab@kuali.org)

Constructor Summary
LoadTimeWeavableTestRunner(Class<?> testClass)
          Constructs a new ParentRunner that will run @TestClass
 
Method Summary
protected  org.junit.runners.model.Statement childrenInvoker(org.junit.runner.notification.RunNotifier notifier)
          Returns a Statement: Call runChild(org.junit.runners.model.FrameworkMethod, org.junit.runner.notification.RunNotifier) on each object returned by getChildren() (subject to any imposed filter and sort)
protected  org.junit.runners.model.Statement classBlock(org.junit.runner.notification.RunNotifier notifier)
          Constructs a Statement to run all of the tests in the test class.
protected  List<org.junit.rules.TestRule> classRules()
           
protected  void collectInitializationErrors(List<Throwable> errors)
          Adds to errors a throwable for each problem noted with the test class (available from getTestClass()).
protected  List<org.junit.runners.model.FrameworkMethod> computeOriginalTestMethods()
           
protected  List<org.junit.runners.model.FrameworkMethod> computeTestMethods()
          Returns the methods that run tests.
protected  Object createTest()
          Returns a new fixture for running a test.
protected  org.junit.runner.Description describeChild(org.junit.runners.model.FrameworkMethod method)
          Returns a Description for child, which can be assumed to be an element of the list returned by getChildren()
protected  org.junit.runner.Description describeOriginalChild(org.junit.runners.model.FrameworkMethod method)
           
 void filter(org.junit.runner.manipulation.Filter filter)
           
protected  List<org.junit.runners.model.FrameworkMethod> getChildren()
          Returns a list of objects that define the children of this Runner.
protected  ClassLoader getCustomClassLoader()
           
 org.junit.runner.Description getDescription()
           
protected  String getName()
          Returns a name used to describe this Runner
protected  List<org.junit.runners.model.FrameworkMethod> getOriginalChildren()
           
 org.junit.runners.model.TestClass getOriginalTestClass()
          Returns the original test class that was passed to this test runner.
protected  Annotation[] getRunnerAnnotations()
           
 org.junit.runners.model.TestClass getTestClass()
          Returns a TestClass object wrapping the class to be executed.
protected  List<org.junit.rules.TestRule> getTestRules(Object target)
           
protected  org.junit.runners.model.Statement methodBlock(org.junit.runners.model.FrameworkMethod method)
          Returns a Statement that, when executed, either returns normally if method passes, or throws an exception if method fails.
protected  org.junit.runners.model.Statement methodInvoker(org.junit.runners.model.FrameworkMethod method, Object test)
          Returns a Statement that invokes method on test
protected  org.junit.runners.model.Statement possiblyExpectingExceptions(org.junit.runners.model.FrameworkMethod method, Object test, org.junit.runners.model.Statement next)
          Deprecated. Will be private soon: use Rules instead
protected  List<org.junit.rules.MethodRule> rules(Object target)
           
 void run(org.junit.runner.notification.RunNotifier notifier)
           
protected  boolean runBootstrapTest(org.junit.runner.notification.RunNotifier notifier, org.junit.runners.model.TestClass testClass)
           
protected  void runChild(org.junit.runners.model.FrameworkMethod method, org.junit.runner.notification.RunNotifier notifier)
          Runs the test corresponding to child, which can be assumed to be an element of the list returned by getChildren().
protected  void runLeaf(org.junit.runners.model.Statement statement, org.junit.runner.Description description, org.junit.runner.notification.RunNotifier notifier)
          Runs a Statement that represents a leaf (aka atomic) test.
protected  void setTestMethod(Object test, Method method)
          Sets the Method on the test case if it is MethodAware
protected  void setTestName(Object test, Method testMethod)
           
 void sort(org.junit.runner.manipulation.Sorter sorter)
           
protected  String testName(org.junit.runners.model.FrameworkMethod method)
          Returns the name that describes method for Descriptions.
protected  void validateConstructor(List<Throwable> errors)
          Adds to errors if the test class has more than one constructor, or if the constructor takes parameters.
protected  void validateFields(List<Throwable> errors)
           
protected  void validateInstanceMethods(List<Throwable> errors)
          Deprecated. unused API, will go away in future version
protected  void validateNoNonStaticInnerClass(List<Throwable> errors)
           
protected  void validateOnlyOneConstructor(List<Throwable> errors)
          Adds to errors if the test class has more than one constructor (do not override)
protected  void validatePublicVoidNoArgMethods(Class<? extends Annotation> annotation, boolean isStatic, List<Throwable> errors)
          Adds to errors if any method in this class is annotated with annotation, but: is not public, or takes parameters, or returns something other than void, or is static (given isStatic is false), or is not static (given isStatic is true).
protected  void validateTestMethods(List<Throwable> errors)
          Adds to errors for each method annotated with @Testthat is not a public, void instance method with no arguments.
protected  void validateZeroArgConstructor(List<Throwable> errors)
          Adds to errors if the test class's single constructor takes parameters (do not override)
protected  org.junit.runners.model.Statement withAfterClasses(org.junit.runners.model.Statement statement)
          Returns a Statement: run all non-overridden @AfterClass methods on this class and superclasses before executing statement; all AfterClass methods are always executed: exceptions thrown by previous steps are combined, if necessary, with exceptions from AfterClass methods into a MultipleFailureException.
protected  org.junit.runners.model.Statement withAfters(org.junit.runners.model.FrameworkMethod method, Object target, org.junit.runners.model.Statement statement)
          Deprecated. Will be private soon: use Rules instead
protected  org.junit.runners.model.Statement withBeforeClasses(org.junit.runners.model.Statement statement)
          Returns a Statement: run all non-overridden @BeforeClass methods on this class and superclasses before executing statement; if any throws an Exception, stop execution and pass the exception on.
protected  org.junit.runners.model.Statement withBefores(org.junit.runners.model.FrameworkMethod method, Object target, org.junit.runners.model.Statement statement)
          Deprecated. Will be private soon: use Rules instead
protected  org.junit.runners.model.Statement withPotentialTimeout(org.junit.runners.model.FrameworkMethod method, Object test, org.junit.runners.model.Statement next)
          Deprecated. Will be private soon: use Rules instead
 
Methods inherited from class org.junit.runner.Runner
testCount
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LoadTimeWeavableTestRunner

public LoadTimeWeavableTestRunner(Class<?> testClass)
                           throws org.junit.runners.model.InitializationError
Constructs a new ParentRunner that will run @TestClass

Throws:
org.junit.runners.model.InitializationError
Method Detail

getCustomClassLoader

protected ClassLoader getCustomClassLoader()

validatePublicVoidNoArgMethods

protected void validatePublicVoidNoArgMethods(Class<? extends Annotation> annotation,
                                              boolean isStatic,
                                              List<Throwable> errors)
Adds to errors if any method in this class is annotated with annotation, but:

classBlock

protected org.junit.runners.model.Statement classBlock(org.junit.runner.notification.RunNotifier notifier)
Constructs a Statement to run all of the tests in the test class. Override to add pre-/post-processing. Here is an outline of the implementation:

Returns:
Statement

withBeforeClasses

protected org.junit.runners.model.Statement withBeforeClasses(org.junit.runners.model.Statement statement)
Returns a Statement: run all non-overridden @BeforeClass methods on this class and superclasses before executing statement; if any throws an Exception, stop execution and pass the exception on.


withAfterClasses

protected org.junit.runners.model.Statement withAfterClasses(org.junit.runners.model.Statement statement)
Returns a Statement: run all non-overridden @AfterClass methods on this class and superclasses before executing statement; all AfterClass methods are always executed: exceptions thrown by previous steps are combined, if necessary, with exceptions from AfterClass methods into a MultipleFailureException.


classRules

protected List<org.junit.rules.TestRule> classRules()
Returns:
the ClassRules that can transform the block that runs each method in the tested class.

childrenInvoker

protected org.junit.runners.model.Statement childrenInvoker(org.junit.runner.notification.RunNotifier notifier)
Returns a Statement: Call runChild(org.junit.runners.model.FrameworkMethod, org.junit.runner.notification.RunNotifier) on each object returned by getChildren() (subject to any imposed filter and sort)


getName

protected String getName()
Returns a name used to describe this Runner


getTestClass

public final org.junit.runners.model.TestClass getTestClass()
Returns a TestClass object wrapping the class to be executed.


getOriginalTestClass

public final org.junit.runners.model.TestClass getOriginalTestClass()
Returns the original test class that was passed to this test runner.


runLeaf

protected final void runLeaf(org.junit.runners.model.Statement statement,
                             org.junit.runner.Description description,
                             org.junit.runner.notification.RunNotifier notifier)
Runs a Statement that represents a leaf (aka atomic) test.


getRunnerAnnotations

protected Annotation[] getRunnerAnnotations()
Returns:
the annotations that should be attached to this runner's description.

getDescription

public org.junit.runner.Description getDescription()
Specified by:
getDescription in interface org.junit.runner.Describable
Specified by:
getDescription in class org.junit.runner.Runner

run

public void run(org.junit.runner.notification.RunNotifier notifier)
Specified by:
run in class org.junit.runner.Runner

runBootstrapTest

protected boolean runBootstrapTest(org.junit.runner.notification.RunNotifier notifier,
                                   org.junit.runners.model.TestClass testClass)

filter

public void filter(org.junit.runner.manipulation.Filter filter)
            throws org.junit.runner.manipulation.NoTestsRemainException
Specified by:
filter in interface org.junit.runner.manipulation.Filterable
Throws:
org.junit.runner.manipulation.NoTestsRemainException

sort

public void sort(org.junit.runner.manipulation.Sorter sorter)
Specified by:
sort in interface org.junit.runner.manipulation.Sortable

runChild

protected void runChild(org.junit.runners.model.FrameworkMethod method,
                        org.junit.runner.notification.RunNotifier notifier)
Runs the test corresponding to child, which can be assumed to be an element of the list returned by getChildren(). Subclasses are responsible for making sure that relevant test events are reported through notifier


describeChild

protected org.junit.runner.Description describeChild(org.junit.runners.model.FrameworkMethod method)
Returns a Description for child, which can be assumed to be an element of the list returned by getChildren()


describeOriginalChild

protected org.junit.runner.Description describeOriginalChild(org.junit.runners.model.FrameworkMethod method)

getChildren

protected List<org.junit.runners.model.FrameworkMethod> getChildren()
Returns a list of objects that define the children of this Runner.


getOriginalChildren

protected List<org.junit.runners.model.FrameworkMethod> getOriginalChildren()

computeTestMethods

protected List<org.junit.runners.model.FrameworkMethod> computeTestMethods()
Returns the methods that run tests. Default implementation returns all methods annotated with @Test on this class and superclasses that are not overridden.


computeOriginalTestMethods

protected List<org.junit.runners.model.FrameworkMethod> computeOriginalTestMethods()

collectInitializationErrors

protected void collectInitializationErrors(List<Throwable> errors)
Adds to errors a throwable for each problem noted with the test class (available from getTestClass()). Default implementation adds an error for each method annotated with @BeforeClass or @AfterClass that is not public static void with no arguments.


validateNoNonStaticInnerClass

protected void validateNoNonStaticInnerClass(List<Throwable> errors)

validateConstructor

protected void validateConstructor(List<Throwable> errors)
Adds to errors if the test class has more than one constructor, or if the constructor takes parameters. Override if a subclass requires different validation rules.


validateOnlyOneConstructor

protected void validateOnlyOneConstructor(List<Throwable> errors)
Adds to errors if the test class has more than one constructor (do not override)


validateZeroArgConstructor

protected void validateZeroArgConstructor(List<Throwable> errors)
Adds to errors if the test class's single constructor takes parameters (do not override)


validateInstanceMethods

@Deprecated
protected void validateInstanceMethods(List<Throwable> errors)
Deprecated. unused API, will go away in future version

Adds to errors for each method annotated with @Test, @Before, or @After that is not a public, void instance method with no arguments.


validateFields

protected void validateFields(List<Throwable> errors)

validateTestMethods

protected void validateTestMethods(List<Throwable> errors)
Adds to errors for each method annotated with @Testthat is not a public, void instance method with no arguments.


createTest

protected Object createTest()
                     throws Exception
Returns a new fixture for running a test. Default implementation executes the test class's no-argument constructor (validation should have ensured one exists).

Throws:
Exception

setTestMethod

protected void setTestMethod(Object test,
                             Method method)
                      throws Exception
Sets the Method on the test case if it is MethodAware

Parameters:
method - the current method to be run
test - the test instance
Throws:
Exception

setTestName

protected void setTestName(Object test,
                           Method testMethod)
                    throws Exception
Throws:
Exception

testName

protected String testName(org.junit.runners.model.FrameworkMethod method)
Returns the name that describes method for Descriptions. Default implementation is the method's name


methodBlock

protected org.junit.runners.model.Statement methodBlock(org.junit.runners.model.FrameworkMethod method)
Returns a Statement that, when executed, either returns normally if method passes, or throws an exception if method fails. Here is an outline of the default implementation: This can be overridden in subclasses, either by overriding this method, or the implementations creating each sub-statement.


methodInvoker

protected org.junit.runners.model.Statement methodInvoker(org.junit.runners.model.FrameworkMethod method,
                                                          Object test)
Returns a Statement that invokes method on test


possiblyExpectingExceptions

@Deprecated
protected org.junit.runners.model.Statement possiblyExpectingExceptions(org.junit.runners.model.FrameworkMethod method,
                                                                                   Object test,
                                                                                   org.junit.runners.model.Statement next)
Deprecated. Will be private soon: use Rules instead

Returns a Statement: if method's @Test annotation has the expecting attribute, return normally only if next throws an exception of the correct type, and throw an exception otherwise.


withPotentialTimeout

@Deprecated
protected org.junit.runners.model.Statement withPotentialTimeout(org.junit.runners.model.FrameworkMethod method,
                                                                            Object test,
                                                                            org.junit.runners.model.Statement next)
Deprecated. Will be private soon: use Rules instead

Returns a Statement: if method's @Test annotation has the timeout attribute, throw an exception if next takes more than the specified number of milliseconds.


withBefores

@Deprecated
protected org.junit.runners.model.Statement withBefores(org.junit.runners.model.FrameworkMethod method,
                                                                   Object target,
                                                                   org.junit.runners.model.Statement statement)
Deprecated. Will be private soon: use Rules instead

Returns a Statement: run all non-overridden @Before methods on this class and superclasses before running next; if any throws an Exception, stop execution and pass the exception on.


withAfters

@Deprecated
protected org.junit.runners.model.Statement withAfters(org.junit.runners.model.FrameworkMethod method,
                                                                  Object target,
                                                                  org.junit.runners.model.Statement statement)
Deprecated. Will be private soon: use Rules instead

Returns a Statement: run all non-overridden @After methods on this class and superclasses before running next; all After methods are always executed: exceptions thrown by previous steps are combined, if necessary, with exceptions from After methods into a MultipleFailureException.


rules

protected List<org.junit.rules.MethodRule> rules(Object target)
Parameters:
target - the test case instance
Returns:
a list of MethodRules that should be applied when executing this test

getTestRules

protected List<org.junit.rules.TestRule> getTestRules(Object target)
Parameters:
target - the test case instance
Returns:
a list of TestRules that should be applied when executing this test


Copyright © 2005–2014 The Kuali Foundation. All rights reserved.