1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
package com.jeantessier.diff; |
34 | |
|
35 | |
import java.io.*; |
36 | |
import java.util.*; |
37 | |
import javax.xml.parsers.*; |
38 | |
import javax.xml.xpath.*; |
39 | |
|
40 | |
import junit.framework.*; |
41 | |
|
42 | |
import org.apache.oro.text.perl.*; |
43 | |
import org.w3c.dom.*; |
44 | |
import org.xml.sax.*; |
45 | |
|
46 | |
import com.jeantessier.classreader.*; |
47 | |
|
48 | 6 | public class TestReport extends TestCase implements ErrorHandler { |
49 | |
private static final String SPECIFIC_ENCODING = "iso-latin-1"; |
50 | |
private static final String SPECIFIC_DTD_PREFIX = "./etc"; |
51 | |
|
52 | 1 | private static final String OLD_CLASSPATH = "tests" + File.separator + "JarJarDiff" + File.separator + "old"; |
53 | 1 | private static final String NEW_CLASSPATH = "tests" + File.separator + "JarJarDiff" + File.separator + "new"; |
54 | |
|
55 | 1 | private static final String OLD_PUBLISHED_CLASSPATH = "tests" + File.separator + "JarJarDiff" + File.separator + "oldpublished"; |
56 | 1 | private static final String NEW_PUBLISHED_CLASSPATH = "tests" + File.separator + "JarJarDiff" + File.separator + "newpublished"; |
57 | |
|
58 | |
private XMLReader reader; |
59 | |
private Perl5Util perl; |
60 | |
|
61 | |
protected void setUp() throws Exception { |
62 | 6 | boolean validate = Boolean.getBoolean("DEPENDENCYFINDER_TESTS_VALIDATE"); |
63 | |
|
64 | 6 | reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader(); |
65 | 6 | reader.setFeature("http://xml.org/sax/features/validation", validate); |
66 | 6 | reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", validate); |
67 | 6 | reader.setErrorHandler(this); |
68 | |
|
69 | 6 | perl = new Perl5Util(); |
70 | 6 | } |
71 | |
|
72 | |
public void testDefaultDTDPrefix() { |
73 | 1 | Report report = new Report(); |
74 | |
|
75 | 1 | String xmlDocument = report.render(); |
76 | 1 | assertTrue(xmlDocument + "Missing DTD", perl.match("/DOCTYPE \\S+ SYSTEM \"(.*)\"/", xmlDocument)); |
77 | 1 | assertTrue("DTD \"" + perl.group(1) + "\" does not have prefix \"" + Report.DEFAULT_DTD_PREFIX + "\"", perl.group(1).startsWith(Report.DEFAULT_DTD_PREFIX)); |
78 | |
|
79 | |
try { |
80 | 1 | reader.parse(new InputSource(new StringReader(xmlDocument))); |
81 | 0 | } catch (SAXException ex) { |
82 | 0 | fail("Could not parse XML Document: " + ex.getMessage() + "\n" + xmlDocument); |
83 | 0 | } catch (IOException ex) { |
84 | 0 | fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument); |
85 | 1 | } |
86 | 1 | } |
87 | |
|
88 | |
public void testSpecificDTDPrefix() { |
89 | 1 | Report report = new Report(Report.DEFAULT_ENCODING, SPECIFIC_DTD_PREFIX); |
90 | |
|
91 | 1 | String xmlDocument = report.render(); |
92 | 1 | assertTrue(xmlDocument + "Missing DTD", perl.match("/DOCTYPE \\S+ SYSTEM \"(.*)\"/", xmlDocument)); |
93 | 1 | assertTrue("DTD \"" + perl.group(1) + "\" does not have prefix \"./etc\"", perl.group(1).startsWith(SPECIFIC_DTD_PREFIX)); |
94 | |
|
95 | |
try { |
96 | 1 | reader.parse(new InputSource(new StringReader(xmlDocument))); |
97 | 0 | } catch (SAXException ex) { |
98 | 0 | fail("Could not parse XML Document: " + ex.getMessage() + "\n" + xmlDocument); |
99 | 0 | } catch (IOException ex) { |
100 | 0 | fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument); |
101 | 1 | } |
102 | 1 | } |
103 | |
|
104 | |
public void testDefaultEncoding() { |
105 | 1 | Report report = new Report(); |
106 | |
|
107 | 1 | String xmlDocument = report.render(); |
108 | 1 | assertTrue(xmlDocument + "Missing encoding", perl.match("/encoding=\"([^\"]*)\"/", xmlDocument)); |
109 | 1 | assertEquals("Encoding", Report.DEFAULT_ENCODING, perl.group(1)); |
110 | |
|
111 | |
try { |
112 | 1 | reader.parse(new InputSource(new StringReader(xmlDocument))); |
113 | 0 | } catch (SAXException ex) { |
114 | 0 | fail("Could not parse XML Document: " + ex.getMessage() + "\n" + xmlDocument); |
115 | 0 | } catch (IOException ex) { |
116 | 0 | fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument); |
117 | 1 | } |
118 | 1 | } |
119 | |
|
120 | |
public void testSpecificEncoding() { |
121 | 1 | Report report = new Report(SPECIFIC_ENCODING, Report.DEFAULT_DTD_PREFIX); |
122 | |
|
123 | 1 | String xmlDocument = report.render(); |
124 | 1 | assertTrue(xmlDocument + "Missing encoding", perl.match("/encoding=\"([^\"]*)\"/", xmlDocument)); |
125 | 1 | assertEquals("Encoding", SPECIFIC_ENCODING, perl.group(1)); |
126 | |
|
127 | |
try { |
128 | 1 | reader.parse(new InputSource(new StringReader(xmlDocument))); |
129 | 0 | } catch (SAXException ex) { |
130 | 0 | fail("Could not parse XML Document: " + ex.getMessage() + "\n" + xmlDocument); |
131 | 0 | } catch (IOException ex) { |
132 | 0 | fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument); |
133 | 1 | } |
134 | 1 | } |
135 | |
|
136 | |
public void testContent() throws Exception { |
137 | 1 | PackageMapper oldPackages = new PackageMapper(); |
138 | 1 | ClassfileLoader oldJar = new AggregatingClassfileLoader(); |
139 | 1 | oldJar.addLoadListener(oldPackages); |
140 | 1 | oldJar.load(Collections.singleton(OLD_CLASSPATH)); |
141 | |
|
142 | 1 | PackageMapper newPackages = new PackageMapper(); |
143 | 1 | ClassfileLoader newJar = new AggregatingClassfileLoader(); |
144 | 1 | newJar.addLoadListener(newPackages); |
145 | 1 | newJar.load(Collections.singleton(NEW_CLASSPATH)); |
146 | |
|
147 | 1 | DifferencesFactory factory = new DifferencesFactory(); |
148 | 1 | ProjectDifferences projectDifferences = (ProjectDifferences) factory.createProjectDifferences("test", "old", oldPackages, "new", newPackages); |
149 | |
|
150 | 1 | Report report = new Report(Report.DEFAULT_ENCODING, SPECIFIC_DTD_PREFIX); |
151 | 1 | projectDifferences.accept(report); |
152 | |
|
153 | 1 | String xmlDocument = report.render(); |
154 | |
|
155 | |
try { |
156 | 1 | reader.parse(new InputSource(new StringReader(xmlDocument))); |
157 | 0 | } catch (SAXException ex) { |
158 | 0 | fail("Could not parse XML Document: " + ex.getMessage() + "\n" + xmlDocument); |
159 | 0 | } catch (IOException ex) { |
160 | 0 | fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument); |
161 | 1 | } |
162 | |
|
163 | 1 | XPath xPath = XPathFactory.newInstance().newXPath(); |
164 | 1 | InputSource in = new InputSource(new StringReader(xmlDocument)); |
165 | 1 | Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in); |
166 | |
|
167 | 1 | assertNotNull("//differences", xPath.evaluate("//differences", doc)); |
168 | 1 | assertNotNull("*/old[text()='old']", xPath.evaluate("*/old[text()='old']", doc)); |
169 | 1 | assertEquals("*/modified-classes/class", 3, ((NodeList) xPath.evaluate("*/modified-classes/class", doc, XPathConstants.NODESET)).getLength()); |
170 | 1 | } |
171 | |
|
172 | |
public void testIncompatibleContent() throws Exception { |
173 | 1 | PackageMapper oldPackages = new PackageMapper(); |
174 | 1 | ClassfileLoader oldJar = new AggregatingClassfileLoader(); |
175 | 1 | oldJar.addLoadListener(oldPackages); |
176 | 1 | oldJar.load(Collections.singleton(OLD_PUBLISHED_CLASSPATH + File.separator + "ModifiedPackage" + File.separator + "CompatibleClass.class")); |
177 | |
|
178 | 1 | PackageMapper newPackages = new PackageMapper(); |
179 | 1 | ClassfileLoader newJar = new AggregatingClassfileLoader(); |
180 | 1 | newJar.addLoadListener(newPackages); |
181 | 1 | newJar.load(Collections.singleton(NEW_PUBLISHED_CLASSPATH + File.separator + "ModifiedPackage" + File.separator + "CompatibleClass.class")); |
182 | |
|
183 | 1 | DifferencesFactory factory = new DifferencesFactory(new IncompatibleDifferenceStrategy(new NoDifferenceStrategy())); |
184 | 1 | ProjectDifferences projectDifferences = (ProjectDifferences) factory.createProjectDifferences("test", "old", oldPackages, "new", newPackages); |
185 | |
|
186 | 1 | Report report = new Report(Report.DEFAULT_ENCODING, SPECIFIC_DTD_PREFIX); |
187 | 1 | projectDifferences.accept(report); |
188 | |
|
189 | 1 | String xmlDocument = report.render(); |
190 | |
|
191 | |
try { |
192 | 1 | reader.parse(new InputSource(new StringReader(xmlDocument))); |
193 | 0 | } catch (SAXException ex) { |
194 | 0 | fail("Could not parse XML Document: " + ex.getMessage() + "\n" + xmlDocument); |
195 | 0 | } catch (IOException ex) { |
196 | 0 | fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument); |
197 | 1 | } |
198 | |
|
199 | 1 | XPath xPath = XPathFactory.newInstance().newXPath(); |
200 | 1 | InputSource in = new InputSource(new StringReader(xmlDocument)); |
201 | 1 | Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in); |
202 | |
|
203 | 1 | assertNotNull("//differences", xPath.evaluate("//differences", doc)); |
204 | 1 | assertNotNull("*/old[text()='old']", xPath.evaluate("*/old[text()='old']", doc)); |
205 | 1 | assertEquals("*/modified-classes/class", 1, ((NodeList) xPath.evaluate("*/modified-classes/class", doc, XPathConstants.NODESET)).getLength()); |
206 | 1 | assertEquals("*/modified-classes/class/modified-declaration", "", xPath.evaluate("*/modified-classes/class/modified-declaration", doc)); |
207 | 1 | assertEquals("*/modified-classes/class/modified-methods/feature", 1, ((NodeList) xPath.evaluate("*/modified-classes/class/modified-methods/feature", doc, XPathConstants.NODESET)).getLength()); |
208 | 1 | assertNotNull("*/modified-classes/class/modified-methods/feature/modified-declaration", xPath.evaluate("*/modified-classes/class/modified-methods/feature/modified-declaration", doc)); |
209 | 1 | } |
210 | |
|
211 | |
public void error(SAXParseException ex) { |
212 | |
|
213 | 0 | } |
214 | |
|
215 | |
public void fatalError(SAXParseException ex) { |
216 | |
|
217 | 0 | } |
218 | |
|
219 | |
public void warning(SAXParseException ex) { |
220 | |
|
221 | 0 | } |
222 | |
} |