WW-5353 Test fixes

This commit is contained in:
Kusal Kithul-Godage
2024-04-20 21:55:02 +10:00
parent 85cf09b7d3
commit 5d4ad83ccd
6 changed files with 39 additions and 12 deletions
@@ -38,8 +38,12 @@ import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
@@ -76,6 +80,8 @@ public class CompoundRootAccessor implements RootAccessor {
private static final Map<MethodCall, Boolean> invalidMethods = new ConcurrentHashMap<>();
private boolean devMode;
private boolean disallowCustomOgnlMap;
private static final Set<String> ALLOWED_MAP_CLASSES = Set.of(
HashMap.class.getName(), TreeMap.class.getName(), LinkedHashMap.class.getName());
@Inject(StrutsConstants.STRUTS_DEVMODE)
protected void setDevMode(String mode) {
@@ -286,7 +292,7 @@ public class CompoundRootAccessor implements RootAccessor {
if (disallowCustomOgnlMap) {
String nodeClassName = ((OgnlContext) context).getCurrentNode().getClass().getName();
if ("ognl.ASTMap".equals(nodeClassName)) {
if ("ognl.ASTMap".equals(nodeClassName) && !ALLOWED_MAP_CLASSES.contains(className)) {
LOG.error("Constructing OGNL ASTMap's from custom classes is forbidden. Attempted class: {}", className);
return null;
}
@@ -22,15 +22,16 @@ import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.ValueStackFactory;
import org.apache.struts2.dispatcher.Dispatcher;
import org.apache.struts2.dispatcher.DispatcherErrorHandler;
import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.struts2.dispatcher.Dispatcher;
import org.apache.struts2.dispatcher.DispatcherErrorHandler;
import java.util.HashMap;
import java.util.Map;
import static java.util.Collections.emptyMap;
import static org.apache.struts2.StrutsConstants.STRUTS_ALLOWLIST_ENABLE;
/**
* Generic test setup methods to be used with any unit testing framework.
@@ -38,7 +39,9 @@ import static java.util.Collections.emptyMap;
public class StrutsTestCaseHelper {
public static Dispatcher initDispatcher(ServletContext ctx, Map<String, String> params) {
Dispatcher du = new DispatcherWrapper(ctx, params != null ? params : emptyMap());
Map<String, String> finalParams = params != null ? new HashMap<>(params) : new HashMap<>();
finalParams.putIfAbsent(STRUTS_ALLOWLIST_ENABLE, "false");
Dispatcher du = new DispatcherWrapper(ctx, finalParams);
du.init();
Dispatcher.setInstance(du);
@@ -25,6 +25,7 @@ import com.opensymphony.xwork2.validator.annotations.RequiredFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;
import com.opensymphony.xwork2.validator.annotations.ValidatorType;
import org.apache.struts2.interceptor.parameter.StrutsParameter;
import org.apache.struts2.views.jsp.ui.User;
import java.util.Arrays;
@@ -92,6 +93,7 @@ public class TestAction extends ActionSupport {
return collection;
}
@StrutsParameter
public void setCollection(Collection collection) {
this.collection = collection;
}
@@ -100,6 +102,7 @@ public class TestAction extends ActionSupport {
return map;
}
@StrutsParameter
public void setMap(Map map) {
this.map = map;
}
@@ -108,6 +111,7 @@ public class TestAction extends ActionSupport {
return foo;
}
@StrutsParameter
public void setFoo(String foo) {
this.foo = foo;
}
@@ -116,6 +120,7 @@ public class TestAction extends ActionSupport {
return result;
}
@StrutsParameter
public void setResult(String result) {
this.result = result;
}
@@ -124,6 +129,7 @@ public class TestAction extends ActionSupport {
return user;
}
@StrutsParameter
public void setUser(User user) {
this.user = user;
}
@@ -132,6 +138,7 @@ public class TestAction extends ActionSupport {
return array;
}
@StrutsParameter
public void setArray(String[] array) {
this.array = array;
}
@@ -140,6 +147,7 @@ public class TestAction extends ActionSupport {
return objectArray;
}
@StrutsParameter
public void setObjectArray(Object[] arrayObject) {
this.objectArray = arrayObject;
}
@@ -148,6 +156,7 @@ public class TestAction extends ActionSupport {
return list;
}
@StrutsParameter
public void setList(String[][] list) {
this.list = list;
}
@@ -156,10 +165,12 @@ public class TestAction extends ActionSupport {
return list2;
}
@StrutsParameter
public void setList2(List list2) {
this.list2 = list2;
}
@StrutsParameter
public void setList3(List list) {
this.list3 = list;
}
@@ -172,6 +183,7 @@ public class TestAction extends ActionSupport {
return this.collection2;
}
@StrutsParameter
public void setCollection2(Collection collection) {
this.collection2 = collection;
}
@@ -180,6 +192,7 @@ public class TestAction extends ActionSupport {
return fooInt;
}
@StrutsParameter
public void setFooInt(Integer fooInt) {
this.fooInt = fooInt;
}
@@ -225,6 +238,7 @@ public class TestAction extends ActionSupport {
return status;
}
@StrutsParameter
public void setStatus(SomeEnum status) {
this.status = status;
}
@@ -237,6 +251,7 @@ public class TestAction extends ActionSupport {
return floatNumber;
}
@StrutsParameter
public void setFloatNumber(Float floatNumber) {
this.floatNumber = floatNumber;
}
@@ -245,6 +260,7 @@ public class TestAction extends ActionSupport {
return id;
}
@StrutsParameter
public void setId(Long id) {
this.id = id;
}
@@ -253,6 +269,7 @@ public class TestAction extends ActionSupport {
return enumList;
}
@StrutsParameter
public void setEnumList(List<SomeEnum> enumList) {
this.enumList = enumList;
}
@@ -261,6 +278,7 @@ public class TestAction extends ActionSupport {
return intList;
}
@StrutsParameter
public void setIntList(List<Integer> intList) {
this.intList = intList;
}
@@ -269,6 +287,7 @@ public class TestAction extends ActionSupport {
return someBool;
}
@StrutsParameter
public void setSomeBool(Boolean someBool) {
this.someBool = someBool;
}
@@ -19,6 +19,7 @@
package org.apache.struts2.junit;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.parameter.StrutsParameter;
import org.springframework.beans.factory.annotation.Autowired;
public class JUnitTestAction extends ActionSupport {
@@ -33,6 +34,7 @@ public class JUnitTestAction extends ActionSupport {
return name;
}
@StrutsParameter
public void setName(String name) {
this.name = name;
}
@@ -30,7 +30,7 @@ import java.util.Map;
* Base test class for TestNG unit tests. Provides common Struts variables
* and performs Struts setup and teardown processes
*/
public class StrutsTestCase extends TestNGXWorkTestCase {
public class TestNGStrutsTestCase extends TestNGXWorkTestCase {
@BeforeTest
@Override
@@ -18,16 +18,13 @@
*/
package org.apache.struts2.testng;
import com.opensymphony.xwork2.config.ConfigurationManager;
import junit.framework.TestCase;
import org.apache.struts2.dispatcher.Dispatcher;
import org.apache.struts2.testng.StrutsTestCase;
import org.testng.TestListenerAdapter;
import org.testng.TestNG;
import org.testng.annotations.Test;
import com.opensymphony.xwork2.config.ConfigurationManager;
public class TestNGStrutsTestCaseTest extends TestCase {
public void testSimpleTest() throws Exception {
@@ -48,7 +45,7 @@ public class TestNGStrutsTestCaseTest extends TestCase {
}
}
public static class RunTest extends StrutsTestCase {
public static class RunTest extends TestNGStrutsTestCase {
public static boolean ran = false;
public static ConfigurationManager mgr;
public static Dispatcher du;