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