KickJava   Java API By Example, From Geeks To Geeks.

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


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 TestNbSubMetricsMeasurement extends TestCase implements MeasurementVisitor {
38     private MeasurementDescriptor descriptor;
39     private NbSubMetricsMeasurement measurement;
40     private Metrics metrics;
41     private Measurement visited;
42     
43     protected void setUp() {
44         metrics = new Metrics("foo");
45
46         descriptor = new MeasurementDescriptor();
47         descriptor.setShortName("foo");
48         descriptor.setLongName("bar");
49         descriptor.setClassFor(NbSubMetricsMeasurement.class);
50         descriptor.setCached(false);
51     }
52
53     public void testCreateFromMeasurementDescriptor() throws Exception JavaDoc {
54         measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement();
55         
56         assertNotNull(measurement);
57         assertEquals(descriptor, measurement.getDescriptor());
58         assertSame(descriptor, measurement.getDescriptor());
59         assertEquals(NbSubMetricsMeasurement.class, measurement.getClass());
60         assertEquals("foo", measurement.getShortName());
61         assertEquals("bar", measurement.getLongName());
62     }
63
64     public void testAddSubMetrics() throws Exception JavaDoc {
65         measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics);
66         
67         assertEquals(0, measurement.intValue());
68         assertEquals(0.0, measurement.doubleValue(), 0.01);
69         assertEquals(0, measurement.getValue().intValue());
70
71         metrics.addSubMetrics(new Metrics("bar"));
72
73         assertEquals(1, measurement.intValue());
74         assertEquals(1.0, measurement.doubleValue(), 0.01);
75         assertEquals(1, measurement.getValue().intValue());
76
77         metrics.addSubMetrics(new Metrics("bar"));
78
79         assertEquals(1, measurement.intValue());
80         assertEquals(1.0, measurement.doubleValue(), 0.01);
81         assertEquals(1, measurement.getValue().intValue());
82
83         metrics.addSubMetrics(new Metrics("baz"));
84
85         assertEquals(2, measurement.intValue());
86         assertEquals(2.0, measurement.doubleValue(), 0.01);
87         assertEquals(2, measurement.getValue().intValue());
88     }
89
90     public void testInUndefinedRange() throws Exception JavaDoc {
91         measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics);
92         
93         assertTrue(measurement.isInRange());
94
95         metrics.addSubMetrics(new Metrics("foo"));
96         
97         assertTrue(measurement.isInRange());
98
99         metrics.addSubMetrics(new Metrics("bar"));
100         metrics.addSubMetrics(new Metrics("baz"));
101
102         assertTrue(measurement.isInRange());
103     }
104
105     public void testInOpenRange() throws Exception JavaDoc {
106         measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics);
107         
108         assertTrue(measurement.isInRange());
109
110         metrics.addSubMetrics(new Metrics("foo"));
111         
112         assertTrue(measurement.isInRange());
113
114         metrics.addSubMetrics(new Metrics("bar"));
115         metrics.addSubMetrics(new Metrics("baz"));
116
117         assertTrue(measurement.isInRange());
118     }
119
120     public void testInLowerBoundRange() throws Exception JavaDoc {
121         descriptor.setLowerThreshold(new Integer JavaDoc(1));
122
123         measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics);
124         
125         assertTrue(!measurement.isInRange());
126
127         metrics.addSubMetrics(new Metrics("foo"));
128         
129         assertTrue(measurement.isInRange());
130
131         metrics.addSubMetrics(new Metrics("bar"));
132         metrics.addSubMetrics(new Metrics("baz"));
133         
134         assertTrue(measurement.isInRange());
135     }
136
137     public void testInUpperBoundRange() throws Exception JavaDoc {
138         descriptor.setUpperThreshold(new Float JavaDoc(1.5));
139
140         measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics);
141         
142         assertTrue(measurement.isInRange());
143
144         metrics.addSubMetrics(new Metrics("foo"));
145         
146         assertTrue(measurement.isInRange());
147
148         metrics.addSubMetrics(new Metrics("bar"));
149         metrics.addSubMetrics(new Metrics("baz"));
150         
151         assertTrue(!measurement.isInRange());
152     }
153
154     public void testInBoundRange() throws Exception JavaDoc {
155         descriptor.setLowerThreshold(new Integer JavaDoc(1));
156         descriptor.setUpperThreshold(new Float JavaDoc(1.5));
157
158         measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics);
159         
160         assertTrue(!measurement.isInRange());
161
162         metrics.addSubMetrics(new Metrics("foo"));
163         
164         assertTrue(measurement.isInRange());
165
166         metrics.addSubMetrics(new Metrics("bar"));
167         metrics.addSubMetrics(new Metrics("baz"));
168         
169         assertTrue(!measurement.isInRange());
170     }
171
172     public void testCachedValue() throws Exception JavaDoc {
173         descriptor.setCached(true);
174
175         measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics);
176
177         assertEquals("empty metrics", 0, measurement.intValue());
178         
179         metrics.addSubMetrics(new Metrics("foo"));
180         metrics.addSubMetrics(new Metrics("bar"));
181         metrics.addSubMetrics(new Metrics("baz"));
182
183         assertEquals("empty metrics", 0, measurement.intValue());
184     }
185
186     public void testAccept() throws Exception JavaDoc {
187         measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics);
188         
189         visited = null;
190         measurement.accept(this);
191         assertSame(measurement, visited);
192     }
193
194     public void testEmpty() throws Exception JavaDoc {
195         measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics);
196
197         assertTrue("Before AddSubMetrics()", measurement.isEmpty());
198         
199         metrics.addSubMetrics(new Metrics("foo"));
200
201         assertFalse("After AddSubMetrics()", measurement.isEmpty());
202     }
203     
204     public void visitStatisticalMeasurement(StatisticalMeasurement measurement) {
205         // Do nothing
206
}
207     
208     public void visitRatioMeasurement(RatioMeasurement measurement) {
209         // Do nothing
210
}
211     
212     public void visitNbSubMetricsMeasurement(NbSubMetricsMeasurement measurement) {
213         visited = measurement;
214     }
215     
216     public void visitCounterMeasurement(CounterMeasurement measurement) {
217         // Do nothing
218
}
219     
220     public void visitContextAccumulatorMeasurement(ContextAccumulatorMeasurement measurement) {
221         // Do nothing
222
}
223     
224     public void visitNameListMeasurement(NameListMeasurement measurement) {
225         // Do nothing
226
}
227     
228     public void visitSubMetricsAccumulatorMeasurement(SubMetricsAccumulatorMeasurement measurement) {
229         // Do nothing
230
}
231     
232     public void visitSumMeasurement(SumMeasurement measurement) {
233         // Do nothing
234
}
235 }
236
Popular Tags