1 11 12 package org.eclipse.jdt.internal.junit.model; 13 14 import java.io.File ; 15 import java.io.FileOutputStream ; 16 import java.io.IOException ; 17 import java.io.OutputStream ; 18 import java.util.ArrayList ; 19 import java.util.HashSet ; 20 import java.util.LinkedList ; 21 import java.util.List ; 22 23 import javax.xml.parsers.ParserConfigurationException ; 24 import javax.xml.parsers.SAXParser ; 25 import javax.xml.parsers.SAXParserFactory ; 26 import javax.xml.transform.OutputKeys ; 27 import javax.xml.transform.Transformer ; 28 import javax.xml.transform.TransformerConfigurationException ; 29 import javax.xml.transform.TransformerException ; 30 import javax.xml.transform.TransformerFactory ; 31 import javax.xml.transform.TransformerFactoryConfigurationError ; 32 import javax.xml.transform.sax.SAXSource ; 33 import javax.xml.transform.stream.StreamResult ; 34 35 import org.eclipse.core.runtime.Assert; 36 import org.eclipse.core.runtime.CoreException; 37 import org.eclipse.core.runtime.IStatus; 38 import org.eclipse.core.runtime.ListenerList; 39 40 import org.eclipse.swt.widgets.Display; 41 42 import org.eclipse.ui.IWorkbenchPage; 43 import org.eclipse.ui.IWorkbenchPart; 44 import org.eclipse.ui.PartInitException; 45 46 import org.eclipse.debug.core.DebugPlugin; 47 import org.eclipse.debug.core.ILaunch; 48 import org.eclipse.debug.core.ILaunchConfiguration; 49 import org.eclipse.debug.core.ILaunchListener; 50 import org.eclipse.debug.core.ILaunchManager; 51 52 import org.eclipse.jdt.core.IJavaProject; 53 54 import org.eclipse.jdt.internal.junit.Messages; 55 import org.eclipse.jdt.internal.junit.launcher.JUnitLaunchConfigurationConstants; 56 import org.eclipse.jdt.internal.junit.model.TestElement.Status; 57 import org.eclipse.jdt.internal.junit.ui.JUnitPlugin; 58 import org.eclipse.jdt.internal.junit.ui.JUnitPreferencesConstants; 59 import org.eclipse.jdt.internal.junit.ui.TestRunnerViewPart; 60 61 import org.xml.sax.InputSource ; 62 import org.xml.sax.SAXException ; 63 64 67 public final class JUnitModel { 68 69 private final class JUnitLaunchListener implements ILaunchListener { 70 71 76 private HashSet fTrackedLaunches= new HashSet (20); 77 78 81 public void launchAdded(ILaunch launch) { 82 fTrackedLaunches.add(launch); 83 } 84 85 88 public void launchRemoved(final ILaunch launch) { 89 fTrackedLaunches.remove(launch); 90 } 99 100 103 public void launchChanged(final ILaunch launch) { 104 if (!fTrackedLaunches.contains(launch)) 105 return; 106 107 ILaunchConfiguration config= launch.getLaunchConfiguration(); 108 if (config == null) 109 return; 110 111 final IJavaProject javaProject= JUnitLaunchConfigurationConstants.getJavaProject(config); 112 if (javaProject == null) 113 return; 114 115 String portStr= launch.getAttribute(JUnitLaunchConfigurationConstants.ATTR_PORT); 117 if (portStr == null) 118 return; 119 try { 120 final int port= Integer.parseInt(portStr); 121 fTrackedLaunches.remove(launch); 122 getDisplay().asyncExec(new Runnable () { 123 public void run() { 124 connectTestRunner(launch, javaProject, port); 125 } 126 }); 127 } catch (NumberFormatException e) { 128 return; 129 } 130 } 131 132 private void connectTestRunner(ILaunch launch, IJavaProject javaProject, int port) { 133 showTestRunnerViewPartInActivePage(findTestRunnerViewPartInActivePage()); 134 135 int maxCount= JUnitPlugin.getDefault().getPreferenceStore().getInt(JUnitPreferencesConstants.MAX_TEST_RUNS); 138 int toDelete= fTestRunSessions.size() - maxCount; 139 while (toDelete > 0) { 140 toDelete--; 141 TestRunSession session= (TestRunSession) fTestRunSessions.removeLast(); 142 notifyTestRunSessionRemoved(session); 143 } 144 145 TestRunSession testRunSession= new TestRunSession(launch, javaProject, port); 146 addTestRunSession(testRunSession); 147 } 148 149 private TestRunnerViewPart showTestRunnerViewPartInActivePage(TestRunnerViewPart testRunner) { 150 IWorkbenchPart activePart= null; 151 IWorkbenchPage page= null; 152 try { 153 if (testRunner != null && testRunner.isCreated()) 156 return testRunner; 157 page= JUnitPlugin.getActivePage(); 158 if (page == null) 159 return null; 160 activePart= page.getActivePart(); 161 return (TestRunnerViewPart) page.showView(TestRunnerViewPart.NAME); 163 } catch (PartInitException pie) { 164 JUnitPlugin.log(pie); 165 return null; 166 } finally{ 167 if (page != null && activePart != null) 169 page.activate(activePart); 170 } 171 } 172 173 private TestRunnerViewPart findTestRunnerViewPartInActivePage() { 174 IWorkbenchPage page= JUnitPlugin.getActivePage(); 175 if (page == null) 176 return null; 177 return (TestRunnerViewPart) page.findView(TestRunnerViewPart.NAME); 178 } 179 180 private Display getDisplay() { 181 Display display= Display.getCurrent(); 186 if (display == null) { 187 display= Display.getDefault(); 188 } 189 return display; 190 } 191 } 192 193 194 private static final class LegacyTestRunSessionListener implements ITestRunSessionListener { 195 private TestRunSession fActiveTestRunSession; 196 private ITestSessionListener fTestSessionListener; 197 198 public void sessionAdded(TestRunSession testRunSession) { 199 if (fActiveTestRunSession != null) 201 return; 202 203 fActiveTestRunSession= testRunSession; 204 205 fTestSessionListener= new ITestSessionListener() { 206 public void testAdded(TestElement testElement) { 207 } 208 209 public void sessionStarted() { 210 org.eclipse.jdt.junit.ITestRunListener[] testRunListeners= JUnitPlugin.getDefault().getTestRunListeners(); 211 for (int i= 0; i < testRunListeners.length; i++) { 212 testRunListeners[i].testRunStarted(fActiveTestRunSession.getTotalCount()); 213 } 214 } 215 public void sessionTerminated() { 216 org.eclipse.jdt.junit.ITestRunListener[] testRunListeners= JUnitPlugin.getDefault().getTestRunListeners(); 217 for (int i= 0; i < testRunListeners.length; i++) { 218 testRunListeners[i].testRunTerminated(); 219 } 220 sessionRemoved(fActiveTestRunSession); 221 } 222 public void sessionStopped(long elapsedTime) { 223 org.eclipse.jdt.junit.ITestRunListener[] testRunListeners= JUnitPlugin.getDefault().getTestRunListeners(); 224 for (int i= 0; i < testRunListeners.length; i++) { 225 testRunListeners[i].testRunStopped(elapsedTime); 226 } 227 sessionRemoved(fActiveTestRunSession); 228 } 229 public void sessionEnded(long elapsedTime) { 230 org.eclipse.jdt.junit.ITestRunListener[] testRunListeners= JUnitPlugin.getDefault().getTestRunListeners(); 231 for (int i= 0; i < testRunListeners.length; i++) { 232 testRunListeners[i].testRunEnded(elapsedTime); 233 } 234 sessionRemoved(fActiveTestRunSession); 235 } 236 public void runningBegins() { 237 } 239 public void testStarted(TestCaseElement testCaseElement) { 240 org.eclipse.jdt.junit.ITestRunListener[] testRunListeners= JUnitPlugin.getDefault().getTestRunListeners(); 241 for (int i= 0; i < testRunListeners.length; i++) { 242 testRunListeners[i].testStarted(testCaseElement.getId(), testCaseElement.getTestName()); 243 } 244 } 245 246 public void testFailed(TestElement testElement, Status status, String trace, String expected, String actual) { 247 org.eclipse.jdt.junit.ITestRunListener[] testRunListeners= JUnitPlugin.getDefault().getTestRunListeners(); 248 for (int i= 0; i < testRunListeners.length; i++) { 249 testRunListeners[i].testFailed(status.getOldCode(), testElement.getId(), testElement.getTestName(), trace); 250 } 251 } 252 253 public void testEnded(TestCaseElement testCaseElement) { 254 org.eclipse.jdt.junit.ITestRunListener[] testRunListeners= JUnitPlugin.getDefault().getTestRunListeners(); 255 for (int i= 0; i < testRunListeners.length; i++) { 256 testRunListeners[i].testEnded(testCaseElement.getId(), testCaseElement.getTestName()); 257 } 258 } 259 260 public void testReran(TestCaseElement testCaseElement, Status status, String trace, String expectedResult, String actualResult) { 261 org.eclipse.jdt.junit.ITestRunListener[] testRunListeners= JUnitPlugin.getDefault().getTestRunListeners(); 262 for (int i= 0; i < testRunListeners.length; i++) { 263 testRunListeners[i].testReran(testCaseElement.getId(), testCaseElement.getClassName(), testCaseElement.getTestMethodName(), status.getOldCode(), trace); 264 } 265 } 266 267 public boolean acceptsSwapToDisk() { 268 return true; 269 } 270 }; 271 fActiveTestRunSession.addTestSessionListener(fTestSessionListener); 272 } 273 274 public void sessionRemoved(TestRunSession testRunSession) { 275 if (fActiveTestRunSession == testRunSession) { 276 fActiveTestRunSession.removeTestSessionListener(fTestSessionListener); 277 fTestSessionListener= null; 278 fActiveTestRunSession= null; 279 } 280 } 281 } 282 283 private final ListenerList fTestRunSessionListeners= new ListenerList(); 284 287 private final LinkedList fTestRunSessions= new LinkedList (); 288 private final ILaunchListener fLaunchListener= new JUnitLaunchListener(); 289 290 293 public void start() { 294 ILaunchManager launchManager= DebugPlugin.getDefault().getLaunchManager(); 295 launchManager.addLaunchListener(fLaunchListener); 296 297 302 325 addTestRunSessionListener(new LegacyTestRunSessionListener()); 326 } 327 328 331 public void stop() { 332 ILaunchManager launchManager= DebugPlugin.getDefault().getLaunchManager(); 333 launchManager.removeLaunchListener(fLaunchListener); 334 335 File historyDirectory= JUnitPlugin.getHistoryDirectory(); 336 File [] swapFiles= historyDirectory.listFiles(); 337 if (swapFiles != null) { 338 for (int i= 0; i < swapFiles.length; i++) { 339 swapFiles[i].delete(); 340 } 341 } 342 343 } 355 356 357 public void addTestRunSessionListener(ITestRunSessionListener listener) { 358 fTestRunSessionListeners.add(listener); 359 } 360 361 public void removeTestRunSessionListener(ITestRunSessionListener listener) { 362 fTestRunSessionListeners.remove(listener); 363 } 364 365 366 371 public List getTestRunSessions() { 372 return new ArrayList (fTestRunSessions); 373 } 374 375 384 public void addTestRunSession(TestRunSession testRunSession) { 385 Assert.isNotNull(testRunSession); 386 Assert.isLegal(! fTestRunSessions.contains(testRunSession)); 387 fTestRunSessions.addFirst(testRunSession); 388 notifyTestRunSessionAdded(testRunSession); 389 } 390 391 398 public static TestRunSession importTestRunSession(File file) throws CoreException { 399 try { 400 SAXParserFactory parserFactory= SAXParserFactory.newInstance(); 401 SAXParser parser= parserFactory.newSAXParser(); 403 TestRunHandler handler= new TestRunHandler(); 404 parser.parse(file, handler); 405 TestRunSession session= handler.getTestRunSession(); 406 JUnitPlugin.getModel().addTestRunSession(session); 407 return session; 408 } catch (ParserConfigurationException e) { 409 throwImportError(file, e); 410 } catch (SAXException e) { 411 throwImportError(file, e); 412 } catch (IOException e) { 413 throwImportError(file, e); 414 } 415 return null; } 417 418 public static void importIntoTestRunSession(File swapFile, TestRunSession testRunSession) throws CoreException { 419 try { 420 SAXParserFactory parserFactory= SAXParserFactory.newInstance(); 421 SAXParser parser= parserFactory.newSAXParser(); 423 TestRunHandler handler= new TestRunHandler(testRunSession); 424 parser.parse(swapFile, handler); 425 } catch (ParserConfigurationException e) { 426 throwImportError(swapFile, e); 427 } catch (SAXException e) { 428 throwImportError(swapFile, e); 429 } catch (IOException e) { 430 throwImportError(swapFile, e); 431 } 432 } 433 434 441 public static void exportTestRunSession(TestRunSession testRunSession, File file) throws CoreException { 442 FileOutputStream out= null; 443 try { 444 out= new FileOutputStream (file); 445 exportTestRunSession(testRunSession, out); 446 447 } catch (IOException e) { 448 throwExportError(file, e); 449 } catch (TransformerConfigurationException e) { 450 throwExportError(file, e); 451 } catch (TransformerException e) { 452 throwExportError(file, e); 453 } finally { 454 if (out != null) { 455 try { 456 out.close(); 457 } catch (IOException e2) { 458 JUnitPlugin.log(e2); 459 } 460 } 461 } 462 } 463 464 public static void exportTestRunSession(TestRunSession testRunSession, OutputStream out) 465 throws TransformerFactoryConfigurationError , TransformerException { 466 467 Transformer transformer= TransformerFactory.newInstance().newTransformer(); 468 InputSource inputSource= new InputSource (); 469 SAXSource source= new SAXSource (new TestRunSessionSerializer(testRunSession), inputSource); 470 StreamResult result= new StreamResult (out); 471 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 480 try { 481 transformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "2"); } catch (IllegalArgumentException e) { 483 } 485 transformer.transform(source, result); 486 } 487 488 private static void throwExportError(File file, Exception e) throws CoreException { 489 throw new CoreException(new org.eclipse.core.runtime.Status(IStatus.ERROR, 490 JUnitPlugin.getPluginId(), 491 Messages.format(ModelMessages.JUnitModel_could_not_write, file.getAbsolutePath()), 492 e)); 493 } 494 495 private static void throwImportError(File file, Exception e) throws CoreException { 496 throw new CoreException(new org.eclipse.core.runtime.Status(IStatus.ERROR, 497 JUnitPlugin.getPluginId(), 498 Messages.format(ModelMessages.JUnitModel_could_not_read, file.getAbsolutePath()), 499 e)); 500 } 501 502 511 public void removeTestRunSession(TestRunSession testRunSession) { 512 boolean existed= fTestRunSessions.remove(testRunSession); 513 if (existed) { 514 notifyTestRunSessionRemoved(testRunSession); 515 } 516 testRunSession.removeSwapFile(); 517 } 518 519 private void notifyTestRunSessionRemoved(TestRunSession testRunSession) { 520 testRunSession.stopTestRun(); 521 ILaunch launch= testRunSession.getLaunch(); 522 if (launch != null) { 523 ILaunchManager launchManager= DebugPlugin.getDefault().getLaunchManager(); 524 launchManager.removeLaunch(launch); 525 } 526 527 Object [] listeners = fTestRunSessionListeners.getListeners(); 528 for (int i = 0; i < listeners.length; ++i) { 529 ((ITestRunSessionListener) listeners[i]).sessionRemoved(testRunSession); 530 } 531 } 532 533 private void notifyTestRunSessionAdded(TestRunSession testRunSession) { 534 Object [] listeners = fTestRunSessionListeners.getListeners(); 535 for (int i = 0; i < listeners.length; ++i) { 536 ((ITestRunSessionListener) listeners[i]).sessionAdded(testRunSession); 537 } 538 } 539 540 } 541 | Popular Tags |