You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2026/08/31 11:19:35 UTC
(commons-secure-xml) 01/02: Tests ExceptionInInitializerError in FallbackIgnoreURIResolver.
This is an automated email from the ASF dual-hosted git repository.
garydgregory pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/commons-secure-xml.git
commit 6709edd447cc8744061a03a3f29772560b248198
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Aug 31 07:05:45 2026 -0400
Tests ExceptionInInitializerError in FallbackIgnoreURIResolver.
---
pom.xml | 5 +++++
.../commons/xml/secure/FallbackIgnoreURIResolver.java | 6 +++++-
.../xml/secure/FallbackIgnoreURIResolverTest.java | 19 +++++++++++++++++++
3 files changed, 29 insertions(+), 1 deletion(-)
diff --git a/pom.xml b/pom.xml
index 135b335..276aa2f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -100,6 +100,11 @@ limitations under the License.
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-core</artifactId>
+ <scope>test</scope>
+ </dependency>
<!-- Reads the compiled classes to compute each secure class' transitive class closure (the same computation maven-shade minimizeJar performs). -->
<dependency>
<groupId>org.vafer</groupId>
diff --git a/src/main/java/org/apache/commons/xml/secure/FallbackIgnoreURIResolver.java b/src/main/java/org/apache/commons/xml/secure/FallbackIgnoreURIResolver.java
index 3a14701..277d233 100644
--- a/src/main/java/org/apache/commons/xml/secure/FallbackIgnoreURIResolver.java
+++ b/src/main/java/org/apache/commons/xml/secure/FallbackIgnoreURIResolver.java
@@ -72,8 +72,12 @@ final class FallbackIgnoreURIResolver implements URIResolver {
* configuration error} or if the implementation is not available or cannot be instantiated.
*/
private static Document newEmptyDocument() {
+ return newEmptyDocument(DocumentBuilderFactory.newInstance());
+ }
+
+ private static Document newEmptyDocument(final DocumentBuilderFactory factory) {
try {
- return DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
+ return factory.newDocumentBuilder().newDocument();
} catch (final ParserConfigurationException e) {
throw new ExceptionInInitializerError(e);
}
diff --git a/src/test/java/org/apache/commons/xml/secure/FallbackIgnoreURIResolverTest.java b/src/test/java/org/apache/commons/xml/secure/FallbackIgnoreURIResolverTest.java
index b479d7f..dbba632 100644
--- a/src/test/java/org/apache/commons/xml/secure/FallbackIgnoreURIResolverTest.java
+++ b/src/test/java/org/apache/commons/xml/secure/FallbackIgnoreURIResolverTest.java
@@ -19,7 +19,14 @@
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.URIResolver;
import javax.xml.transform.dom.DOMSource;
@@ -28,6 +35,18 @@
class FallbackIgnoreURIResolverTest {
+ @Test
+ void newEmptyDocumentThrowsExceptionInInitializerError() throws Exception {
+ final DocumentBuilderFactory factory = mock(DocumentBuilderFactory.class);
+ final ParserConfigurationException failure = new ParserConfigurationException("test");
+ when(factory.newDocumentBuilder()).thenThrow(failure);
+ final Method method = FallbackIgnoreURIResolver.class.getDeclaredMethod("newEmptyDocument", DocumentBuilderFactory.class);
+ method.setAccessible(true);
+ final InvocationTargetException exception = assertThrows(InvocationTargetException.class, () -> method.invoke(null, factory));
+ final ExceptionInInitializerError error = (ExceptionInInitializerError) exception.getCause();
+ assertSame(failure, error.getCause());
+ }
+
@Test
void resolvesDelegatedAndFallbackSources() throws Exception {
final DOMSource empty = new DOMSource();