package org.junit.tests.experimental.rules;

import static junit.framework.Assert.fail;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assume.assumeTrue;
import static org.junit.runner.JUnitCore.runClasses;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;

public class TestWatcherTest {
	public static class ViolatedAssumptionTest {
		private static StringBuilder watchedLog= new StringBuilder();

		@Rule
		public TestRule watcher= new LoggingTestWatcher(watchedLog);

		@Test
		public void succeeds() {
			assumeTrue(false);
		}
	}

	@Test
	public void neitherLogSuccessNorFailedForViolatedAssumption() {
		ViolatedAssumptionTest.watchedLog= new StringBuilder();
		runClasses(ViolatedAssumptionTest.class);
		assertThat(ViolatedAssumptionTest.watchedLog.toString(),
				is("starting finished "));
	}

	public static class FailingTest {
		private static StringBuilder watchedLog= new StringBuilder();

		@Rule
		public TestRule watcher= new LoggingTestWatcher(watchedLog);

		@Test
		public void succeeds() {
			fail();
		}
	}

	@Test
	public void logFailingTest() {
		FailingTest.watchedLog= new StringBuilder();
		runClasses(FailingTest.class);
		assertThat(FailingTest.watchedLog.toString(),
				is("starting failed finished "));
	}
}
@@ -239,114 +239,378 @@
 nit.
-runner.JUnitCore.runClasses;%0Aimport org.junit.Rule;%0Aimport org.junit.Test;%0Aimport org.junit.rules.TestRule
+experimental.results.PrintableResult.testResult;%0Aimport static org.junit.experimental.results.ResultMatchers.*;%0Aimport static org.junit.runner.JUnitCore.runClasses;%0Aimport org.junit.Rule;%0Aimport org.junit.Test;%0Aimport org.junit.experimental.results.PrintableResult;%0Aimport org.junit.rules.TestRule;%0Aimport org.junit.rules.TestWatcher;%0Aimport org.junit.runner.Description
 ;%0A%0Ap
@@ -1589,9 +1589,653 @@
 %22));%0A%09%7D%0A
+%09%0A%09public static class TestWatcherThrowsExceptionTest %7B%0A%09%09@Rule%0A%09%09public TestRule watcher= new TestWatcher() %7B%0A%09%09%09@Override%0A%09%09%09protected void failed(Throwable e, Description description) %7B%0A%09%09%09%09throw new RuntimeException(%22watcher failure%22);%0A%09%09%09%7D%0A%09%09%7D;%0A%0A%09%09@Test%0A%09%09public void fails() %7B%0A%09%09%09throw new IllegalArgumentException(%22test failure%22);%0A%09%09%7D%0A%09%7D%0A%09%0A%09@Test%0A%09public void testWatcherThrowsException() %7B%0A%09%09PrintableResult result= testResult(TestWatcherThrowsExceptionTest.class);%0A%09%09assertThat(result, failureCountIs(2));%0A%09%09assertThat(result, hasFailureContaining(%22test failure%22));%0A%09%09assertThat(result, hasFailureContaining(%22watcher failure%22));%0A%09%09%0A%09%7D%0A
 %7D

Throw MulitpleFailureException