KickJava   Java API By Example, From Geeks To Geeks.

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


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