From 65f5fb120d160a6d320de1299f982aa354cc7a35 Mon Sep 17 00:00:00 2001 From: Manfred Riem Date: Thu, 28 Nov 2024 08:15:28 -0600 Subject: [PATCH] Fixes #4299 - Cleanup various bits --- .../core/api/AttributeManagerTest.java | 109 ++++++++++++++++++ .../platform/src/main/java/module-info.java | 2 +- .../extension/policy/PolicyExtension.java | 1 + .../InternalPolicyServletRequestListener.java | 1 + .../internal/InternalPolicyThreadLocal.java | 3 + .../policy/src/main/java/module-info.java | 2 +- .../extension/policy/PolicyExtensionTest.java | 2 + .../src/main/java/module-info.java | 2 +- .../src/main/java/module-info.java | 2 +- .../servlet/src/main/java/module-info.java | 2 +- 10 files changed, 121 insertions(+), 5 deletions(-) create mode 100644 core/api/src/test/java/cloud/piranha/core/api/AttributeManagerTest.java diff --git a/core/api/src/test/java/cloud/piranha/core/api/AttributeManagerTest.java b/core/api/src/test/java/cloud/piranha/core/api/AttributeManagerTest.java new file mode 100644 index 0000000000..f96a542673 --- /dev/null +++ b/core/api/src/test/java/cloud/piranha/core/api/AttributeManagerTest.java @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2002-2024 Manorrock.com. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package cloud.piranha.core.api; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.Collections; +import java.util.Enumeration; +import java.util.Hashtable; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class AttributeManagerTest { + + private AttributeManager attributeManager; + private Hashtable attributes; + + @BeforeEach + void setUp() { + attributes = new Hashtable<>(); + attributeManager = new AttributeManager() { + @Override + public Object getAttribute(String name) { + return attributes.get(name); + } + + @Override + public Enumeration getAttributeNames() { + return attributes.keys(); + } + + @Override + public void removeAttribute(String name) { + attributes.remove(name); + } + + @Override + public void setAttribute(String name, Object value) { + attributes.put(name, value); + } + }; + } + + @Test + void testGetAttribute() { + attributeManager.setAttribute("test", "value"); + assertEquals("value", attributeManager.getAttribute("test")); + } + + @Test + void testGetAttributeNames() { + attributeManager.setAttribute("test1", "value1"); + attributeManager.setAttribute("test2", "value2"); + Enumeration names = attributeManager.getAttributeNames(); + assertTrue(Collections.list(names).contains("test1")); + names = attributeManager.getAttributeNames(); + assertTrue(Collections.list(names).contains("test2")); + } + + @Test + void testRemoveAttribute() { + attributeManager.setAttribute("test", "value"); + attributeManager.removeAttribute("test"); + assertNull(attributeManager.getAttribute("test")); + } + + @Test + void testSetAttribute() { + attributeManager.setAttribute("test", "value"); + assertEquals("value", attributeManager.getAttribute("test")); + } + + @Test + void testContainsAttribute() { + attributeManager.setAttribute("test", "value"); + assertTrue(attributeManager.containsAttribute("test")); + attributeManager.removeAttribute("test"); + assertFalse(attributeManager.containsAttribute("test")); + } +} \ No newline at end of file diff --git a/extension/platform/src/main/java/module-info.java b/extension/platform/src/main/java/module-info.java index 25e460268c..f8b79a3715 100644 --- a/extension/platform/src/main/java/module-info.java +++ b/extension/platform/src/main/java/module-info.java @@ -47,7 +47,7 @@ exports cloud.piranha.extension.platform; opens cloud.piranha.extension.platform; - requires cloud.piranha.core.api; + requires transitive cloud.piranha.core.api; requires cloud.piranha.extension.annotationscan; requires cloud.piranha.extension.annotationscan.classfile; requires cloud.piranha.extension.expressly; diff --git a/extension/policy/src/main/java/cloud/piranha/extension/policy/PolicyExtension.java b/extension/policy/src/main/java/cloud/piranha/extension/policy/PolicyExtension.java index b572d4e728..1757b90fb6 100644 --- a/extension/policy/src/main/java/cloud/piranha/extension/policy/PolicyExtension.java +++ b/extension/policy/src/main/java/cloud/piranha/extension/policy/PolicyExtension.java @@ -57,6 +57,7 @@ public class PolicyExtension implements WebApplicationExtension { * * @param webApplication the web application. */ + @SuppressWarnings("removal") @Override public void configure(WebApplication webApplication) { diff --git a/extension/policy/src/main/java/cloud/piranha/extension/policy/internal/InternalPolicyServletRequestListener.java b/extension/policy/src/main/java/cloud/piranha/extension/policy/internal/InternalPolicyServletRequestListener.java index 471fa2522f..ba0d7ff9d2 100644 --- a/extension/policy/src/main/java/cloud/piranha/extension/policy/internal/InternalPolicyServletRequestListener.java +++ b/extension/policy/src/main/java/cloud/piranha/extension/policy/internal/InternalPolicyServletRequestListener.java @@ -53,6 +53,7 @@ public void requestDestroyed(ServletRequestEvent event) { InternalPolicyThreadLocal.removePolicy(); } + @SuppressWarnings("removal") @Override public void requestInitialized(ServletRequestEvent event) { LOGGER.log(DEBUG, "Setting Policy"); diff --git a/extension/policy/src/main/java/cloud/piranha/extension/policy/internal/InternalPolicyThreadLocal.java b/extension/policy/src/main/java/cloud/piranha/extension/policy/internal/InternalPolicyThreadLocal.java index 750ba12cbf..37fe3e578c 100644 --- a/extension/policy/src/main/java/cloud/piranha/extension/policy/internal/InternalPolicyThreadLocal.java +++ b/extension/policy/src/main/java/cloud/piranha/extension/policy/internal/InternalPolicyThreadLocal.java @@ -40,11 +40,13 @@ public class InternalPolicyThreadLocal { /** * Stores the policies by thread id. */ + @SuppressWarnings("removal") private static final HashMap POLICIES = new HashMap<>(1); /** * Remove the policy. */ + @SuppressWarnings("deprecation") public static void removePolicy() { POLICIES.remove(Thread.currentThread().getId()); } @@ -54,6 +56,7 @@ public static void removePolicy() { * * @param policy the policy. */ + @SuppressWarnings({ "removal", "deprecation" }) public static void setPolicy(Policy policy) { POLICIES.put(Thread.currentThread().getId(), policy); } diff --git a/extension/policy/src/main/java/module-info.java b/extension/policy/src/main/java/module-info.java index 7ee27b5246..cc5edbfcca 100644 --- a/extension/policy/src/main/java/module-info.java +++ b/extension/policy/src/main/java/module-info.java @@ -35,5 +35,5 @@ exports cloud.piranha.extension.policy; opens cloud.piranha.extension.policy; - requires cloud.piranha.core.api; + requires transitive cloud.piranha.core.api; } diff --git a/extension/policy/src/test/java/cloud/piranha/extension/policy/PolicyExtensionTest.java b/extension/policy/src/test/java/cloud/piranha/extension/policy/PolicyExtensionTest.java index 06bf9c4040..d6be6cb676 100644 --- a/extension/policy/src/test/java/cloud/piranha/extension/policy/PolicyExtensionTest.java +++ b/extension/policy/src/test/java/cloud/piranha/extension/policy/PolicyExtensionTest.java @@ -77,6 +77,7 @@ void testValidatePolicyNotSet() throws Exception { PolicyExtension extension = new PolicyExtension(); extension.configure(webApplication); webApplication.addListener(new ServletRequestListener() { + @SuppressWarnings("removal") @Override public void requestInitialized(ServletRequestEvent sre) { assertNull(webApplication.getAttribute(Policy.class.getName())); @@ -104,6 +105,7 @@ void testValidatePolicySet() throws Exception { PolicyExtension extension = new PolicyExtension(); extension.configure(webApplication); webApplication.addListener(new ServletRequestListener() { + @SuppressWarnings("removal") @Override public void requestInitialized(ServletRequestEvent sre) { assertNotNull(webApplication.getAttribute(Policy.class.getName())); diff --git a/extension/scinitializer/src/main/java/module-info.java b/extension/scinitializer/src/main/java/module-info.java index 2beea11426..8a4cf8bd46 100644 --- a/extension/scinitializer/src/main/java/module-info.java +++ b/extension/scinitializer/src/main/java/module-info.java @@ -51,6 +51,6 @@ exports cloud.piranha.extension.scinitializer; opens cloud.piranha.extension.scinitializer; - requires cloud.piranha.core.api; + requires transitive cloud.piranha.core.api; uses jakarta.servlet.ServletContainerInitializer; } diff --git a/extension/security-servlet/src/main/java/module-info.java b/extension/security-servlet/src/main/java/module-info.java index 1b4ef925d1..c655a7ef00 100644 --- a/extension/security-servlet/src/main/java/module-info.java +++ b/extension/security-servlet/src/main/java/module-info.java @@ -36,7 +36,7 @@ exports cloud.piranha.extension.security.servlet; opens cloud.piranha.extension.security.servlet; - requires cloud.piranha.core.api; + requires transitive cloud.piranha.core.api; requires cloud.piranha.core.impl; requires transitive cloud.piranha.extension.exousia; requires transitive cloud.piranha.extension.epicyro; diff --git a/extension/servlet/src/main/java/module-info.java b/extension/servlet/src/main/java/module-info.java index 150fbb498d..14f8b6a3c7 100644 --- a/extension/servlet/src/main/java/module-info.java +++ b/extension/servlet/src/main/java/module-info.java @@ -51,7 +51,7 @@ exports cloud.piranha.extension.servlet; opens cloud.piranha.extension.servlet; - requires cloud.piranha.core.api; + requires transitive cloud.piranha.core.api; requires cloud.piranha.extension.annotationscan; requires cloud.piranha.extension.annotationscan.classfile; requires cloud.piranha.extension.fileupload;