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 java.util.*;
036:
037: import org.apache.oro.text.perl.*;
038:
039: import junit.framework.*;
040:
041: public class TestNbSubMetricsMeasurementSelectionCriteria extends
042: TestCase {
043: private Metrics metrics;
044: private MeasurementDescriptor descriptor;
045:
046: private Metrics m1;
047: private Metrics m2;
048: private Metrics m3;
049: private Metrics m4;
050: private Metrics m5;
051: private Metrics m6;
052:
053: protected void setUp() throws Exception {
054: m1 = new Metrics("m1");
055: m2 = new Metrics("m2");
056: m3 = new Metrics("m3");
057: m4 = new Metrics("m4");
058: m5 = new Metrics("m5");
059: m6 = new Metrics("m6");
060:
061: MeasurementDescriptor present = new MeasurementDescriptor();
062: present.setShortName("P");
063: present.setLongName("present");
064: present.setClassFor(CounterMeasurement.class);
065:
066: MeasurementDescriptor counter = new MeasurementDescriptor();
067: counter.setShortName("C");
068: counter.setLongName("counter");
069: counter.setClassFor(CounterMeasurement.class);
070:
071: m1.track(present.createMeasurement(m1));
072:
073: m2.track(present.createMeasurement(m2));
074: m2.track(counter.createMeasurement(m2));
075: m2.addToMeasurement("C", 0);
076:
077: m3.track(counter.createMeasurement(m3));
078: m3.addToMeasurement("C", 1);
079:
080: m4.track(counter.createMeasurement(m4));
081: m4.addToMeasurement("C", 2);
082:
083: m5.track(counter.createMeasurement(m5));
084: m5.addToMeasurement("C", 3);
085:
086: m6.track(counter.createMeasurement(m6));
087: m6.addToMeasurement("C", 4);
088:
089: metrics = new Metrics("metrics");
090:
091: metrics.addSubMetrics(m1);
092: metrics.addSubMetrics(m2);
093: metrics.addSubMetrics(m3);
094: metrics.addSubMetrics(m4);
095: metrics.addSubMetrics(m5);
096: metrics.addSubMetrics(m6);
097:
098: descriptor = new MeasurementDescriptor();
099: descriptor.setShortName("Nb");
100: descriptor.setLongName("Number");
101: descriptor.setClassFor(NbSubMetricsMeasurement.class);
102: }
103:
104: public void testDefault() throws Exception {
105: NbSubMetricsMeasurement measurement = (NbSubMetricsMeasurement) descriptor
106: .createMeasurement(metrics);
107: assertEquals("default", 6, measurement.intValue());
108: }
109:
110: public void testPresence() throws Exception {
111: descriptor.setInitText("P");
112:
113: NbSubMetricsMeasurement measurement = (NbSubMetricsMeasurement) descriptor
114: .createMeasurement(metrics);
115: assertEquals("presence", 2, measurement.intValue());
116: }
117:
118: public void testLesserThan() throws Exception {
119: descriptor.setInitText("C < 3");
120:
121: NbSubMetricsMeasurement measurement = (NbSubMetricsMeasurement) descriptor
122: .createMeasurement(metrics);
123: assertEquals("lesser than", 3, measurement.intValue());
124: }
125:
126: public void testLesserThanOrEqual() throws Exception {
127: descriptor.setInitText("C <= 3");
128:
129: NbSubMetricsMeasurement measurement = (NbSubMetricsMeasurement) descriptor
130: .createMeasurement(metrics);
131: assertEquals("lesser than or equal", 4, measurement.intValue());
132: }
133:
134: public void testGreaterThan() throws Exception {
135: descriptor.setInitText("C > 1");
136:
137: NbSubMetricsMeasurement measurement = (NbSubMetricsMeasurement) descriptor
138: .createMeasurement(metrics);
139: assertEquals("greater than", 3, measurement.intValue());
140: }
141:
142: public void testGreaterThanOrEqual() throws Exception {
143: descriptor.setInitText("C >= 1");
144:
145: NbSubMetricsMeasurement measurement = (NbSubMetricsMeasurement) descriptor
146: .createMeasurement(metrics);
147: assertEquals("greater than or equal", 4, measurement.intValue());
148: }
149:
150: public void testEqual() throws Exception {
151: descriptor.setInitText("C == 1");
152:
153: NbSubMetricsMeasurement measurement = (NbSubMetricsMeasurement) descriptor
154: .createMeasurement(metrics);
155: assertEquals("equal", 1, measurement.intValue());
156: }
157:
158: public void testNotEqual() throws Exception {
159: descriptor.setInitText("C != 1");
160:
161: NbSubMetricsMeasurement measurement = (NbSubMetricsMeasurement) descriptor
162: .createMeasurement(metrics);
163: assertEquals("not equal", 4, measurement.intValue());
164: }
165:
166: public void testAnd() throws Exception {
167: descriptor.setInitText("1 <= C <= 3");
168:
169: NbSubMetricsMeasurement measurement = (NbSubMetricsMeasurement) descriptor
170: .createMeasurement(metrics);
171: assertEquals("and", 3, measurement.intValue());
172: }
173:
174: public void testOr() throws Exception {
175: descriptor.setInitText("C == 1\nC == 2");
176:
177: NbSubMetricsMeasurement measurement = (NbSubMetricsMeasurement) descriptor
178: .createMeasurement(metrics);
179: assertEquals("or", 2, measurement.intValue());
180: }
181:
182: public void testSplit() {
183: String operators = "/(<)|(<=)|(>)|(>=)|(==)|(!=)/";
184: Perl5Util perl = new org.apache.oro.text.perl.Perl5Util();
185:
186: List list = new ArrayList();
187: String str;
188:
189: list.clear();
190: str = "";
191: perl.split(list, operators, str);
192: assertEquals("split(\"" + str + "\") expected [] but was "
193: + list, 0, list.size());
194:
195: list.clear();
196: str = "P";
197: perl.split(list, operators, str);
198: assertEquals("split(\"" + str + "\") expected [\"P\"] but was "
199: + list, 1, list.size());
200: assertEquals("split(\"" + str + "\") expected [\"P\"] but was "
201: + list, str, list.get(0));
202:
203: list.clear();
204: str = "P > 0";
205: perl.split(list, operators, str);
206: assertEquals("split(\"" + str
207: + "\") expected [\"P \", \">\", \" 0\"] but was "
208: + list, 3, list.size());
209: assertEquals("split(\"" + str
210: + "\") expected [\"P \", \">\", \" 0\"] but was "
211: + list, "P ", list.get(0));
212: assertEquals("split(\"" + str
213: + "\") expected [\"P \", \">\", \" 0\"] but was "
214: + list, ">", list.get(1));
215: assertEquals("split(\"" + str
216: + "\") expected [\"P \", \">\", \" 0\"] but was "
217: + list, " 0", list.get(2));
218:
219: list.clear();
220: str = "1 < P < 3";
221: perl.split(list, operators, str);
222: assertEquals(
223: "split(\""
224: + str
225: + "\") expected [\"1 \", \"<\", \" P \", \"<\", \" 3\"] but was "
226: + list, 5, list.size());
227: assertEquals(
228: "split(\""
229: + str
230: + "\") expected [\"1 \", \"<\", \" P \", \"<\", \" 3\"] but was "
231: + list, "1 ", list.get(0));
232: assertEquals(
233: "split(\""
234: + str
235: + "\") expected [\"1 \", \"<\", \" P \", \"<\", \" 3\"] but was "
236: + list, "<", list.get(1));
237: assertEquals(
238: "split(\""
239: + str
240: + "\") expected [\"1 \", \"<\", \" P \", \"<\", \" 3\"] but was "
241: + list, " P ", list.get(2));
242: assertEquals(
243: "split(\""
244: + str
245: + "\") expected [\"1 \", \"<\", \" P \", \"<\", \" 3\"] but was "
246: + list, "<", list.get(3));
247: assertEquals(
248: "split(\""
249: + str
250: + "\") expected [\"1 \", \"<\", \" P \", \"<\", \" 3\"] but was "
251: + list, " 3", list.get(4));
252:
253: list.clear();
254: str = "1 < P DISPOSE_MEAN < 3";
255: perl.split(list, operators, str);
256: assertEquals(
257: "split(\""
258: + str
259: + "\") expected [\"1 \", \"<\", \" P DISPOSE_MEAN \", \"<\", \" 3\"] but was "
260: + list, 5, list.size());
261: assertEquals(
262: "split(\""
263: + str
264: + "\") expected [\"1 \", \"<\", \" P DISPOSE_MEAN \", \"<\", \" 3\"] but was "
265: + list, "1 ", list.get(0));
266: assertEquals(
267: "split(\""
268: + str
269: + "\") expected [\"1 \", \"<\", \" P DISPOSE_MEAN \", \"<\", \" 3\"] but was "
270: + list, "<", list.get(1));
271: assertEquals(
272: "split(\""
273: + str
274: + "\") expected [\"1 \", \"<\", \" P DISPOSE_MEAN \", \"<\", \" 3\"] but was "
275: + list, " P DISPOSE_MEAN ", list.get(2));
276: assertEquals(
277: "split(\""
278: + str
279: + "\") expected [\"1 \", \"<\", \" P DISPOSE_MEAN \", \"<\", \" 3\"] but was "
280: + list, "<", list.get(3));
281: assertEquals(
282: "split(\""
283: + str
284: + "\") expected [\"1 \", \"<\", \" P DISPOSE_MEAN \", \"<\", \" 3\"] but was "
285: + list, " 3", list.get(4));
286: }
287: }
|