1 13 14 package org.eclipse.jdt.internal.junit.runner.junit3; 15 16 import java.lang.reflect.Constructor ; 17 import java.lang.reflect.InvocationTargetException ; 18 import java.lang.reflect.Method ; 19 import java.lang.reflect.Modifier ; 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 SUITE_METHODNAME= "suite"; private static final String SET_UP_TEST_METHOD_NAME = "setUpTest"; 37 39 47 public ITestReference[] loadTests(Class [] testClasses, String testName, String [] failureNames, RemoteTestRunner listener) { 48 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 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 testName, Class testClass) { 68 Class [] classArgs= { String .class }; 69 Test test; 70 Constructor constructor= null; 71 try { 72 try { 73 constructor= testClass.getConstructor(classArgs); 74 test= (Test) constructor.newInstance(new Object [] { testName }); 75 } catch (NoSuchMethodException e) { 76 constructor= testClass.getConstructor(new Class [0]); 78 test= (Test) constructor.newInstance(new Object [0]); 79 if (test instanceof TestCase) 80 ((TestCase) test).setName(testName); 81 } 82 if (test != null) 83 return test; 84 } catch (InstantiationException e) { 85 } catch (IllegalAccessException e) { 86 } catch (InvocationTargetException e) { 87 } catch (NoSuchMethodException e) { 88 } catch (ClassCastException e) { 89 } 90 return warning("Could not create test \'" + testName + "\' "); } 92 93 public Test getTest(Class testClass, String testName, RemoteTestRunner failureListener) { 94 if (testName != null) { 95 return setupTest(testClass, createTest(testName, testClass)); 96 } 97 Method suiteMethod= null; 98 try { 99 suiteMethod= testClass.getMethod(JUnit3TestLoader.SUITE_METHODNAME, new Class [0]); 100 } catch (Exception e) { 101 return new TestSuite(testClass); 103 } 104 if (!Modifier.isStatic(suiteMethod.getModifiers())) { 105 return warning(JUnitMessages.getString("RemoteTestRunner.error.suite.notstatic")); } 107 try { 108 Test test= (Test) suiteMethod.invoke(null, new Class [0]); if (test != null) { 110 return test; 111 } 112 return warning(JUnitMessages.getString("RemoteTestRunner.error.suite.nullreturn")); } catch (InvocationTargetException e) { 114 String message= JUnitMessages.getFormattedString("RemoteTestRunner.error.invoke", e.getTargetException().toString()); failureListener.runFailed(message, e); 116 return new TestSuite(testClass); 117 } catch (IllegalAccessException e) { 118 String message= JUnitMessages.getFormattedString("RemoteTestRunner.error.invoke", e.toString()); failureListener.runFailed(message, e); 120 return new TestSuite(testClass); 121 } 122 } 123 124 132 private Test setupTest(Class reloadedTestClass, Test reloadedTest) { 133 if (reloadedTestClass == null) 134 return reloadedTest; 135 136 Method setup= null; 137 try { 138 setup= reloadedTestClass.getMethod(SET_UP_TEST_METHOD_NAME, new Class [] { Test.class }); 139 } catch (SecurityException e1) { 140 return reloadedTest; 141 } catch (NoSuchMethodException e) { 142 return reloadedTest; 143 } 144 if (setup.getReturnType() != Test.class) 145 return warning(JUnitMessages.getString("RemoteTestRunner.error.notestreturn")); if (!Modifier.isPublic(setup.getModifiers())) 147 return warning(JUnitMessages.getString("RemoteTestRunner.error.shouldbepublic")); if (!Modifier.isStatic(setup.getModifiers())) 149 return warning(JUnitMessages.getString("RemoteTestRunner.error.shouldbestatic")); try { 151 Test test= (Test) setup.invoke(null, new Object [] { reloadedTest }); 152 if (test == null) 153 return warning(JUnitMessages.getString("RemoteTestRunner.error.nullreturn")); return test; 155 } catch (IllegalArgumentException e) { 156 return warning(JUnitMessages.getFormattedString("RemoteTestRunner.error.couldnotinvoke", e)); } catch (IllegalAccessException e) { 158 return warning(JUnitMessages.getFormattedString("RemoteTestRunner.error.couldnotinvoke", e)); } catch (InvocationTargetException e) { 160 return warning(JUnitMessages.getFormattedString("RemoteTestRunner.error.invocationexception", e.getTargetException())); } 162 } 163 164 167 private Test warning(final String message) { 168 return new TestCase("warning") { protected void runTest() { 170 fail(message); 171 } 172 }; 173 } 174 } 175 | Popular Tags |