001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.metrics;
034:
035: import junit.framework.*;
036:
037: public class TestMetrics extends TestCase {
038: public void testCreate() {
039: Metrics metrics = new Metrics("test");
040:
041: assertEquals("test", metrics.getName());
042: assertNotNull(metrics.getMeasurement("test"));
043: assertEquals(NullMeasurement.class, metrics.getMeasurement(
044: "test").getClass());
045: assertTrue(metrics.getSubMetrics().isEmpty());
046: }
047:
048: public void testTrack() throws Exception {
049: Metrics metrics = new Metrics("test");
050:
051: metrics
052: .track("test1",
053: new CounterMeasurement(null, null, null));
054:
055: MeasurementDescriptor descriptor = new MeasurementDescriptor();
056: descriptor.setShortName("test2");
057: descriptor.setClassFor(CounterMeasurement.class);
058:
059: metrics.track(descriptor.createMeasurement());
060:
061: assertNotNull(metrics.getMeasurement("test1"));
062: assertNotNull(metrics.getMeasurement("test2"));
063:
064: assertEquals(NullMeasurement.class, metrics.getMeasurement(
065: "test").getClass());
066: assertEquals(0, metrics.getMeasurement("test1").intValue());
067: assertEquals(0, metrics.getMeasurement("test2").intValue());
068: }
069:
070: public void testAddToMeasurement() {
071: Metrics metrics = new Metrics("test");
072:
073: Measurement m0 = new CounterMeasurement(null, null, null);
074: Measurement m1 = new CounterMeasurement(null, null, null);
075: Measurement m2 = new CounterMeasurement(null, null, null);
076:
077: m1.add(1);
078: m2.add(2.5);
079:
080: metrics.track("test0", m0);
081: metrics.track("test1", m1);
082: metrics.track("test2", m2);
083:
084: assertEquals(NullMeasurement.class, metrics.getMeasurement(
085: "test").getClass());
086: assertEquals(0.0,
087: metrics.getMeasurement("test0").doubleValue(), 0.01);
088: assertEquals(1.0,
089: metrics.getMeasurement("test1").doubleValue(), 0.01);
090: assertEquals(2.5,
091: metrics.getMeasurement("test2").doubleValue(), 0.01);
092:
093: metrics.addToMeasurement("test", 1.0);
094: metrics.addToMeasurement("test0", 1.0);
095: metrics.addToMeasurement("test1", 1.0);
096: metrics.addToMeasurement("test2", 1.0);
097:
098: assertEquals(NullMeasurement.class, metrics.getMeasurement(
099: "test").getClass());
100: assertEquals(1.0,
101: metrics.getMeasurement("test0").doubleValue(), 0.01);
102: assertEquals(2.0,
103: metrics.getMeasurement("test1").doubleValue(), 0.01);
104: assertEquals(3.5,
105: metrics.getMeasurement("test2").doubleValue(), 0.01);
106:
107: metrics.addToMeasurement("test", new Double(1.0));
108: metrics.addToMeasurement("test0", new Double(1.0));
109: metrics.addToMeasurement("test1", new Double(1.0));
110: metrics.addToMeasurement("test2", new Double(1.0));
111:
112: assertEquals(NullMeasurement.class, metrics.getMeasurement(
113: "test").getClass());
114: assertEquals(2.0,
115: metrics.getMeasurement("test0").doubleValue(), 0.01);
116: assertEquals(3.0,
117: metrics.getMeasurement("test1").doubleValue(), 0.01);
118: assertEquals(4.5,
119: metrics.getMeasurement("test2").doubleValue(), 0.01);
120:
121: metrics.addToMeasurement("test", 1);
122: metrics.addToMeasurement("test0", 1);
123: metrics.addToMeasurement("test1", 1);
124: metrics.addToMeasurement("test2", 1);
125:
126: assertEquals(NullMeasurement.class, metrics.getMeasurement(
127: "test").getClass());
128: assertEquals(3.0,
129: metrics.getMeasurement("test0").doubleValue(), 0.01);
130: assertEquals(4.0,
131: metrics.getMeasurement("test1").doubleValue(), 0.01);
132: assertEquals(5.5,
133: metrics.getMeasurement("test2").doubleValue(), 0.01);
134: }
135:
136: public void testAddSubMetrics() {
137: Metrics metrics = new Metrics("test");
138:
139: metrics.addSubMetrics(new Metrics("a"));
140: metrics.addSubMetrics(new Metrics("b"));
141:
142: assertEquals(2, metrics.getSubMetrics().size());
143: }
144:
145: public void testMetricsInSubMetrics() {
146: Metrics metrics = new Metrics("test");
147:
148: Metrics a = new Metrics("test.a");
149: Metrics b = new Metrics("test.b");
150:
151: Metrics aA = new Metrics("test.a.A");
152: Metrics aB = new Metrics("test.a.B");
153: Metrics bA = new Metrics("test.b.A");
154: Metrics bB = new Metrics("test.b.B");
155:
156: Metrics aAf = new Metrics("test.a.A.f");
157: Metrics aAg = new Metrics("test.a.A.g");
158: Metrics aBf = new Metrics("test.a.B.f");
159: Metrics aBg = new Metrics("test.a.B.g");
160: Metrics bAf = new Metrics("test.b.A.f");
161: Metrics bAg = new Metrics("test.b.A.g");
162: Metrics bBf = new Metrics("test.b.B.f");
163: Metrics bBg = new Metrics("test.b.B.g");
164:
165: metrics.addSubMetrics(a);
166: metrics.addSubMetrics(b);
167:
168: a.addSubMetrics(aA);
169: a.addSubMetrics(aB);
170: b.addSubMetrics(bA);
171: b.addSubMetrics(bB);
172:
173: aA.addSubMetrics(aAf);
174: aA.addSubMetrics(aAg);
175: aB.addSubMetrics(aBf);
176: aB.addSubMetrics(aBg);
177: bA.addSubMetrics(bAf);
178: bA.addSubMetrics(bAg);
179: bB.addSubMetrics(bBf);
180: bB.addSubMetrics(bBg);
181:
182: aAf.track("0001", new CounterMeasurement(null, null, null));
183: aAf.track("0011", new CounterMeasurement(null, null, null));
184: aAf.track("0101", new CounterMeasurement(null, null, null));
185: aAf.track("0111", new CounterMeasurement(null, null, null));
186: aAg.track("0001", new CounterMeasurement(null, null, null));
187: aAg.track("0011", new CounterMeasurement(null, null, null));
188: aAg.track("0101", new CounterMeasurement(null, null, null));
189: aAg.track("0111", new CounterMeasurement(null, null, null));
190: aBf.track("0001", new CounterMeasurement(null, null, null));
191: aBf.track("0011", new CounterMeasurement(null, null, null));
192: aBf.track("0101", new CounterMeasurement(null, null, null));
193: aBf.track("0111", new CounterMeasurement(null, null, null));
194: aBg.track("0001", new CounterMeasurement(null, null, null));
195: aBg.track("0011", new CounterMeasurement(null, null, null));
196: aBg.track("0101", new CounterMeasurement(null, null, null));
197: aBg.track("0111", new CounterMeasurement(null, null, null));
198: bAf.track("1001", new CounterMeasurement(null, null, null));
199: bAf.track("1011", new CounterMeasurement(null, null, null));
200: bAf.track("1101", new CounterMeasurement(null, null, null));
201: bAf.track("1111", new CounterMeasurement(null, null, null));
202: bAg.track("1001", new CounterMeasurement(null, null, null));
203: bAg.track("1011", new CounterMeasurement(null, null, null));
204: bAg.track("1101", new CounterMeasurement(null, null, null));
205: bAg.track("1111", new CounterMeasurement(null, null, null));
206: bBf.track("1001", new CounterMeasurement(null, null, null));
207: bBf.track("1011", new CounterMeasurement(null, null, null));
208: bBf.track("1101", new CounterMeasurement(null, null, null));
209: bBf.track("1111", new CounterMeasurement(null, null, null));
210: bBg.track("1001", new CounterMeasurement(null, null, null));
211: bBg.track("1011", new CounterMeasurement(null, null, null));
212: bBg.track("1101", new CounterMeasurement(null, null, null));
213: bBg.track("1111", new CounterMeasurement(null, null, null));
214:
215: aA.track("0011", new CounterMeasurement(null, null, null));
216: aA.track("0010", new CounterMeasurement(null, null, null));
217: aA.track("0111", new CounterMeasurement(null, null, null));
218: aA.track("0110", new CounterMeasurement(null, null, null));
219: aB.track("0011", new CounterMeasurement(null, null, null));
220: aB.track("0010", new CounterMeasurement(null, null, null));
221: aB.track("0111", new CounterMeasurement(null, null, null));
222: aB.track("0110", new CounterMeasurement(null, null, null));
223: bA.track("1011", new CounterMeasurement(null, null, null));
224: bA.track("1010", new CounterMeasurement(null, null, null));
225: bA.track("1111", new CounterMeasurement(null, null, null));
226: bA.track("1110", new CounterMeasurement(null, null, null));
227: bB.track("1011", new CounterMeasurement(null, null, null));
228: bB.track("1010", new CounterMeasurement(null, null, null));
229: bB.track("1111", new CounterMeasurement(null, null, null));
230: bB.track("1110", new CounterMeasurement(null, null, null));
231:
232: aA.addToMeasurement("0011", 1);
233: aA.addToMeasurement("0010", 1);
234: aA.addToMeasurement("0111", 1);
235: aA.addToMeasurement("0110", 1);
236: aB.addToMeasurement("0011", 1);
237: aB.addToMeasurement("0010", 1);
238: aB.addToMeasurement("0111", 1);
239: aB.addToMeasurement("0110", 1);
240: bA.addToMeasurement("1011", 1);
241: bA.addToMeasurement("1010", 1);
242: bA.addToMeasurement("1111", 1);
243: bA.addToMeasurement("1110", 1);
244: bB.addToMeasurement("1011", 1);
245: bB.addToMeasurement("1010", 1);
246: bB.addToMeasurement("1111", 1);
247: bB.addToMeasurement("1110", 1);
248:
249: a.track("0100", new CounterMeasurement(null, null, null));
250: a.track("0101", new CounterMeasurement(null, null, null));
251: a.track("0110", new CounterMeasurement(null, null, null));
252: a.track("0111", new CounterMeasurement(null, null, null));
253: b.track("1100", new CounterMeasurement(null, null, null));
254: b.track("1101", new CounterMeasurement(null, null, null));
255: b.track("1110", new CounterMeasurement(null, null, null));
256: b.track("1111", new CounterMeasurement(null, null, null));
257:
258: a.addToMeasurement("0100", 2);
259: a.addToMeasurement("0101", 2);
260: a.addToMeasurement("0110", 2);
261: a.addToMeasurement("0111", 2);
262: b.addToMeasurement("1100", 2);
263: b.addToMeasurement("1101", 2);
264: b.addToMeasurement("1110", 2);
265: b.addToMeasurement("1111", 2);
266:
267: metrics.track("1100", new CounterMeasurement(null, null, null));
268: metrics.track("1101", new CounterMeasurement(null, null, null));
269: metrics.track("1110", new CounterMeasurement(null, null, null));
270: metrics.track("1111", new CounterMeasurement(null, null, null));
271:
272: metrics.addToMeasurement("1100", 3);
273: metrics.addToMeasurement("1101", 3);
274: metrics.addToMeasurement("1110", 3);
275: metrics.addToMeasurement("1111", 3);
276:
277: assertEquals(2, metrics.getSubMetrics().size());
278: assertEquals(NullMeasurement.class, metrics.getMeasurement(
279: "0000").getClass());
280: assertEquals(NullMeasurement.class, metrics.getMeasurement(
281: "0001").getClass());
282: assertEquals(NullMeasurement.class, metrics.getMeasurement(
283: "0010").getClass());
284: assertEquals(NullMeasurement.class, metrics.getMeasurement(
285: "0011").getClass());
286: assertEquals(NullMeasurement.class, metrics.getMeasurement(
287: "0100").getClass());
288: assertEquals(NullMeasurement.class, metrics.getMeasurement(
289: "0101").getClass());
290: assertEquals(NullMeasurement.class, metrics.getMeasurement(
291: "0110").getClass());
292: assertEquals(NullMeasurement.class, metrics.getMeasurement(
293: "0111").getClass());
294: assertEquals(NullMeasurement.class, metrics.getMeasurement(
295: "1000").getClass());
296: assertEquals(NullMeasurement.class, metrics.getMeasurement(
297: "1001").getClass());
298: assertEquals(NullMeasurement.class, metrics.getMeasurement(
299: "1010").getClass());
300: assertEquals(NullMeasurement.class, metrics.getMeasurement(
301: "1011").getClass());
302: assertEquals(3.0, metrics.getMeasurement("1100").doubleValue(),
303: 0.01);
304: assertEquals(3.0, metrics.getMeasurement("1101").doubleValue(),
305: 0.01);
306: assertEquals(3.0, metrics.getMeasurement("1110").doubleValue(),
307: 0.01);
308: assertEquals(3.0, metrics.getMeasurement("1111").doubleValue(),
309: 0.01);
310: }
311:
312: public void testInRange() throws Exception {
313: Metrics metrics = new Metrics("test");
314:
315: assertTrue(metrics.isInRange());
316:
317: MeasurementDescriptor descriptor1 = new MeasurementDescriptor();
318: descriptor1.setShortName("foo");
319: descriptor1.setLongName("foo");
320: descriptor1.setClassFor(CounterMeasurement.class);
321: descriptor1.setUpperThreshold(new Integer(1));
322:
323: metrics.track(descriptor1.createMeasurement(metrics));
324:
325: MeasurementDescriptor descriptor2 = new MeasurementDescriptor();
326: descriptor2.setShortName("bar");
327: descriptor2.setLongName("bar");
328: descriptor2.setClassFor(CounterMeasurement.class);
329:
330: metrics.track(descriptor2.createMeasurement(metrics));
331:
332: assertTrue(metrics.isInRange());
333:
334: metrics.addToMeasurement("foo", 2);
335:
336: assertFalse(metrics.isInRange());
337: }
338:
339: public void testEmptyWithOneNonEmptyMeasurement() throws Exception {
340: Metrics metrics = new Metrics("test");
341:
342: assertTrue("Before Track(foo)", metrics.isEmpty());
343:
344: MeasurementDescriptor descriptor1 = new MeasurementDescriptor();
345: descriptor1.setShortName("foo");
346: descriptor1.setLongName("foo");
347: descriptor1.setClassFor(CounterMeasurement.class);
348:
349: metrics.track(descriptor1.createMeasurement(metrics));
350:
351: assertTrue("After Track(foo)", metrics.isEmpty());
352:
353: MeasurementDescriptor descriptor2 = new MeasurementDescriptor();
354: descriptor2.setShortName("bar");
355: descriptor2.setLongName("bar");
356: descriptor2.setClassFor(CounterMeasurement.class);
357:
358: metrics.track(descriptor2.createMeasurement(metrics));
359:
360: assertTrue("After Track(bar)", metrics.isEmpty());
361:
362: metrics.addToMeasurement("foo", 2);
363:
364: assertFalse("After Add()", metrics.isEmpty());
365: }
366:
367: public void testEmptyWithOtherNonEmptyMeasurement()
368: throws Exception {
369: Metrics metrics = new Metrics("test");
370:
371: assertTrue("Before Track(foo)", metrics.isEmpty());
372:
373: MeasurementDescriptor descriptor1 = new MeasurementDescriptor();
374: descriptor1.setShortName("foo");
375: descriptor1.setLongName("foo");
376: descriptor1.setClassFor(CounterMeasurement.class);
377:
378: metrics.track(descriptor1.createMeasurement(metrics));
379:
380: assertTrue("After Track(foo)", metrics.isEmpty());
381:
382: MeasurementDescriptor descriptor2 = new MeasurementDescriptor();
383: descriptor2.setShortName("bar");
384: descriptor2.setLongName("bar");
385: descriptor2.setClassFor(CounterMeasurement.class);
386:
387: metrics.track(descriptor2.createMeasurement(metrics));
388:
389: assertTrue("After Track(bar)", metrics.isEmpty());
390:
391: metrics.addToMeasurement("bar", 2);
392:
393: assertFalse("After Add()", metrics.isEmpty());
394: }
395:
396: public void testEmptyWithOneNonVisibleNonEmptyMeasurement()
397: throws Exception {
398: Metrics metrics = new Metrics("test");
399:
400: assertTrue("Before Track(foo)", metrics.isEmpty());
401:
402: MeasurementDescriptor descriptor1 = new MeasurementDescriptor();
403: descriptor1.setShortName("foo");
404: descriptor1.setLongName("foo");
405: descriptor1.setClassFor(CounterMeasurement.class);
406: descriptor1.setVisible(false);
407:
408: metrics.track(descriptor1.createMeasurement(metrics));
409:
410: assertTrue("After Track(foo)", metrics.isEmpty());
411:
412: MeasurementDescriptor descriptor2 = new MeasurementDescriptor();
413: descriptor2.setShortName("bar");
414: descriptor2.setLongName("bar");
415: descriptor2.setClassFor(CounterMeasurement.class);
416:
417: metrics.track(descriptor2.createMeasurement(metrics));
418:
419: assertTrue("After Track(bar)", metrics.isEmpty());
420:
421: metrics.addToMeasurement("foo", 2);
422:
423: assertTrue("After Add()", metrics.isEmpty());
424: }
425:
426: public void testEmptyWithOneNonEmptySubMetrics() throws Exception {
427: Metrics metrics = new Metrics("test");
428: Metrics submetrics1 = new Metrics("submetrics1");
429: Metrics submetrics2 = new Metrics("submetrics2");
430:
431: metrics.addSubMetrics(submetrics1);
432: metrics.addSubMetrics(submetrics2);
433:
434: MeasurementDescriptor descriptor = new MeasurementDescriptor();
435: descriptor.setShortName("foo");
436: descriptor.setLongName("foo");
437: descriptor.setClassFor(CounterMeasurement.class);
438:
439: submetrics1.track(descriptor.createMeasurement(submetrics1));
440: submetrics2.track(descriptor.createMeasurement(submetrics2));
441:
442: assertTrue("Before Add() to submetrics1", submetrics1.isEmpty());
443: assertTrue("Before Add() to submetrics1", submetrics2.isEmpty());
444: assertTrue("Before Add() to submetrics1", metrics.isEmpty());
445:
446: submetrics1.addToMeasurement("foo", 2);
447:
448: assertFalse("After Add() to submetrics1", submetrics1.isEmpty());
449: assertTrue("After Add() to submetrics1", submetrics2.isEmpty());
450: assertFalse("After Add() to submetrics1", metrics.isEmpty());
451: }
452:
453: public void testEmptyWithOtherNonEmptySubMetrics() throws Exception {
454: Metrics metrics = new Metrics("test");
455: Metrics submetrics1 = new Metrics("submetrics1");
456: Metrics submetrics2 = new Metrics("submetrics2");
457:
458: metrics.addSubMetrics(submetrics1);
459: metrics.addSubMetrics(submetrics2);
460:
461: MeasurementDescriptor descriptor = new MeasurementDescriptor();
462: descriptor.setShortName("foo");
463: descriptor.setLongName("foo");
464: descriptor.setClassFor(CounterMeasurement.class);
465:
466: submetrics1.track(descriptor.createMeasurement(submetrics1));
467: submetrics2.track(descriptor.createMeasurement(submetrics2));
468:
469: assertTrue("Before Add() to submetrics1", submetrics1.isEmpty());
470: assertTrue("Before Add() to submetrics1", submetrics2.isEmpty());
471: assertTrue("Before Add() to submetrics2", metrics.isEmpty());
472:
473: submetrics2.addToMeasurement("foo", 2);
474:
475: assertTrue("After Add() to submetrics1", submetrics1.isEmpty());
476: assertFalse("After Add() to submetrics1", submetrics2.isEmpty());
477: assertFalse("After Add() to submetrics2", metrics.isEmpty());
478: }
479: }
|