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.XMLable;
22  import net.sf.jameleon.bean.FunctionalPoint;
23  
24  import java.util.ArrayList;
25  import java.util.Iterator;
26  import java.util.List;
27  
28  /***
29   * A JameleonTestResult that can have children
30   */
31  public abstract class TestResultWithChildren extends JameleonTestResult implements HasChildResults {
32  
33      /***
34       * A list of Child Results
35       */
36      protected List childrenResults = new ArrayList();
37      /***
38       * A list of failed Child Results
39       */
40      protected List failedResults = new ArrayList();
41  
42      public TestResultWithChildren(){
43          super();
44      }
45  
46      public TestResultWithChildren(FunctionalPoint tag){
47          super(tag);
48      }
49  
50      public TestResultWithChildren(FunctionalPoint tag, HasChildResults parentResults){
51          super(tag, parentResults);
52      }
53  
54      /***
55       * Adds a JameleonTestResult to the list of JameleonTestResult int the TestCase
56       * @param childResult - a child result
57       */
58      public void addChildResult(JameleonTestResult childResult){
59          childrenResults.add(childResult);
60      }
61  
62      /***
63       * Adds a failed JameleonTestResult to the list of failed results
64       * @param failedResult - A failed child result
65       */
66      public void addFailedResult(JameleonTestResult failedResult){
67          failed = true;
68          failedResults.add(failedResult);
69          if (parentResults != null) {
70              parentResults.addFailedResult(failedResult);
71          }
72      }
73  
74      /***
75       * Gets all the ancestor children results
76       * 
77       * @return List
78       */
79      public List getAllChildrenResults(){
80          List allResults = new ArrayList();
81          JameleonTestResult jtr;
82          for (Iterator it = getChildrenResults().iterator(); it.hasNext();) {
83              jtr = (JameleonTestResult)it.next();
84              allResults.add(jtr);
85              if (jtr instanceof HasChildResults) {
86                  allResults.addAll(((HasChildResults)jtr).getAllChildrenResults());
87              }
88          }
89          return allResults;
90      }
91  
92      /***
93       * Gets all the ancestor leaf children results that have failed
94       *
95       * @return List
96       */
97      public List getAllFailedLeafChildrenResults(){
98          List allFailedLeafResults = new ArrayList();
99          List allResults = getAllChildrenResults();
100         JameleonTestResult jtr;
101         for (Iterator it = allResults.iterator(); it.hasNext();) {
102             jtr = (JameleonTestResult)it.next();
103             if (jtr.failed() && !jtr.isParent()) {
104                 allFailedLeafResults.add(jtr);
105             }
106         }
107         return allFailedLeafResults;
108     }
109 
110     /***
111      * Gets all the countable ancestor children results that can be counted as
112      * test case results
113      * 
114      * @return List
115      */
116     public List getCountableResults(){
117         List countabeResults = new ArrayList();
118         Object obj;
119         if (this instanceof CountableResult) {
120             countabeResults.add(this);
121         }
122         countabeResults.addAll(getCountableChildResults());
123         return countabeResults;
124     }
125 
126     protected List getCountableChildResults(){
127         List countabeResults = new ArrayList();
128         Object obj;
129         for (Iterator it = getChildrenResults().iterator(); it.hasNext();) {
130             obj = it.next();
131             if (obj instanceof HasChildResults) {
132                 countabeResults.addAll(((HasChildResults)obj).getCountableResults());
133             }else if (obj instanceof CountableResult) {
134                 countabeResults.add(obj);
135             }
136         }
137         return countabeResults;
138     }
139 
140     /***
141      * Gets all the failed ancestor children results that can be counted as
142      * test case results
143      * 
144      * @return List
145      */
146     public List getFailedCountableResults(){
147         List failedCountableResults = new ArrayList();
148         CountableResult cr;
149         for (Iterator it = getCountableResults().iterator(); it.hasNext();) {
150             cr = (CountableResult)it.next();
151             if (cr.isCountableResultFailed()) {
152                 failedCountableResults.add(cr);
153             }
154         }
155         return failedCountableResults;
156     }
157 
158     /***
159      * Gets a list of child results
160      * @return a list of child results
161      */
162     public List getChildrenResults(){
163         return childrenResults;
164     }
165 
166     /***
167      * Gets a list of failed child results
168      * @return a list of failed child results
169      */
170     public List getFailedResults(){
171         return failedResults;
172     }
173 
174     public void destroy(){
175         super.destroy();
176         childrenResults.clear();
177     }
178 
179 	public boolean isParent() {
180 		return true;
181 	}
182 
183     /***
184      * @return an XML representation of the results
185      */
186     public String toXML(){
187         StringBuffer str = new StringBuffer("\n");
188         str.append(super.toXML());
189         if (getChildrenResults().size() > 0) {
190             str.append("<children-results>\n");
191         }
192         for (Iterator it = getChildrenResults().iterator(); it.hasNext();) {
193             str.append(((XMLable)it.next()).toXML());
194         }
195         if (getChildrenResults().size() > 0) {
196             str.append("</children-results>\n");
197         }
198 
199         return str.toString();
200     }
201 
202     public boolean hasChildren(){
203         return true;
204     }
205 
206 }