Skip to content

Commit ce4f192

Browse files
committed
add properties support to XWorkTestCase.loadButAdd
also includes cleanups for PRs #292 and #320 (cherry picked from commit a15c12a)
1 parent 3ee47f1 commit ce4f192

File tree

9 files changed

+34
-616
lines changed

9 files changed

+34
-616
lines changed

core/src/main/java/com/opensymphony/xwork2/XWorkTestCase.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.opensymphony.xwork2.util.XWorkTestCaseHelper;
2828
import com.opensymphony.xwork2.util.location.LocatableProperties;
2929
import junit.framework.TestCase;
30+
import org.apache.commons.lang3.ClassUtils;
3031

3132
/**
3233
* Base JUnit TestCase to extend for XWork specific JUnit tests. Uses
@@ -78,16 +79,20 @@ protected void loadButAdd(final Class<?> type, final String name, final Object i
7879
@Override
7980
public void register(ContainerBuilder builder,
8081
LocatableProperties props) throws ConfigurationException {
81-
builder.factory(type, name, new Factory() {
82-
public Object create(Context context) throws Exception {
83-
return impl;
84-
}
82+
if (impl instanceof String || ClassUtils.isPrimitiveOrWrapper(impl.getClass())) {
83+
props.setProperty(name, "" + impl);
84+
} else {
85+
builder.factory(type, name, new Factory() {
86+
public Object create(Context context) throws Exception {
87+
return impl;
88+
}
8589

86-
@Override
87-
public Class type() {
88-
return impl.getClass();
89-
}
90-
}, Scope.SINGLETON);
90+
@Override
91+
public Class type() {
92+
return impl.getClass();
93+
}
94+
}, Scope.SINGLETON);
95+
}
9196
}
9297
});
9398
}

core/src/test/java/com/opensymphony/xwork2/ognl/OgnlUtilTest.java

Lines changed: 20 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@
1919
package com.opensymphony.xwork2.ognl;
2020

2121
import com.opensymphony.xwork2.ActionContext;
22-
import com.opensymphony.xwork2.ActionProxyFactory;
2322
import com.opensymphony.xwork2.XWorkException;
2423
import com.opensymphony.xwork2.XWorkTestCase;
25-
import com.opensymphony.xwork2.config.ConfigurationManager;
26-
import com.opensymphony.xwork2.config.ConfigurationProvider;
27-
import com.opensymphony.xwork2.config.providers.XmlConfigurationProvider;
24+
import com.opensymphony.xwork2.config.ConfigurationException;
2825
import com.opensymphony.xwork2.conversion.impl.XWorkConverter;
29-
import com.opensymphony.xwork2.inject.Container;
26+
import com.opensymphony.xwork2.inject.ContainerBuilder;
3027
import com.opensymphony.xwork2.interceptor.ChainingInterceptor;
28+
import com.opensymphony.xwork2.test.StubConfigurationProvider;
3129
import com.opensymphony.xwork2.test.User;
3230
import com.opensymphony.xwork2.util.*;
31+
import com.opensymphony.xwork2.util.location.LocatableProperties;
3332
import com.opensymphony.xwork2.util.reflection.ReflectionContextState;
3433
import ognl.*;
34+
import org.apache.struts2.StrutsConstants;
3535

3636
import java.lang.reflect.Method;
3737
import java.text.DateFormat;
@@ -1312,61 +1312,25 @@ public void testGetExcludedPackageNamePatterns() {
13121312
}
13131313

13141314
private void reloadTestContainerConfiguration(boolean devMode, boolean allowStaticMethod) throws Exception {
1315-
super.tearDown();
1316-
1317-
ConfigurationProvider configurationProvider;
1318-
if (devMode == true && allowStaticMethod == true) {
1319-
configurationProvider = new XmlConfigurationProvider("com/opensymphony/xwork2/config/providers/xwork-test-allowstatic-devmode-true.xml", true);
1320-
}
1321-
else if (devMode == true && allowStaticMethod == false) {
1322-
configurationProvider = new XmlConfigurationProvider("com/opensymphony/xwork2/config/providers/xwork-test-devmode-true.xml", true);
1323-
}
1324-
else if (devMode == false && allowStaticMethod == true) {
1325-
configurationProvider = new XmlConfigurationProvider("com/opensymphony/xwork2/config/providers/xwork-test-allowstatic-true.xml", true);
1326-
}
1327-
else { // devMode, allowStatic both false
1328-
configurationProvider = new XmlConfigurationProvider("com/opensymphony/xwork2/config/providers/xwork-test-allowstatic-devmode-false.xml", true);
1329-
}
1330-
1331-
configurationManager = new ConfigurationManager(Container.DEFAULT_NAME);
1332-
configurationManager.addContainerProvider(configurationProvider);
1333-
configuration = configurationManager.getConfiguration();
1334-
container = configuration.getContainer();
1335-
container.inject(configurationProvider);
1336-
configurationProvider.init(configuration);
1337-
actionProxyFactory = container.getInstance(ActionProxyFactory.class);
1338-
1339-
// Reset the value stack
1340-
ValueStack stack = container.getInstance(ValueStackFactory.class).createValueStack();
1341-
stack.getContext().put(ActionContext.CONTAINER, container);
1342-
ActionContext.setContext(new ActionContext(stack.getContext()));
1343-
1315+
loadConfigurationProviders(new StubConfigurationProvider() {
1316+
@Override
1317+
public void register(ContainerBuilder builder,
1318+
LocatableProperties props) throws ConfigurationException {
1319+
props.setProperty(StrutsConstants.STRUTS_DEVMODE, "" + devMode);
1320+
props.setProperty(StrutsConstants.STRUTS_ALLOW_STATIC_METHOD_ACCESS, "" + allowStaticMethod);
1321+
}
1322+
});
13441323
ognlUtil = container.getInstance(OgnlUtil.class);
13451324
}
13461325

13471326
private void reloadTestContainerConfiguration(boolean allowStaticField) throws Exception {
1348-
super.tearDown();
1349-
1350-
ConfigurationProvider configurationProvider;
1351-
if (allowStaticField) {
1352-
configurationProvider = new XmlConfigurationProvider("com/opensymphony/xwork2/config/providers/xwork-test-staticfield-true.xml", true);
1353-
} else {
1354-
configurationProvider = new XmlConfigurationProvider("com/opensymphony/xwork2/config/providers/xwork-test-staticfield-false.xml", true);
1355-
}
1356-
1357-
configurationManager = new ConfigurationManager(Container.DEFAULT_NAME);
1358-
configurationManager.addContainerProvider(configurationProvider);
1359-
configuration = configurationManager.getConfiguration();
1360-
container = configuration.getContainer();
1361-
container.inject(configurationProvider);
1362-
configurationProvider.init(configuration);
1363-
actionProxyFactory = container.getInstance(ActionProxyFactory.class);
1364-
1365-
// Reset the value stack
1366-
ValueStack stack = container.getInstance(ValueStackFactory.class).createValueStack();
1367-
stack.getContext().put(ActionContext.CONTAINER, container);
1368-
ActionContext.setContext(new ActionContext(stack.getContext()));
1369-
1327+
loadConfigurationProviders(new StubConfigurationProvider() {
1328+
@Override
1329+
public void register(ContainerBuilder builder,
1330+
LocatableProperties props) throws ConfigurationException {
1331+
props.setProperty(StrutsConstants.STRUTS_ALLOW_STATIC_FIELD_ACCESS, "" + allowStaticField);
1332+
}
1333+
});
13701334
ognlUtil = container.getInstance(OgnlUtil.class);
13711335
}
13721336

core/src/test/java/com/opensymphony/xwork2/ognl/OgnlValueStackTest.java

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -980,39 +980,6 @@ public void testWarnAboutInvalidProperties() {
980980
assertEquals(null, stack.findValue("address.country.name", String.class));
981981
}
982982

983-
private void reloadTestContainerConfiguration(boolean devMode, boolean allowStatic) throws Exception {
984-
super.tearDown();
985-
986-
ConfigurationProvider configurationProvider;
987-
if (devMode == true && allowStatic == true) {
988-
configurationProvider = new XmlConfigurationProvider("com/opensymphony/xwork2/config/providers/xwork-test-allowstatic-devmode-true.xml", true);
989-
}
990-
else if (devMode == true && allowStatic == false) {
991-
configurationProvider = new XmlConfigurationProvider("com/opensymphony/xwork2/config/providers/xwork-test-devmode-true.xml", true);
992-
}
993-
else if (devMode == false && allowStatic == true) {
994-
configurationProvider = new XmlConfigurationProvider("com/opensymphony/xwork2/config/providers/xwork-test-allowstatic-true.xml", true);
995-
}
996-
else { // devMode, allowStatic both false
997-
configurationProvider = new XmlConfigurationProvider("com/opensymphony/xwork2/config/providers/xwork-test-allowstatic-devmode-false.xml", true);
998-
}
999-
1000-
configurationManager = new ConfigurationManager(Container.DEFAULT_NAME);
1001-
configurationManager.addContainerProvider(configurationProvider);
1002-
configuration = configurationManager.getConfiguration();
1003-
container = configuration.getContainer();
1004-
container.inject(configurationProvider);
1005-
configurationProvider.init(configuration);
1006-
actionProxyFactory = container.getInstance(ActionProxyFactory.class);
1007-
1008-
// Reset the value stack
1009-
ValueStack stack = container.getInstance(ValueStackFactory.class).createValueStack();
1010-
stack.getContext().put(ActionContext.CONTAINER, container);
1011-
ActionContext.setContext(new ActionContext(stack.getContext()));
1012-
1013-
ognlUtil = container.getInstance(OgnlUtil.class);
1014-
}
1015-
1016983
class BadJavaBean {
1017984
private int count;
1018985
private int count2;

core/src/test/resources/com/opensymphony/xwork2/config/providers/xwork-test-allowstatic-devmode-false.xml

Lines changed: 0 additions & 86 deletions
This file was deleted.

0 commit comments

Comments
 (0)