1 // Tags: not-a-test |
|
2 // Copyright (C) 2004 by Object Refinery Limited |
|
3 // Written by David Gilbert (david.gilbert@object-refinery.com) |
|
4 // Modified by Fabien DUMINY (fduminy@jnode.org) |
|
5 |
|
6 // This file is part of Mauve Reporter. |
|
7 |
|
8 // Mauve Reporter is free software; you can redistribute it and/or modify |
|
9 // it under the terms of the GNU General Public License as published by |
|
10 // the Free Software Foundation; either version 2 of the License, or |
|
11 // (at your option) any later version. |
|
12 |
|
13 // Mauve Reporter is distributed in the hope that it will be useful, |
|
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16 // GNU General Public License for more details. |
|
17 |
|
18 // You should have received a copy of the GNU General Public License |
|
19 // along with Mauve Reporter; if not, write to the Free Software |
|
20 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
21 package gnu.testlet.runner; |
|
22 |
|
23 import java.util.*; |
|
24 import java.io.*; |
|
25 |
|
26 /** |
|
27 * Represents the result of running one test. A test usually contains multiple |
|
28 * checks and corresponds to a single method in the API being tested. There |
|
29 * are exceptions of course. |
|
30 */ |
|
31 public class TestResult implements Comparable, Result { |
|
32 |
|
33 /** The name of the test (usually the method name). */ |
|
34 private String name; |
|
35 |
|
36 /** A list containing results for each of the checks applied by the test. */ |
|
37 private List checkResults; |
|
38 |
|
39 private String error = null; |
|
40 |
|
41 /** |
|
42 * Creates a new result, initially empty. |
|
43 * @param name |
|
44 */ |
|
45 TestResult(String name) { |
|
46 this.name = name; |
|
47 checkResults = new ArrayList(); |
|
48 } |
|
49 |
|
50 /** |
|
51 * Returns the test name (this is most often the name of the method |
|
52 * being tested). |
|
53 * |
|
54 * @return The test name. |
|
55 */ |
|
56 public String getName() { |
|
57 return name; |
|
58 } |
|
59 |
|
60 /** |
|
61 * Sets the test name. |
|
62 * |
|
63 * @param name the name. |
|
64 */ |
|
65 void setName(String name) { |
|
66 this.name = name; |
|
67 } |
|
68 |
|
69 /** |
|
70 * Adds a check result. |
|
71 * |
|
72 * @param result the check result. |
|
73 */ |
|
74 void add(CheckResult result) { |
|
75 checkResults.add(result); |
|
76 } |
|
77 |
|
78 /** |
|
79 * Returns an iterator that provides access to all the check results. |
|
80 * |
|
81 * @return An iterator. |
|
82 */ |
|
83 public Iterator getCheckIterator() { |
|
84 return checkResults.iterator(); |
|
85 } |
|
86 |
|
87 /** |
|
88 * Returns the total number of checks performed by this test. |
|
89 * |
|
90 * @return The check count. |
|
91 */ |
|
92 public int getCheckCount() { |
|
93 return checkResults.size(); |
|
94 } |
|
95 |
|
96 /** |
|
97 * Returns the number of checks with the specified status. |
|
98 * |
|
99 * @param passed the check status. |
|
100 * |
|
101 * @return The number of checks passed or failed. |
|
102 */ |
|
103 public int getCheckCount(boolean passed) { |
|
104 int result = 0; |
|
105 Iterator iterator = checkResults.iterator(); |
|
106 while (iterator.hasNext()) { |
|
107 CheckResult check = (CheckResult) iterator.next(); |
|
108 if (check.getPassed() == passed) |
|
109 result++; |
|
110 } |
|
111 if(!passed && error != null) |
|
112 result++; // count stacktrace as a failure |
|
113 return result; |
|
114 } |
|
115 |
|
116 public int compareTo(Object obj) { |
|
117 TestResult that = (TestResult) obj; |
|
118 return getName().compareTo(that.getName()); |
|
119 } |
|
120 |
|
121 void failed(Throwable t) { |
|
122 ByteArrayOutputStream out = new ByteArrayOutputStream(); |
|
123 PrintWriter w = new PrintWriter(out, false); |
|
124 t.printStackTrace(w); |
|
125 w.close(); |
|
126 try { |
|
127 out.close(); |
|
128 error = out.toString(); |
|
129 } catch(IOException e) { // this should never happen.. |
|
130 } |
|
131 } |
|
132 |
|
133 public boolean isFailed() { |
|
134 return error != null; |
|
135 } |
|
136 |
|
137 public String getFailedMessage() { |
|
138 return error; |
|
139 } |
|
140 |
|
141 public void setFailedMessage(String error) { |
|
142 this.error = error; |
|
143 } |
|
144 } |
|