1 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 READER_CLASSNAME = "org.apache.xerces.parsers.SAXParser"; 47 private static final String TEST_CLASS = "test"; 48 private static final String TEST_FILENAME = "classes" + File.separator + "test.class"; 49 private static final String TEST_DIRECTORY = "tests" + File.separator + "JarJarDiff" + File.separator + "new"; 50 51 private static final String SPECIFIC_ENCODING = "iso-latin-1"; 52 private static final String 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 { 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 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 } 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 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 } 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 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 } 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 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 } 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 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 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 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 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 } 220 221 public void fatalError(SAXParseException ex) { 222 } 224 225 public void warning(SAXParseException ex) { 226 } 228 } 229 | Popular Tags |