1 | /*
|
---|
2 | * Copyright 2000-2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 |
|
---|
18 | package org.apache.tools.ant.types;
|
---|
19 |
|
---|
20 | import org.apache.tools.ant.BuildException;
|
---|
21 | import org.apache.tools.ant.DefaultLogger;
|
---|
22 | import org.apache.tools.ant.Project;
|
---|
23 | import org.apache.tools.ant.util.JAXPUtils;
|
---|
24 |
|
---|
25 | import junit.framework.TestCase;
|
---|
26 |
|
---|
27 | import java.io.File;
|
---|
28 |
|
---|
29 | import java.net.MalformedURLException;
|
---|
30 | import java.net.URL;
|
---|
31 |
|
---|
32 | import org.xml.sax.InputSource;
|
---|
33 | import org.xml.sax.SAXException;
|
---|
34 |
|
---|
35 | import javax.xml.transform.Source;
|
---|
36 | import javax.xml.transform.sax.SAXSource;
|
---|
37 | import javax.xml.transform.TransformerException;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * JUnit testcases for org.apache.tools.ant.types.XMLCatalog
|
---|
41 | *
|
---|
42 | */
|
---|
43 | public class XMLCatalogTest extends TestCase {
|
---|
44 |
|
---|
45 | private Project project;
|
---|
46 | private XMLCatalog catalog;
|
---|
47 |
|
---|
48 | private XMLCatalog newCatalog() {
|
---|
49 | XMLCatalog cat = new XMLCatalog();
|
---|
50 | cat.setProject(project);
|
---|
51 | return cat;
|
---|
52 | }
|
---|
53 |
|
---|
54 | private String toURLString(File file) throws MalformedURLException {
|
---|
55 | return JAXPUtils.getSystemId(file);
|
---|
56 | }
|
---|
57 |
|
---|
58 | public XMLCatalogTest(String name) {
|
---|
59 | super(name);
|
---|
60 | }
|
---|
61 |
|
---|
62 | public void setUp() {
|
---|
63 | project = new Project();
|
---|
64 | project.setBasedir(".");
|
---|
65 |
|
---|
66 | // This causes XMLCatalog to print out detailed logging
|
---|
67 | // messages for debugging
|
---|
68 | //
|
---|
69 | // DefaultLogger logger = new DefaultLogger();
|
---|
70 | // logger.setMessageOutputLevel(Project.MSG_DEBUG);
|
---|
71 | // logger.setOutputPrintStream(System.out);
|
---|
72 | // logger.setErrorPrintStream(System.err);
|
---|
73 | // project.addBuildListener(logger);
|
---|
74 |
|
---|
75 | catalog = newCatalog();
|
---|
76 | }
|
---|
77 |
|
---|
78 | public void tearDown() {
|
---|
79 | project = null;
|
---|
80 | catalog = null;
|
---|
81 | }
|
---|
82 |
|
---|
83 | public void testEmptyCatalog() {
|
---|
84 | try {
|
---|
85 | InputSource result = catalog.resolveEntity("PUBLIC ID ONE",
|
---|
86 | "i/dont/exist.dtd");
|
---|
87 | assertNull("Empty catalog should return null", result);
|
---|
88 | } catch (Exception e) {
|
---|
89 | fail("resolveEntity() failed!" + e.toString());
|
---|
90 | }
|
---|
91 |
|
---|
92 | try {
|
---|
93 | Source result = catalog.resolve("i/dont/exist.dtd", null);
|
---|
94 | String expected = toURLString(new File(project.getBaseDir() +
|
---|
95 | "/i/dont/exist.dtd"));
|
---|
96 | //
|
---|
97 | // These shenanigans are necessary b/c Norm Walsh's resolver
|
---|
98 | // has a different idea of how file URLs are created on windoze
|
---|
99 | // ie file://c:/foo instead of file:///c:/foo
|
---|
100 | //
|
---|
101 | String resultStr = new URL(((SAXSource)result).getInputSource().getSystemId()).getFile();
|
---|
102 | assertTrue("Empty catalog should return input",
|
---|
103 | expected.endsWith(resultStr));
|
---|
104 | } catch (Exception e) {
|
---|
105 | fail("resolve() failed!" + e.toString());
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | public void testNonExistentEntry() {
|
---|
110 |
|
---|
111 | ResourceLocation dtd = new ResourceLocation();
|
---|
112 | dtd.setPublicId("PUBLIC ID ONE");
|
---|
113 | dtd.setLocation("i/dont/exist.dtd");
|
---|
114 |
|
---|
115 | try {
|
---|
116 | InputSource result = catalog.resolveEntity("PUBLIC ID ONE",
|
---|
117 | "i/dont/exist.dtd");
|
---|
118 | assertNull("Nonexistent Catalog entry should not be returned", result);
|
---|
119 | } catch (Exception e) {
|
---|
120 | fail("resolveEntity() failed!" + e.toString());
|
---|
121 | }
|
---|
122 |
|
---|
123 | try {
|
---|
124 | Source result = catalog.resolve("i/dont/exist.dtd", null);
|
---|
125 | String expected = toURLString(new File(project.getBaseDir().toURL() +
|
---|
126 | "/i/dont/exist.dtd"));
|
---|
127 | String resultStr = new URL(((SAXSource)result).getInputSource().getSystemId()).getFile();
|
---|
128 | assertTrue("Nonexistent Catalog entry return input",
|
---|
129 | expected.endsWith(resultStr));
|
---|
130 | } catch (Exception e) {
|
---|
131 | fail("resolve() failed!" + e.toString());
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | public void testEmptyElementIfIsReference() {
|
---|
136 | ResourceLocation dtd = new ResourceLocation();
|
---|
137 | dtd.setPublicId("PUBLIC ID ONE");
|
---|
138 | dtd.setLocation("i/dont/exist.dtd");
|
---|
139 | catalog.addDTD(dtd);
|
---|
140 | project.addReference("catalog", catalog);
|
---|
141 |
|
---|
142 | try {
|
---|
143 | catalog.setRefid(new Reference("dummyref"));
|
---|
144 | fail("Can add reference to nonexistent XMLCatalog");
|
---|
145 | } catch (BuildException be) {
|
---|
146 | assertEquals("You must not specify more than one "
|
---|
147 | + "attribute when using refid", be.getMessage());
|
---|
148 | }
|
---|
149 |
|
---|
150 | XMLCatalog catalog2 = newCatalog();
|
---|
151 | catalog2.setRefid(new Reference("catalog"));
|
---|
152 |
|
---|
153 | try {
|
---|
154 | catalog2.addConfiguredXMLCatalog(catalog);
|
---|
155 | fail("Can add nested XMLCatalog to XMLCatalog that is a reference");
|
---|
156 | } catch (BuildException be) {
|
---|
157 | assertEquals("You must not specify nested elements when using refid",
|
---|
158 | be.getMessage());
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | public void testCircularReferenceCheck() {
|
---|
163 |
|
---|
164 | // catalog <--> catalog
|
---|
165 | project.addReference("catalog", catalog);
|
---|
166 | catalog.setRefid(new Reference("catalog"));
|
---|
167 |
|
---|
168 | try {
|
---|
169 | InputSource result = catalog.resolveEntity("PUBLIC ID ONE",
|
---|
170 | "i/dont/exist.dtd");
|
---|
171 | fail("Can make XMLCatalog a Reference to itself.");
|
---|
172 | } catch (BuildException be) {
|
---|
173 | assertEquals("This data type contains a circular reference.",
|
---|
174 | be.getMessage());
|
---|
175 | } catch (Exception e) {
|
---|
176 | fail("resolveEntity() failed!" + e.toString());
|
---|
177 | }
|
---|
178 |
|
---|
179 | // catalog1 --> catalog2 --> catalog3 --> catalog1
|
---|
180 | XMLCatalog catalog1 = newCatalog();
|
---|
181 | project.addReference("catalog1", catalog1);
|
---|
182 | XMLCatalog catalog2 = newCatalog();
|
---|
183 | project.addReference("catalog2", catalog2);
|
---|
184 | XMLCatalog catalog3 = newCatalog();
|
---|
185 | project.addReference("catalog3", catalog3);
|
---|
186 |
|
---|
187 | catalog3.setRefid(new Reference("catalog1"));
|
---|
188 | catalog2.setRefid(new Reference("catalog3"));
|
---|
189 | catalog1.setRefid(new Reference("catalog2"));
|
---|
190 |
|
---|
191 | try {
|
---|
192 | InputSource result = catalog1.resolveEntity("PUBLIC ID ONE",
|
---|
193 | "i/dont/exist.dtd");
|
---|
194 | fail("Can make circular reference");
|
---|
195 | } catch (BuildException be) {
|
---|
196 | assertEquals("This data type contains a circular reference.",
|
---|
197 | be.getMessage());
|
---|
198 | } catch (Exception e) {
|
---|
199 | fail("resolveEntity() failed!" + e.toString());
|
---|
200 | }
|
---|
201 | }
|
---|
202 | // inspired by Bugzilla Report 23913
|
---|
203 | // a problem used to happen under Windows when the location of the DTD was given as an absolute path
|
---|
204 | // possibly with a mixture of file separators
|
---|
205 | public void testAbsolutePath() {
|
---|
206 | ResourceLocation dtd = new ResourceLocation();
|
---|
207 | dtd.setPublicId("-//stevo//DTD doc 1.0//EN");
|
---|
208 |
|
---|
209 | String sysid = System.getProperty("user.dir") + File.separator + "src/etc/testcases/taskdefs/optional/xml/doc.dtd";
|
---|
210 | dtd.setLocation(sysid);
|
---|
211 | catalog.addDTD(dtd);
|
---|
212 | File dtdFile = project.resolveFile(sysid);
|
---|
213 |
|
---|
214 | try {
|
---|
215 | InputSource result = catalog.resolveEntity("-//stevo//DTD doc 1.0//EN",
|
---|
216 | "nap:chemical+brothers");
|
---|
217 | assertNotNull(result);
|
---|
218 | assertEquals(toURLString(dtdFile),
|
---|
219 | result.getSystemId());
|
---|
220 | } catch (Exception e) {
|
---|
221 | fail("resolveEntity() failed!" + e.toString());
|
---|
222 | }
|
---|
223 |
|
---|
224 | }
|
---|
225 |
|
---|
226 | public void testSimpleEntry() {
|
---|
227 |
|
---|
228 | ResourceLocation dtd = new ResourceLocation();
|
---|
229 | dtd.setPublicId("-//stevo//DTD doc 1.0//EN");
|
---|
230 | String sysid = "src/etc/testcases/taskdefs/optional/xml/doc.dtd";
|
---|
231 | dtd.setLocation(sysid);
|
---|
232 | catalog.addDTD(dtd);
|
---|
233 | File dtdFile = project.resolveFile(sysid);
|
---|
234 |
|
---|
235 | try {
|
---|
236 | InputSource result = catalog.resolveEntity("-//stevo//DTD doc 1.0//EN",
|
---|
237 | "nap:chemical+brothers");
|
---|
238 | assertNotNull(result);
|
---|
239 | assertEquals(toURLString(dtdFile),
|
---|
240 | result.getSystemId());
|
---|
241 | } catch (Exception e) {
|
---|
242 | fail("resolveEntity() failed!" + e.toString());
|
---|
243 | }
|
---|
244 | }
|
---|
245 |
|
---|
246 | public void testEntryReference() {
|
---|
247 |
|
---|
248 | String publicId = "-//stevo//DTD doc 1.0//EN";
|
---|
249 | String sysid = "src/etc/testcases/taskdefs/optional/xml/doc.dtd";
|
---|
250 |
|
---|
251 | // catalog2 --> catalog1 --> catalog
|
---|
252 | ResourceLocation dtd = new ResourceLocation();
|
---|
253 | dtd.setPublicId(publicId);
|
---|
254 | dtd.setLocation(sysid);
|
---|
255 | catalog.addDTD(dtd);
|
---|
256 | File dtdFile = project.resolveFile(sysid);
|
---|
257 |
|
---|
258 | String uri = "http://foo.com/bar/blah.xml";
|
---|
259 | String uriLoc = "src/etc/testcases/taskdefs/optional/xml/about.xml";
|
---|
260 |
|
---|
261 | ResourceLocation entity = new ResourceLocation();
|
---|
262 | entity.setPublicId(uri);
|
---|
263 | entity.setLocation(uriLoc);
|
---|
264 | catalog.addEntity(entity);
|
---|
265 | File xmlFile = project.resolveFile(uriLoc);
|
---|
266 |
|
---|
267 | project.addReference("catalog", catalog);
|
---|
268 |
|
---|
269 | XMLCatalog catalog1 = newCatalog();
|
---|
270 | project.addReference("catalog1", catalog1);
|
---|
271 | XMLCatalog catalog2 = newCatalog();
|
---|
272 | project.addReference("catalog2", catalog1);
|
---|
273 |
|
---|
274 | catalog1.setRefid(new Reference("catalog"));
|
---|
275 | catalog2.setRefid(new Reference("catalog1"));
|
---|
276 |
|
---|
277 | try {
|
---|
278 | InputSource result = catalog2.resolveEntity(publicId,
|
---|
279 | "nap:chemical+brothers");
|
---|
280 |
|
---|
281 | assertNotNull(result);
|
---|
282 | assertEquals(toURLString(dtdFile),
|
---|
283 | result.getSystemId());
|
---|
284 | } catch (Exception e) {
|
---|
285 | fail("resolveEntity() failed!" + e.toString());
|
---|
286 | }
|
---|
287 |
|
---|
288 | try {
|
---|
289 | Source result = catalog.resolve(uri, null);
|
---|
290 | assertNotNull(result);
|
---|
291 | assertEquals(toURLString(xmlFile),
|
---|
292 | result.getSystemId());
|
---|
293 | } catch (Exception e) {
|
---|
294 | fail("resolve() failed!" + e.toString());
|
---|
295 | }
|
---|
296 | }
|
---|
297 |
|
---|
298 | public void testNestedCatalog() {
|
---|
299 |
|
---|
300 | String publicId = "-//stevo//DTD doc 1.0//EN";
|
---|
301 | String dtdLoc = "src/etc/testcases/taskdefs/optional/xml/doc.dtd";
|
---|
302 |
|
---|
303 | ResourceLocation dtd = new ResourceLocation();
|
---|
304 | dtd.setPublicId(publicId);
|
---|
305 | dtd.setLocation(dtdLoc);
|
---|
306 | catalog.addDTD(dtd);
|
---|
307 | File dtdFile = project.resolveFile(dtdLoc);
|
---|
308 |
|
---|
309 | String uri = "http://foo.com/bar/blah.xml";
|
---|
310 | String uriLoc = "src/etc/testcases/taskdefs/optional/xml/about.xml";
|
---|
311 |
|
---|
312 | ResourceLocation entity = new ResourceLocation();
|
---|
313 | entity.setPublicId(uri);
|
---|
314 | entity.setLocation(uriLoc);
|
---|
315 | catalog.addEntity(entity);
|
---|
316 | File xmlFile = project.resolveFile(uriLoc);
|
---|
317 |
|
---|
318 | XMLCatalog catalog1 = newCatalog();
|
---|
319 | catalog1.addConfiguredXMLCatalog(catalog);
|
---|
320 |
|
---|
321 | try {
|
---|
322 | InputSource result = catalog1.resolveEntity(publicId,
|
---|
323 | "nap:chemical+brothers");
|
---|
324 | assertNotNull(result);
|
---|
325 | assertEquals(toURLString(dtdFile),
|
---|
326 | result.getSystemId());
|
---|
327 | } catch (Exception e) {
|
---|
328 | fail("resolveEntity() failed!" + e.toString());
|
---|
329 | }
|
---|
330 |
|
---|
331 | try {
|
---|
332 | Source result = catalog.resolve(uri, null);
|
---|
333 | assertNotNull(result);
|
---|
334 | assertEquals(toURLString(xmlFile),
|
---|
335 | result.getSystemId());
|
---|
336 | } catch (Exception e) {
|
---|
337 | fail("resolve() failed!" + e.toString());
|
---|
338 | }
|
---|
339 |
|
---|
340 | }
|
---|
341 |
|
---|
342 | public void testResolverBase() {
|
---|
343 |
|
---|
344 | String uri = "http://foo.com/bar/blah.xml";
|
---|
345 | String uriLoc = "etc/testcases/taskdefs/optional/xml/about.xml";
|
---|
346 | String base = null;
|
---|
347 | try {
|
---|
348 | base = toURLString(project.getBaseDir()) + "/src/";
|
---|
349 | } catch (MalformedURLException ex) {
|
---|
350 | fail (ex.toString());
|
---|
351 | }
|
---|
352 |
|
---|
353 | ResourceLocation entity = new ResourceLocation();
|
---|
354 | entity.setPublicId(uri);
|
---|
355 | entity.setLocation(uriLoc);
|
---|
356 | catalog.addEntity(entity);
|
---|
357 | File xmlFile = project.resolveFile("src/" + uriLoc);
|
---|
358 |
|
---|
359 | try {
|
---|
360 | Source result = catalog.resolve(uri, base);
|
---|
361 | assertNotNull(result);
|
---|
362 | assertEquals(toURLString(xmlFile),
|
---|
363 | result.getSystemId());
|
---|
364 | } catch (Exception e) {
|
---|
365 | fail("resolve() failed!" + e.toString());
|
---|
366 | }
|
---|
367 | }
|
---|
368 |
|
---|
369 | public void testClasspath() {
|
---|
370 |
|
---|
371 |
|
---|
372 | String publicId = "-//stevo//DTD doc 1.0//EN";
|
---|
373 | String dtdLoc = "testcases/taskdefs/optional/xml/doc.dtd";
|
---|
374 | String path1 = project.getBaseDir().toString() + "/src/etc";
|
---|
375 |
|
---|
376 | ResourceLocation dtd = new ResourceLocation();
|
---|
377 | dtd.setPublicId(publicId);
|
---|
378 | dtd.setLocation(dtdLoc);
|
---|
379 | catalog.addDTD(dtd);
|
---|
380 | File dtdFile = project.resolveFile("src/etc/" + dtdLoc);
|
---|
381 |
|
---|
382 | String uri = "http://foo.com/bar/blah.xml";
|
---|
383 | String uriLoc = "etc/testcases/taskdefs/optional/xml/about.xml";
|
---|
384 | String path2 = project.getBaseDir().toString() + "/src";
|
---|
385 |
|
---|
386 | ResourceLocation entity = new ResourceLocation();
|
---|
387 | entity.setPublicId(uri);
|
---|
388 | entity.setLocation(uriLoc);
|
---|
389 | catalog.addEntity(entity);
|
---|
390 | File xmlFile = project.resolveFile("src/" + uriLoc);
|
---|
391 |
|
---|
392 | Path aPath = new Path(project, path1);
|
---|
393 | aPath.append(new Path(project, path2));
|
---|
394 | catalog.setClasspath(aPath);
|
---|
395 |
|
---|
396 | try {
|
---|
397 | InputSource result = catalog.resolveEntity(publicId,
|
---|
398 | "nap:chemical+brothers");
|
---|
399 | assertNotNull(result);
|
---|
400 | String resultStr = new URL(result.getSystemId()).getFile();
|
---|
401 | assertTrue(toURLString(dtdFile).endsWith(resultStr));
|
---|
402 | } catch (Exception e) {
|
---|
403 | fail("resolveEntity() failed!" + e.toString());
|
---|
404 | }
|
---|
405 |
|
---|
406 | try {
|
---|
407 | Source result = catalog.resolve(uri, null);
|
---|
408 | assertNotNull(result);
|
---|
409 | String resultStr = new URL(result.getSystemId()).getFile();
|
---|
410 | assertTrue(toURLString(xmlFile).endsWith(resultStr));
|
---|
411 | } catch (Exception e) {
|
---|
412 | fail("resolve() failed!" + e.toString());
|
---|
413 | }
|
---|
414 | }
|
---|
415 | }
|
---|