Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

New test and fix for issue 80, adding support for external RunListene…

…r implementations, which get automatically loaded at startup through the Java 6+ SPI (ie, java.util.ServiceLocator). Listener implementations are specified in third-party "META-INF/services/org.junit.runner.notification.RunListener" files, made available in the runtime classpath.
  • Loading branch information...
commit 6335f54b5bbf7c7f8ecd753de88349c3c80b39d9 1 parent f361e49
@rliesenfeld authored
View
9 src/main/java/org/junit/runner/notification/RunNotifier.java
@@ -6,6 +6,7 @@
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
+import java.util.ServiceLoader;
import org.junit.internal.AssumptionViolatedException;
import org.junit.runner.Description;
@@ -22,6 +23,14 @@
private final List<RunListener> fListeners=
Collections.synchronizedList(new ArrayList<RunListener>());
private volatile boolean fPleaseStop= false;
+
+ public RunNotifier() {
+ Iterable<RunListener> externalListeners;
+ try { externalListeners = ServiceLoader.load(RunListener.class); }
+ catch (NoClassDefFoundError ignore) { return; }
+ for (RunListener listener : externalListeners)
+ fListeners.add(listener);
+ }
/** Internal use only
*/
View
4 src/test/java/org/junit/tests/AllTests.java
@@ -53,6 +53,7 @@
import org.junit.tests.listening.TestListenerTest;
import org.junit.tests.listening.TextListenerTest;
import org.junit.tests.listening.UserStopTest;
+import org.junit.tests.listening.ExternalListenerTest;
import org.junit.tests.manipulation.FilterTest;
import org.junit.tests.manipulation.FilterableTest;
import org.junit.tests.manipulation.SingleMethodTest;
@@ -162,7 +163,8 @@
RuleChainTest.class,
BlockJUnit4ClassRunnerTest.class,
MethodSorterTest.class,
- TestedOnSupplierTest.class
+ TestedOnSupplierTest.class,
+ ExternalListenerTest.class
})
public class AllTests {
public static Test suite() {
View
50 src/test/java/org/junit/tests/listening/ExternalListenerTest.java
@@ -0,0 +1,50 @@
+package org.junit.tests.listening;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.Writer;
+import java.net.URL;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.Description;
+import org.junit.runner.notification.RunListener;
+import org.junit.runner.notification.RunNotifier;
+
+public final class ExternalListenerTest {
+ public static final class CustomListener extends RunListener {
+ boolean testRunStarted;
+ public CustomListener() { externalListener = this; }
+ @Override public void testRunStarted(Description description) { testRunStarted = true; }
+ }
+
+ private static CustomListener externalListener;
+
+ public static final class OneTest {
+ @Test
+ public void nothing() {}
+ }
+
+ @BeforeClass
+ public static void createServiceProviderConfigurationFile() throws Exception {
+ URL location = CustomListener.class.getProtectionDomain().getCodeSource().getLocation();
+ File file = new File(location.getPath() + "META-INF/services/" + RunListener.class.getName());
+ file.getParentFile().mkdirs();
+
+ Writer output = new FileWriter(file);
+ output.write(CustomListener.class.getName());
+ output.close();
+
+ file.deleteOnExit();
+ }
+
+ @Test
+ public void publishTestRunEventsToExternalRunListener() {
+ new RunNotifier().fireTestRunStarted(null);
+
+ assertNotNull("External RunListener not loaded", externalListener);
+ assertTrue("Missing event on external RunListener", externalListener.testRunStarted);
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.