001/* 002 * Copyright 2002-2015 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.context.junit4.statements; 018 019import java.lang.reflect.Method; 020 021import org.apache.commons.logging.Log; 022import org.apache.commons.logging.LogFactory; 023import org.junit.runners.model.Statement; 024 025import org.springframework.test.annotation.TestAnnotationUtils; 026 027/** 028 * {@code SpringRepeat} is a custom JUnit {@link Statement} which adds support 029 * for Spring's {@link org.springframework.test.annotation.Repeat @Repeat} 030 * annotation by repeating the test the specified number of times. 031 * 032 * @author Sam Brannen 033 * @since 3.0 034 * @see #evaluate() 035 */ 036public class SpringRepeat extends Statement { 037 038 protected static final Log logger = LogFactory.getLog(SpringRepeat.class); 039 040 private final Statement next; 041 042 private final Method testMethod; 043 044 private final int repeat; 045 046 047 /** 048 * Construct a new {@code SpringRepeat} statement for the supplied 049 * {@code testMethod}, retrieving the configured repeat count from the 050 * {@code @Repeat} annotation on the supplied method. 051 * @param next the next {@code Statement} in the execution chain 052 * @param testMethod the current test method 053 * @see TestAnnotationUtils#getRepeatCount(Method) 054 */ 055 public SpringRepeat(Statement next, Method testMethod) { 056 this(next, testMethod, TestAnnotationUtils.getRepeatCount(testMethod)); 057 } 058 059 /** 060 * Construct a new {@code SpringRepeat} statement for the supplied 061 * {@code testMethod} and {@code repeat} count. 062 * @param next the next {@code Statement} in the execution chain 063 * @param testMethod the current test method 064 * @param repeat the configured repeat count for the current test method 065 */ 066 public SpringRepeat(Statement next, Method testMethod, int repeat) { 067 this.next = next; 068 this.testMethod = testMethod; 069 this.repeat = Math.max(1, repeat); 070 } 071 072 073 /** 074 * Evaluate the next {@link Statement statement} in the execution chain 075 * repeatedly, using the specified repeat count. 076 */ 077 @Override 078 public void evaluate() throws Throwable { 079 for (int i = 0; i < this.repeat; i++) { 080 if (this.repeat > 1 && logger.isInfoEnabled()) { 081 logger.info(String.format("Repetition %d of test %s#%s()", (i + 1), 082 this.testMethod.getDeclaringClass().getSimpleName(), this.testMethod.getName())); 083 } 084 this.next.evaluate(); 085 } 086 } 087 088}