Browse Source

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
pull/143/head
Dominik Stadler 5 years ago
parent
commit
0bf0822195

+ 4
- 4
src/ooxml/java/org/apache/poi/ooxml/util/DocumentHelper.java View 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",

+ 4
- 10
src/ooxml/java/org/apache/poi/ooxml/util/SAXHelper.java View 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) {

+ 7
- 6
src/ooxml/testcases/org/apache/poi/ooxml/util/TestDocumentHelper.java View 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) {

+ 10
- 3
src/ooxml/testcases/org/apache/poi/ooxml/util/TestSAXHelper.java View 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());

Loading…
Cancel
Save