Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #4299 - Cleanup various bits #4300

Merged
merged 2 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<String, Object> attributes;

@BeforeEach
void setUp() {
attributes = new Hashtable<>();
attributeManager = new AttributeManager() {
@Override
public Object getAttribute(String name) {
return attributes.get(name);
}

@Override
public Enumeration<String> 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<String> 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"));
}
}
2 changes: 1 addition & 1 deletion extension/platform/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class PolicyExtension implements WebApplicationExtension {
*
* @param webApplication the web application.
*/
@SuppressWarnings("removal")
@Override
public void configure(WebApplication webApplication) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public void requestDestroyed(ServletRequestEvent event) {
InternalPolicyThreadLocal.removePolicy();
}

@SuppressWarnings("removal")
@Override
public void requestInitialized(ServletRequestEvent event) {
LOGGER.log(DEBUG, "Setting Policy");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ public class InternalPolicyThreadLocal {
/**
* Stores the policies by thread id.
*/
@SuppressWarnings("removal")
private static final HashMap<Long, Policy> POLICIES = new HashMap<>(1);

/**
* Remove the policy.
*/
@SuppressWarnings("deprecation")
public static void removePolicy() {
POLICIES.remove(Thread.currentThread().getId());
}
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion extension/policy/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down Expand Up @@ -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()));
Expand Down
2 changes: 1 addition & 1 deletion extension/scinitializer/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion extension/security-servlet/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion extension/servlet/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading