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.result.TestCaseResult;
22  import net.sf.jameleon.util.JarResourceExtractor;
23  import net.sf.jameleon.util.JameleonDefaultValues;
24  import net.sf.jameleon.util.Configurator;
25  
26  import java.io.File;
27  import java.io.IOException;
28  import java.io.Writer;
29  import java.util.Calendar;
30  import java.util.HashMap;
31  import java.util.Map;
32  
33  /***
34   * Reports on all script results in a given test run as a summary in HTML. The current implementation reports
35   * a single result as an HTML table row.
36   */
37  public class HtmlTestRunSummaryReporter implements TestRunSummaryReporter {
38  
39      public static final String DEFAULT_HEADER_TEMPLATE = "templates/results/summary/TestRunSummaryResultsHeader.html";
40      public static final String DEFAULT_RESULT_ROW_TEMPLATE = "templates/results/summary/TestRunSummaryRowResult.html";
41      public static final String DEFAULT_RESULT_BEGIN_ROW_TEMPLATE = "templates/results/summary/TestRunBeginSummaryRowResult.html";
42  
43      private String testRunSummaryResultsHeaderTemplate = DEFAULT_HEADER_TEMPLATE;
44      private String testRunResultRowTemplate = DEFAULT_RESULT_ROW_TEMPLATE;
45      private String testRunBeginResultRowTemplate = DEFAULT_RESULT_BEGIN_ROW_TEMPLATE;
46  
47      private boolean printHeader;
48      private Writer writer;
49  
50      public void extractResources(){
51          JarResourceExtractor extractor = null;
52          try{
53              extractor = new JarResourceExtractor();
54              File resultsDir = ReporterUtils.getResultsDir();
55              extractor.extractFilesInDirectory("icons", resultsDir);
56              extractor.extractFilesInDirectory("images", resultsDir);
57              extractor.extractFilesInDirectory("css", resultsDir);
58              extractor.extractFilesInDirectory("js", resultsDir);
59          }catch(IOException ioe){
60              System.err.println("Can not copy over required images, css and js files for reporting");
61              ioe.printStackTrace();
62          }finally{
63              if (extractor != null && extractor.getJarFile() != null){
64                  try{
65                      extractor.getJarFile().close();
66                  }catch(IOException ioe){
67                      System.err.println("Could not close jameleon-core.jar");
68                      ioe.printStackTrace();
69                      //so we couldn't close the jar file?
70                  }
71              }
72          }
73  
74      }
75  
76      public void printHeader(){
77          Map params = new HashMap();
78          params.put("encoding",  Configurator.getInstance().getValue("genTestCaseDocsEncoding", JameleonDefaultValues.FILE_CHARSET));
79  
80          ReporterUtils.outputToTemplate(getWriter(), getTestRunSummaryResultsHeaderTemplate(), params);
81      }
82  
83      public void reportTestRunStart(Calendar startTime) {
84          if (isPrintHeader()){
85              extractResources();
86              printHeader();
87              setPrintHeader(false);
88          }
89          Map params = new HashMap();
90          params.put("testRunId", ReporterUtils.formatTime(startTime));
91          ReporterUtils.outputToTemplate(getWriter(), getTestRunBeginResultRowTemplate(), params);
92      }
93  
94      public void reportTestRunComplete(Calendar startTime, Calendar endTime, TestCaseCounter counter) {
95          Map params = new HashMap();
96          String outcome = "fail";
97          if (counter.getNumFailed() == 0){
98              outcome = "pass";
99          }
100         params.put("testRunId", ReporterUtils.formatTime(startTime));
101         params.put("outcome", outcome);        
102         params.put("totalRun", new Integer(counter.getNumRun()));
103         params.put("totalFailed", new Integer(counter.getNumFailed()));
104         params.put("totalTime", ReporterUtils.getExecutionTime(startTime, endTime));
105         params.put("percentPassed", TestCaseResult.getPercentagePassed(counter.getNumRun(), counter.getNumFailed()));
106         ReporterUtils.outputToTemplate(getWriter(), getTestRunResultRowTemplate(), params);
107     }
108 
109     public void cleanUp() {
110         try{
111             if (writer != null){
112                 writer.flush();
113                 writer.close();
114                 writer = null;
115             }
116         }catch(IOException ioe){
117             ioe.printStackTrace();
118         }
119     }
120 
121 
122     //////////////////////////////////////////////////////////////////////////////////////////////////////////
123     //////                      GETTERS & SETTERS                                                       //////
124     //////////////////////////////////////////////////////////////////////////////////////////////////////////
125 
126 
127     public Writer getWriter() {
128         return writer;
129     }
130 
131     public void setWriter(Writer writer) {
132         this.writer = writer;
133     }
134 
135     /***
136      * Tells whether the header should be printed or not
137      * @return true if the header should be printed
138      */
139     public boolean isPrintHeader(){
140         return printHeader;
141     }
142 
143     /***
144      * Sets the print header option
145      * @param printHeader true if the header should be printed
146      */
147     public void setPrintHeader(boolean printHeader) {
148         this.printHeader = printHeader;
149     }
150 
151     /***
152      * Gets the template to be used when the test run is started
153      * @return The template to be used when the test run is started
154      */
155     public String getTestRunBeginResultRowTemplate() {
156         return testRunBeginResultRowTemplate;
157     }
158 
159     /***
160      * Sets the template to be used when the test run is started
161      * @param testRunBeginResultRowTemplate The template to be used when the test run is started
162      */
163     public void setTestRunBeginResultRowTemplate(String testRunBeginResultRowTemplate) {
164         this.testRunBeginResultRowTemplate = testRunBeginResultRowTemplate;
165     }
166 
167     /***
168      * Gets the template for the test run results header file
169      * @return The test run results template
170      */
171     public String getTestRunSummaryResultsHeaderTemplate() {
172         return testRunSummaryResultsHeaderTemplate;
173     }
174 
175     /***
176      * Sets the template for the test run results header file
177      * @param testRunSummaryResultsHeaderTemplate The test run results template
178      */
179     public void setTestRunSummaryResultsHeaderTemplate(String testRunSummaryResultsHeaderTemplate) {
180         this.testRunSummaryResultsHeaderTemplate = testRunSummaryResultsHeaderTemplate;
181     }
182 
183     /***
184      * Gets the template for the invidual test script result
185      * @return The template for the invidual test script result
186      */
187     public String getTestRunResultRowTemplate() {
188         return testRunResultRowTemplate;
189     }
190 
191     /***
192      * Sets the template for the invidual test script result
193      * @param testRunResultRowTemplate The template for the invidual test script result
194      */
195     public void setTestRunResultRowTemplate(String testRunResultRowTemplate) {
196         this.testRunResultRowTemplate = testRunResultRowTemplate;
197     }
198 }