summaryrefslogtreecommitdiff
path: root/qpid/java/junit-toolkit/src/main/org/apache/qpid/junit/extensions/listeners/ConsoleTestListener.java
blob: 276fec328ebeaf97cf10314850c8c5af9765ee5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
package org.apache.qpid.junit.extensions.listeners;

import junit.framework.AssertionFailedError;
import junit.framework.Test;
import junit.framework.TestListener;

import org.apache.qpid.junit.extensions.SleepThrottle;
import org.apache.qpid.junit.extensions.Throttle;

import java.util.Properties;

/**
 * ConsoleTestListener provides feedback to the console, as test timings are taken, by drawing a '.', or an 'E', or an
 * 'F', for each test that passes, is in error or fails. It does this for every test result registered with the framework,
 * not just on the completion of each test method as the JUnit one does. It also uses a throttle to cap the rate of
 * dot drawing, as exessively high rates can degrade test performance without providing much usefull feedback to the user.
 * Unlike the JUnit dot drawing feedback, this one will correctly wrap lines when tests are run concurrently (the
 * rate capping ensures that this does not become a hot-spot for thread contention).
 *
 * <p/>Where rate capping causes the conflation of multiple requested dots into a single dot, the dot that is actually
 * drawn will be the worst result within the conflation period, that is, error is worse than fail which is worse than pass.
 *
 * <p/><table id="crc"><caption>CRC Card</caption>
 * <tr><th> Responsibilities <th> Collaborations
 * <tr><td> Draw dots as each test result completes, at a capped rate.
 * </table>
 *
 * @author Rupert Smith
 */
public class ConsoleTestListener implements TestListener, TKTestListener
{
    /** Used to indicate a test pass. */
    private static final int PASS = 1;

    /** Used to indicate a test failure. */
    private static final int FAIL = 2;

    /** Used to indicate a test error. */
    private static final int ERROR = 3;

    /** Defines the maximum number of columns of dots to print. */
    private static final int MAX_COLUMNS = 80;

    /** Used to throttle the dot writing rate. */
    Throttle throttle;

    /** Tracks the worst test result so far, when the throttled print method must conflate results due to throttling. */
    private int conflatedResult = 0;

    /** Tracks the column count as dots are printed, so that newlines can be inserted at the right margin. */
    private int columnCount = 0;

    /** Used as a monitor on the print method criticial section, to ensure that line ends always happen in the right place. */
    private final Object printMonitor = new Object();

    /**
     * Creates a dot drawing feedback test listener, set by default to 80 columns at 80 dots per second capped rate.
     */
    public ConsoleTestListener()
    {
        throttle = new SleepThrottle();
        throttle.setRate(80f);
    }

    /**
     * Prints dots at a capped rate, conflating the requested type of dot to draw if this method is called at a rate
     * higher than the capped rate. The conflation works by always printing the worst result that occurs within the
     * conflation period, that is, error is worse than fail which is worse than a pass.
     *
     * @param result The type of dot to draw, {@link #PASS}, {@link #FAIL} or {@link #ERROR}.
     */
    private void throttledPrint(int result)
    {
        conflatedResult = (result > conflatedResult) ? result : conflatedResult;

        if (throttle.checkThrottle())
        {
            synchronized (printMonitor)
            {
                switch (conflatedResult)
                {
                default:
                case PASS:
                    System.out.print('.');
                    break;

                case FAIL:
                    System.out.print('F');
                    break;

                case ERROR:
                    System.out.print('E');
                    break;
                }

                columnCount = (columnCount >= MAX_COLUMNS) ? 0 : (columnCount + 1);

                if (columnCount == 0)
                {
                    System.out.print('\n');
                }

                conflatedResult = 0;
            }
        }
    }

    /**
     * An error occurred.
     *
     * @param test The test in error. Ignored.
     * @param t    The error that the test threw. Ignored.
     */
    public void addError(Test test, Throwable t)
    {
        throttledPrint(ERROR);
    }

    /**
     * A failure occurred.
     *
     * @param test The test that failed. Ignored.
     * @param t    The assertion failure that the test threw. Ignored.
     */
    public void addFailure(Test test, AssertionFailedError t)
    {
        throttledPrint(FAIL);
    }

    /**
     * A test ended.
     *
     * @param test The test that ended. Ignored.
     */
    public void endTest(Test test)
    {
        throttledPrint(PASS);
    }

    /**
     * A test started.
     *
     * @param test The test that started. Ignored.
     */
    public void startTest(Test test)
    { }

    /**
     * Resets the test results to the default state of time zero, memory usage zero, parameter zero, test passed.
     *
     * @param test     The test to resest any results for.
     * @param threadId Optional thread id if not calling from thread that started the test method. May be null.
     */
    public void reset(Test test, Long threadId)
    { }

    /**
     * Should be called every time a test completes with the run time of that test.
     *
     * @param test     The name of the test.
     * @param nanos    The run time of the test in nanoseconds.
     * @param threadId Optional thread id if not calling from thread that started the test method. May be null.
     */
    public void timing(Test test, long nanos, Long threadId)
    { }

    /**
     * Optionally called every time a test completes with the second timing test.
     *
     * @param test    The name of the test.
     * @param nanos   The second timing information of the test in nanoseconds.
     * @param threadId Optional thread id if not calling from thread that started the test method. May be null.
     */
    public void timing2(Test test, Long nanos, Long threadId)
    { }

    /**
     * Should be called every time a test completed with the amount of memory used before and after the test was run.
     *
     * @param test     The test which memory was measured for.
     * @param memStart The total JVM memory used before the test was run.
     * @param memEnd   The total JVM memory used after the test was run.
     * @param threadId Optional thread id if not calling from thread that started the test method. May be null.
     */
    public void memoryUsed(Test test, long memStart, long memEnd, Long threadId)
    { }

    /**
     * Should be called every time a parameterized test completed with the int value of its test parameter.
     *
     * @param test      The test which memory was measured for.
     * @param parameter The int parameter value.
     * @param threadId  Optional thread id if not calling from thread that started the test method. May be null.
     */
    public void parameterValue(Test test, int parameter, Long threadId)
    { }

    /**
     * Should be called every time a test completes with the current number of test threads running.
     *
     * @param test     The test for which the measurement is being generated.
     * @param threads  The number of tests being run concurrently.
     * @param threadId Optional thread id if not calling from thread that started the test method. May be null.
     */
    public void concurrencyLevel(Test test, int threads, Long threadId)
    { }

    /**
     * Called when a test completes. Success, failure and errors. This method should be used when registering an
     * end test from a different thread than the one that started the test.
     *
     * @param test     The test which completed.
     * @param threadId Optional thread id if not calling from thread that started the test method. May be null.
     */
    public void endTest(Test test, Long threadId)
    {
        throttledPrint(PASS);
    }

    /**
     * Called when a test completes to mark it as a test fail. This method should be used when registering a
     * failure from a different thread than the one that started the test.
     *
     * @param test     The test which failed.
     * @param e        The assertion that failed the test.
     * @param threadId Optional thread id if not calling from thread that started the test method. May be null.
     */
    public void addFailure(Test test, AssertionFailedError e, Long threadId)
    {
        throttledPrint(FAIL);
    }

    /**
     * Notifies listeners of the start of a complete run of tests.
     */
    public void startBatch()
    { }

    /**
     * Notifies listeners of the end of a complete run of tests.
     *
     * @param parameters The optional test parameters to log out with the batch results.
     */
    public void endBatch(Properties parameters)
    { }

    /**
     * Notifies listeners of the tests read/set properties.
     *
     * @param properties The tests read/set properties.
     */
    public void properties(Properties properties)
    { }
}