KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2006 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.lang.reflect.Constructor JavaDoc;
17 import java.lang.reflect.InvocationTargetException JavaDoc;
18 import java.lang.reflect.Method JavaDoc;
19 import java.lang.reflect.Modifier JavaDoc;
20
21 import junit.framework.Test;
22 import junit.framework.TestCase;
23 import junit.framework.TestSuite;
24
25 import org.eclipse.jdt.internal.junit.runner.FailuresFirstPrioritizer;
26 import org.eclipse.jdt.internal.junit.runner.ITestLoader;
27 import org.eclipse.jdt.internal.junit.runner.ITestPrioritizer;
28 import org.eclipse.jdt.internal.junit.runner.ITestReference;
29 import org.eclipse.jdt.internal.junit.runner.JUnitMessages;
30 import org.eclipse.jdt.internal.junit.runner.NullPrioritizer;
31 import org.eclipse.jdt.internal.junit.runner.RemoteTestRunner;
32
33 public class JUnit3TestLoader implements ITestLoader {
34     private static final String JavaDoc SUITE_METHODNAME= "suite"; //$NON-NLS-1$
35
private static final String JavaDoc SET_UP_TEST_METHOD_NAME = "setUpTest"; //$NON-NLS-1$
36

37     // WANT: give test loaders a schema
38

39     /*
40      * (non-Javadoc)
41      *
42      * @see org.eclipse.jdt.internal.junit.runner.TestLoader#loadSuites(java.lang.String[],
43      * java.lang.String, java.lang.String[],
44      * org.eclipse.jdt.internal.junit.runner.RunFailureListener,
45      * org.eclipse.jdt.internal.junit.runner.TestIdMap)
46      */

47     public ITestReference[] loadTests(Class JavaDoc[] testClasses, String JavaDoc testName, String JavaDoc[] failureNames, RemoteTestRunner listener) {
48         // instantiate all tests
49
ITestReference[] suites= new ITestReference[testClasses.length];
50         ITestPrioritizer prioritizer;
51
52         if (failureNames != null)
53             prioritizer= new FailuresFirstPrioritizer(failureNames);
54         else
55             prioritizer= new NullPrioritizer();
56
57         for (int i= 0; i < suites.length; i++) {
58             Class JavaDoc testClassName= testClasses[i];
59             Test test= getTest(testClassName, testName, listener);
60             prioritizer.prioritize(test);
61             suites[i]= new JUnit3TestReference(test);
62         }
63
64         return suites;
65     }
66
67     private Test createTest(String JavaDoc testName, Class JavaDoc testClass) {
68         Class JavaDoc[] classArgs= { String JavaDoc.class };
69         Test test;
70         Constructor JavaDoc constructor= null;
71         try {
72             try {
73                 constructor= testClass.getConstructor(classArgs);
74                 test= (Test) constructor.newInstance(new Object JavaDoc[] { testName });
75             } catch (NoSuchMethodException JavaDoc e) {
76                 // try the no arg constructor supported in 3.8.1
77
constructor= testClass.getConstructor(new Class JavaDoc[0]);
78                 test= (Test) constructor.newInstance(new Object JavaDoc[0]);
79                 if (test instanceof TestCase)
80                     ((TestCase) test).setName(testName);
81             }
82             if (test != null)
83                 return test;
84         } catch (InstantiationException JavaDoc e) {
85         } catch (IllegalAccessException JavaDoc e) {
86         } catch (InvocationTargetException JavaDoc e) {
87         } catch (NoSuchMethodException JavaDoc e) {
88         } catch (ClassCastException JavaDoc e) {
89         }
90         return warning("Could not create test \'" + testName + "\' "); //$NON-NLS-1$ //$NON-NLS-2$
91
}
92
93     public Test getTest(Class JavaDoc testClass, String JavaDoc testName, RemoteTestRunner failureListener) {
94         if (testName != null) {
95             return setupTest(testClass, createTest(testName, testClass));
96         }
97         Method JavaDoc suiteMethod= null;
98         try {
99             suiteMethod= testClass.getMethod(JUnit3TestLoader.SUITE_METHODNAME, new Class JavaDoc[0]);
100         } catch (Exception JavaDoc e) {
101             // try to extract a test suite automatically
102
return new TestSuite(testClass);
103         }
104         if (!Modifier.isStatic(suiteMethod.getModifiers())) {
105             return warning(JUnitMessages.getString("RemoteTestRunner.error.suite.notstatic"));//$NON-NLS-1$
106
}
107         try {
108             Test test= (Test) suiteMethod.invoke(null, new Class JavaDoc[0]); // static
109
if (test != null) {
110                 return test;
111             }
112             return warning(JUnitMessages.getString("RemoteTestRunner.error.suite.nullreturn")); //$NON-NLS-1$
113
} catch (InvocationTargetException JavaDoc e) {
114             String JavaDoc message= JUnitMessages.getFormattedString("RemoteTestRunner.error.invoke", e.getTargetException().toString()); //$NON-NLS-1$
115
failureListener.runFailed(message, e);
116             return new TestSuite(testClass);
117         } catch (IllegalAccessException JavaDoc e) {
118             String JavaDoc message= JUnitMessages.getFormattedString("RemoteTestRunner.error.invoke", e.toString()); //$NON-NLS-1$
119
failureListener.runFailed(message, e);
120             return new TestSuite(testClass);
121         }
122     }
123
124     /**
125      * Prepare a single test to be run standalone. If the test case class
126      * provides a static method Test setUpTest(Test test) then this method will
127      * be invoked. Instead of calling the test method directly the "decorated"
128      * test returned from setUpTest will be called. The purpose of this
129      * mechanism is to enable tests which requires a set-up to be run
130      * individually.
131      */

132     private Test setupTest(Class JavaDoc reloadedTestClass, Test reloadedTest) {
133         if (reloadedTestClass == null)
134             return reloadedTest;
135
136         Method JavaDoc setup= null;
137         try {
138             setup= reloadedTestClass.getMethod(SET_UP_TEST_METHOD_NAME, new Class JavaDoc[] { Test.class });
139         } catch (SecurityException JavaDoc e1) {
140             return reloadedTest;
141         } catch (NoSuchMethodException JavaDoc e) {
142             return reloadedTest;
143         }
144         if (setup.getReturnType() != Test.class)
145             return warning(JUnitMessages.getString("RemoteTestRunner.error.notestreturn")); //$NON-NLS-1$
146
if (!Modifier.isPublic(setup.getModifiers()))
147             return warning(JUnitMessages.getString("RemoteTestRunner.error.shouldbepublic")); //$NON-NLS-1$
148
if (!Modifier.isStatic(setup.getModifiers()))
149             return warning(JUnitMessages.getString("RemoteTestRunner.error.shouldbestatic")); //$NON-NLS-1$
150
try {
151             Test test= (Test) setup.invoke(null, new Object JavaDoc[] { reloadedTest });
152             if (test == null)
153                 return warning(JUnitMessages.getString("RemoteTestRunner.error.nullreturn")); //$NON-NLS-1$
154
return test;
155         } catch (IllegalArgumentException JavaDoc e) {
156             return warning(JUnitMessages.getFormattedString("RemoteTestRunner.error.couldnotinvoke", e)); //$NON-NLS-1$
157
} catch (IllegalAccessException JavaDoc e) {
158             return warning(JUnitMessages.getFormattedString("RemoteTestRunner.error.couldnotinvoke", e)); //$NON-NLS-1$
159
} catch (InvocationTargetException JavaDoc e) {
160             return warning(JUnitMessages.getFormattedString("RemoteTestRunner.error.invocationexception", e.getTargetException())); //$NON-NLS-1$
161
}
162     }
163
164     /**
165      * Returns a test which will fail and log a warning message.
166      */

167     private Test warning(final String JavaDoc message) {
168         return new TestCase("warning") { //$NON-NLS-1$
169
protected void runTest() {
170                 fail(message);
171             }
172         };
173     }
174 }
175
Popular Tags