KickJava   Java API By Example, From Geeks To Geeks.

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


1 package hudson.tasks.junit;
2
3 import com.thoughtworks.xstream.XStream;
4 import hudson.XmlFile;
5 import hudson.model.AbstractBuild;
6 import hudson.model.Action;
7 import hudson.model.BuildListener;
8 import hudson.tasks.test.AbstractTestResultAction;
9 import hudson.util.StringConverter2;
10 import hudson.util.XStream2;
11 import org.kohsuke.stapler.StaplerProxy;
12
13 import java.io.File JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.lang.ref.WeakReference JavaDoc;
16 import java.util.logging.Level JavaDoc;
17 import java.util.logging.Logger JavaDoc;
18
19 /**
20  * {@link Action} that displays the JUnit test result.
21  *
22  * <p>
23  * The actual test reports are isolated by {@link WeakReference}
24  * so that it doesn't eat up too much memory.
25  *
26  * @author Kohsuke Kawaguchi
27  */

28 public class TestResultAction extends AbstractTestResultAction<TestResultAction> implements StaplerProxy {
29     private transient WeakReference JavaDoc<TestResult> result;
30
31     // Hudson < 1.25 didn't set these fields, so use Integer
32
// so that we can distinguish between 0 tests vs not-computed-yet.
33
private int failCount;
34     private Integer JavaDoc totalCount;
35
36
37     public TestResultAction(AbstractBuild owner, TestResult result, BuildListener listener) {
38         super(owner);
39
40         result.freeze(this);
41
42         totalCount = result.getTotalCount();
43         failCount = result.getFailCount();
44
45         // persist the data
46
try {
47             getDataFile().write(result);
48         } catch (IOException JavaDoc e) {
49             e.printStackTrace(listener.fatalError("Failed to save the JUnit test result"));
50         }
51
52         this.result = new WeakReference JavaDoc<TestResult>(result);
53     }
54
55     private XmlFile getDataFile() {
56         return new XmlFile(XSTREAM,new File(owner.getRootDir(), "junitResult.xml"));
57     }
58
59     public synchronized TestResult getResult() {
60         if(result==null) {
61             TestResult r = load();
62             result = new WeakReference JavaDoc<TestResult>(r);
63             return r;
64         }
65         TestResult r = result.get();
66         if(r==null) {
67             r = load();
68             result = new WeakReference JavaDoc<TestResult>(r);
69         }
70         if(totalCount==null) {
71             totalCount = r.getTotalCount();
72             failCount = r.getFailCount();
73         }
74         return r;
75     }
76
77     @Override JavaDoc
78     public int getFailCount() {
79         if(totalCount==null)
80             getResult(); // this will compute the result
81
return failCount;
82     }
83
84     @Override JavaDoc
85     public int getTotalCount() {
86         if(totalCount==null)
87             getResult(); // this will compute the result
88
return totalCount;
89     }
90
91     /**
92      * Loads a {@link TestResult} from disk.
93      */

94     private TestResult load() {
95         TestResult r;
96         try {
97             r = (TestResult)getDataFile().read();
98         } catch (IOException JavaDoc e) {
99             logger.log(Level.WARNING, "Failed to load "+getDataFile(),e);
100             r = new TestResult(); // return a dummy
101
}
102         r.freeze(this);
103         return r;
104     }
105
106     public Object JavaDoc getTarget() {
107         return getResult();
108     }
109
110
111
112     private static final Logger JavaDoc logger = Logger.getLogger(TestResultAction.class.getName());
113
114     private static final XStream XSTREAM = new XStream2();
115
116     static {
117         XSTREAM.alias("result",TestResult.class);
118         XSTREAM.alias("suite",SuiteResult.class);
119         XSTREAM.alias("case",CaseResult.class);
120         XSTREAM.registerConverter(new StringConverter2(),100);
121
122     }
123 }
124
Popular Tags