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