Readd some deleted tests

This commit is contained in:
Clément Fournier
2020-12-14 13:35:18 +01:00
parent ca4b96ef13
commit d877bdb779
2 changed files with 30 additions and 0 deletions

View File

@@ -4,6 +4,7 @@
package net.sourceforge.pmd.lang.xml.ast.internal;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
@@ -16,6 +17,7 @@ import javax.xml.parsers.ParserConfigurationException;
import org.apache.commons.io.IOUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
@@ -25,6 +27,8 @@ import net.sourceforge.pmd.lang.xml.ast.XmlNode;
public final class XmlParserImpl {
// never throws on unresolved resource
private static final EntityResolver SILENT_ENTITY_RESOLVER = (publicId, systemId) -> new InputSource(new ByteArrayInputStream("".getBytes()));
private final Map<org.w3c.dom.Node, XmlNode> nodeCache = new HashMap<>();
@@ -44,6 +48,7 @@ public final class XmlParserImpl {
dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
DocumentBuilder documentBuilder = dbf.newDocumentBuilder();
documentBuilder.setEntityResolver(SILENT_ENTITY_RESOLVER);
return documentBuilder.parse(new InputSource(new StringReader(xmlData)));
} catch (ParserConfigurationException | SAXException | IOException e) {
throw new ParseException(e);

View File

@@ -40,4 +40,29 @@ public class XmlParserTest extends BaseTreeDumpTest {
public void testBug1518() {
doTest("bug1518");
}
@Test
public void dtdIsNotLookedUp() {
// no exception should be thrown
XmlParsingHelper.XML.parse(
"<!DOCTYPE struts-config PUBLIC "
+ " \"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN \" "
+ " \"http://jakarta.inexistinghost.org/struts/dtds/struts-config_1_1.dtd\" >"
+ "<struts-config/>");
}
@Test
public void xsdIsNotLookedUp() {
// no exception should be thrown
XmlParsingHelper.XML.parse(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?> "
+ "<web-app xmlns=\"http://java.sun.com/xml/ns/javaee\" "
+ "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
+ "xsi:schemaLocation=\"http://java.sun.com/xml/ns/javaee http://java.inexisting.com/xml/ns/javaee/web-app_2_5.xsd\" "
+ "version=\"2.5\">"
+ "</web-app>");
}
}