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 TestContextAccumulatorMeasurement extends TestCase
038: implements MeasurementVisitor {
039: private MeasurementDescriptor descriptor;
040: private AccumulatorMeasurement measurement;
041: private Metrics metrics;
042: private Measurement visited;
043:
044: private MeasurementDescriptor nameList;
045: private MeasurementDescriptor numberList;
046: private MeasurementDescriptor counter;
047:
048: private Metrics m1;
049: private Metrics m2;
050: private Metrics m3;
051:
052: protected void setUp() throws Exception {
053: m1 = new Metrics("m1");
054: m2 = new Metrics("m2");
055: m3 = new Metrics("m3");
056:
057: nameList = new MeasurementDescriptor();
058: nameList.setShortName("NL");
059: nameList.setLongName("name list");
060: nameList.setClassFor(NameListMeasurement.class);
061:
062: numberList = new MeasurementDescriptor();
063: numberList.setShortName("NbL");
064: numberList.setLongName("number list");
065: numberList.setClassFor(NameListMeasurement.class);
066:
067: counter = new MeasurementDescriptor();
068: counter.setShortName("NL");
069: counter.setLongName("counter");
070: counter.setClassFor(CounterMeasurement.class);
071:
072: m1.track(nameList.createMeasurement(m1));
073: m1.addToMeasurement("NL", "abc");
074: m1.addToMeasurement("NL", "def");
075: m1.addToMeasurement("NL", "ghi");
076:
077: m1.track(numberList.createMeasurement(m1));
078: m1.addToMeasurement("NbL", "123");
079: m1.addToMeasurement("NbL", "456");
080: m1.addToMeasurement("NbL", "789");
081:
082: m2.track(nameList.createMeasurement(m2));
083: m2.addToMeasurement("NL", "jkl");
084: m2.addToMeasurement("NL", "abc");
085:
086: m2.track(numberList.createMeasurement(m2));
087: m2.addToMeasurement("NbL", "159");
088: m2.addToMeasurement("NbL", "248");
089:
090: m3.track(counter.createMeasurement(m3));
091: m3.addToMeasurement("NL", 1);
092:
093: metrics = new Metrics("metrics");
094:
095: descriptor = new MeasurementDescriptor();
096: descriptor.setShortName("foo");
097: descriptor.setLongName("bar");
098: descriptor.setClassFor(ContextAccumulatorMeasurement.class);
099: descriptor.setCached(false);
100: }
101:
102: public void testCreateFromMeasurementDescriptor() throws Exception {
103: measurement = (AccumulatorMeasurement) descriptor
104: .createMeasurement(metrics);
105:
106: assertNotNull(measurement);
107: assertEquals(descriptor, measurement.getDescriptor());
108: assertSame(descriptor, measurement.getDescriptor());
109: assertEquals(ContextAccumulatorMeasurement.class, measurement
110: .getClass());
111: assertEquals("foo", measurement.getShortName());
112: assertEquals("bar", measurement.getLongName());
113: }
114:
115: public void testNullInit() throws Exception {
116: measurement = (AccumulatorMeasurement) descriptor
117: .createMeasurement(metrics);
118: assertEquals(0, measurement.intValue());
119: assertTrue(measurement.getValues().isEmpty());
120:
121: metrics.addSubMetrics(m1);
122: metrics.addSubMetrics(m2);
123: metrics.addSubMetrics(m3);
124:
125: assertEquals(0, measurement.intValue());
126: assertTrue(measurement.getValues().isEmpty());
127: }
128:
129: public void testEmptyInit() throws Exception {
130: descriptor.setInitText("");
131:
132: measurement = (AccumulatorMeasurement) descriptor
133: .createMeasurement(metrics);
134: assertEquals(0, measurement.intValue());
135: assertTrue(measurement.getValues().isEmpty());
136:
137: metrics.addSubMetrics(m1);
138: metrics.addSubMetrics(m2);
139: metrics.addSubMetrics(m3);
140:
141: assertEquals(0, measurement.intValue());
142: assertTrue(measurement.getValues().isEmpty());
143: }
144:
145: public void testRawValues() throws Exception {
146: descriptor.setInitText("NL");
147:
148: measurement = (AccumulatorMeasurement) descriptor
149: .createMeasurement(metrics);
150: assertEquals(0, measurement.intValue());
151: assertTrue(measurement.getValues().isEmpty());
152:
153: metrics.track(nameList.createMeasurement(metrics));
154: metrics.addToMeasurement("NL", "foo");
155: metrics.addToMeasurement("NL", "bar");
156:
157: metrics.addSubMetrics(m1);
158: metrics.addSubMetrics(m2);
159: metrics.addSubMetrics(m3);
160:
161: assertEquals(2, measurement.intValue());
162: assertTrue("\"foo\" not in " + measurement.getValues(),
163: measurement.getValues().contains("foo"));
164: assertTrue("\"bar\" not in " + measurement.getValues(),
165: measurement.getValues().contains("bar"));
166: }
167:
168: public void testCachedValues() throws Exception {
169: descriptor.setInitText("NL");
170: descriptor.setCached(true);
171:
172: measurement = (AccumulatorMeasurement) descriptor
173: .createMeasurement(metrics);
174: assertEquals(0, measurement.intValue());
175: assertTrue(measurement.getValues().isEmpty());
176:
177: metrics.track(nameList.createMeasurement(metrics));
178: metrics.addToMeasurement("NL", "foo");
179: metrics.addToMeasurement("NL", "bar");
180:
181: metrics.addSubMetrics(m1);
182: metrics.addSubMetrics(m2);
183: metrics.addSubMetrics(m3);
184:
185: assertEquals(0, measurement.intValue());
186: assertTrue(measurement.getValues().isEmpty());
187: }
188:
189: public void testSingleFiltered() throws Exception {
190: descriptor.setInitText("NL /a/");
191:
192: measurement = (AccumulatorMeasurement) descriptor
193: .createMeasurement(metrics);
194: assertEquals(0, measurement.intValue());
195: assertTrue(measurement.getValues().isEmpty());
196:
197: metrics.track(nameList.createMeasurement(metrics));
198: metrics.addToMeasurement("NL", "foo");
199: metrics.addToMeasurement("NL", "bar");
200:
201: metrics.addSubMetrics(m1);
202: metrics.addSubMetrics(m2);
203: metrics.addSubMetrics(m3);
204:
205: assertEquals(1, measurement.intValue());
206: assertTrue("\"bar\" not in " + measurement.getValues(),
207: measurement.getValues().contains("bar"));
208: }
209:
210: public void testMultiFilterFiltered() throws Exception {
211: descriptor.setInitText("NL /a/\nNL /o/");
212:
213: measurement = (AccumulatorMeasurement) descriptor
214: .createMeasurement(metrics);
215: assertEquals(0, measurement.intValue());
216: assertTrue(measurement.getValues().isEmpty());
217:
218: metrics.track(nameList.createMeasurement(metrics));
219: metrics.addToMeasurement("NL", "foo");
220: metrics.addToMeasurement("NL", "bar");
221:
222: metrics.addSubMetrics(m1);
223: metrics.addSubMetrics(m2);
224: metrics.addSubMetrics(m3);
225:
226: assertEquals(2, measurement.intValue());
227: assertTrue("\"foo\" not in " + measurement.getValues(),
228: measurement.getValues().contains("foo"));
229: assertTrue("\"bar\" not in " + measurement.getValues(),
230: measurement.getValues().contains("bar"));
231: }
232:
233: public void testModifiedValues() throws Exception {
234: descriptor.setInitText("NL /(a)/");
235:
236: measurement = (AccumulatorMeasurement) descriptor
237: .createMeasurement(metrics);
238: assertEquals(0, measurement.intValue());
239: assertTrue(measurement.getValues().isEmpty());
240:
241: metrics.track(nameList.createMeasurement(metrics));
242: metrics.addToMeasurement("NL", "foo");
243: metrics.addToMeasurement("NL", "bar");
244:
245: metrics.addSubMetrics(m1);
246: metrics.addSubMetrics(m2);
247: metrics.addSubMetrics(m3);
248:
249: assertEquals(1, measurement.intValue());
250: assertTrue("\"a\" not in " + measurement.getValues(),
251: measurement.getValues().contains("a"));
252: }
253:
254: public void testMultiMeasurements() throws Exception {
255: descriptor.setInitText("NL /a/\nNbL /2/");
256:
257: measurement = (AccumulatorMeasurement) descriptor
258: .createMeasurement(metrics);
259: assertEquals(0, measurement.intValue());
260: assertTrue(measurement.getValues().isEmpty());
261:
262: metrics.track(nameList.createMeasurement(metrics));
263: metrics.addToMeasurement("NL", "foo");
264: metrics.addToMeasurement("NL", "bar");
265:
266: metrics.track(numberList.createMeasurement(metrics));
267: metrics.addToMeasurement("NbL", "1234");
268: metrics.addToMeasurement("NbL", "5678");
269:
270: metrics.addSubMetrics(m1);
271: metrics.addSubMetrics(m2);
272: metrics.addSubMetrics(m3);
273:
274: assertEquals(2, measurement.intValue());
275: assertTrue("\"bar\" not in " + measurement.getValues(),
276: measurement.getValues().contains("bar"));
277: assertTrue("\"1234\" not in " + measurement.getValues(),
278: measurement.getValues().contains("1234"));
279: }
280:
281: public void testAccept() throws Exception {
282: measurement = (AccumulatorMeasurement) descriptor
283: .createMeasurement(metrics);
284:
285: visited = null;
286: measurement.accept(this );
287: assertSame(measurement, visited);
288: }
289:
290: public void testEmpty() throws Exception {
291: descriptor.setInitText("NL");
292:
293: measurement = (AccumulatorMeasurement) descriptor
294: .createMeasurement(metrics);
295: metrics.track(nameList.createMeasurement(metrics));
296:
297: assertTrue("Before Add()", measurement.isEmpty());
298:
299: metrics.addToMeasurement("NL", "foo");
300:
301: assertFalse("After Add()", measurement.isEmpty());
302: }
303:
304: public void visitStatisticalMeasurement(
305: StatisticalMeasurement measurement) {
306: // Do nothing
307: }
308:
309: public void visitRatioMeasurement(RatioMeasurement measurement) {
310: // Do nothing
311: }
312:
313: public void visitNbSubMetricsMeasurement(
314: NbSubMetricsMeasurement measurement) {
315: // Do nothing
316: }
317:
318: public void visitCounterMeasurement(CounterMeasurement measurement) {
319: // Do nothing
320: }
321:
322: public void visitContextAccumulatorMeasurement(
323: ContextAccumulatorMeasurement measurement) {
324: visited = measurement;
325: }
326:
327: public void visitNameListMeasurement(NameListMeasurement measurement) {
328: // Do nothing
329: }
330:
331: public void visitSubMetricsAccumulatorMeasurement(
332: SubMetricsAccumulatorMeasurement measurement) {
333: // Do nothing
334: }
335:
336: public void visitSumMeasurement(SumMeasurement measurement) {
337: // Do nothing
338: }
339: }
|