1 /***
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Exoffice Technologies. For written permission,
18 * please contact tma@netspace.net.au.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Exoffice Technologies. Exolab is a registered
23 * trademark of Exoffice Technologies.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 2003-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42 *
43 * $Id: TestStatistics.java,v 1.4 2004/01/31 13:44:24 tanderson Exp $
44 */
45 package org.exolab.jmscts.core;
46
47 import java.util.HashMap;
48
49 import org.apache.log4j.Category;
50
51 import org.exolab.castor.types.Time;
52
53 import org.exolab.jmscts.report.Failure;
54 import org.exolab.jmscts.report.ReportHelper;
55 import org.exolab.jmscts.report.Statistic;
56 import org.exolab.jmscts.report.Statistics;
57 import org.exolab.jmscts.report.TestRun;
58 import org.exolab.jmscts.report.TestRuns;
59 import org.exolab.jmscts.report.types.StatisticType;
60
61
62 /***
63 * This class maintains statistics for the test suite
64 *
65 * @version $Revision: 1.4 $ $Date: 2004/01/31 13:44:24 $
66 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
67 */
68 public class TestStatistics {
69
70 /***
71 * A map of test names to their corresponding TestRuns instances
72 */
73 private HashMap _testRuns = new HashMap();
74
75 /***
76 * The current test
77 */
78 private TestRun _current;
79
80 /***
81 * The statistics for all executed tests
82 */
83 private Statistics _statistics = new Statistics();
84
85
86 /***
87 * The logger
88 */
89 private static final Category log =
90 Category.getInstance(TestStatistics.class);
91
92
93 /***
94 * Log a statistic for the current test
95 *
96 * @param statistic the statistic to log
97 */
98 public synchronized void log(Statistic statistic) {
99 _current.addStatistic(statistic);
100 }
101
102 /***
103 * Log a statistic for the current test
104 *
105 * @param type the type of the statistic
106 * @param count the number of iterations
107 * @param start the start time, in milliseconds
108 * @param end the end time, in milliseconds
109 */
110 public void log(StatisticType type, int count, long start, long end) {
111 long duration = end - start;
112 log(type, count, duration);
113 }
114
115 /***
116 *
117 * Log a statistic for the current test
118 *
119 * @param type the type of the statistic
120 * @param count the number of iterations
121 * @param duration the elapsed time, in milliseconds
122 */
123 public void log(StatisticType type, int count, long duration) {
124 final int oneSec = 1000;
125 Statistic statistic = new Statistic();
126 statistic.setType(type);
127 statistic.setCount(count);
128 statistic.setTime(new Time(duration));
129 double seconds = 0.0;
130 double rate = 0.0;
131 if (duration != 0) {
132 seconds = (double) duration / oneSec;
133 rate = (double) count / seconds;
134 }
135 statistic.setRate(rate);
136 log(statistic);
137 }
138
139 /***
140 * Returns the test suite statistics
141 *
142 * @return the test suite statistics
143 */
144 public synchronized Statistics getStatistics() {
145 return _statistics;
146 }
147
148 /***
149 * Log the start of a test
150 *
151 * @param test the test case
152 */
153 public synchronized void begin(JMSTestCase test) {
154 String name = ReportHelper.getName(test);
155 TestRuns runs = (TestRuns) _testRuns.get(name);
156 if (runs == null) {
157 runs = ReportHelper.getTestRuns(test);
158 _testRuns.put(name, runs);
159 _statistics.addTestRuns(runs);
160 }
161 TestRun run = ReportHelper.getTestRun(test);
162 runs.addTestRun(run);
163 _current = run;
164 }
165
166 /***
167 * Log the end of a test
168 *
169 * @param test the test that has completed
170 */
171 public synchronized void end(JMSTestCase test) {
172 _current = null;
173 }
174
175 /***
176 * Log a test case failure
177 *
178 * @param test the test case
179 * @param cause the reason for the failure
180 * @param rootCause the root cause of the failure. May be null
181 */
182 public synchronized void failed(JMSTestCase test, Throwable cause,
183 Throwable rootCause) {
184
185 if (_current != null) {
186 Failure failure = ReportHelper.getFailure(cause, rootCause);
187 _current.setFailure(failure);
188 } else {
189 log.error("Current test=" + ReportHelper.getName(test)
190 + " is invalid. Cannot record failure: "
191 + cause.getMessage());
192 }
193 }
194
195 }
This page was automatically generated by Maven