KickJava   Java API By Example, From Geeks To Geeks.

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


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 TestNameListMeasurement extends TestCase implements MeasurementVisitor {
38     private NameListMeasurement measurement;
39     private Measurement visited;
40     
41     protected void setUp() {
42         measurement = new NameListMeasurement(null, null, null);
43     }
44
45     public void testMeasurementDescriptor() throws Exception JavaDoc {
46         MeasurementDescriptor descriptor = new MeasurementDescriptor();
47         descriptor.setShortName("foo");
48         descriptor.setLongName("bar");
49         descriptor.setClassFor(NameListMeasurement.class);
50
51         measurement = (NameListMeasurement) descriptor.createMeasurement();
52         
53         assertNotNull(measurement.getDescriptor());
54         assertEquals(NameListMeasurement.class, measurement.getDescriptor().getClassFor());
55         assertEquals("foo", measurement.getShortName());
56         assertEquals("bar", measurement.getLongName());
57     }
58
59     public void testCreateFromMeasurementDescriptor() throws Exception JavaDoc {
60         MeasurementDescriptor descriptor = new MeasurementDescriptor();
61         descriptor.setShortName("foo");
62         descriptor.setLongName("bar");
63         descriptor.setClassFor(NameListMeasurement.class);
64
65         measurement = (NameListMeasurement) descriptor.createMeasurement();
66         
67         assertNotNull(measurement);
68         assertEquals(descriptor, measurement.getDescriptor());
69         assertSame(descriptor, measurement.getDescriptor());
70         assertEquals(NameListMeasurement.class, measurement.getClass());
71         assertEquals("foo", measurement.getShortName());
72         assertEquals("bar", measurement.getLongName());
73     }
74
75     public void testCreateSet() {
76         measurement = new NameListMeasurement(null, null, "SET");
77
78         measurement.add("abc");
79         measurement.add("abc");
80
81         assertEquals(1, measurement.intValue());
82     }
83     
84     public void testCreateList() {
85         measurement = new NameListMeasurement(null, null, "LIST");
86
87         measurement.add("abc");
88         measurement.add("abc");
89
90         assertEquals(2, measurement.intValue());
91     }
92
93     public void testCreateDefault() {
94         measurement.add("abc");
95         measurement.add("abc");
96
97         assertEquals(1, measurement.intValue());
98     }
99
100     public void testAddObject() {
101         Object JavaDoc o1 = new Object JavaDoc();
102         Object JavaDoc o2 = new Object JavaDoc();
103
104         assertEquals("zero", 0, measurement.intValue());
105         assertEquals("zero", 0.0, measurement.doubleValue(), 0.01);
106         assertEquals("zero", 0, measurement.getValue().intValue());
107
108         measurement.add(o1);
109         assertEquals("one", 1, measurement.intValue());
110         assertEquals("one", 1.0, measurement.doubleValue(), 0.01);
111         assertEquals("one", 1, measurement.getValue().intValue());
112
113         measurement.add(o2);
114         assertEquals("two", 2, measurement.intValue());
115         assertEquals("two", 2.0, measurement.doubleValue(), 0.01);
116         assertEquals("two", 2, measurement.getValue().intValue());
117
118         measurement.add(o1);
119         assertEquals("three", 2, measurement.intValue());
120         assertEquals("three", 2.0, measurement.doubleValue(), 0.01);
121         assertEquals("three", 2, measurement.getValue().intValue());
122     }
123
124     public void testValues() {
125         Object JavaDoc o1 = new Object JavaDoc();
126         Object JavaDoc o2 = new Object JavaDoc();
127
128         measurement.add(o1);
129         measurement.add(o2);
130
131         assertEquals("size", 2, measurement.getValues().size());
132         assertTrue("Missing o1", measurement.getValues().contains(o1));
133         assertTrue("Missing o2", measurement.getValues().contains(o2));
134
135         try {
136             measurement.getValues().add(o2);
137             fail("Was allowed to modify the Values() collection");
138         } catch (UnsupportedOperationException JavaDoc ex) {
139             // Ignore
140
}
141     }
142
143     public void testAddInt() {
144         assertEquals("zero", 0, measurement.intValue());
145         assertEquals("zero", 0.0, measurement.doubleValue(), 0.01);
146         assertEquals("zero", 0, measurement.getValue().intValue());
147
148         measurement.add(1);
149         assertEquals("one", 0, measurement.intValue());
150         assertEquals("one", 0.0, measurement.doubleValue(), 0.01);
151         assertEquals("one", 0, measurement.getValue().intValue());
152
153         measurement.add(1);
154         assertEquals("two", 0, measurement.intValue());
155         assertEquals("two", 0.0, measurement.doubleValue(), 0.01);
156         assertEquals("two", 0, measurement.getValue().intValue());
157     }
158
159     public void testAddFloat() {
160         assertEquals("zero", 0, measurement.intValue());
161         assertEquals("zero", 0.0, measurement.doubleValue(), 0.01);
162         assertEquals("zero", 0, measurement.getValue().intValue());
163
164         measurement.add(1.0);
165         assertEquals("one", 0, measurement.intValue());
166         assertEquals("one", 0.0, measurement.doubleValue(), 0.01);
167         assertEquals("one", 0, measurement.getValue().intValue());
168
169         measurement.add(1.0);
170         assertEquals("two", 0, measurement.intValue());
171         assertEquals("two", 0.0, measurement.doubleValue(), 0.01);
172         assertEquals("two", 0, measurement.getValue().intValue());
173     }
174
175     public void testInUndefinedRange() {
176         assertTrue(measurement.isInRange());
177
178         measurement.add(new Object JavaDoc());
179         
180         assertTrue(measurement.isInRange());
181
182         measurement.add(new Object JavaDoc());
183         measurement.add(new Object JavaDoc());
184
185         assertTrue(measurement.isInRange());
186     }
187
188     public void testInOpenRange() throws Exception JavaDoc {
189         MeasurementDescriptor descriptor = new MeasurementDescriptor();
190         descriptor.setShortName("foo");
191         descriptor.setLongName("bar");
192         descriptor.setClassFor(NameListMeasurement.class);
193
194         measurement = (NameListMeasurement) descriptor.createMeasurement();
195         
196         assertTrue(measurement.isInRange());
197
198         measurement.add(new Object JavaDoc());
199         
200         assertTrue(measurement.isInRange());
201
202         measurement.add(new Object JavaDoc());
203         measurement.add(new Object JavaDoc());
204
205         assertTrue(measurement.isInRange());
206     }
207
208     public void testInLowerBoundRange() throws Exception JavaDoc {
209         MeasurementDescriptor descriptor = new MeasurementDescriptor();
210         descriptor.setShortName("foo");
211         descriptor.setLongName("bar");
212         descriptor.setClassFor(NameListMeasurement.class);
213         descriptor.setLowerThreshold(new Integer JavaDoc(1));
214
215         measurement = (NameListMeasurement) descriptor.createMeasurement();
216         
217         assertTrue(!measurement.isInRange());
218
219         measurement.add(new Object JavaDoc());
220         
221         assertTrue(measurement.isInRange());
222
223         measurement.add(new Object JavaDoc());
224         measurement.add(new Object JavaDoc());
225         
226         assertTrue(measurement.isInRange());
227     }
228
229     public void testInUpperBoundRange() throws Exception JavaDoc {
230         MeasurementDescriptor descriptor = new MeasurementDescriptor();
231         descriptor.setShortName("foo");
232         descriptor.setLongName("bar");
233         descriptor.setClassFor(NameListMeasurement.class);
234         descriptor.setUpperThreshold(new Float JavaDoc(1.5));
235
236         measurement = (NameListMeasurement) descriptor.createMeasurement();
237         
238         assertTrue(measurement.isInRange());
239
240         measurement.add(new Object JavaDoc());
241         
242         assertTrue(measurement.isInRange());
243
244         measurement.add(new Object JavaDoc());
245         measurement.add(new Object JavaDoc());
246         
247         assertTrue(!measurement.isInRange());
248     }
249
250     public void testInBoundRange() throws Exception JavaDoc {
251         MeasurementDescriptor descriptor = new MeasurementDescriptor();
252         descriptor.setShortName("foo");
253         descriptor.setLongName("bar");
254         descriptor.setClassFor(NameListMeasurement.class);
255         descriptor.setLowerThreshold(new Integer JavaDoc(1));
256         descriptor.setUpperThreshold(new Float JavaDoc(1.5));
257
258         measurement = (NameListMeasurement) descriptor.createMeasurement();
259         
260         assertTrue(!measurement.isInRange());
261
262         measurement.add(new Object JavaDoc());
263         
264         assertTrue(measurement.isInRange());
265
266         measurement.add(new Object JavaDoc());
267         measurement.add(new Object JavaDoc());
268         
269         assertTrue(!measurement.isInRange());
270     }
271
272     public void testAccept() {
273         visited = null;
274         measurement.accept(this);
275         assertSame(measurement, visited);
276     }
277
278     public void testEmpty() throws Exception JavaDoc {
279         MeasurementDescriptor descriptor = new MeasurementDescriptor();
280         descriptor.setShortName("foo");
281         descriptor.setLongName("bar");
282         descriptor.setClassFor(NameListMeasurement.class);
283
284         measurement = (NameListMeasurement) descriptor.createMeasurement();
285         
286         assertTrue("Before Add()", measurement.isEmpty());
287
288         measurement.add("foo");
289
290         assertFalse("After Add()", measurement.isEmpty());
291     }
292     
293     public void visitStatisticalMeasurement(StatisticalMeasurement measurement) {
294         // Do nothing
295
}
296     
297     public void visitRatioMeasurement(RatioMeasurement measurement) {
298         // Do nothing
299
}
300     
301     public void visitNbSubMetricsMeasurement(NbSubMetricsMeasurement measurement) {
302         // Do nothing
303
}
304     
305     public void visitCounterMeasurement(CounterMeasurement measurement) {
306         // Do nothing
307
}
308     
309     public void visitContextAccumulatorMeasurement(ContextAccumulatorMeasurement measurement) {
310         // Do nothing
311
}
312         
313     public void visitNameListMeasurement(NameListMeasurement measurement) {
314         visited = measurement;
315     }
316     
317     public void visitSubMetricsAccumulatorMeasurement(SubMetricsAccumulatorMeasurement measurement) {
318         // Do nothing
319
}
320
321     public void visitSumMeasurement(SumMeasurement measurement) {
322         // Do nothing
323
}
324 }
325
Popular Tags