KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > runner > junit3 > JUnit3TestReference


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  * IBM Corporation - initial API and implementation
10  * David Saff (saff@mit.edu) - initial API and implementation
11  * (bug 102632: [JUnit] Support for JUnit 4.)
12  *******************************************************************************/

13
14 package org.eclipse.jdt.internal.junit.runner.junit3;
15
16 import java.io.PrintWriter JavaDoc;
17 import java.io.StringWriter JavaDoc;
18 import java.lang.reflect.Field JavaDoc;
19 import java.lang.reflect.Method JavaDoc;
20 import java.text.MessageFormat JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23
24 import junit.extensions.TestDecorator;
25 import junit.framework.Test;
26 import junit.framework.TestCase;
27 import junit.framework.TestResult;
28 import junit.framework.TestSuite;
29
30 import org.eclipse.jdt.internal.junit.runner.FailedComparison;
31 import org.eclipse.jdt.internal.junit.runner.IClassifiesThrowables;
32 import org.eclipse.jdt.internal.junit.runner.IListensToTestExecutions;
33 import org.eclipse.jdt.internal.junit.runner.IStopListener;
34 import org.eclipse.jdt.internal.junit.runner.ITestIdentifier;
35 import org.eclipse.jdt.internal.junit.runner.ITestReference;
36 import org.eclipse.jdt.internal.junit.runner.IVisitsTestTrees;
37 import org.eclipse.jdt.internal.junit.runner.MessageIds;
38 import org.eclipse.jdt.internal.junit.runner.TestExecution;
39 import org.eclipse.jdt.internal.junit.runner.TestReferenceFailure;
40
41 public class JUnit3TestReference implements ITestReference {
42     
43     private final Test fTest;
44     
45     public static Object JavaDoc getField(Object JavaDoc object, String JavaDoc fieldName) {
46         Class JavaDoc clazz= object.getClass();
47         try {
48             Field JavaDoc field= clazz.getDeclaredField(fieldName);
49             field.setAccessible(true);
50             return field.get(object);
51         } catch (Exception JavaDoc e) {
52             // fall through
53
}
54         return null;
55     }
56
57     public JUnit3TestReference(Test test) {
58         if (test == null)
59             throw new NullPointerException JavaDoc();
60         this.fTest= test;
61     }
62
63     /*
64      * (non-Javadoc)
65      *
66      * @see org.eclipse.jdt.internal.junit.runner.TestReference#countTestCases()
67      */

68     public int countTestCases() {
69         return fTest.countTestCases();
70     }
71
72     public boolean equals(Object JavaDoc obj) {
73         if (! (obj instanceof JUnit3TestReference))
74             return false;
75         
76         JUnit3TestReference ref= (JUnit3TestReference) obj;
77         return ref.fTest.equals(fTest);
78     }
79
80     public int hashCode() {
81         return fTest.hashCode();
82     }
83
84     /*
85      * (non-Javadoc)
86      *
87      * @see org.eclipse.jdt.internal.junit.runner.TestReference#getName()
88      */

89     public String JavaDoc getName() {
90         if (isJUnit4TestCaseAdapter(fTest)) {
91             Method JavaDoc method= (Method JavaDoc) callJUnit4GetterMethod(fTest, "getTestMethod"); //$NON-NLS-1$
92
return MessageFormat.format(
93                     MessageIds.TEST_IDENTIFIER_MESSAGE_FORMAT, new String JavaDoc[] { method.getName(), method.getDeclaringClass().getName() });
94         }
95         if (fTest instanceof TestCase) {
96             TestCase testCase= (TestCase) fTest;
97             return MessageFormat.format(MessageIds.TEST_IDENTIFIER_MESSAGE_FORMAT, new String JavaDoc[] { testCase.getName(), fTest.getClass().getName() });
98         }
99         if (fTest instanceof TestSuite) {
100             TestSuite suite= (TestSuite) fTest;
101             if (suite.getName() != null)
102                 return suite.getName();
103             return suite.getClass().getName();
104         }
105         if (fTest instanceof TestDecorator) {
106             TestDecorator decorator= (TestDecorator) fTest;
107             return decorator.getClass().getName();
108         }
109         if (isJUnit4TestSuiteAdapter(fTest)) {
110             Class JavaDoc testClass= (Class JavaDoc) callJUnit4GetterMethod(fTest, "getTestClass"); //$NON-NLS-1$
111
return testClass.getName();
112         }
113         return fTest.toString();
114     }
115
116     public Test getTest() {
117         return fTest;
118     }
119
120     public void run(TestExecution execution) {
121         final TestResult testResult= new TestResult();
122         testResult.addListener(new JUnit3Listener(execution));
123         execution.addStopListener(new IStopListener() {
124             public void stop() {
125                 testResult.stop();
126             }
127         });
128         TestResult tr= testResult;
129
130         fTest.run(tr);
131     }
132
133     public void sendTree(IVisitsTestTrees notified) {
134         if (fTest instanceof TestDecorator) {
135             TestDecorator decorator= (TestDecorator) fTest;
136             notified.visitTreeEntry(getIdentifier(), true, 1);
137             sendTreeOfChild(decorator.getTest(), notified);
138         } else if (fTest instanceof TestSuite) {
139             TestSuite suite= (TestSuite) fTest;
140             notified.visitTreeEntry(getIdentifier(), true, suite.testCount());
141             for (int i= 0; i < suite.testCount(); i++) {
142                 sendTreeOfChild(suite.testAt(i), notified);
143             }
144         } else if (isJUnit4TestSuiteAdapter(fTest)) {
145             notified.visitTreeEntry(getIdentifier(), true, fTest.countTestCases());
146             List JavaDoc tests= (List JavaDoc) callJUnit4GetterMethod(fTest, "getTests"); //$NON-NLS-1$
147
for (Iterator JavaDoc iter= tests.iterator(); iter.hasNext();) {
148                 sendTreeOfChild((Test) iter.next(), notified);
149             }
150         } else {
151             notified.visitTreeEntry(getIdentifier(), false, fTest.countTestCases());
152         }
153     }
154
155     void sendFailure(Throwable JavaDoc throwable, IClassifiesThrowables classifier, String JavaDoc status, IListensToTestExecutions notified) {
156         TestReferenceFailure failure;
157         try {
158             failure= new TestReferenceFailure(getIdentifier(), status, classifier.getTrace(throwable));
159             if (classifier.isComparisonFailure(throwable)) {
160                 // transmit the expected and the actual string
161
Object JavaDoc expected= JUnit3TestReference.getField(throwable, "fExpected"); //$NON-NLS-1$
162
Object JavaDoc actual= JUnit3TestReference.getField(throwable, "fActual"); //$NON-NLS-1$
163
if (expected != null && actual != null) {
164                     failure.setComparison(new FailedComparison((String JavaDoc) expected, (String JavaDoc) actual));
165                 }
166             }
167         } catch (RuntimeException JavaDoc e) {
168             StringWriter JavaDoc stringWriter= new StringWriter JavaDoc();
169             e.printStackTrace(new PrintWriter JavaDoc(stringWriter));
170             failure= new TestReferenceFailure(getIdentifier(), MessageIds.TEST_FAILED, stringWriter.getBuffer().toString(), null);
171         }
172         notified.notifyTestFailed(failure);
173     }
174
175     private Object JavaDoc callJUnit4GetterMethod(Test test, String JavaDoc methodName) {
176         Object JavaDoc result;
177         try {
178             Method JavaDoc method= test.getClass().getMethod(methodName, new Class JavaDoc[0]);
179             result= method.invoke(test, new Object JavaDoc[0]);
180         } catch (Exception JavaDoc e) {
181             e.printStackTrace(System.err);
182             result= null;
183         }
184         return result;
185     }
186
187     private boolean isJUnit4TestCaseAdapter(Test test) {
188         return test.getClass().getName().equals("junit.framework.JUnit4TestCaseAdapter"); //$NON-NLS-1$
189
}
190
191     private boolean isJUnit4TestSuiteAdapter(Test test) {
192         String JavaDoc name= test.getClass().getName();
193         return name.endsWith("JUnit4TestAdapter") && name.startsWith("junit.framework"); //$NON-NLS-1$ //$NON-NLS-2$
194
}
195
196     private void sendTreeOfChild(Test test, IVisitsTestTrees notified) {
197         new JUnit3TestReference(test).sendTree(notified);
198     }
199
200     public ITestIdentifier getIdentifier() {
201         return new JUnit3Identifier(this);
202     }
203 }
204
Popular Tags