KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > classreader > 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.classreader;
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 public class TestXMLPrinter extends TestCase implements ErrorHandler {
46     private static final String JavaDoc READER_CLASSNAME = "org.apache.xerces.parsers.SAXParser";
47     private static final String JavaDoc TEST_CLASS = "test";
48     private static final String JavaDoc TEST_FILENAME = "classes" + File.separator + "test.class";
49     private static final String JavaDoc TEST_DIRECTORY = "tests" + File.separator + "JarJarDiff" + File.separator + "new";
50
51     private static final String JavaDoc SPECIFIC_ENCODING = "iso-latin-1";
52     private static final String JavaDoc SPECIFIC_DTD_PREFIX = "./etc";
53
54     private ClassfileLoader loader;
55     private StringWriter buffer;
56     private Visitor printer;
57     private XMLReader reader;
58
59     private Perl5Util perl;
60
61     protected void setUp() throws Exception JavaDoc {
62         loader = new AggregatingClassfileLoader();
63
64         buffer = new StringWriter();
65         printer = new XMLPrinter(new PrintWriter(buffer), XMLPrinter.DEFAULT_ENCODING, SPECIFIC_DTD_PREFIX);
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         buffer = new StringWriter();
77         printer = new XMLPrinter(new PrintWriter(buffer));
78
79         String JavaDoc xmlDocument = buffer.toString();
80         assertTrue(xmlDocument + "Missing DTD", perl.match("/DOCTYPE \\S+ SYSTEM \"(.*)\"/", xmlDocument));
81         assertTrue("DTD \"" + perl.group(1) + "\" does not have prefix \"" + XMLPrinter.DEFAULT_DTD_PREFIX + "\"", perl.group(1).startsWith(XMLPrinter.DEFAULT_DTD_PREFIX));
82         
83         try {
84             reader.parse(new InputSource(new StringReader(xmlDocument)));
85             fail("Parsed non-existant document\n" + xmlDocument);
86         } catch (SAXException ex) {
87             // Ignore
88
} catch (IOException ex) {
89             fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument);
90         }
91     }
92     
93     public void testSpecificDTDPrefix() {
94         buffer = new StringWriter();
95         printer = new XMLPrinter(new PrintWriter(buffer), XMLPrinter.DEFAULT_ENCODING, SPECIFIC_DTD_PREFIX);
96
97         String JavaDoc xmlDocument = buffer.toString();
98         assertTrue(xmlDocument + "Missing DTD", perl.match("/DOCTYPE \\S+ SYSTEM \"(.*)\"/", xmlDocument));
99         assertTrue("DTD \"" + perl.group(1) + "\" does not have prefix \"./etc\"", perl.group(1).startsWith(SPECIFIC_DTD_PREFIX));
100         
101         try {
102             reader.parse(new InputSource(new StringReader(xmlDocument)));
103             fail("Parsed non-existant document\n" + xmlDocument);
104         } catch (SAXException ex) {
105             // Ignore
106
} catch (IOException ex) {
107             fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument);
108         }
109     }
110
111     public void testDefaultEncoding() {
112         buffer = new StringWriter();
113         printer = new XMLPrinter(new PrintWriter(buffer));
114
115         String JavaDoc xmlDocument = buffer.toString();
116         assertTrue(xmlDocument + "Missing encoding", perl.match("/encoding=\"([^\"]*)\"/", xmlDocument));
117         assertEquals("Encoding", XMLPrinter.DEFAULT_ENCODING, perl.group(1));
118         
119         try {
120             reader.parse(new InputSource(new StringReader(xmlDocument)));
121             fail("Parsed non-existant document\n" + xmlDocument);
122         } catch (SAXException ex) {
123             // Ignore
124
} catch (IOException ex) {
125             fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument);
126         }
127     }
128
129     public void testSpecificEncoding() {
130         buffer = new StringWriter();
131         printer = new XMLPrinter(new PrintWriter(buffer), SPECIFIC_ENCODING, XMLPrinter.DEFAULT_DTD_PREFIX);
132
133         String JavaDoc xmlDocument = buffer.toString();
134         assertTrue(xmlDocument + "Missing encoding", perl.match("/encoding=\"([^\"]*)\"/", xmlDocument));
135         assertEquals("Encoding", SPECIFIC_ENCODING, perl.group(1));
136         
137         try {
138             reader.parse(new InputSource(new StringReader(xmlDocument)));
139             fail("Parsed non-existant document\n" + xmlDocument);
140         } catch (SAXException ex) {
141             // Ignore
142
} catch (IOException ex) {
143             fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument);
144         }
145     }
146
147     public void testSingleClassfile() {
148         loader.load(Collections.singleton(TEST_FILENAME));
149
150         loader.getClassfile(TEST_CLASS).accept(printer);
151
152         String JavaDoc xmlDocument = buffer.toString();
153         assertTrue(xmlDocument + "Missing DOCTYPE", perl.match("/DOCTYPE (\\w+) SYSTEM/", xmlDocument));
154         assertEquals("DOCTYPE", "classfiles", perl.group(1));
155
156         try {
157             reader.parse(new InputSource(new StringReader(xmlDocument)));
158         } catch (SAXException ex) {
159             fail("Could not parse XML Document: " + ex.getMessage() + "\n" + xmlDocument);
160         } catch (IOException ex) {
161             fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument);
162         }
163     }
164     
165     public void testZeroClassfile() {
166         printer.visitClassfiles(Collections.EMPTY_LIST);
167
168         String JavaDoc xmlDocument = buffer.toString();
169         assertTrue(xmlDocument + "Missing DOCTYPE", perl.match("/DOCTYPE (\\w+) SYSTEM/", xmlDocument));
170         assertEquals("DOCTYPE", "classfiles", perl.group(1));
171
172         try {
173             reader.parse(new InputSource(new StringReader(xmlDocument)));
174         } catch (SAXException ex) {
175             fail("Could not parse XML Document: " + ex.getMessage() + "\n" + xmlDocument);
176         } catch (IOException ex) {
177             fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument);
178         }
179     }
180     
181     public void testOneClassfile() {
182         loader.load(Collections.singleton(TEST_FILENAME));
183
184         printer.visitClassfiles(loader.getAllClassfiles());
185
186         String JavaDoc xmlDocument = buffer.toString();
187         assertTrue(xmlDocument + "Missing DOCTYPE", perl.match("/DOCTYPE (\\w+) SYSTEM/", xmlDocument));
188         assertEquals("DOCTYPE", "classfiles", perl.group(1));
189
190         try {
191             reader.parse(new InputSource(new StringReader(xmlDocument)));
192         } catch (SAXException ex) {
193             fail("Could not parse XML Document: " + ex.getMessage() + "\n" + xmlDocument);
194         } catch (IOException ex) {
195             fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument);
196         }
197     }
198
199     public void testMultipleClassfiles() throws SAXException, IOException {
200         loader.load(Collections.singleton(TEST_DIRECTORY));
201
202         printer.visitClassfiles(loader.getAllClassfiles());
203
204         String JavaDoc xmlDocument = buffer.toString();
205         assertTrue(xmlDocument + "Missing DOCTYPE", perl.match("/DOCTYPE (\\w+) SYSTEM/", xmlDocument));
206         assertEquals("DOCTYPE", "classfiles", perl.group(1));
207         
208         try {
209             reader.parse(new InputSource(new StringReader(xmlDocument)));
210         } catch (SAXException ex) {
211             fail("Could not parse XML Document: " + ex.getMessage() + "\n" + xmlDocument);
212         } catch (IOException ex) {
213             fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument);
214         }
215     }
216
217     public void error(SAXParseException ex) {
218         // Ignore
219
}
220
221     public void fatalError(SAXParseException ex) {
222         // Ignore
223
}
224
225     public void warning(SAXParseException ex) {
226         // Ignore
227
}
228 }
229
Popular Tags