[nobug] Fix external references from disturbing build (always a bad practice)
diff --git a/tests/org.eclipse.wst.xml.xpath2.processor.tests/src/org/eclipse/wst/xml/xpath2/processor/test/AbstractPsychoPathTest.java b/tests/org.eclipse.wst.xml.xpath2.processor.tests/src/org/eclipse/wst/xml/xpath2/processor/test/AbstractPsychoPathTest.java index 6274e4e..bdf94bb 100644 --- a/tests/org.eclipse.wst.xml.xpath2.processor.tests/src/org/eclipse/wst/xml/xpath2/processor/test/AbstractPsychoPathTest.java +++ b/tests/org.eclipse.wst.xml.xpath2.processor.tests/src/org/eclipse/wst/xml/xpath2/processor/test/AbstractPsychoPathTest.java
@@ -25,6 +25,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; +import java.net.MalformedURLException; import java.net.URL; import java.nio.charset.Charset; import java.util.HashMap; @@ -80,7 +81,10 @@ import org.w3c.dom.ls.DOMImplementationLS; import org.w3c.dom.ls.LSOutput; import org.w3c.dom.ls.LSSerializer; +import org.xml.sax.EntityResolver; +import org.xml.sax.InputSource; import org.xml.sax.SAXException; +import org.xml.sax.ext.EntityResolver2; public class AbstractPsychoPathTest extends XMLTestCase { @@ -116,15 +120,72 @@ protected void loadDOMDocument(URL fileURL) throws IOException, DOMLoaderException { - InputStream is = fileURL.openStream(); + InputStream is = testResolve(fileURL); DOMLoader domloader = new XercesLoader(); domloader.set_validating(false); domDoc = domloader.load(is); domDoc.setDocumentURI(fileURL.toString()); } + + private InputStream testResolve(URL url) throws IOException { + if (url.getProtocol().equals("http")) { + return AbstractPsychoPathTest.class.getResourceAsStream("/org/eclipse/wst/xml/xpath2/processor/test/" + url.getFile()); + } else { + return url.openStream(); + } + } + + protected InputSource getTestSource(String systemId) { + if (systemId.startsWith("http://")) { + try { + URL u = new URL(systemId); + InputSource inputSource = new InputSource(testResolve(u)); + inputSource.setSystemId(systemId); + return inputSource; + } catch (MalformedURLException e) { + throw new RuntimeException(e); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + return new InputSource(systemId); + } + + protected EntityResolver makeTestResolver() { + return new EntityResolver2() { + + public InputSource resolveEntity(String publicId, String systemId) { + if (systemId.startsWith("http://")) { + URL u; + try { + u = new URL(systemId); + return new InputSource(testResolve(u)); + } catch (MalformedURLException e) { + throw new RuntimeException(e); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + return new InputSource(systemId); + } + + public InputSource getExternalSubset(String publicId, String systemId) + throws SAXException, IOException { + return resolveEntity(publicId, systemId); + } + + public InputSource resolveEntity(String name, + String publicId, + String baseURI, + String systemId) throws SAXException, IOException { + return resolveEntity(publicId, systemId); + } + + }; + } protected void loadDOMDocument(URL fileURL, Schema schema) throws IOException, DOMLoaderException { - InputStream is = fileURL.openStream(); + InputStream is = testResolve(fileURL); DOMLoader domloader = new XercesLoader(schema); domloader.set_validating(false); domDoc = domloader.load(is); @@ -134,8 +195,8 @@ protected void load2DOMDocument(URL fileURL, URL fileURL2) throws IOException, DOMLoaderException { - InputStream is = fileURL.openStream(); - InputStream is2 = fileURL2.openStream(); + InputStream is = testResolve(fileURL); + InputStream is2 = testResolve(fileURL2); DOMLoader domloader = new XercesLoader(); domloader.set_validating(false); @@ -163,8 +224,8 @@ protected void loadDOMDocument(URL fileURL, URL schemaURL) throws IOException, DOMLoaderException, SAXException { - InputStream is = fileURL.openStream(); - InputStream schemaIs = schemaURL.openStream(); + InputStream is = testResolve(fileURL); + InputStream schemaIs = testResolve(schemaURL); Schema jaxpSchema = getSchema(schemaIs); DOMLoader domloader = new XercesLoader(jaxpSchema); domloader.set_validating(false); @@ -182,7 +243,7 @@ protected XSModel getGrammar(URL schemaURL) throws IOException, SAXException { - InputStream schemaIs = schemaURL.openStream(); + InputStream schemaIs = testResolve(schemaURL); SchemaFactory sf = SchemaFactory .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = sf.newSchema(new StreamSource(schemaIs)); @@ -303,7 +364,7 @@ try { URL entryUrl = bundle.getEntry(xqFile); - InputStream isxq = entryUrl.openStream(); + InputStream isxq = testResolve(entryUrl); if (dynamicContext.base_uri().string_value() == null) dynamicContext.set_base_uri(entryUrl.toString()); BufferedReader xqreader = new BufferedReader(new InputStreamReader(
diff --git a/tests/org.eclipse.wst.xml.xpath2.processor.tests/src/org/eclipse/wst/xml/xpath2/processor/test/Bug269833.java b/tests/org.eclipse.wst.xml.xpath2.processor.tests/src/org/eclipse/wst/xml/xpath2/processor/test/Bug269833.java index 77fef61..02ce96a 100644 --- a/tests/org.eclipse.wst.xml.xpath2.processor.tests/src/org/eclipse/wst/xml/xpath2/processor/test/Bug269833.java +++ b/tests/org.eclipse.wst.xml.xpath2.processor.tests/src/org/eclipse/wst/xml/xpath2/processor/test/Bug269833.java
@@ -37,7 +37,7 @@ @Override protected void setUp() throws Exception { - URL fileURL = new URL("http://www.w3schools.com/xml/note.xml"); + URL fileURL = new URL("http://resolve-locally/xml/note.xml"); loadDOMDocument(fileURL); } @@ -56,6 +56,6 @@ String actual = rs.first().string_value(); - assertEquals("Tove", actual); + assertEquals("Self", actual); } } \ No newline at end of file
diff --git a/tests/org.eclipse.wst.xml.xpath2.processor.tests/src/org/eclipse/wst/xml/xpath2/processor/test/TestBugs.java b/tests/org.eclipse.wst.xml.xpath2.processor.tests/src/org/eclipse/wst/xml/xpath2/processor/test/TestBugs.java index 43df338..48e2641 100644 --- a/tests/org.eclipse.wst.xml.xpath2.processor.tests/src/org/eclipse/wst/xml/xpath2/processor/test/TestBugs.java +++ b/tests/org.eclipse.wst.xml.xpath2.processor.tests/src/org/eclipse/wst/xml/xpath2/processor/test/TestBugs.java
@@ -67,6 +67,7 @@ import org.eclipse.wst.xml.xpath2.processor.internal.types.XSDuration; import org.eclipse.wst.xml.xpath2.processor.internal.types.XSFloat; import org.osgi.framework.Bundle; +import org.xml.sax.InputSource; public class TestBugs extends AbstractPsychoPathTest { @@ -276,22 +277,22 @@ // DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // DocumentBuilder docBuilder = dbf.newDocumentBuilder(); - loadDOMDocument(new URL("http://www.w3schools.com/xml/note.xml")); + loadDOMDocument(new URL("http://resolved-locally/xml/note.xml")); // for testing this bug, we read the XML document from the web. // this ensures, that base-uri property of DOM is not null. - // domDoc = docBuilder.parse("http://www.w3schools.com/xml/note.xml"); + // domDoc = docBuilder.parse("http://resolved-locally/xml/note.xml"); // we pass XSModel as null for this test case. Otherwise, we would // get an exception. DynamicContext dc = setupDynamicContext(null); - String xpath = "base-uri(note) eq xs:anyURI('http://www.w3schools.com/xml/note.xml')"; + String xpath = "base-uri(note) eq xs:anyURI('http://resolved-locally/xml/note.xml')"; // please note: The below XPath would also work, with base-uri using // arity 0. // String xpath = - // "note/base-uri() eq xs:anyURI('http://www.w3schools.com/xml/note.xml')"; + // "note/base-uri() eq xs:anyURI('http://resolved-locally/xml/note.xml')"; XPath path = compileXPath(dc, xpath); @@ -309,12 +310,13 @@ // Bug 274731 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = dbf.newDocumentBuilder(); - - domDoc = docBuilder.parse("http://www.w3schools.com/xml/note.xml"); + + InputSource inputSource = getTestSource("http://resolved-locally/xml/note.xml"); + domDoc = docBuilder.parse(inputSource); DynamicContext dc = setupDynamicContext(null); - String xpath = "document-uri(/) eq xs:anyURI('http://www.w3schools.com/xml/note.xml')"; + String xpath = "document-uri(/) eq xs:anyURI('http://resolved-locally/xml/note.xml')"; XPath path = compileXPath(dc, xpath);
diff --git a/tests/org.eclipse.wst.xml.xpath2.processor.tests/src/org/eclipse/wst/xml/xpath2/processor/test/xml/note.xml b/tests/org.eclipse.wst.xml.xpath2.processor.tests/src/org/eclipse/wst/xml/xpath2/processor/test/xml/note.xml new file mode 100644 index 0000000..d4966d1 --- /dev/null +++ b/tests/org.eclipse.wst.xml.xpath2.processor.tests/src/org/eclipse/wst/xml/xpath2/processor/test/xml/note.xml
@@ -0,0 +1,7 @@ +<?xml version="1.0" encoding="UTF-8"?> +<note> + <to>Self</to> + <from>XPath2 Developers</from> + <heading>Reminder</heading> + <body>Don't reference external entities in tests</body> +</note> \ No newline at end of file