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.classreader;
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: import sun.tools.asm.*;
043:
044: public class TestXMLPrinter extends TestCase implements ErrorHandler {
045: private static final String TEST_CLASS = "test";
046: private static final String TEST_FILENAME = "classes"
047: + File.separator + "test.class";
048: private static final String TEST_DIRECTORY = "tests"
049: + File.separator + "JarJarDiff" + File.separator + "new";
050:
051: private static final String SPECIFIC_ENCODING = "iso-latin-1";
052: private static final String SPECIFIC_DTD_PREFIX = "./etc";
053:
054: private ClassfileLoader loader;
055: private StringWriter buffer;
056: private Visitor printer;
057: private XMLReader reader;
058:
059: private Perl5Util perl;
060:
061: protected void setUp() throws Exception {
062: loader = new AggregatingClassfileLoader();
063:
064: buffer = new StringWriter();
065: printer = new XMLPrinter(new PrintWriter(buffer),
066: XMLPrinter.DEFAULT_ENCODING, SPECIFIC_DTD_PREFIX);
067:
068: reader = SAXParserFactory.newInstance().newSAXParser()
069: .getXMLReader();
070: reader.setFeature("http://xml.org/sax/features/validation",
071: true);
072: reader
073: .setFeature(
074: "http://apache.org/xml/features/nonvalidating/load-external-dtd",
075: true);
076: reader.setErrorHandler(this );
077:
078: perl = new Perl5Util();
079: }
080:
081: public void testDefaultDTDPrefix() {
082: buffer = new StringWriter();
083: printer = new XMLPrinter(new PrintWriter(buffer));
084:
085: String xmlDocument = buffer.toString();
086: assertTrue(xmlDocument + "Missing DTD", perl.match(
087: "/DOCTYPE \\S+ SYSTEM \"(.*)\"/", xmlDocument));
088: assertTrue("DTD \"" + perl.group(1)
089: + "\" does not have prefix \""
090: + XMLPrinter.DEFAULT_DTD_PREFIX + "\"", perl.group(1)
091: .startsWith(XMLPrinter.DEFAULT_DTD_PREFIX));
092:
093: try {
094: reader
095: .parse(new InputSource(
096: new StringReader(xmlDocument)));
097: fail("Parsed non-existant document\n" + xmlDocument);
098: } catch (SAXException ex) {
099: // Ignore
100: } catch (IOException ex) {
101: fail("Could not read XML Document: " + ex.getMessage()
102: + "\n" + xmlDocument);
103: }
104: }
105:
106: public void testSpecificDTDPrefix() {
107: buffer = new StringWriter();
108: printer = new XMLPrinter(new PrintWriter(buffer),
109: XMLPrinter.DEFAULT_ENCODING, SPECIFIC_DTD_PREFIX);
110:
111: String xmlDocument = buffer.toString();
112: assertTrue(xmlDocument + "Missing DTD", perl.match(
113: "/DOCTYPE \\S+ SYSTEM \"(.*)\"/", xmlDocument));
114: assertTrue("DTD \"" + perl.group(1)
115: + "\" does not have prefix \"./etc\"", perl.group(1)
116: .startsWith(SPECIFIC_DTD_PREFIX));
117:
118: try {
119: reader
120: .parse(new InputSource(
121: new StringReader(xmlDocument)));
122: fail("Parsed non-existant document\n" + xmlDocument);
123: } catch (SAXException ex) {
124: // Ignore
125: } catch (IOException ex) {
126: fail("Could not read XML Document: " + ex.getMessage()
127: + "\n" + xmlDocument);
128: }
129: }
130:
131: public void testDefaultEncoding() {
132: buffer = new StringWriter();
133: printer = new XMLPrinter(new PrintWriter(buffer));
134:
135: String xmlDocument = buffer.toString();
136: assertTrue(xmlDocument + "Missing encoding", perl.match(
137: "/encoding=\"([^\"]*)\"/", xmlDocument));
138: assertEquals("Encoding", XMLPrinter.DEFAULT_ENCODING, perl
139: .group(1));
140:
141: try {
142: reader
143: .parse(new InputSource(
144: new StringReader(xmlDocument)));
145: fail("Parsed non-existant document\n" + xmlDocument);
146: } catch (SAXException ex) {
147: // Ignore
148: } catch (IOException ex) {
149: fail("Could not read XML Document: " + ex.getMessage()
150: + "\n" + xmlDocument);
151: }
152: }
153:
154: public void testSpecificEncoding() {
155: buffer = new StringWriter();
156: printer = new XMLPrinter(new PrintWriter(buffer),
157: SPECIFIC_ENCODING, XMLPrinter.DEFAULT_DTD_PREFIX);
158:
159: String xmlDocument = buffer.toString();
160: assertTrue(xmlDocument + "Missing encoding", perl.match(
161: "/encoding=\"([^\"]*)\"/", xmlDocument));
162: assertEquals("Encoding", SPECIFIC_ENCODING, perl.group(1));
163:
164: try {
165: reader
166: .parse(new InputSource(
167: new StringReader(xmlDocument)));
168: fail("Parsed non-existant document\n" + xmlDocument);
169: } catch (SAXException ex) {
170: // Ignore
171: } catch (IOException ex) {
172: fail("Could not read XML Document: " + ex.getMessage()
173: + "\n" + xmlDocument);
174: }
175: }
176:
177: public void testSingleClassfile() {
178: loader.load(Collections.singleton(TEST_FILENAME));
179:
180: loader.getClassfile(TEST_CLASS).accept(printer);
181:
182: String xmlDocument = buffer.toString();
183: assertTrue(xmlDocument + "Missing DOCTYPE", perl.match(
184: "/DOCTYPE (\\w+) SYSTEM/", xmlDocument));
185: assertEquals("DOCTYPE", "classfiles", perl.group(1));
186:
187: try {
188: reader
189: .parse(new InputSource(
190: new StringReader(xmlDocument)));
191: } catch (SAXException ex) {
192: fail("Could not parse XML Document: " + ex.getMessage()
193: + "\n" + xmlDocument);
194: } catch (IOException ex) {
195: fail("Could not read XML Document: " + ex.getMessage()
196: + "\n" + xmlDocument);
197: }
198: }
199:
200: public void testZeroClassfile() {
201: printer.visitClassfiles(Collections.EMPTY_LIST);
202:
203: String xmlDocument = buffer.toString();
204: assertTrue(xmlDocument + "Missing DOCTYPE", perl.match(
205: "/DOCTYPE (\\w+) SYSTEM/", xmlDocument));
206: assertEquals("DOCTYPE", "classfiles", perl.group(1));
207:
208: try {
209: reader
210: .parse(new InputSource(
211: new StringReader(xmlDocument)));
212: } catch (SAXException ex) {
213: fail("Could not parse XML Document: " + ex.getMessage()
214: + "\n" + xmlDocument);
215: } catch (IOException ex) {
216: fail("Could not read XML Document: " + ex.getMessage()
217: + "\n" + xmlDocument);
218: }
219: }
220:
221: public void testOneClassfile() {
222: loader.load(Collections.singleton(TEST_FILENAME));
223:
224: printer.visitClassfiles(loader.getAllClassfiles());
225:
226: String xmlDocument = buffer.toString();
227: assertTrue(xmlDocument + "Missing DOCTYPE", perl.match(
228: "/DOCTYPE (\\w+) SYSTEM/", xmlDocument));
229: assertEquals("DOCTYPE", "classfiles", perl.group(1));
230:
231: try {
232: reader
233: .parse(new InputSource(
234: new StringReader(xmlDocument)));
235: } catch (SAXException ex) {
236: fail("Could not parse XML Document: " + ex.getMessage()
237: + "\n" + xmlDocument);
238: } catch (IOException ex) {
239: fail("Could not read XML Document: " + ex.getMessage()
240: + "\n" + xmlDocument);
241: }
242: }
243:
244: public void testMultipleClassfiles() {
245: loader.load(Collections.singleton(TEST_DIRECTORY));
246:
247: printer.visitClassfiles(loader.getAllClassfiles());
248:
249: String xmlDocument = buffer.toString();
250: assertTrue(xmlDocument + "Missing DOCTYPE", perl.match(
251: "/DOCTYPE (\\w+) SYSTEM/", xmlDocument));
252: assertEquals("DOCTYPE", "classfiles", perl.group(1));
253:
254: try {
255: reader
256: .parse(new InputSource(
257: new StringReader(xmlDocument)));
258: } catch (SAXException ex) {
259: fail("Could not parse XML Document: " + ex.getMessage()
260: + "\n" + xmlDocument);
261: } catch (IOException ex) {
262: fail("Could not read XML Document: " + ex.getMessage()
263: + "\n" + xmlDocument);
264: }
265: }
266:
267: public void testLdcWithIntConstant() {
268: loader.load(Collections.singleton(TEST_FILENAME));
269:
270: Classfile classfile = loader.getClassfile(TEST_CLASS);
271: Method_info method = classfile
272: .getMethod("main(java.lang.String[])");
273: Code_attribute code = method.getCode();
274:
275: byte[] bytecode = new byte[] { (byte) 0x12, (byte) 0x00 };
276:
277: Instruction ldc = new Instruction(code, bytecode, 0);
278: }
279:
280: public void error(SAXParseException ex) {
281: // Ignore
282: }
283:
284: public void fatalError(SAXParseException ex) {
285: // Ignore
286: }
287:
288: public void warning(SAXParseException ex) {
289: // Ignore
290: }
291: }
|