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: import java.io.*;
038:
039: import javax.xml.parsers.*;
040:
041: import org.xml.sax.*;
042:
043: public class TestMetricsConfigurationLoader extends TestCase {
044: private MetricsConfiguration configuration;
045: private MetricsConfigurationLoader loader;
046:
047: protected void setUp() throws Exception {
048: configuration = new MetricsConfiguration();
049: loader = new MetricsConfigurationLoader(configuration, Boolean
050: .getBoolean("DEPENDENCYFINDER_TESTS_VALIDATE"));
051: }
052:
053: public void testEmptyFile() throws IOException, SAXException,
054: ParserConfigurationException {
055: Reader in = new StringReader("");
056:
057: try {
058: loader.load(in);
059: fail("Read empty file");
060: } catch (SAXParseException ex) {
061: // Ignore
062: }
063:
064: assertEquals("ProjectMeasurements", 0, configuration
065: .getProjectMeasurements().size());
066: assertEquals("GroupMeasurements", 0, configuration
067: .getGroupMeasurements().size());
068: assertEquals("ClassMeasurements", 0, configuration
069: .getClassMeasurements().size());
070: assertEquals("MethodMeasurements", 0, configuration
071: .getMethodMeasurements().size());
072: }
073:
074: public void testEmptyDocument() throws IOException, SAXException,
075: ParserConfigurationException {
076: Reader in = new StringReader("<metrics-configuration/>");
077:
078: MetricsConfiguration configuration2 = loader.load(in);
079: assertSame(configuration, configuration2);
080:
081: assertEquals("ProjectMeasurements", 0, configuration
082: .getProjectMeasurements().size());
083: assertEquals("GroupMeasurements", 0, configuration
084: .getGroupMeasurements().size());
085: assertEquals("ClassMeasurements", 0, configuration
086: .getClassMeasurements().size());
087: assertEquals("MethodMeasurements", 0, configuration
088: .getMethodMeasurements().size());
089: }
090:
091: public void testNonWellFormedDocument() throws IOException,
092: SAXException, ParserConfigurationException {
093: Reader in = new StringReader("<metrics-configuration>");
094:
095: try {
096: loader.load(in);
097: fail("Read non well formed file");
098: } catch (SAXParseException ex) {
099: // Ignore
100: }
101:
102: assertEquals("ProjectMeasurements", 0, configuration
103: .getProjectMeasurements().size());
104: assertEquals("GroupMeasurements", 0, configuration
105: .getGroupMeasurements().size());
106: assertEquals("ClassMeasurements", 0, configuration
107: .getClassMeasurements().size());
108: assertEquals("MethodMeasurements", 0, configuration
109: .getMethodMeasurements().size());
110: }
111:
112: public void testValidation() throws IOException, SAXException,
113: ParserConfigurationException {
114: StringBuffer document = new StringBuffer();
115:
116: document
117: .append("<!DOCTYPE metrics-configuration SYSTEM \"http://depfind.sourceforge.net/dtd/metrics-configuration.dtd\">\n");
118: document.append("<metrics-configuration>\n");
119: document.append(" <project-measurements/>\n");
120: document.append(" <group-measurements/>\n");
121: document.append(" <class-measurements/>\n");
122: document.append(" <method-measurements/>\n");
123: document.append("</metrics-configuration>\n");
124:
125: Reader in = new StringReader(document.toString());
126:
127: MetricsConfiguration configuration2 = loader.load(in);
128: assertSame(configuration, configuration2);
129:
130: assertEquals("ProjectMeasurements", 0, configuration
131: .getProjectMeasurements().size());
132: assertEquals("GroupMeasurements", 0, configuration
133: .getGroupMeasurements().size());
134: assertEquals("ClassMeasurements", 0, configuration
135: .getClassMeasurements().size());
136: assertEquals("MethodMeasurements", 0, configuration
137: .getMethodMeasurements().size());
138: }
139:
140: public void testPackageMeasurement() throws IOException,
141: SAXException, ParserConfigurationException {
142: StringBuffer document = new StringBuffer();
143:
144: document
145: .append("<!DOCTYPE metrics-configuration SYSTEM \"http://depfind.sourceforge.net/dtd/metrics-configuration.dtd\">\n");
146: document.append("<metrics-configuration>\n");
147: document.append(" <project-measurements>\n");
148: document.append(" <measurement>\n");
149: document.append(" <short-name>SLOC</short-name>\n");
150: document
151: .append(" <long-name>Single Lines of Code</long-name>\n");
152: document
153: .append(" <class>com.jeantessier.metrics.StatisticalMeasurement</class>\n");
154: document.append(" <init>\n");
155: document.append(" SLOC\n");
156: document.append(" DISPOSE_SUM\n");
157: document.append(" </init>\n");
158: document.append(" </measurement>\n");
159: document.append(" </project-measurements>\n");
160: document.append(" <group-measurements/>\n");
161: document.append(" <class-measurements/>\n");
162: document.append(" <method-measurements/>\n");
163: document.append("</metrics-configuration>\n");
164:
165: Reader in = new StringReader(document.toString());
166:
167: MetricsConfiguration configuration2 = loader.load(in);
168: assertSame(configuration, configuration2);
169:
170: assertEquals("ProjectMeasurements", 1, configuration
171: .getProjectMeasurements().size());
172: assertEquals("GroupMeasurements", 0, configuration
173: .getGroupMeasurements().size());
174: assertEquals("ClassMeasurements", 0, configuration
175: .getClassMeasurements().size());
176: assertEquals("MethodMeasurements", 0, configuration
177: .getMethodMeasurements().size());
178:
179: MeasurementDescriptor descriptor = (MeasurementDescriptor) configuration
180: .getProjectMeasurements().get(0);
181: assertEquals("SLOC", descriptor.getShortName());
182: assertEquals("Single Lines of Code", descriptor.getLongName());
183: assertEquals(
184: com.jeantessier.metrics.StatisticalMeasurement.class,
185: descriptor.getClassFor());
186: assertEquals("SLOC\n DISPOSE_SUM", descriptor
187: .getInitText());
188: assertNull("descriptor.LowerThreshold()", descriptor
189: .getLowerThreshold());
190: assertNull("descriptor.UpperThreshold()", descriptor
191: .getUpperThreshold());
192: }
193:
194: public void testGroupMeasurement() throws IOException,
195: SAXException, ParserConfigurationException {
196: StringBuffer document = new StringBuffer();
197:
198: document
199: .append("<!DOCTYPE metrics-configuration SYSTEM \"http://depfind.sourceforge.net/dtd/metrics-configuration.dtd\">\n");
200: document.append("<metrics-configuration>\n");
201: document.append(" <project-measurements/>\n");
202: document.append(" <group-measurements>\n");
203: document.append(" <measurement>\n");
204: document.append(" <short-name>SLOC</short-name>\n");
205: document
206: .append(" <long-name>Single Lines of Code</long-name>\n");
207: document
208: .append(" <class>com.jeantessier.metrics.StatisticalMeasurement</class>\n");
209: document.append(" <init>\n");
210: document.append(" SLOC\n");
211: document.append(" DISPOSE_SUM\n");
212: document.append(" </init>\n");
213: document.append(" </measurement>\n");
214: document.append(" </group-measurements>\n");
215: document.append(" <class-measurements/>\n");
216: document.append(" <method-measurements/>\n");
217: document.append("</metrics-configuration>\n");
218:
219: Reader in = new StringReader(document.toString());
220:
221: MetricsConfiguration configuration2 = loader.load(in);
222: assertSame(configuration, configuration2);
223:
224: assertEquals("ProjectMeasurements", 0, configuration
225: .getProjectMeasurements().size());
226: assertEquals("GroupMeasurements", 1, configuration
227: .getGroupMeasurements().size());
228: assertEquals("ClassMeasurements", 0, configuration
229: .getClassMeasurements().size());
230: assertEquals("MethodMeasurements", 0, configuration
231: .getMethodMeasurements().size());
232:
233: MeasurementDescriptor descriptor = (MeasurementDescriptor) configuration
234: .getGroupMeasurements().get(0);
235: assertEquals("SLOC", descriptor.getShortName());
236: assertEquals("Single Lines of Code", descriptor.getLongName());
237: assertEquals(
238: com.jeantessier.metrics.StatisticalMeasurement.class,
239: descriptor.getClassFor());
240: assertEquals("SLOC\n DISPOSE_SUM", descriptor
241: .getInitText());
242: assertNull("descriptor.LowerThreshold()", descriptor
243: .getLowerThreshold());
244: assertNull("descriptor.UpperThreshold()", descriptor
245: .getUpperThreshold());
246: }
247:
248: public void testClassMeasurement() throws IOException,
249: SAXException, ParserConfigurationException {
250: StringBuffer document = new StringBuffer();
251:
252: document
253: .append("<!DOCTYPE metrics-configuration SYSTEM \"http://depfind.sourceforge.net/dtd/metrics-configuration.dtd\">\n");
254: document.append("<metrics-configuration>\n");
255: document.append(" <project-measurements/>\n");
256: document.append(" <group-measurements/>\n");
257: document.append(" <class-measurements>\n");
258: document.append(" <measurement>\n");
259: document.append(" <short-name>SLOC</short-name>\n");
260: document
261: .append(" <long-name>Single Lines of Code</long-name>\n");
262: document
263: .append(" <class>com.jeantessier.metrics.StatisticalMeasurement</class>\n");
264: document.append(" <init>\n");
265: document.append(" SLOC\n");
266: document.append(" </init>\n");
267: document.append(" </measurement>\n");
268: document.append(" </class-measurements>\n");
269: document.append(" <method-measurements/>\n");
270: document.append("</metrics-configuration>\n");
271:
272: Reader in = new StringReader(document.toString());
273:
274: MetricsConfiguration configuration2 = loader.load(in);
275: assertSame(configuration, configuration2);
276:
277: assertEquals("ProjectMeasurements", 0, configuration
278: .getProjectMeasurements().size());
279: assertEquals("GroupMeasurements", 0, configuration
280: .getGroupMeasurements().size());
281: assertEquals("ClassMeasurements", 1, configuration
282: .getClassMeasurements().size());
283: assertEquals("MethodMeasurements", 0, configuration
284: .getMethodMeasurements().size());
285:
286: MeasurementDescriptor descriptor = (MeasurementDescriptor) configuration
287: .getClassMeasurements().get(0);
288: assertEquals("SLOC", descriptor.getShortName());
289: assertEquals("Single Lines of Code", descriptor.getLongName());
290: assertEquals(
291: com.jeantessier.metrics.StatisticalMeasurement.class,
292: descriptor.getClassFor());
293: assertEquals("SLOC", descriptor.getInitText());
294: assertNull("descriptor.LowerThreshold()", descriptor
295: .getLowerThreshold());
296: assertNull("descriptor.UpperThreshold()", descriptor
297: .getUpperThreshold());
298: }
299:
300: public void testMethodMeasurement() throws IOException,
301: SAXException, ParserConfigurationException {
302: StringBuffer document = new StringBuffer();
303:
304: document
305: .append("<!DOCTYPE metrics-configuration SYSTEM \"http://depfind.sourceforge.net/dtd/metrics-configuration.dtd\">\n");
306: document.append("<metrics-configuration>\n");
307: document.append(" <project-measurements/>\n");
308: document.append(" <group-measurements/>\n");
309: document.append(" <class-measurements/>\n");
310: document.append(" <method-measurements>\n");
311: document.append(" <measurement>\n");
312: document.append(" <short-name>SLOC</short-name>\n");
313: document
314: .append(" <long-name>Single Lines of Code</long-name>\n");
315: document
316: .append(" <class>com.jeantessier.metrics.CounterMeasurement</class>\n");
317: document
318: .append(" <upper-threshold>50</upper-threshold>\n");
319: document.append(" </measurement>\n");
320: document.append(" </method-measurements>\n");
321: document.append("</metrics-configuration>\n");
322:
323: Reader in = new StringReader(document.toString());
324:
325: MetricsConfiguration configuration2 = loader.load(in);
326: assertSame(configuration, configuration2);
327:
328: assertEquals("ProjectMeasurements", 0, configuration
329: .getProjectMeasurements().size());
330: assertEquals("GroupMeasurements", 0, configuration
331: .getGroupMeasurements().size());
332: assertEquals("ClassMeasurements", 0, configuration
333: .getClassMeasurements().size());
334: assertEquals("MethodMeasurements", 1, configuration
335: .getMethodMeasurements().size());
336:
337: MeasurementDescriptor descriptor = (MeasurementDescriptor) configuration
338: .getMethodMeasurements().get(0);
339: assertEquals("SLOC", descriptor.getShortName());
340: assertEquals("Single Lines of Code", descriptor.getLongName());
341: assertEquals(com.jeantessier.metrics.CounterMeasurement.class,
342: descriptor.getClassFor());
343: assertNull("descriptor.Init()", descriptor.getInitText());
344: assertNull("descriptor.LowerThreshold()", descriptor
345: .getLowerThreshold());
346: assertEquals("descriptor.UpperThreshold()", "50", descriptor
347: .getUpperThreshold());
348: }
349:
350: public void testGroupDefinitions() throws IOException,
351: SAXException, ParserConfigurationException {
352: StringBuffer document = new StringBuffer();
353:
354: document
355: .append("<!DOCTYPE metrics-configuration SYSTEM \"http://depfind.sourceforge.net/dtd/metrics-configuration.dtd\">\n");
356: document.append("<metrics-configuration>\n");
357: document.append(" <project-measurements/>\n");
358: document.append(" <group-definitions>\n");
359: document.append(" <group-definition>\n");
360: document.append(" <name>Jean Tessier</name>\n");
361: document
362: .append(" <pattern>/^com.jeantessier/</pattern>\n");
363: document.append(" </group-definition>\n");
364: document.append(" </group-definitions>\n");
365: document.append(" <group-measurements/>\n");
366: document.append(" <class-measurements/>\n");
367: document.append(" <method-measurements/>\n");
368: document.append("</metrics-configuration>\n");
369:
370: Reader in = new StringReader(document.toString());
371:
372: MetricsConfiguration configuration2 = loader.load(in);
373: assertSame(configuration, configuration2);
374:
375: assertEquals("ProjectMeasurements", 0, configuration
376: .getProjectMeasurements().size());
377: assertEquals("GroupMeasurements", 0, configuration
378: .getGroupMeasurements().size());
379: assertEquals("ClassMeasurements", 0, configuration
380: .getClassMeasurements().size());
381: assertEquals("MethodMeasurements", 0, configuration
382: .getMethodMeasurements().size());
383:
384: assertEquals("groups for foobar", 0, configuration.getGroups(
385: "foobar").size());
386: assertEquals("groups for com.jeantessier.metrics", 1,
387: configuration.getGroups("com.jeantessier.metrics")
388: .size());
389: assertEquals("Jean Tessier", configuration.getGroups(
390: "com.jeantessier.metrics").iterator().next());
391: }
392: }
|