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 java.util.*;
037: import javax.xml.parsers.*;
038:
039: import junit.framework.*;
040: import org.apache.oro.text.perl.*;
041: import org.xml.sax.*;
042:
043: import com.jeantessier.classreader.*;
044:
045: public class TestXMLPrinter extends TestCase implements ErrorHandler {
046: private static final String TEST_CLASS = "test";
047: private static final String TEST_FILENAME = "classes"
048: + File.separator + "test.class";
049: private static final String CONFIGURATION_FILENAME = "etc"
050: + File.separator + "MetricsConfig.xml";
051:
052: private static final String SPECIFIC_ENCODING = "iso-latin-1";
053: private static final String SPECIFIC_DTD_PREFIX = "./etc";
054:
055: private StringWriter buffer;
056: private MetricsConfiguration configuration;
057: private XMLReader reader;
058:
059: private Perl5Util perl;
060:
061: protected void setUp() throws Exception {
062: buffer = new StringWriter();
063: configuration = new MetricsConfigurationLoader()
064: .load(CONFIGURATION_FILENAME);
065:
066: reader = SAXParserFactory.newInstance().newSAXParser()
067: .getXMLReader();
068: reader.setFeature("http://xml.org/sax/features/validation",
069: true);
070: reader
071: .setFeature(
072: "http://apache.org/xml/features/nonvalidating/load-external-dtd",
073: true);
074: reader.setErrorHandler(this );
075:
076: perl = new Perl5Util();
077: }
078:
079: public void testDefaultDTDPrefix() {
080: XMLPrinter printer = new XMLPrinter(new PrintWriter(buffer),
081: configuration);
082:
083: String xmlDocument = buffer.toString();
084: assertTrue(xmlDocument + "Missing DTD", perl.match(
085: "/DOCTYPE \\S+ SYSTEM \"(.*)\"/", xmlDocument));
086: assertTrue("DTD \"" + perl.group(1)
087: + "\" does not have prefix \""
088: + XMLPrinter.DEFAULT_DTD_PREFIX + "\"", perl.group(1)
089: .startsWith(XMLPrinter.DEFAULT_DTD_PREFIX));
090:
091: try {
092: reader
093: .parse(new InputSource(
094: new StringReader(xmlDocument)));
095: fail("Parsed non-existant document\n" + xmlDocument);
096: } catch (SAXException ex) {
097: // Ignore
098: } catch (IOException ex) {
099: fail("Could not read XML Document: " + ex.getMessage()
100: + "\n" + xmlDocument);
101: }
102: }
103:
104: public void testSpecificDTDPrefix() {
105: XMLPrinter printer = new XMLPrinter(new PrintWriter(buffer),
106: configuration, XMLPrinter.DEFAULT_ENCODING,
107: SPECIFIC_DTD_PREFIX);
108:
109: String xmlDocument = buffer.toString();
110: assertTrue(xmlDocument + "Missing DTD", perl.match(
111: "/DOCTYPE \\S+ SYSTEM \"(.*)\"/", xmlDocument));
112: assertTrue("DTD \"" + perl.group(1)
113: + "\" does not have prefix \"./etc\"", perl.group(1)
114: .startsWith(SPECIFIC_DTD_PREFIX));
115:
116: try {
117: reader
118: .parse(new InputSource(
119: new StringReader(xmlDocument)));
120: fail("Parsed non-existant document\n" + xmlDocument);
121: } catch (SAXException ex) {
122: // Ignore
123: } catch (IOException ex) {
124: fail("Could not read XML Document: " + ex.getMessage()
125: + "\n" + xmlDocument);
126: }
127: }
128:
129: public void testDefaultEncoding() {
130: XMLPrinter printer = new XMLPrinter(new PrintWriter(buffer),
131: configuration);
132:
133: String xmlDocument = buffer.toString();
134: assertTrue(xmlDocument + "Missing encoding", perl.match(
135: "/encoding=\"([^\"]*)\"/", xmlDocument));
136: assertEquals("Encoding", XMLPrinter.DEFAULT_ENCODING, perl
137: .group(1));
138:
139: try {
140: reader
141: .parse(new InputSource(
142: new StringReader(xmlDocument)));
143: fail("Parsed non-existant document\n" + xmlDocument);
144: } catch (SAXException ex) {
145: // Ignore
146: } catch (IOException ex) {
147: fail("Could not read XML Document: " + ex.getMessage()
148: + "\n" + xmlDocument);
149: }
150: }
151:
152: public void testSpecificEncoding() {
153: XMLPrinter printer = new XMLPrinter(new PrintWriter(buffer),
154: configuration, SPECIFIC_ENCODING,
155: XMLPrinter.DEFAULT_DTD_PREFIX);
156:
157: String xmlDocument = buffer.toString();
158: assertTrue(xmlDocument + "Missing encoding", perl.match(
159: "/encoding=\"([^\"]*)\"/", xmlDocument));
160: assertEquals("Encoding", SPECIFIC_ENCODING, perl.group(1));
161:
162: try {
163: reader
164: .parse(new InputSource(
165: new StringReader(xmlDocument)));
166: fail("Parsed non-existant document\n" + xmlDocument);
167: } catch (SAXException ex) {
168: // Ignore
169: } catch (IOException ex) {
170: fail("Could not read XML Document: " + ex.getMessage()
171: + "\n" + xmlDocument);
172: }
173: }
174:
175: public void testOneClass() {
176: MetricsFactory factory = new MetricsFactory("test",
177: configuration);
178:
179: ClassfileLoader loader = new AggregatingClassfileLoader();
180: loader.load(Collections.singleton(TEST_FILENAME));
181: loader.getClassfile(TEST_CLASS).accept(
182: new MetricsGatherer("test", factory));
183:
184: XMLPrinter printer = new XMLPrinter(new PrintWriter(buffer),
185: configuration, XMLPrinter.DEFAULT_ENCODING,
186: SPECIFIC_DTD_PREFIX);
187: printer.visitMetrics(factory.getProjectMetrics());
188:
189: String xmlDocument = buffer.toString();
190:
191: try {
192: reader
193: .parse(new InputSource(
194: new StringReader(xmlDocument)));
195: } catch (SAXException ex) {
196: fail("Could not parse XML Document: " + ex.getMessage()
197: + "\n" + xmlDocument);
198: } catch (IOException ex) {
199: fail("Could not read XML Document: " + ex.getMessage()
200: + "\n" + xmlDocument);
201: }
202: }
203:
204: public void error(SAXParseException ex) {
205: // Ignore
206: }
207:
208: public void fatalError(SAXParseException ex) {
209: // Ignore
210: }
211:
212: public void warning(SAXParseException ex) {
213: // Ignore
214: }
215: }
|