]> source.dussan.org Git - poi.git/commitdiff
Adjust test to not fail with Xerces XML Parser, fix some IDE warnings
authorDominik Stadler <centic@apache.org>
Sun, 10 Mar 2019 08:18:54 +0000 (08:18 +0000)
committerDominik Stadler <centic@apache.org>
Sun, 10 Mar 2019 08:18:54 +0000 (08:18 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1855139 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/ooxml/util/DocumentHelper.java
src/ooxml/java/org/apache/poi/ooxml/util/SAXHelper.java
src/ooxml/testcases/org/apache/poi/ooxml/util/TestDocumentHelper.java
src/ooxml/testcases/org/apache/poi/ooxml/util/TestSAXHelper.java

index 734f169dc1c6b617d2e5980ac01bc30c992e5e7b..79f3876482bbbfb2c6b23a020e66b43b536be209 100644 (file)
@@ -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",
index c954b46ca1de9dcce9e42aa193cc2b7a01d573b0..ede0ac4582e7075a16d0f8ee79c6c72f5c154640 100644 (file)
@@ -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) {
index eb8d28ba720145f8b77f351365ea9fea50ab5625..3c07f25d5b47913703c87299d697031b28c201e6 100644 (file)
@@ -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("<xml></xml>".getBytes("UTF-8"))));
+        documentBuilder.parse(new InputSource(new ByteArrayInputStream("<xml></xml>".getBytes(StandardCharsets.UTF_8))));
     }
 
     @Test
@@ -45,9 +48,7 @@ public class TestDocumentHelper {
         int limit = 1000;
         ArrayList<CompletableFuture<DocumentBuilder>> futures = new ArrayList<>();
         for(int i = 0; i < limit; i++) {
-            futures.add(CompletableFuture.supplyAsync(() -> {
-                return DocumentHelper.newDocumentBuilder();
-            }));
+            futures.add(CompletableFuture.supplyAsync(DocumentHelper::newDocumentBuilder));
         }
         HashSet<DocumentBuilder> dbs = new HashSet<>();
         for(CompletableFuture<DocumentBuilder> future : futures) {
index 4087f2515761ed74471b03a9b0a56836c290732d..536194dea45ab567f53cb279f44dd5313daa748c 100644 (file)
@@ -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("<xml></xml>".getBytes("UTF-8"))));
+        reader.parse(new InputSource(new ByteArrayInputStream("<xml></xml>".getBytes(StandardCharsets.UTF_8))));
     }
 
     @Test
@@ -70,7 +70,14 @@ public class TestSAXHelper {
         HashSet<XMLReader> readers = new HashSet<>();
         for(CompletableFuture<XMLReader> 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());