diff options
author | Andreas Beeker <kiwiwings@apache.org> | 2019-12-03 21:56:47 +0000 |
---|---|---|
committer | Andreas Beeker <kiwiwings@apache.org> | 2019-12-03 21:56:47 +0000 |
commit | 08159e66a738c291572745cee8cc3617777dc2b5 (patch) | |
tree | 849e1239faa41d3493e57a0141a1c3d6b598fd9f /src/testcases | |
parent | dd3279df30eb9656c3a72f4a6a2445f777c7dd5a (diff) | |
download | poi-08159e66a738c291572745cee8cc3617777dc2b5.tar.gz poi-08159e66a738c291572745cee8cc3617777dc2b5.zip |
Sonar Fixes + Refactor scattered XML initializations to XMLHelper
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1870769 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/testcases')
-rw-r--r-- | src/testcases/org/apache/poi/util/TestStaxHelper.java | 64 | ||||
-rw-r--r-- | src/testcases/org/apache/poi/util/TestXMLHelper.java | 161 |
2 files changed, 161 insertions, 64 deletions
diff --git a/src/testcases/org/apache/poi/util/TestStaxHelper.java b/src/testcases/org/apache/poi/util/TestStaxHelper.java deleted file mode 100644 index 9b781fd6fb..0000000000 --- a/src/testcases/org/apache/poi/util/TestStaxHelper.java +++ /dev/null @@ -1,64 +0,0 @@ -/* ==================================================================== - Licensed to the Apache Software Foundation (ASF) under one or more - contributor license agreements. See the NOTICE file distributed with - this work for additional information regarding copyright ownership. - The ASF licenses this file to You under the Apache License, Version 2.0 - (the "License"); you may not use this file except in compliance with - the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -==================================================================== */ - -package org.apache.poi.util; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -import javax.xml.stream.XMLInputFactory; -import javax.xml.stream.XMLOutputFactory; -import javax.xml.stream.XMLStreamException; - -import org.junit.Test; - -/** - * Unit test for StaxHelper - */ -public class TestStaxHelper { - - /** - * test that newXMLInputFactory returns a factory with sensible defaults - */ - @Test - public void testNewXMLInputFactory() throws XMLStreamException { - XMLInputFactory factory = StaxHelper.newXMLInputFactory(); - assertEquals(true, factory.getProperty(XMLInputFactory.IS_NAMESPACE_AWARE)); - assertEquals(false, factory.getProperty(XMLInputFactory.IS_VALIDATING)); - assertEquals(false, factory.getProperty(XMLInputFactory.SUPPORT_DTD)); - assertEquals(false, factory.getProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES)); - } - - /** - * test that newXMLOutputFactory returns a factory with sensible defaults - */ - @Test - public void testNewXMLOutputFactory() { - XMLOutputFactory factory = StaxHelper.newXMLOutputFactory(); - assertEquals(true, factory.getProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES)); - } - - /** - * test that newXMLEventFactory returns a factory - */ - @Test - public void testNewXMLEventFactory() { - assertNotNull(StaxHelper.newXMLEventFactory()); - } - -} - diff --git a/src/testcases/org/apache/poi/util/TestXMLHelper.java b/src/testcases/org/apache/poi/util/TestXMLHelper.java new file mode 100644 index 0000000000..5536168545 --- /dev/null +++ b/src/testcases/org/apache/poi/util/TestXMLHelper.java @@ -0,0 +1,161 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.util; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertTrue; + +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 javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLOutputFactory; + +import org.junit.Test; +import org.xml.sax.InputSource; +import org.xml.sax.SAXNotRecognizedException; +import org.xml.sax.XMLReader; + +public class TestXMLHelper { + @Test + public void testDocumentBuilder() throws Exception { + DocumentBuilder documentBuilder = XMLHelper.newDocumentBuilder(); + assertNotSame(documentBuilder, XMLHelper.newDocumentBuilder()); + assertTrue(documentBuilder.isNamespaceAware()); + assertFalse(documentBuilder.isValidating()); + documentBuilder.parse(new InputSource(new ByteArrayInputStream("<xml></xml>".getBytes(StandardCharsets.UTF_8)))); + } + + @Test + public void testCreatingManyDocumentBuilders() throws Exception { + int limit = 1000; + ArrayList<CompletableFuture<DocumentBuilder>> futures = new ArrayList<>(); + for (int i = 0; i < limit; i++) { + futures.add(CompletableFuture.supplyAsync(XMLHelper::newDocumentBuilder)); + } + HashSet<DocumentBuilder> dbs = new HashSet<>(); + for (CompletableFuture<DocumentBuilder> future : futures) { + DocumentBuilder documentBuilder = future.get(10, TimeUnit.SECONDS); + assertTrue(documentBuilder.isNamespaceAware()); + dbs.add(documentBuilder); + } + assertEquals(limit, dbs.size()); + } + + @Test + public void testDocumentBuilderFactory() throws Exception { + try { + DocumentBuilderFactory dbf = XMLHelper.getDocumentBuilderFactory(); + assertTrue(dbf.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING)); + assertTrue(dbf.getFeature(XMLHelper.FEATURE_DISALLOW_DOCTYPE_DECL)); + assertFalse(dbf.getFeature(XMLHelper.FEATURE_LOAD_DTD_GRAMMAR)); + assertFalse(dbf.getFeature(XMLHelper.FEATURE_LOAD_EXTERNAL_DTD)); + } catch (AbstractMethodError e) { + // ignore exceptions from old parsers that don't support this API (https://bz.apache.org/bugzilla/show_bug.cgi?id=62692) + } + } + + @Test + public void testXMLReader() throws Exception { + XMLReader reader = XMLHelper.newXMLReader(); + assertNotSame(reader, XMLHelper.newXMLReader()); + try { + assertTrue(reader.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING)); + assertFalse(reader.getFeature(XMLHelper.FEATURE_LOAD_DTD_GRAMMAR)); + assertFalse(reader.getFeature(XMLHelper.FEATURE_LOAD_EXTERNAL_DTD)); + // assertEquals(XMLHelper.IGNORING_ENTITY_RESOLVER, reader.getEntityResolver()); + assertNotNull(reader.getProperty(XMLHelper.PROPERTY_ENTITY_EXPANSION_LIMIT)); + assertEquals("1", reader.getProperty(XMLHelper.PROPERTY_ENTITY_EXPANSION_LIMIT)); + assertNotNull(reader.getProperty(XMLHelper.PROPERTY_SECURITY_MANAGER)); + } catch (SAXNotRecognizedException e) { + // 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(StandardCharsets.UTF_8)))); + } + + @Test + public void testCreatingManyXMLReaders() throws Exception { + int limit = 1000; + ArrayList<CompletableFuture<XMLReader>> futures = new ArrayList<>(); + for (int i = 0; i < limit; i++) { + futures.add(CompletableFuture.supplyAsync(() -> { + try { + return XMLHelper.newXMLReader(); + } catch (RuntimeException e) { + throw e; + } catch (Exception e) { + throw new RuntimeException(e); + } + })); + } + HashSet<XMLReader> readers = new HashSet<>(); + for (CompletableFuture<XMLReader> future : futures) { + XMLReader reader = future.get(10, TimeUnit.SECONDS); + 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()); + } + + /** + * test that newXMLInputFactory returns a factory with sensible defaults + */ + @Test + public void testNewXMLInputFactory() { + XMLInputFactory factory = XMLHelper.newXMLInputFactory(); + assertTrue((boolean)factory.getProperty(XMLInputFactory.IS_NAMESPACE_AWARE)); + assertFalse((boolean)factory.getProperty(XMLInputFactory.IS_VALIDATING)); + assertFalse((boolean)factory.getProperty(XMLInputFactory.SUPPORT_DTD)); + assertFalse((boolean)factory.getProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES)); + } + + /** + * test that newXMLOutputFactory returns a factory with sensible defaults + */ + @Test + public void testNewXMLOutputFactory() { + XMLOutputFactory factory = XMLHelper.newXMLOutputFactory(); + assertTrue((boolean)factory.getProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES)); + } + + /** + * test that newXMLEventFactory returns a factory + */ + @Test + public void testNewXMLEventFactory() { + assertNotNull(XMLHelper.newXMLEventFactory()); + } +} |