Skip to content

Commit

Permalink
WW-5353 Test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kusalk committed Apr 20, 2024
1 parent 85cf09b commit 5d4ad83
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@
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;

Expand Down Expand Up @@ -76,6 +80,8 @@ public String getSourceSetter(OgnlContext context, Object target, Object index)
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) {
Expand Down Expand Up @@ -286,7 +292,7 @@ public Class classForName(String className, Map context) throws ClassNotFoundExc

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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,26 @@
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.
*/
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);

Expand Down
19 changes: 19 additions & 0 deletions core/src/test/java/org/apache/struts2/TestAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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;
Expand Down Expand Up @@ -92,6 +93,7 @@ public Collection getCollection() {
return collection;
}

@StrutsParameter
public void setCollection(Collection collection) {
this.collection = collection;
}
Expand All @@ -100,6 +102,7 @@ public Map getMap() {
return map;
}

@StrutsParameter
public void setMap(Map map) {
this.map = map;
}
Expand All @@ -108,6 +111,7 @@ public String getFoo() {
return foo;
}

@StrutsParameter
public void setFoo(String foo) {
this.foo = foo;
}
Expand All @@ -116,6 +120,7 @@ public String getResult() {
return result;
}

@StrutsParameter
public void setResult(String result) {
this.result = result;
}
Expand All @@ -124,6 +129,7 @@ public User getUser() {
return user;
}

@StrutsParameter
public void setUser(User user) {
this.user = user;
}
Expand All @@ -132,6 +138,7 @@ public String[] getArray() {
return array;
}

@StrutsParameter
public void setArray(String[] array) {
this.array = array;
}
Expand All @@ -140,6 +147,7 @@ public Object[] getObjectArray() {
return objectArray;
}

@StrutsParameter
public void setObjectArray(Object[] arrayObject) {
this.objectArray = arrayObject;
}
Expand All @@ -148,6 +156,7 @@ public String[][] getList() {
return list;
}

@StrutsParameter
public void setList(String[][] list) {
this.list = list;
}
Expand All @@ -156,10 +165,12 @@ public List getList2() {
return list2;
}

@StrutsParameter
public void setList2(List list2) {
this.list2 = list2;
}

@StrutsParameter
public void setList3(List list) {
this.list3 = list;
}
Expand All @@ -172,6 +183,7 @@ public Collection getCollection2() {
return this.collection2;
}

@StrutsParameter
public void setCollection2(Collection collection) {
this.collection2 = collection;
}
Expand All @@ -180,6 +192,7 @@ public Integer getFooInt() {
return fooInt;
}

@StrutsParameter
public void setFooInt(Integer fooInt) {
this.fooInt = fooInt;
}
Expand Down Expand Up @@ -225,6 +238,7 @@ public SomeEnum getStatus() {
return status;
}

@StrutsParameter
public void setStatus(SomeEnum status) {
this.status = status;
}
Expand All @@ -237,6 +251,7 @@ public Float getFloatNumber() {
return floatNumber;
}

@StrutsParameter
public void setFloatNumber(Float floatNumber) {
this.floatNumber = floatNumber;
}
Expand All @@ -245,6 +260,7 @@ public Long getId() {
return id;
}

@StrutsParameter
public void setId(Long id) {
this.id = id;
}
Expand All @@ -253,6 +269,7 @@ public List<SomeEnum> getEnumList() {
return enumList;
}

@StrutsParameter
public void setEnumList(List<SomeEnum> enumList) {
this.enumList = enumList;
}
Expand All @@ -261,6 +278,7 @@ public List<Integer> getIntList() {
return intList;
}

@StrutsParameter
public void setIntList(List<Integer> intList) {
this.intList = intList;
}
Expand All @@ -269,6 +287,7 @@ public Boolean getSomeBool() {
return someBool;
}

@StrutsParameter
public void setSomeBool(Boolean someBool) {
this.someBool = someBool;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -33,6 +34,7 @@ public String getName() {
return name;
}

@StrutsParameter
public void setName(String name) {
this.name = name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -48,7 +45,7 @@ public void testSimpleTest() throws Exception {
}
}

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;
Expand Down

0 comments on commit 5d4ad83

Please sign in to comment.