WW-5411 Delete deprecated methods from cache factories and OgnlUtil constructor

This commit is contained in:
Kusal Kithul-Godage
2024-07-08 17:19:47 +10:00
parent 4944f1838f
commit ead66f2f75
8 changed files with 78 additions and 152 deletions
@@ -30,13 +30,6 @@ import org.apache.struts2.StrutsConstants;
public class DefaultOgnlBeanInfoCacheFactory<Key, Value> extends DefaultOgnlCacheFactory<Key, Value>
implements BeanInfoCacheFactory<Key, Value> {
/**
* @deprecated since 6.4.0, use {@link #DefaultOgnlBeanInfoCacheFactory(String, String)}
*/
@Deprecated
public DefaultOgnlBeanInfoCacheFactory() {
}
@Inject
public DefaultOgnlBeanInfoCacheFactory(@Inject(value = StrutsConstants.STRUTS_OGNL_BEANINFO_CACHE_MAXSIZE) String cacheMaxSize,
@Inject(value = StrutsConstants.STRUTS_OGNL_BEANINFO_CACHE_TYPE) String defaultCacheType) {
@@ -15,8 +15,6 @@
*/
package com.opensymphony.xwork2.ognl;
import org.apache.commons.lang3.BooleanUtils;
/**
* <p>Default OGNL Cache factory implementation.</p>
*
@@ -30,18 +28,10 @@ public class DefaultOgnlCacheFactory<Key, Value> implements OgnlCacheFactory<Key
private static final int DEFAULT_INIT_CAPACITY = 16;
private static final float DEFAULT_LOAD_FACTOR = 0.75f;
private CacheType defaultCacheType;
private int cacheMaxSize;
private final CacheType defaultCacheType;
private final int cacheMaxSize;
private final int initialCapacity;
/**
* @deprecated since 6.4.0, use {@link #DefaultOgnlCacheFactory(int, CacheType)}
*/
@Deprecated
public DefaultOgnlCacheFactory() {
this(10000, CacheType.BASIC);
}
public DefaultOgnlCacheFactory(int cacheMaxSize, CacheType defaultCacheType) {
this(cacheMaxSize, defaultCacheType, DEFAULT_INIT_CAPACITY);
}
@@ -62,16 +52,11 @@ public class DefaultOgnlCacheFactory<Key, Value> implements OgnlCacheFactory<Key
int initialCapacity,
float loadFactor,
CacheType cacheType) {
switch (cacheType) {
case BASIC:
return new OgnlDefaultCache<>(evictionLimit, initialCapacity, loadFactor);
case LRU:
return new OgnlLRUCache<>(evictionLimit, initialCapacity, loadFactor);
case WTLFU:
return new OgnlCaffeineCache<>(evictionLimit, initialCapacity);
default:
throw new IllegalArgumentException("Unknown cache type: " + cacheType);
}
return switch (cacheType) {
case BASIC -> new OgnlDefaultCache<>(evictionLimit, initialCapacity, loadFactor);
case LRU -> new OgnlLRUCache<>(evictionLimit, initialCapacity, loadFactor);
case WTLFU -> new OgnlCaffeineCache<>(evictionLimit, initialCapacity);
};
}
@Override
@@ -79,28 +64,8 @@ public class DefaultOgnlCacheFactory<Key, Value> implements OgnlCacheFactory<Key
return cacheMaxSize;
}
/**
* @deprecated since 6.4.0
*/
@Deprecated
protected void setCacheMaxSize(String maxSize) {
cacheMaxSize = Integer.parseInt(maxSize);
}
@Override
public CacheType getDefaultCacheType() {
return defaultCacheType;
}
/**
* No effect when {@code useLRUMode} is {@code false}
*
* @deprecated since 6.4.0
*/
@Deprecated
protected void setUseLRUCache(String useLRUMode) {
if (BooleanUtils.toBoolean(useLRUMode)) {
defaultCacheType = CacheType.LRU;
}
}
}
@@ -30,13 +30,6 @@ import org.apache.struts2.StrutsConstants;
public class DefaultOgnlExpressionCacheFactory<Key, Value> extends DefaultOgnlCacheFactory<Key, Value>
implements ExpressionCacheFactory<Key, Value> {
/**
* @deprecated since 6.4.0, use {@link #DefaultOgnlExpressionCacheFactory(String, String)}
*/
@Deprecated
public DefaultOgnlExpressionCacheFactory() {
}
@Inject
public DefaultOgnlExpressionCacheFactory(@Inject(value = StrutsConstants.STRUTS_OGNL_EXPRESSION_CACHE_MAXSIZE) String cacheMaxSize,
@Inject(value = StrutsConstants.STRUTS_OGNL_EXPRESSION_CACHE_TYPE) String defaultCacheType) {
@@ -25,22 +25,6 @@ package com.opensymphony.xwork2.ognl;
public interface OgnlCacheFactory<Key, Value> {
OgnlCache<Key, Value> buildOgnlCache();
/**
* Note that if {@code lruCache} is {@code false}, the cache type could still be LRU if the default cache type is
* configured as such.
* @deprecated since 6.4.0, use {@link #buildOgnlCache(int, int, float, CacheType)}
*/
@Deprecated
default OgnlCache<Key, Value> buildOgnlCache(int evictionLimit,
int initialCapacity,
float loadFactor,
boolean lruCache) {
return buildOgnlCache(evictionLimit,
initialCapacity,
loadFactor,
lruCache ? CacheType.LRU : getDefaultCacheType());
}
/**
* @param evictionLimit maximum capacity of the cache where applicable for cache type chosen
* @param initialCapacity initial capacity of the cache where applicable for cache type chosen
@@ -52,14 +36,6 @@ public interface OgnlCacheFactory<Key, Value> {
int getCacheMaxSize();
/**
* @deprecated since 6.4.0
*/
@Deprecated
default boolean getUseLRUCache() {
return CacheType.LRU.equals(getDefaultCacheType());
}
CacheType getDefaultCacheType();
enum CacheType {
@@ -36,7 +36,6 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.StrutsConstants;
import org.apache.struts2.ognl.OgnlGuard;
import org.apache.struts2.ognl.StrutsOgnlGuard;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
@@ -78,18 +77,6 @@ public class OgnlUtil {
private Container container;
/**
* Construct a new OgnlUtil instance for use with the framework
*
* @deprecated since 6.0.0. Use {@link #OgnlUtil(ExpressionCacheFactory, BeanInfoCacheFactory, OgnlGuard) instead.
*/
@Deprecated
public OgnlUtil() {
this(new DefaultOgnlExpressionCacheFactory<>(),
new DefaultOgnlBeanInfoCacheFactory<>(),
new StrutsOgnlGuard());
}
/**
* Construct a new OgnlUtil instance for use with the framework, with optional cache factories for OGNL Expression
* and BeanInfo caches.
@@ -26,14 +26,10 @@ import com.opensymphony.xwork2.interceptor.Interceptor;
import com.opensymphony.xwork2.mock.MockActionProxy;
import com.opensymphony.xwork2.mock.MockInterceptor;
import com.opensymphony.xwork2.mock.MockResult;
import com.opensymphony.xwork2.ognl.DefaultOgnlBeanInfoCacheFactory;
import com.opensymphony.xwork2.ognl.DefaultOgnlExpressionCacheFactory;
import com.opensymphony.xwork2.ognl.OgnlUtil;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.ValueStackFactory;
import org.apache.struts2.config.StrutsXmlConfigurationProvider;
import org.apache.struts2.dispatcher.HttpParameters;
import org.apache.struts2.ognl.StrutsOgnlGuard;
import java.util.ArrayList;
import java.util.HashMap;
@@ -42,6 +38,8 @@ import java.util.concurrent.Callable;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import static com.opensymphony.xwork2.ognl.OgnlUtilTest.createOgnlUtil;
/**
* A partial test of DefaultActionInvocation.
@@ -531,14 +529,6 @@ public class DefaultActionInvocationTest extends XWorkTestCase {
loadConfigurationProviders(configurationProvider);
}
private OgnlUtil createOgnlUtil() {
return new OgnlUtil(
new DefaultOgnlExpressionCacheFactory<>(),
new DefaultOgnlBeanInfoCacheFactory<>(),
new StrutsOgnlGuard()
);
}
private static class SimpleActionEventListener implements ActionEventListener {
private final String name;
@@ -63,6 +63,8 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import static com.opensymphony.xwork2.ognl.OgnlCacheFactory.CacheType.BASIC;
import static com.opensymphony.xwork2.ognl.OgnlCacheFactory.CacheType.LRU;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertThrows;
@@ -1343,13 +1345,13 @@ public class OgnlUtilTest extends XWorkTestCase {
public void testDefaultOgnlUtilAlternateConstructorArguments() {
// Code coverage test for the OgnlUtil alternate constructor method, and verify expected behaviour.
try {
OgnlUtil basicOgnlUtil = new OgnlUtil(new DefaultOgnlExpressionCacheFactory<>(), null, null);
new OgnlUtil(createDefaultOgnlExpressionCacheFactory(), null, null);
fail("null beanInfoCacheFactory should result in exception");
} catch (NullPointerException iaex) {
// expected result
}
try {
OgnlUtil basicOgnlUtil = new OgnlUtil(null, new DefaultOgnlBeanInfoCacheFactory<>(), null);
new OgnlUtil(null, createDefaultOgnlBeanInfoCacheFactory(), null);
fail("null expressionCacheFactory should result in exception");
} catch (NullPointerException iaex) {
// expected result
@@ -1614,23 +1616,24 @@ public class OgnlUtilTest extends XWorkTestCase {
*/
public void testOgnlDefaultCacheFactoryCoverage() {
OgnlCache<String, Object> ognlCache;
DefaultOgnlCacheFactory defaultOgnlCacheFactory = new DefaultOgnlCacheFactory<String, Object>();
// Normal cache
defaultOgnlCacheFactory.setCacheMaxSize("12");
defaultOgnlCacheFactory.setUseLRUCache("false");
DefaultOgnlCacheFactory<String, Object> defaultOgnlCacheFactory = new DefaultOgnlCacheFactory<>(12, BASIC);
ognlCache = defaultOgnlCacheFactory.buildOgnlCache();
assertNotNull("No param build method result null ?", ognlCache);
assertEquals("Eviction limit for cache mismatches limit for factory ?", 12, ognlCache.getEvictionLimit());
ognlCache = defaultOgnlCacheFactory.buildOgnlCache(6, 6, 0.75f, false);
ognlCache = defaultOgnlCacheFactory.buildOgnlCache(6, 6, 0.75f, BASIC);
assertNotNull("No param build method result null ?", ognlCache);
assertEquals("Eviction limit for cache mismatches limit for factory ?", 6, ognlCache.getEvictionLimit());
// LRU cache
defaultOgnlCacheFactory.setCacheMaxSize("30");
defaultOgnlCacheFactory.setUseLRUCache("true");
defaultOgnlCacheFactory = new DefaultOgnlCacheFactory<>(30, LRU);
ognlCache = defaultOgnlCacheFactory.buildOgnlCache();
assertNotNull("No param build method result null ?", ognlCache);
assertEquals("Eviction limit for cache mismatches limit for factory ?", 30, ognlCache.getEvictionLimit());
ognlCache = defaultOgnlCacheFactory.buildOgnlCache(15, 15, 0.75f, true);
ognlCache = defaultOgnlCacheFactory.buildOgnlCache(15, 15, 0.75f, LRU);
assertNotNull("No param build method result null ?", ognlCache);
assertEquals("Eviction limit for cache mismatches limit for factory ?", 15, ognlCache.getEvictionLimit());
}
@@ -1653,12 +1656,8 @@ public class OgnlUtilTest extends XWorkTestCase {
*/
private OgnlUtil generateOgnlUtilInstanceWithDefaultLRUCacheFactories() {
final OgnlUtil result;
final DefaultOgnlExpressionCacheFactory<String, Object> expressionFactory = new DefaultOgnlExpressionCacheFactory<>();
final DefaultOgnlBeanInfoCacheFactory<Class<?>, BeanInfo> beanInfoFactory = new DefaultOgnlBeanInfoCacheFactory<>();
expressionFactory.setUseLRUCache("true");
expressionFactory.setCacheMaxSize("25");
beanInfoFactory.setUseLRUCache("true");
beanInfoFactory.setCacheMaxSize("25");
final DefaultOgnlExpressionCacheFactory<String, Object> expressionFactory = new DefaultOgnlExpressionCacheFactory<>(String.valueOf(25), LRU.toString());
final DefaultOgnlBeanInfoCacheFactory<Class<?>, BeanInfo> beanInfoFactory = new DefaultOgnlBeanInfoCacheFactory<>(String.valueOf(25), LRU.toString());
result = new OgnlUtil(expressionFactory, beanInfoFactory, new StrutsOgnlGuard());
return result;
}
@@ -1806,4 +1805,19 @@ public class OgnlUtilTest extends XWorkTestCase {
}
}
public static OgnlUtil createOgnlUtil() {
return new OgnlUtil(
createDefaultOgnlExpressionCacheFactory(),
createDefaultOgnlBeanInfoCacheFactory(),
new StrutsOgnlGuard()
);
}
public static <K, V> DefaultOgnlExpressionCacheFactory<K, V> createDefaultOgnlExpressionCacheFactory() {
return new DefaultOgnlExpressionCacheFactory<>(String.valueOf(10_000), BASIC.toString());
}
public static <K, V> DefaultOgnlBeanInfoCacheFactory<K, V> createDefaultOgnlBeanInfoCacheFactory() {
return new DefaultOgnlBeanInfoCacheFactory<>(String.valueOf(10_000), BASIC.toString());
}
}
@@ -28,20 +28,24 @@ import com.opensymphony.xwork2.config.entities.InterceptorMapping;
import com.opensymphony.xwork2.config.entities.ResultConfig;
import com.opensymphony.xwork2.mock.MockActionProxy;
import com.opensymphony.xwork2.mock.MockInterceptor;
import com.opensymphony.xwork2.ognl.DefaultOgnlBeanInfoCacheFactory;
import com.opensymphony.xwork2.ognl.DefaultOgnlExpressionCacheFactory;
import com.opensymphony.xwork2.ognl.OgnlUtil;
import com.opensymphony.xwork2.util.XWorkTestCaseHelper;
import jakarta.servlet.http.HttpServletResponse;
import junit.framework.TestCase;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.ognl.StrutsOgnlGuard;
import org.apache.struts2.result.HttpHeaderResult;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import jakarta.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static com.opensymphony.xwork2.ognl.OgnlCacheFactory.CacheType.BASIC;
import static jakarta.servlet.http.HttpServletResponse.SC_NOT_MODIFIED;
public class RestActionInvocationTest extends TestCase {
@@ -53,7 +57,7 @@ public class RestActionInvocationTest extends TestCase {
@Override
protected void setUp() throws Exception {
super.setUp();
restActionInvocation = new RestActionInvocationTester();
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
@@ -61,7 +65,7 @@ public class RestActionInvocationTest extends TestCase {
ServletActionContext.setResponse(response);
}
/**
* Test the correct action results: null, String, HttpHeaders, Result
* @throws Exception
@@ -71,12 +75,12 @@ public class RestActionInvocationTest extends TestCase {
Object methodResult = "index";
ActionConfig actionConfig = restActionInvocation.getProxy().getConfig();
assertEquals("index", restActionInvocation.saveResult(actionConfig, methodResult));
setUp();
methodResult = new DefaultHttpHeaders("show");
assertEquals("show", restActionInvocation.saveResult(actionConfig, methodResult));
assertEquals(methodResult, restActionInvocation.httpHeaders);
setUp();
methodResult = new HttpHeaderResult(HttpServletResponse.SC_ACCEPTED);
assertEquals(null, restActionInvocation.saveResult(actionConfig, methodResult));
@@ -89,18 +93,18 @@ public class RestActionInvocationTest extends TestCase {
// ko
assertFalse(true);
} catch (ConfigurationException c) {
// ok, object not allowed
}
}
/**
* Test the target selection: exception, error messages, model and null
* @throws Exception
*/
public void testSelectTarget() throws Exception {
// Exception
Exception e = new Exception();
restActionInvocation.getStack().set("exception", e);
@@ -118,7 +122,7 @@ public class RestActionInvocationTest extends TestCase {
errors.put("actionErrors", list);
restActionInvocation.selectTarget();
assertEquals(errors, restActionInvocation.target);
// Model with get and no content in post, put, delete
setUp();
RestAction restAction = (RestAction)restActionInvocation.getAction();
@@ -168,18 +172,18 @@ public class RestActionInvocationTest extends TestCase {
};
model.add("Item");
restAction.model = model;
restActionInvocation.processResult();
assertEquals(SC_NOT_MODIFIED, response.getStatus());
}
/**
* Test the default error result.
* @throws Exception
*/
public void testDefaultErrorResult() throws Exception {
// Exception
Exception e = new Exception();
restActionInvocation.getStack().set("exception", e);
@@ -189,24 +193,24 @@ public class RestActionInvocationTest extends TestCase {
List<String> model = new ArrayList<String>();
model.add("Item");
restAction.model = model;
restActionInvocation.setDefaultErrorResultName("default-error");
ResultConfig resultConfig = new ResultConfig.Builder("default-error",
ResultConfig resultConfig = new ResultConfig.Builder("default-error",
"org.apache.struts2.result.HttpHeaderResult")
.addParam("status", "123").build();
ActionConfig actionConfig = new ActionConfig.Builder("org.apache.rest",
ActionConfig actionConfig = new ActionConfig.Builder("org.apache.rest",
"RestAction", "org.apache.rest.RestAction")
.addResultConfig(resultConfig)
.build();
((MockActionProxy)restActionInvocation.getProxy()).setConfig(actionConfig);
restActionInvocation.processResult();
assertEquals(123, response.getStatus());
}
public void testNoResult() throws Exception {
RestAction restAction = (RestAction)restActionInvocation.getAction();
List<String> model = new ArrayList<String>();
model.add("Item");
@@ -219,35 +223,39 @@ public class RestActionInvocationTest extends TestCase {
// ko
assertFalse(true);
} catch (ConfigurationException c) {
// ok, no result
}
}
/**
* Test the global execution
* @throws Exception
*/
public void testInvoke() throws Exception {
// Default index method return 'success'
((MockActionProxy)restActionInvocation.getProxy()).setMethod("index");
// Define result 'success'
ResultConfig resultConfig = new ResultConfig.Builder("success",
ResultConfig resultConfig = new ResultConfig.Builder("success",
"org.apache.struts2.result.HttpHeaderResult")
.addParam("status", "123").build();
ActionConfig actionConfig = new ActionConfig.Builder("org.apache.rest",
ActionConfig actionConfig = new ActionConfig.Builder("org.apache.rest",
"RestAction", "org.apache.rest.RestAction")
.addResultConfig(resultConfig)
.build();
((MockActionProxy)restActionInvocation.getProxy()).setConfig(actionConfig);
request.setMethod("GET");
restActionInvocation.setOgnlUtil(new OgnlUtil());
restActionInvocation.setOgnlUtil(new OgnlUtil(
new DefaultOgnlExpressionCacheFactory<>(String.valueOf(10_000), BASIC.toString()),
new DefaultOgnlBeanInfoCacheFactory<>(String.valueOf(10_000), BASIC.toString()),
new StrutsOgnlGuard()
));
restActionInvocation.invoke();
assertEquals(123, response.getStatus());
@@ -264,7 +272,7 @@ public class RestActionInvocationTest extends TestCase {
interceptorMappings.add(new InterceptorMapping("interceptor", mockInterceptor));
interceptors = interceptorMappings.iterator();
MockActionProxy actionProxy = new MockActionProxy();
ActionConfig actionConfig = new ActionConfig.Builder("org.apache.rest",
ActionConfig actionConfig = new ActionConfig.Builder("org.apache.rest",
"RestAction", "org.apache.rest.RestAction").build();
actionProxy.setConfig(actionConfig);
proxy = actionProxy;
@@ -280,18 +288,18 @@ public class RestActionInvocationTest extends TestCase {
container = ActionContext.getContext().getContainer();
stack = ActionContext.getContext().getValueStack();
objectFactory = container.getInstance(ObjectFactory.class);
}
}
class RestAction extends RestActionSupport implements ModelDriven<List<String>> {
List<String> model;
public List<String> getModel() {
return model;
}
}
}