Polishing

This commit is contained in:
Juergen Hoeller
2015-09-28 23:11:53 +02:00
parent 4428c347a5
commit 1239595672
14 changed files with 159 additions and 122 deletions

View File

@@ -25,15 +25,17 @@ import org.junit.runners.model.Statement;
import org.springframework.test.context.TestContextManager;
/**
* {@code RunAfterTestClassCallbacks} is a custom JUnit {@link Statement} which allows the
* <em>Spring TestContext Framework</em> to be plugged into the JUnit execution chain by
* calling {@link TestContextManager#afterTestClass() afterTestClass()} on the supplied
* {@code RunAfterTestClassCallbacks} is a custom JUnit {@link Statement} which allows
* the <em>Spring TestContext Framework</em> to be plugged into the JUnit execution chain
* by calling {@link TestContextManager#afterTestClass afterTestClass()} on the supplied
* {@link TestContextManager}.
*
* @see #evaluate()
* @see RunBeforeTestMethodCallbacks
* <p><strong>NOTE:</strong> This class requires JUnit 4.9 or higher.
*
* @author Sam Brannen
* @since 3.0
* @see #evaluate()
* @see RunBeforeTestClassCallbacks
*/
public class RunAfterTestClassCallbacks extends Statement {
@@ -43,8 +45,7 @@ public class RunAfterTestClassCallbacks extends Statement {
/**
* Constructs a new {@code RunAfterTestClassCallbacks} statement.
*
* Construct a new {@code RunAfterTestClassCallbacks} statement.
* @param next the next {@code Statement} in the execution chain
* @param testContextManager the TestContextManager upon which to call
* {@code afterTestClass()}
@@ -54,12 +55,13 @@ public class RunAfterTestClassCallbacks extends Statement {
this.testContextManager = testContextManager;
}
/**
* Invokes the next {@link Statement} in the execution chain (typically an instance of
* Evaluate the next {@link Statement} in the execution chain (typically an instance of
* {@link org.junit.internal.runners.statements.RunAfters RunAfters}), catching any
* exceptions thrown, and then calls {@link TestContextManager#afterTestClass()}. If
* the call to {@code afterTestClass()} throws an exception, it will also be tracked.
* Multiple exceptions will be combined into a {@link MultipleFailureException}.
* exceptions thrown, and then invoke {@link TestContextManager#afterTestClass()}.
* <p>If the invocation of {@code afterTestClass()} throws an exception, it will also
* be tracked. Multiple exceptions will be combined into a {@link MultipleFailureException}.
*/
@Override
public void evaluate() throws Throwable {
@@ -67,15 +69,15 @@ public class RunAfterTestClassCallbacks extends Statement {
try {
this.next.evaluate();
}
catch (Throwable e) {
errors.add(e);
catch (Throwable ex) {
errors.add(ex);
}
try {
this.testContextManager.afterTestClass();
}
catch (Exception e) {
errors.add(e);
catch (Throwable ex) {
errors.add(ex);
}
if (errors.isEmpty()) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -28,13 +28,15 @@ import org.springframework.test.context.TestContextManager;
/**
* {@code RunAfterTestMethodCallbacks} is a custom JUnit {@link Statement} which allows
* the <em>Spring TestContext Framework</em> to be plugged into the JUnit execution chain
* by calling {@link TestContextManager#afterTestMethod(Object, Method, Throwable)
* afterTestMethod()} on the supplied {@link TestContextManager}.
* by calling {@link TestContextManager#afterTestMethod afterTestMethod()} on the supplied
* {@link TestContextManager}.
*
* <p><strong>NOTE:</strong> This class requires JUnit 4.9 or higher.
*
* @see #evaluate()
* @see RunBeforeTestMethodCallbacks
* @author Sam Brannen
* @since 3.0
* @see #evaluate()
* @see RunBeforeTestMethodCallbacks
*/
public class RunAfterTestMethodCallbacks extends Statement {
@@ -48,8 +50,7 @@ public class RunAfterTestMethodCallbacks extends Statement {
/**
* Constructs a new {@code RunAfterTestMethodCallbacks} statement.
*
* Construct a new {@code RunAfterTestMethodCallbacks} statement.
* @param next the next {@code Statement} in the execution chain
* @param testInstance the current test instance (never {@code null})
* @param testMethod the test method which has just been executed on the
@@ -59,19 +60,22 @@ public class RunAfterTestMethodCallbacks extends Statement {
*/
public RunAfterTestMethodCallbacks(Statement next, Object testInstance, Method testMethod,
TestContextManager testContextManager) {
this.next = next;
this.testInstance = testInstance;
this.testMethod = testMethod;
this.testContextManager = testContextManager;
}
/**
* Invokes the next {@link Statement} in the execution chain (typically an instance of
* Evaluate the next {@link Statement} in the execution chain (typically an instance of
* {@link org.junit.internal.runners.statements.RunAfters RunAfters}), catching any
* exceptions thrown, and then calls
* {@link TestContextManager#afterTestMethod(Object, Method, Throwable)} with the
* first caught exception (if any). If the call to {@code afterTestMethod()} throws an
* exception, it will also be tracked. Multiple exceptions will be combined into a
* exceptions thrown, and then invoke
* {@link TestContextManager#afterTestMethod(Object, Method, Throwable)} supplying the
* first caught exception (if any).
* <p>If the invocation of {@code afterTestMethod()} throws an exception, that
* exception will also be tracked. Multiple exceptions will be combined into a
* {@link MultipleFailureException}.
*/
@Override
@@ -81,16 +85,16 @@ public class RunAfterTestMethodCallbacks extends Statement {
try {
this.next.evaluate();
}
catch (Throwable e) {
testException = e;
errors.add(e);
catch (Throwable ex) {
testException = ex;
errors.add(ex);
}
try {
this.testContextManager.afterTestMethod(this.testInstance, this.testMethod, testException);
}
catch (Exception e) {
errors.add(e);
catch (Throwable ex) {
errors.add(ex);
}
if (errors.isEmpty()) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -26,10 +26,10 @@ import org.springframework.test.context.TestContextManager;
* by calling {@link TestContextManager#beforeTestClass() beforeTestClass()} on the
* supplied {@link TestContextManager}.
*
* @see #evaluate()
* @see RunAfterTestMethodCallbacks
* @author Sam Brannen
* @since 3.0
* @see #evaluate()
* @see RunAfterTestMethodCallbacks
*/
public class RunBeforeTestClassCallbacks extends Statement {
@@ -39,8 +39,7 @@ public class RunBeforeTestClassCallbacks extends Statement {
/**
* Constructs a new {@code RunBeforeTestClassCallbacks} statement.
*
* Construct a new {@code RunBeforeTestClassCallbacks} statement.
* @param next the next {@code Statement} in the execution chain
* @param testContextManager the TestContextManager upon which to call
* {@code beforeTestClass()}
@@ -50,10 +49,11 @@ public class RunBeforeTestClassCallbacks extends Statement {
this.testContextManager = testContextManager;
}
/**
* Calls {@link TestContextManager#beforeTestClass()} and then invokes the next
* {@link Statement} in the execution chain (typically an instance of
* {@link org.junit.internal.runners.statements.RunBefores RunBefores}).
* Invoke {@link TestContextManager#beforeTestClass()} and then evaluate
* the next {@link Statement} in the execution chain (typically an instance
* of {@link org.junit.internal.runners.statements.RunBefores RunBefores}).
*/
@Override
public void evaluate() throws Throwable {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -28,10 +28,10 @@ import org.springframework.test.context.TestContextManager;
* by calling {@link TestContextManager#beforeTestMethod(Object, Method)
* beforeTestMethod()} on the supplied {@link TestContextManager}.
*
* @see #evaluate()
* @see RunAfterTestMethodCallbacks
* @author Sam Brannen
* @since 3.0
* @see #evaluate()
* @see RunAfterTestMethodCallbacks
*/
public class RunBeforeTestMethodCallbacks extends Statement {
@@ -45,8 +45,7 @@ public class RunBeforeTestMethodCallbacks extends Statement {
/**
* Constructs a new {@code RunBeforeTestMethodCallbacks} statement.
*
* Construct a new {@code RunBeforeTestMethodCallbacks} statement.
* @param next the next {@code Statement} in the execution chain
* @param testInstance the current test instance (never {@code null})
* @param testMethod the test method which is about to be executed on the
@@ -56,15 +55,18 @@ public class RunBeforeTestMethodCallbacks extends Statement {
*/
public RunBeforeTestMethodCallbacks(Statement next, Object testInstance, Method testMethod,
TestContextManager testContextManager) {
this.next = next;
this.testInstance = testInstance;
this.testMethod = testMethod;
this.testContextManager = testContextManager;
}
/**
* Calls {@link TestContextManager#beforeTestMethod(Object, Method)} and then invokes
* the next {@link Statement} in the execution chain (typically an instance of
* Invoke {@link TestContextManager#beforeTestMethod(Object, Method)}
* and then evaluate the next {@link Statement} in the execution chain
* (typically an instance of
* {@link org.junit.internal.runners.statements.RunBefores RunBefores}).
*/
@Override

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,17 +20,20 @@ import java.util.concurrent.TimeoutException;
import org.junit.runners.model.Statement;
import org.springframework.test.annotation.Timed;
/**
* {@code SpringFailOnTimeout} is a custom JUnit {@link Statement} which adds
* support for Spring's {@link Timed @Timed} annotation by throwing an exception
* if the next statement in the execution chain takes more than the specified
* number of milliseconds.
* support for Spring's {@link org.springframework.test.annotation.Timed @Timed}
* annotation by throwing an exception if the next statement in the execution
* chain takes more than the specified number of milliseconds.
*
* <p>In contrast to JUnit's
* {@link org.junit.internal.runners.statements.FailOnTimeout FailOnTimeout},
* the next {@code statement} will be executed in the same thread as the
* caller and will therefore not be aborted preemptively.
*
* @see #evaluate()
* @author Sam Brannen
* @since 3.0
* @see #evaluate()
*/
public class SpringFailOnTimeout extends Statement {
@@ -40,24 +43,25 @@ public class SpringFailOnTimeout extends Statement {
/**
* Constructs a new {@code SpringFailOnTimeout} statement.
*
* @param next the next {@code Statement} in the execution chain
* @param timeout the configured {@code timeout} for the current test
* @see Timed#millis()
* Construct a new {@code SpringFailOnTimeout} statement for the supplied
* {@code timeout}.
* <p>If the supplied {@code timeout} is {@code 0}, the execution of the
* {@code next} statement will not be timed.
* @param next the next {@code Statement} in the execution chain; never {@code null}
* @param timeout the configured {@code timeout} for the current test, in milliseconds;
* never negative
*/
public SpringFailOnTimeout(Statement next, long timeout) {
this.next = next;
this.timeout = timeout;
}
/**
* Invokes the next {@link Statement statement} in the execution chain
* (typically an instance of
* {@link org.junit.internal.runners.statements.InvokeMethod InvokeMethod}
* or {@link org.junit.internal.runners.statements.ExpectException
* ExpectException}) and throws an exception if the next {@code statement}
* takes more than the specified {@code timeout}.
* Evaluate the next {@link Statement statement} in the execution chain
* (typically an instance of {@link SpringRepeat}) and throw a
* {@link TimeoutException} if the next {@code statement} executes longer
* than the specified {@code timeout}.
*/
@Override
public void evaluate() throws Throwable {
@@ -68,7 +72,8 @@ public class SpringFailOnTimeout extends Statement {
finally {
long elapsed = System.currentTimeMillis() - startTime;
if (elapsed > this.timeout) {
throw new TimeoutException(String.format("Test took %s ms; limit was %s ms.", elapsed, this.timeout));
throw new TimeoutException(
String.format("Test took %s ms; limit was %s ms.", elapsed, this.timeout));
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,17 +22,14 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.runners.model.Statement;
import org.springframework.test.annotation.Repeat;
import org.springframework.util.ClassUtils;
/**
* {@code SpringRepeat} is a custom JUnit {@link Statement} which adds support
* for Spring's {@link Repeat @Repeat} annotation by repeating the test for
* the specified number of times.
* for Spring's {@link org.springframework.test.annotation.Repeat @Repeat}
* annotation by repeating the test the specified number of times.
*
* @see #evaluate()
* @author Sam Brannen
* @since 3.0
* @see #evaluate()
*/
public class SpringRepeat extends Statement {
@@ -46,12 +43,11 @@ public class SpringRepeat extends Statement {
/**
* Constructs a new {@code SpringRepeat} statement.
*
* Construct a new {@code SpringRepeat} statement for the supplied
* {@code testMethod} and {@code repeat} count.
* @param next the next {@code Statement} in the execution chain
* @param testMethod the current test method
* @param repeat the configured repeat count for the current test method
* @see Repeat#value()
*/
public SpringRepeat(Statement next, Method testMethod, int repeat) {
this.next = next;
@@ -59,16 +55,17 @@ public class SpringRepeat extends Statement {
this.repeat = Math.max(1, repeat);
}
/**
* Invokes the next {@link Statement statement} in the execution chain for
* the specified repeat count.
* Evaluate the next {@link Statement statement} in the execution chain
* repeatedly, using the specified repeat count.
*/
@Override
public void evaluate() throws Throwable {
for (int i = 0; i < this.repeat; i++) {
if (this.repeat > 1 && logger.isInfoEnabled()) {
logger.info(String.format("Repetition %d of test %s#%s()", (i + 1),
ClassUtils.getShortName(this.testMethod.getDeclaringClass()), this.testMethod.getName()));
this.testMethod.getDeclaringClass().getSimpleName(), this.testMethod.getName()));
}
this.next.evaluate();
}