File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\test\java\hudson\tasks\junit\SuiteResultTest.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Erik Ramfelt, Xavier Le Vourch, Yahoo!, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.tasks.junit;
25
26 import edu.umd.cs.findbugs.annotations.SuppressWarnings;
27 import java.io.File;
28 import java.util.List;
29 import java.net.URISyntaxException;
30
31 import hudson.XmlFile;
32
33 import org.jvnet.hudson.test.Bug;
34
35 import java.io.FileWriter;
36 import java.io.PrintWriter;
37 import java.io.Writer;
38
39 import junit.framework.TestCase;
40
41 /**
42 * Test cases for parsing JUnit report XML files.
43 * As there are no XML schema for JUnit xml files, Hudson needs to handle
44 * varied xml files.
45 *
46 * @author Erik Ramfelt
47 * @author Christoph Kutzinski
48 */
49 public class SuiteResultTest extends TestCase {
50
51 private File getDataFile(String name) throws URISyntaxException {
52 return new File(SuiteResultTest.class.getResource(name).toURI());
53 }
54
55 private SuiteResult parseOne(File file) throws Exception {
56 List<SuiteResult> results = SuiteResult.parse(file, false);
57 assertEquals(1,results.size());
58 return results.get(0);
59 }
60
61 private List<SuiteResult> parseSuites(File file) throws Exception {
62 return SuiteResult.parse(file, false);
63 }
64
65 @Bug(1233)
66 public void testIssue1233() throws Exception {
67 SuiteResult result = parseOne(getDataFile("junit-report-1233.xml"));
68
69 List<CaseResult> cases = result.getCases();
70 assertEquals("Class name is incorrect", "test.foo.bar.DefaultIntegrationTest", cases.get(0).getClassName());
71 assertEquals("Class name is incorrect", "test.foo.bar.BundleResolverIntegrationTest", cases.get(1).getClassName());
72 assertEquals("Class name is incorrect", "test.foo.bar.BundleResolverIntegrationTest", cases.get(2).getClassName());
73 assertEquals("Class name is incorrect", "test.foo.bar.ProjectSettingsTest", cases.get(3).getClassName());
74 assertEquals("Class name is incorrect", "test.foo.bar.ProjectSettingsTest", cases.get(4).getClassName());
75 }
76 /**
77 * JUnit report file is generated by SoapUI Pro 1.7.6
78 */
79 @Bug(1463)
80 public void testIssue1463() throws Exception {
81 SuiteResult result = parseOne(getDataFile("junit-report-1463.xml"));
82
83 List<CaseResult> cases = result.getCases();
84 for (CaseResult caseResult : cases) {
85 assertEquals("Test class name is incorrect in " + caseResult.getDisplayName(), "WLI-FI-Tests-Fake", caseResult.getClassName());
86 }
87 assertEquals("Test name is incorrect", "IF_importTradeConfirmationToDwh", cases.get(0).getName());
88 assertEquals("Test name is incorrect", "IF_getAmartaDisbursements", cases.get(1).getName());
89 assertEquals("Test name is incorrect", "IF_importGLReconDataToDwh", cases.get(2).getName());
90 assertEquals("Test name is incorrect", "IF_importTradeInstructionsToDwh", cases.get(3).getName());
91 assertEquals("Test name is incorrect", "IF_getDeviationTradeInstructions", cases.get(4).getName());
92 assertEquals("Test name is incorrect", "IF_getDwhGLData", cases.get(5).getName());
93 }
94
95 /**
96 * JUnit report produced by TAP (Test Anything Protocol)
97 */
98 @Bug(1472)
99 public void testIssue1472() throws Exception {
100 List<SuiteResult> results = SuiteResult.parse(getDataFile("junit-report-1472.xml"), false);
101 assertTrue(results.size()>20); // lots of data here
102
103 SuiteResult sr0 = results.get(0);
104 SuiteResult sr1 = results.get(1);
105 assertEquals("make_test.t_basic_lint_t",sr0.getName());
106 assertEquals("make_test.t_basic_meta_t",sr1.getName());
107 assertTrue(!sr0.getStdout().equals(sr1.getStdout()));
108 }
109
110 @Bug(2874)
111 public void testIssue2874() throws Exception {
112 SuiteResult result = parseOne(getDataFile("junit-report-2874.xml"));
113
114 assertEquals("test suite name", "DummyTest", result.getName());
115 }
116
117 public void testErrorDetails() throws Exception {
118 SuiteResult result = parseOne(getDataFile("junit-report-errror-details.xml"));
119
120 List<CaseResult> cases = result.getCases();
121 for (CaseResult caseResult : cases) {
122 assertEquals("Test class name is incorrect in " + caseResult.getDisplayName(), "some.package.somewhere.WhooHoo", caseResult.getClassName());
123 }
124 assertEquals("this normally has the string like, expected mullet, but got bream", cases.get(0).getErrorDetails());
125 }
126
127 public void testSuiteResultPersistence() throws Exception {
128 SuiteResult source = parseOne(getDataFile("junit-report-1233.xml"));
129
130 File dest = File.createTempFile("testSuiteResultPersistence", ".xml");
131 try {
132 XmlFile xmlFile = new XmlFile(dest);
133 xmlFile.write(source);
134
135 SuiteResult result = (SuiteResult)xmlFile.read();
136 assertNotNull(result);
137
138 assertEquals(source.getName(), result.getName());
139 assertEquals(source.getTimestamp(), result.getTimestamp());
140 assertEquals(source.getDuration(), result.getDuration());
141 assertEquals(source.getStderr(), result.getStderr());
142 assertEquals(source.getStdout(), result.getStdout());
143 assertEquals(source.getCases().size(), result.getCases().size());
144 assertNotNull(result.getCase("testGetBundle"));
145 } finally {
146 dest.delete();
147 }
148 }
149
150 //@Bug(6516)
151 public void testSuiteStdioTrimming() throws Exception {
152 File data = File.createTempFile("testSuiteStdioTrimming", ".xml");
153 try {
154 Writer w = new FileWriter(data);
155 try {
156 PrintWriter pw = new PrintWriter(w);
157 pw.println("<testsuites name='x'>");
158 pw.println("<testsuite failures='0' errors='0' tests='1' name='x'>");
159 pw.println("<testcase name='x' classname='x'/>");
160 pw.println("<system-out/>");
161 pw.print("<system-err><![CDATA[");
162 pw.println("First line is intact.");
163 for (int i = 0; i < 100; i++) {
164 pw.println("Line #" + i + " might be elided.");
165 }
166 pw.println("Last line is intact.");
167 pw.println("]]></system-err>");
168 pw.println("</testsuite>");
169 pw.println("</testsuites>");
170 pw.flush();
171 } finally {
172 w.close();
173 }
174 SuiteResult sr = parseOne(data);
175 assertEquals(sr.getStderr(), 1030, sr.getStderr().length());
176 } finally {
177 data.delete();
178 }
179 }
180
181 @SuppressWarnings({"RV_RETURN_VALUE_IGNORED_BAD_PRACTICE", "DM_DEFAULT_ENCODING", "OS_OPEN_STREAM"})
182 public void testSuiteStdioTrimmingSurefire() throws Exception {
183 File data = File.createTempFile("TEST-", ".xml");
184 try {
185 Writer w = new FileWriter(data);
186 try {
187 PrintWriter pw = new PrintWriter(w);
188 pw.println("<testsuites name='x'>");
189 pw.println("<testsuite failures='0' errors='0' tests='1' name='x'>");
190 pw.println("<testcase name='x' classname='x'/>");
191 pw.println("</testsuite>");
192 pw.println("</testsuites>");
193 pw.flush();
194 } finally {
195 w.close();
196 }
197 File data2 = new File(data.getParentFile(), data.getName().replaceFirst("^TEST-(.+)[.]xml$", "$1-output.txt"));
198 try {
199 w = new FileWriter(data2);
200 try {
201 PrintWriter pw = new PrintWriter(w);
202 pw.println("First line is intact.");
203 for (int i = 0; i < 100; i++) {
204 pw.println("Line #" + i + " might be elided.");
205 }
206 pw.println("Last line is intact.");
207 pw.flush();
208 } finally {
209 w.close();
210 }
211 SuiteResult sr = parseOne(data);
212 assertEquals(sr.getStdout(), 1030, sr.getStdout().length());
213 } finally {
214 data2.delete();
215 }
216 } finally {
217 data.delete();
218 }
219 }
220
221 /**
222 * When the testcase fails to initialize (exception in constructor or @Before)
223 * there is no 'testcase' element at all.
224 */
225 @Bug(6700)
226 public void testErrorInTestInitialization() throws Exception {
227 SuiteResult suiteResult = parseOne(getDataFile("junit-report-6700.xml"));
228
229 assertEquals(1, suiteResult.getCases().size());
230
231 CaseResult result = suiteResult.getCases().get(0);
232 assertEquals(1, result.getFailCount());
233 assertTrue(result.getErrorStackTrace() != null);
234 }
235
236 @Bug(6454)
237 public void testParseNestedTestSuites() throws Exception {
238 // A report with several nested suites
239 // 3 of them have actual some tests - each exactly one
240 List<SuiteResult> results = parseSuites(getDataFile("junit-report-nested-testsuites.xml"));
241 assertEquals(3, results.size());
242
243 for (SuiteResult result : results) {
244 assertEquals(1, result.getCases().size());
245 }
246 }
247 }
248