patterntesting.runtime.junit.ProxyRunner Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of patterntesting-rt Show documentation
Show all versions of patterntesting-rt Show documentation
PatternTesting Runtime (patterntesting-rt) is the runtime component for
the PatternTesting framework. It provides the annotations and base classes
for the PatternTesting testing framework (e.g. patterntesting-check,
patterntesting-concurrent or patterntesting-exception) but can be also
used standalone for classpath monitoring or profiling.
It uses AOP and AspectJ to perform this feat.
/*
* $Id: ProxyRunner.java,v 1.2 2014/01/04 19:28:54 oboehm Exp $
*
* Copyright (c) 2011 by Oliver Boehm
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* (c)reated 11.11.2011 by oliver ([email protected])
*/
package patterntesting.runtime.junit;
import java.lang.annotation.Annotation;
import java.lang.reflect.*;
import java.util.*;
import org.junit.runner.Description;
import org.junit.runner.notification.*;
import org.junit.runners.ParentRunner;
import org.junit.runners.model.*;
import org.slf4j.*;
import patterntesting.runtime.annotation.DelegateTo;
import patterntesting.runtime.junit.SmokeRunner;
import patterntesting.runtime.junit.internal.*;
import patterntesting.runtime.util.ReflectionHelper;
/**
* This is a JUnit runner which delegates the call one (or perhaps several)
* other JUnit runners.
*
* Till 1.2.10-YEARS it was placed in the experimental package.
*
* @author oliver ([email protected])
* @since 1.2 (11.11.2011)
*/
public class ProxyRunner extends SmokeRunner {
private static final Logger log = LoggerFactory.getLogger(ProxyRunner.class);
private ParentRunner delegateRunner;
/**
* Instantiates a new proxy runner.
*
* @param testClass the test class
* @throws InitializationError the initialization error
*/
public ProxyRunner(final Class> testClass) throws InitializationError {
super(testClass);
}
/**
* Gets the Runner defined by @DelegateTo
.
*
* Note: This class is public for testing reasons.
*
* @return the delegate runner
*/
public final ParentRunner getDelegateRunner() {
if (this.delegateRunner == null) {
try {
this.delegateRunner = createDelegateRunner();
} catch (InitializationError ie) {
throw new RuntimeException(ie);
}
}
return this.delegateRunner;
}
private ParentRunner createDelegateRunner() throws InitializationError {
Class extends ParentRunner> runnerClass = getDelegateRunnerClass();
try {
Constructor extends ParentRunner> ctor = runnerClass.getDeclaredConstructor(Class.class);
return ctor.newInstance(this.getTestClass().getJavaClass());
} catch (SecurityException e) {
throw new ModelInitializationError(e);
} catch (NoSuchMethodException e) {
throw new ModelInitializationError(e);
} catch (IllegalArgumentException e) {
throw new ModelInitializationError(e);
} catch (InstantiationException e) {
throw new ModelInitializationError(e);
} catch (IllegalAccessException e) {
throw new ModelInitializationError(e);
} catch (InvocationTargetException e) {
throw new ModelInitializationError(e);
}
}
private Class extends ParentRunner> getDelegateRunnerClass() {
DelegateTo delegateTo = this.getTestClass().getJavaClass().getAnnotation(DelegateTo.class);
return delegateTo.value();
}
/**
* Gets the description.
*
* @return the description
* @see org.junit.runner.Runner#getDescription()
*/
@Override
public Description getDescription() {
Description description = this.getDelegateRunner().getDescription();
Collection annotationList = description.getAnnotations();
Annotation[] annotations = annotationList.toArray(new Annotation[annotationList.size()]);
Description filtered = DescriptionUtils.createTestDescription(description, annotations);
for (Description descr : description.getChildren()) {
filtered.addChild(descr);
}
return filtered;
}
/**
* Here we decide if the whole class is to be run.
*
* @param notifier the notifier
* @see org.junit.runner.Runner#run(org.junit.runner.notification.RunNotifier)
*/
@Override
public void run(final RunNotifier notifier) {
//delegateRunner.run(notifier);
super.run(notifier);
}
/**
* Gets the children.
*
* @return the children
* @see org.junit.runners.ParentRunner#getChildren()
*/
@SuppressWarnings("unchecked")
@Override
protected List getChildren() {
// TODO: put a Proxy around each FrameworkMethod
List methods = (List) ReflectionHelper.invokeMethod(this.getDelegateRunner(), "getChildren");
return methods;
}
/**
* Run child. This method may be not called (e.g. it is not called by
* SpringJUnit4ClassRunner) but must be implemented because of the abstract
* superclass (ParentRunner).
*
* @param method the method
* @param notifier the notifier
* @see org.junit.runners.ParentRunner#runChild(java.lang.Object, org.junit.runner.notification.RunNotifier)
*/
@Override
protected void runChild(final FrameworkMethod method, final RunNotifier notifier) {
Description description = describeChild(method);
try {
if (shouldBeIgnored(method)) {
notifier.fireTestIgnored(description);
return;
}
} catch (IllegalArgumentException iae) {
fireTestAssumptionFailed(notifier, description, iae);
notifier.fireTestFinished(description);
return;
}
long startTime = System.currentTimeMillis();
ReflectionHelper.invokeMethod(delegateRunner, "runChild", method, notifier);
logMethod(method, System.currentTimeMillis() - startTime);
}
private static void logMethod(final FrameworkMethod method, final long time) {
log.info("{}.{} (" + time + " ms)", method.getMethod().getDeclaringClass().getName(),
method.getName());
}
}