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.io.*;
036: import javax.xml.parsers.*;
037:
038: import junit.framework.*;
039: import org.xml.sax.*;
040:
041: public class TestMetricsConfigurationHandler extends TestCase {
042: private MetricsConfigurationHandler handler;
043: private XMLReader reader;
044:
045: protected void setUp() throws Exception {
046: handler = new MetricsConfigurationHandler();
047:
048: reader = SAXParserFactory.newInstance().newSAXParser()
049: .getXMLReader();
050: reader.setDTDHandler(handler);
051: reader.setContentHandler(handler);
052: reader.setErrorHandler(handler);
053: if (Boolean.getBoolean("DEPENDENCYFINDER_TESTS_VALIDATE")) {
054: reader.setFeature("http://xml.org/sax/features/validation",
055: true);
056: reader
057: .setFeature(
058: "http://apache.org/xml/features/nonvalidating/load-external-dtd",
059: true);
060: } else {
061: reader.setFeature("http://xml.org/sax/features/validation",
062: false);
063: reader
064: .setFeature(
065: "http://apache.org/xml/features/nonvalidating/load-external-dtd",
066: false);
067: }
068: }
069:
070: public void testEmptyFile() throws IOException, SAXException {
071: InputSource in = new InputSource(new StringReader(""));
072:
073: try {
074: reader.parse(in);
075: fail("Read empty file");
076: } catch (SAXParseException ex) {
077: // Ignore
078: }
079:
080: assertEquals("ProjectMeasurements", 0, handler
081: .getMetricsConfiguration().getProjectMeasurements()
082: .size());
083: assertEquals("GroupMeasurements", 0, handler
084: .getMetricsConfiguration().getGroupMeasurements()
085: .size());
086: assertEquals("ClassMeasurements", 0, handler
087: .getMetricsConfiguration().getClassMeasurements()
088: .size());
089: assertEquals("MethodMeasurements", 0, handler
090: .getMetricsConfiguration().getMethodMeasurements()
091: .size());
092: }
093:
094: public void testEmptyDocument() throws IOException, SAXException {
095: InputSource in = new InputSource(new StringReader(
096: "<metrics-configuration/>"));
097:
098: reader.parse(in);
099:
100: assertEquals("ProjectMeasurements", 0, handler
101: .getMetricsConfiguration().getProjectMeasurements()
102: .size());
103: assertEquals("GroupMeasurements", 0, handler
104: .getMetricsConfiguration().getGroupMeasurements()
105: .size());
106: assertEquals("ClassMeasurements", 0, handler
107: .getMetricsConfiguration().getClassMeasurements()
108: .size());
109: assertEquals("MethodMeasurements", 0, handler
110: .getMetricsConfiguration().getMethodMeasurements()
111: .size());
112: }
113:
114: public void testNonWellFormedDocument() throws IOException,
115: SAXException {
116: InputSource in = new InputSource(new StringReader(
117: "<metrics-configuration>"));
118:
119: try {
120: reader.parse(in);
121: fail("Read non well formed file");
122: } catch (SAXParseException ex) {
123: // Ignore
124: }
125:
126: assertEquals("ProjectMeasurements", 0, handler
127: .getMetricsConfiguration().getProjectMeasurements()
128: .size());
129: assertEquals("GroupMeasurements", 0, handler
130: .getMetricsConfiguration().getGroupMeasurements()
131: .size());
132: assertEquals("ClassMeasurements", 0, handler
133: .getMetricsConfiguration().getClassMeasurements()
134: .size());
135: assertEquals("MethodMeasurements", 0, handler
136: .getMetricsConfiguration().getMethodMeasurements()
137: .size());
138: }
139:
140: public void testValidation() throws IOException, SAXException {
141: StringBuffer document = new StringBuffer();
142:
143: document
144: .append("<!DOCTYPE metrics-configuration SYSTEM \"http://depfind.sourceforge.net/dtd/metrics-configuration.dtd\">\n");
145: document.append("<metrics-configuration>\n");
146: document.append(" <project-measurements/>\n");
147: document.append(" <group-measurements/>\n");
148: document.append(" <class-measurements/>\n");
149: document.append(" <method-measurements/>\n");
150: document.append("</metrics-configuration>\n");
151:
152: InputSource in = new InputSource(new StringReader(document
153: .toString()));
154:
155: reader.parse(in);
156:
157: assertEquals("ProjectMeasurements", 0, handler
158: .getMetricsConfiguration().getProjectMeasurements()
159: .size());
160: assertEquals("GroupMeasurements", 0, handler
161: .getMetricsConfiguration().getGroupMeasurements()
162: .size());
163: assertEquals("ClassMeasurements", 0, handler
164: .getMetricsConfiguration().getClassMeasurements()
165: .size());
166: assertEquals("MethodMeasurements", 0, handler
167: .getMetricsConfiguration().getMethodMeasurements()
168: .size());
169: }
170:
171: public void testPackageMeasurement() throws IOException,
172: SAXException {
173: StringBuffer document = new StringBuffer();
174:
175: document
176: .append("<!DOCTYPE metrics-configuration SYSTEM \"http://depfind.sourceforge.net/dtd/metrics-configuration.dtd\">\n");
177: document.append("<metrics-configuration>\n");
178: document.append(" <project-measurements>\n");
179: document.append(" <measurement>\n");
180: document.append(" <short-name>SLOC</short-name>\n");
181: document
182: .append(" <long-name>Single Lines of Code</long-name>\n");
183: document
184: .append(" <class>com.jeantessier.metrics.StatisticalMeasurement</class>\n");
185: document.append(" <init>\n");
186: document.append(" SLOC\n");
187: document.append(" DISPOSE_SUM\n");
188: document.append(" </init>\n");
189: document.append(" </measurement>\n");
190: document.append(" </project-measurements>\n");
191: document.append(" <group-measurements/>\n");
192: document.append(" <class-measurements/>\n");
193: document.append(" <method-measurements/>\n");
194: document.append("</metrics-configuration>\n");
195:
196: InputSource in = new InputSource(new StringReader(document
197: .toString()));
198:
199: reader.parse(in);
200:
201: assertEquals("ProjectMeasurements", 1, handler
202: .getMetricsConfiguration().getProjectMeasurements()
203: .size());
204: assertEquals("GroupMeasurements", 0, handler
205: .getMetricsConfiguration().getGroupMeasurements()
206: .size());
207: assertEquals("ClassMeasurements", 0, handler
208: .getMetricsConfiguration().getClassMeasurements()
209: .size());
210: assertEquals("MethodMeasurements", 0, handler
211: .getMetricsConfiguration().getMethodMeasurements()
212: .size());
213:
214: MeasurementDescriptor descriptor = (MeasurementDescriptor) handler
215: .getMetricsConfiguration().getProjectMeasurements()
216: .get(0);
217: assertEquals("SLOC", descriptor.getShortName());
218: assertEquals("Single Lines of Code", descriptor.getLongName());
219: assertEquals(
220: com.jeantessier.metrics.StatisticalMeasurement.class,
221: descriptor.getClassFor());
222: assertEquals("SLOC\n DISPOSE_SUM", descriptor
223: .getInitText());
224: assertNull("descriptor.LowerThreshold()", descriptor
225: .getLowerThreshold());
226: assertNull("descriptor.UpperThreshold()", descriptor
227: .getUpperThreshold());
228: }
229:
230: public void testGroupMeasurement() throws IOException, SAXException {
231: StringBuffer document = new StringBuffer();
232:
233: document
234: .append("<!DOCTYPE metrics-configuration SYSTEM \"http://depfind.sourceforge.net/dtd/metrics-configuration.dtd\">\n");
235: document.append("<metrics-configuration>\n");
236: document.append(" <project-measurements/>\n");
237: document.append(" <group-measurements>\n");
238: document.append(" <measurement>\n");
239: document.append(" <short-name>SLOC</short-name>\n");
240: document
241: .append(" <long-name>Single Lines of Code</long-name>\n");
242: document
243: .append(" <class>com.jeantessier.metrics.StatisticalMeasurement</class>\n");
244: document.append(" <init>\n");
245: document.append(" SLOC\n");
246: document.append(" DISPOSE_SUM\n");
247: document.append(" </init>\n");
248: document.append(" </measurement>\n");
249: document.append(" </group-measurements>\n");
250: document.append(" <class-measurements/>\n");
251: document.append(" <method-measurements/>\n");
252: document.append("</metrics-configuration>\n");
253:
254: InputSource in = new InputSource(new StringReader(document
255: .toString()));
256:
257: reader.parse(in);
258:
259: assertEquals("ProjectMeasurements", 0, handler
260: .getMetricsConfiguration().getProjectMeasurements()
261: .size());
262: assertEquals("GroupMeasurements", 1, handler
263: .getMetricsConfiguration().getGroupMeasurements()
264: .size());
265: assertEquals("ClassMeasurements", 0, handler
266: .getMetricsConfiguration().getClassMeasurements()
267: .size());
268: assertEquals("MethodMeasurements", 0, handler
269: .getMetricsConfiguration().getMethodMeasurements()
270: .size());
271:
272: MeasurementDescriptor descriptor = (MeasurementDescriptor) handler
273: .getMetricsConfiguration().getGroupMeasurements()
274: .get(0);
275: assertEquals("SLOC", descriptor.getShortName());
276: assertEquals("Single Lines of Code", descriptor.getLongName());
277: assertEquals(
278: com.jeantessier.metrics.StatisticalMeasurement.class,
279: descriptor.getClassFor());
280: assertEquals("SLOC\n DISPOSE_SUM", descriptor
281: .getInitText());
282: assertNull("descriptor.LowerThreshold()", descriptor
283: .getLowerThreshold());
284: assertNull("descriptor.UpperThreshold()", descriptor
285: .getUpperThreshold());
286: }
287:
288: public void testClassMeasurement() throws IOException, SAXException {
289: StringBuffer document = new StringBuffer();
290:
291: document
292: .append("<!DOCTYPE metrics-configuration SYSTEM \"http://depfind.sourceforge.net/dtd/metrics-configuration.dtd\">\n");
293: document.append("<metrics-configuration>\n");
294: document.append(" <project-measurements/>\n");
295: document.append(" <group-measurements/>\n");
296: document.append(" <class-measurements>\n");
297: document.append(" <measurement>\n");
298: document.append(" <short-name>SLOC</short-name>\n");
299: document
300: .append(" <long-name>Single Lines of Code</long-name>\n");
301: document
302: .append(" <class>com.jeantessier.metrics.StatisticalMeasurement</class>\n");
303: document.append(" <init>\n");
304: document.append(" SLOC\n");
305: document.append(" </init>\n");
306: document.append(" </measurement>\n");
307: document.append(" </class-measurements>\n");
308: document.append(" <method-measurements/>\n");
309: document.append("</metrics-configuration>\n");
310:
311: InputSource in = new InputSource(new StringReader(document
312: .toString()));
313:
314: reader.parse(in);
315:
316: assertEquals("ProjectMeasurements", 0, handler
317: .getMetricsConfiguration().getProjectMeasurements()
318: .size());
319: assertEquals("GroupMeasurements", 0, handler
320: .getMetricsConfiguration().getGroupMeasurements()
321: .size());
322: assertEquals("ClassMeasurements", 1, handler
323: .getMetricsConfiguration().getClassMeasurements()
324: .size());
325: assertEquals("MethodMeasurements", 0, handler
326: .getMetricsConfiguration().getMethodMeasurements()
327: .size());
328:
329: MeasurementDescriptor descriptor = (MeasurementDescriptor) handler
330: .getMetricsConfiguration().getClassMeasurements()
331: .get(0);
332: assertEquals("SLOC", descriptor.getShortName());
333: assertEquals("Single Lines of Code", descriptor.getLongName());
334: assertEquals(
335: com.jeantessier.metrics.StatisticalMeasurement.class,
336: descriptor.getClassFor());
337: assertEquals("SLOC", descriptor.getInitText());
338: assertNull("descriptor.LowerThreshold()", descriptor
339: .getLowerThreshold());
340: assertNull("descriptor.UpperThreshold()", descriptor
341: .getUpperThreshold());
342: }
343:
344: public void testMethodMeasurement() throws IOException,
345: SAXException {
346: StringBuffer document = new StringBuffer();
347:
348: document
349: .append("<!DOCTYPE metrics-configuration SYSTEM \"http://depfind.sourceforge.net/dtd/metrics-configuration.dtd\">\n");
350: document.append("<metrics-configuration>\n");
351: document.append(" <project-measurements/>\n");
352: document.append(" <group-measurements/>\n");
353: document.append(" <class-measurements/>\n");
354: document.append(" <method-measurements>\n");
355: document.append(" <measurement>\n");
356: document.append(" <short-name>SLOC</short-name>\n");
357: document
358: .append(" <long-name>Single Lines of Code</long-name>\n");
359: document
360: .append(" <class>com.jeantessier.metrics.CounterMeasurement</class>\n");
361: document
362: .append(" <upper-threshold>50</upper-threshold>\n");
363: document.append(" </measurement>\n");
364: document.append(" </method-measurements>\n");
365: document.append("</metrics-configuration>\n");
366:
367: InputSource in = new InputSource(new StringReader(document
368: .toString()));
369:
370: reader.parse(in);
371:
372: assertEquals("ProjectMeasurements", 0, handler
373: .getMetricsConfiguration().getProjectMeasurements()
374: .size());
375: assertEquals("GroupMeasurements", 0, handler
376: .getMetricsConfiguration().getGroupMeasurements()
377: .size());
378: assertEquals("ClassMeasurements", 0, handler
379: .getMetricsConfiguration().getClassMeasurements()
380: .size());
381: assertEquals("MethodMeasurements", 1, handler
382: .getMetricsConfiguration().getMethodMeasurements()
383: .size());
384:
385: MeasurementDescriptor descriptor = (MeasurementDescriptor) handler
386: .getMetricsConfiguration().getMethodMeasurements().get(
387: 0);
388: assertEquals("SLOC", descriptor.getShortName());
389: assertEquals("Single Lines of Code", descriptor.getLongName());
390: assertEquals(com.jeantessier.metrics.CounterMeasurement.class,
391: descriptor.getClassFor());
392: assertNull("descriptor.Init()", descriptor.getInitText());
393: assertNull("descriptor.LowerThreshold()", descriptor
394: .getLowerThreshold());
395: assertEquals("descriptor.UpperThreshold()", "50", descriptor
396: .getUpperThreshold());
397: }
398:
399: public void testTrueVisible() throws IOException, SAXException {
400: StringBuffer document = new StringBuffer();
401:
402: document
403: .append("<!DOCTYPE metrics-configuration SYSTEM \"http://depfind.sourceforge.net/dtd/metrics-configuration.dtd\">\n");
404: document.append("<metrics-configuration>\n");
405: document.append(" <project-measurements/>\n");
406: document.append(" <group-measurements/>\n");
407: document.append(" <class-measurements/>\n");
408: document.append(" <method-measurements>\n");
409: document.append(" <measurement visible=\"true\">\n");
410: document.append(" <short-name>SLOC</short-name>\n");
411: document
412: .append(" <long-name>Single Lines of Code</long-name>\n");
413: document
414: .append(" <class>com.jeantessier.metrics.CounterMeasurement</class>\n");
415: document.append(" </measurement>\n");
416: document.append(" </method-measurements>\n");
417: document.append("</metrics-configuration>\n");
418:
419: InputSource in = new InputSource(new StringReader(document
420: .toString()));
421:
422: reader.parse(in);
423:
424: MeasurementDescriptor descriptor = (MeasurementDescriptor) handler
425: .getMetricsConfiguration().getMethodMeasurements().get(
426: 0);
427: assertTrue("Not visible", descriptor.isVisible());
428: }
429:
430: public void testYesVisible() throws IOException, SAXException {
431: StringBuffer document = new StringBuffer();
432:
433: document
434: .append("<!DOCTYPE metrics-configuration SYSTEM \"http://depfind.sourceforge.net/dtd/metrics-configuration.dtd\">\n");
435: document.append("<metrics-configuration>\n");
436: document.append(" <project-measurements/>\n");
437: document.append(" <group-measurements/>\n");
438: document.append(" <class-measurements/>\n");
439: document.append(" <method-measurements>\n");
440: document.append(" <measurement visible=\"yes\">\n");
441: document.append(" <short-name>SLOC</short-name>\n");
442: document
443: .append(" <long-name>Single Lines of Code</long-name>\n");
444: document
445: .append(" <class>com.jeantessier.metrics.CounterMeasurement</class>\n");
446: document.append(" </measurement>\n");
447: document.append(" </method-measurements>\n");
448: document.append("</metrics-configuration>\n");
449:
450: InputSource in = new InputSource(new StringReader(document
451: .toString()));
452:
453: reader.parse(in);
454:
455: MeasurementDescriptor descriptor = (MeasurementDescriptor) handler
456: .getMetricsConfiguration().getMethodMeasurements().get(
457: 0);
458: assertTrue("Not visible", descriptor.isVisible());
459: }
460:
461: public void testOnVisible() throws IOException, SAXException {
462: StringBuffer document = new StringBuffer();
463:
464: document
465: .append("<!DOCTYPE metrics-configuration SYSTEM \"http://depfind.sourceforge.net/dtd/metrics-configuration.dtd\">\n");
466: document.append("<metrics-configuration>\n");
467: document.append(" <project-measurements/>\n");
468: document.append(" <group-measurements/>\n");
469: document.append(" <class-measurements/>\n");
470: document.append(" <method-measurements>\n");
471: document.append(" <measurement visible=\"on\">\n");
472: document.append(" <short-name>SLOC</short-name>\n");
473: document
474: .append(" <long-name>Single Lines of Code</long-name>\n");
475: document
476: .append(" <class>com.jeantessier.metrics.CounterMeasurement</class>\n");
477: document.append(" </measurement>\n");
478: document.append(" </method-measurements>\n");
479: document.append("</metrics-configuration>\n");
480:
481: InputSource in = new InputSource(new StringReader(document
482: .toString()));
483:
484: reader.parse(in);
485:
486: MeasurementDescriptor descriptor = (MeasurementDescriptor) handler
487: .getMetricsConfiguration().getMethodMeasurements().get(
488: 0);
489: assertTrue("Not visible", descriptor.isVisible());
490: }
491:
492: public void testDefaultVisible() throws IOException, SAXException {
493: StringBuffer document = new StringBuffer();
494:
495: document
496: .append("<!DOCTYPE metrics-configuration SYSTEM \"http://depfind.sourceforge.net/dtd/metrics-configuration.dtd\">\n");
497: document.append("<metrics-configuration>\n");
498: document.append(" <project-measurements/>\n");
499: document.append(" <group-measurements/>\n");
500: document.append(" <class-measurements/>\n");
501: document.append(" <method-measurements>\n");
502: document.append(" <measurement>\n");
503: document.append(" <short-name>SLOC</short-name>\n");
504: document
505: .append(" <long-name>Single Lines of Code</long-name>\n");
506: document
507: .append(" <class>com.jeantessier.metrics.CounterMeasurement</class>\n");
508: document.append(" </measurement>\n");
509: document.append(" </method-measurements>\n");
510: document.append("</metrics-configuration>\n");
511:
512: InputSource in = new InputSource(new StringReader(document
513: .toString()));
514:
515: reader.parse(in);
516:
517: MeasurementDescriptor descriptor = (MeasurementDescriptor) handler
518: .getMetricsConfiguration().getMethodMeasurements().get(
519: 0);
520: assertTrue("Not visible", descriptor.isVisible());
521: }
522:
523: public void testNotVisible() throws IOException, SAXException {
524: StringBuffer document = new StringBuffer();
525:
526: document
527: .append("<!DOCTYPE metrics-configuration SYSTEM \"http://depfind.sourceforge.net/dtd/metrics-configuration.dtd\">\n");
528: document.append("<metrics-configuration>\n");
529: document.append(" <project-measurements/>\n");
530: document.append(" <group-measurements/>\n");
531: document.append(" <class-measurements/>\n");
532: document.append(" <method-measurements>\n");
533: document.append(" <measurement visible=\"no\">\n");
534: document.append(" <short-name>SLOC</short-name>\n");
535: document
536: .append(" <long-name>Single Lines of Code</long-name>\n");
537: document
538: .append(" <class>com.jeantessier.metrics.CounterMeasurement</class>\n");
539: document.append(" </measurement>\n");
540: document.append(" </method-measurements>\n");
541: document.append("</metrics-configuration>\n");
542:
543: InputSource in = new InputSource(new StringReader(document
544: .toString()));
545:
546: reader.parse(in);
547:
548: MeasurementDescriptor descriptor = (MeasurementDescriptor) handler
549: .getMetricsConfiguration().getMethodMeasurements().get(
550: 0);
551: assertTrue("Visible", !descriptor.isVisible());
552: }
553:
554: public void testGroupDefinitions() throws IOException, SAXException {
555: StringBuffer document = new StringBuffer();
556:
557: document
558: .append("<!DOCTYPE metrics-configuration SYSTEM \"http://depfind.sourceforge.net/dtd/metrics-configuration.dtd\">\n");
559: document.append("<metrics-configuration>\n");
560: document.append(" <project-measurements/>\n");
561: document.append(" <group-definitions>\n");
562: document.append(" <group-definition>\n");
563: document.append(" <name>Jean Tessier</name>\n");
564: document
565: .append(" <pattern>/^com.jeantessier/</pattern>\n");
566: document.append(" </group-definition>\n");
567: document.append(" </group-definitions>\n");
568: document.append(" <group-measurements/>\n");
569: document.append(" <class-measurements/>\n");
570: document.append(" <method-measurements/>\n");
571: document.append("</metrics-configuration>\n");
572:
573: InputSource in = new InputSource(new StringReader(document
574: .toString()));
575:
576: reader.parse(in);
577:
578: assertEquals("ProjectMeasurements", 0, handler
579: .getMetricsConfiguration().getProjectMeasurements()
580: .size());
581: assertEquals("GroupMeasurements", 0, handler
582: .getMetricsConfiguration().getGroupMeasurements()
583: .size());
584: assertEquals("ClassMeasurements", 0, handler
585: .getMetricsConfiguration().getClassMeasurements()
586: .size());
587: assertEquals("MethodMeasurements", 0, handler
588: .getMetricsConfiguration().getMethodMeasurements()
589: .size());
590:
591: assertEquals("groups for foobar", 0, handler
592: .getMetricsConfiguration().getGroups("foobar").size());
593: assertEquals("groups for com.jeantessier.metrics", 1, handler
594: .getMetricsConfiguration().getGroups(
595: "com.jeantessier.metrics").size());
596: assertEquals("Jean Tessier", handler.getMetricsConfiguration()
597: .getGroups("com.jeantessier.metrics").iterator().next());
598: }
599: }
|