7 changed files with 407 additions and 60 deletions
@ -0,0 +1,104 @@
@@ -0,0 +1,104 @@
|
||||
/* |
||||
* Copyright 2009 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. |
||||
* 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 or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.test.context.junit4.statements; |
||||
|
||||
import java.lang.reflect.Method; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import org.junit.internal.runners.model.MultipleFailureException; |
||||
import org.junit.runners.model.Statement; |
||||
import org.springframework.test.context.TestContextManager; |
||||
|
||||
/** |
||||
* <code>RunSpringTestContextAfters</code> is a custom JUnit 4.5+ |
||||
* {@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) afterTestMethod()} |
||||
* on the supplied {@link TestContextManager}. |
||||
* |
||||
* @see #evaluate() |
||||
* @see RunSpringTestContextBefores |
||||
* @author Sam Brannen |
||||
* @since 3.0 |
||||
*/ |
||||
public class RunSpringTestContextAfters extends Statement { |
||||
|
||||
private final Statement next; |
||||
|
||||
private final Object testInstance; |
||||
|
||||
private final Method testMethod; |
||||
|
||||
private final TestContextManager testContextManager; |
||||
|
||||
|
||||
/** |
||||
* Constructs a new <code>RunSpringTestContextAfters</code> statement. |
||||
* |
||||
* @param next the next <code>Statement</code> in the execution chain |
||||
* @param testInstance the current test instance (never <code>null</code>) |
||||
* @param testMethod the test method which has just been executed on the |
||||
* test instance |
||||
* @param testContextManager the TestContextManager upon which to call |
||||
* <code>afterTestMethod()</code> |
||||
*/ |
||||
public RunSpringTestContextAfters(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 {@link org.junit.internal.runners.statements.RunAfters |
||||
* RunAfters}), catching any exceptions thrown, and then calls |
||||
* {@link TestContextManager#afterTestMethod(Object, Method)} with the first |
||||
* caught exception (if any). If the call to <code>afterTestMethod()</code> |
||||
* throws an exception, it will also be tracked. Multiple exceptions will be |
||||
* combined into a {@link MultipleFailureException}. |
||||
*/ |
||||
@Override |
||||
public void evaluate() throws Throwable { |
||||
Throwable testException = null; |
||||
List<Throwable> errors = new ArrayList<Throwable>(); |
||||
try { |
||||
this.next.evaluate(); |
||||
} |
||||
catch (Throwable e) { |
||||
testException = e; |
||||
errors.add(e); |
||||
} |
||||
|
||||
try { |
||||
this.testContextManager.afterTestMethod(this.testInstance, this.testMethod, testException); |
||||
} |
||||
catch (Exception e) { |
||||
errors.add(e); |
||||
} |
||||
|
||||
if (errors.isEmpty()) { |
||||
return; |
||||
} |
||||
if (errors.size() == 1) { |
||||
throw errors.get(0); |
||||
} |
||||
throw new MultipleFailureException(errors); |
||||
} |
||||
} |
@ -0,0 +1,77 @@
@@ -0,0 +1,77 @@
|
||||
/* |
||||
* Copyright 2009 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. |
||||
* 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 or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.test.context.junit4.statements; |
||||
|
||||
import java.lang.reflect.Method; |
||||
|
||||
import org.junit.runners.model.Statement; |
||||
import org.springframework.test.context.TestContextManager; |
||||
|
||||
/** |
||||
* <code>RunSpringTestContextBefores</code> is a custom JUnit 4.5+ |
||||
* {@link Statement} which allows the <em>Spring TestContext Framework</em> to |
||||
* be plugged into the JUnit execution chain by calling |
||||
* {@link TestContextManager#beforeTestMethod(Object, Method) |
||||
* beforeTestMethod()} on the supplied {@link TestContextManager}. |
||||
* |
||||
* @see #evaluate() |
||||
* @see RunSpringTestContextAfters |
||||
* @author Sam Brannen |
||||
* @since 3.0 |
||||
*/ |
||||
public class RunSpringTestContextBefores extends Statement { |
||||
|
||||
private final Statement next; |
||||
|
||||
private final Object testInstance; |
||||
|
||||
private final Method testMethod; |
||||
|
||||
private final TestContextManager testContextManager; |
||||
|
||||
|
||||
/** |
||||
* Constructs a new <code>RunSpringTestContextBefores</code> statement. |
||||
* |
||||
* @param next the next <code>Statement</code> in the execution chain |
||||
* @param testInstance the current test instance (never <code>null</code>) |
||||
* @param testMethod the test method which is about to be executed on the |
||||
* test instance |
||||
* @param testContextManager the TestContextManager upon which to call |
||||
* <code>beforeTestMethod()</code> |
||||
*/ |
||||
public RunSpringTestContextBefores(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 {@link org.junit.internal.runners.statements.RunBefores |
||||
* RunBefores} ). |
||||
*/ |
||||
@Override |
||||
public void evaluate() throws Throwable { |
||||
this.testContextManager.beforeTestMethod(this.testInstance, this.testMethod); |
||||
this.next.evaluate(); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,76 @@
@@ -0,0 +1,76 @@
|
||||
/* |
||||
* Copyright 2009 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. |
||||
* 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 or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.test.context.junit4.statements; |
||||
|
||||
import java.util.concurrent.TimeoutException; |
||||
|
||||
import org.junit.runners.model.Statement; |
||||
import org.springframework.test.annotation.Timed; |
||||
|
||||
/** |
||||
* <code>SpringFailOnTimeout</code> is a custom JUnit 4.5+ {@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. |
||||
* |
||||
* @see #evaluate() |
||||
* @author Sam Brannen |
||||
* @since 3.0 |
||||
*/ |
||||
public class SpringFailOnTimeout extends Statement { |
||||
|
||||
private final Statement next; |
||||
|
||||
private final long timeout; |
||||
|
||||
|
||||
/** |
||||
* Constructs a new <code>SpringFailOnTimeout</code> statement. |
||||
* |
||||
* @param next the next <code>Statement</code> in the execution chain |
||||
* @param timeout the configured <code>timeout</code> for the current test |
||||
* @see Timed#millis() |
||||
*/ |
||||
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</code> takes more than the specified <code>timeout</code> |
||||
* . |
||||
*/ |
||||
@Override |
||||
public void evaluate() throws Throwable { |
||||
long startTime = System.currentTimeMillis(); |
||||
try { |
||||
this.next.evaluate(); |
||||
} |
||||
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)); |
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,76 @@
@@ -0,0 +1,76 @@
|
||||
/* |
||||
* Copyright 2009 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. |
||||
* 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 or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.test.context.junit4.statements; |
||||
|
||||
import java.lang.reflect.Method; |
||||
|
||||
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</code> is a custom JUnit 4.5+ {@link Statement} which adds |
||||
* support for Spring's {@link Repeat @Repeat} annotation by repeating the |
||||
* test for the specified number of times. |
||||
* |
||||
* @see #evaluate() |
||||
* @author Sam Brannen |
||||
* @since 3.0 |
||||
*/ |
||||
public class SpringRepeat extends Statement { |
||||
|
||||
protected static final Log logger = LogFactory.getLog(SpringRepeat.class); |
||||
|
||||
private final Statement next; |
||||
|
||||
private final Method testMethod; |
||||
|
||||
private final int repeat; |
||||
|
||||
|
||||
/** |
||||
* Constructs a new <code>SpringRepeat</code> statement. |
||||
* |
||||
* @param next the next <code>Statement</code> 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; |
||||
this.testMethod = testMethod; |
||||
this.repeat = Math.max(1, repeat); |
||||
} |
||||
|
||||
/** |
||||
* Invokes the next {@link Statement statement} in the execution chain for |
||||
* 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.next.evaluate(); |
||||
} |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue