diff --git a/spring-test/src/main/java/org/springframework/test/annotation/TestAnnotationUtils.java b/spring-test/src/main/java/org/springframework/test/annotation/TestAnnotationUtils.java new file mode 100644 index 0000000000..57cd5b3c0b --- /dev/null +++ b/spring-test/src/main/java/org/springframework/test/annotation/TestAnnotationUtils.java @@ -0,0 +1,66 @@ +/* + * 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. + * 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.annotation; + +import java.lang.reflect.Method; + +import org.springframework.core.annotation.AnnotatedElementUtils; +import org.springframework.core.annotation.AnnotationAttributes; +import org.springframework.core.annotation.AnnotationUtils; + +/** + * Collection of utility methods for working with Spring's core testing annotations. + * + * @author Sam Brannen + * @since 4.2 + */ +public class TestAnnotationUtils { + + /** + * Get the {@code timeout} configured via the {@link Timed @Timed} + * annotation on the supplied {@code method}. + *
Negative configured values will be converted to {@code 0}.
+ * @return the configured timeout, or {@code 0} if the method is not
+ * annotated with {@code @Timed}
+ */
+ public static long getTimeout(Method method) {
+ AnnotationAttributes attributes = AnnotatedElementUtils.findAnnotationAttributes(method, Timed.class.getName());
+ if (attributes == null) {
+ return 0;
+ }
+ else {
+ long millis = attributes. Non-negative configured values will be converted to {@code 1}.
+ * @return the configured repeat count, or {@code 1} if the method is
+ * not annotated with {@code @Repeat}
+ */
+ public static int getRepeatCount(Method method) {
+ Repeat repeat = AnnotationUtils.findAnnotation(method, Repeat.class);
+ if (repeat == null) {
+ return 1;
+ }
+ return Math.max(1, repeat.value());
+ }
+
+}
diff --git a/spring-test/src/main/java/org/springframework/test/context/junit4/SpringJUnit4ClassRunner.java b/spring-test/src/main/java/org/springframework/test/context/junit4/SpringJUnit4ClassRunner.java
index 6762e37ca6..45a4d13d98 100644
--- a/spring-test/src/main/java/org/springframework/test/context/junit4/SpringJUnit4ClassRunner.java
+++ b/spring-test/src/main/java/org/springframework/test/context/junit4/SpringJUnit4ClassRunner.java
@@ -35,12 +35,8 @@ import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;
-import org.springframework.core.annotation.AnnotatedElementUtils;
-import org.springframework.core.annotation.AnnotationAttributes;
-import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.test.annotation.ProfileValueUtils;
-import org.springframework.test.annotation.Repeat;
-import org.springframework.test.annotation.Timed;
+import org.springframework.test.annotation.TestAnnotationUtils;
import org.springframework.test.context.TestContextManager;
import org.springframework.test.context.junit4.rules.SpringClassRule;
import org.springframework.test.context.junit4.rules.SpringMethodRule;
@@ -68,8 +64,8 @@ import org.springframework.util.ReflectionUtils;
* Supports both Spring's {@link Timed @Timed} and JUnit's
- * {@link Test#timeout() @Test(timeout=...)} annotations, but not both
+ * Supports both Spring's {@link org.springframework.test.annotation.Timed @Timed}
+ * and JUnit's {@link Test#timeout() @Test(timeout=...)} annotations, but not both
* simultaneously.
* @return either a {@link SpringFailOnTimeout}, a {@link FailOnTimeout},
* or the supplied {@link Statement} as appropriate
@@ -391,26 +384,19 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner {
* @return the timeout, or {@code 0} if none was specified
*/
protected long getJUnitTimeout(FrameworkMethod frameworkMethod) {
- Test testAnnotation = frameworkMethod.getAnnotation(Test.class);
- return (testAnnotation != null && testAnnotation.timeout() > 0 ? testAnnotation.timeout() : 0);
+ Test test = frameworkMethod.getAnnotation(Test.class);
+ return ((test != null) && (test.timeout() > 0) ? test.timeout() : 0);
}
/**
* Retrieve the configured Spring-specific {@code timeout} from the
- * {@link Timed @Timed} annotation on the supplied
- * {@linkplain FrameworkMethod test method}.
+ * {@link org.springframework.test.annotation.Timed @Timed} annotation
+ * on the supplied {@linkplain FrameworkMethod test method}.
* @return the timeout, or {@code 0} if none was specified
+ * @see TestAnnotationUtils#getTimeout(Method)
*/
protected long getSpringTimeout(FrameworkMethod frameworkMethod) {
- AnnotationAttributes annAttrs = AnnotatedElementUtils.findAnnotationAttributes(frameworkMethod.getMethod(),
- Timed.class.getName());
- if (annAttrs == null) {
- return 0;
- }
- else {
- long millis = annAttrs. Supports Spring's {@link Repeat @Repeat} annotation by returning a
- * {@code SpringRepeat} statement initialized with the configured repeat
- * count (if greater than {@code 1}); otherwise, the supplied statement
- * is returned unmodified.
- * @return either a {@code SpringRepeat} or the supplied {@code Statement}
- * as appropriate
+ * Wrap the supplied {@link Statement} with a {@code SpringRepeat} statement.
+ * Supports Spring's {@link org.springframework.test.annotation.Repeat @Repeat}
+ * annotation.
+ * @see TestAnnotationUtils#getRepeatCount(Method)
* @see SpringRepeat
*/
protected Statement withPotentialRepeat(FrameworkMethod frameworkMethod, Object testInstance, Statement next) {
- Repeat repeatAnnotation = AnnotationUtils.getAnnotation(frameworkMethod.getMethod(), Repeat.class);
- int repeat = (repeatAnnotation != null ? repeatAnnotation.value() : 1);
- return (repeat > 1 ? new SpringRepeat(next, frameworkMethod.getMethod(), repeat) : next);
+ return new SpringRepeat(next, frameworkMethod.getMethod());
}
}
diff --git a/spring-test/src/main/java/org/springframework/test/context/junit4/rules/SpringMethodRule.java b/spring-test/src/main/java/org/springframework/test/context/junit4/rules/SpringMethodRule.java
index 5c0255eabe..8772387828 100644
--- a/spring-test/src/main/java/org/springframework/test/context/junit4/rules/SpringMethodRule.java
+++ b/spring-test/src/main/java/org/springframework/test/context/junit4/rules/SpringMethodRule.java
@@ -26,11 +26,7 @@ import org.junit.rules.MethodRule;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
-import org.springframework.core.annotation.AnnotatedElementUtils;
-import org.springframework.core.annotation.AnnotationAttributes;
-import org.springframework.core.annotation.AnnotationUtils;
-import org.springframework.test.annotation.Repeat;
-import org.springframework.test.annotation.Timed;
+import org.springframework.test.annotation.TestAnnotationUtils;
import org.springframework.test.context.TestContextManager;
import org.springframework.test.context.junit4.statements.ProfileValueChecker;
import org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks;
@@ -78,8 +74,8 @@ import org.springframework.util.ReflectionUtils;
* implementations.)
*
* Supports Spring's {@link Repeat @Repeat} annotation by returning a
- * {@link SpringRepeat} statement initialized with the configured repeat
- * count (if greater than {@code 1}); otherwise, the supplied statement
- * is returned unmodified.
- * @return either a {@code SpringRepeat} or the supplied {@code Statement}
+ * Wrap the supplied {@link Statement} with a {@code SpringRepeat} statement.
+ * Supports Spring's {@link org.springframework.test.annotation.Repeat @Repeat}
+ * annotation.
+ * @see TestAnnotationUtils#getRepeatCount(java.lang.reflect.Method)
* @see SpringRepeat
*/
private Statement withPotentialRepeat(Statement next, FrameworkMethod frameworkMethod, Object testInstance) {
- Repeat repeatAnnotation = AnnotationUtils.getAnnotation(frameworkMethod.getMethod(), Repeat.class);
- int repeat = (repeatAnnotation != null ? repeatAnnotation.value() : 1);
- return (repeat > 1 ? new SpringRepeat(next, frameworkMethod.getMethod(), repeat) : next);
+ return new SpringRepeat(next, frameworkMethod.getMethod());
}
/**
- * Return a {@link Statement} that potentially throws an exception if
- * the {@code next} statement in the execution chain takes longer than
- * a specified timeout.
- * Supports Spring's {@link Timed @Timed} annotation by returning a
- * {@link SpringFailOnTimeout} statement initialized with the configured
- * timeout (if greater than {@code 0}); otherwise, the supplied statement
- * is returned unmodified.
- * @return either a {@code SpringFailOnTimeout} or the supplied {@code Statement}
- * @see #getSpringTimeout(FrameworkMethod)
+ * Wrap the supplied {@link Statement} with a {@code SpringFailOnTimeout} statement.
+ * Supports Spring's {@link org.springframework.test.annotation.Timed @Timed}
+ * annotation.
+ * @see TestAnnotationUtils#getTimeout(java.lang.reflect.Method)
* @see SpringFailOnTimeout
*/
private Statement withPotentialTimeout(Statement next, FrameworkMethod frameworkMethod, Object testInstance) {
- long springTimeout = getSpringTimeout(frameworkMethod);
- return (springTimeout > 0 ? new SpringFailOnTimeout(next, springTimeout) : next);
- }
-
- /**
- * Retrieve the configured Spring-specific {@code timeout} from the
- * {@link Timed @Timed} annotation on the supplied
- * {@linkplain FrameworkMethod test method}.
- * @return the timeout, or {@code 0} if none was specified
- */
- private long getSpringTimeout(FrameworkMethod frameworkMethod) {
- AnnotationAttributes annAttrs = AnnotatedElementUtils.findAnnotationAttributes(frameworkMethod.getMethod(),
- Timed.class.getName());
- if (annAttrs == null) {
- return 0;
- }
- else {
- long millis = annAttrs.
*
- *
@@ -180,54 +176,25 @@ public class SpringMethodRule implements MethodRule {
}
/**
- * Return a {@link Statement} that potentially repeats the execution of
- * the {@code next} statement.
- *