|
1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 package com.symbian.pde.test.utils; |
|
16 |
|
17 import java.io.File; |
|
18 import java.io.FileNotFoundException; |
|
19 import java.io.FileOutputStream; |
|
20 |
|
21 import junit.framework.AssertionFailedError; |
|
22 import junit.framework.TestCase; |
|
23 import junit.framework.TestResult; |
|
24 |
|
25 import org.apache.tools.ant.taskdefs.optional.junit.JUnitTest; |
|
26 import org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter; |
|
27 import org.eclipse.jdt.internal.junit.model.ITestRunListener2; |
|
28 |
|
29 public class PDETestListener implements ITestRunListener2 { |
|
30 private Object resultsCollector; |
|
31 private int totalNumberOfTests; |
|
32 private int testsRunCount; |
|
33 private int numberOfTestsPassed; |
|
34 private int numberOfTestsFailed; |
|
35 private int numberOfTestsWithError; |
|
36 private boolean testRunEnded = false; |
|
37 private XMLJUnitResultFormatter xmlResultsFormatter; |
|
38 private File outputFile; |
|
39 private String suiteName; |
|
40 private JUnitTest junitTestSuite; |
|
41 private TestCase currentTest; |
|
42 |
|
43 public PDETestListener(Object collector, String suite) { |
|
44 resultsCollector = collector; |
|
45 suiteName = suite; |
|
46 junitTestSuite = new JUnitTest(suiteName); |
|
47 junitTestSuite.setProperties(System.getProperties()); |
|
48 } |
|
49 |
|
50 public void setOutputFile(String filename) { |
|
51 outputFile = new File(filename); |
|
52 } |
|
53 |
|
54 public File getOutputFile() { |
|
55 if (outputFile == null) { |
|
56 setOutputFile("TEST-" + suiteName + ".xml"); |
|
57 } |
|
58 return outputFile; |
|
59 } |
|
60 |
|
61 public boolean failed() { |
|
62 return ((numberOfTestsFailed + numberOfTestsWithError) > 0) || (testRunEnded && (testsRunCount == 0)); |
|
63 } |
|
64 |
|
65 public int count() { |
|
66 return testsRunCount; |
|
67 } |
|
68 |
|
69 private XMLJUnitResultFormatter getXMLJUnitResultFormatter() { |
|
70 if (xmlResultsFormatter == null) { |
|
71 xmlResultsFormatter = new XMLJUnitResultFormatter(); |
|
72 try { |
|
73 xmlResultsFormatter.setOutput(new FileOutputStream(getOutputFile())); |
|
74 } catch (FileNotFoundException e) { |
|
75 e.printStackTrace(); |
|
76 } |
|
77 } |
|
78 return xmlResultsFormatter; |
|
79 } |
|
80 |
|
81 public synchronized void testRunStarted(int testCount) { |
|
82 totalNumberOfTests = testCount; |
|
83 testsRunCount = 0; |
|
84 numberOfTestsPassed = 0; |
|
85 numberOfTestsFailed = 0; |
|
86 numberOfTestsWithError = 0; |
|
87 testRunEnded = false; |
|
88 getXMLJUnitResultFormatter().startTestSuite(junitTestSuite); |
|
89 System.out.println("PDE Test Run Started - running " + totalNumberOfTests + " tests ..."); |
|
90 } |
|
91 |
|
92 public synchronized void testRunEnded(long elapsedTime) { |
|
93 testRunEnded = true; |
|
94 junitTestSuite.setCounts(testsRunCount, numberOfTestsFailed, numberOfTestsWithError); |
|
95 junitTestSuite.setRunTime(elapsedTime); |
|
96 getXMLJUnitResultFormatter().endTestSuite(junitTestSuite); |
|
97 System.out.println("Test Run Ended - " + (failed() ? "FAILED" : "PASSED") + " - Total: " + totalNumberOfTests |
|
98 + " (Errors: " + numberOfTestsWithError |
|
99 + ", Failed: " + numberOfTestsFailed |
|
100 + ", Passed: " + numberOfTestsPassed + "), duration " + elapsedTime + "ms."); |
|
101 |
|
102 synchronized (resultsCollector) { |
|
103 resultsCollector.notifyAll(); |
|
104 } |
|
105 } |
|
106 |
|
107 public synchronized void testRunStopped(long elapsedTime) { |
|
108 System.out.println("Test Run Stopped"); |
|
109 testRunEnded(elapsedTime); |
|
110 } |
|
111 |
|
112 public synchronized void testRunTerminated() { |
|
113 System.out.println("Test Run Terminated"); |
|
114 testRunEnded(0); |
|
115 } |
|
116 |
|
117 public synchronized void testStarted(String testId, String testName) { |
|
118 testsRunCount++; |
|
119 currentTest = new WrapperTestCase(testName); |
|
120 getXMLJUnitResultFormatter().startTest(currentTest); |
|
121 System.out.println(" Test Started - " + count() + " - " + testName); |
|
122 } |
|
123 |
|
124 public synchronized void testEnded(String testId, String testName) { |
|
125 numberOfTestsPassed = count() - (numberOfTestsFailed + numberOfTestsWithError); |
|
126 getXMLJUnitResultFormatter().endTest(currentTest); |
|
127 System.out.println(" Test Ended - " + count() + " - " + testName); |
|
128 } |
|
129 |
|
130 public synchronized void testFailed(int status, String testId, String testName, String trace, String expected, String actual) { |
|
131 String statusMessage = String.valueOf(status); |
|
132 if (status == ITestRunListener2.STATUS_OK) { |
|
133 numberOfTestsPassed++; |
|
134 statusMessage = "OK"; |
|
135 } else if (status == ITestRunListener2.STATUS_FAILURE) { |
|
136 numberOfTestsFailed++; |
|
137 statusMessage = "FAILED"; |
|
138 getXMLJUnitResultFormatter().addFailure(currentTest, new AssertionFailedError(trace)); |
|
139 } else if (status == ITestRunListener2.STATUS_ERROR) { |
|
140 numberOfTestsWithError++; |
|
141 statusMessage = "ERROR"; |
|
142 getXMLJUnitResultFormatter().addError(currentTest, new Exception(trace)); |
|
143 } |
|
144 System.out.println(" Test Failed - " + count() + " - " + testName + " - status: " + statusMessage |
|
145 + ", trace: " + trace + ", expected: " + expected + ", actual: " + actual); |
|
146 } |
|
147 |
|
148 public synchronized void testReran(String testId, String testClass, String testName, int status, String trace, String expected, String actual) { |
|
149 String statusMessage = String.valueOf(status); |
|
150 if (status == ITestRunListener2.STATUS_OK) { |
|
151 statusMessage = "OK"; |
|
152 } else if (status == ITestRunListener2.STATUS_FAILURE) { |
|
153 statusMessage = "FAILED"; |
|
154 } else if (status == ITestRunListener2.STATUS_ERROR) { |
|
155 statusMessage = "ERROR"; |
|
156 } |
|
157 |
|
158 System.out.println(" Test ReRan - " + testName + " - test class: " + testClass + ", status: " + statusMessage |
|
159 + ", trace: " + trace + ", expected: " + expected + ", actual: " + actual); |
|
160 } |
|
161 |
|
162 public synchronized void testTreeEntry(String description) { |
|
163 System.out.println("Test Tree Entry - Description: " + description); |
|
164 } |
|
165 |
|
166 class WrapperTestCase extends TestCase { |
|
167 |
|
168 public WrapperTestCase(String name) { |
|
169 super(name); |
|
170 } |
|
171 |
|
172 public int countTestCases() { |
|
173 return 1; |
|
174 } |
|
175 |
|
176 public void run(TestResult result) { |
|
177 } |
|
178 } |
|
179 } |