Permalink
Browse files

Added SynchronizedRunListenerProxy with testcase

  • Loading branch information...
1 parent 4041354 commit 7c4061a4530e6157e7bf6d9ba04125c58bcb3d18 @krosenvold committed Nov 9, 2010
@@ -0,0 +1,51 @@
+package org.junit.runner.notification;
+
+import org.junit.runner.Description;
+import org.junit.runner.Result;
+
+/**
+ * Adds synchronization to a run listener, to facilitate non-threadsafe run listeners in a threading environment.
+ *
+ * @author Kristian Rosenvold
+ */
+public class SynchronizedRunListenerProxy {
+ private final RunListener target;
+
+ public SynchronizedRunListenerProxy(RunListener target) {
+ this.target = target;
+ }
+
+ public synchronized void testRunStarted(Description description)
+ throws Exception {
+ target.testRunStarted(description);
+ }
+
+ public synchronized void testRunFinished(Result result)
+ throws Exception {
+ target.testRunFinished(result);
+ }
+
+ public synchronized void testStarted(Description description)
+ throws Exception {
+ target.testStarted(description);
+ }
+
+ public synchronized void testFinished(Description description)
+ throws Exception {
+ target.testFinished(description);
+ }
+
+ public synchronized void testFailure(Failure failure)
+ throws Exception {
+ target.testFailure(failure);
+ }
+
+ public synchronized void testAssumptionFailure(Failure failure) {
+ target.testAssumptionFailure(failure);
+ }
+
+ public synchronized void testIgnored(Description description)
+ throws Exception {
+ target.testIgnored(description);
+ }
+}
@@ -0,0 +1,90 @@
+package org.junit.tests.running.classes;
+
+import org.junit.Test;
+import org.junit.runner.Description;
+import org.junit.runner.Result;
+import org.junit.runner.notification.Failure;
+import org.junit.runner.notification.RunListener;
+import org.junit.runner.notification.SynchronizedRunListenerProxy;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+import static junit.framework.Assert.assertEquals;
+
+/**
+ * @author Kristian Rosenvold
+ */
+public class SynchronizedRunlistenerTest {
+
+ @Test
+ public void checkCounts()
+ throws Exception {
+ CountingRunListener countingRunListener = new CountingRunListener();
+ SynchronizedRunListenerProxy listenerProxy = new SynchronizedRunListenerProxy(countingRunListener);
+ listenerProxy.testRunStarted(Description.createSuiteDescription(this.getClass()));
+ assertEquals(1, countingRunListener.runstarted.get());
+ listenerProxy.testStarted(Description.createSuiteDescription(this.getClass()));
+ assertEquals(1, countingRunListener.started.get());
+ listenerProxy.testFinished(Description.createSuiteDescription(this.getClass()));
+ assertEquals(1, countingRunListener.finished.get());
+ listenerProxy.testFailure(new Failure(Description.createSuiteDescription(this.getClass()), new RuntimeException("foo")));
+ assertEquals(1, countingRunListener.failure.get());
+ listenerProxy.testAssumptionFailure(new Failure(Description.createSuiteDescription(this.getClass()), new RuntimeException("foo")));
+ assertEquals(1, countingRunListener.assumptionFailure.get());
+ listenerProxy.testIgnored(Description.createSuiteDescription(this.getClass()));
+ assertEquals(1, countingRunListener.ignored.get());
+
+
+ }
+
+ class CountingRunListener extends RunListener {
+ private final AtomicInteger runstarted = new AtomicInteger();
+ private final AtomicInteger runfinished = new AtomicInteger();
+ private final AtomicInteger started = new AtomicInteger();
+ private final AtomicInteger finished = new AtomicInteger();
+ private final AtomicInteger failure = new AtomicInteger();
+ private final AtomicInteger assumptionFailure = new AtomicInteger();
+ private final AtomicInteger ignored = new AtomicInteger();
+
+ @Override
+ public void testRunStarted(Description description)
+ throws Exception {
+ runstarted.incrementAndGet();
+ }
+
+ @Override
+ public void testRunFinished(Result result)
+ throws Exception {
+ runfinished.incrementAndGet();
+ }
+
+ @Override
+ public void testStarted(Description description)
+ throws Exception {
+ started.incrementAndGet();
+ }
+
+ @Override
+ public void testFinished(Description description)
+ throws Exception {
+ finished.incrementAndGet();
+ }
+
+ @Override
+ public void testFailure(Failure failure)
+ throws Exception {
+ this.failure.incrementAndGet();
+ }
+
+ @Override
+ public void testAssumptionFailure(Failure failure) {
+ this.assumptionFailure.incrementAndGet();
+ }
+
+ @Override
+ public void testIgnored(Description description)
+ throws Exception {
+ this.ignored.incrementAndGet();
+ }
+ }
+}

0 comments on commit 7c4061a

Please sign in to comment.