This is an automated email from the ASF dual-hosted git repository. robbie pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/activemq-artemis.git
commit f64d9184066f33a08bf04d6ce75a7649af17b87e Author: Robbie Gemmell <[email protected]> AuthorDate: Tue May 21 16:18:38 2024 +0100 ARTEMIS-4778: rework tests manipulating Xxe config so it is reset after class, and not updated at all for skipped tests --- .../activemq/artemis/utils/XmlProviderTest.java | 41 ++++++++++++++- .../core/config/impl/FileConfigurationTest.java | 61 ++++++++++++++++++---- .../config/impl/FileXIncludeConfigurationTest.java | 18 ++++--- .../impl/FileXIncludeSchemaConfigurationTest.java | 18 ++++--- 4 files changed, 115 insertions(+), 23 deletions(-) diff --git a/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/XmlProviderTest.java b/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/XmlProviderTest.java index 989ae50f6e..86ce633949 100644 --- a/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/XmlProviderTest.java +++ b/artemis-commons/src/test/java/org/apache/activemq/artemis/utils/XmlProviderTest.java @@ -21,30 +21,67 @@ import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamSource; import javax.xml.validation.Validator; import java.io.File; +import java.lang.invoke.MethodHandles; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.Map; +import org.apache.activemq.artemis.tests.util.ArtemisTestCase; +import org.junit.AfterClass; import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; @RunWith(Parameterized.class) -public class XmlProviderTest { +public class XmlProviderTest extends ArtemisTestCase { + + private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + private static Boolean origXxeEnabled; + + protected boolean xxeEnabled; @Parameterized.Parameters(name = "xxeEnabled={0}") public static Collection getParameters() { return Arrays.asList(new Boolean[]{true, false}); } - public XmlProviderTest(boolean xxeEnabled) { + @BeforeClass + public static void beforeAll() { + if (origXxeEnabled == null) { + origXxeEnabled = XmlProvider.isXxeEnabled(); + } + + logger.trace("BeforeAll - origXxeEnabled={}, isXxeEnabled={}", origXxeEnabled, XmlProvider.isXxeEnabled()); + } + + @AfterClass + public static void afterAll() { + logger.trace("AfterAll - origXxeEnabled={}, isXxeEnabled={} ", origXxeEnabled, XmlProvider.isXxeEnabled()); + if (origXxeEnabled != null) { + logger.trace("AfterAll - Resetting XxeEnabled={}", origXxeEnabled); + XmlProvider.setXxeEnabled(origXxeEnabled); + } + } + + @Before + public void setUp() { + logger.trace("Running setUp - xxeEnabled={}", xxeEnabled); XmlProvider.setXxeEnabled(xxeEnabled); } + public XmlProviderTest(boolean xxeEnabled) { + this.xxeEnabled = xxeEnabled; + } + @Test public void testDocument() throws Exception { DocumentBuilder documentBuilder = XmlProvider.newDocumentBuilder(); diff --git a/artemis-server/src/test/java/org/apache/activemq/artemis/core/config/impl/FileConfigurationTest.java b/artemis-server/src/test/java/org/apache/activemq/artemis/core/config/impl/FileConfigurationTest.java index 214b6e7b65..f9d4958336 100644 --- a/artemis-server/src/test/java/org/apache/activemq/artemis/core/config/impl/FileConfigurationTest.java +++ b/artemis-server/src/test/java/org/apache/activemq/artemis/core/config/impl/FileConfigurationTest.java @@ -24,6 +24,7 @@ import java.nio.file.Path; import java.nio.file.StandardCopyOption; import java.util.Arrays; import java.util.Collection; +import java.lang.invoke.MethodHandles; import java.util.Collections; import java.util.HashSet; import java.util.List; @@ -76,39 +77,81 @@ import org.apache.activemq.artemis.logs.AssertionLoggerHandler; import org.apache.activemq.artemis.utils.RandomUtil; import org.apache.activemq.artemis.utils.XmlProvider; import org.apache.activemq.artemis.utils.critical.CriticalAnalyzerPolicy; +import org.junit.After; import org.junit.AfterClass; import org.junit.Assert; +import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; @RunWith(Parameterized.class) public class FileConfigurationTest extends AbstractConfigurationTestBase { + private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + private static Boolean origXxeEnabled; + + protected boolean xxeEnabled; + + @Parameterized.Parameters(name = "xxeEnabled={0}") + public static Collection getParameters() { + return Arrays.asList(new Boolean[] {true, false}); + } + @BeforeClass - public static void setupProperties() { + public static void beforeAll() { + if (origXxeEnabled == null) { + origXxeEnabled = XmlProvider.isXxeEnabled(); + } + + logger.trace("BeforeAll - origXxeEnabled={}, isXxeEnabled={}", origXxeEnabled, XmlProvider.isXxeEnabled()); + } + + @AfterClass + public static void afterAll() { + logger.trace("AfterAll - origXxeEnabled={}, isXxeEnabled={} ", origXxeEnabled, XmlProvider.isXxeEnabled()); + if (origXxeEnabled != null) { + logger.trace("AfterAll - Resetting XxeEnabled={}", origXxeEnabled); + XmlProvider.setXxeEnabled(origXxeEnabled); + } + } + + @Override + @Before + public void setUp() throws Exception { + logger.trace("Running setUp - xxeEnabled={}", xxeEnabled); + XmlProvider.setXxeEnabled(xxeEnabled); + + setupProperties(); + + super.setUp(); + } + + @After + public void afterEach() { + clearProperties(); + } + + public void setupProperties() { System.setProperty("a2Prop", "a2"); System.setProperty("falseProp", "false"); System.setProperty("trueProp", "true"); System.setProperty("ninetyTwoProp", "92"); } - @AfterClass - public static void clearProperties() { + public void clearProperties() { System.clearProperty("a2Prop"); System.clearProperty("falseProp"); System.clearProperty("trueProp"); System.clearProperty("ninetyTwoProp"); } - @Parameterized.Parameters(name = "xxeEnabled={0}") - public static Collection getParameters() { - return Arrays.asList(new Boolean[] {true, false}); - } - public FileConfigurationTest(boolean xxeEnabled) { - XmlProvider.setXxeEnabled(xxeEnabled); + this.xxeEnabled = xxeEnabled; } protected String getConfigurationName() { diff --git a/artemis-server/src/test/java/org/apache/activemq/artemis/core/config/impl/FileXIncludeConfigurationTest.java b/artemis-server/src/test/java/org/apache/activemq/artemis/core/config/impl/FileXIncludeConfigurationTest.java index 8422d9fd7d..9004997665 100644 --- a/artemis-server/src/test/java/org/apache/activemq/artemis/core/config/impl/FileXIncludeConfigurationTest.java +++ b/artemis-server/src/test/java/org/apache/activemq/artemis/core/config/impl/FileXIncludeConfigurationTest.java @@ -16,9 +16,8 @@ */ package org.apache.activemq.artemis.core.config.impl; -import org.junit.AfterClass; import org.junit.Assume; -import org.junit.BeforeClass; +import org.junit.Before; public class FileXIncludeConfigurationTest extends FileConfigurationTest { @@ -29,11 +28,18 @@ public class FileXIncludeConfigurationTest extends FileConfigurationTest { public FileXIncludeConfigurationTest(boolean xxeEnabled) { super(xxeEnabled); + } + + @Override + @Before + public void setUp() throws Exception { Assume.assumeTrue(xxeEnabled); + + super.setUp(); } - @BeforeClass - public static void setupProperties() { + @Override + public void setupProperties() { System.setProperty("xincludePath", "./src/test/resources"); System.setProperty("a2Prop", "a2"); System.setProperty("falseProp", "false"); @@ -41,8 +47,8 @@ public class FileXIncludeConfigurationTest extends FileConfigurationTest { System.setProperty("ninetyTwoProp", "92"); } - @AfterClass - public static void clearProperties() { + @Override + public void clearProperties() { System.clearProperty("xincludePath"); System.clearProperty("a2Prop"); System.clearProperty("falseProp"); diff --git a/artemis-server/src/test/java/org/apache/activemq/artemis/core/config/impl/FileXIncludeSchemaConfigurationTest.java b/artemis-server/src/test/java/org/apache/activemq/artemis/core/config/impl/FileXIncludeSchemaConfigurationTest.java index a45e64c73a..329d78d702 100644 --- a/artemis-server/src/test/java/org/apache/activemq/artemis/core/config/impl/FileXIncludeSchemaConfigurationTest.java +++ b/artemis-server/src/test/java/org/apache/activemq/artemis/core/config/impl/FileXIncludeSchemaConfigurationTest.java @@ -17,9 +17,8 @@ package org.apache.activemq.artemis.core.config.impl; import org.apache.activemq.artemis.core.server.plugin.ActiveMQServerBasePlugin; -import org.junit.AfterClass; import org.junit.Assume; -import org.junit.BeforeClass; +import org.junit.Before; import org.junit.Test; public class FileXIncludeSchemaConfigurationTest extends FileConfigurationTest { @@ -31,11 +30,18 @@ public class FileXIncludeSchemaConfigurationTest extends FileConfigurationTest { public FileXIncludeSchemaConfigurationTest(boolean xxeEnabled) { super(xxeEnabled); + } + + @Override + @Before + public void setUp() throws Exception { Assume.assumeTrue(xxeEnabled); + + super.setUp(); } - @BeforeClass - public static void setupProperties() { + @Override + public void setupProperties() { System.setProperty("xincludePath", "./src/test/resources"); System.setProperty("a2Prop", "a2"); System.setProperty("falseProp", "false"); @@ -43,8 +49,8 @@ public class FileXIncludeSchemaConfigurationTest extends FileConfigurationTest { System.setProperty("ninetyTwoProp", "92"); } - @AfterClass - public static void clearProperties() { + @Override + public void clearProperties() { System.clearProperty("xincludePath"); System.clearProperty("a2Prop"); System.clearProperty("falseProp");
