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.dependency;
034:
035: import java.io.*;
036: import java.util.*;
037:
038: import javax.xml.parsers.*;
039:
040: import junit.framework.*;
041: import org.xml.sax.*;
042: import org.apache.oro.text.perl.*;
043:
044: public class TestXMLCyclePrinter extends TestCase implements
045: ErrorHandler {
046: private static final String SPECIFIC_ENCODING = "iso-latin-1";
047: private static final String SPECIFIC_DTD_PREFIX = "./etc";
048:
049: private XMLReader reader;
050: private Perl5Util perl;
051: private NodeFactory factory;
052: private StringWriter out;
053:
054: private Node a_package;
055: private Node b_package;
056: private Node c_package;
057:
058: protected void setUp() throws Exception {
059: reader = SAXParserFactory.newInstance().newSAXParser()
060: .getXMLReader();
061: reader.setFeature("http://xml.org/sax/features/validation",
062: true);
063: reader
064: .setFeature(
065: "http://apache.org/xml/features/nonvalidating/load-external-dtd",
066: true);
067: reader.setErrorHandler(this );
068:
069: perl = new Perl5Util();
070:
071: factory = new NodeFactory();
072: out = new StringWriter();
073:
074: a_package = factory.createPackage("a");
075: b_package = factory.createPackage("b");
076: c_package = factory.createPackage("c");
077: }
078:
079: public void testDefaultDTDPrefix() {
080: XMLCyclePrinter printer = new XMLCyclePrinter(new PrintWriter(
081: out));
082:
083: String xmlDocument = out.toString();
084: assertTrue(xmlDocument + "Missing DTD", perl.match(
085: "/DOCTYPE \\S+ SYSTEM \"(.*)\"/", xmlDocument));
086: assertTrue("DTD \"" + perl.group(1)
087: + "\" does not have prefix \""
088: + XMLPrinter.DEFAULT_DTD_PREFIX + "\"", perl.group(1)
089: .startsWith(XMLPrinter.DEFAULT_DTD_PREFIX));
090:
091: try {
092: reader
093: .parse(new InputSource(
094: new StringReader(xmlDocument)));
095: fail("Parsed non-existant document\n" + xmlDocument);
096: } catch (SAXException ex) {
097: // Ignore
098: } catch (IOException ex) {
099: fail("Could not read XML Document: " + ex.getMessage()
100: + "\n" + xmlDocument);
101: }
102: }
103:
104: public void testSpecificDTDPrefix() {
105: XMLCyclePrinter printer = new XMLCyclePrinter(new PrintWriter(
106: out), XMLPrinter.DEFAULT_ENCODING, SPECIFIC_DTD_PREFIX);
107:
108: String xmlDocument = out.toString();
109: assertTrue(xmlDocument + "Missing DTD", perl.match(
110: "/DOCTYPE \\S+ SYSTEM \"(.*)\"/", xmlDocument));
111: assertTrue("DTD \"" + perl.group(1)
112: + "\" does not have prefix \"./etc\"", perl.group(1)
113: .startsWith(SPECIFIC_DTD_PREFIX));
114:
115: try {
116: reader
117: .parse(new InputSource(
118: new StringReader(xmlDocument)));
119: fail("Parsed non-existant document\n" + xmlDocument);
120: } catch (SAXException ex) {
121: // Ignore
122: } catch (IOException ex) {
123: fail("Could not read XML Document: " + ex.getMessage()
124: + "\n" + xmlDocument);
125: }
126: }
127:
128: public void testDefaultEncoding() {
129: XMLCyclePrinter printer = new XMLCyclePrinter(new PrintWriter(
130: out));
131:
132: String xmlDocument = out.toString();
133: assertTrue(xmlDocument + "Missing encoding", perl.match(
134: "/encoding=\"([^\"]*)\"/", xmlDocument));
135: assertEquals("Encoding", XMLPrinter.DEFAULT_ENCODING, perl
136: .group(1));
137:
138: try {
139: reader
140: .parse(new InputSource(
141: new StringReader(xmlDocument)));
142: fail("Parsed non-existant document\n" + xmlDocument);
143: } catch (SAXException ex) {
144: // Ignore
145: } catch (IOException ex) {
146: fail("Could not read XML Document: " + ex.getMessage()
147: + "\n" + xmlDocument);
148: }
149: }
150:
151: public void testSpecificEncoding() {
152: XMLCyclePrinter printer = new XMLCyclePrinter(new PrintWriter(
153: out), SPECIFIC_ENCODING, XMLPrinter.DEFAULT_DTD_PREFIX);
154:
155: String xmlDocument = out.toString();
156: assertTrue(xmlDocument + "Missing encoding", perl.match(
157: "/encoding=\"([^\"]*)\"/", xmlDocument));
158: assertEquals("Encoding", SPECIFIC_ENCODING, perl.group(1));
159:
160: try {
161: reader
162: .parse(new InputSource(
163: new StringReader(xmlDocument)));
164: fail("Parsed non-existant document\n" + xmlDocument);
165: } catch (SAXException ex) {
166: // Ignore
167: } catch (IOException ex) {
168: fail("Could not read XML Document: " + ex.getMessage()
169: + "\n" + xmlDocument);
170: }
171: }
172:
173: public void testVisitCyclesWith2NodeCycle() throws IOException {
174: List<Node> nodes = new ArrayList<Node>();
175: nodes.add(a_package);
176: nodes.add(b_package);
177: Cycle cycle = new Cycle(nodes);
178:
179: XMLCyclePrinter printer = new XMLCyclePrinter(new PrintWriter(
180: out), XMLPrinter.DEFAULT_ENCODING, SPECIFIC_DTD_PREFIX);
181: printer.visitCycles(Collections.singletonList(cycle));
182: int lineNumber = 0;
183: BufferedReader in = new BufferedReader(new StringReader(out
184: .toString()));
185:
186: assertEquals("line " + ++lineNumber,
187: "<?xml version=\"1.0\" encoding=\"utf-8\" ?>", in
188: .readLine());
189: assertEquals("line " + ++lineNumber, "", in.readLine());
190: assertEquals("line " + ++lineNumber,
191: "<!DOCTYPE dependencies SYSTEM \"./etc/cycles.dtd\">",
192: in.readLine());
193: assertEquals("line " + ++lineNumber, "", in.readLine());
194: assertEquals("line " + ++lineNumber, "<cycles>", in.readLine());
195: assertEquals("line " + ++lineNumber, " <cycle>", in
196: .readLine());
197: assertEquals("line " + ++lineNumber,
198: " <node type=\"package\">a</node>", in
199: .readLine());
200: assertEquals("line " + ++lineNumber,
201: " <node type=\"package\">b</node>", in
202: .readLine());
203: assertEquals("line " + ++lineNumber, " </cycle>", in
204: .readLine());
205: assertEquals("line " + ++lineNumber, "</cycles>", in.readLine());
206:
207: assertEquals("End of file", null, in.readLine());
208: }
209:
210: public void testVisitCyclesWith2NodeCycleWithIndentText()
211: throws IOException {
212: List<Node> nodes = new ArrayList<Node>();
213: nodes.add(a_package);
214: nodes.add(b_package);
215: Cycle cycle = new Cycle(nodes);
216:
217: CyclePrinter printer = new XMLCyclePrinter(
218: new PrintWriter(out), XMLPrinter.DEFAULT_ENCODING,
219: SPECIFIC_DTD_PREFIX);
220: printer.setIndentText("*");
221: printer.visitCycles(Collections.singletonList(cycle));
222: int lineNumber = 0;
223: BufferedReader in = new BufferedReader(new StringReader(out
224: .toString()));
225:
226: assertEquals("line " + ++lineNumber,
227: "<?xml version=\"1.0\" encoding=\"utf-8\" ?>", in
228: .readLine());
229: assertEquals("line " + ++lineNumber, "", in.readLine());
230: assertEquals("line " + ++lineNumber,
231: "<!DOCTYPE dependencies SYSTEM \"./etc/cycles.dtd\">",
232: in.readLine());
233: assertEquals("line " + ++lineNumber, "", in.readLine());
234: assertEquals("line " + ++lineNumber, "<cycles>", in.readLine());
235: assertEquals("line " + ++lineNumber, "*<cycle>", in.readLine());
236: assertEquals("line " + ++lineNumber,
237: "**<node type=\"package\">a</node>", in.readLine());
238: assertEquals("line " + ++lineNumber,
239: "**<node type=\"package\">b</node>", in.readLine());
240: assertEquals("line " + ++lineNumber, "*</cycle>", in.readLine());
241: assertEquals("line " + ++lineNumber, "</cycles>", in.readLine());
242:
243: assertEquals("End of file", null, in.readLine());
244: }
245:
246: public void testVisitCycleWith3NodeCycle() throws IOException {
247: List<Node> nodes = new ArrayList<Node>();
248: nodes.add(a_package);
249: nodes.add(b_package);
250: nodes.add(c_package);
251: Cycle cycle = new Cycle(nodes);
252:
253: XMLCyclePrinter printer = new XMLCyclePrinter(new PrintWriter(
254: out), XMLPrinter.DEFAULT_ENCODING, SPECIFIC_DTD_PREFIX);
255: printer.visitCycles(Collections.singletonList(cycle));
256: int lineNumber = 0;
257: BufferedReader in = new BufferedReader(new StringReader(out
258: .toString()));
259:
260: assertEquals("line " + ++lineNumber,
261: "<?xml version=\"1.0\" encoding=\"utf-8\" ?>", in
262: .readLine());
263: assertEquals("line " + ++lineNumber, "", in.readLine());
264: assertEquals("line " + ++lineNumber,
265: "<!DOCTYPE dependencies SYSTEM \"./etc/cycles.dtd\">",
266: in.readLine());
267: assertEquals("line " + ++lineNumber, "", in.readLine());
268: assertEquals("line " + ++lineNumber, "<cycles>", in.readLine());
269: assertEquals("line " + ++lineNumber, " <cycle>", in
270: .readLine());
271: assertEquals("line " + ++lineNumber,
272: " <node type=\"package\">a</node>", in
273: .readLine());
274: assertEquals("line " + ++lineNumber,
275: " <node type=\"package\">b</node>", in
276: .readLine());
277: assertEquals("line " + ++lineNumber,
278: " <node type=\"package\">c</node>", in
279: .readLine());
280: assertEquals("line " + ++lineNumber, " </cycle>", in
281: .readLine());
282: assertEquals("line " + ++lineNumber, "</cycles>", in.readLine());
283:
284: assertEquals("End of file", null, in.readLine());
285: }
286:
287: public void testVisitCycleWith2Cycles() throws IOException {
288: List<Cycle> cycles = new ArrayList<Cycle>();
289:
290: List<Node> nodes1 = new ArrayList<Node>();
291: nodes1.add(a_package);
292: nodes1.add(b_package);
293: cycles.add(new Cycle(nodes1));
294:
295: List<Node> nodes2 = new ArrayList<Node>();
296: nodes2.add(a_package);
297: nodes2.add(b_package);
298: nodes2.add(c_package);
299: cycles.add(new Cycle(nodes2));
300:
301: XMLCyclePrinter printer = new XMLCyclePrinter(new PrintWriter(
302: out), XMLPrinter.DEFAULT_ENCODING, SPECIFIC_DTD_PREFIX);
303: printer.visitCycles(cycles);
304: int lineNumber = 0;
305: BufferedReader in = new BufferedReader(new StringReader(out
306: .toString()));
307:
308: assertEquals("line " + ++lineNumber,
309: "<?xml version=\"1.0\" encoding=\"utf-8\" ?>", in
310: .readLine());
311: assertEquals("line " + ++lineNumber, "", in.readLine());
312: assertEquals("line " + ++lineNumber,
313: "<!DOCTYPE dependencies SYSTEM \"./etc/cycles.dtd\">",
314: in.readLine());
315: assertEquals("line " + ++lineNumber, "", in.readLine());
316: assertEquals("line " + ++lineNumber, "<cycles>", in.readLine());
317: assertEquals("line " + ++lineNumber, " <cycle>", in
318: .readLine());
319: assertEquals("line " + ++lineNumber,
320: " <node type=\"package\">a</node>", in
321: .readLine());
322: assertEquals("line " + ++lineNumber,
323: " <node type=\"package\">b</node>", in
324: .readLine());
325: assertEquals("line " + ++lineNumber, " </cycle>", in
326: .readLine());
327: assertEquals("line " + ++lineNumber, " <cycle>", in
328: .readLine());
329: assertEquals("line " + ++lineNumber,
330: " <node type=\"package\">a</node>", in
331: .readLine());
332: assertEquals("line " + ++lineNumber,
333: " <node type=\"package\">b</node>", in
334: .readLine());
335: assertEquals("line " + ++lineNumber,
336: " <node type=\"package\">c</node>", in
337: .readLine());
338: assertEquals("line " + ++lineNumber, " </cycle>", in
339: .readLine());
340: assertEquals("line " + ++lineNumber, "</cycles>", in.readLine());
341:
342: assertEquals("End of file", null, in.readLine());
343: }
344:
345: public void error(SAXParseException ex) {
346: // Ignore
347: }
348:
349: public void fatalError(SAXParseException ex) {
350: // Ignore
351: }
352:
353: public void warning(SAXParseException ex) {
354: // Ignore
355: }
356: }
|