KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.*;
36 import java.util.*;
37
38 import org.xml.sax.*;
39 import org.xml.sax.helpers.*;
40
41 import org.apache.oro.text.perl.*;
42
43 import junit.framework.*;
44
45 import com.jeantessier.classreader.*;
46
47 public class TestXMLPrinter extends TestCase implements ErrorHandler {
48     private static final String JavaDoc TEST_CLASS = "test";
49     private static final String JavaDoc TEST_FILENAME = "classes" + File.separator + "test.class";
50     private static final String JavaDoc CONFIGURATION_FILENAME = "etc" + File.separator + "MetricsConfig.xml";
51     private static final String JavaDoc READER_CLASSNAME = "org.apache.xerces.parsers.SAXParser";
52
53     private static final String JavaDoc SPECIFIC_ENCODING = "iso-latin-1";
54     private static final String JavaDoc SPECIFIC_DTD_PREFIX = "./etc";
55     
56     private StringWriter buffer;
57     private MetricsConfiguration configuration;
58     private com.jeantessier.metrics.XMLPrinter printer;
59     private XMLReader reader;
60
61     private Perl5Util perl;
62
63     protected void setUp() throws Exception JavaDoc {
64         buffer = new StringWriter();
65         configuration = new MetricsConfigurationLoader().load(CONFIGURATION_FILENAME);
66
67         reader = XMLReaderFactory.createXMLReader(READER_CLASSNAME);
68         reader.setFeature("http://xml.org/sax/features/validation", true);
69         reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", true);
70         reader.setErrorHandler(this);
71
72         perl = new Perl5Util();
73     }
74
75     public void testDefaultDTDPrefix() {
76         printer = new XMLPrinter(new PrintWriter(buffer), configuration);
77
78         String JavaDoc xmlDocument = buffer.toString();
79         assertTrue(xmlDocument + "Missing DTD", perl.match("/DOCTYPE \\S+ SYSTEM \"(.*)\"/", xmlDocument));
80         assertTrue("DTD \"" + perl.group(1) + "\" does not have prefix \"" + XMLPrinter.DEFAULT_DTD_PREFIX + "\"", perl.group(1).startsWith(XMLPrinter.DEFAULT_DTD_PREFIX));
81         
82         try {
83             reader.parse(new InputSource(new StringReader(xmlDocument)));
84             fail("Parsed non-existant document\n" + xmlDocument);
85         } catch (SAXException ex) {
86             // Ignore
87
} catch (IOException ex) {
88             fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument);
89         }
90     }
91     
92     public void testSpecificDTDPrefix() {
93         printer = new XMLPrinter(new PrintWriter(buffer), configuration, XMLPrinter.DEFAULT_ENCODING, SPECIFIC_DTD_PREFIX);
94
95         String JavaDoc xmlDocument = buffer.toString();
96         assertTrue(xmlDocument + "Missing DTD", perl.match("/DOCTYPE \\S+ SYSTEM \"(.*)\"/", xmlDocument));
97         assertTrue("DTD \"" + perl.group(1) + "\" does not have prefix \"./etc\"", perl.group(1).startsWith(SPECIFIC_DTD_PREFIX));
98         
99         try {
100             reader.parse(new InputSource(new StringReader(xmlDocument)));
101             fail("Parsed non-existant document\n" + xmlDocument);
102         } catch (SAXException ex) {
103             // Ignore
104
} catch (IOException ex) {
105             fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument);
106         }
107     }
108
109     public void testDefaultEncoding() {
110         printer = new XMLPrinter(new PrintWriter(buffer), configuration);
111
112         String JavaDoc xmlDocument = buffer.toString();
113         assertTrue(xmlDocument + "Missing encoding", perl.match("/encoding=\"([^\"]*)\"/", xmlDocument));
114         assertEquals("Encoding", XMLPrinter.DEFAULT_ENCODING, perl.group(1));
115         
116         try {
117             reader.parse(new InputSource(new StringReader(xmlDocument)));
118             fail("Parsed non-existant document\n" + xmlDocument);
119         } catch (SAXException ex) {
120             // Ignore
121
} catch (IOException ex) {
122             fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument);
123         }
124     }
125
126     public void testSpecificEncoding() {
127         printer = new XMLPrinter(new PrintWriter(buffer), configuration, SPECIFIC_ENCODING, XMLPrinter.DEFAULT_DTD_PREFIX);
128
129         String JavaDoc xmlDocument = buffer.toString();
130         assertTrue(xmlDocument + "Missing encoding", perl.match("/encoding=\"([^\"]*)\"/", xmlDocument));
131         assertEquals("Encoding", SPECIFIC_ENCODING, perl.group(1));
132         
133         try {
134             reader.parse(new InputSource(new StringReader(xmlDocument)));
135             fail("Parsed non-existant document\n" + xmlDocument);
136         } catch (SAXException ex) {
137             // Ignore
138
} catch (IOException ex) {
139             fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument);
140         }
141     }
142
143     public void testOneClass() {
144         MetricsFactory factory = new MetricsFactory("test", configuration);
145
146         ClassfileLoader loader = new AggregatingClassfileLoader();
147         loader.load(Collections.singleton(TEST_FILENAME));
148         loader.getClassfile(TEST_CLASS).accept(new MetricsGatherer("test", factory));
149
150         printer = new XMLPrinter(new PrintWriter(buffer), configuration, com.jeantessier.metrics.XMLPrinter.DEFAULT_ENCODING, SPECIFIC_DTD_PREFIX);
151         printer.visitMetrics(factory.getProjectMetrics());
152
153         String JavaDoc xmlDocument = buffer.toString();
154         
155         try {
156             reader.parse(new InputSource(new StringReader(xmlDocument)));
157         } catch (SAXException ex) {
158             fail("Could not parse XML Document: " + ex.getMessage() + "\n" + xmlDocument);
159         } catch (IOException ex) {
160             fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument);
161         }
162     }
163
164     public void error(SAXParseException ex) {
165         // Ignore
166
}
167
168     public void fatalError(SAXParseException ex) {
169         // Ignore
170
}
171
172     public void warning(SAXParseException ex) {
173         // Ignore
174
}
175 }
176
Popular Tags