View Javadoc

1   /*
2       Jameleon - An automation testing tool..
3       Copyright (C) 2007 Christian W. Hargraves (engrean@hotmail.com)
4   
5       This library is free software; you can redistribute it and/or
6       modify it under the terms of the GNU Lesser General Public
7       License as published by the Free Software Foundation; either
8       version 2.1 of the License, or (at your option) any later version.
9   
10      This library is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13      Lesser General Public License for more details.
14  
15      You should have received a copy of the GNU Lesser General Public
16      License along with this library; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19  package net.sf.jameleon.reporting;
20  
21  import net.sf.jameleon.TestCaseTag;
22  import net.sf.jameleon.event.TestCaseEvent;
23  import net.sf.jameleon.event.TestCaseListener;
24  import net.sf.jameleon.event.TestRunEvent;
25  import net.sf.jameleon.event.TestRunListener;
26  import net.sf.jameleon.result.TestCaseResult;
27  
28  import java.io.File;
29  import java.io.PrintWriter;
30  import java.util.Calendar;
31  
32  /***
33   * Writes the HTML results out to a file. This class writes both the main results
34   * as well as the individual testcase docs.
35   */
36  public class ResultsReporter implements TestCaseListener, TestRunListener {
37  
38      private static ResultsReporter reporter;
39  
40      private Calendar startTime;
41      private TestCaseCounter counter;
42      private TestRunReporter htmlTestRunReporter;
43      private TestRunReporter simpleTestRunReporter;
44      private TestRunSummaryReporter htmlTestRunSummaryReporter;
45  
46      // Make this class a singleton
47      private ResultsReporter(){
48          counter = new TestCaseCounter();
49          htmlTestRunReporter = new HtmlTestRunReporter();
50          htmlTestRunSummaryReporter = new HtmlTestRunSummaryReporter();
51          simpleTestRunReporter = new SimpleTestRunReporter();
52          PrintWriter printWriter = new PrintWriter(System.out);
53          simpleTestRunReporter.setWriter(printWriter);
54      }
55  
56      public static ResultsReporter getInstance(){
57          if (reporter == null){
58              reporter = new ResultsReporter();
59          }
60          return reporter;
61      }
62  
63      public static void clearInstance(){
64          if (reporter != null){
65              reporter.getHtmlTestRunReporter().cleanUp();
66              reporter = null;
67          }
68      }
69      
70      /***
71       * Gets the timestamp formatted results directory
72       * @param baseDir - The director to use as the base or just '.'
73       * @param c - The date and time
74       * @return a file representing the directory to store the results to
75       */
76      public static File getResultsDir(File baseDir, Calendar c) {
77          String path = baseDir.getPath()+File.separator + ReporterUtils.formatTime(c);
78          return new File(path);
79      }
80  
81      protected File getResultsDirectoryForTestCase(TestCaseTag tct){
82          return new File(getResultsDir(tct.getResultsDir(false), startTime), tct.getResults().getTestName());
83      }
84  
85      protected void genTestCaseResultsDoc(TestCaseTag tct){
86          if (tct.isGenTestCaseDocs()) {
87              HtmlTestCaseResultGenerator tcResultGen = new HtmlTestCaseResultGenerator(tct);
88              File mainFile = tcResultGen.generateTestCaseResultPage();
89              tct.getResults().setTestCaseDocsFile(mainFile.getPath());
90          }
91      }
92  
93      /***
94       * Resets the test run stats (TestCaseCounter)
95       */
96      public void resetStats() {
97          counter = new TestCaseCounter();
98      }
99  
100     private void checkCounter(){
101         if (counter == null){
102             resetStats();
103         }
104     }
105 
106     ///////////////////////////////////////////////////////////////////////////////////////////////
107     ////                                    TestCaseListner methods                            ////
108     ///////////////////////////////////////////////////////////////////////////////////////////////
109 
110     public void beginTestCase(TestCaseEvent event) {
111         TestCaseTag tct = (TestCaseTag)event.getSource();
112         tct.setTimestampedResultsDir(getResultsDirectoryForTestCase(tct));
113     }
114 
115     public void endTestCase(TestCaseEvent event) {
116         try{
117             TestCaseTag tct = (TestCaseTag)event.getSource();
118             genTestCaseResultsDoc(tct);
119             TestCaseResult result = tct.getResults();
120             final int totalRun = result.getCountableResults().size();
121             final int failed = result.getFailedCountableResults().size();
122             checkCounter();
123             counter.incrementPassed(totalRun - failed);
124             counter.incrementFailed(failed);
125             htmlTestRunReporter.reportScriptResult(tct, counter);
126             simpleTestRunReporter.reportScriptResult(tct, counter);
127         }finally{
128             checkCounter();
129             counter.incrementTestCaseNum(1);
130         }
131     }
132 
133     public void beginTestRun(TestRunEvent event) {
134         setStartTime((Calendar)event.getSource());
135         htmlTestRunReporter.reportTestRunStart(getStartTime());
136         htmlTestRunSummaryReporter.reportTestRunStart(getStartTime());
137     }
138 
139     public void endTestRun(TestRunEvent event) {
140         Calendar endTime = (Calendar)event.getSource();
141         htmlTestRunReporter.reportTestRunComplete(getStartTime(), endTime, counter);
142         htmlTestRunSummaryReporter.reportTestRunComplete(getStartTime(), endTime, counter);
143         simpleTestRunReporter.reportTestRunComplete(getStartTime(), endTime, counter);
144         resetStats();
145     }
146 
147     ///////////////////////////////////////////////////////////////////////////////////////////////
148     ////                                    SETTERS & GETTERS                                  ////
149     ///////////////////////////////////////////////////////////////////////////////////////////////
150 
151     public Calendar getStartTime(){
152         return startTime;
153     }
154 
155     public void setStartTime(Calendar startTime) {
156         this.startTime = startTime;
157     }
158 
159     public TestCaseCounter getTestCaseCounter() {
160         return counter;
161     }
162 
163     public void setTestCaseCounter(TestCaseCounter counter) {
164         this.counter = counter;
165     }
166 
167     public TestRunReporter getHtmlTestRunReporter() {
168         return htmlTestRunReporter;
169     }
170 
171     public void setHtmlTestRunReporter(TestRunReporter htmlTestRunReporter) {
172         this.htmlTestRunReporter = htmlTestRunReporter;
173     }
174 
175     public TestRunSummaryReporter getHtmlTestRunSummaryReporter() {
176         return htmlTestRunSummaryReporter;
177     }
178 
179     public void setHtmlTestRunSummaryReporter(TestRunSummaryReporter htmlTestRunSummaryReporter) {
180         this.htmlTestRunSummaryReporter = htmlTestRunSummaryReporter;
181     }
182 
183     public TestRunReporter getSimpleTestRunReporter() {
184         return simpleTestRunReporter;
185     }
186 
187     public void setSimpleTestRunReporter(TestRunReporter simpleTestRunReporter) {
188         this.simpleTestRunReporter = simpleTestRunReporter;
189     }
190 
191 
192 }