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 ; 19 import java.io.IOException ; 20 import java.io.Serializable ; 21 22 27 public class JUnitResultArchiver extends Publisher implements Serializable { 28 29 32 private final String testResults; 33 34 public JUnitResultArchiver(String testResults) { 35 this.testResults = testResults; 36 } 37 38 public boolean perform(Build build, Launcher launcher, BuildListener listener) throws InterruptedException , IOException { 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 ws, VirtualChannel channel) throws IOException { 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 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 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 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 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 |