KickJava   Java API By Example, From Geeks To Geeks.

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


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 TestMetrics extends TestCase {
38     public void testCreate() {
39         Metrics metrics = new Metrics("test");
40
41         assertEquals("test", metrics.getName());
42         assertNotNull(metrics.getMeasurement("test"));
43         assertEquals(NullMeasurement.class, metrics.getMeasurement("test").getClass());
44         assertTrue(metrics.getSubMetrics().isEmpty());
45     }
46
47     public void testTrack() throws Exception JavaDoc {
48         Metrics metrics = new Metrics("test");
49
50         metrics.track("test1", new CounterMeasurement(null, null, null));
51
52         MeasurementDescriptor descriptor = new MeasurementDescriptor();
53         descriptor.setShortName("test2");
54         descriptor.setClassFor(CounterMeasurement.class);
55
56         metrics.track(descriptor.createMeasurement());
57
58         assertNotNull(metrics.getMeasurement("test1"));
59         assertNotNull(metrics.getMeasurement("test2"));
60
61         assertEquals(NullMeasurement.class, metrics.getMeasurement("test").getClass());
62         assertEquals(0, metrics.getMeasurement("test1").intValue());
63         assertEquals(0, metrics.getMeasurement("test2").intValue());
64     }
65
66     public void testAddToMeasurement() {
67         Metrics metrics = new Metrics("test");
68
69         Measurement m0 = new CounterMeasurement(null, null, null);
70         Measurement m1 = new CounterMeasurement(null, null, null);
71         Measurement m2 = new CounterMeasurement(null, null, null);
72
73         m1.add(1);
74         m2.add(2.5);
75         
76         metrics.track("test0", m0);
77         metrics.track("test1", m1);
78         metrics.track("test2", m2);
79
80         assertEquals(NullMeasurement.class, metrics.getMeasurement("test").getClass());
81         assertEquals(0.0, metrics.getMeasurement("test0").doubleValue(), 0.01);
82         assertEquals(1.0, metrics.getMeasurement("test1").doubleValue(), 0.01);
83         assertEquals(2.5, metrics.getMeasurement("test2").doubleValue(), 0.01);
84
85         metrics.addToMeasurement("test", 1.0);
86         metrics.addToMeasurement("test0", 1.0);
87         metrics.addToMeasurement("test1", 1.0);
88         metrics.addToMeasurement("test2", 1.0);
89         
90         assertEquals(NullMeasurement.class, metrics.getMeasurement("test").getClass());
91         assertEquals(1.0, metrics.getMeasurement("test0").doubleValue(), 0.01);
92         assertEquals(2.0, metrics.getMeasurement("test1").doubleValue(), 0.01);
93         assertEquals(3.5, metrics.getMeasurement("test2").doubleValue(), 0.01);
94
95         metrics.addToMeasurement("test", new Double JavaDoc(1.0));
96         metrics.addToMeasurement("test0", new Double JavaDoc(1.0));
97         metrics.addToMeasurement("test1", new Double JavaDoc(1.0));
98         metrics.addToMeasurement("test2", new Double JavaDoc(1.0));
99         
100         assertEquals(NullMeasurement.class, metrics.getMeasurement("test").getClass());
101         assertEquals(2.0, metrics.getMeasurement("test0").doubleValue(), 0.01);
102         assertEquals(3.0, metrics.getMeasurement("test1").doubleValue(), 0.01);
103         assertEquals(4.5, metrics.getMeasurement("test2").doubleValue(), 0.01);
104
105         metrics.addToMeasurement("test", 1);
106         metrics.addToMeasurement("test0", 1);
107         metrics.addToMeasurement("test1", 1);
108         metrics.addToMeasurement("test2", 1);
109         
110         assertEquals(NullMeasurement.class, metrics.getMeasurement("test").getClass());
111         assertEquals(3.0, metrics.getMeasurement("test0").doubleValue(), 0.01);
112         assertEquals(4.0, metrics.getMeasurement("test1").doubleValue(), 0.01);
113         assertEquals(5.5, metrics.getMeasurement("test2").doubleValue(), 0.01);
114     }
115
116     public void testAddSubMetrics() {
117         Metrics metrics = new Metrics("test");
118
119         metrics.addSubMetrics(new Metrics("a"));
120         metrics.addSubMetrics(new Metrics("b"));
121         
122         assertEquals(2, metrics.getSubMetrics().size());
123     }
124
125     public void testMetricsInSubMetrics() {
126         Metrics metrics = new Metrics("test");
127
128         Metrics a = new Metrics("test.a");
129         Metrics b = new Metrics("test.b");
130         
131         Metrics aA = new Metrics("test.a.A");
132         Metrics aB = new Metrics("test.a.B");
133         Metrics bA = new Metrics("test.b.A");
134         Metrics bB = new Metrics("test.b.B");
135
136         Metrics aAf = new Metrics("test.a.A.f");
137         Metrics aAg = new Metrics("test.a.A.g");
138         Metrics aBf = new Metrics("test.a.B.f");
139         Metrics aBg = new Metrics("test.a.B.g");
140         Metrics bAf = new Metrics("test.b.A.f");
141         Metrics bAg = new Metrics("test.b.A.g");
142         Metrics bBf = new Metrics("test.b.B.f");
143         Metrics bBg = new Metrics("test.b.B.g");
144
145         metrics.addSubMetrics(a);
146         metrics.addSubMetrics(b);
147
148         a.addSubMetrics(aA);
149         a.addSubMetrics(aB);
150         b.addSubMetrics(bA);
151         b.addSubMetrics(bB);
152         
153         aA.addSubMetrics(aAf);
154         aA.addSubMetrics(aAg);
155         aB.addSubMetrics(aBf);
156         aB.addSubMetrics(aBg);
157         bA.addSubMetrics(bAf);
158         bA.addSubMetrics(bAg);
159         bB.addSubMetrics(bBf);
160         bB.addSubMetrics(bBg);
161
162         aAf.track("0001", new CounterMeasurement(null, null, null));
163         aAf.track("0011", new CounterMeasurement(null, null, null));
164         aAf.track("0101", new CounterMeasurement(null, null, null));
165         aAf.track("0111", new CounterMeasurement(null, null, null));
166         aAg.track("0001", new CounterMeasurement(null, null, null));
167         aAg.track("0011", new CounterMeasurement(null, null, null));
168         aAg.track("0101", new CounterMeasurement(null, null, null));
169         aAg.track("0111", new CounterMeasurement(null, null, null));
170         aBf.track("0001", new CounterMeasurement(null, null, null));
171         aBf.track("0011", new CounterMeasurement(null, null, null));
172         aBf.track("0101", new CounterMeasurement(null, null, null));
173         aBf.track("0111", new CounterMeasurement(null, null, null));
174         aBg.track("0001", new CounterMeasurement(null, null, null));
175         aBg.track("0011", new CounterMeasurement(null, null, null));
176         aBg.track("0101", new CounterMeasurement(null, null, null));
177         aBg.track("0111", new CounterMeasurement(null, null, null));
178         bAf.track("1001", new CounterMeasurement(null, null, null));
179         bAf.track("1011", new CounterMeasurement(null, null, null));
180         bAf.track("1101", new CounterMeasurement(null, null, null));
181         bAf.track("1111", new CounterMeasurement(null, null, null));
182         bAg.track("1001", new CounterMeasurement(null, null, null));
183         bAg.track("1011", new CounterMeasurement(null, null, null));
184         bAg.track("1101", new CounterMeasurement(null, null, null));
185         bAg.track("1111", new CounterMeasurement(null, null, null));
186         bBf.track("1001", new CounterMeasurement(null, null, null));
187         bBf.track("1011", new CounterMeasurement(null, null, null));
188         bBf.track("1101", new CounterMeasurement(null, null, null));
189         bBf.track("1111", new CounterMeasurement(null, null, null));
190         bBg.track("1001", new CounterMeasurement(null, null, null));
191         bBg.track("1011", new CounterMeasurement(null, null, null));
192         bBg.track("1101", new CounterMeasurement(null, null, null));
193         bBg.track("1111", new CounterMeasurement(null, null, null));
194
195         aA.track("0011", new CounterMeasurement(null, null, null));
196         aA.track("0010", new CounterMeasurement(null, null, null));
197         aA.track("0111", new CounterMeasurement(null, null, null));
198         aA.track("0110", new CounterMeasurement(null, null, null));
199         aB.track("0011", new CounterMeasurement(null, null, null));
200         aB.track("0010", new CounterMeasurement(null, null, null));
201         aB.track("0111", new CounterMeasurement(null, null, null));
202         aB.track("0110", new CounterMeasurement(null, null, null));
203         bA.track("1011", new CounterMeasurement(null, null, null));
204         bA.track("1010", new CounterMeasurement(null, null, null));
205         bA.track("1111", new CounterMeasurement(null, null, null));
206         bA.track("1110", new CounterMeasurement(null, null, null));
207         bB.track("1011", new CounterMeasurement(null, null, null));
208         bB.track("1010", new CounterMeasurement(null, null, null));
209         bB.track("1111", new CounterMeasurement(null, null, null));
210         bB.track("1110", new CounterMeasurement(null, null, null));
211
212         aA.addToMeasurement("0011", 1);
213         aA.addToMeasurement("0010", 1);
214         aA.addToMeasurement("0111", 1);
215         aA.addToMeasurement("0110", 1);
216         aB.addToMeasurement("0011", 1);
217         aB.addToMeasurement("0010", 1);
218         aB.addToMeasurement("0111", 1);
219         aB.addToMeasurement("0110", 1);
220         bA.addToMeasurement("1011", 1);
221         bA.addToMeasurement("1010", 1);
222         bA.addToMeasurement("1111", 1);
223         bA.addToMeasurement("1110", 1);
224         bB.addToMeasurement("1011", 1);
225         bB.addToMeasurement("1010", 1);
226         bB.addToMeasurement("1111", 1);
227         bB.addToMeasurement("1110", 1);
228
229         a.track("0100", new CounterMeasurement(null, null, null));
230         a.track("0101", new CounterMeasurement(null, null, null));
231         a.track("0110", new CounterMeasurement(null, null, null));
232         a.track("0111", new CounterMeasurement(null, null, null));
233         b.track("1100", new CounterMeasurement(null, null, null));
234         b.track("1101", new CounterMeasurement(null, null, null));
235         b.track("1110", new CounterMeasurement(null, null, null));
236         b.track("1111", new CounterMeasurement(null, null, null));
237
238         a.addToMeasurement("0100", 2);
239         a.addToMeasurement("0101", 2);
240         a.addToMeasurement("0110", 2);
241         a.addToMeasurement("0111", 2);
242         b.addToMeasurement("1100", 2);
243         b.addToMeasurement("1101", 2);
244         b.addToMeasurement("1110", 2);
245         b.addToMeasurement("1111", 2);
246
247         metrics.track("1100", new CounterMeasurement(null, null, null));
248         metrics.track("1101", new CounterMeasurement(null, null, null));
249         metrics.track("1110", new CounterMeasurement(null, null, null));
250         metrics.track("1111", new CounterMeasurement(null, null, null));
251
252         metrics.addToMeasurement("1100", 3);
253         metrics.addToMeasurement("1101", 3);
254         metrics.addToMeasurement("1110", 3);
255         metrics.addToMeasurement("1111", 3);
256
257         assertEquals(2, metrics.getSubMetrics().size());
258         assertEquals(NullMeasurement.class, metrics.getMeasurement("0000").getClass());
259         assertEquals(NullMeasurement.class, metrics.getMeasurement("0001").getClass());
260         assertEquals(NullMeasurement.class, metrics.getMeasurement("0010").getClass());
261         assertEquals(NullMeasurement.class, metrics.getMeasurement("0011").getClass());
262         assertEquals(NullMeasurement.class, metrics.getMeasurement("0100").getClass());
263         assertEquals(NullMeasurement.class, metrics.getMeasurement("0101").getClass());
264         assertEquals(NullMeasurement.class, metrics.getMeasurement("0110").getClass());
265         assertEquals(NullMeasurement.class, metrics.getMeasurement("0111").getClass());
266         assertEquals(NullMeasurement.class, metrics.getMeasurement("1000").getClass());
267         assertEquals(NullMeasurement.class, metrics.getMeasurement("1001").getClass());
268         assertEquals(NullMeasurement.class, metrics.getMeasurement("1010").getClass());
269         assertEquals(NullMeasurement.class, metrics.getMeasurement("1011").getClass());
270         assertEquals(3.0, metrics.getMeasurement("1100").doubleValue(), 0.01);
271         assertEquals(3.0, metrics.getMeasurement("1101").doubleValue(), 0.01);
272         assertEquals(3.0, metrics.getMeasurement("1110").doubleValue(), 0.01);
273         assertEquals(3.0, metrics.getMeasurement("1111").doubleValue(), 0.01);
274     }
275
276     public void testInRange() throws Exception JavaDoc {
277         Metrics metrics = new Metrics("test");
278
279         assertTrue(metrics.isInRange());
280
281         MeasurementDescriptor descriptor1 = new MeasurementDescriptor();
282         descriptor1.setShortName("foo");
283         descriptor1.setLongName("foo");
284         descriptor1.setClassFor(CounterMeasurement.class);
285         descriptor1.setUpperThreshold(new Integer JavaDoc(1));
286
287         metrics.track(descriptor1.createMeasurement(metrics));
288
289         MeasurementDescriptor descriptor2 = new MeasurementDescriptor();
290         descriptor2.setShortName("bar");
291         descriptor2.setLongName("bar");
292         descriptor2.setClassFor(CounterMeasurement.class);
293
294         metrics.track(descriptor2.createMeasurement(metrics));
295
296         assertTrue(metrics.isInRange());
297
298         metrics.addToMeasurement("foo", 2);
299
300         assertFalse(metrics.isInRange());
301     }
302
303     public void testEmptyWithOneNonEmptyMeasurement() throws Exception JavaDoc {
304         Metrics metrics = new Metrics("test");
305
306         assertTrue("Before Track(foo)", metrics.isEmpty());
307
308         MeasurementDescriptor descriptor1 = new MeasurementDescriptor();
309         descriptor1.setShortName("foo");
310         descriptor1.setLongName("foo");
311         descriptor1.setClassFor(CounterMeasurement.class);
312
313         metrics.track(descriptor1.createMeasurement(metrics));
314
315         assertTrue("After Track(foo)", metrics.isEmpty());
316         
317         MeasurementDescriptor descriptor2 = new MeasurementDescriptor();
318         descriptor2.setShortName("bar");
319         descriptor2.setLongName("bar");
320         descriptor2.setClassFor(CounterMeasurement.class);
321
322         metrics.track(descriptor2.createMeasurement(metrics));
323
324         assertTrue("After Track(bar)", metrics.isEmpty());
325         
326         metrics.addToMeasurement("foo", 2);
327
328         assertFalse("After Add()", metrics.isEmpty());
329     }
330
331     public void testEmptyWithOtherNonEmptyMeasurement() throws Exception JavaDoc {
332         Metrics metrics = new Metrics("test");
333
334         assertTrue("Before Track(foo)", metrics.isEmpty());
335
336         MeasurementDescriptor descriptor1 = new MeasurementDescriptor();
337         descriptor1.setShortName("foo");
338         descriptor1.setLongName("foo");
339         descriptor1.setClassFor(CounterMeasurement.class);
340
341         metrics.track(descriptor1.createMeasurement(metrics));
342
343         assertTrue("After Track(foo)", metrics.isEmpty());
344         
345         MeasurementDescriptor descriptor2 = new MeasurementDescriptor();
346         descriptor2.setShortName("bar");
347         descriptor2.setLongName("bar");
348         descriptor2.setClassFor(CounterMeasurement.class);
349
350         metrics.track(descriptor2.createMeasurement(metrics));
351
352         assertTrue("After Track(bar)", metrics.isEmpty());
353         
354         metrics.addToMeasurement("bar", 2);
355
356         assertFalse("After Add()", metrics.isEmpty());
357     }
358
359     public void testEmptyWithOneNonVisibleNonEmptyMeasurement() throws Exception JavaDoc {
360         Metrics metrics = new Metrics("test");
361
362         assertTrue("Before Track(foo)", metrics.isEmpty());
363
364         MeasurementDescriptor descriptor1 = new MeasurementDescriptor();
365         descriptor1.setShortName("foo");
366         descriptor1.setLongName("foo");
367         descriptor1.setClassFor(CounterMeasurement.class);
368         descriptor1.setVisible(false);
369
370         metrics.track(descriptor1.createMeasurement(metrics));
371
372         assertTrue("After Track(foo)", metrics.isEmpty());
373         
374         MeasurementDescriptor descriptor2 = new MeasurementDescriptor();
375         descriptor2.setShortName("bar");
376         descriptor2.setLongName("bar");
377         descriptor2.setClassFor(CounterMeasurement.class);
378
379         metrics.track(descriptor2.createMeasurement(metrics));
380
381         assertTrue("After Track(bar)", metrics.isEmpty());
382         
383         metrics.addToMeasurement("foo", 2);
384
385         assertTrue("After Add()", metrics.isEmpty());
386     }
387
388     public void testEmptyWithOneNonEmptySubMetrics() throws Exception JavaDoc {
389         Metrics metrics = new Metrics("test");
390         Metrics submetrics1 = new Metrics("submetrics1");
391         Metrics submetrics2 = new Metrics("submetrics2");
392
393         metrics.addSubMetrics(submetrics1);
394         metrics.addSubMetrics(submetrics2);
395         
396         MeasurementDescriptor descriptor = new MeasurementDescriptor();
397         descriptor.setShortName("foo");
398         descriptor.setLongName("foo");
399         descriptor.setClassFor(CounterMeasurement.class);
400
401         submetrics1.track(descriptor.createMeasurement(submetrics1));
402         submetrics2.track(descriptor.createMeasurement(submetrics2));
403
404         assertTrue("Before Add() to submetrics1", submetrics1.isEmpty());
405         assertTrue("Before Add() to submetrics1", submetrics2.isEmpty());
406         assertTrue("Before Add() to submetrics1", metrics.isEmpty());
407         
408         submetrics1.addToMeasurement("foo", 2);
409
410         assertFalse("After Add() to submetrics1", submetrics1.isEmpty());
411         assertTrue("After Add() to submetrics1", submetrics2.isEmpty());
412         assertFalse("After Add() to submetrics1", metrics.isEmpty());
413     }
414
415     public void testEmptyWithOtherNonEmptySubMetrics() throws Exception JavaDoc {
416         Metrics metrics = new Metrics("test");
417         Metrics submetrics1 = new Metrics("submetrics1");
418         Metrics submetrics2 = new Metrics("submetrics2");
419
420         metrics.addSubMetrics(submetrics1);
421         metrics.addSubMetrics(submetrics2);
422         
423         MeasurementDescriptor descriptor = new MeasurementDescriptor();
424         descriptor.setShortName("foo");
425         descriptor.setLongName("foo");
426         descriptor.setClassFor(CounterMeasurement.class);
427
428         submetrics1.track(descriptor.createMeasurement(submetrics1));
429         submetrics2.track(descriptor.createMeasurement(submetrics2));
430
431         assertTrue("Before Add() to submetrics1", submetrics1.isEmpty());
432         assertTrue("Before Add() to submetrics1", submetrics2.isEmpty());
433         assertTrue("Before Add() to submetrics2", metrics.isEmpty());
434         
435         submetrics2.addToMeasurement("foo", 2);
436
437         assertTrue("After Add() to submetrics1", submetrics1.isEmpty());
438         assertFalse("After Add() to submetrics1", submetrics2.isEmpty());
439         assertFalse("After Add() to submetrics2", metrics.isEmpty());
440     }
441 }
442
Popular Tags