1 33 34 package edu.rice.cs.drjava.model.junit; 35 36 import junit.framework.*; 37 38 import java.io.File ; 39 import java.io.IOException ; 40 import java.util.Enumeration ; 41 import java.util.List ; 42 import java.util.ArrayList ; 43 44 import edu.rice.cs.util.UnexpectedException; 45 import edu.rice.cs.util.StringOps; 46 import edu.rice.cs.util.classloader.ClassFileError; 47 import edu.rice.cs.util.swing.Utilities; 48 import edu.rice.cs.util.swing.ScrollableDialog; 49 50 import java.lang.reflect.Modifier ; 51 52 56 public class JUnitTestManager { 57 58 59 private final JUnitModelCallback _jmc; 60 61 62 private JUnitTestRunner _testRunner; 63 64 65 private TestSuite _suite = null; 66 67 68 private List <String > _testClassNames = null; 69 70 71 private List <File > _testFiles = null; 72 73 74 public JUnitTestManager(JUnitModelCallback jmc) { _jmc = jmc; } 75 76 public JUnitTestRunner getTestRunner() { return _testRunner; } 77 78 83 public List <String > findTestClasses(final List <String > classNames, final List <File > files) { 84 85 87 if (_testClassNames != null && ! _testClassNames.isEmpty()) 88 throw new IllegalStateException ("Test suite is still pending!"); 89 90 _testRunner = new JUnitTestRunner(_jmc); 91 92 _testClassNames = new ArrayList <String >(); 93 _testFiles = new ArrayList <File >(); 94 _suite = new TestSuite(); 95 96 98 int i = 0; 99 try { 100 for (i = 0; i < classNames.size(); i++) { 101 String cName = classNames.get(i); 102 try { 104 if (_isTestCase(cName)) { 105 _testClassNames.add(cName); 107 _testFiles.add(files.get(i)); 108 _suite.addTest(_testRunner.getTest(cName)); 109 } 110 } 111 catch(LinkageError e) { 112 _jmc.classFileError(new ClassFileError(cName, files.get(i).getCanonicalPath(), e)); 114 } 115 } 116 } 117 catch(IOException e) { throw new UnexpectedException(e); } 118 120 return _testClassNames; 121 } 122 123 126 public boolean runTestSuite() { 127 128 if (_testClassNames == null || _testClassNames.isEmpty()) return false; 129 130 132 try { 133 TestResult result = _testRunner.doRun(_suite); 134 135 JUnitError[] errors = new JUnitError[result.errorCount() + result.failureCount()]; 136 137 Enumeration failures = result.failures(); 138 Enumeration errEnum = result.errors(); 139 140 int i = 0; 141 142 while (errEnum.hasMoreElements()) { 143 TestFailure tErr = (TestFailure) errEnum.nextElement(); 144 errors[i] = _makeJUnitError(tErr, _testClassNames, true, _testFiles); 145 i++; 146 } 147 148 while (failures.hasMoreElements()) { 149 TestFailure tFail = (TestFailure) failures.nextElement(); 150 errors[i] = _makeJUnitError(tFail, _testClassNames, false, _testFiles); 151 i++; 152 } 153 155 _jmc.testSuiteEnded(errors); 156 } 157 catch(Throwable t) { 158 JUnitError[] errors = new JUnitError[1]; 159 errors[0] = new JUnitError(null, -1, -1, t.getMessage(), 160 false, "", "", StringOps.getStackTrace(t)); 161 _jmc.testSuiteEnded(errors); 162 164 } 165 finally { 166 _suite = null; 167 _testClassNames = null; 168 _testFiles = null; 169 } 170 return true; 171 } 172 173 177 private boolean _isJUnitTest(Class c) { 178 180 return Test.class.isAssignableFrom(c) && !Modifier.isAbstract(c.getModifiers()) && 181 !Modifier.isInterface(c.getModifiers()); 182 } 183 184 185 private boolean _isTestCase(String className) { 186 try { return _isJUnitTest(_testRunner.getLoader().load(className)); } 187 catch (ClassNotFoundException cnfe) { 188 return false; } 190 } 191 192 199 private JUnitError _makeJUnitError(TestFailure failure, List <String > classNames, boolean isError, List <File > files) { 200 201 Test failedTest = failure.failedTest(); 202 String testName; 203 if (failedTest instanceof TestCase) testName = ((TestCase)failedTest).getName(); 204 else testName = failedTest.getClass().getName(); 205 206 String testString = failure.toString(); 207 int firstIndex = testString.indexOf('(') + 1; 208 int secondIndex = testString.indexOf(')'); 209 210 211 212 String className; 213 String className1 = testString.substring(firstIndex, secondIndex); 214 String className2 = testString.substring(0, firstIndex-1); 215 if (firstIndex == secondIndex) className = className2; 216 else className = className1; 217 218 String classNameAndTest = className + "." + testName; 219 String stackTrace = StringOps.getStackTrace(failure.thrownException()); 220 221 224 if (stackTrace.indexOf(className) == -1) { 225 226 String trace = failure.trace(); 227 233 trace = trace.substring(trace.indexOf('\n')+1); 234 while (trace.indexOf("junit.framework.Assert") != -1 && 235 trace.indexOf("junit.framework.Assert") < trace.indexOf("(")) { 236 242 trace = trace.substring(trace.indexOf('\n') + 1); 243 } 244 trace = trace.substring(trace.indexOf('(')+1); 245 trace = trace.substring(0, trace.indexOf(')')); 246 className = trace.substring(0,trace.indexOf(':')); 247 className = trace.substring(0,trace.lastIndexOf('.')); 248 classNameAndTest = className + "." + testName; 249 } 250 251 252 253 int lineNum = _lineNumber(stackTrace, classNameAndTest); 254 255 257 String exception = (isError) ? failure.thrownException().toString(): 258 failure.thrownException().getMessage(); 259 boolean isFailure = (failure.thrownException() instanceof AssertionFailedError) && 260 !classNameAndTest.equals("junit.framework.TestSuite$1.warning"); 261 262 281 int indexOfClass = classNames.indexOf(className); 282 File file; 283 if (indexOfClass != -1) file = files.get(indexOfClass); 284 else file = _jmc.getFileForClassName(className); 285 286 288 if (file == null) { 290 return new JUnitError(new File ("nofile"), 0, 0, exception, !isFailure, testName, className, stackTrace); 291 } 292 293 String name = file.getName(); 296 int adjLineNum; 297 if (name.endsWith(".dj0") || name.endsWith(".dj0")) adjLineNum = lineNum - 1; 298 else adjLineNum = lineNum; 299 300 return new JUnitError(file, adjLineNum, 0, exception, !isFailure, testName, className, stackTrace); 301 } 302 303 304 private int _lineNumber(String sw, String classname) { 305 int lineNum; 306 int idxClassname = sw.indexOf(classname); 307 if (idxClassname == -1) return -1; 308 309 String theLine = sw.substring(idxClassname, sw.length()); 310 311 theLine = theLine.substring(theLine.indexOf(classname), theLine.length()); 312 theLine = theLine.substring(theLine.indexOf("(") + 1, theLine.length()); 313 theLine = theLine.substring(0, theLine.indexOf(")")); 314 315 try { 316 int i = theLine.indexOf(":") + 1; 317 lineNum = Integer.parseInt(theLine.substring(i, theLine.length())) - 1; 318 } 319 catch (NumberFormatException e) { throw new UnexpectedException(e); } 320 321 return lineNum; 322 } 323 } 324 | Popular Tags |