KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit4 > runner > JUnit4TestListener


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * David Saff (saff@mit.edu) - initial API and implementation
10  * (bug 102632: [JUnit] Support for JUnit 4.)
11  *******************************************************************************/

12 package org.eclipse.jdt.internal.junit4.runner;
13
14 import java.io.PrintWriter JavaDoc;
15 import java.io.StringWriter JavaDoc;
16
17 import org.eclipse.jdt.internal.junit.runner.FailedComparison;
18 import org.eclipse.jdt.internal.junit.runner.IListensToTestExecutions;
19 import org.eclipse.jdt.internal.junit.runner.ITestIdentifier;
20 import org.eclipse.jdt.internal.junit.runner.MessageIds;
21 import org.eclipse.jdt.internal.junit.runner.TestReferenceFailure;
22 import org.junit.runner.Description;
23 import org.junit.runner.notification.Failure;
24 import org.junit.runner.notification.RunListener;
25
26 public class JUnit4TestListener extends RunListener {
27     
28     private static class IgnoredTestIdentifier extends JUnit4Identifier {
29         public IgnoredTestIdentifier(Description description) {
30             super(description);
31         }
32         @Override JavaDoc
33         public String JavaDoc getName() {
34             String JavaDoc name= super.getName();
35             if (name != null)
36                 return MessageIds.IGNORED_TEST_PREFIX + name;
37             return null;
38         }
39     }
40
41     
42     private final IListensToTestExecutions fNotified;
43
44     public JUnit4TestListener(IListensToTestExecutions notified) {
45         fNotified= notified;
46     }
47
48     @Override JavaDoc
49     public void testStarted(Description plan) throws Exception JavaDoc {
50         fNotified.notifyTestStarted(getIdentifier(plan));
51     }
52
53     @Override JavaDoc
54     public void testFailure(Failure failure) throws Exception JavaDoc {
55         TestReferenceFailure testReferenceFailure;
56         try {
57             Throwable JavaDoc exception= failure.getException();
58             String JavaDoc status= exception instanceof AssertionError JavaDoc ? MessageIds.TEST_FAILED : MessageIds.TEST_ERROR;
59             FailedComparison comparison= null;
60             if (exception instanceof junit.framework.ComparisonFailure) {
61                 junit.framework.ComparisonFailure comparisonFailure= (junit.framework.ComparisonFailure) exception;
62                 comparison= new FailedComparison(comparisonFailure.getExpected(), comparisonFailure.getActual());
63             } else if (exception instanceof org.junit.ComparisonFailure) {
64                 org.junit.ComparisonFailure comparisonFailure= (org.junit.ComparisonFailure) exception;
65                 comparison= new FailedComparison(comparisonFailure.getExpected(), comparisonFailure.getActual());
66             }
67             testReferenceFailure= new TestReferenceFailure(getIdentifier(failure.getDescription()), status, failure.getTrace(), comparison);
68         } catch (RuntimeException JavaDoc e) {
69             StringWriter JavaDoc stringWriter= new StringWriter JavaDoc();
70             e.printStackTrace(new PrintWriter JavaDoc(stringWriter));
71             testReferenceFailure= new TestReferenceFailure(getIdentifier(failure.getDescription()), MessageIds.TEST_FAILED, stringWriter.getBuffer().toString(), null);
72         }
73         fNotified.notifyTestFailed(testReferenceFailure);
74     }
75
76     @Override JavaDoc
77     public void testIgnored(Description plan) throws Exception JavaDoc {
78         // Send message to listeners which would be stale otherwise
79
ITestIdentifier identifier= new IgnoredTestIdentifier(plan);
80         fNotified.notifyTestStarted(identifier);
81         fNotified.notifyTestEnded(identifier);
82     }
83
84     @Override JavaDoc
85     public void testFinished(Description plan) throws Exception JavaDoc {
86         fNotified.notifyTestEnded(getIdentifier(plan));
87     }
88
89     private ITestIdentifier getIdentifier(Description plan) {
90         return new JUnit4Identifier(plan);
91     }
92 }
93
Popular Tags