@ -84,6 +84,10 @@ public class RepeatRule implements TestRule {
public static class RepeatedTestException extends RuntimeException {
public static class RepeatedTestException extends RuntimeException {
private static final long serialVersionUID = 1L ;
private static final long serialVersionUID = 1L ;
public RepeatedTestException ( String message ) {
super ( message ) ;
}
public RepeatedTestException ( String message , Throwable cause ) {
public RepeatedTestException ( String message , Throwable cause ) {
super ( message , cause ) ;
super ( message , cause ) ;
}
}
@ -93,28 +97,45 @@ public class RepeatRule implements TestRule {
private final int repetitions ;
private final int repetitions ;
private boolean abortOnFailure ;
private final Statement statement ;
private final Statement statement ;
private RepeatStatement ( int repetitions , Statement statement ) {
private RepeatStatement ( int repetitions , boolean abortOnFailure ,
Statement statement ) {
this . repetitions = repetitions ;
this . repetitions = repetitions ;
this . abortOnFailure = abortOnFailure ;
this . statement = statement ;
this . statement = statement ;
}
}
@Override
@Override
public void evaluate ( ) throws Throwable {
public void evaluate ( ) throws Throwable {
int failures = 0 ;
for ( int i = 0 ; i < repetitions ; i + + ) {
for ( int i = 0 ; i < repetitions ; i + + ) {
try {
try {
statement . evaluate ( ) ;
statement . evaluate ( ) ;
} catch ( Throwable e ) {
} catch ( Throwable e ) {
failures + = 1 ;
RepeatedTestException ex = new RepeatedTestException (
RepeatedTestException ex = new RepeatedTestException (
MessageFormat . format (
MessageFormat . format (
"Repeated test failed when run for the {0}. time" ,
"Repeated test failed when run for the {0}. time" ,
Integer . valueOf ( i + 1 ) ) ,
Integer . valueOf ( i + 1 ) ) ,
e ) ;
e ) ;
LOG . log ( Level . SEVERE , ex . getMessage ( ) , ex ) ;
LOG . log ( Level . SEVERE , ex . getMessage ( ) , ex ) ;
throw ex ;
if ( abortOnFailure ) {
throw ex ;
}
}
}
}
}
if ( failures > 0 ) {
RepeatedTestException e = new RepeatedTestException (
MessageFormat . format (
"Test failed {0} times out of {1} repeated executions" ,
Integer . valueOf ( failures ) ,
Integer . valueOf ( repetitions ) ) ) ;
LOG . log ( Level . SEVERE , e . getMessage ( ) , e ) ;
throw e ;
}
}
}
}
}
@ -125,7 +146,8 @@ public class RepeatRule implements TestRule {
Repeat repeat = description . getAnnotation ( Repeat . class ) ;
Repeat repeat = description . getAnnotation ( Repeat . class ) ;
if ( repeat ! = null ) {
if ( repeat ! = null ) {
int n = repeat . n ( ) ;
int n = repeat . n ( ) ;
result = new RepeatStatement ( n , statement ) ;
boolean abortOnFailure = repeat . abortOnFailure ( ) ;
result = new RepeatStatement ( n , abortOnFailure , statement ) ;
}
}
return result ;
return result ;
}
}