1   /*
2    * The MIT License
3    *
4    * Copyright (c) <2010> <tap4j>
5    * 
6    * Permission is hereby granted, free of charge, to any person obtaining a copy
7    * of this software and associated documentation files (the "Software"), to deal
8    * in the Software without restriction, including without limitation the rights
9    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10   * copies of the Software, and to permit persons to whom the Software is
11   * furnished to do so, subject to the following conditions:
12   * 
13   * The above copyright notice and this permission notice shall be included in
14   * all copies or substantial portions of the Software.
15   * 
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22   * THE SOFTWARE.
23   */
24  package org.tap4j.model;
25  
26  import java.io.Serializable;
27  import java.util.LinkedList;
28  import java.util.List;
29  
30  import org.tap4j.util.StatusValues;
31  
32  /**
33   * A Test Set is the top element in a TAP File. It holds references to the
34   * Header, Plan, List of Test Results and the rest of elements in TAP spec.
35   * 
36   * @since 1.0
37   */
38  public class TestSet implements Serializable {
39  
40      /*
41       * Serial Version UID.
42       */
43      private static final long serialVersionUID = 114777557084672201L;
44  
45      /**
46       * TAP Header.
47       */
48      private Header header;
49  
50      /**
51       * TAP Plan.
52       */
53      private Plan plan;
54  
55      /**
56       * List of TAP Lines.
57       */
58      private List<TapResult> tapLines = new LinkedList<TapResult>();
59  
60      /**
61       * List of Test Results.
62       */
63      private List<TestResult> testResults = new LinkedList<TestResult>();
64  
65      /**
66       * List of Bail Outs.
67       */
68      private List<BailOut> bailOuts = new LinkedList<BailOut>();
69  
70      /**
71       * List of comments.
72       */
73      private List<Comment> comments = new LinkedList<Comment>();
74  
75      /**
76       * TAP Footer.
77       */
78      private Footer footer;
79  
80      /**
81       * Default constructor.
82       */
83      public TestSet() {
84          super();
85      }
86  
87      /**
88       * @return TAP Header.
89       */
90      public Header getHeader() {
91          return this.header;
92      }
93  
94      /**
95       * @param header TAP Header.
96       */
97      public void setHeader(Header header) {
98          this.header = header;
99      }
100 
101     /**
102      * @return TAP Plan.
103      */
104     public Plan getPlan() {
105         return this.plan;
106     }
107 
108     /**
109      * @param plan TAP Plan.
110      */
111     public void setPlan(Plan plan) {
112         this.plan = plan;
113     }
114 
115     /**
116      * @return List of TAP Lines. These lines may be either a TestResult or a
117      *         BailOut.
118      */
119     public List<TapResult> getTapLines() {
120         return this.tapLines;
121     }
122 
123     /**
124      * @return List of Test Results.
125      */
126     public List<TestResult> getTestResults() {
127         return this.testResults;
128     }
129     
130     /**
131      * @return Next test number.
132      */
133     public int getNextTestNumber() {
134         return (this.testResults.size() + 1);
135     }
136 
137     /**
138      * @return List of Bail Outs.
139      */
140     public List<BailOut> getBailOuts() {
141         return this.bailOuts;
142     }
143 
144     /**
145      * @return List of Comments.
146      */
147     public List<Comment> getComments() {
148         return this.comments;
149     }
150 
151     /**
152      * Adds a new TAP Line.
153      * 
154      * @param tapLine TAP Line.
155      * @return True if the TAP Line could be added into the list successfully.
156      */
157     public boolean addTapLine(TapResult tapLine) {
158         return this.tapLines.add(tapLine);
159     }
160 
161     /**
162      * Adds a TestResult into the list of TestResults. If the TestResult Test
163      * Number is null or less than or equals to zero it is changed to the next
164      * Test Number in the sequence.
165      * 
166      * @param testResult TAP TestResult.
167      * @return Whether could add to TestResult list or not.
168      */
169     public boolean addTestResult(TestResult testResult) {
170         if (testResult.getTestNumber() == null ||
171             testResult.getTestNumber() <= 0) {
172             testResult.setTestNumber(this.testResults.size() + 1);
173         }
174         this.testResults.add(testResult);
175         return this.tapLines.add(testResult);
176     }
177 
178     /**
179      * @param bailOut Bail Out.
180      * @return Whether could add to BailOut list or not.
181      */
182     public boolean addBailOut(BailOut bailOut) {
183         this.bailOuts.add(bailOut);
184         return this.tapLines.add(bailOut);
185     }
186 
187     /**
188      * @param comment Comment. Whether could add to Comment list or not.
189      */
190     public boolean addComment(Comment comment) {
191         this.comments.add(comment);
192         return this.tapLines.add(comment);
193     }
194 
195     /**
196      * Removes a TAP Line from the list.
197      * 
198      * @param tapLine TAP Line object.
199      * @return True if could successfully remove the TAP Line from the list.
200      */
201     protected boolean removeTapLine(TapResult tapLine) {
202         return this.tapLines.remove(tapLine);
203     }
204 
205     /**
206      * Removes a Test Result from the list.
207      * 
208      * @param testResult Test Result.
209      * @return True if could successfully remove the Test Result from the list.
210      */
211     public boolean removeTestResult(TestResult testResult) {
212         boolean flag = false;
213         if (this.tapLines.remove(testResult)) {
214             this.testResults.remove(testResult);
215             flag = true;
216         }
217         return flag;
218     }
219 
220     /**
221      * Removes a Bail Out from the list.
222      * 
223      * @param bailOut Bail Out object.
224      * @return True if could successfully remove the Bail Out from the list.
225      */
226     public boolean removeBailOut(BailOut bailOut) {
227         boolean flag = false;
228         if (this.tapLines.remove(bailOut)) {
229             this.bailOuts.remove(bailOut);
230             flag = true;
231         }
232         return flag;
233     }
234 
235     /**
236      * Removes a Comment from the list.
237      * 
238      * @param comment Comment.
239      * @return True if could successfully remove the Comment from the list.
240      */
241     public boolean removeComment(Comment comment) {
242         boolean flag = false;
243         if (this.tapLines.remove(comment)) {
244             this.comments.remove(comment);
245             flag = true;
246         }
247         return flag;
248     }
249 
250     /**
251      * @return Number of TAP Lines. It includes Test Results, Bail Outs and
252      *         Comments (the footer is not included).
253      */
254     public int getNumberOfTapLines() {
255         return this.tapLines.size();
256     }
257 
258     /**
259      * @return Number of Test Results.
260      */
261     public int getNumberOfTestResults() {
262         return this.testResults.size();
263     }
264 
265     /**
266      * @return Number of Bail Outs.
267      */
268     public int getNumberOfBailOuts() {
269         return this.bailOuts.size();
270     }
271 
272     /**
273      * @return Number of Comments.
274      */
275     public int getNumberOfComments() {
276         return this.comments.size();
277     }
278 
279     /**
280      * @return Footer
281      */
282     public Footer getFooter() {
283         return this.footer;
284     }
285 
286     /**
287      * @param footer Footer
288      */
289     public void setFooter(Footer footer) {
290         this.footer = footer;
291     }
292 
293     /**
294      * @return True if it has any Bail Out statement, false otherwise.
295      */
296     public boolean hasBailOut() {
297         boolean isBailOut = false;
298 
299         for (TapResult tapLine : tapLines) {
300             if (tapLine instanceof BailOut) {
301                 isBailOut = true;
302                 break;
303             }
304         }
305 
306         return isBailOut;
307     }
308 
309     /*
310      * (non-Javadoc)
311      * @see org.tap4j.TapConsumer#containsOk()
312      */
313     public Boolean containsOk() {
314         Boolean containsOk = false;
315 
316         for (TestResult testResult : this.testResults) {
317             if (testResult.getStatus().equals(StatusValues.OK)) {
318                 containsOk = true;
319                 break;
320             }
321         }
322 
323         return containsOk;
324     }
325 
326     /*
327      * (non-Javadoc)
328      * @see org.tap4j.TapConsumer#containsNotOk()
329      */
330     public Boolean containsNotOk() {
331         Boolean containsNotOk = false;
332 
333         for (TestResult testResult : this.testResults) {
334             if (testResult.getStatus().equals(StatusValues.NOT_OK)) {
335                 containsNotOk = true;
336                 break;
337             }
338         }
339 
340         return containsNotOk;
341     }
342 
343     /*
344      * (non-Javadoc)
345      * @see org.tap4j.TapConsumer#containsBailOut()
346      */
347     public Boolean containsBailOut() {
348         return this.bailOuts.size() > 0;
349     }
350 
351     /*
352      * (non-Javadoc)
353      * @see org.tap4j.TapConsumer#getTestResult(java.lang.Integer)
354      */
355     public TestResult getTestResult(Integer testNumber) {
356         TestResult foundTestResult = null;
357 
358         for (TestResult testResult : this.testResults) {
359             if (testResult.getTestNumber() != null &&
360                 testResult.getTestNumber().equals(testNumber)) {
361                 foundTestResult = testResult;
362                 break;
363             }
364         }
365 
366         return foundTestResult;
367     }
368 
369 }