From a15c12a05109da5ad48efb77921f4178ceab16fa Mon Sep 17 00:00:00 2001 From: Yasser Zamani Date: Sun, 24 Feb 2019 17:53:45 +0330 Subject: [PATCH] add properties support to XWorkTestCase.loadButAdd also includes cleanups for PRs #292 and #320 --- .../opensymphony/xwork2/XWorkTestCase.java | 23 +++-- .../xwork2/ognl/OgnlUtilTest.java | 53 ++++------- .../xwork2/ognl/OgnlValueStackTest.java | 33 ------- .../xwork-test-allowstatic-devmode-false.xml | 89 ------------------- .../xwork-test-allowstatic-devmode-true.xml | 89 ------------------- .../providers/xwork-test-allowstatic-true.xml | 89 ------------------- .../providers/xwork-test-devmode-true.xml | 89 ------------------- 7 files changed, 31 insertions(+), 434 deletions(-) delete mode 100644 core/src/test/resources/com/opensymphony/xwork2/config/providers/xwork-test-allowstatic-devmode-false.xml delete mode 100644 core/src/test/resources/com/opensymphony/xwork2/config/providers/xwork-test-allowstatic-devmode-true.xml delete mode 100644 core/src/test/resources/com/opensymphony/xwork2/config/providers/xwork-test-allowstatic-true.xml delete mode 100644 core/src/test/resources/com/opensymphony/xwork2/config/providers/xwork-test-devmode-true.xml diff --git a/core/src/main/java/com/opensymphony/xwork2/XWorkTestCase.java b/core/src/main/java/com/opensymphony/xwork2/XWorkTestCase.java index 21442dabc..bc6b0a8e5 100644 --- a/core/src/main/java/com/opensymphony/xwork2/XWorkTestCase.java +++ b/core/src/main/java/com/opensymphony/xwork2/XWorkTestCase.java @@ -27,6 +27,7 @@ import com.opensymphony.xwork2.test.StubConfigurationProvider; import com.opensymphony.xwork2.util.XWorkTestCaseHelper; import com.opensymphony.xwork2.util.location.LocatableProperties; import junit.framework.TestCase; +import org.apache.commons.lang3.ClassUtils; /** * Base JUnit TestCase to extend for XWork specific JUnit tests. Uses @@ -78,16 +79,20 @@ public abstract class XWorkTestCase extends TestCase { @Override public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException { - builder.factory(type, name, new Factory() { - public Object create(Context context) throws Exception { - return impl; - } + if (impl instanceof String || ClassUtils.isPrimitiveOrWrapper(impl.getClass())) { + props.setProperty(name, "" + impl); + } else { + builder.factory(type, name, new Factory() { + public Object create(Context context) throws Exception { + return impl; + } - @Override - public Class type() { - return impl.getClass(); - } - }, Scope.SINGLETON); + @Override + public Class type() { + return impl.getClass(); + } + }, Scope.SINGLETON); + } } }); } diff --git a/core/src/test/java/com/opensymphony/xwork2/ognl/OgnlUtilTest.java b/core/src/test/java/com/opensymphony/xwork2/ognl/OgnlUtilTest.java index f8669ba22..9c5e59bc5 100644 --- a/core/src/test/java/com/opensymphony/xwork2/ognl/OgnlUtilTest.java +++ b/core/src/test/java/com/opensymphony/xwork2/ognl/OgnlUtilTest.java @@ -19,19 +19,20 @@ package com.opensymphony.xwork2.ognl; import com.opensymphony.xwork2.ActionContext; -import com.opensymphony.xwork2.ActionProxyFactory; +import com.opensymphony.xwork2.XWorkConstants; import com.opensymphony.xwork2.XWorkException; import com.opensymphony.xwork2.XWorkTestCase; -import com.opensymphony.xwork2.config.ConfigurationManager; -import com.opensymphony.xwork2.config.ConfigurationProvider; -import com.opensymphony.xwork2.config.providers.XmlConfigurationProvider; +import com.opensymphony.xwork2.config.ConfigurationException; import com.opensymphony.xwork2.conversion.impl.XWorkConverter; -import com.opensymphony.xwork2.inject.Container; +import com.opensymphony.xwork2.inject.ContainerBuilder; import com.opensymphony.xwork2.interceptor.ChainingInterceptor; +import com.opensymphony.xwork2.test.StubConfigurationProvider; import com.opensymphony.xwork2.test.User; import com.opensymphony.xwork2.util.*; +import com.opensymphony.xwork2.util.location.LocatableProperties; import com.opensymphony.xwork2.util.reflection.ReflectionContextState; import ognl.*; +import org.apache.struts2.StrutsConstants; import java.lang.reflect.Method; import java.text.DateFormat; @@ -1232,37 +1233,17 @@ public class OgnlUtilTest extends XWorkTestCase { } } - private void reloadTestContainerConfiguration(boolean devMode, boolean allowStatic) throws Exception { - super.tearDown(); - - ConfigurationProvider configurationProvider; - if (devMode == true && allowStatic == true) { - configurationProvider = new XmlConfigurationProvider("com/opensymphony/xwork2/config/providers/xwork-test-allowstatic-devmode-true.xml", true); - } - else if (devMode == true && allowStatic == false) { - configurationProvider = new XmlConfigurationProvider("com/opensymphony/xwork2/config/providers/xwork-test-devmode-true.xml", true); - } - else if (devMode == false && allowStatic == true) { - configurationProvider = new XmlConfigurationProvider("com/opensymphony/xwork2/config/providers/xwork-test-allowstatic-true.xml", true); - } - else { // devMode, allowStatic both false - configurationProvider = new XmlConfigurationProvider("com/opensymphony/xwork2/config/providers/xwork-test-allowstatic-devmode-false.xml", true); - } - - configurationManager = new ConfigurationManager(Container.DEFAULT_NAME); - configurationManager.addContainerProvider(configurationProvider); - configuration = configurationManager.getConfiguration(); - container = configuration.getContainer(); - container.inject(configurationProvider); - configurationProvider.init(configuration); - actionProxyFactory = container.getInstance(ActionProxyFactory.class); - - // Reset the value stack - ValueStack stack = container.getInstance(ValueStackFactory.class).createValueStack(); - stack.getContext().put(ActionContext.CONTAINER, container); - ActionContext.setContext(new ActionContext(stack.getContext())); - - ognlUtil = container.getInstance(OgnlUtil.class); + private void reloadTestContainerConfiguration(final boolean devMode, final boolean allowStatic) throws Exception { + loadConfigurationProviders(new StubConfigurationProvider() { + @Override + public void register(ContainerBuilder builder, + LocatableProperties props) throws ConfigurationException { + props.setProperty(StrutsConstants.STRUTS_DEVMODE, "" + devMode); + props.setProperty(XWorkConstants.DEV_MODE, "" + devMode); + props.setProperty(StrutsConstants.STRUTS_ALLOW_STATIC_METHOD_ACCESS, "" + allowStatic); + props.setProperty(XWorkConstants.ALLOW_STATIC_METHOD_ACCESS, "" + allowStatic); + } + }); } public static class Email { diff --git a/core/src/test/java/com/opensymphony/xwork2/ognl/OgnlValueStackTest.java b/core/src/test/java/com/opensymphony/xwork2/ognl/OgnlValueStackTest.java index e66fc2466..c4e31f6b3 100644 --- a/core/src/test/java/com/opensymphony/xwork2/ognl/OgnlValueStackTest.java +++ b/core/src/test/java/com/opensymphony/xwork2/ognl/OgnlValueStackTest.java @@ -972,39 +972,6 @@ public class OgnlValueStackTest extends XWorkTestCase { assertEquals(null, stack.findValue("address.country.name", String.class)); } - private void reloadTestContainerConfiguration(boolean devMode, boolean allowStatic) throws Exception { - super.tearDown(); - - ConfigurationProvider configurationProvider; - if (devMode == true && allowStatic == true) { - configurationProvider = new XmlConfigurationProvider("com/opensymphony/xwork2/config/providers/xwork-test-allowstatic-devmode-true.xml", true); - } - else if (devMode == true && allowStatic == false) { - configurationProvider = new XmlConfigurationProvider("com/opensymphony/xwork2/config/providers/xwork-test-devmode-true.xml", true); - } - else if (devMode == false && allowStatic == true) { - configurationProvider = new XmlConfigurationProvider("com/opensymphony/xwork2/config/providers/xwork-test-allowstatic-true.xml", true); - } - else { // devMode, allowStatic both false - configurationProvider = new XmlConfigurationProvider("com/opensymphony/xwork2/config/providers/xwork-test-allowstatic-devmode-false.xml", true); - } - - configurationManager = new ConfigurationManager(Container.DEFAULT_NAME); - configurationManager.addContainerProvider(configurationProvider); - configuration = configurationManager.getConfiguration(); - container = configuration.getContainer(); - container.inject(configurationProvider); - configurationProvider.init(configuration); - actionProxyFactory = container.getInstance(ActionProxyFactory.class); - - // Reset the value stack - ValueStack stack = container.getInstance(ValueStackFactory.class).createValueStack(); - stack.getContext().put(ActionContext.CONTAINER, container); - ActionContext.setContext(new ActionContext(stack.getContext())); - - ognlUtil = container.getInstance(OgnlUtil.class); - } - class BadJavaBean { private int count; private int count2; diff --git a/core/src/test/resources/com/opensymphony/xwork2/config/providers/xwork-test-allowstatic-devmode-false.xml b/core/src/test/resources/com/opensymphony/xwork2/config/providers/xwork-test-allowstatic-devmode-false.xml deleted file mode 100644 index 1f0c0ee90..000000000 --- a/core/src/test/resources/com/opensymphony/xwork2/config/providers/xwork-test-allowstatic-devmode-false.xml +++ /dev/null @@ -1,89 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/core/src/test/resources/com/opensymphony/xwork2/config/providers/xwork-test-allowstatic-devmode-true.xml b/core/src/test/resources/com/opensymphony/xwork2/config/providers/xwork-test-allowstatic-devmode-true.xml deleted file mode 100644 index 7551cb10d..000000000 --- a/core/src/test/resources/com/opensymphony/xwork2/config/providers/xwork-test-allowstatic-devmode-true.xml +++ /dev/null @@ -1,89 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/core/src/test/resources/com/opensymphony/xwork2/config/providers/xwork-test-allowstatic-true.xml b/core/src/test/resources/com/opensymphony/xwork2/config/providers/xwork-test-allowstatic-true.xml deleted file mode 100644 index 220432cae..000000000 --- a/core/src/test/resources/com/opensymphony/xwork2/config/providers/xwork-test-allowstatic-true.xml +++ /dev/null @@ -1,89 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/core/src/test/resources/com/opensymphony/xwork2/config/providers/xwork-test-devmode-true.xml b/core/src/test/resources/com/opensymphony/xwork2/config/providers/xwork-test-devmode-true.xml deleted file mode 100644 index 59914b6c4..000000000 --- a/core/src/test/resources/com/opensymphony/xwork2/config/providers/xwork-test-devmode-true.xml +++ /dev/null @@ -1,89 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file