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 TestNbSubMetricsMeasurement extends TestCase implements
038: MeasurementVisitor {
039: private MeasurementDescriptor descriptor;
040: private NbSubMetricsMeasurement measurement;
041: private Metrics metrics;
042: private Measurement visited;
043:
044: protected void setUp() {
045: metrics = new Metrics("foo");
046:
047: descriptor = new MeasurementDescriptor();
048: descriptor.setShortName("foo");
049: descriptor.setLongName("bar");
050: descriptor.setClassFor(NbSubMetricsMeasurement.class);
051: descriptor.setCached(false);
052: }
053:
054: public void testCreateFromMeasurementDescriptor() throws Exception {
055: measurement = (NbSubMetricsMeasurement) descriptor
056: .createMeasurement();
057:
058: assertNotNull(measurement);
059: assertEquals(descriptor, measurement.getDescriptor());
060: assertSame(descriptor, measurement.getDescriptor());
061: assertEquals(NbSubMetricsMeasurement.class, measurement
062: .getClass());
063: assertEquals("foo", measurement.getShortName());
064: assertEquals("bar", measurement.getLongName());
065: }
066:
067: public void testAddSubMetrics() throws Exception {
068: measurement = (NbSubMetricsMeasurement) descriptor
069: .createMeasurement(metrics);
070:
071: assertEquals(0, measurement.intValue());
072: assertEquals(0.0, measurement.doubleValue(), 0.01);
073: assertEquals(0, measurement.getValue().intValue());
074:
075: metrics.addSubMetrics(new Metrics("bar"));
076:
077: assertEquals(1, measurement.intValue());
078: assertEquals(1.0, measurement.doubleValue(), 0.01);
079: assertEquals(1, measurement.getValue().intValue());
080:
081: metrics.addSubMetrics(new Metrics("bar"));
082:
083: assertEquals(1, measurement.intValue());
084: assertEquals(1.0, measurement.doubleValue(), 0.01);
085: assertEquals(1, measurement.getValue().intValue());
086:
087: metrics.addSubMetrics(new Metrics("baz"));
088:
089: assertEquals(2, measurement.intValue());
090: assertEquals(2.0, measurement.doubleValue(), 0.01);
091: assertEquals(2, measurement.getValue().intValue());
092: }
093:
094: public void testInUndefinedRange() throws Exception {
095: measurement = (NbSubMetricsMeasurement) descriptor
096: .createMeasurement(metrics);
097:
098: assertTrue(measurement.isInRange());
099:
100: metrics.addSubMetrics(new Metrics("foo"));
101:
102: assertTrue(measurement.isInRange());
103:
104: metrics.addSubMetrics(new Metrics("bar"));
105: metrics.addSubMetrics(new Metrics("baz"));
106:
107: assertTrue(measurement.isInRange());
108: }
109:
110: public void testInOpenRange() throws Exception {
111: measurement = (NbSubMetricsMeasurement) descriptor
112: .createMeasurement(metrics);
113:
114: assertTrue(measurement.isInRange());
115:
116: metrics.addSubMetrics(new Metrics("foo"));
117:
118: assertTrue(measurement.isInRange());
119:
120: metrics.addSubMetrics(new Metrics("bar"));
121: metrics.addSubMetrics(new Metrics("baz"));
122:
123: assertTrue(measurement.isInRange());
124: }
125:
126: public void testInLowerBoundRange() throws Exception {
127: descriptor.setLowerThreshold(new Integer(1));
128:
129: measurement = (NbSubMetricsMeasurement) descriptor
130: .createMeasurement(metrics);
131:
132: assertTrue(!measurement.isInRange());
133:
134: metrics.addSubMetrics(new Metrics("foo"));
135:
136: assertTrue(measurement.isInRange());
137:
138: metrics.addSubMetrics(new Metrics("bar"));
139: metrics.addSubMetrics(new Metrics("baz"));
140:
141: assertTrue(measurement.isInRange());
142: }
143:
144: public void testInUpperBoundRange() throws Exception {
145: descriptor.setUpperThreshold(new Float(1.5));
146:
147: measurement = (NbSubMetricsMeasurement) descriptor
148: .createMeasurement(metrics);
149:
150: assertTrue(measurement.isInRange());
151:
152: metrics.addSubMetrics(new Metrics("foo"));
153:
154: assertTrue(measurement.isInRange());
155:
156: metrics.addSubMetrics(new Metrics("bar"));
157: metrics.addSubMetrics(new Metrics("baz"));
158:
159: assertTrue(!measurement.isInRange());
160: }
161:
162: public void testInBoundRange() throws Exception {
163: descriptor.setLowerThreshold(new Integer(1));
164: descriptor.setUpperThreshold(new Float(1.5));
165:
166: measurement = (NbSubMetricsMeasurement) descriptor
167: .createMeasurement(metrics);
168:
169: assertTrue(!measurement.isInRange());
170:
171: metrics.addSubMetrics(new Metrics("foo"));
172:
173: assertTrue(measurement.isInRange());
174:
175: metrics.addSubMetrics(new Metrics("bar"));
176: metrics.addSubMetrics(new Metrics("baz"));
177:
178: assertTrue(!measurement.isInRange());
179: }
180:
181: public void testCachedValue() throws Exception {
182: descriptor.setCached(true);
183:
184: measurement = (NbSubMetricsMeasurement) descriptor
185: .createMeasurement(metrics);
186:
187: assertEquals("empty metrics", 0, measurement.intValue());
188:
189: metrics.addSubMetrics(new Metrics("foo"));
190: metrics.addSubMetrics(new Metrics("bar"));
191: metrics.addSubMetrics(new Metrics("baz"));
192:
193: assertEquals("empty metrics", 0, measurement.intValue());
194: }
195:
196: public void testAccept() throws Exception {
197: measurement = (NbSubMetricsMeasurement) descriptor
198: .createMeasurement(metrics);
199:
200: visited = null;
201: measurement.accept(this );
202: assertSame(measurement, visited);
203: }
204:
205: public void testEmpty() throws Exception {
206: measurement = (NbSubMetricsMeasurement) descriptor
207: .createMeasurement(metrics);
208:
209: assertTrue("Before AddSubMetrics()", measurement.isEmpty());
210:
211: metrics.addSubMetrics(new Metrics("foo"));
212:
213: assertFalse("After AddSubMetrics()", measurement.isEmpty());
214: }
215:
216: public void visitStatisticalMeasurement(
217: StatisticalMeasurement measurement) {
218: // Do nothing
219: }
220:
221: public void visitRatioMeasurement(RatioMeasurement measurement) {
222: // Do nothing
223: }
224:
225: public void visitNbSubMetricsMeasurement(
226: NbSubMetricsMeasurement measurement) {
227: visited = measurement;
228: }
229:
230: public void visitCounterMeasurement(CounterMeasurement measurement) {
231: // Do nothing
232: }
233:
234: public void visitContextAccumulatorMeasurement(
235: ContextAccumulatorMeasurement measurement) {
236: // Do nothing
237: }
238:
239: public void visitNameListMeasurement(NameListMeasurement measurement) {
240: // Do nothing
241: }
242:
243: public void visitSubMetricsAccumulatorMeasurement(
244: SubMetricsAccumulatorMeasurement measurement) {
245: // Do nothing
246: }
247:
248: public void visitSumMeasurement(SumMeasurement measurement) {
249: // Do nothing
250: }
251: }
|