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 TestNameListMeasurement extends TestCase implements
038: MeasurementVisitor {
039: private NameListMeasurement measurement;
040: private Measurement visited;
041:
042: protected void setUp() {
043: measurement = new NameListMeasurement(null, null, null);
044: }
045:
046: public void testMeasurementDescriptor() throws Exception {
047: MeasurementDescriptor descriptor = new MeasurementDescriptor();
048: descriptor.setShortName("foo");
049: descriptor.setLongName("bar");
050: descriptor.setClassFor(NameListMeasurement.class);
051:
052: measurement = (NameListMeasurement) descriptor
053: .createMeasurement();
054:
055: assertNotNull(measurement.getDescriptor());
056: assertEquals(NameListMeasurement.class, measurement
057: .getDescriptor().getClassFor());
058: assertEquals("foo", measurement.getShortName());
059: assertEquals("bar", measurement.getLongName());
060: }
061:
062: public void testCreateFromMeasurementDescriptor() throws Exception {
063: MeasurementDescriptor descriptor = new MeasurementDescriptor();
064: descriptor.setShortName("foo");
065: descriptor.setLongName("bar");
066: descriptor.setClassFor(NameListMeasurement.class);
067:
068: measurement = (NameListMeasurement) descriptor
069: .createMeasurement();
070:
071: assertNotNull(measurement);
072: assertEquals(descriptor, measurement.getDescriptor());
073: assertSame(descriptor, measurement.getDescriptor());
074: assertEquals(NameListMeasurement.class, measurement.getClass());
075: assertEquals("foo", measurement.getShortName());
076: assertEquals("bar", measurement.getLongName());
077: }
078:
079: public void testCreateSet() {
080: measurement = new NameListMeasurement(null, null, "SET");
081:
082: measurement.add("abc");
083: measurement.add("abc");
084:
085: assertEquals(1, measurement.intValue());
086: }
087:
088: public void testCreateList() {
089: measurement = new NameListMeasurement(null, null, "LIST");
090:
091: measurement.add("abc");
092: measurement.add("abc");
093:
094: assertEquals(2, measurement.intValue());
095: }
096:
097: public void testCreateDefault() {
098: measurement.add("abc");
099: measurement.add("abc");
100:
101: assertEquals(1, measurement.intValue());
102: }
103:
104: public void testAddObject() {
105: Object o = new Object();
106:
107: assertEquals("zero", 0, measurement.intValue());
108: assertEquals("zero", 0.0, measurement.doubleValue(), 0.01);
109: assertEquals("zero", 0, measurement.getValue().intValue());
110: assertEquals("zero", 0, measurement.getValues().size());
111: assertTrue("zero", measurement.isEmpty());
112:
113: measurement.add(o);
114:
115: assertEquals("one", 0, measurement.intValue());
116: assertEquals("one", 0.0, measurement.doubleValue(), 0.01);
117: assertEquals("one", 0, measurement.getValue().intValue());
118: assertEquals("zero", 0, measurement.getValues().size());
119: assertTrue("zero", measurement.isEmpty());
120: }
121:
122: public void testAddString() {
123: String s1 = "foo";
124: String s2 = "bar";
125:
126: assertEquals("zero", 0, measurement.intValue());
127: assertEquals("zero", 0.0, measurement.doubleValue(), 0.01);
128: assertEquals("zero", 0, measurement.getValue().intValue());
129:
130: measurement.add(s1);
131: assertEquals("one", 1, measurement.intValue());
132: assertEquals("one", 1.0, measurement.doubleValue(), 0.01);
133: assertEquals("one", 1, measurement.getValue().intValue());
134:
135: measurement.add(s2);
136: assertEquals("two", 2, measurement.intValue());
137: assertEquals("two", 2.0, measurement.doubleValue(), 0.01);
138: assertEquals("two", 2, measurement.getValue().intValue());
139:
140: measurement.add(s1);
141: assertEquals("three", 2, measurement.intValue());
142: assertEquals("three", 2.0, measurement.doubleValue(), 0.01);
143: assertEquals("three", 2, measurement.getValue().intValue());
144: }
145:
146: public void testValues() {
147: String s1 = "foo";
148: String s2 = "bar";
149:
150: measurement.add(s1);
151: measurement.add(s2);
152:
153: assertEquals("size", 2, measurement.getValues().size());
154: assertTrue("Missing s1", measurement.getValues().contains(s1));
155: assertTrue("Missing s2", measurement.getValues().contains(s2));
156:
157: try {
158: measurement.getValues().add(s2);
159: fail("Was allowed to modify the Values() collection");
160: } catch (UnsupportedOperationException ex) {
161: // Ignore
162: }
163: }
164:
165: public void testAddInt() {
166: assertEquals("zero", 0, measurement.intValue());
167: assertEquals("zero", 0.0, measurement.doubleValue(), 0.01);
168: assertEquals("zero", 0, measurement.getValue().intValue());
169:
170: measurement.add(1);
171: assertEquals("one", 0, measurement.intValue());
172: assertEquals("one", 0.0, measurement.doubleValue(), 0.01);
173: assertEquals("one", 0, measurement.getValue().intValue());
174:
175: measurement.add(1);
176: assertEquals("two", 0, measurement.intValue());
177: assertEquals("two", 0.0, measurement.doubleValue(), 0.01);
178: assertEquals("two", 0, measurement.getValue().intValue());
179: }
180:
181: public void testAddFloat() {
182: assertEquals("zero", 0, measurement.intValue());
183: assertEquals("zero", 0.0, measurement.doubleValue(), 0.01);
184: assertEquals("zero", 0, measurement.getValue().intValue());
185:
186: measurement.add(1.0);
187: assertEquals("one", 0, measurement.intValue());
188: assertEquals("one", 0.0, measurement.doubleValue(), 0.01);
189: assertEquals("one", 0, measurement.getValue().intValue());
190:
191: measurement.add(1.0);
192: assertEquals("two", 0, measurement.intValue());
193: assertEquals("two", 0.0, measurement.doubleValue(), 0.01);
194: assertEquals("two", 0, measurement.getValue().intValue());
195: }
196:
197: public void testInUndefinedRange() {
198: assertTrue(measurement.isInRange());
199:
200: measurement.add("foo");
201:
202: assertTrue(measurement.isInRange());
203:
204: measurement.add("bar");
205: measurement.add("baz");
206:
207: assertTrue(measurement.isInRange());
208: }
209:
210: public void testInOpenRange() throws Exception {
211: MeasurementDescriptor descriptor = new MeasurementDescriptor();
212: descriptor.setShortName("foo");
213: descriptor.setLongName("bar");
214: descriptor.setClassFor(NameListMeasurement.class);
215:
216: measurement = (NameListMeasurement) descriptor
217: .createMeasurement();
218:
219: assertTrue(measurement.isInRange());
220:
221: measurement.add("foo");
222:
223: assertTrue(measurement.isInRange());
224:
225: measurement.add("bar");
226: measurement.add("baz");
227:
228: assertTrue(measurement.isInRange());
229: }
230:
231: public void testInLowerBoundRange() throws Exception {
232: MeasurementDescriptor descriptor = new MeasurementDescriptor();
233: descriptor.setShortName("foo");
234: descriptor.setLongName("bar");
235: descriptor.setClassFor(NameListMeasurement.class);
236: descriptor.setLowerThreshold(1);
237:
238: measurement = (NameListMeasurement) descriptor
239: .createMeasurement();
240:
241: assertTrue(!measurement.isInRange());
242:
243: measurement.add("foo");
244:
245: assertTrue(measurement.isInRange());
246:
247: measurement.add("bar");
248: measurement.add("baz");
249:
250: assertTrue(measurement.isInRange());
251: }
252:
253: public void testInUpperBoundRange() throws Exception {
254: MeasurementDescriptor descriptor = new MeasurementDescriptor();
255: descriptor.setShortName("foo");
256: descriptor.setLongName("bar");
257: descriptor.setClassFor(NameListMeasurement.class);
258: descriptor.setUpperThreshold(new Float(1.5));
259:
260: measurement = (NameListMeasurement) descriptor
261: .createMeasurement();
262:
263: assertTrue(measurement.isInRange());
264:
265: measurement.add("foo");
266:
267: assertTrue(measurement.isInRange());
268:
269: measurement.add("bar");
270: measurement.add("baz");
271:
272: assertTrue(!measurement.isInRange());
273: }
274:
275: public void testInBoundRange() throws Exception {
276: MeasurementDescriptor descriptor = new MeasurementDescriptor();
277: descriptor.setShortName("foo");
278: descriptor.setLongName("bar");
279: descriptor.setClassFor(NameListMeasurement.class);
280: descriptor.setLowerThreshold(1);
281: descriptor.setUpperThreshold(1.5);
282:
283: measurement = (NameListMeasurement) descriptor
284: .createMeasurement();
285:
286: assertTrue(!measurement.isInRange());
287:
288: measurement.add("foo");
289:
290: assertTrue(measurement.isInRange());
291:
292: measurement.add("bar");
293: measurement.add("baz");
294:
295: assertTrue(!measurement.isInRange());
296: }
297:
298: public void testAccept() {
299: visited = null;
300: measurement.accept(this );
301: assertSame(measurement, visited);
302: }
303:
304: public void testEmpty() throws Exception {
305: MeasurementDescriptor descriptor = new MeasurementDescriptor();
306: descriptor.setShortName("foo");
307: descriptor.setLongName("bar");
308: descriptor.setClassFor(NameListMeasurement.class);
309:
310: measurement = (NameListMeasurement) descriptor
311: .createMeasurement();
312:
313: assertTrue("Before Add()", measurement.isEmpty());
314:
315: measurement.add("foo");
316:
317: assertFalse("After Add()", measurement.isEmpty());
318: }
319:
320: public void visitStatisticalMeasurement(
321: StatisticalMeasurement measurement) {
322: // Do nothing
323: }
324:
325: public void visitRatioMeasurement(RatioMeasurement measurement) {
326: // Do nothing
327: }
328:
329: public void visitNbSubMetricsMeasurement(
330: NbSubMetricsMeasurement measurement) {
331: // Do nothing
332: }
333:
334: public void visitCounterMeasurement(CounterMeasurement measurement) {
335: // Do nothing
336: }
337:
338: public void visitContextAccumulatorMeasurement(
339: ContextAccumulatorMeasurement measurement) {
340: // Do nothing
341: }
342:
343: public void visitNameListMeasurement(NameListMeasurement measurement) {
344: visited = measurement;
345: }
346:
347: public void visitSubMetricsAccumulatorMeasurement(
348: SubMetricsAccumulatorMeasurement measurement) {
349: // Do nothing
350: }
351:
352: public void visitSumMeasurement(SumMeasurement measurement) {
353: // Do nothing
354: }
355: }
|