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 TestRatioMeasurement extends TestCase implements
038: MeasurementVisitor {
039: private MeasurementDescriptor descriptor;
040: private RatioMeasurement measurement;
041: private Metrics metrics;
042: private Measurement visited;
043:
044: private Measurement m1;
045: private Measurement m2;
046:
047: protected void setUp() throws Exception {
048: metrics = new Metrics("foobar");
049:
050: m1 = new CounterMeasurement(null, metrics, null);
051: m2 = new CounterMeasurement(null, metrics, null);
052:
053: metrics.track("base", m1);
054: metrics.track("divider", m2);
055:
056: descriptor = new MeasurementDescriptor();
057: descriptor.setShortName("foo");
058: descriptor.setLongName("bar");
059: descriptor.setClassFor(RatioMeasurement.class);
060: descriptor.setInitText("base\ndivider");
061: descriptor.setCached(false);
062: }
063:
064: public void testMeasurementDescriptor() {
065: measurement = new RatioMeasurement(descriptor, metrics,
066: "base\ndivider");
067:
068: assertNotNull(measurement.getDescriptor());
069: assertEquals(RatioMeasurement.class, measurement
070: .getDescriptor().getClassFor());
071: assertEquals("foo", measurement.getShortName());
072: assertEquals("bar", measurement.getLongName());
073: }
074:
075: public void testCreateFromMeasurementDescriptor() throws Exception {
076: descriptor.setInitText(null);
077:
078: measurement = (RatioMeasurement) descriptor.createMeasurement();
079:
080: assertNotNull(measurement);
081: assertEquals(descriptor, measurement.getDescriptor());
082: assertSame(descriptor, measurement.getDescriptor());
083: assertEquals(RatioMeasurement.class, measurement.getClass());
084: assertEquals("foo", measurement.getShortName());
085: assertEquals("bar", measurement.getLongName());
086:
087: assertNull(measurement.getBaseName());
088: assertNull(measurement.getDividerName());
089: assertTrue(Double.isNaN(measurement.doubleValue()));
090: }
091:
092: public void testCreateAndInitFromMeasurementDescriptor()
093: throws Exception {
094: measurement = (RatioMeasurement) descriptor.createMeasurement();
095:
096: assertNotNull(measurement);
097: assertEquals(descriptor, measurement.getDescriptor());
098: assertSame(descriptor, measurement.getDescriptor());
099: assertEquals(RatioMeasurement.class, measurement.getClass());
100: assertEquals("foo", measurement.getShortName());
101: assertEquals("bar", measurement.getLongName());
102: assertEquals("base", measurement.getBaseName());
103: assertEquals("divider", measurement.getDividerName());
104:
105: assertTrue(Double.isNaN(measurement.doubleValue()));
106: }
107:
108: public void testCreate() {
109: measurement = new RatioMeasurement(null, null, null);
110:
111: assertNull(measurement.getBaseName());
112: assertEquals(StatisticalMeasurement.DISPOSE_IGNORE, measurement
113: .getBaseDispose());
114: assertNull(measurement.getDividerName());
115: assertEquals(StatisticalMeasurement.DISPOSE_IGNORE, measurement
116: .getDividerDispose());
117:
118: measurement = new RatioMeasurement(null, null, "base\ndivider");
119:
120: assertEquals("base", measurement.getBaseName());
121: assertEquals(StatisticalMeasurement.DISPOSE_IGNORE, measurement
122: .getBaseDispose());
123: assertEquals("divider", measurement.getDividerName());
124: assertEquals(StatisticalMeasurement.DISPOSE_IGNORE, measurement
125: .getDividerDispose());
126:
127: measurement = new RatioMeasurement(null, null, "base");
128:
129: assertNull(measurement.getBaseName());
130: assertEquals(StatisticalMeasurement.DISPOSE_IGNORE, measurement
131: .getBaseDispose());
132: assertNull(measurement.getDividerName());
133: assertEquals(StatisticalMeasurement.DISPOSE_IGNORE, measurement
134: .getDividerDispose());
135:
136: measurement = new RatioMeasurement(null, null, null);
137:
138: assertNull(measurement.getBaseName());
139: assertEquals(StatisticalMeasurement.DISPOSE_IGNORE, measurement
140: .getBaseDispose());
141: assertNull(measurement.getDividerName());
142: assertEquals(StatisticalMeasurement.DISPOSE_IGNORE, measurement
143: .getDividerDispose());
144:
145: measurement = new RatioMeasurement(null, null, "foo\nbar");
146:
147: assertEquals("foo", measurement.getBaseName());
148: assertEquals(StatisticalMeasurement.DISPOSE_IGNORE, measurement
149: .getBaseDispose());
150: assertEquals("bar", measurement.getDividerName());
151: assertEquals(StatisticalMeasurement.DISPOSE_IGNORE, measurement
152: .getDividerDispose());
153:
154: measurement = new RatioMeasurement(null, null, "foo\nbar");
155:
156: assertEquals("foo", measurement.getBaseName());
157: assertEquals(StatisticalMeasurement.DISPOSE_IGNORE, measurement
158: .getBaseDispose());
159: assertEquals("bar", measurement.getDividerName());
160: assertEquals(StatisticalMeasurement.DISPOSE_IGNORE, measurement
161: .getDividerDispose());
162:
163: measurement = new RatioMeasurement(null, null,
164: "foo DISPOSE_MINIMUM\nbar DISPOSE_AVERAGE");
165:
166: assertEquals("foo", measurement.getBaseName());
167: assertEquals(StatisticalMeasurement.DISPOSE_MINIMUM,
168: measurement.getBaseDispose());
169: assertEquals("bar", measurement.getDividerName());
170: assertEquals(StatisticalMeasurement.DISPOSE_AVERAGE,
171: measurement.getDividerDispose());
172: }
173:
174: public void testStatistical() {
175: Metrics c = new Metrics("foobar");
176: Metrics m1 = new Metrics("foo");
177: Metrics m2 = new Metrics("bar");
178:
179: c.addSubMetrics(m1);
180: c.addSubMetrics(m2);
181:
182: m1.track("base", new CounterMeasurement(null, null, null));
183: m1.track("divider", new CounterMeasurement(null, null, null));
184: m2.track("base", new CounterMeasurement(null, null, null));
185: m2.track("divider", new CounterMeasurement(null, null, null));
186:
187: m1.addToMeasurement("base", 1);
188: m1.addToMeasurement("divider", 2);
189: m2.addToMeasurement("base", 3);
190: m2.addToMeasurement("divider", 4);
191:
192: c.track("base", new StatisticalMeasurement(null, c, "base"));
193: c.track("divider", new StatisticalMeasurement(null, c,
194: "divider"));
195:
196: measurement = new RatioMeasurement(descriptor, c,
197: "base DISPOSE_MINIMUM\ndivider DISPOSE_MINIMUM");
198: assertEquals(0.5, measurement.doubleValue(), 0.01);
199:
200: measurement = new RatioMeasurement(descriptor, c,
201: "base DISPOSE_AVERAGE\ndivider DISPOSE_AVERAGE");
202: assertEquals(2.0 / 3.0, measurement.doubleValue(), 0.01);
203:
204: measurement = new RatioMeasurement(descriptor, c,
205: "base DISPOSE_AVERAGE\ndivider DISPOSE_NB_DATA_POINTS");
206: assertEquals(1.0, measurement.doubleValue(), 0.01);
207: }
208:
209: public void testNormal() throws Exception {
210: measurement = (RatioMeasurement) descriptor
211: .createMeasurement(metrics);
212:
213: m1.add(10);
214: m2.add(1);
215:
216: assertEquals(10 / 1, measurement.intValue());
217: assertEquals(10 / 1, measurement.doubleValue(), 0.01);
218: assertEquals(10 / 1, measurement.getValue().intValue());
219:
220: m2.add(1);
221:
222: assertEquals(10 / 2, measurement.intValue());
223: assertEquals(10 / 2, measurement.doubleValue(), 0.01);
224: assertEquals(10 / 2, measurement.getValue().intValue());
225:
226: m1.add(m1.getValue());
227:
228: assertEquals(20 / 2, measurement.intValue());
229: assertEquals(20 / 2, measurement.doubleValue(), 0.01);
230: assertEquals(20 / 2, measurement.getValue().intValue());
231: }
232:
233: public void testDevideByZero() throws Exception {
234: measurement = (RatioMeasurement) descriptor
235: .createMeasurement(metrics);
236:
237: assertTrue("0/0 not NaN", Double.isNaN(measurement.getValue()
238: .doubleValue()));
239:
240: m1.add(1);
241:
242: assertTrue("1/0 not infitity", Double.isInfinite(measurement
243: .getValue().doubleValue()));
244:
245: m1.add(-2);
246:
247: assertTrue("-1/0 not infitity", Double.isInfinite(measurement
248: .getValue().doubleValue()));
249: }
250:
251: public void testZeroDevidedBy() throws Exception {
252: measurement = (RatioMeasurement) descriptor
253: .createMeasurement(metrics);
254:
255: assertTrue("0/0 not NaN", Double.isNaN(measurement.getValue()
256: .doubleValue()));
257:
258: m2.add(1);
259:
260: assertEquals("0/1 not zero", 0.0, measurement.getValue()
261: .doubleValue(), 0.01);
262:
263: m2.add(-2);
264:
265: assertEquals("0/-1 not zero", 0.0, measurement.getValue()
266: .doubleValue(), 0.01);
267: }
268:
269: public void testInUndefinedRange() throws Exception {
270: measurement = (RatioMeasurement) descriptor
271: .createMeasurement(metrics);
272:
273: m2.add(1);
274:
275: assertTrue(measurement.isInRange());
276:
277: m1.add(1);
278:
279: assertTrue(measurement.isInRange());
280:
281: m1.add(2);
282:
283: assertTrue(measurement.isInRange());
284: }
285:
286: public void testInOpenRange() throws Exception {
287: measurement = (RatioMeasurement) descriptor
288: .createMeasurement(metrics);
289:
290: m2.add(1);
291:
292: assertTrue(measurement.isInRange());
293:
294: m1.add(1);
295:
296: assertTrue(measurement.isInRange());
297:
298: m1.add(2);
299:
300: assertTrue(measurement.isInRange());
301: }
302:
303: public void testInLowerBoundRange() throws Exception {
304: descriptor.setLowerThreshold(new Integer(1));
305:
306: measurement = (RatioMeasurement) descriptor
307: .createMeasurement(metrics);
308:
309: m2.add(1);
310:
311: assertTrue(!measurement.isInRange());
312:
313: m1.add(1);
314:
315: assertTrue(measurement.isInRange());
316:
317: m1.add(2);
318:
319: assertTrue(measurement.isInRange());
320: }
321:
322: public void testInUpperBoundRange() throws Exception {
323: descriptor.setUpperThreshold(new Float(1.5));
324:
325: measurement = (RatioMeasurement) descriptor
326: .createMeasurement(metrics);
327:
328: m2.add(1);
329:
330: assertTrue(measurement.isInRange());
331:
332: m1.add(1);
333:
334: assertTrue(measurement.isInRange());
335:
336: m1.add(2);
337:
338: assertTrue(!measurement.isInRange());
339: }
340:
341: public void testInBoundRange() throws Exception {
342: descriptor.setLowerThreshold(new Integer(1));
343: descriptor.setUpperThreshold(new Float(1.5));
344:
345: measurement = (RatioMeasurement) descriptor
346: .createMeasurement(metrics);
347:
348: m2.add(1);
349:
350: assertTrue(!measurement.isInRange());
351:
352: m1.add(1);
353:
354: assertTrue(measurement.isInRange());
355:
356: m1.add(2);
357:
358: assertTrue(!measurement.isInRange());
359: }
360:
361: public void testCachedValue() throws Exception {
362: descriptor.setCached(true);
363:
364: measurement = (RatioMeasurement) descriptor
365: .createMeasurement(metrics);
366:
367: assertTrue("0/0 not NaN", Double.isNaN(measurement.getValue()
368: .doubleValue()));
369:
370: m2.add(1);
371:
372: assertTrue("cached 0/0 not NaN", Double.isNaN(measurement
373: .getValue().doubleValue()));
374: }
375:
376: public void testAccept() throws Exception {
377: measurement = (RatioMeasurement) descriptor
378: .createMeasurement(metrics);
379:
380: visited = null;
381: measurement.accept(this );
382: assertSame(measurement, visited);
383: }
384:
385: public void testEmpty() throws Exception {
386: measurement = (RatioMeasurement) descriptor
387: .createMeasurement(metrics);
388:
389: assertEquals("base == 0", 0, m1.intValue());
390: assertEquals("divider == 0", 0, m2.intValue());
391: assertTrue("0/0", measurement.isEmpty());
392:
393: m1.add(1);
394:
395: assertEquals("base != 1", 1, m1.intValue());
396: assertEquals("divider != 0", 0, m2.intValue());
397: assertTrue("1/0", measurement.isEmpty());
398:
399: m2.add(1);
400:
401: assertEquals("base != 1", 1, m1.intValue());
402: assertEquals("divider != 1", 1, m2.intValue());
403: assertFalse("1/1", measurement.isEmpty());
404:
405: m1.add(-1);
406:
407: assertEquals("base != 0", 0, m1.intValue());
408: assertEquals("divider != 1", 1, m2.intValue());
409: assertFalse("0/1", measurement.isEmpty());
410:
411: m2.add(-1);
412:
413: assertEquals("base != 0", 0, m1.intValue());
414: assertEquals("divider != 0", 0, m2.intValue());
415: assertTrue("0/0", measurement.isEmpty());
416: }
417:
418: public void visitStatisticalMeasurement(
419: StatisticalMeasurement measurement) {
420: // Do nothing
421: }
422:
423: public void visitRatioMeasurement(RatioMeasurement measurement) {
424: visited = measurement;
425: }
426:
427: public void visitNbSubMetricsMeasurement(
428: NbSubMetricsMeasurement measurement) {
429: // Do nothing
430: }
431:
432: public void visitCounterMeasurement(CounterMeasurement measurement) {
433: // Do nothing
434: }
435:
436: public void visitContextAccumulatorMeasurement(
437: ContextAccumulatorMeasurement measurement) {
438: // Do nothing
439: }
440:
441: public void visitNameListMeasurement(NameListMeasurement measurement) {
442: // Do nothing
443: }
444:
445: public void visitSubMetricsAccumulatorMeasurement(
446: SubMetricsAccumulatorMeasurement measurement) {
447: // Do nothing
448: }
449:
450: public void visitSumMeasurement(SumMeasurement measurement) {
451: // Do nothing
452: }
453: }
|