KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > tasks > junit > JUnitResultArchiver


1 package hudson.tasks.junit;
2
3 import hudson.FilePath.FileCallable;
4 import hudson.Launcher;
5 import hudson.model.Action;
6 import hudson.model.Build;
7 import hudson.model.BuildListener;
8 import hudson.model.Descriptor;
9 import hudson.model.Result;
10 import hudson.remoting.VirtualChannel;
11 import hudson.tasks.Publisher;
12 import hudson.tasks.test.TestResultProjectAction;
13 import org.apache.tools.ant.DirectoryScanner;
14 import org.apache.tools.ant.Project;
15 import org.apache.tools.ant.types.FileSet;
16 import org.kohsuke.stapler.StaplerRequest;
17
18 import java.io.File JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.Serializable JavaDoc;
21
22 /**
23  * Generates HTML report from JUnit test result XML files.
24  *
25  * @author Kohsuke Kawaguchi
26  */

27 public class JUnitResultArchiver extends Publisher implements Serializable JavaDoc {
28
29     /**
30      * {@link FileSet} "includes" string, like "foo/bar/*.xml"
31      */

32     private final String JavaDoc testResults;
33
34     public JUnitResultArchiver(String JavaDoc testResults) {
35         this.testResults = testResults;
36     }
37
38     public boolean perform(Build build, Launcher launcher, BuildListener listener) throws InterruptedException JavaDoc, IOException JavaDoc {
39         TestResult result;
40
41         listener.getLogger().println("Recording test results");
42
43         try {
44             final long buildTime = build.getTimestamp().getTimeInMillis();
45
46             result = build.getProject().getWorkspace().act(new FileCallable<TestResult>() {
47                 public TestResult invoke(File JavaDoc ws, VirtualChannel channel) throws IOException JavaDoc {
48                     FileSet fs = new FileSet();
49                     Project p = new Project();
50                     fs.setProject(p);
51                     fs.setDir(ws);
52                     fs.setIncludes(testResults);
53                     DirectoryScanner ds = fs.getDirectoryScanner(p);
54
55                     if(ds.getIncludedFiles().length==0) {
56                         // no test result. Most likely a configuration error or fatal problem
57
throw new AbortException("No test report files were found. Configuration error?");
58                     }
59
60                     return new TestResult(buildTime,ds);
61                 }
62             });
63         } catch (AbortException e) {
64             listener.getLogger().println(e.getMessage());
65             build.setResult(Result.FAILURE);
66             return true;
67         }
68
69
70         TestResultAction action = new TestResultAction(build, result, listener);
71         build.getActions().add(action);
72
73         TestResult r = action.getResult();
74
75         if(r.getPassCount()==0 && r.getFailCount()==0) {
76             listener.getLogger().println("Test reports were found but none of them are new. Did tests run?");
77             // no test result. Most likely a configuration error or fatal problem
78
build.setResult(Result.FAILURE);
79         }
80
81         if(r.getFailCount()>0)
82             build.setResult(Result.UNSTABLE);
83
84         return true;
85     }
86
87     public String JavaDoc getTestResults() {
88         return testResults;
89     }
90
91     public Action getProjectAction(hudson.model.Project project) {
92         return new TestResultProjectAction(project);
93     }
94
95     public Descriptor<Publisher> getDescriptor() {
96         return DescriptorImpl.DESCRIPTOR;
97     }
98
99     private static final long serialVersionUID = 1L;
100
101     public static class DescriptorImpl extends Descriptor<Publisher> {
102         public static final Descriptor<Publisher> DESCRIPTOR = new DescriptorImpl();
103
104         public DescriptorImpl() {
105             super(JUnitResultArchiver.class);
106         }
107
108         public String JavaDoc getDisplayName() {
109             return "Publish JUnit test result report";
110         }
111
112         public Publisher newInstance(StaplerRequest req) {
113             return new JUnitResultArchiver(req.getParameter("junitreport_includes"));
114         }
115     }
116 }
117
Popular Tags