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 TestSubMetricsAccumulatorMeasurement 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(SubMetricsAccumulatorMeasurement.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(SubMetricsAccumulatorMeasurement.class,
110: measurement.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.addSubMetrics(m1);
154: metrics.addSubMetrics(m2);
155: metrics.addSubMetrics(m3);
156:
157: assertEquals(4, measurement.intValue());
158: assertTrue("\"abc\" not in " + measurement.getValues(),
159: measurement.getValues().contains("abc"));
160: assertTrue("\"def\" not in " + measurement.getValues(),
161: measurement.getValues().contains("def"));
162: assertTrue("\"ghi\" not in " + measurement.getValues(),
163: measurement.getValues().contains("ghi"));
164: assertTrue("\"jkl\" not in " + measurement.getValues(),
165: measurement.getValues().contains("jkl"));
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.addSubMetrics(m1);
178: metrics.addSubMetrics(m2);
179: metrics.addSubMetrics(m3);
180:
181: assertEquals(0, measurement.intValue());
182: assertTrue(measurement.getValues().isEmpty());
183: }
184:
185: public void testSingleFiltered() throws Exception {
186: descriptor.setInitText("NL /a/");
187:
188: measurement = (AccumulatorMeasurement) descriptor
189: .createMeasurement(metrics);
190: assertEquals(0, measurement.intValue());
191: assertTrue(measurement.getValues().isEmpty());
192:
193: metrics.addSubMetrics(m1);
194: metrics.addSubMetrics(m2);
195: metrics.addSubMetrics(m3);
196:
197: assertEquals(1, measurement.intValue());
198: assertTrue("\"abc\" not in " + measurement.getValues(),
199: measurement.getValues().contains("abc"));
200: }
201:
202: public void testMultiFilterFiltered() throws Exception {
203: descriptor.setInitText("NL /a/\nNL /k/");
204:
205: measurement = (AccumulatorMeasurement) descriptor
206: .createMeasurement(metrics);
207: assertEquals(0, measurement.intValue());
208: assertTrue(measurement.getValues().isEmpty());
209:
210: metrics.addSubMetrics(m1);
211: metrics.addSubMetrics(m2);
212: metrics.addSubMetrics(m3);
213:
214: assertEquals(2, measurement.intValue());
215: assertTrue("\"abc\" not in " + measurement.getValues(),
216: measurement.getValues().contains("abc"));
217: assertTrue("\"jkl\" not in " + measurement.getValues(),
218: measurement.getValues().contains("jkl"));
219: }
220:
221: public void testModifiedValues() throws Exception {
222: descriptor.setInitText("NL /(a)/");
223:
224: measurement = (AccumulatorMeasurement) descriptor
225: .createMeasurement(metrics);
226: assertEquals(0, measurement.intValue());
227: assertTrue(measurement.getValues().isEmpty());
228:
229: metrics.addSubMetrics(m1);
230: metrics.addSubMetrics(m2);
231: metrics.addSubMetrics(m3);
232:
233: assertEquals(1, measurement.intValue());
234: assertTrue("\"a\" not in " + measurement.getValues(),
235: measurement.getValues().contains("a"));
236: }
237:
238: public void testMultiMeasurements() throws Exception {
239: descriptor.setInitText("NL /a/\nNbL /2/");
240:
241: measurement = (AccumulatorMeasurement) descriptor
242: .createMeasurement(metrics);
243: assertEquals(0, measurement.intValue());
244: assertTrue(measurement.getValues().isEmpty());
245:
246: metrics.addSubMetrics(m1);
247: metrics.addSubMetrics(m2);
248: metrics.addSubMetrics(m3);
249:
250: assertEquals(3, measurement.intValue());
251: assertTrue("\"abc\" not in " + measurement.getValues(),
252: measurement.getValues().contains("abc"));
253: assertTrue("\"123\" not in " + measurement.getValues(),
254: measurement.getValues().contains("123"));
255: assertTrue("\"248\" not in " + measurement.getValues(),
256: measurement.getValues().contains("248"));
257: }
258:
259: public void testAccept() throws Exception {
260: measurement = (AccumulatorMeasurement) descriptor
261: .createMeasurement(metrics);
262:
263: visited = null;
264: measurement.accept(this );
265: assertSame(measurement, visited);
266: }
267:
268: public void testEmpty() throws Exception {
269: descriptor.setInitText("NL");
270:
271: measurement = (AccumulatorMeasurement) descriptor
272: .createMeasurement(metrics);
273: metrics.track(nameList.createMeasurement(metrics));
274:
275: Metrics submetrics = new Metrics("submetrics");
276: submetrics.track(nameList.createMeasurement(submetrics));
277: metrics.addSubMetrics(submetrics);
278:
279: assertTrue("Before Add()", measurement.isEmpty());
280:
281: submetrics.addToMeasurement("NL", "foo");
282:
283: assertFalse("After Add()", measurement.isEmpty());
284: }
285:
286: public void visitStatisticalMeasurement(
287: StatisticalMeasurement measurement) {
288: // Do nothing
289: }
290:
291: public void visitRatioMeasurement(RatioMeasurement measurement) {
292: // Do nothing
293: }
294:
295: public void visitNbSubMetricsMeasurement(
296: NbSubMetricsMeasurement measurement) {
297: // Do nothing
298: }
299:
300: public void visitCounterMeasurement(CounterMeasurement measurement) {
301: // Do nothing
302: }
303:
304: public void visitContextAccumulatorMeasurement(
305: ContextAccumulatorMeasurement measurement) {
306: // Do nothing
307: }
308:
309: public void visitNameListMeasurement(NameListMeasurement measurement) {
310: // Do nothing
311: }
312:
313: public void visitSubMetricsAccumulatorMeasurement(
314: SubMetricsAccumulatorMeasurement measurement) {
315: visited = measurement;
316: }
317:
318: public void visitSumMeasurement(SumMeasurement measurement) {
319: // Do nothing
320: }
321: }
|