KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > metrics > TestSubMetricsAccumulatorMeasurement


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.metrics;
34
35 import junit.framework.*;
36
37 public class TestSubMetricsAccumulatorMeasurement extends TestCase implements MeasurementVisitor {
38     private MeasurementDescriptor descriptor;
39     private AccumulatorMeasurement measurement;
40     private Metrics metrics;
41     private Measurement visited;
42
43     private MeasurementDescriptor nameList;
44     private MeasurementDescriptor numberList;
45     private MeasurementDescriptor counter;
46
47     private Metrics m1;
48     private Metrics m2;
49     private Metrics m3;
50
51     protected void setUp() throws Exception JavaDoc {
52         m1 = new Metrics("m1");
53         m2 = new Metrics("m2");
54         m3 = new Metrics("m3");
55
56         nameList = new MeasurementDescriptor();
57         nameList.setShortName("NL");
58         nameList.setLongName("name list");
59         nameList.setClassFor(NameListMeasurement.class);
60
61         numberList = new MeasurementDescriptor();
62         numberList.setShortName("NbL");
63         numberList.setLongName("number list");
64         numberList.setClassFor(NameListMeasurement.class);
65
66         counter = new MeasurementDescriptor();
67         counter.setShortName("NL");
68         counter.setLongName("counter");
69         counter.setClassFor(CounterMeasurement.class);
70
71         m1.track(nameList.createMeasurement(m1));
72         m1.addToMeasurement("NL", "abc");
73         m1.addToMeasurement("NL", "def");
74         m1.addToMeasurement("NL", "ghi");
75
76         m1.track(numberList.createMeasurement(m1));
77         m1.addToMeasurement("NbL", "123");
78         m1.addToMeasurement("NbL", "456");
79         m1.addToMeasurement("NbL", "789");
80
81         m2.track(nameList.createMeasurement(m2));
82         m2.addToMeasurement("NL", "jkl");
83         m2.addToMeasurement("NL", "abc");
84
85         m2.track(numberList.createMeasurement(m2));
86         m2.addToMeasurement("NbL", "159");
87         m2.addToMeasurement("NbL", "248");
88
89         m3.track(counter.createMeasurement(m3));
90         m3.addToMeasurement("NL", 1);
91
92         metrics = new Metrics("metrics");
93
94         descriptor = new MeasurementDescriptor();
95         descriptor.setShortName("foo");
96         descriptor.setLongName("bar");
97         descriptor.setClassFor(SubMetricsAccumulatorMeasurement.class);
98         descriptor.setCached(false);
99     }
100
101     public void testCreateFromMeasurementDescriptor() throws Exception JavaDoc {
102         measurement = (AccumulatorMeasurement) descriptor.createMeasurement(metrics);
103
104         assertNotNull(measurement);
105         assertEquals(descriptor, measurement.getDescriptor());
106         assertSame(descriptor, measurement.getDescriptor());
107         assertEquals(SubMetricsAccumulatorMeasurement.class, measurement.getClass());
108         assertEquals("foo", measurement.getShortName());
109         assertEquals("bar", measurement.getLongName());
110     }
111
112     public void testNullInit() throws Exception JavaDoc {
113         measurement = (AccumulatorMeasurement) descriptor.createMeasurement(metrics);
114         assertEquals(0, measurement.intValue());
115         assertTrue(measurement.getValues().isEmpty());
116
117         metrics.addSubMetrics(m1);
118         metrics.addSubMetrics(m2);
119         metrics.addSubMetrics(m3);
120
121         assertEquals(0, measurement.intValue());
122         assertTrue(measurement.getValues().isEmpty());
123     }
124
125     public void testEmptyInit() throws Exception JavaDoc {
126         descriptor.setInitText("");
127
128         measurement = (AccumulatorMeasurement) descriptor.createMeasurement(metrics);
129         assertEquals(0, measurement.intValue());
130         assertTrue(measurement.getValues().isEmpty());
131
132         metrics.addSubMetrics(m1);
133         metrics.addSubMetrics(m2);
134         metrics.addSubMetrics(m3);
135
136         assertEquals(0, measurement.intValue());
137         assertTrue(measurement.getValues().isEmpty());
138     }
139
140     public void testRawValues() throws Exception JavaDoc {
141         descriptor.setInitText("NL");
142
143         measurement = (AccumulatorMeasurement) descriptor.createMeasurement(metrics);
144         assertEquals(0, measurement.intValue());
145         assertTrue(measurement.getValues().isEmpty());
146
147         metrics.addSubMetrics(m1);
148         metrics.addSubMetrics(m2);
149         metrics.addSubMetrics(m3);
150
151         assertEquals(4, measurement.intValue());
152         assertTrue("\"abc\" not in " + measurement.getValues(), measurement.getValues().contains("abc"));
153         assertTrue("\"def\" not in " + measurement.getValues(), measurement.getValues().contains("def"));
154         assertTrue("\"ghi\" not in " + measurement.getValues(), measurement.getValues().contains("ghi"));
155         assertTrue("\"jkl\" not in " + measurement.getValues(), measurement.getValues().contains("jkl"));
156     }
157
158     public void testCachedValues() throws Exception JavaDoc {
159         descriptor.setInitText("NL");
160         descriptor.setCached(true);
161
162         measurement = (AccumulatorMeasurement) descriptor.createMeasurement(metrics);
163         assertEquals(0, measurement.intValue());
164         assertTrue(measurement.getValues().isEmpty());
165
166         metrics.addSubMetrics(m1);
167         metrics.addSubMetrics(m2);
168         metrics.addSubMetrics(m3);
169
170         assertEquals(0, measurement.intValue());
171         assertTrue(measurement.getValues().isEmpty());
172     }
173
174     public void testSingleFiltered() throws Exception JavaDoc {
175         descriptor.setInitText("NL /a/");
176
177         measurement = (AccumulatorMeasurement) descriptor.createMeasurement(metrics);
178         assertEquals(0, measurement.intValue());
179         assertTrue(measurement.getValues().isEmpty());
180
181         metrics.addSubMetrics(m1);
182         metrics.addSubMetrics(m2);
183         metrics.addSubMetrics(m3);
184
185         assertEquals(1, measurement.intValue());
186         assertTrue("\"abc\" not in " + measurement.getValues(), measurement.getValues().contains("abc"));
187     }
188
189     public void testMultiFilterFiltered() throws Exception JavaDoc {
190         descriptor.setInitText("NL /a/\nNL /k/");
191
192         measurement = (AccumulatorMeasurement) descriptor.createMeasurement(metrics);
193         assertEquals(0, measurement.intValue());
194         assertTrue(measurement.getValues().isEmpty());
195
196         metrics.addSubMetrics(m1);
197         metrics.addSubMetrics(m2);
198         metrics.addSubMetrics(m3);
199
200         assertEquals(2, measurement.intValue());
201         assertTrue("\"abc\" not in " + measurement.getValues(), measurement.getValues().contains("abc"));
202         assertTrue("\"jkl\" not in " + measurement.getValues(), measurement.getValues().contains("jkl"));
203     }
204
205     public void testModifiedValues() throws Exception JavaDoc {
206         descriptor.setInitText("NL /(a)/");
207
208         measurement = (AccumulatorMeasurement) descriptor.createMeasurement(metrics);
209         assertEquals(0, measurement.intValue());
210         assertTrue(measurement.getValues().isEmpty());
211
212         metrics.addSubMetrics(m1);
213         metrics.addSubMetrics(m2);
214         metrics.addSubMetrics(m3);
215
216         assertEquals(1, measurement.intValue());
217         assertTrue("\"a\" not in " + measurement.getValues(), measurement.getValues().contains("a"));
218     }
219
220     public void testMultiMeasurements() throws Exception JavaDoc {
221         descriptor.setInitText("NL /a/\nNbL /2/");
222
223         measurement = (AccumulatorMeasurement) descriptor.createMeasurement(metrics);
224         assertEquals(0, measurement.intValue());
225         assertTrue(measurement.getValues().isEmpty());
226
227         metrics.addSubMetrics(m1);
228         metrics.addSubMetrics(m2);
229         metrics.addSubMetrics(m3);
230
231         assertEquals(3, measurement.intValue());
232         assertTrue("\"abc\" not in " + measurement.getValues(), measurement.getValues().contains("abc"));
233         assertTrue("\"123\" not in " + measurement.getValues(), measurement.getValues().contains("123"));
234         assertTrue("\"248\" not in " + measurement.getValues(), measurement.getValues().contains("248"));
235     }
236
237     public void testAccept() throws Exception JavaDoc {
238         measurement = (AccumulatorMeasurement) descriptor.createMeasurement(metrics);
239
240         visited = null;
241         measurement.accept(this);
242         assertSame(measurement, visited);
243     }
244
245     public void testEmpty() throws Exception JavaDoc {
246         descriptor.setInitText("NL");
247
248         measurement = (AccumulatorMeasurement) descriptor.createMeasurement(metrics);
249         metrics.track(nameList.createMeasurement(metrics));
250
251         Metrics submetrics = new Metrics("submetrics");
252         submetrics.track(nameList.createMeasurement(submetrics));
253         metrics.addSubMetrics(submetrics);
254         
255         assertTrue("Before Add()", measurement.isEmpty());
256
257         submetrics.addToMeasurement("NL", "foo");
258
259         assertFalse("After Add()", measurement.isEmpty());
260     }
261     
262     public void visitStatisticalMeasurement(StatisticalMeasurement measurement) {
263         // Do nothing
264
}
265     
266     public void visitRatioMeasurement(RatioMeasurement measurement) {
267         // Do nothing
268
}
269     
270     public void visitNbSubMetricsMeasurement(NbSubMetricsMeasurement measurement) {
271         // Do nothing
272
}
273     
274     public void visitCounterMeasurement(CounterMeasurement measurement) {
275         // Do nothing
276
}
277     
278     public void visitContextAccumulatorMeasurement(ContextAccumulatorMeasurement measurement) {
279         // Do nothing
280
}
281         
282     public void visitNameListMeasurement(NameListMeasurement measurement) {
283         // Do nothing
284
}
285     
286     public void visitSubMetricsAccumulatorMeasurement(SubMetricsAccumulatorMeasurement measurement) {
287         visited = measurement;
288     }
289
290     public void visitSumMeasurement(SumMeasurement measurement) {
291         // Do nothing
292
}
293 }
294
Popular Tags