From f13f493551f6af6740a2aa6297b926b063ac6d44 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sun, 17 May 2015 22:21:44 +0200 Subject: [PATCH] Introduce TestAnnotationUtils to reduce code duplication --- .../test/annotation/TestAnnotationUtils.java | 66 +++++++++++++++++++ .../junit4/SpringJUnit4ClassRunner.java | 56 +++++----------- .../junit4/rules/SpringMethodRule.java | 59 ++++------------- .../statements/SpringFailOnTimeout.java | 22 +++++-- .../junit4/statements/SpringRepeat.java | 27 ++++++-- 5 files changed, 132 insertions(+), 98 deletions(-) create mode 100644 spring-test/src/main/java/org/springframework/test/annotation/TestAnnotationUtils.java 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. getNumber("millis").longValue(); + return Math.max(0, millis); + } + } + + /** + * Get the repeat count configured via the {@link Repeat @Repeat} + * annotation on the supplied {@code method}. + *

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; *