From 5329f25b0a8ebe88c6049b0abc292824489d2193 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Sun, 10 Mar 2019 08:18:54 +0000 Subject: [PATCH] Adjust test to not fail with Xerces XML Parser, fix some IDE warnings git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1855139 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/ooxml/util/DocumentHelper.java | 8 ++++---- .../java/org/apache/poi/ooxml/util/SAXHelper.java | 14 ++++---------- .../apache/poi/ooxml/util/TestDocumentHelper.java | 13 +++++++------ .../org/apache/poi/ooxml/util/TestSAXHelper.java | 13 ++++++++++--- 4 files changed, 25 insertions(+), 23 deletions(-) diff --git a/src/ooxml/java/org/apache/poi/ooxml/util/DocumentHelper.java b/src/ooxml/java/org/apache/poi/ooxml/util/DocumentHelper.java index 734f169dc1..79f3876482 100644 --- a/src/ooxml/java/org/apache/poi/ooxml/util/DocumentHelper.java +++ b/src/ooxml/java/org/apache/poi/ooxml/util/DocumentHelper.java @@ -45,11 +45,11 @@ public final class DocumentHelper { private static class DocHelperErrorHandler implements ErrorHandler { - public void warning(SAXParseException exception) throws SAXException { + public void warning(SAXParseException exception) { printError(POILogger.WARN, exception); } - public void error(SAXParseException exception) throws SAXException { + public void error(SAXParseException exception) { printError(POILogger.ERROR, exception); } @@ -111,7 +111,7 @@ public final class DocumentHelper { trySetXercesSecurityManager(documentBuilderFactory); } - private static void trySetFeature(DocumentBuilderFactory dbf, String feature, boolean enabled) { + private static void trySetFeature(@SuppressWarnings("SameParameterValue") DocumentBuilderFactory dbf, String feature, boolean enabled) { try { dbf.setFeature(feature, enabled); } catch (Exception e) { @@ -121,7 +121,7 @@ public final class DocumentHelper { } } - private static void trySetXercesSecurityManager(DocumentBuilderFactory dbf) { + private static void trySetXercesSecurityManager(@SuppressWarnings("SameParameterValue") DocumentBuilderFactory dbf) { // Try built-in JVM one first, standalone if not for (String securityManagerClassName : new String[]{ //"com.sun.org.apache.xerces.internal.util.SecurityManager", diff --git a/src/ooxml/java/org/apache/poi/ooxml/util/SAXHelper.java b/src/ooxml/java/org/apache/poi/ooxml/util/SAXHelper.java index c954b46ca1..ede0ac4582 100644 --- a/src/ooxml/java/org/apache/poi/ooxml/util/SAXHelper.java +++ b/src/ooxml/java/org/apache/poi/ooxml/util/SAXHelper.java @@ -17,7 +17,6 @@ package org.apache.poi.ooxml.util; -import java.io.IOException; import java.io.StringReader; import java.lang.reflect.Method; import java.util.concurrent.TimeUnit; @@ -54,13 +53,7 @@ public final class SAXHelper { return xmlReader; } - static final EntityResolver IGNORING_ENTITY_RESOLVER = new EntityResolver() { - @Override - public InputSource resolveEntity(String publicId, String systemId) - throws SAXException, IOException { - return new InputSource(new StringReader("")); - } - }; + static final EntityResolver IGNORING_ENTITY_RESOLVER = (publicId, systemId) -> new InputSource(new StringReader("")); private static final SAXParserFactory saxFactory; static { @@ -84,7 +77,8 @@ public final class SAXHelper { } } - private static void trySetSAXFeature(SAXParserFactory spf, String feature, boolean flag) { + private static void trySetSAXFeature(@SuppressWarnings("SameParameterValue") SAXParserFactory spf, + String feature, boolean flag) { try { spf.setFeature(feature, flag); } catch (Exception e) { @@ -94,7 +88,7 @@ public final class SAXHelper { } } - private static void trySetSAXFeature(XMLReader xmlReader, String feature) { + private static void trySetSAXFeature(XMLReader xmlReader, @SuppressWarnings("SameParameterValue") String feature) { try { xmlReader.setFeature(feature, true); } catch (Exception e) { diff --git a/src/ooxml/testcases/org/apache/poi/ooxml/util/TestDocumentHelper.java b/src/ooxml/testcases/org/apache/poi/ooxml/util/TestDocumentHelper.java index eb8d28ba72..3c07f25d5b 100644 --- a/src/ooxml/testcases/org/apache/poi/ooxml/util/TestDocumentHelper.java +++ b/src/ooxml/testcases/org/apache/poi/ooxml/util/TestDocumentHelper.java @@ -18,17 +18,20 @@ package org.apache.poi.ooxml.util; import org.junit.Test; import org.xml.sax.InputSource; -import org.xml.sax.XMLReader; import javax.xml.XMLConstants; import javax.xml.parsers.DocumentBuilder; import java.io.ByteArrayInputStream; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashSet; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertTrue; public class TestDocumentHelper { @Test @@ -37,7 +40,7 @@ public class TestDocumentHelper { assertNotSame(documentBuilder, DocumentHelper.newDocumentBuilder()); assertTrue(documentBuilder.isNamespaceAware()); assertFalse(documentBuilder.isValidating()); - documentBuilder.parse(new InputSource(new ByteArrayInputStream("".getBytes("UTF-8")))); + documentBuilder.parse(new InputSource(new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8)))); } @Test @@ -45,9 +48,7 @@ public class TestDocumentHelper { int limit = 1000; ArrayList> futures = new ArrayList<>(); for(int i = 0; i < limit; i++) { - futures.add(CompletableFuture.supplyAsync(() -> { - return DocumentHelper.newDocumentBuilder(); - })); + futures.add(CompletableFuture.supplyAsync(DocumentHelper::newDocumentBuilder)); } HashSet dbs = new HashSet<>(); for(CompletableFuture future : futures) { diff --git a/src/ooxml/testcases/org/apache/poi/ooxml/util/TestSAXHelper.java b/src/ooxml/testcases/org/apache/poi/ooxml/util/TestSAXHelper.java index 4087f25157..536194dea4 100644 --- a/src/ooxml/testcases/org/apache/poi/ooxml/util/TestSAXHelper.java +++ b/src/ooxml/testcases/org/apache/poi/ooxml/util/TestSAXHelper.java @@ -19,13 +19,13 @@ package org.apache.poi.ooxml.util; import static org.junit.Assert.*; import java.io.ByteArrayInputStream; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashSet; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; import javax.xml.XMLConstants; -import javax.xml.parsers.DocumentBuilder; import org.junit.Test; import org.xml.sax.InputSource; @@ -49,7 +49,7 @@ public class TestSAXHelper { // ignore exceptions from old parsers that don't support these features // (https://bz.apache.org/bugzilla/show_bug.cgi?id=62692) } - reader.parse(new InputSource(new ByteArrayInputStream("".getBytes("UTF-8")))); + reader.parse(new InputSource(new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8)))); } @Test @@ -70,7 +70,14 @@ public class TestSAXHelper { HashSet readers = new HashSet<>(); for(CompletableFuture future : futures) { XMLReader reader = future.get(10, TimeUnit.SECONDS); - assertTrue(reader.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING)); + try { + assertTrue(reader.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING)); + } catch (SAXNotRecognizedException e) { + // can happen for older XML Parsers, e.g. we have a CI Job which runs with Xerces XML Parser + assertTrue("Had Exception about not-recognized SAX feature: " + e + " which is only expected" + + " for Xerces XML Parser, but had parser: " + reader, + reader.getClass().getName().contains("org.apache.xerces")); + } readers.add(reader); } assertEquals(limit, readers.size()); -- 2.39.5