KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > diff > TestReport


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.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 JavaDoc READER_CLASSNAME = "org.apache.xerces.parsers.SAXParser";
54
55     private static final String JavaDoc SPECIFIC_ENCODING = "iso-latin-1";
56     private static final String JavaDoc SPECIFIC_DTD_PREFIX = "./etc";
57
58     private static final String JavaDoc OLD_CLASSPATH = "tests" + File.separator + "JarJarDiff" + File.separator + "old";
59     private static final String JavaDoc 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 JavaDoc {
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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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         // Ignore
175
}
176
177     public void fatalError(SAXParseException ex) {
178         // Ignore
179
}
180
181     public void warning(SAXParseException ex) {
182         // Ignore
183
}
184 }
185
Popular Tags