KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.*;
38
39 import org.xml.sax.*;
40 import org.xml.sax.helpers.*;
41
42 public class TestMetricsConfigurationHandler extends TestCase {
43     private MetricsConfigurationHandler handler;
44     private XMLReader reader;
45     
46     protected void setUp() throws Exception JavaDoc {
47         handler = new MetricsConfigurationHandler();
48         
49         reader = XMLReaderFactory.createXMLReader();
50         reader.setDTDHandler(handler);
51         reader.setContentHandler(handler);
52         reader.setErrorHandler(handler);
53         if (Boolean.getBoolean("DEPENDENCYFINDER_TESTS_VALIDATE")) {
54             reader.setFeature("http://xml.org/sax/features/validation", true);
55             reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", true);
56         } else {
57             reader.setFeature("http://xml.org/sax/features/validation", false);
58             reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
59         }
60     }
61     
62     public void testEmptyFile() throws IOException, SAXException {
63         InputSource in = new InputSource(new StringReader(""));
64
65         try {
66             reader.parse(in);
67             fail("Read empty file");
68         } catch (SAXParseException ex) {
69             // Ignore
70
}
71
72         assertEquals("ProjectMeasurements", 0, handler.getMetricsConfiguration().getProjectMeasurements().size());
73         assertEquals("GroupMeasurements", 0, handler.getMetricsConfiguration().getGroupMeasurements().size());
74         assertEquals("ClassMeasurements", 0, handler.getMetricsConfiguration().getClassMeasurements().size());
75         assertEquals("MethodMeasurements", 0, handler.getMetricsConfiguration().getMethodMeasurements().size());
76     }
77
78     public void testEmptyDocument() throws IOException, SAXException {
79         InputSource in = new InputSource(new StringReader("<metrics-configuration/>"));
80
81         reader.parse(in);
82
83         assertEquals("ProjectMeasurements", 0, handler.getMetricsConfiguration().getProjectMeasurements().size());
84         assertEquals("GroupMeasurements", 0, handler.getMetricsConfiguration().getGroupMeasurements().size());
85         assertEquals("ClassMeasurements", 0, handler.getMetricsConfiguration().getClassMeasurements().size());
86         assertEquals("MethodMeasurements", 0, handler.getMetricsConfiguration().getMethodMeasurements().size());
87     }
88
89     public void testNonWellFormedDocument() throws IOException, SAXException {
90         InputSource in = new InputSource(new StringReader("<metrics-configuration>"));
91
92         try {
93             reader.parse(in);
94             fail("Read non well formed file");
95         } catch (SAXParseException ex) {
96             // Ignore
97
}
98
99         assertEquals("ProjectMeasurements", 0, handler.getMetricsConfiguration().getProjectMeasurements().size());
100         assertEquals("GroupMeasurements", 0, handler.getMetricsConfiguration().getGroupMeasurements().size());
101         assertEquals("ClassMeasurements", 0, handler.getMetricsConfiguration().getClassMeasurements().size());
102         assertEquals("MethodMeasurements", 0, handler.getMetricsConfiguration().getMethodMeasurements().size());
103     }
104
105     public void testValidation() throws IOException, SAXException {
106         StringBuffer JavaDoc document = new StringBuffer JavaDoc();
107
108         document.append("<!DOCTYPE metrics-configuration SYSTEM \"http://depfind.sourceforge.net/dtd/metrics-configuration.dtd\">\n");
109         document.append("<metrics-configuration>\n");
110         document.append(" <project-measurements/>\n");
111         document.append(" <group-measurements/>\n");
112         document.append(" <class-measurements/>\n");
113         document.append(" <method-measurements/>\n");
114         document.append("</metrics-configuration>\n");
115
116         InputSource in = new InputSource(new StringReader(document.toString()));
117
118         reader.parse(in);
119
120         assertEquals("ProjectMeasurements", 0, handler.getMetricsConfiguration().getProjectMeasurements().size());
121         assertEquals("GroupMeasurements", 0, handler.getMetricsConfiguration().getGroupMeasurements().size());
122         assertEquals("ClassMeasurements", 0, handler.getMetricsConfiguration().getClassMeasurements().size());
123         assertEquals("MethodMeasurements", 0, handler.getMetricsConfiguration().getMethodMeasurements().size());
124     }
125
126     public void testPackageMeasurement() throws IOException, SAXException {
127         StringBuffer JavaDoc document = new StringBuffer JavaDoc();
128
129         document.append("<!DOCTYPE metrics-configuration SYSTEM \"http://depfind.sourceforge.net/dtd/metrics-configuration.dtd\">\n");
130         document.append("<metrics-configuration>\n");
131         document.append(" <project-measurements>\n");
132         document.append(" <measurement>\n");
133         document.append(" <short-name>SLOC</short-name>\n");
134         document.append(" <long-name>Single Lines of Code</long-name>\n");
135         document.append(" <class>com.jeantessier.metrics.StatisticalMeasurement</class>\n");
136         document.append(" <init>\n");
137         document.append(" SLOC\n");
138         document.append(" DISPOSE_SUM\n");
139         document.append(" </init>\n");
140         document.append(" </measurement>\n");
141         document.append(" </project-measurements>\n");
142         document.append(" <group-measurements/>\n");
143         document.append(" <class-measurements/>\n");
144         document.append(" <method-measurements/>\n");
145         document.append("</metrics-configuration>\n");
146
147         InputSource in = new InputSource(new StringReader(document.toString()));
148
149         reader.parse(in);
150
151         assertEquals("ProjectMeasurements", 1, handler.getMetricsConfiguration().getProjectMeasurements().size());
152         assertEquals("GroupMeasurements", 0, handler.getMetricsConfiguration().getGroupMeasurements().size());
153         assertEquals("ClassMeasurements", 0, handler.getMetricsConfiguration().getClassMeasurements().size());
154         assertEquals("MethodMeasurements", 0, handler.getMetricsConfiguration().getMethodMeasurements().size());
155
156         MeasurementDescriptor descriptor = (MeasurementDescriptor) handler.getMetricsConfiguration().getProjectMeasurements().get(0);
157         assertEquals("SLOC", descriptor.getShortName());
158         assertEquals("Single Lines of Code", descriptor.getLongName());
159         assertEquals(com.jeantessier.metrics.StatisticalMeasurement.class, descriptor.getClassFor());
160         assertEquals("SLOC\n DISPOSE_SUM", descriptor.getInitText());
161         assertNull("descriptor.LowerThreshold()", descriptor.getLowerThreshold());
162         assertNull("descriptor.UpperThreshold()", descriptor.getUpperThreshold());
163     }
164
165     public void testGroupMeasurement() throws IOException, SAXException {
166         StringBuffer JavaDoc document = new StringBuffer JavaDoc();
167
168         document.append("<!DOCTYPE metrics-configuration SYSTEM \"http://depfind.sourceforge.net/dtd/metrics-configuration.dtd\">\n");
169         document.append("<metrics-configuration>\n");
170         document.append(" <project-measurements/>\n");
171         document.append(" <group-measurements>\n");
172         document.append(" <measurement>\n");
173         document.append(" <short-name>SLOC</short-name>\n");
174         document.append(" <long-name>Single Lines of Code</long-name>\n");
175         document.append(" <class>com.jeantessier.metrics.StatisticalMeasurement</class>\n");
176         document.append(" <init>\n");
177         document.append(" SLOC\n");
178         document.append(" DISPOSE_SUM\n");
179         document.append(" </init>\n");
180         document.append(" </measurement>\n");
181         document.append(" </group-measurements>\n");
182         document.append(" <class-measurements/>\n");
183         document.append(" <method-measurements/>\n");
184         document.append("</metrics-configuration>\n");
185
186         InputSource in = new InputSource(new StringReader(document.toString()));
187
188         reader.parse(in);
189
190         assertEquals("ProjectMeasurements", 0, handler.getMetricsConfiguration().getProjectMeasurements().size());
191         assertEquals("GroupMeasurements", 1, handler.getMetricsConfiguration().getGroupMeasurements().size());
192         assertEquals("ClassMeasurements", 0, handler.getMetricsConfiguration().getClassMeasurements().size());
193         assertEquals("MethodMeasurements", 0, handler.getMetricsConfiguration().getMethodMeasurements().size());
194
195         MeasurementDescriptor descriptor = (MeasurementDescriptor) handler.getMetricsConfiguration().getGroupMeasurements().get(0);
196         assertEquals("SLOC", descriptor.getShortName());
197         assertEquals("Single Lines of Code", descriptor.getLongName());
198         assertEquals(com.jeantessier.metrics.StatisticalMeasurement.class, descriptor.getClassFor());
199         assertEquals("SLOC\n DISPOSE_SUM", descriptor.getInitText());
200         assertNull("descriptor.LowerThreshold()", descriptor.getLowerThreshold());
201         assertNull("descriptor.UpperThreshold()", descriptor.getUpperThreshold());
202     }
203
204     public void testClassMeasurement() throws IOException, SAXException {
205         StringBuffer JavaDoc document = new StringBuffer JavaDoc();
206
207         document.append("<!DOCTYPE metrics-configuration SYSTEM \"http://depfind.sourceforge.net/dtd/metrics-configuration.dtd\">\n");
208         document.append("<metrics-configuration>\n");
209         document.append(" <project-measurements/>\n");
210         document.append(" <group-measurements/>\n");
211         document.append(" <class-measurements>\n");
212         document.append(" <measurement>\n");
213         document.append(" <short-name>SLOC</short-name>\n");
214         document.append(" <long-name>Single Lines of Code</long-name>\n");
215         document.append(" <class>com.jeantessier.metrics.StatisticalMeasurement</class>\n");
216         document.append(" <init>\n");
217         document.append(" SLOC\n");
218         document.append(" </init>\n");
219         document.append(" </measurement>\n");
220         document.append(" </class-measurements>\n");
221         document.append(" <method-measurements/>\n");
222         document.append("</metrics-configuration>\n");
223
224         InputSource in = new InputSource(new StringReader(document.toString()));
225
226         reader.parse(in);
227
228         assertEquals("ProjectMeasurements", 0, handler.getMetricsConfiguration().getProjectMeasurements().size());
229         assertEquals("GroupMeasurements", 0, handler.getMetricsConfiguration().getGroupMeasurements().size());
230         assertEquals("ClassMeasurements", 1, handler.getMetricsConfiguration().getClassMeasurements().size());
231         assertEquals("MethodMeasurements", 0, handler.getMetricsConfiguration().getMethodMeasurements().size());
232
233         MeasurementDescriptor descriptor = (MeasurementDescriptor) handler.getMetricsConfiguration().getClassMeasurements().get(0);
234         assertEquals("SLOC", descriptor.getShortName());
235         assertEquals("Single Lines of Code", descriptor.getLongName());
236         assertEquals(com.jeantessier.metrics.StatisticalMeasurement.class, descriptor.getClassFor());
237         assertEquals("SLOC", descriptor.getInitText());
238         assertNull("descriptor.LowerThreshold()", descriptor.getLowerThreshold());
239         assertNull("descriptor.UpperThreshold()", descriptor.getUpperThreshold());
240     }
241
242     public void testMethodMeasurement() throws IOException, SAXException {
243         StringBuffer JavaDoc document = new StringBuffer JavaDoc();
244
245         document.append("<!DOCTYPE metrics-configuration SYSTEM \"http://depfind.sourceforge.net/dtd/metrics-configuration.dtd\">\n");
246         document.append("<metrics-configuration>\n");
247         document.append(" <project-measurements/>\n");
248         document.append(" <group-measurements/>\n");
249         document.append(" <class-measurements/>\n");
250         document.append(" <method-measurements>\n");
251         document.append(" <measurement>\n");
252         document.append(" <short-name>SLOC</short-name>\n");
253         document.append(" <long-name>Single Lines of Code</long-name>\n");
254         document.append(" <class>com.jeantessier.metrics.CounterMeasurement</class>\n");
255         document.append(" <upper-threshold>50</upper-threshold>\n");
256         document.append(" </measurement>\n");
257         document.append(" </method-measurements>\n");
258         document.append("</metrics-configuration>\n");
259
260         InputSource in = new InputSource(new StringReader(document.toString()));
261
262         reader.parse(in);
263
264         assertEquals("ProjectMeasurements", 0, handler.getMetricsConfiguration().getProjectMeasurements().size());
265         assertEquals("GroupMeasurements", 0, handler.getMetricsConfiguration().getGroupMeasurements().size());
266         assertEquals("ClassMeasurements", 0, handler.getMetricsConfiguration().getClassMeasurements().size());
267         assertEquals("MethodMeasurements", 1, handler.getMetricsConfiguration().getMethodMeasurements().size());
268
269         MeasurementDescriptor descriptor = (MeasurementDescriptor) handler.getMetricsConfiguration().getMethodMeasurements().get(0);
270         assertEquals("SLOC", descriptor.getShortName());
271         assertEquals("Single Lines of Code", descriptor.getLongName());
272         assertEquals(com.jeantessier.metrics.CounterMeasurement.class, descriptor.getClassFor());
273         assertNull("descriptor.Init()", descriptor.getInitText());
274         assertNull("descriptor.LowerThreshold()", descriptor.getLowerThreshold());
275         assertEquals("descriptor.UpperThreshold()", "50", descriptor.getUpperThreshold());
276     }
277
278     public void testTrueVisible() throws IOException, SAXException {
279         StringBuffer JavaDoc document = new StringBuffer JavaDoc();
280
281         document.append("<!DOCTYPE metrics-configuration SYSTEM \"http://depfind.sourceforge.net/dtd/metrics-configuration.dtd\">\n");
282         document.append("<metrics-configuration>\n");
283         document.append(" <project-measurements/>\n");
284         document.append(" <group-measurements/>\n");
285         document.append(" <class-measurements/>\n");
286         document.append(" <method-measurements>\n");
287         document.append(" <measurement visible=\"true\">\n");
288         document.append(" <short-name>SLOC</short-name>\n");
289         document.append(" <long-name>Single Lines of Code</long-name>\n");
290         document.append(" <class>com.jeantessier.metrics.CounterMeasurement</class>\n");
291         document.append(" </measurement>\n");
292         document.append(" </method-measurements>\n");
293         document.append("</metrics-configuration>\n");
294
295         InputSource in = new InputSource(new StringReader(document.toString()));
296
297         reader.parse(in);
298
299         MeasurementDescriptor descriptor = (MeasurementDescriptor) handler.getMetricsConfiguration().getMethodMeasurements().get(0);
300         assertTrue("Not visible", descriptor.isVisible());
301     }
302
303     public void testYesVisible() throws IOException, SAXException {
304         StringBuffer JavaDoc document = new StringBuffer JavaDoc();
305
306         document.append("<!DOCTYPE metrics-configuration SYSTEM \"http://depfind.sourceforge.net/dtd/metrics-configuration.dtd\">\n");
307         document.append("<metrics-configuration>\n");
308         document.append(" <project-measurements/>\n");
309         document.append(" <group-measurements/>\n");
310         document.append(" <class-measurements/>\n");
311         document.append(" <method-measurements>\n");
312         document.append(" <measurement visible=\"yes\">\n");
313         document.append(" <short-name>SLOC</short-name>\n");
314         document.append(" <long-name>Single Lines of Code</long-name>\n");
315         document.append(" <class>com.jeantessier.metrics.CounterMeasurement</class>\n");
316         document.append(" </measurement>\n");
317         document.append(" </method-measurements>\n");
318         document.append("</metrics-configuration>\n");
319
320         InputSource in = new InputSource(new StringReader(document.toString()));
321
322         reader.parse(in);
323
324         MeasurementDescriptor descriptor = (MeasurementDescriptor) handler.getMetricsConfiguration().getMethodMeasurements().get(0);
325         assertTrue("Not visible", descriptor.isVisible());
326     }
327
328     public void testOnVisible() throws IOException, SAXException {
329         StringBuffer JavaDoc document = new StringBuffer JavaDoc();
330
331         document.append("<!DOCTYPE metrics-configuration SYSTEM \"http://depfind.sourceforge.net/dtd/metrics-configuration.dtd\">\n");
332         document.append("<metrics-configuration>\n");
333         document.append(" <project-measurements/>\n");
334         document.append(" <group-measurements/>\n");
335         document.append(" <class-measurements/>\n");
336         document.append(" <method-measurements>\n");
337         document.append(" <measurement visible=\"on\">\n");
338         document.append(" <short-name>SLOC</short-name>\n");
339         document.append(" <long-name>Single Lines of Code</long-name>\n");
340         document.append(" <class>com.jeantessier.metrics.CounterMeasurement</class>\n");
341         document.append(" </measurement>\n");
342         document.append(" </method-measurements>\n");
343         document.append("</metrics-configuration>\n");
344
345         InputSource in = new InputSource(new StringReader(document.toString()));
346
347         reader.parse(in);
348
349         MeasurementDescriptor descriptor = (MeasurementDescriptor) handler.getMetricsConfiguration().getMethodMeasurements().get(0);
350         assertTrue("Not visible", descriptor.isVisible());
351     }
352
353     public void testDefaultVisible() throws IOException, SAXException {
354         StringBuffer JavaDoc document = new StringBuffer JavaDoc();
355
356         document.append("<!DOCTYPE metrics-configuration SYSTEM \"http://depfind.sourceforge.net/dtd/metrics-configuration.dtd\">\n");
357         document.append("<metrics-configuration>\n");
358         document.append(" <project-measurements/>\n");
359         document.append(" <group-measurements/>\n");
360         document.append(" <class-measurements/>\n");
361         document.append(" <method-measurements>\n");
362         document.append(" <measurement>\n");
363         document.append(" <short-name>SLOC</short-name>\n");
364         document.append(" <long-name>Single Lines of Code</long-name>\n");
365         document.append(" <class>com.jeantessier.metrics.CounterMeasurement</class>\n");
366         document.append(" </measurement>\n");
367         document.append(" </method-measurements>\n");
368         document.append("</metrics-configuration>\n");
369
370         InputSource in = new InputSource(new StringReader(document.toString()));
371
372         reader.parse(in);
373
374         MeasurementDescriptor descriptor = (MeasurementDescriptor) handler.getMetricsConfiguration().getMethodMeasurements().get(0);
375         assertTrue("Not visible", descriptor.isVisible());
376     }
377
378     public void testNotVisible() throws IOException, SAXException {
379         StringBuffer JavaDoc document = new StringBuffer JavaDoc();
380
381         document.append("<!DOCTYPE metrics-configuration SYSTEM \"http://depfind.sourceforge.net/dtd/metrics-configuration.dtd\">\n");
382         document.append("<metrics-configuration>\n");
383         document.append(" <project-measurements/>\n");
384         document.append(" <group-measurements/>\n");
385         document.append(" <class-measurements/>\n");
386         document.append(" <method-measurements>\n");
387         document.append(" <measurement visible=\"no\">\n");
388         document.append(" <short-name>SLOC</short-name>\n");
389         document.append(" <long-name>Single Lines of Code</long-name>\n");
390         document.append(" <class>com.jeantessier.metrics.CounterMeasurement</class>\n");
391         document.append(" </measurement>\n");
392         document.append(" </method-measurements>\n");
393         document.append("</metrics-configuration>\n");
394
395         InputSource in = new InputSource(new StringReader(document.toString()));
396
397         reader.parse(in);
398
399         MeasurementDescriptor descriptor = (MeasurementDescriptor) handler.getMetricsConfiguration().getMethodMeasurements().get(0);
400         assertTrue("Visible", !descriptor.isVisible());
401     }
402
403     public void testGroupDefinitions() throws IOException, SAXException {
404         StringBuffer JavaDoc document = new StringBuffer JavaDoc();
405
406         document.append("<!DOCTYPE metrics-configuration SYSTEM \"http://depfind.sourceforge.net/dtd/metrics-configuration.dtd\">\n");
407         document.append("<metrics-configuration>\n");
408         document.append(" <project-measurements/>\n");
409         document.append(" <group-definitions>\n");
410         document.append(" <group-definition>\n");
411         document.append(" <name>Jean Tessier</name>\n");
412         document.append(" <pattern>/^com.jeantessier/</pattern>\n");
413         document.append(" </group-definition>\n");
414         document.append(" </group-definitions>\n");
415         document.append(" <group-measurements/>\n");
416         document.append(" <class-measurements/>\n");
417         document.append(" <method-measurements/>\n");
418         document.append("</metrics-configuration>\n");
419
420         InputSource in = new InputSource(new StringReader(document.toString()));
421
422         reader.parse(in);
423
424         assertEquals("ProjectMeasurements", 0, handler.getMetricsConfiguration().getProjectMeasurements().size());
425         assertEquals("GroupMeasurements", 0, handler.getMetricsConfiguration().getGroupMeasurements().size());
426         assertEquals("ClassMeasurements", 0, handler.getMetricsConfiguration().getClassMeasurements().size());
427         assertEquals("MethodMeasurements", 0, handler.getMetricsConfiguration().getMethodMeasurements().size());
428
429         assertEquals("groups for foobar", 0, handler.getMetricsConfiguration().getGroups("foobar").size());
430         assertEquals("groups for com.jeantessier.metrics", 1, handler.getMetricsConfiguration().getGroups("com.jeantessier.metrics").size());
431         assertEquals("Jean Tessier", handler.getMetricsConfiguration().getGroups("com.jeantessier.metrics").iterator().next());
432     }
433 }
434
Popular Tags