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.dependency; |
34 |
| |
35 |
| import java.io.*; |
36 |
| import java.util.*; |
37 |
| |
38 |
| import javax.xml.parsers.*; |
39 |
| |
40 |
| import junit.framework.*; |
41 |
| import org.xml.sax.*; |
42 |
| import org.apache.oro.text.perl.*; |
43 |
| |
44 |
| public class TestXMLCyclePrinter extends TestCase implements ErrorHandler { |
45 |
| private static final String SPECIFIC_ENCODING = "iso-latin-1"; |
46 |
| private static final String SPECIFIC_DTD_PREFIX = "./etc"; |
47 |
| |
48 |
| private XMLReader reader; |
49 |
| private Perl5Util perl; |
50 |
| private NodeFactory factory; |
51 |
| private StringWriter out; |
52 |
| |
53 |
| private Node a_package; |
54 |
| private Node b_package; |
55 |
| private Node c_package; |
56 |
| |
57 |
8
| protected void setUp() throws Exception {
|
58 |
8
| boolean validate = Boolean.getBoolean("DEPENDENCYFINDER_TESTS_VALIDATE");
|
59 |
| |
60 |
8
| reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
|
61 |
8
| reader.setFeature("http://xml.org/sax/features/validation", validate);
|
62 |
8
| reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", validate);
|
63 |
8
| reader.setErrorHandler(this);
|
64 |
| |
65 |
8
| perl = new Perl5Util();
|
66 |
| |
67 |
8
| factory = new NodeFactory();
|
68 |
8
| out = new StringWriter();
|
69 |
| |
70 |
8
| a_package = factory.createPackage("a");
|
71 |
8
| b_package = factory.createPackage("b");
|
72 |
8
| c_package = factory.createPackage("c");
|
73 |
| } |
74 |
| |
75 |
1
| public void testDefaultDTDPrefix() {
|
76 |
1
| XMLCyclePrinter printer = new XMLCyclePrinter(new PrintWriter(out));
|
77 |
| |
78 |
1
| String xmlDocument = out.toString();
|
79 |
1
| assertTrue(xmlDocument + "Missing DTD", perl.match("/DOCTYPE \\S+ SYSTEM \"(.*)\"/", xmlDocument));
|
80 |
1
| assertTrue("DTD \"" + perl.group(1) + "\" does not have prefix \"" + XMLPrinter.DEFAULT_DTD_PREFIX + "\"", perl.group(1).startsWith(XMLPrinter.DEFAULT_DTD_PREFIX));
|
81 |
| |
82 |
1
| try {
|
83 |
1
| reader.parse(new InputSource(new StringReader(xmlDocument)));
|
84 |
0
| fail("Parsed non-existant document\n" + xmlDocument);
|
85 |
| } catch (SAXException ex) { |
86 |
| |
87 |
| } catch (IOException ex) { |
88 |
0
| fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument);
|
89 |
| } |
90 |
| } |
91 |
| |
92 |
1
| public void testSpecificDTDPrefix() {
|
93 |
1
| XMLCyclePrinter printer = new XMLCyclePrinter(new PrintWriter(out), XMLPrinter.DEFAULT_ENCODING, SPECIFIC_DTD_PREFIX);
|
94 |
| |
95 |
1
| String xmlDocument = out.toString();
|
96 |
1
| assertTrue(xmlDocument + "Missing DTD", perl.match("/DOCTYPE \\S+ SYSTEM \"(.*)\"/", xmlDocument));
|
97 |
1
| assertTrue("DTD \"" + perl.group(1) + "\" does not have prefix \"./etc\"", perl.group(1).startsWith(SPECIFIC_DTD_PREFIX));
|
98 |
| |
99 |
1
| try {
|
100 |
1
| reader.parse(new InputSource(new StringReader(xmlDocument)));
|
101 |
0
| fail("Parsed non-existant document\n" + xmlDocument);
|
102 |
| } catch (SAXException ex) { |
103 |
| |
104 |
| } catch (IOException ex) { |
105 |
0
| fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument);
|
106 |
| } |
107 |
| } |
108 |
| |
109 |
1
| public void testDefaultEncoding() {
|
110 |
1
| XMLCyclePrinter printer = new XMLCyclePrinter(new PrintWriter(out));
|
111 |
| |
112 |
1
| String xmlDocument = out.toString();
|
113 |
1
| assertTrue(xmlDocument + "Missing encoding", perl.match("/encoding=\"([^\"]*)\"/", xmlDocument));
|
114 |
1
| assertEquals("Encoding", XMLPrinter.DEFAULT_ENCODING, perl.group(1));
|
115 |
| |
116 |
1
| try {
|
117 |
1
| reader.parse(new InputSource(new StringReader(xmlDocument)));
|
118 |
0
| fail("Parsed non-existant document\n" + xmlDocument);
|
119 |
| } catch (SAXException ex) { |
120 |
| |
121 |
| } catch (IOException ex) { |
122 |
0
| fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument);
|
123 |
| } |
124 |
| } |
125 |
| |
126 |
1
| public void testSpecificEncoding() {
|
127 |
1
| XMLCyclePrinter printer = new XMLCyclePrinter(new PrintWriter(out), SPECIFIC_ENCODING, XMLPrinter.DEFAULT_DTD_PREFIX);
|
128 |
| |
129 |
1
| String xmlDocument = out.toString();
|
130 |
1
| assertTrue(xmlDocument + "Missing encoding", perl.match("/encoding=\"([^\"]*)\"/", xmlDocument));
|
131 |
1
| assertEquals("Encoding", SPECIFIC_ENCODING, perl.group(1));
|
132 |
| |
133 |
1
| try {
|
134 |
1
| reader.parse(new InputSource(new StringReader(xmlDocument)));
|
135 |
0
| fail("Parsed non-existant document\n" + xmlDocument);
|
136 |
| } catch (SAXException ex) { |
137 |
| |
138 |
| } catch (IOException ex) { |
139 |
0
| fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument);
|
140 |
| } |
141 |
| } |
142 |
| |
143 |
1
| public void testVisitCyclesWith2NodeCycle() throws IOException {
|
144 |
1
| List<Node> nodes = new ArrayList<Node>();
|
145 |
1
| nodes.add(a_package);
|
146 |
1
| nodes.add(b_package);
|
147 |
1
| Cycle cycle = new Cycle(nodes);
|
148 |
| |
149 |
1
| XMLCyclePrinter printer = new XMLCyclePrinter(new PrintWriter(out), XMLPrinter.DEFAULT_ENCODING, SPECIFIC_DTD_PREFIX);
|
150 |
1
| printer.visitCycles(Collections.singletonList(cycle));
|
151 |
1
| int lineNumber = 0;
|
152 |
1
| BufferedReader in = new BufferedReader(new StringReader(out.toString()));
|
153 |
| |
154 |
1
| assertEquals("line " + ++lineNumber, "<?xml version=\"1.0\" encoding=\"utf-8\" ?>", in.readLine());
|
155 |
1
| assertEquals("line " + ++lineNumber, "", in.readLine());
|
156 |
1
| assertEquals("line " + ++lineNumber, "<!DOCTYPE dependencies SYSTEM \"./etc/cycles.dtd\">", in.readLine());
|
157 |
1
| assertEquals("line " + ++lineNumber, "", in.readLine());
|
158 |
1
| assertEquals("line " + ++lineNumber, "<cycles>", in.readLine());
|
159 |
1
| assertEquals("line " + ++lineNumber, " <cycle>", in.readLine());
|
160 |
1
| assertEquals("line " + ++lineNumber, " <node type=\"package\">a</node>", in.readLine());
|
161 |
1
| assertEquals("line " + ++lineNumber, " <node type=\"package\">b</node>", in.readLine());
|
162 |
1
| assertEquals("line " + ++lineNumber, " </cycle>", in.readLine());
|
163 |
1
| assertEquals("line " + ++lineNumber, "</cycles>", in.readLine());
|
164 |
| |
165 |
1
| assertEquals("End of file", null, in.readLine());
|
166 |
| } |
167 |
| |
168 |
1
| public void testVisitCyclesWith2NodeCycleWithIndentText() throws IOException {
|
169 |
1
| List<Node> nodes = new ArrayList<Node>();
|
170 |
1
| nodes.add(a_package);
|
171 |
1
| nodes.add(b_package);
|
172 |
1
| Cycle cycle = new Cycle(nodes);
|
173 |
| |
174 |
1
| CyclePrinter printer = new XMLCyclePrinter(new PrintWriter(out), XMLPrinter.DEFAULT_ENCODING, SPECIFIC_DTD_PREFIX);
|
175 |
1
| printer.setIndentText("*");
|
176 |
1
| printer.visitCycles(Collections.singletonList(cycle));
|
177 |
1
| int lineNumber = 0;
|
178 |
1
| BufferedReader in = new BufferedReader(new StringReader(out.toString()));
|
179 |
| |
180 |
1
| assertEquals("line " + ++lineNumber, "<?xml version=\"1.0\" encoding=\"utf-8\" ?>", in.readLine());
|
181 |
1
| assertEquals("line " + ++lineNumber, "", in.readLine());
|
182 |
1
| assertEquals("line " + ++lineNumber, "<!DOCTYPE dependencies SYSTEM \"./etc/cycles.dtd\">", in.readLine());
|
183 |
1
| assertEquals("line " + ++lineNumber, "", in.readLine());
|
184 |
1
| assertEquals("line " + ++lineNumber, "<cycles>", in.readLine());
|
185 |
1
| assertEquals("line " + ++lineNumber, "*<cycle>", in.readLine());
|
186 |
1
| assertEquals("line " + ++lineNumber, "**<node type=\"package\">a</node>", in.readLine());
|
187 |
1
| assertEquals("line " + ++lineNumber, "**<node type=\"package\">b</node>", in.readLine());
|
188 |
1
| assertEquals("line " + ++lineNumber, "*</cycle>", in.readLine());
|
189 |
1
| assertEquals("line " + ++lineNumber, "</cycles>", in.readLine());
|
190 |
| |
191 |
1
| assertEquals("End of file", null, in.readLine());
|
192 |
| } |
193 |
| |
194 |
1
| public void testVisitCycleWith3NodeCycle() throws IOException {
|
195 |
1
| List<Node> nodes = new ArrayList<Node>();
|
196 |
1
| nodes.add(a_package);
|
197 |
1
| nodes.add(b_package);
|
198 |
1
| nodes.add(c_package);
|
199 |
1
| Cycle cycle = new Cycle(nodes);
|
200 |
| |
201 |
1
| XMLCyclePrinter printer = new XMLCyclePrinter(new PrintWriter(out), XMLPrinter.DEFAULT_ENCODING, SPECIFIC_DTD_PREFIX);
|
202 |
1
| printer.visitCycles(Collections.singletonList(cycle));
|
203 |
1
| int lineNumber = 0;
|
204 |
1
| BufferedReader in = new BufferedReader(new StringReader(out.toString()));
|
205 |
| |
206 |
1
| assertEquals("line " + ++lineNumber, "<?xml version=\"1.0\" encoding=\"utf-8\" ?>", in.readLine());
|
207 |
1
| assertEquals("line " + ++lineNumber, "", in.readLine());
|
208 |
1
| assertEquals("line " + ++lineNumber, "<!DOCTYPE dependencies SYSTEM \"./etc/cycles.dtd\">", in.readLine());
|
209 |
1
| assertEquals("line " + ++lineNumber, "", in.readLine());
|
210 |
1
| assertEquals("line " + ++lineNumber, "<cycles>", in.readLine());
|
211 |
1
| assertEquals("line " + ++lineNumber, " <cycle>", in.readLine());
|
212 |
1
| assertEquals("line " + ++lineNumber, " <node type=\"package\">a</node>", in.readLine());
|
213 |
1
| assertEquals("line " + ++lineNumber, " <node type=\"package\">b</node>", in.readLine());
|
214 |
1
| assertEquals("line " + ++lineNumber, " <node type=\"package\">c</node>", in.readLine());
|
215 |
1
| assertEquals("line " + ++lineNumber, " </cycle>", in.readLine());
|
216 |
1
| assertEquals("line " + ++lineNumber, "</cycles>", in.readLine());
|
217 |
| |
218 |
1
| assertEquals("End of file", null, in.readLine());
|
219 |
| } |
220 |
| |
221 |
1
| public void testVisitCycleWith2Cycles() throws IOException {
|
222 |
1
| List<Cycle> cycles = new ArrayList<Cycle>();
|
223 |
| |
224 |
1
| List<Node> nodes1 = new ArrayList<Node>();
|
225 |
1
| nodes1.add(a_package);
|
226 |
1
| nodes1.add(b_package);
|
227 |
1
| cycles.add(new Cycle(nodes1));
|
228 |
| |
229 |
1
| List<Node> nodes2 = new ArrayList<Node>();
|
230 |
1
| nodes2.add(a_package);
|
231 |
1
| nodes2.add(b_package);
|
232 |
1
| nodes2.add(c_package);
|
233 |
1
| cycles.add(new Cycle(nodes2));
|
234 |
| |
235 |
1
| XMLCyclePrinter printer = new XMLCyclePrinter(new PrintWriter(out), XMLPrinter.DEFAULT_ENCODING, SPECIFIC_DTD_PREFIX);
|
236 |
1
| printer.visitCycles(cycles);
|
237 |
1
| int lineNumber = 0;
|
238 |
1
| BufferedReader in = new BufferedReader(new StringReader(out.toString()));
|
239 |
| |
240 |
1
| assertEquals("line " + ++lineNumber, "<?xml version=\"1.0\" encoding=\"utf-8\" ?>", in.readLine());
|
241 |
1
| assertEquals("line " + ++lineNumber, "", in.readLine());
|
242 |
1
| assertEquals("line " + ++lineNumber, "<!DOCTYPE dependencies SYSTEM \"./etc/cycles.dtd\">", in.readLine());
|
243 |
1
| assertEquals("line " + ++lineNumber, "", in.readLine());
|
244 |
1
| assertEquals("line " + ++lineNumber, "<cycles>", in.readLine());
|
245 |
1
| assertEquals("line " + ++lineNumber, " <cycle>", in.readLine());
|
246 |
1
| assertEquals("line " + ++lineNumber, " <node type=\"package\">a</node>", in.readLine());
|
247 |
1
| assertEquals("line " + ++lineNumber, " <node type=\"package\">b</node>", in.readLine());
|
248 |
1
| assertEquals("line " + ++lineNumber, " </cycle>", in.readLine());
|
249 |
1
| assertEquals("line " + ++lineNumber, " <cycle>", in.readLine());
|
250 |
1
| assertEquals("line " + ++lineNumber, " <node type=\"package\">a</node>", in.readLine());
|
251 |
1
| assertEquals("line " + ++lineNumber, " <node type=\"package\">b</node>", in.readLine());
|
252 |
1
| assertEquals("line " + ++lineNumber, " <node type=\"package\">c</node>", in.readLine());
|
253 |
1
| assertEquals("line " + ++lineNumber, " </cycle>", in.readLine());
|
254 |
1
| assertEquals("line " + ++lineNumber, "</cycles>", in.readLine());
|
255 |
| |
256 |
1
| assertEquals("End of file", null, in.readLine());
|
257 |
| } |
258 |
| |
259 |
0
| public void error(SAXParseException ex) {
|
260 |
| |
261 |
| } |
262 |
| |
263 |
4
| public void fatalError(SAXParseException ex) {
|
264 |
| |
265 |
| } |
266 |
| |
267 |
0
| public void warning(SAXParseException ex) {
|
268 |
| |
269 |
| } |
270 |
| } |