View Javadoc

1   /*
2       Jameleon - An automation testing tool..
3       Copyright (C) 2003-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.result;
20  
21  import net.sf.jameleon.bean.FunctionalPoint;
22  import java.text.NumberFormat;
23  import java.util.ArrayList;
24  import java.util.Calendar;
25  import java.util.List;
26  
27  /***
28   * An implementation of @see TestResult that keeps track of all of the function point results as well as the the sessio results
29   */
30  public class TestCaseResult 
31      extends TestResultWithChildren
32      implements CountableResult {
33  
34      private static final long serialVersionUID = 1L;
35  
36      /***
37       * The name of the test case
38       */
39      protected String testName;
40      /***
41       * The name of the test case docs file
42       */
43      protected String testCaseDocsFile;
44      protected boolean countableResultFailed;
45      protected Calendar dateTimeExecuted;
46  
47      public TestCaseResult(){
48          super();
49          dateTimeExecuted = Calendar.getInstance();
50      }
51  
52      public TestCaseResult(FunctionalPoint tag){
53          super(tag);
54          dateTimeExecuted = Calendar.getInstance();
55      }
56  
57      public TestCaseResult(String testName, FunctionalPoint tag){
58          this(tag);
59          this.testName = testName;
60      }
61  
62      public void setTestName(String name){
63          testName = name;
64      }
65  
66      public String getTestName(){
67          return testName;
68      }
69  
70      public String getTestCaseDocsFile(){
71          return testCaseDocsFile;
72      }
73  
74      public void setTestCaseDocsFile(String testCaseDocsFile){
75          this.testCaseDocsFile = testCaseDocsFile;
76      }
77  
78  
79      public Calendar getDateTimeExecuted() {
80          return dateTimeExecuted;
81      }
82  
83      public void setDateTimeExecuted(Calendar dateTimeExecuted) {
84          this.dateTimeExecuted = dateTimeExecuted;
85      }
86  
87      /***
88       * Gets a list of failed child results
89       * @return a list of failed child results
90       */
91      public List getFailedResults(){
92          if (getError() != null && !failedResults.contains(this)) {
93              failedResults.add(this);
94          }
95          return failedResults;
96      }
97  
98  
99      public void destroy(){
100         super.destroy();
101         testCaseDocsFile = null;
102     }
103 
104     /***
105      * @return an XML representation of the results
106      */
107     public String toXML(){
108         StringBuffer str = new StringBuffer("\n");
109         str.append("\t<test-case>\n");
110         str.append("\t\t<test-case-name>").append(testName).append("</test-case-name>\n");
111         str.append(super.toXML());
112         if (testCaseDocsFile != null && testCaseDocsFile.length() > 0) {
113             str.append("\t\t<doc-file>").append(testCaseDocsFile.replace('//','/')).append("</doc-file>\n");
114         }
115         str.append(super.toXML());
116         str.append("\t</test-case>\n");
117         return str.toString();
118     }
119 
120     public List getCountableResults(){
121         List countabeResults = new ArrayList();
122         countabeResults.addAll(getCountableChildResults());
123         if (countabeResults.size() == 0) {
124             countabeResults.add(this);
125         }
126         return countabeResults;
127     }
128 
129     /***
130      * Gets all the failed ancestor children results that can be counted as
131      * test case results
132      * 
133      * @return List
134      */
135     public List getFailedCountableResults(){
136         List failedCountableResults = super.getFailedCountableResults();
137         if (failedCountableResults.size() == 0 &&
138             isCountableResultFailed()) {
139             failedCountableResults.add(this);
140         }
141         return failedCountableResults;
142     }
143 
144     /***
145      *  Gets the percentage total tests passed.
146      *  @return the summary for the total tests run, passed and failed.
147      */
148     public String getPercentagePassed() {
149         int testsRun = getCountableResults().size();
150         int testsFailed = getFailedCountableResults().size();
151         return getPercentagePassed(testsRun, testsFailed);
152     }
153 
154     /***
155      *  Gets the percentage total tests passed.
156      * @param testsRun The number of tests run
157      * @param testsFailed The number of tests failed
158      *  @return the percentage total tests passed.
159      */
160     public static String getPercentagePassed(int testsRun, int testsFailed) {
161         String percentagePassed = "N/A";
162         try {
163             NumberFormat nf = NumberFormat.getPercentInstance();
164             int testsPassed = testsRun - testsFailed;
165             if (testsRun > 0) {
166                 percentagePassed = nf.format((double) testsPassed/testsRun);
167             } else {
168                 percentagePassed = nf.format(0);
169             }
170         } catch (ArithmeticException ae) {
171             System.err.println("Problem calculating percentage passed");
172             ae.printStackTrace();
173         }
174         return percentagePassed;
175     }
176 
177     public void recordFailureToCountableResult(){
178         countFailure();
179     }
180 
181     public boolean isDataDriven() {
182         return (getTag().getAttribute("useCSV").getValue().equals("true"));
183     }
184 
185     /////////////////////////////////////////////////////////////////////////////////
186     //                CountableResult methods                                      //
187     /////////////////////////////////////////////////////////////////////////////////
188 
189     public void countFailure(){
190         countableResultFailed = true;
191     }
192 
193     public boolean isCountableResultFailed(){
194         return countableResultFailed;
195     }
196 }