KickJava   Java API By Example, From Geeks To Geeks.

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


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 TestSumMeasurement extends TestCase implements MeasurementVisitor {
38     private MeasurementDescriptor descriptor;
39     private Metrics metrics;
40     private SumMeasurement measurement;
41     private Measurement visited;
42     
43     protected void setUp() {
44         descriptor = new MeasurementDescriptor();
45         descriptor.setShortName("foo");
46         descriptor.setLongName("FOO");
47         descriptor.setClassFor(SumMeasurement.class);
48         descriptor.setCached(false);
49
50         metrics = new Metrics("foobar");
51     }
52
53     public void testMeasurementDescriptor() throws Exception JavaDoc {
54         measurement = (SumMeasurement) descriptor.createMeasurement();
55         
56         assertNotNull(measurement.getDescriptor());
57         assertEquals(SumMeasurement.class, measurement.getDescriptor().getClassFor());
58         assertEquals("foo", measurement.getShortName());
59         assertEquals("FOO", measurement.getLongName());
60     }
61
62     public void testCreateFromMeasurementDescriptor() throws Exception JavaDoc {
63         measurement = (SumMeasurement) descriptor.createMeasurement();
64         
65         assertNotNull(measurement);
66         assertEquals(descriptor, measurement.getDescriptor());
67         assertSame(descriptor, measurement.getDescriptor());
68         assertEquals(SumMeasurement.class, measurement.getClass());
69         assertEquals("foo", measurement.getShortName());
70         assertEquals("FOO", measurement.getLongName());
71     }
72
73     public void testCreateDefault() {
74         measurement = new SumMeasurement(descriptor, null, null);
75         
76         assertEquals(0, measurement.doubleValue(), 0.01);
77     }
78
79     public void testEmptyInitText() throws Exception JavaDoc {
80         descriptor.setInitText("");
81
82         measurement = (SumMeasurement) descriptor.createMeasurement();
83
84         assertEquals(0, measurement.doubleValue(), 0.01);
85     }
86
87     public void testEmptyLineInitText() throws Exception JavaDoc {
88         descriptor.setInitText("\n");
89
90         measurement = (SumMeasurement) descriptor.createMeasurement();
91
92         assertEquals(0, measurement.doubleValue(), 0.01);
93     }
94
95     public void testDashInitText() throws Exception JavaDoc {
96         descriptor.setInitText("-");
97
98         measurement = (SumMeasurement) descriptor.createMeasurement();
99
100         assertEquals(0, measurement.doubleValue(), 0.01);
101     }
102
103     public void testConstant() throws Exception JavaDoc {
104         descriptor.setInitText("2");
105
106         measurement = (SumMeasurement) descriptor.createMeasurement();
107
108         assertEquals(2, measurement.doubleValue(), 0.01);
109     }
110
111     public void testConstantAndEmptyLine() throws Exception JavaDoc {
112         descriptor.setInitText("\n2\n");
113
114         measurement = (SumMeasurement) descriptor.createMeasurement();
115
116         assertEquals(2, measurement.doubleValue(), 0.01);
117     }
118
119     public void testAddition() throws Exception JavaDoc {
120         descriptor.setInitText("1\n1");
121
122         measurement = (SumMeasurement) descriptor.createMeasurement();
123
124         assertEquals(2, measurement.doubleValue(), 0.01);
125     }
126
127     public void testNegative() throws Exception JavaDoc {
128         descriptor.setInitText("-2");
129
130         measurement = (SumMeasurement) descriptor.createMeasurement();
131
132         assertEquals(-2, measurement.doubleValue(), 0.01);
133     }
134
135     public void testSubstraction() throws Exception JavaDoc {
136         descriptor.setInitText("2\n-1");
137
138         measurement = (SumMeasurement) descriptor.createMeasurement();
139
140         assertEquals(1, measurement.doubleValue(), 0.01);
141
142         descriptor.setInitText("1\n-2");
143
144         measurement = (SumMeasurement) descriptor.createMeasurement();
145
146         assertEquals(-1, measurement.doubleValue(), 0.01);
147     }
148
149     public void testSubMeasurement() throws Exception JavaDoc {
150         descriptor.setInitText("bar");
151
152         metrics.track("bar", new CounterMeasurement(null, metrics, "2"));
153         
154         measurement = (SumMeasurement) descriptor.createMeasurement(metrics);
155
156         assertEquals(2, measurement.doubleValue(), 0.01);
157     }
158
159     public void testStatisticalMeasurement() throws Exception JavaDoc {
160         descriptor.setInitText("bar DISPOSE_SUM");
161
162         metrics.track("bar", new StatisticalMeasurement(null, metrics, "bar"));
163
164         Metrics submetrics = new Metrics("submetrics");
165         submetrics.track("bar", new CounterMeasurement(null, submetrics, "2"));
166         metrics.addSubMetrics(submetrics);
167         
168         measurement = (SumMeasurement) descriptor.createMeasurement(metrics);
169
170         assertEquals(2, measurement.doubleValue(), 0.01);
171     }
172
173     public void testAddMeasurements() throws Exception JavaDoc {
174         descriptor.setInitText("bar\nbaz");
175
176         metrics.track("bar", new CounterMeasurement(null, metrics, "1"));
177         metrics.track("baz", new CounterMeasurement(null, metrics, "1"));
178
179         measurement = (SumMeasurement) descriptor.createMeasurement(metrics);
180
181         assertEquals(2, measurement.doubleValue(), 0.01);
182     }
183
184     public void testSubstractMeasurements() throws Exception JavaDoc {
185         descriptor.setInitText("bar\n-baz");
186
187         metrics.track("bar", new CounterMeasurement(null, metrics, "1"));
188         metrics.track("baz", new CounterMeasurement(null, metrics, "2"));
189
190         measurement = (SumMeasurement) descriptor.createMeasurement(metrics);
191
192         assertEquals(-1, measurement.doubleValue(), 0.01);
193     }
194
195     public void testInUndefinedRange() throws Exception JavaDoc {
196         descriptor.setInitText("bar");
197
198         metrics.track("bar", new CounterMeasurement(null, null, null));
199
200         measurement = (SumMeasurement) descriptor.createMeasurement(metrics);
201
202         assertTrue(measurement.isInRange());
203
204         metrics.addToMeasurement("bar", 1);
205         
206         assertTrue(measurement.isInRange());
207
208         metrics.addToMeasurement("bar", 1);
209
210         assertTrue(measurement.isInRange());
211     }
212
213     public void testInOpenRange() throws Exception JavaDoc {
214         descriptor.setInitText("bar");
215
216         metrics.track("bar", new CounterMeasurement(null, null, null));
217
218         measurement = (SumMeasurement) descriptor.createMeasurement(metrics);
219         
220         assertTrue(measurement.isInRange());
221
222         metrics.addToMeasurement("bar", 1);
223         
224         assertTrue(measurement.isInRange());
225
226         metrics.addToMeasurement("bar", 1);
227
228         assertTrue(measurement.isInRange());
229     }
230
231     public void testInLowerBoundRange() throws Exception JavaDoc {
232         descriptor.setInitText("bar");
233         descriptor.setLowerThreshold(new Integer JavaDoc(1));
234
235         metrics.track("bar", new CounterMeasurement(null, null, null));
236
237         measurement = (SumMeasurement) descriptor.createMeasurement(metrics);
238         
239         assertEquals(0, measurement.intValue());
240         assertTrue(!measurement.isInRange());
241
242         metrics.addToMeasurement("bar", 1);
243         
244         assertEquals(1, measurement.intValue());
245         assertTrue(measurement.isInRange());
246
247         metrics.addToMeasurement("bar", 1);
248         
249         assertEquals(2, measurement.intValue());
250         assertTrue(measurement.isInRange());
251     }
252
253     public void testInUpperBoundRange() throws Exception JavaDoc {
254         descriptor.setInitText("bar");
255         descriptor.setUpperThreshold(new Float JavaDoc(1.5));
256
257         metrics.track("bar", new CounterMeasurement(null, null, null));
258
259         measurement = (SumMeasurement) descriptor.createMeasurement(metrics);
260         
261         assertTrue(measurement.isInRange());
262
263         metrics.addToMeasurement("bar", 1);
264         
265         assertTrue(measurement.isInRange());
266
267         metrics.addToMeasurement("bar", 1);
268         
269         assertTrue(!measurement.isInRange());
270     }
271
272     public void testInBoundRange() throws Exception JavaDoc {
273         descriptor.setInitText("bar");
274         descriptor.setLowerThreshold(new Integer JavaDoc(1));
275         descriptor.setUpperThreshold(new Float JavaDoc(1.5));
276
277         metrics.track("bar", new CounterMeasurement(null, null, null));
278
279         measurement = (SumMeasurement) descriptor.createMeasurement(metrics);
280         
281         assertTrue(!measurement.isInRange());
282
283         metrics.addToMeasurement("bar", 1);
284         
285         assertTrue(measurement.isInRange());
286
287         metrics.addToMeasurement("bar", 1);
288         
289         assertTrue(!measurement.isInRange());
290     }
291
292     public void testCachedValue() throws Exception JavaDoc {
293         descriptor.setInitText("bar");
294         descriptor.setCached(true);
295
296         metrics.track("bar", new CounterMeasurement(null, null, null));
297
298         measurement = (SumMeasurement) descriptor.createMeasurement(metrics);
299         
300         assertEquals(0, measurement.doubleValue(), 0.01);
301
302         metrics.addToMeasurement("bar", 1);
303         
304         assertEquals(0, measurement.doubleValue(), 0.01);
305     }
306     
307     public void testAccept() {
308         measurement = new SumMeasurement(null, null, null);
309
310         visited = null;
311         measurement.accept(this);
312         assertSame(measurement, visited);
313     }
314
315     public void testEmptyWithOneMeasurement() throws Exception JavaDoc {
316         descriptor.setInitText("bar");
317
318         metrics.track("bar", new CounterMeasurement(null, null, null));
319
320         measurement = (SumMeasurement) descriptor.createMeasurement(metrics);
321         
322         assertTrue("Before Add()", measurement.isEmpty());
323
324         metrics.addToMeasurement("bar", 1);
325         
326         assertFalse("After Add(1)", measurement.isEmpty());
327
328         metrics.addToMeasurement("bar", -1);
329
330         assertFalse("After Add(-1)", measurement.isEmpty());
331     }
332
333     public void testEmptyWithTwoMeasurements() throws Exception JavaDoc {
334         descriptor.setInitText("bar\nbaz");
335
336         metrics.track("bar", new CounterMeasurement(null, null, null));
337         metrics.track("baz", new CounterMeasurement(null, null, null));
338
339         measurement = (SumMeasurement) descriptor.createMeasurement(metrics);
340
341         assertTrue("bar is not empty", metrics.getMeasurement("bar").isEmpty());
342         assertTrue("baz is not empty", metrics.getMeasurement("baz").isEmpty());
343         assertTrue("Before Add()", measurement.isEmpty());
344
345         metrics.addToMeasurement("bar", 1);
346         
347         assertFalse("bar is empty", metrics.getMeasurement("bar").isEmpty());
348         assertTrue("baz is not empty", metrics.getMeasurement("baz").isEmpty());
349         assertFalse("After Add(1)", measurement.isEmpty());
350
351         metrics.addToMeasurement("bar", -1);
352
353         assertFalse("bar is empty", metrics.getMeasurement("bar").isEmpty());
354         assertTrue("baz is not empty", metrics.getMeasurement("baz").isEmpty());
355         assertFalse("After Add(-1)", measurement.isEmpty());
356     }
357
358     public void testEmptyWithConstant() throws Exception JavaDoc {
359         descriptor.setInitText("2");
360
361         measurement = (SumMeasurement) descriptor.createMeasurement(metrics);
362
363         assertTrue("with constants", measurement.isEmpty());
364     }
365     
366     public void visitStatisticalMeasurement(StatisticalMeasurement measurement) {
367         // Do nothing
368
}
369     
370     public void visitRatioMeasurement(RatioMeasurement measurement) {
371         // Do nothing
372
}
373     
374     public void visitNbSubMetricsMeasurement(NbSubMetricsMeasurement measurement) {
375         // Do nothing
376
}
377     
378     public void visitCounterMeasurement(CounterMeasurement measurement) {
379         // Do nothing
380
}
381     
382     public void visitContextAccumulatorMeasurement(ContextAccumulatorMeasurement measurement) {
383         // Do nothing
384
}
385     
386     public void visitNameListMeasurement(NameListMeasurement measurement) {
387         // Do nothing
388
}
389     
390     public void visitSubMetricsAccumulatorMeasurement(SubMetricsAccumulatorMeasurement measurement) {
391         // Do nothing
392
}
393     
394     public void visitSumMeasurement(SumMeasurement measurement) {
395         visited = measurement;
396     }
397 }
398
Popular Tags