001/*
002 * Copyright 2002-2016 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.test.annotation;
018
019import java.lang.reflect.Method;
020
021import org.springframework.core.annotation.AnnotatedElementUtils;
022
023/**
024 * Collection of utility methods for working with Spring's core testing annotations.
025 *
026 * @author Sam Brannen
027 * @since 4.2
028 */
029public class TestAnnotationUtils {
030
031        /**
032         * Get the {@code timeout} configured via the {@link Timed @Timed}
033         * annotation on the supplied {@code method}.
034         * <p>Negative configured values will be converted to {@code 0}.
035         * @return the configured timeout, or {@code 0} if the method is not
036         * annotated with {@code @Timed}
037         */
038        public static long getTimeout(Method method) {
039                Timed timed = AnnotatedElementUtils.findMergedAnnotation(method, Timed.class);
040                if (timed == null) {
041                        return 0;
042                }
043                return Math.max(0, timed.millis());
044        }
045
046        /**
047         * Get the repeat count configured via the {@link Repeat @Repeat}
048         * annotation on the supplied {@code method}.
049         * <p>Non-negative configured values will be converted to {@code 1}.
050         * @return the configured repeat count, or {@code 1} if the method is
051         * not annotated with {@code @Repeat}
052         */
053        public static int getRepeatCount(Method method) {
054                Repeat repeat = AnnotatedElementUtils.findMergedAnnotation(method, Repeat.class);
055                if (repeat == null) {
056                        return 1;
057                }
058                return Math.max(1, repeat.value());
059        }
060
061}