KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > metrics > TestCounterMeasurement


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.metrics;
34
35 import junit.framework.*;
36
37 public class TestCounterMeasurement extends TestCase implements MeasurementVisitor {
38     private CounterMeasurement measurement;
39     private Measurement visited;
40     
41     protected void setUp() {
42         measurement = new CounterMeasurement(null, null, null);
43     }
44
45     public void testMeasurementDescriptor() throws Exception JavaDoc {
46         MeasurementDescriptor descriptor = new MeasurementDescriptor();
47         descriptor.setShortName("foo");
48         descriptor.setLongName("bar");
49         descriptor.setClassFor(CounterMeasurement.class);
50
51         measurement = (CounterMeasurement) descriptor.createMeasurement();
52         
53         assertNotNull(measurement.getDescriptor());
54         assertEquals(CounterMeasurement.class, measurement.getDescriptor().getClassFor());
55         assertEquals("foo", measurement.getShortName());
56         assertEquals("bar", measurement.getLongName());
57     }
58
59     public void testCreateFromMeasurementDescriptor() throws Exception JavaDoc {
60         MeasurementDescriptor descriptor = new MeasurementDescriptor();
61         descriptor.setShortName("foo");
62         descriptor.setLongName("bar");
63         descriptor.setClassFor(CounterMeasurement.class);
64
65         measurement = (CounterMeasurement) descriptor.createMeasurement();
66         
67         assertNotNull(measurement);
68         assertEquals(descriptor, measurement.getDescriptor());
69         assertSame(descriptor, measurement.getDescriptor());
70         assertEquals(CounterMeasurement.class, measurement.getClass());
71         assertEquals("foo", measurement.getShortName());
72         assertEquals("bar", measurement.getLongName());
73     }
74     
75     public void testCreateNumber() {
76         measurement = new CounterMeasurement(null, null, "2");
77         assertEquals(2.0, measurement.doubleValue(), 0.01);
78
79         measurement = new CounterMeasurement(null, null, "2.0");
80         assertEquals(2.0, measurement.doubleValue(), 0.01);
81
82         measurement = new CounterMeasurement(null, null, "-2.5");
83         assertEquals(-2.5, measurement.doubleValue(), 0.01);
84
85         measurement = new CounterMeasurement(null, null, " 2.0 ");
86         assertEquals(2.0, measurement.doubleValue(), 0.01);
87     }
88     
89     public void testCreateInvalid() {
90         measurement = new CounterMeasurement(null, null, null);
91         assertEquals(0.0, measurement.doubleValue(), 0.01);
92
93         measurement = new CounterMeasurement(null, null, "foobar");
94         assertEquals(0.0, measurement.doubleValue(), 0.01);
95     }
96
97     public void testCreateDefault() {
98         assertEquals(0.0, measurement.doubleValue(), 0.01);
99     }
100     
101     public void testAddObject() {
102         measurement.add(new Object JavaDoc());
103
104         assertEquals(1, measurement.intValue());
105         assertEquals(1, measurement.doubleValue(), 0.01);
106         assertEquals(1, measurement.getValue().intValue());
107
108         measurement.add(new Object JavaDoc());
109
110         assertEquals(2, measurement.getValue().intValue());
111         assertEquals(2, measurement.getValue().doubleValue(), 0.01);
112     }
113     
114     public void testAddNumber() {
115         measurement.add(new Integer JavaDoc(1));
116
117         assertEquals(1, measurement.intValue());
118         assertEquals(1, measurement.doubleValue(), 0.01);
119         assertEquals(1, measurement.getValue().intValue());
120
121         measurement.add(new Float JavaDoc(0.5));
122
123         assertEquals(1, measurement.getValue().intValue());
124         assertEquals(1.5, measurement.getValue().doubleValue(), 0.01);
125     }
126     
127     public void testAddInt() {
128         measurement.add(1);
129
130         assertEquals(1, measurement.intValue());
131         assertEquals(1, measurement.doubleValue(), 0.01);
132         assertEquals(1, measurement.getValue().intValue());
133
134         measurement.add(2);
135
136         assertEquals(3, measurement.intValue());
137         assertEquals(3, measurement.doubleValue(), 0.01);
138         assertEquals(3, measurement.getValue().intValue());
139     }
140     
141     public void testAddFloat() {
142         measurement.add(1.0);
143
144         assertEquals(1, measurement.intValue());
145         assertEquals(1, measurement.doubleValue(), 0.01);
146         assertEquals(1, measurement.getValue().intValue());
147
148         measurement.add(0.5);
149
150         assertEquals(1, measurement.intValue());
151         assertEquals(1.5, measurement.doubleValue(), 0.01);
152         assertEquals(1, measurement.getValue().intValue());
153     }
154
155     public void testSubstract() {
156         measurement.add(new Integer JavaDoc(-1));
157
158         assertEquals(-1, measurement.getValue().intValue());
159         assertEquals(-1, measurement.getValue().doubleValue(), 0.01);
160
161         measurement.add(new Float JavaDoc(0.4));
162
163         assertEquals(0, measurement.getValue().intValue());
164         assertEquals(-0.6, measurement.getValue().doubleValue(), 0.01);
165
166         measurement.add(new Float JavaDoc(0.1));
167
168         assertEquals(0, measurement.getValue().intValue());
169         assertEquals(-0.5, measurement.getValue().doubleValue(), 0.01);
170     }
171
172     public void testInUndefinedRange() {
173         assertTrue(measurement.isInRange());
174
175         measurement.add(1);
176         
177         assertTrue(measurement.isInRange());
178
179         measurement.add(2);
180         
181         assertTrue(measurement.isInRange());
182     }
183
184     public void testInOpenRange() throws Exception JavaDoc {
185         MeasurementDescriptor descriptor = new MeasurementDescriptor();
186         descriptor.setShortName("foo");
187         descriptor.setLongName("bar");
188         descriptor.setClassFor(CounterMeasurement.class);
189
190         measurement = (CounterMeasurement) descriptor.createMeasurement();
191         
192         assertTrue(measurement.isInRange());
193
194         measurement.add(1);
195         
196         assertTrue(measurement.isInRange());
197
198         measurement.add(2);
199         
200         assertTrue(measurement.isInRange());
201     }
202
203     public void testInLowerBoundRange() throws Exception JavaDoc {
204         MeasurementDescriptor descriptor = new MeasurementDescriptor();
205         descriptor.setShortName("foo");
206         descriptor.setLongName("bar");
207         descriptor.setClassFor(CounterMeasurement.class);
208         descriptor.setLowerThreshold(new Integer JavaDoc(1));
209
210         measurement = (CounterMeasurement) descriptor.createMeasurement();
211         
212         assertTrue(!measurement.isInRange());
213
214         measurement.add(1);
215         
216         assertTrue(measurement.isInRange());
217
218         measurement.add(2);
219         
220         assertTrue(measurement.isInRange());
221     }
222
223     public void testInStringLowerBoundRange() throws Exception JavaDoc {
224         MeasurementDescriptor descriptor = new MeasurementDescriptor();
225         descriptor.setShortName("foo");
226         descriptor.setLongName("bar");
227         descriptor.setClassFor(CounterMeasurement.class);
228         descriptor.setLowerThreshold("1");
229
230         measurement = (CounterMeasurement) descriptor.createMeasurement();
231         
232         assertTrue(!measurement.isInRange());
233
234         measurement.add(1);
235         
236         assertTrue(measurement.isInRange());
237
238         measurement.add(2);
239         
240         assertTrue(measurement.isInRange());
241     }
242
243     public void testInUpperBoundRange() throws Exception JavaDoc {
244         MeasurementDescriptor descriptor = new MeasurementDescriptor();
245         descriptor.setShortName("foo");
246         descriptor.setLongName("bar");
247         descriptor.setClassFor(CounterMeasurement.class);
248         descriptor.setUpperThreshold(new Float JavaDoc(1.5));
249
250         measurement = (CounterMeasurement) descriptor.createMeasurement();
251         
252         assertTrue(measurement.isInRange());
253
254         measurement.add(1);
255         
256         assertTrue(measurement.isInRange());
257
258         measurement.add(2);
259         
260         assertTrue(!measurement.isInRange());
261     }
262
263     public void testInStringUpperBoundRange() throws Exception JavaDoc {
264         MeasurementDescriptor descriptor = new MeasurementDescriptor();
265         descriptor.setShortName("foo");
266         descriptor.setLongName("bar");
267         descriptor.setClassFor(CounterMeasurement.class);
268         descriptor.setUpperThreshold("1.5");
269
270         measurement = (CounterMeasurement) descriptor.createMeasurement();
271         
272         assertTrue(measurement.isInRange());
273
274         measurement.add(1);
275         
276         assertTrue(measurement.isInRange());
277
278         measurement.add(2);
279         
280         assertTrue(!measurement.isInRange());
281     }
282
283     public void testInBoundRange() throws Exception JavaDoc {
284         MeasurementDescriptor descriptor = new MeasurementDescriptor();
285         descriptor.setShortName("foo");
286         descriptor.setLongName("bar");
287         descriptor.setClassFor(CounterMeasurement.class);
288         descriptor.setLowerThreshold(new Integer JavaDoc(1));
289         descriptor.setUpperThreshold(new Float JavaDoc(1.5));
290
291         measurement = (CounterMeasurement) descriptor.createMeasurement();
292         
293         assertTrue(!measurement.isInRange());
294
295         measurement.add(1);
296         
297         assertTrue(measurement.isInRange());
298
299         measurement.add(2);
300         
301         assertTrue(!measurement.isInRange());
302     }
303
304     public void testInStringBoundRange() throws Exception JavaDoc {
305         MeasurementDescriptor descriptor = new MeasurementDescriptor();
306         descriptor.setShortName("foo");
307         descriptor.setLongName("bar");
308         descriptor.setClassFor(CounterMeasurement.class);
309         descriptor.setLowerThreshold("1");
310         descriptor.setUpperThreshold("1.5");
311
312         measurement = (CounterMeasurement) descriptor.createMeasurement();
313         
314         assertTrue(!measurement.isInRange());
315
316         measurement.add(1);
317         
318         assertTrue(measurement.isInRange());
319
320         measurement.add(2);
321         
322         assertTrue(!measurement.isInRange());
323     }
324
325     public void testAccept() {
326         visited = null;
327         measurement.accept(this);
328         assertSame(measurement, visited);
329     }
330
331     public void testEmpty() throws Exception JavaDoc {
332         MeasurementDescriptor descriptor = new MeasurementDescriptor();
333         descriptor.setShortName("foo");
334         descriptor.setLongName("bar");
335         descriptor.setClassFor(CounterMeasurement.class);
336
337         measurement = (CounterMeasurement) descriptor.createMeasurement();
338         
339         assertTrue("Before Add()", measurement.isEmpty());
340
341         measurement.add(1);
342         
343         assertFalse("After Add(1)", measurement.isEmpty());
344
345         measurement.add(-1);
346         
347         assertFalse("After Add(-1)", measurement.isEmpty());
348     }
349     
350     public void visitStatisticalMeasurement(StatisticalMeasurement measurement) {
351         // Do nothing
352
}
353     
354     public void visitRatioMeasurement(RatioMeasurement measurement) {
355         // Do nothing
356
}
357     
358     public void visitNbSubMetricsMeasurement(NbSubMetricsMeasurement measurement) {
359         // Do nothing
360
}
361     
362     public void visitCounterMeasurement(CounterMeasurement measurement) {
363         visited = measurement;
364     }
365     
366     public void visitContextAccumulatorMeasurement(ContextAccumulatorMeasurement measurement) {
367         // Do nothing
368
}
369     
370     public void visitNameListMeasurement(NameListMeasurement measurement) {
371         // Do nothing
372
}
373     
374     public void visitSubMetricsAccumulatorMeasurement(SubMetricsAccumulatorMeasurement measurement) {
375         // Do nothing
376
}
377     
378     public void visitSumMeasurement(SumMeasurement measurement) {
379         // Do nothing
380
}
381 }
382
Popular Tags