KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.*;
36
37 import org.apache.oro.text.perl.*;
38
39 import junit.framework.*;
40
41 public class TestNbSubMetricsMeasurementSelectionCriteria extends TestCase {
42     private Metrics metrics;
43     private MeasurementDescriptor descriptor;
44
45     private Metrics m1;
46     private Metrics m2;
47     private Metrics m3;
48     private Metrics m4;
49     private Metrics m5;
50     private Metrics m6;
51     
52     protected void setUp() throws Exception JavaDoc {
53         m1 = new Metrics("m1");
54         m2 = new Metrics("m2");
55         m3 = new Metrics("m3");
56         m4 = new Metrics("m4");
57         m5 = new Metrics("m5");
58         m6 = new Metrics("m6");
59         
60         MeasurementDescriptor present = new MeasurementDescriptor();
61         present.setShortName("P");
62         present.setLongName("present");
63         present.setClassFor(CounterMeasurement.class);
64
65         MeasurementDescriptor counter = new MeasurementDescriptor();
66         counter.setShortName("C");
67         counter.setLongName("counter");
68         counter.setClassFor(CounterMeasurement.class);
69
70         m1.track(present.createMeasurement(m1));
71
72         m2.track(present.createMeasurement(m2));
73         m2.track(counter.createMeasurement(m2));
74         m2.addToMeasurement("C", 0);
75
76         m3.track(counter.createMeasurement(m3));
77         m3.addToMeasurement("C", 1);
78
79         m4.track(counter.createMeasurement(m4));
80         m4.addToMeasurement("C", 2);
81
82         m5.track(counter.createMeasurement(m5));
83         m5.addToMeasurement("C", 3);
84
85         m6.track(counter.createMeasurement(m6));
86         m6.addToMeasurement("C", 4);
87
88         metrics = new Metrics("metrics");
89         
90         metrics.addSubMetrics(m1);
91         metrics.addSubMetrics(m2);
92         metrics.addSubMetrics(m3);
93         metrics.addSubMetrics(m4);
94         metrics.addSubMetrics(m5);
95         metrics.addSubMetrics(m6);
96
97         descriptor = new MeasurementDescriptor();
98         descriptor.setShortName("Nb");
99         descriptor.setLongName("Number");
100         descriptor.setClassFor(NbSubMetricsMeasurement.class);
101     }
102
103     public void testDefault() throws Exception JavaDoc {
104         NbSubMetricsMeasurement measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics);
105         assertEquals("default", 6, measurement.intValue());
106     }
107
108     public void testPresence() throws Exception JavaDoc {
109         descriptor.setInitText("P");
110
111         NbSubMetricsMeasurement measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics);
112         assertEquals("presence", 2, measurement.intValue());
113     }
114
115     public void testLesserThan() throws Exception JavaDoc {
116         descriptor.setInitText("C < 3");
117
118         NbSubMetricsMeasurement measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics);
119         assertEquals("lesser than", 3, measurement.intValue());
120     }
121
122     public void testLesserThanOrEqual() throws Exception JavaDoc {
123         descriptor.setInitText("C <= 3");
124
125         NbSubMetricsMeasurement measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics);
126         assertEquals("lesser than or equal", 4, measurement.intValue());
127     }
128
129     public void testGreaterThan() throws Exception JavaDoc {
130         descriptor.setInitText("C > 1");
131
132         NbSubMetricsMeasurement measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics);
133         assertEquals("greater than", 3, measurement.intValue());
134     }
135
136     public void testGreaterThanOrEqual() throws Exception JavaDoc {
137         descriptor.setInitText("C >= 1");
138
139         NbSubMetricsMeasurement measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics);
140         assertEquals("greater than or equal", 4, measurement.intValue());
141     }
142
143     public void testEqual() throws Exception JavaDoc {
144         descriptor.setInitText("C == 1");
145
146         NbSubMetricsMeasurement measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics);
147         assertEquals("equal", 1, measurement.intValue());
148     }
149
150     public void testNotEqual() throws Exception JavaDoc {
151         descriptor.setInitText("C != 1");
152
153         NbSubMetricsMeasurement measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics);
154         assertEquals("not equal", 4, measurement.intValue());
155     }
156
157     public void testAnd() throws Exception JavaDoc {
158         descriptor.setInitText("1 <= C <= 3");
159
160         NbSubMetricsMeasurement measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics);
161         assertEquals("and", 3, measurement.intValue());
162     }
163
164     public void testOr() throws Exception JavaDoc {
165         descriptor.setInitText("C == 1\nC == 2");
166
167         NbSubMetricsMeasurement measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics);
168         assertEquals("or", 2, measurement.intValue());
169     }
170
171     public void testSplit() {
172         String JavaDoc operators = "/(<)|(<=)|(>)|(>=)|(==)|(!=)/";
173         Perl5Util perl = new org.apache.oro.text.perl.Perl5Util();
174
175         List list = new ArrayList();
176         String JavaDoc str;
177
178         list.clear();
179         str = "";
180         perl.split(list, operators, str);
181         assertEquals("split(\"" + str + "\") expected [] but was " + list, 0, list.size());
182
183         list.clear();
184         str = "P";
185         perl.split(list, operators, str);
186         assertEquals("split(\"" + str + "\") expected [\"P\"] but was " + list, 1, list.size());
187         assertEquals("split(\"" + str + "\") expected [\"P\"] but was " + list, str, list.get(0));
188
189         list.clear();
190         str = "P > 0";
191         perl.split(list, operators, str);
192         assertEquals("split(\"" + str + "\") expected [\"P \", \">\", \" 0\"] but was " + list, 3, list.size());
193         assertEquals("split(\"" + str + "\") expected [\"P \", \">\", \" 0\"] but was " + list, "P ", list.get(0));
194         assertEquals("split(\"" + str + "\") expected [\"P \", \">\", \" 0\"] but was " + list, ">", list.get(1));
195         assertEquals("split(\"" + str + "\") expected [\"P \", \">\", \" 0\"] but was " + list, " 0", list.get(2));
196
197         list.clear();
198         str = "1 < P < 3";
199         perl.split(list, operators, str);
200         assertEquals("split(\"" + str + "\") expected [\"1 \", \"<\", \" P \", \"<\", \" 3\"] but was " + list, 5, list.size());
201         assertEquals("split(\"" + str + "\") expected [\"1 \", \"<\", \" P \", \"<\", \" 3\"] but was " + list, "1 ", list.get(0));
202         assertEquals("split(\"" + str + "\") expected [\"1 \", \"<\", \" P \", \"<\", \" 3\"] but was " + list, "<", list.get(1));
203         assertEquals("split(\"" + str + "\") expected [\"1 \", \"<\", \" P \", \"<\", \" 3\"] but was " + list, " P ", list.get(2));
204         assertEquals("split(\"" + str + "\") expected [\"1 \", \"<\", \" P \", \"<\", \" 3\"] but was " + list, "<", list.get(3));
205         assertEquals("split(\"" + str + "\") expected [\"1 \", \"<\", \" P \", \"<\", \" 3\"] but was " + list, " 3", list.get(4));
206
207         list.clear();
208         str = "1 < P DISPOSE_MEAN < 3";
209         perl.split(list, operators, str);
210         assertEquals("split(\"" + str + "\") expected [\"1 \", \"<\", \" P DISPOSE_MEAN \", \"<\", \" 3\"] but was " + list, 5, list.size());
211         assertEquals("split(\"" + str + "\") expected [\"1 \", \"<\", \" P DISPOSE_MEAN \", \"<\", \" 3\"] but was " + list, "1 ", list.get(0));
212         assertEquals("split(\"" + str + "\") expected [\"1 \", \"<\", \" P DISPOSE_MEAN \", \"<\", \" 3\"] but was " + list, "<", list.get(1));
213         assertEquals("split(\"" + str + "\") expected [\"1 \", \"<\", \" P DISPOSE_MEAN \", \"<\", \" 3\"] but was " + list, " P DISPOSE_MEAN ", list.get(2));
214         assertEquals("split(\"" + str + "\") expected [\"1 \", \"<\", \" P DISPOSE_MEAN \", \"<\", \" 3\"] but was " + list, "<", list.get(3));
215         assertEquals("split(\"" + str + "\") expected [\"1 \", \"<\", \" P DISPOSE_MEAN \", \"<\", \" 3\"] but was " + list, " 3", list.get(4));
216     }
217 }
218
Popular Tags