1 32 33 package com.jeantessier.diff; 34 35 import java.io.*; 36 import java.util.*; 37 38 import javax.xml.parsers.*; 39 import javax.xml.transform.*; 40 41 import org.apache.xpath.*; 42 import org.w3c.dom.*; 43 import org.xml.sax.*; 44 import org.xml.sax.helpers.*; 45 46 import org.apache.oro.text.perl.*; 47 48 import junit.framework.*; 49 50 import com.jeantessier.classreader.*; 51 52 public class TestReport extends TestCase implements ErrorHandler { 53 private static final String READER_CLASSNAME = "org.apache.xerces.parsers.SAXParser"; 54 55 private static final String SPECIFIC_ENCODING = "iso-latin-1"; 56 private static final String SPECIFIC_DTD_PREFIX = "./etc"; 57 58 private static final String OLD_CLASSPATH = "tests" + File.separator + "JarJarDiff" + File.separator + "old"; 59 private static final String NEW_CLASSPATH = "tests" + File.separator + "JarJarDiff" + File.separator + "new"; 60 61 private Visitor printer; 62 private XMLReader reader; 63 64 private Perl5Util perl; 65 66 protected void setUp() throws Exception { 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 Report(); 77 78 String xmlDocument = printer.toString(); 79 assertTrue(xmlDocument + "Missing DTD", perl.match("/DOCTYPE \\S+ SYSTEM \"(.*)\"/", xmlDocument)); 80 assertTrue("DTD \"" + perl.group(1) + "\" does not have prefix \"" + Report.DEFAULT_DTD_PREFIX + "\"", perl.group(1).startsWith(Report.DEFAULT_DTD_PREFIX)); 81 82 try { 83 reader.parse(new InputSource(new StringReader(xmlDocument))); 84 } catch (SAXException ex) { 85 fail("Could not parse XML Document: " + ex.getMessage() + "\n" + xmlDocument); 86 } catch (IOException ex) { 87 fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument); 88 } 89 } 90 91 public void testSpecificDTDPrefix() { 92 printer = new Report(Report.DEFAULT_ENCODING, SPECIFIC_DTD_PREFIX); 93 94 String xmlDocument = printer.toString(); 95 assertTrue(xmlDocument + "Missing DTD", perl.match("/DOCTYPE \\S+ SYSTEM \"(.*)\"/", xmlDocument)); 96 assertTrue("DTD \"" + perl.group(1) + "\" does not have prefix \"./etc\"", perl.group(1).startsWith(SPECIFIC_DTD_PREFIX)); 97 98 try { 99 reader.parse(new InputSource(new StringReader(xmlDocument))); 100 } catch (SAXException ex) { 101 fail("Could not parse XML Document: " + ex.getMessage() + "\n" + xmlDocument); 102 } catch (IOException ex) { 103 fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument); 104 } 105 } 106 107 public void testDefaultEncoding() { 108 printer = new Report(); 109 110 String xmlDocument = printer.toString(); 111 assertTrue(xmlDocument + "Missing encoding", perl.match("/encoding=\"([^\"]*)\"/", xmlDocument)); 112 assertEquals("Encoding", Report.DEFAULT_ENCODING, perl.group(1)); 113 114 try { 115 reader.parse(new InputSource(new StringReader(xmlDocument))); 116 } catch (SAXException ex) { 117 fail("Could not parse XML Document: " + ex.getMessage() + "\n" + xmlDocument); 118 } catch (IOException ex) { 119 fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument); 120 } 121 } 122 123 public void testSpecificEncoding() { 124 printer = new Report(SPECIFIC_ENCODING, Report.DEFAULT_DTD_PREFIX); 125 126 String xmlDocument = printer.toString(); 127 assertTrue(xmlDocument + "Missing encoding", perl.match("/encoding=\"([^\"]*)\"/", xmlDocument)); 128 assertEquals("Encoding", SPECIFIC_ENCODING, perl.group(1)); 129 130 try { 131 reader.parse(new InputSource(new StringReader(xmlDocument))); 132 } catch (SAXException ex) { 133 fail("Could not parse XML Document: " + ex.getMessage() + "\n" + xmlDocument); 134 } catch (IOException ex) { 135 fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument); 136 } 137 } 138 139 public void testContent() throws IOException, ParserConfigurationException, SAXException, TransformerException { 140 ClassfileLoader oldJar = new AggregatingClassfileLoader(); 141 oldJar.load(Collections.singleton(OLD_CLASSPATH)); 142 143 ClassfileLoader newJar = new AggregatingClassfileLoader(); 144 newJar.load(Collections.singleton(NEW_CLASSPATH)); 145 146 Validator oldValidator = new ListBasedValidator(new BufferedReader(new FileReader(OLD_CLASSPATH + ".txt"))); 147 Validator newValidator = new ListBasedValidator(new BufferedReader(new FileReader(NEW_CLASSPATH + ".txt"))); 148 149 DifferencesFactory factory = new DifferencesFactory(oldValidator, newValidator); 150 JarDifferences jarDifferences = (JarDifferences) factory.createJarDifferences("test", "old", oldJar, "new", newJar); 151 152 printer = new Report(Report.DEFAULT_ENCODING, SPECIFIC_DTD_PREFIX); 153 jarDifferences.accept(printer); 154 155 String xmlDocument = printer.toString(); 156 157 try { 158 reader.parse(new InputSource(new StringReader(xmlDocument))); 159 } catch (SAXException ex) { 160 fail("Could not parse XML Document: " + ex.getMessage() + "\n" + xmlDocument); 161 } catch (IOException ex) { 162 fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument); 163 } 164 165 InputSource in = new InputSource(new StringReader(xmlDocument)); 166 Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in); 167 168 assertNotNull("//differences", XPathAPI.selectSingleNode(doc, "//differences")); 169 assertNotNull("*/old[text()='old']", XPathAPI.selectSingleNode(doc, "*/old[text()='old']")); 170 assertEquals("*/modified-classes/class", 1, XPathAPI.selectNodeList(doc, "*/modified-classes/class").getLength()); 171 } 172 173 public void error(SAXParseException ex) { 174 } 176 177 public void fatalError(SAXParseException ex) { 178 } 180 181 public void warning(SAXParseException ex) { 182 } 184 } 185 | Popular Tags |