You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestXMLHelper.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.util;
  16. import static org.junit.jupiter.api.Assertions.assertEquals;
  17. import static org.junit.jupiter.api.Assertions.assertFalse;
  18. import static org.junit.jupiter.api.Assertions.assertNotNull;
  19. import static org.junit.jupiter.api.Assertions.assertNotSame;
  20. import static org.junit.jupiter.api.Assertions.assertTrue;
  21. import java.io.ByteArrayInputStream;
  22. import java.nio.charset.StandardCharsets;
  23. import java.util.ArrayList;
  24. import java.util.HashSet;
  25. import java.util.concurrent.CompletableFuture;
  26. import java.util.concurrent.TimeUnit;
  27. import javax.xml.XMLConstants;
  28. import javax.xml.parsers.DocumentBuilder;
  29. import javax.xml.parsers.DocumentBuilderFactory;
  30. import javax.xml.stream.XMLInputFactory;
  31. import javax.xml.stream.XMLOutputFactory;
  32. import org.junit.jupiter.api.Test;
  33. import org.xml.sax.InputSource;
  34. import org.xml.sax.SAXNotRecognizedException;
  35. import org.xml.sax.XMLReader;
  36. class TestXMLHelper {
  37. @Test
  38. void testDocumentBuilder() throws Exception {
  39. DocumentBuilder documentBuilder = XMLHelper.newDocumentBuilder();
  40. assertNotSame(documentBuilder, XMLHelper.newDocumentBuilder());
  41. assertTrue(documentBuilder.isNamespaceAware());
  42. assertFalse(documentBuilder.isValidating());
  43. documentBuilder.parse(new InputSource(new ByteArrayInputStream("<xml></xml>".getBytes(StandardCharsets.UTF_8))));
  44. }
  45. @Test
  46. void testCreatingManyDocumentBuilders() throws Exception {
  47. int limit = 1000;
  48. ArrayList<CompletableFuture<DocumentBuilder>> futures = new ArrayList<>();
  49. for (int i = 0; i < limit; i++) {
  50. futures.add(CompletableFuture.supplyAsync(XMLHelper::newDocumentBuilder));
  51. }
  52. HashSet<DocumentBuilder> dbs = new HashSet<>();
  53. for (CompletableFuture<DocumentBuilder> future : futures) {
  54. DocumentBuilder documentBuilder = future.get(10, TimeUnit.SECONDS);
  55. assertTrue(documentBuilder.isNamespaceAware());
  56. dbs.add(documentBuilder);
  57. }
  58. assertEquals(limit, dbs.size());
  59. }
  60. @Test
  61. void testDocumentBuilderFactory() throws Exception {
  62. try {
  63. DocumentBuilderFactory dbf = XMLHelper.getDocumentBuilderFactory();
  64. assertTrue(dbf.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING));
  65. assertTrue(dbf.getFeature(XMLHelper.FEATURE_DISALLOW_DOCTYPE_DECL));
  66. assertFalse(dbf.getFeature(XMLHelper.FEATURE_LOAD_DTD_GRAMMAR));
  67. assertFalse(dbf.getFeature(XMLHelper.FEATURE_LOAD_EXTERNAL_DTD));
  68. } catch (AbstractMethodError e) {
  69. // ignore exceptions from old parsers that don't support this API (https://bz.apache.org/bugzilla/show_bug.cgi?id=62692)
  70. }
  71. }
  72. @Test
  73. void testXMLReader() throws Exception {
  74. XMLReader reader = XMLHelper.newXMLReader();
  75. assertNotSame(reader, XMLHelper.newXMLReader());
  76. try {
  77. assertTrue(reader.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING));
  78. assertFalse(reader.getFeature(XMLHelper.FEATURE_LOAD_DTD_GRAMMAR));
  79. assertFalse(reader.getFeature(XMLHelper.FEATURE_LOAD_EXTERNAL_DTD));
  80. // assertEquals(XMLHelper.IGNORING_ENTITY_RESOLVER, reader.getEntityResolver());
  81. assertNotNull(reader.getProperty(XMLHelper.PROPERTY_ENTITY_EXPANSION_LIMIT));
  82. assertEquals("1", reader.getProperty(XMLHelper.PROPERTY_ENTITY_EXPANSION_LIMIT));
  83. assertNotNull(reader.getProperty(XMLHelper.PROPERTY_SECURITY_MANAGER));
  84. } catch (SAXNotRecognizedException e) {
  85. // ignore exceptions from old parsers that don't support these features
  86. // (https://bz.apache.org/bugzilla/show_bug.cgi?id=62692)
  87. }
  88. reader.parse(new InputSource(new ByteArrayInputStream("<xml></xml>".getBytes(StandardCharsets.UTF_8))));
  89. }
  90. @Test
  91. void testCreatingManyXMLReaders() throws Exception {
  92. int limit = 1000;
  93. ArrayList<CompletableFuture<XMLReader>> futures = new ArrayList<>();
  94. for (int i = 0; i < limit; i++) {
  95. futures.add(CompletableFuture.supplyAsync(() -> {
  96. try {
  97. return XMLHelper.newXMLReader();
  98. } catch (RuntimeException e) {
  99. throw e;
  100. } catch (Exception e) {
  101. throw new RuntimeException(e);
  102. }
  103. }));
  104. }
  105. HashSet<XMLReader> readers = new HashSet<>();
  106. for (CompletableFuture<XMLReader> future : futures) {
  107. XMLReader reader = future.get(10, TimeUnit.SECONDS);
  108. try {
  109. assertTrue(reader.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING));
  110. } catch (SAXNotRecognizedException e) {
  111. // can happen for older XML Parsers, e.g. we have a CI Job which runs with Xerces XML Parser
  112. assertTrue(reader.getClass().getName().contains("org.apache.xerces"),
  113. "Had Exception about not-recognized SAX feature: " + e + " which is only expected" +
  114. " for Xerces XML Parser, but had parser: " + reader);
  115. }
  116. readers.add(reader);
  117. }
  118. assertEquals(limit, readers.size());
  119. }
  120. /**
  121. * test that newXMLInputFactory returns a factory with sensible defaults
  122. */
  123. @Test
  124. void testNewXMLInputFactory() {
  125. XMLInputFactory factory = XMLHelper.newXMLInputFactory();
  126. assertTrue((boolean)factory.getProperty(XMLInputFactory.IS_NAMESPACE_AWARE));
  127. assertFalse((boolean)factory.getProperty(XMLInputFactory.IS_VALIDATING));
  128. assertFalse((boolean)factory.getProperty(XMLInputFactory.SUPPORT_DTD));
  129. assertFalse((boolean)factory.getProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES));
  130. }
  131. /**
  132. * test that newXMLOutputFactory returns a factory with sensible defaults
  133. */
  134. @Test
  135. void testNewXMLOutputFactory() {
  136. XMLOutputFactory factory = XMLHelper.newXMLOutputFactory();
  137. assertTrue((boolean)factory.getProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES));
  138. }
  139. /**
  140. * test that newXMLEventFactory returns a factory
  141. */
  142. @Test
  143. void testNewXMLEventFactory() {
  144. assertNotNull(XMLHelper.newXMLEventFactory());
  145. }
  146. }