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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2009, 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 hudson.model.TaskListener;
27 import hudson.tasks.test.TestResultParser;
28 import hudson.model.AbstractBuild;
29 import hudson.*;
30 import hudson.remoting.VirtualChannel;
31
32 import java.io.IOException;
33 import java.io.File;
34
35 import org.apache.tools.ant.types.FileSet;
36 import org.apache.tools.ant.DirectoryScanner;
37
38 /**
39 * Parse some JUnit xml files and generate a TestResult containing all the
40 * results parsed.
41 */
42 @Extension
43 public class JUnitParser extends TestResultParser {
44
45 private final boolean keepLongStdio;
46
47 /** XXX TestResultParser.all does not seem to ever be called so why must this be an Extension? */
48 @Deprecated
49 public JUnitParser() {
50 this(false);
51 }
52
53 /**
54 * @param keepLongStdio if true, retain a suite's complete stdout/stderr even if this is huge and the suite passed
55 * @since 1.358
56 */
57 public JUnitParser(boolean keepLongStdio) {
58 this.keepLongStdio = keepLongStdio;
59 }
60
61 @Override
62 public String getDisplayName() {
63 return Messages.JUnitParser_DisplayName();
64 }
65
66 @Override
67 public String getTestResultLocationMessage() {
68 return Messages.JUnitParser_TestResultLocationMessage();
69 }
70
71 @Override
72 public TestResult parse(String testResultLocations,
73 AbstractBuild build, Launcher launcher,
74 TaskListener listener)
75 throws InterruptedException, IOException
76 {
77 final long buildTime = build.getTimestamp().getTimeInMillis();
78 final long timeOnMaster = System.currentTimeMillis();
79
80 // [BUG 3123310] TODO - Test Result Refactor: review and fix TestDataPublisher/TestAction subsystem]
81 // also get code that deals with testDataPublishers from JUnitResultArchiver.perform
82
83 FilePath workspace = build.getWorkspace();
84 if (workspace == null) {
85 throw new AbortException(Messages.JUnitParser_no_workspace_found(build));
86 }
87 return workspace.act(new ParseResultCallable(testResultLocations, buildTime, timeOnMaster, keepLongStdio));
88 }
89
90 private static final class ParseResultCallable implements
91 FilePath.FileCallable<TestResult> {
92 private final long buildTime;
93 private final String testResults;
94 private final long nowMaster;
95 private final boolean keepLongStdio;
96
97 private ParseResultCallable(String testResults, long buildTime, long nowMaster, boolean keepLongStdio) {
98 this.buildTime = buildTime;
99 this.testResults = testResults;
100 this.nowMaster = nowMaster;
101 this.keepLongStdio = keepLongStdio;
102 }
103
104 public TestResult invoke(File ws, VirtualChannel channel) throws IOException {
105 final long nowSlave = System.currentTimeMillis();
106
107 FileSet fs = Util.createFileSet(ws, testResults);
108 DirectoryScanner ds = fs.getDirectoryScanner();
109
110 String[] files = ds.getIncludedFiles();
111 if (files.length == 0) {
112 // no test result. Most likely a configuration
113 // error or fatal problem
114 throw new AbortException(Messages.JUnitResultArchiver_NoTestReportFound());
115 }
116
117 TestResult result = new TestResult(buildTime + (nowSlave - nowMaster), ds, keepLongStdio);
118 result.tally();
119 return result;
120 }
121 }
122
123 }
124