在junit4中的监听接口为RunnerListener,具体的实现为:
package org.junit.runner.notification; import org.junit.runner.Description; import org.junit.runner.Result; public class RunListener { public void testRunStarted(Description description) throws Exception { } public void testRunFinished(Result result) throws Exception { } public void testStarted(Description description) throws Exception { } public void testFinished(Description description) throws Exception { } public void testFailure(Failure failure) throws Exception { } public void testAssumptionFailure(Failure failure) { } public void testIgnored(Description description) throws Exception { } }
通知的类为:
package org.junit.runner.notification; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; import org.junit.runner.Description; import org.junit.runner.Result; public class RunNotifier { private final List<RunListener> fListeners; private boolean fPleaseStop; public RunNotifier() { this.fListeners = Collections.synchronizedList(new ArrayList()); this.fPleaseStop = false; } public void addListener(RunListener listener) { this.fListeners.add(listener); } public void removeListener(RunListener listener) { this.fListeners.remove(listener); } public void fireTestRunStarted(Description description) { new SafeNotifier(description) { protected void notifyListener(RunListener each) throws Exception { each.testRunStarted(this.val$description); } }.run(); } public void fireTestRunFinished(Result result) { new SafeNotifier(result) { protected void notifyListener(RunListener each) throws Exception { each.testRunFinished(this.val$result); } }.run(); } public void fireTestStarted(Description description) throws StoppedByUserException { if (this.fPleaseStop) throw new StoppedByUserException(); new SafeNotifier(description) { protected void notifyListener(RunListener each) throws Exception { each.testStarted(this.val$description); } }.run(); } public void fireTestFailure(Failure failure) { new SafeNotifier(failure) { protected void notifyListener(RunListener each) throws Exception { each.testFailure(this.val$failure); } }.run(); } public void fireTestAssumptionFailed(Failure failure) { new SafeNotifier(failure) { protected void notifyListener(RunListener each) throws Exception { each.testAssumptionFailure(this.val$failure); } }.run(); } public void fireTestIgnored(Description description) { new SafeNotifier(description) { protected void notifyListener(RunListener each) throws Exception { each.testIgnored(this.val$description); } }.run(); } public void fireTestFinished(Description description) { new SafeNotifier(description) { protected void notifyListener(RunListener each) throws Exception { each.testFinished(this.val$description); } }.run(); } public void pleaseStop() { this.fPleaseStop = true; } public void addFirstListener(RunListener listener) { this.fListeners.add(0, listener); } private abstract class SafeNotifier { void run() { Iterator all; synchronized (RunNotifier.this.fListeners) { for (all = RunNotifier.this.fListeners.iterator(); all .hasNext();) try { notifyListener((RunListener) all.next()); } catch (Exception e) { all.remove(); RunNotifier.this.fireTestFailure(new Failure( Description.TEST_MECHANISM, e)); } } } protected abstract void notifyListener(RunListener paramRunListener) throws Exception; }
SWTBot的截屏通知为:
package org.eclipse.swtbot.swt.finder.junit; import org.apache.log4j.Logger; import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences; import org.eclipse.swtbot.swt.finder.utils.SWTUtils; import org.junit.runner.notification.Failure; import org.junit.runner.notification.RunListener; /** * Captures screenshots on failure notifications. * * @author Hans Schwaebli (Bug 259787) * @version $Id$ * @noinstantiate This class is not intended to be instantiated by clients. */ public final class ScreenshotCaptureListener extends RunListener { /** The logger. */ private static Logger log = Logger.getLogger(SWTBotApplicationLauncherClassRunner.class); /** Counts the screenshots to determine if maximum number is reached. */ private static int screenshotCounter = 0; public void testFailure(Failure failure) throws Exception { captureScreenshot(failure); } private void captureScreenshot(Failure failure) { try { int maximumScreenshots = SWTBotPreferences.MAX_ERROR_SCREENSHOT_COUNT; String fileName = SWTBotPreferences.SCREENSHOTS_DIR + "/" + failure.getTestHeader() + "." + SWTBotPreferences.SCREENSHOT_FORMAT.toLowerCase(); //$NON-NLS-1$ if (++screenshotCounter <= maximumScreenshots) { captureScreenshot(fileName); } else { log.info("No screenshot captured for '" + failure.getTestHeader() + "' because maximum number of screenshots reached: " //$NON-NLS-1$ + maximumScreenshots); } } catch (Exception e) { log.warn("Could not capture screenshot", e); //$NON-NLS-1$ } } private boolean captureScreenshot(String fileName) { return SWTUtils.captureScreenshot(fileName); } public int hashCode() { return 31; } public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; return true; } }