1/** 2 * 3 */ 4package org.junit.rules; 5 6import org.junit.internal.runners.statements.FailOnTimeout; 7import org.junit.runner.Description; 8import org.junit.runners.model.Statement; 9 10/** 11 * The Timeout Rule applies the same timeout to all test methods in a class: 12 * 13 * <pre> 14 * public static class HasGlobalTimeout { 15 * public static String log; 16 * 17 * @Rule 18 * public MethodRule globalTimeout= new Timeout(20); 19 * 20 * @Test 21 * public void testInfiniteLoop1() { 22 * log+= "ran1"; 23 * for (;;) { 24 * } 25 * } 26 * 27 * @Test 28 * public void testInfiniteLoop2() { 29 * log+= "ran2"; 30 * for (;;) { 31 * } 32 * } 33 * } 34 * </pre> 35 */ 36public class Timeout implements TestRule { 37 private final int fMillis; 38 39 /** 40 * @param millis the millisecond timeout 41 */ 42 public Timeout(int millis) { 43 fMillis= millis; 44 } 45 46 public Statement apply(Statement base, Description description) { 47 return new FailOnTimeout(base, fMillis); 48 } 49}