Skip to content

Commit 9da5b7b

Browse files
committed
test: fix erratic error FileNotFoundException: http://.../catalog.dtd
Addresses erratic runtime exceptions because of catalog.dtd temporarily not being available online: java.io.FileNotFoundException: http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:2010) at ... at java.xml/javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:206) at org.eclipse.wildwebdeveloper.tests.TestXML.testXMLCatalog(TestXML.java:246) at java.base/java.lang.reflect.Method.invoke(Method.java:580) at java.base/java.util.ArrayList.forEach(ArrayList.java:1596) at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
1 parent db2a1fa commit 9da5b7b

File tree

3 files changed

+206
-6
lines changed

3 files changed

+206
-6
lines changed

org.eclipse.wildwebdeveloper.tests/src/org/eclipse/wildwebdeveloper/tests/TestXML.java

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@
1212
*******************************************************************************/
1313
package org.eclipse.wildwebdeveloper.tests;
1414

15-
import static org.junit.jupiter.api.Assertions.assertEquals;
16-
import static org.junit.jupiter.api.Assertions.assertFalse;
17-
import static org.junit.jupiter.api.Assertions.assertNotNull;
18-
import static org.junit.jupiter.api.Assertions.assertTrue;
15+
import static org.junit.jupiter.api.Assertions.*;
1916

2017
import java.io.File;
2118
import java.io.FileOutputStream;
@@ -24,7 +21,9 @@
2421
import java.nio.charset.StandardCharsets;
2522
import java.nio.file.Files;
2623
import java.nio.file.Path;
24+
import java.util.HashMap;
2725
import java.util.List;
26+
import java.util.concurrent.Callable;
2827
import java.util.jar.JarEntry;
2928
import java.util.jar.JarOutputStream;
3029
import java.util.stream.Collectors;
@@ -242,8 +241,8 @@ public void testXMLCatalog() throws Exception {
242241
// Find system.catalog in well known location from plugin
243242
File systemCatalog = plugin.getStateLocation().append("system-catalog.xml").toFile();
244243
// Parse system-catalog to check it
245-
Document systemCatalogDom = DocumentBuilderFactory.newDefaultInstance().newDocumentBuilder()
246-
.parse(systemCatalog);
244+
Document systemCatalogDom = runWithLocalCatalogOnlyForTestXMLCatalog(
245+
() -> DocumentBuilderFactory.newDefaultInstance().newDocumentBuilder().parse(systemCatalog));
247246

248247
// root
249248
Node catalogNode = systemCatalogDom.getLastChild();
@@ -276,4 +275,47 @@ public void testXMLCatalog() throws Exception {
276275
// Uninstall bundle once again
277276
catalogBundle.uninstall();
278277
}
278+
279+
/**
280+
* Workaround for erratic runtime exceptions because of catalog.dtd temporarily not being available online:
281+
* <pre>{@code
282+
* java.io.FileNotFoundException: http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd
283+
* at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:2010)
284+
* at ...
285+
* at java.xml/javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:206)
286+
* at org.eclipse.wildwebdeveloper.tests.TestXML.testXMLCatalog(TestXML.java:246)
287+
* at java.base/java.lang.reflect.Method.invoke(Method.java:580)
288+
* at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
289+
* at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
290+
* }</pre>
291+
*/
292+
private <T> T runWithLocalCatalogOnlyForTestXMLCatalog(Callable<T> callable) throws Exception {
293+
var origSysProps = new HashMap<String, String>();
294+
List.of( //
295+
"javax.xml.useCatalog", //
296+
"javax.xml.catalog.files", //
297+
"javax.xml.catalog.prefer", //
298+
"javax.xml.catalog.resolve"
299+
).forEach(k -> origSysProps.put(k, System.getProperty(k)));
300+
301+
var catalogRedirect = Path.of("src/org/eclipse/wildwebdeveloper/tests/catalog/oasis-catalog-redirect.xml");
302+
assertTrue(Files.exists(catalogRedirect));
303+
// enforce usage of local catalog.dtd file
304+
System.setProperty("javax.xml.useCatalog", "true");
305+
System.setProperty("javax.xml.catalog.files", catalogRedirect.toUri().toString());
306+
System.setProperty("javax.xml.catalog.prefer", "system");
307+
System.setProperty("javax.xml.catalog.resolve", "strict");
308+
try {
309+
return callable.call();
310+
} finally {
311+
// restore previous behaviour
312+
for (var e : origSysProps.entrySet()) {
313+
if (e.getValue() == null) {
314+
System.clearProperty(e.getKey());
315+
} else {
316+
System.setProperty(e.getKey(), e.getValue());
317+
}
318+
}
319+
}
320+
}
279321
}
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
<!-- copied from http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd -->
2+
<!-- $Id: catalog.dtd,v 1.10 2002/10/18 23:54:58 ndw Exp $ -->
3+
4+
<!ENTITY % pubIdChars "CDATA">
5+
<!ENTITY % publicIdentifier "%pubIdChars;">
6+
<!ENTITY % partialPublicIdentifier "%pubIdChars;">
7+
<!ENTITY % uriReference "CDATA">
8+
<!ENTITY % string "CDATA">
9+
<!ENTITY % systemOrPublic "(system|public)">
10+
11+
<!ENTITY % p "">
12+
<!ENTITY % s "">
13+
<!ENTITY % nsdecl "xmlns%s;">
14+
15+
<!ENTITY % catalog "%p;catalog">
16+
<!ENTITY % public "%p;public">
17+
<!ENTITY % system "%p;system">
18+
<!ENTITY % uri "%p;uri">
19+
<!ENTITY % rewriteSystem "%p;rewriteSystem">
20+
<!ENTITY % rewriteURI "%p;rewriteURI">
21+
<!ENTITY % delegatePublic "%p;delegatePublic">
22+
<!ENTITY % delegateSystem "%p;delegateSystem">
23+
<!ENTITY % delegateURI "%p;delegateURI">
24+
<!ENTITY % nextCatalog "%p;nextCatalog">
25+
<!ENTITY % group "%p;group">
26+
27+
<!ENTITY % local.catalog.mix "">
28+
<!ENTITY % local.catalog.attribs "">
29+
30+
<!ELEMENT %catalog; (%public;|%system;|%uri;
31+
|%rewriteSystem;|%rewriteURI;
32+
|%delegatePublic;|%delegateSystem;|%delegateURI;
33+
|%nextCatalog;|%group; %local.catalog.mix;)+>
34+
<!ATTLIST %catalog;
35+
%nsdecl; %uriReference; #FIXED
36+
'urn:oasis:names:tc:entity:xmlns:xml:catalog'
37+
prefer %systemOrPublic; #IMPLIED
38+
xml:base %uriReference; #IMPLIED
39+
%local.catalog.attribs;
40+
>
41+
42+
<!ENTITY % local.public.attribs "">
43+
44+
<!ELEMENT %public; EMPTY>
45+
<!ATTLIST %public;
46+
id ID #IMPLIED
47+
publicId %publicIdentifier; #REQUIRED
48+
uri %uriReference; #REQUIRED
49+
xml:base %uriReference; #IMPLIED
50+
%local.public.attribs;
51+
>
52+
53+
<!ENTITY % local.system.attribs "">
54+
55+
<!ELEMENT %system; EMPTY>
56+
<!ATTLIST %system;
57+
id ID #IMPLIED
58+
systemId %string; #REQUIRED
59+
uri %uriReference; #REQUIRED
60+
xml:base %uriReference; #IMPLIED
61+
%local.system.attribs;
62+
>
63+
64+
<!ENTITY % local.uri.attribs "">
65+
66+
<!ELEMENT %uri; EMPTY>
67+
<!ATTLIST %uri;
68+
id ID #IMPLIED
69+
name %string; #REQUIRED
70+
uri %uriReference; #REQUIRED
71+
xml:base %uriReference; #IMPLIED
72+
%local.uri.attribs;
73+
>
74+
75+
<!ENTITY % local.rewriteSystem.attribs "">
76+
77+
<!ELEMENT %rewriteSystem; EMPTY>
78+
<!ATTLIST %rewriteSystem;
79+
id ID #IMPLIED
80+
systemIdStartString %string; #REQUIRED
81+
rewritePrefix %string; #REQUIRED
82+
%local.rewriteSystem.attribs;
83+
>
84+
85+
<!ENTITY % local.rewriteURI.attribs "">
86+
87+
<!ELEMENT %rewriteURI; EMPTY>
88+
<!ATTLIST %rewriteURI;
89+
id ID #IMPLIED
90+
uriStartString %string; #REQUIRED
91+
rewritePrefix %string; #REQUIRED
92+
%local.rewriteURI.attribs;
93+
>
94+
95+
<!ENTITY % local.delegatePublic.attribs "">
96+
97+
<!ELEMENT %delegatePublic; EMPTY>
98+
<!ATTLIST %delegatePublic;
99+
id ID #IMPLIED
100+
publicIdStartString %partialPublicIdentifier; #REQUIRED
101+
catalog %uriReference; #REQUIRED
102+
xml:base %uriReference; #IMPLIED
103+
%local.delegatePublic.attribs;
104+
>
105+
106+
<!ENTITY % local.delegateSystem.attribs "">
107+
108+
<!ELEMENT %delegateSystem; EMPTY>
109+
<!ATTLIST %delegateSystem;
110+
id ID #IMPLIED
111+
systemIdStartString %string; #REQUIRED
112+
catalog %uriReference; #REQUIRED
113+
xml:base %uriReference; #IMPLIED
114+
%local.delegateSystem.attribs;
115+
>
116+
117+
<!ENTITY % local.delegateURI.attribs "">
118+
119+
<!ELEMENT %delegateURI; EMPTY>
120+
<!ATTLIST %delegateURI;
121+
id ID #IMPLIED
122+
uriStartString %string; #REQUIRED
123+
catalog %uriReference; #REQUIRED
124+
xml:base %uriReference; #IMPLIED
125+
%local.delegateURI.attribs;
126+
>
127+
128+
<!ENTITY % local.nextCatalog.attribs "">
129+
130+
<!ELEMENT %nextCatalog; EMPTY>
131+
<!ATTLIST %nextCatalog;
132+
id ID #IMPLIED
133+
catalog %uriReference; #REQUIRED
134+
xml:base %uriReference; #IMPLIED
135+
%local.nextCatalog.attribs;
136+
>
137+
138+
<!ENTITY % local.group.mix "">
139+
<!ENTITY % local.group.attribs "">
140+
141+
<!ELEMENT %group; (%public;|%system;|%uri;
142+
|%rewriteSystem;|%rewriteURI;
143+
|%delegatePublic;|%delegateSystem;|%delegateURI;
144+
|%nextCatalog; %local.group.mix;)+>
145+
<!ATTLIST %group;
146+
id ID #IMPLIED
147+
prefer %systemOrPublic; #IMPLIED
148+
xml:base %uriReference; #IMPLIED
149+
%local.group.attribs;
150+
>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE catalog PUBLIC "-//OASIS//DTD XML Catalogs V1.0//EN" "catalog-1.0.dtd">
3+
<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog" prefer="system">
4+
<!-- Map legacy and current systemIds to the bundled local DTD -->
5+
<system systemId="http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd" uri="catalog-1.0.dtd" />
6+
<system systemId="https://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd" uri="catalog-1.0.dtd" />
7+
<system systemId="https://docs.oasis-open.org/entity/release/1.0/catalog-1.0.dtd" uri="catalog-1.0.dtd" />
8+
</catalog>

0 commit comments

Comments
 (0)