1 13 14 package org.eclipse.jdt.internal.junit.runner.junit3; 15 16 import java.io.PrintWriter ; 17 import java.io.StringWriter ; 18 import java.lang.reflect.Field ; 19 import java.lang.reflect.Method ; 20 import java.text.MessageFormat ; 21 import java.util.Iterator ; 22 import java.util.List ; 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 getField(Object object, String fieldName) { 46 Class clazz= object.getClass(); 47 try { 48 Field field= clazz.getDeclaredField(fieldName); 49 field.setAccessible(true); 50 return field.get(object); 51 } catch (Exception e) { 52 } 54 return null; 55 } 56 57 public JUnit3TestReference(Test test) { 58 if (test == null) 59 throw new NullPointerException (); 60 this.fTest= test; 61 } 62 63 68 public int countTestCases() { 69 return fTest.countTestCases(); 70 } 71 72 public boolean equals(Object 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 89 public String getName() { 90 if (isJUnit4TestCaseAdapter(fTest)) { 91 Method method= (Method ) callJUnit4GetterMethod(fTest, "getTestMethod"); return MessageFormat.format( 93 MessageIds.TEST_IDENTIFIER_MESSAGE_FORMAT, new String [] { 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 [] { 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 testClass= (Class ) callJUnit4GetterMethod(fTest, "getTestClass"); 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 tests= (List ) callJUnit4GetterMethod(fTest, "getTests"); for (Iterator 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 throwable, IClassifiesThrowables classifier, String status, IListensToTestExecutions notified) { 156 TestReferenceFailure failure; 157 try { 158 failure= new TestReferenceFailure(getIdentifier(), status, classifier.getTrace(throwable)); 159 if (classifier.isComparisonFailure(throwable)) { 160 Object expected= JUnit3TestReference.getField(throwable, "fExpected"); Object actual= JUnit3TestReference.getField(throwable, "fActual"); if (expected != null && actual != null) { 164 failure.setComparison(new FailedComparison((String ) expected, (String ) actual)); 165 } 166 } 167 } catch (RuntimeException e) { 168 StringWriter stringWriter= new StringWriter (); 169 e.printStackTrace(new PrintWriter (stringWriter)); 170 failure= new TestReferenceFailure(getIdentifier(), MessageIds.TEST_FAILED, stringWriter.getBuffer().toString(), null); 171 } 172 notified.notifyTestFailed(failure); 173 } 174 175 private Object callJUnit4GetterMethod(Test test, String methodName) { 176 Object result; 177 try { 178 Method method= test.getClass().getMethod(methodName, new Class [0]); 179 result= method.invoke(test, new Object [0]); 180 } catch (Exception 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"); } 190 191 private boolean isJUnit4TestSuiteAdapter(Test test) { 192 String name= test.getClass().getName(); 193 return name.endsWith("JUnit4TestAdapter") && name.startsWith("junit.framework"); } 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 |