package org.junit.rules;

import org.junit.internal.AssumptionViolatedException;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

/**
 * TestWatcher is a base class for Rules that take note of the testing
 * action, without modifying it. For example, this class will keep a log of each
 * passing and failing test:
 * 
 * <pre>
 * public static class WatchmanTest {
 * 	private static String watchedLog;
 * 
 * 	&#064;Rule
 * 	public TestRule watchman= new TestWatcher() {
 * 		&#064;Override
 * 		protected void failed(Throwable e, Description description) {
 * 			watchedLog+= description + &quot;\n&quot;;
 * 		}
 * 
 * 		&#064;Override
 * 		protected void succeeded(Description description) {
 * 			watchedLog+= description + &quot; &quot; + &quot;success!\n&quot;;
 * 		}
 * 	};
 * 
 * 	&#064;Test
 * 	public void fails() {
 * 		fail();
 * 	}
 * 
 * 	&#064;Test
 * 	public void succeeds() {
 * 	}
 * }
 * </pre>
 */
public abstract class TestWatcher implements TestRule {
	public Statement apply(final Statement base, final Description description) {
		return new Statement() {
			@Override
			public void evaluate() throws Throwable {
				starting(description);
				try {
					base.evaluate();
					succeeded(description);
				} catch (AssumptionViolatedException e) {
					throw e;
				} catch (Throwable t) {
					failed(t, description);
					throw t;
				} finally {
					finished(description);
				}
			}
		};
	}

	/**
	 * Invoked when a test succeeds
	 * 
	 * @param description
	 */
	protected void succeeded(Description description) {
	}

	/**
	 * Invoked when a test fails
	 * 
	 * @param e 
	 * @param description
	 */
	protected void failed(Throwable e, Description description) {
	}

	/**
	 * Invoked when a test is about to start
	 * 
	 * @param description  
	 */
	protected void starting(Description description) {
	}


	/**
	 * Invoked when a test method finishes (whether passing or failing)
	 * 
	 * @param description  
	 */
	protected void finished(Description description) {
	}
}

@@ -19,16 +19,68 @@
 rules;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 import o
@@ -159,24 +159,81 @@
 escription;%0A
+import org.junit.runners.model.MultipleFailureException;%0A
 import org.j
@@ -1050,16 +1050,30 @@
  %3C/pre%3E%0A
+ * @since 4.9%0A
  */%0Apubl
@@ -1288,16 +1288,84 @@
 wable %7B%0A
+%09%09%09%09List%3CThrowable%3E errors = new ArrayList%3CThrowable%3E();%0A%09%09%09%09try %7B%0A%09
 %09%09%09%09star
@@ -1383,30 +1383,32 @@
 ption);%0A%09%09%09%09
+%09
 try %7B%0A
+%09
 %09%09%09%09%09base.ev
@@ -1422,16 +1422,17 @@
 );%0A%09%09%09%09%09
+%09
 succeede
@@ -1443,24 +1443,25 @@
 scription);%0A
+%09
 %09%09%09%09%7D catch 
@@ -1495,24 +1495,25 @@
 n e) %7B%0A%09%09%09%09%09
+%09
 throw e;%0A%09%09%09
@@ -1509,16 +1509,17 @@
 hrow e;%0A
+%09
 %09%09%09%09%7D ca
@@ -1534,24 +1534,46 @@
 owable t) %7B%0A
+%09%09%09%09%09%09errors.add(t);%0A%09
 %09%09%09%09%09failed(
@@ -1594,21 +1594,8 @@
 );%0A%09
-%09%09%09%09throw t;%0A
 %09%09%09%09
@@ -1602,24 +1602,25 @@
 %7D finally %7B%0A
+%09
 %09%09%09%09%09finishe
@@ -1639,17 +1639,208 @@
 n);%0A%09%09%09%09
-%7D
+%09%7D%0A%09%09%09%09%7D catch (AssumptionViolatedException e) %7B%0A%09%09%09%09%09throw e;%0A%09%09%09%09%7D catch (Throwable t) %7B%0A%09%09%09%09%09errors.add(t);%0A%09%09%09%09%7D%0A%09%09%09%09if (!errors.isEmpty())%0A%09%09%09%09%09throw new MultipleFailureException(errors);
 %0A%09%09%09%7D%0A%09%09
@@ -1841,24 +1841,25 @@
 %09%09%7D%0A%09%09%7D;%0A%09%7D%0A
+%09
 %0A%09/**%0A%09 * In
@@ -2428,9 +2428,8 @@
 ) %7B%0A%09%7D%0A%7D
-%0A

Throw MulitpleFailureException