KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > model > TestRunSession


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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  *******************************************************************************/

11
12 package org.eclipse.jdt.internal.junit.model;
13
14 import java.io.File JavaDoc;
15 import java.text.SimpleDateFormat JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Arrays JavaDoc;
18 import java.util.Date JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.eclipse.core.runtime.Assert;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.ListenerList;
25
26 import org.eclipse.debug.core.DebugPlugin;
27 import org.eclipse.debug.core.ILaunch;
28 import org.eclipse.debug.core.ILaunchConfiguration;
29 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
30 import org.eclipse.debug.core.ILaunchManager;
31 import org.eclipse.debug.core.ILaunchesListener2;
32
33 import org.eclipse.jdt.core.IJavaProject;
34
35 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
36
37 import org.eclipse.jdt.junit.model.ITestElement;
38 import org.eclipse.jdt.junit.model.ITestElementContainer;
39 import org.eclipse.jdt.junit.model.ITestRunSession;
40
41 import org.eclipse.jdt.internal.junit.Messages;
42 import org.eclipse.jdt.internal.junit.launcher.ITestKind;
43 import org.eclipse.jdt.internal.junit.launcher.JUnitLaunchConfigurationConstants;
44 import org.eclipse.jdt.internal.junit.model.TestElement.Status;
45 import org.eclipse.jdt.internal.junit.runner.MessageIds;
46 import org.eclipse.jdt.internal.junit.ui.JUnitMessages;
47 import org.eclipse.jdt.internal.junit.ui.JUnitPlugin;
48
49 /**
50  * A test run session holds all information about a test run, i.e.
51  * launch configuration, launch, test tree (including results).
52  */

53 public class TestRunSession implements ITestRunSession {
54
55     /**
56      * The launch, or <code>null</code> iff this session was run externally.
57      */

58     private final ILaunch fLaunch;
59     private final String JavaDoc fTestRunName;
60     /**
61      * Java project, or <code>null</code>.
62      */

63     private final IJavaProject fProject;
64
65     private final ITestKind fTestRunnerKind;
66     
67     /**
68      * Test runner client or <code>null</code>.
69      */

70     private RemoteTestRunnerClient fTestRunnerClient;
71
72     private final ListenerList/*<ITestSessionListener>*/ fSessionListeners;
73     
74     /**
75      * The model root, or <code>null</code> if swapped to disk.
76      */

77     private TestRoot fTestRoot;
78     
79     /**
80      * The test run session's cached result, or <code>null</code> if <code>fTestRoot != null</code>.
81      */

82     private Result fTestResult;
83     
84     /**
85      * Map from testId to testElement.
86      */

87     private HashMap JavaDoc/*<String, TestElement>*/ fIdToTest;
88     
89     /**
90      * The TestSuites for which additional children are expected.
91      */

92     private List JavaDoc/*<IncompleteTestSuite>*/ fIncompleteTestSuites;
93     
94     /**
95      * Suite for unrooted test case elements, or <code>null</code>.
96      */

97     private TestSuiteElement fUnrootedSuite;
98     
99     /**
100      * Number of tests started during this test run.
101      */

102     volatile int fStartedCount;
103     /**
104      * Number of tests ignored during this test run.
105      */

106     volatile int fIgnoredCount;
107     /**
108      * Number of errors during this test run.
109      */

110     volatile int fErrorCount;
111     /**
112      * Number of failures during this test run.
113      */

114     volatile int fFailureCount;
115     /**
116      * Total number of tests to run.
117      */

118     volatile int fTotalCount;
119     /**
120      * Start time in millis.
121      */

122     volatile long fStartTime;
123     volatile boolean fIsRunning;
124     
125     volatile boolean fIsStopped;
126     
127
128     /**
129      * @param testRunName
130      * @param project may be <code>null</code>
131      */

132     public TestRunSession(String JavaDoc testRunName, IJavaProject project) {
133         //TODO: check assumptions about non-null fields
134

135         fLaunch= null;
136         fProject= null; //TODO
137

138         Assert.isNotNull(testRunName);
139         fTestRunName= testRunName;
140         fTestRunnerKind= ITestKind.NULL; //TODO
141

142         fTestRoot= new TestRoot(this);
143         fIdToTest= new HashMap JavaDoc();
144         
145         fTestRunnerClient= null;
146
147         fSessionListeners= new ListenerList();
148     }
149     
150     
151     public TestRunSession(ILaunch launch, IJavaProject project, int port) {
152         Assert.isNotNull(launch);
153         
154         fLaunch= launch;
155         fProject= project;
156         
157         ILaunchConfiguration launchConfiguration= launch.getLaunchConfiguration();
158         if (launchConfiguration != null) {
159             fTestRunName= launchConfiguration.getName();
160             fTestRunnerKind= JUnitLaunchConfigurationConstants.getTestRunnerKind(launchConfiguration);
161         } else {
162             fTestRunName= project.getElementName();
163             fTestRunnerKind= ITestKind.NULL;
164         }
165         
166         fTestRoot= new TestRoot(this);
167         fIdToTest= new HashMap JavaDoc();
168         
169         fTestRunnerClient= new RemoteTestRunnerClient();
170         fTestRunnerClient.startListening(new ITestRunListener2[] { new TestSessionNotifier() }, port);
171         
172         final ILaunchManager launchManager= DebugPlugin.getDefault().getLaunchManager();
173         launchManager.addLaunchListener(new ILaunchesListener2() {
174             public void launchesTerminated(ILaunch[] launches) {
175                 if (Arrays.asList(launches).contains(fLaunch)) {
176                     if (fTestRunnerClient != null) {
177                         fTestRunnerClient.stopWaiting();
178                     }
179                     launchManager.removeLaunchListener(this);
180                 }
181             }
182             public void launchesRemoved(ILaunch[] launches) {
183                 if (Arrays.asList(launches).contains(fLaunch)) {
184                     if (fTestRunnerClient != null) {
185                         fTestRunnerClient.stopWaiting();
186                     }
187                     launchManager.removeLaunchListener(this);
188                 }
189             }
190             public void launchesChanged(ILaunch[] launches) {
191             }
192             public void launchesAdded(ILaunch[] launches) {
193             }
194         });
195
196         fSessionListeners= new ListenerList();
197         addTestSessionListener(new TestRunListenerAdapter(this));
198     }
199     
200     void reset() {
201         fStartedCount= 0;
202         fFailureCount= 0;
203         fErrorCount= 0;
204         fIgnoredCount= 0;
205         fTotalCount= 0;
206         
207         fTestRoot= new TestRoot(this);
208         fTestResult= null;
209         fIdToTest= new HashMap JavaDoc();
210     }
211     
212     /* (non-Javadoc)
213      * @see org.eclipse.jdt.junit.ITestRunSession#getProgressState()
214      */

215     public ProgressState getProgressState() {
216         if (isRunning()) {
217             return ProgressState.RUNNING;
218         }
219         if (isStopped()) {
220             return ProgressState.STOPPED;
221         }
222         return ProgressState.COMPLETED;
223     }
224     
225     /* (non-Javadoc)
226      * @see org.eclipse.jdt.junit.model.ITestElement#getTestResult(boolean)
227      */

228     public Result getTestResult(boolean includeChildren) {
229         if (fTestRoot != null) {
230             return fTestRoot.getTestResult(true);
231         } else {
232             return fTestResult;
233         }
234     }
235     
236     /* (non-Javadoc)
237      * @see org.eclipse.jdt.junit.model.ITestElementContainer#getChildren()
238      */

239     public ITestElement[] getChildren() {
240         return getTestRoot().getChildren();
241     }
242     
243     /* (non-Javadoc)
244      * @see org.eclipse.jdt.junit.model.ITestElement#getFailureTrace()
245      */

246     public FailureTrace getFailureTrace() {
247         return null;
248     }
249     
250     /* (non-Javadoc)
251      * @see org.eclipse.jdt.junit.model.ITestElement#getParentContainer()
252      */

253     public ITestElementContainer getParentContainer() {
254         return null;
255     }
256     
257     /* (non-Javadoc)
258      * @see org.eclipse.jdt.junit.model.ITestElement#getTestRunSession()
259      */

260     public ITestRunSession getTestRunSession() {
261         return this;
262     }
263     
264     
265     public TestRoot getTestRoot() {
266         swapIn(); //TODO: TestRoot should stay (e.g. for getTestRoot().getStatus())
267
return fTestRoot;
268     }
269
270     /**
271      * @return the Java project, or <code>null</code>
272      */

273     public IJavaProject getLaunchedProject() {
274         return fProject;
275     }
276     
277     public ITestKind getTestRunnerKind() {
278         return fTestRunnerKind;
279     }
280     
281     
282     /**
283      * @return the launch, or <code>null</code> iff this session was run externally
284      */

285     public ILaunch getLaunch() {
286         return fLaunch;
287     }
288     
289     public String JavaDoc getTestRunName() {
290         return fTestRunName;
291     }
292     
293     public int getErrorCount() {
294         return fErrorCount;
295     }
296
297     public int getFailureCount() {
298         return fFailureCount;
299     }
300
301     public int getStartedCount() {
302         return fStartedCount;
303     }
304
305     public int getIgnoredCount() {
306         return fIgnoredCount;
307     }
308     
309     public int getTotalCount() {
310         return fTotalCount;
311     }
312
313     public long getStartTime() {
314         return fStartTime;
315     }
316     
317     /**
318      * @return <code>true</code> iff the session has been stopped or terminated
319      */

320     public boolean isStopped() {
321         return fIsStopped;
322     }
323
324     public void addTestSessionListener(ITestSessionListener listener) {
325         swapIn();
326         fSessionListeners.add(listener);
327     }
328     
329     public void removeTestSessionListener(ITestSessionListener listener) {
330         fSessionListeners.remove(listener);
331     }
332     
333     public void swapOut() {
334         if (fTestRoot == null)
335             return;
336         if (isRunning() || isStarting() || isKeptAlive())
337             return;
338         
339         Object JavaDoc[] listeners= fSessionListeners.getListeners();
340         for (int i= 0; i < listeners.length; ++i) {
341             ITestSessionListener registered= (ITestSessionListener) listeners[i];
342             if (! registered.acceptsSwapToDisk())
343                 return;
344         }
345         
346         try {
347             File JavaDoc swapFile= getSwapFile();
348             
349             JUnitModel.exportTestRunSession(this, swapFile);
350             fTestResult= fTestRoot.getTestResult(true);
351             fTestRoot= null;
352             fTestRunnerClient= null;
353             fIdToTest= new HashMap JavaDoc();
354             fIncompleteTestSuites= null;
355             fUnrootedSuite= null;
356             
357         } catch (IllegalStateException JavaDoc e) {
358             JUnitPlugin.log(e);
359         } catch (CoreException e) {
360             JUnitPlugin.log(e);
361         }
362     }
363     
364     private boolean isStarting() {
365         return getStartTime() == 0 && fLaunch != null && ! fLaunch.isTerminated();
366     }
367
368
369     public void removeSwapFile() {
370         File JavaDoc swapFile= getSwapFile();
371         if (swapFile.exists())
372             swapFile.delete();
373     }
374
375     private File JavaDoc getSwapFile() throws IllegalStateException JavaDoc {
376         File JavaDoc historyDir= JUnitPlugin.getHistoryDirectory();
377         String JavaDoc isoTime= new SimpleDateFormat JavaDoc("yyyyMMdd-HHmmss.SSS").format(new Date JavaDoc(getStartTime())); //$NON-NLS-1$
378
String JavaDoc swapFileName= isoTime + ".xml"; //$NON-NLS-1$
379
return new File JavaDoc(historyDir, swapFileName);
380     }
381
382
383     public void swapIn() {
384         if (fTestRoot != null)
385             return;
386
387         try {
388             JUnitModel.importIntoTestRunSession(getSwapFile(), this);
389         } catch (IllegalStateException JavaDoc e) {
390             JUnitPlugin.log(e);
391             fTestRoot= new TestRoot(this);
392             fTestResult= null;
393         } catch (CoreException e) {
394             JUnitPlugin.log(e);
395             fTestRoot= new TestRoot(this);
396             fTestResult= null;
397         }
398     }
399
400     public void stopTestRun() {
401         if (isRunning() || ! isKeptAlive())
402             fIsStopped= true;
403         if (fTestRunnerClient != null)
404             fTestRunnerClient.stopTest();
405     }
406
407     /**
408      * @return <code>true</code> iff the runtime VM of this test session is still alive
409      */

410     public boolean isKeptAlive() {
411         if (fTestRunnerClient != null
412                 && fLaunch != null
413                 && fTestRunnerClient.isRunning()
414                 && ILaunchManager.DEBUG_MODE.equals(fLaunch.getLaunchMode())) {
415             ILaunchConfiguration config= fLaunch.getLaunchConfiguration();
416             try {
417                 return config != null
418                 && config.getAttribute(JUnitLaunchConfigurationConstants.ATTR_KEEPRUNNING, false);
419             } catch (CoreException e) {
420                 return false;
421             }
422             
423         } else {
424             return false;
425         }
426     }
427
428     /**
429      * @return <code>true</code> iff this session has been started, but not ended nor stopped nor terminated
430      */

431     public boolean isRunning() {
432         return fIsRunning;
433     }
434     
435     /**
436      * @param testId
437      * @param className
438      * @param testName
439      * @param launchMode
440      * @return <code>false</code> iff the rerun could not be started
441      * @throws CoreException
442      */

443     public boolean rerunTest(String JavaDoc testId, String JavaDoc className, String JavaDoc testName, String JavaDoc launchMode) throws CoreException {
444         if (isKeptAlive()) {
445             Status status= ((TestCaseElement) getTestElement(testId)).getStatus();
446             if (status == Status.ERROR) {
447                 fErrorCount--;
448             } else if (status == Status.FAILURE) {
449                 fFailureCount--;
450             }
451             fTestRunnerClient.rerunTest(testId, className, testName);
452             return true;
453             
454         } else if (fLaunch != null) {
455             // run the selected test using the previous launch configuration
456
ILaunchConfiguration launchConfiguration= fLaunch.getLaunchConfiguration();
457             if (launchConfiguration != null) {
458
459                 String JavaDoc name= className;
460                 if (testName != null)
461                     name+= "."+testName; //$NON-NLS-1$
462
String JavaDoc configName= Messages.format(JUnitMessages.TestRunnerViewPart_configName, name);
463                 ILaunchConfigurationWorkingCopy tmp= launchConfiguration.copy(configName);
464                 // fix for bug: 64838 junit view run single test does not use correct class [JUnit]
465
tmp.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, className);
466                 // reset the container
467
tmp.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_CONTAINER, ""); //$NON-NLS-1$
468
if (testName != null) {
469                     tmp.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_METHOD_NAME, testName);
470                     // String args= "-rerun "+testId;
471
// tmp.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS, args);
472
}
473                 tmp.launch(launchMode, null);
474                 return true;
475             }
476         }
477         
478         return false;
479     }
480     
481     public TestElement getTestElement(String JavaDoc id) {
482         return (TestElement) fIdToTest.get(id);
483     }
484
485     private TestElement addTreeEntry(String JavaDoc treeEntry) {
486         // format: testId","testName","isSuite","testcount
487
int index0= treeEntry.indexOf(',');
488         String JavaDoc id= treeEntry.substring(0, index0);
489         
490         StringBuffer JavaDoc testNameBuffer= new StringBuffer JavaDoc(100);
491         int index1= scanTestName(treeEntry, index0 + 1, testNameBuffer);
492         String JavaDoc testName= testNameBuffer.toString().trim();
493         
494         int index2= treeEntry.indexOf(',', index1 + 1);
495         boolean isSuite= treeEntry.substring(index1 + 1, index2).equals("true"); //$NON-NLS-1$
496

497         int testCount= Integer.parseInt(treeEntry.substring(index2 + 1));
498         
499         if (fIncompleteTestSuites.isEmpty()) {
500             return createTestElement(fTestRoot, id, testName, isSuite, testCount);
501         } else {
502             int suiteIndex= fIncompleteTestSuites.size() - 1;
503             IncompleteTestSuite openSuite= (IncompleteTestSuite) fIncompleteTestSuites.get(suiteIndex);
504             openSuite.fOutstandingChildren--;
505             if (openSuite.fOutstandingChildren <= 0)
506                 fIncompleteTestSuites.remove(suiteIndex);
507             return createTestElement(openSuite.fTestSuiteElement, id, testName, isSuite, testCount);
508         }
509     }
510
511     public TestElement createTestElement(TestSuiteElement parent, String JavaDoc id, String JavaDoc testName, boolean isSuite, int testCount) {
512         TestElement testElement;
513         if (isSuite) {
514             TestSuiteElement testSuiteElement= new TestSuiteElement(parent, id, testName, testCount);
515             testElement= testSuiteElement;
516             if (testCount > 0)
517                 fIncompleteTestSuites.add(new IncompleteTestSuite(testSuiteElement, testCount));
518         } else {
519             testElement= new TestCaseElement(parent, id, testName);
520         }
521         fIdToTest.put(id, testElement);
522         return testElement;
523     }
524     
525     /**
526      * Append the test name from <code>s</code> to <code>testName</code>.
527      *
528      * @param s the string to scan
529      * @param start the offset of the first character in <code>s</code>
530      * @param testName the result
531      *
532      * @return the index of the next ','
533      */

534     private int scanTestName(String JavaDoc s, int start, StringBuffer JavaDoc testName) {
535         boolean inQuote= false;
536         int i= start;
537         for (; i < s.length(); i++) {
538             char c= s.charAt(i);
539             if (c == '\\' && !inQuote) {
540                 inQuote= true;
541                 continue;
542             } else if (inQuote) {
543                 inQuote= false;
544                 testName.append(c);
545             } else if (c == ',')
546                 break;
547             else
548                 testName.append(c);
549         }
550         return i;
551     }
552
553     /**
554      * An {@link ITestRunListener2} that listens to events from the
555      * {@link RemoteTestRunnerClient} and translates them into high-level model
556      * events (broadcasted to {@link ITestSessionListener}s).
557      */

558     private class TestSessionNotifier implements ITestRunListener2 {
559         
560         public void testRunStarted(int testCount) {
561             fIncompleteTestSuites= new ArrayList JavaDoc();
562             
563             fStartedCount= 0;
564             fIgnoredCount= 0;
565             fFailureCount= 0;
566             fErrorCount= 0;
567             fTotalCount= testCount;
568             
569             fStartTime= System.currentTimeMillis();
570             fIsRunning= true;
571             
572             Object JavaDoc[] listeners= fSessionListeners.getListeners();
573             for (int i= 0; i < listeners.length; ++i) {
574                 ((ITestSessionListener) listeners[i]).sessionStarted();
575             }
576         }
577     
578         public void testRunEnded(long elapsedTime) {
579             fIsRunning= false;
580             
581             Object JavaDoc[] listeners= fSessionListeners.getListeners();
582             for (int i= 0; i < listeners.length; ++i) {
583                 ((ITestSessionListener) listeners[i]).sessionEnded(elapsedTime);
584             }
585         }
586     
587         public void testRunStopped(long elapsedTime) {
588             fIsRunning= false;
589             fIsStopped= true;
590             
591             Object JavaDoc[] listeners= fSessionListeners.getListeners();
592             for (int i= 0; i < listeners.length; ++i) {
593                 ((ITestSessionListener) listeners[i]).sessionStopped(elapsedTime);
594             }
595         }
596     
597         public void testRunTerminated() {
598             fIsRunning= false;
599             fIsStopped= true;
600             
601             Object JavaDoc[] listeners= fSessionListeners.getListeners();
602             for (int i= 0; i < listeners.length; ++i) {
603                 ((ITestSessionListener) listeners[i]).sessionTerminated();
604             }
605         }
606     
607         /* (non-Javadoc)
608          * @see org.eclipse.jdt.internal.junit.model.ITestRunListener2#testTreeEntry(java.lang.String)
609          */

610         public void testTreeEntry(String JavaDoc description) {
611             TestElement testElement= addTreeEntry(description);
612             
613             Object JavaDoc[] listeners= fSessionListeners.getListeners();
614             for (int i= 0; i < listeners.length; ++i) {
615                 ((ITestSessionListener) listeners[i]).testAdded(testElement);
616             }
617         }
618     
619         private TestElement createUnrootedTestElement(String JavaDoc testId, String JavaDoc testName) {
620             TestSuiteElement unrootedSuite= getUnrootedSuite();
621             TestElement testElement= createTestElement(unrootedSuite, testId, testName, false, 1);
622             
623             Object JavaDoc[] listeners= fSessionListeners.getListeners();
624             for (int i= 0; i < listeners.length; ++i) {
625                 ((ITestSessionListener) listeners[i]).testAdded(testElement);
626             }
627             
628             return testElement;
629         }
630
631         private TestSuiteElement getUnrootedSuite() {
632             if (fUnrootedSuite == null) {
633                 fUnrootedSuite= (TestSuiteElement) createTestElement(fTestRoot, "-2", JUnitMessages.TestRunSession_unrootedTests, true, 0); //$NON-NLS-1$
634
}
635             return fUnrootedSuite;
636         }
637
638         public void testStarted(String JavaDoc testId, String JavaDoc testName) {
639             if (fStartedCount == 0) {
640                 Object JavaDoc[] listeners= fSessionListeners.getListeners();
641                 for (int i= 0; i < listeners.length; ++i) {
642                     ((ITestSessionListener) listeners[i]).runningBegins();
643                 }
644             }
645             TestElement testElement= getTestElement(testId);
646             if (testElement == null) {
647                 testElement= createUnrootedTestElement(testId, testName);
648             } else if (! (testElement instanceof TestCaseElement)) {
649                 logUnexpectedTest(testId, testElement);
650                 return;
651             }
652             TestCaseElement testCaseElement= (TestCaseElement) testElement;
653             setStatus(testCaseElement, Status.RUNNING);
654             
655             fStartedCount++;
656             
657             Object JavaDoc[] listeners= fSessionListeners.getListeners();
658             for (int i= 0; i < listeners.length; ++i) {
659                 ((ITestSessionListener) listeners[i]).testStarted(testCaseElement);
660             }
661         }
662     
663         public void testEnded(String JavaDoc testId, String JavaDoc testName) {
664             TestElement testElement= getTestElement(testId);
665             if (testElement == null) {
666                 testElement= createUnrootedTestElement(testId, testName);
667             } else if (! (testElement instanceof TestCaseElement)) {
668                 logUnexpectedTest(testId, testElement);
669                 return;
670             }
671             TestCaseElement testCaseElement= (TestCaseElement) testElement;
672             if (testName.startsWith(MessageIds.IGNORED_TEST_PREFIX)) {
673                 testCaseElement.setIgnored(true);
674                 fIgnoredCount++;
675             }
676
677             if (testCaseElement.getStatus() == Status.RUNNING)
678                 setStatus(testCaseElement, Status.OK);
679             
680             Object JavaDoc[] listeners= fSessionListeners.getListeners();
681             for (int i= 0; i < listeners.length; ++i) {
682                 ((ITestSessionListener) listeners[i]).testEnded(testCaseElement);
683             }
684         }
685         
686         
687         public void testFailed(int status, String JavaDoc testId, String JavaDoc testName, String JavaDoc trace) {
688             testFailed(status, testId, testName, trace, null, null);
689         }
690         
691         /* (non-Javadoc)
692          * @see org.eclipse.jdt.internal.junit.model.ITestRunListener2#testFailed(int, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
693          */

694         public void testFailed(int statusCode, String JavaDoc testId, String JavaDoc testName, String JavaDoc trace, String JavaDoc expected, String JavaDoc actual) {
695             TestElement testElement= getTestElement(testId);
696             if (testElement == null) {
697                 testElement= createUnrootedTestElement(testId, testName);
698                 return;
699             }
700
701             Status status= Status.convert(statusCode);
702             registerTestFailed(testElement, status, trace, nullifyEmpty(expected), nullifyEmpty(actual));
703             
704             Object JavaDoc[] listeners= fSessionListeners.getListeners();
705             for (int i= 0; i < listeners.length; ++i) {
706                 ((ITestSessionListener) listeners[i]).testFailed(testElement, status, trace, expected, actual);
707             }
708         }
709
710         private String JavaDoc nullifyEmpty(String JavaDoc string) {
711             int length= string.length();
712             if (length == 0)
713                 return null;
714             else if (string.charAt(length - 1) == '\n')
715                 return string.substring(0, length - 1);
716             else
717                 return string;
718         }
719     
720         public void testReran(String JavaDoc testId, String JavaDoc testClass, String JavaDoc testName, int status, String JavaDoc trace) {
721             testReran(testId, testClass, testName, status, trace, "", ""); //$NON-NLS-1$ //$NON-NLS-2$
722
}
723         
724         /* (non-Javadoc)
725          * @see org.eclipse.jdt.internal.junit.model.ITestRunListener2#testReran(java.lang.String, java.lang.String, java.lang.String, int, java.lang.String, java.lang.String, java.lang.String)
726          */

727         public void testReran(String JavaDoc testId, String JavaDoc className, String JavaDoc testName, int statusCode, String JavaDoc trace, String JavaDoc expectedResult, String JavaDoc actualResult) {
728             TestElement testElement= getTestElement(testId);
729             if (testElement == null) {
730                 testElement= createUnrootedTestElement(testId, testName);
731             } else if (! (testElement instanceof TestCaseElement)) {
732                 logUnexpectedTest(testId, testElement);
733                 return;
734             }
735             TestCaseElement testCaseElement= (TestCaseElement) testElement;
736             
737             Status status= Status.convert(statusCode);
738             registerTestFailed(testElement, status, trace, nullifyEmpty(expectedResult), nullifyEmpty(actualResult));
739             
740             Object JavaDoc[] listeners= fSessionListeners.getListeners();
741             for (int i= 0; i < listeners.length; ++i) {
742                 //TODO: post old & new status?
743
((ITestSessionListener) listeners[i]).testReran(testCaseElement, status, trace, expectedResult, actualResult);
744             }
745         }
746     
747         private void logUnexpectedTest(String JavaDoc testId, TestElement testElement) {
748             JUnitPlugin.log(new Exception JavaDoc("Unexpected TestElement type for testId '" + testId + "': " + testElement)); //$NON-NLS-1$ //$NON-NLS-2$
749
}
750     }
751
752     private static class IncompleteTestSuite {
753         public TestSuiteElement fTestSuiteElement;
754         public int fOutstandingChildren;
755         
756         public IncompleteTestSuite(TestSuiteElement testSuiteElement, int outstandingChildren) {
757             fTestSuiteElement= testSuiteElement;
758             fOutstandingChildren= outstandingChildren;
759         }
760     }
761     
762     public void registerTestFailed(TestElement testElement, Status status, String JavaDoc trace, String JavaDoc expected, String JavaDoc actual) {
763         testElement.setStatus(status, trace, expected, actual);
764         if (status.isError()) {
765             fErrorCount++;
766         } else {
767             fFailureCount++;
768         }
769     }
770
771     public void registerTestEnded(TestElement testElement, boolean completed) {
772         if (testElement instanceof TestCaseElement) {
773             fTotalCount++;
774             if (! completed) {
775                 return;
776             }
777             fStartedCount++;
778             if (((TestCaseElement) testElement).isIgnored()) {
779                 fIgnoredCount++;
780             }
781             if (! testElement.getStatus().isErrorOrFailure())
782                 setStatus(testElement, Status.OK);
783         }
784     }
785     
786     private void setStatus(TestElement testElement, Status status) {
787         testElement.setStatus(status);
788     }
789     
790     public TestElement[] getAllFailedTestElements() {
791         ArrayList JavaDoc failures= new ArrayList JavaDoc();
792         addFailures(failures, getTestRoot());
793         return (TestElement[]) failures.toArray(new TestElement[failures.size()]);
794     }
795
796     private void addFailures(ArrayList JavaDoc failures, ITestElement testElement) {
797         Result testResult= testElement.getTestResult(true);
798         if (testResult == Result.ERROR || testResult == Result.FAILURE) {
799             failures.add(testElement);
800         }
801         if (testElement instanceof TestSuiteElement) {
802             TestSuiteElement testSuiteElement= (TestSuiteElement) testElement;
803             ITestElement[] children= testSuiteElement.getChildren();
804             for (int i= 0; i < children.length; i++) {
805                 addFailures(failures, children[i]);
806             }
807         }
808     }
809
810
811 }
812
Popular Tags