Skip to content

Commit

Permalink
add properties support to XWorkTestCase.loadButAdd
Browse files Browse the repository at this point in the history
also includes cleanups for PRs apache#292 and apache#320

(cherry picked from commit a15c12a)
  • Loading branch information
yasserzamani committed Feb 24, 2019
1 parent 3ee47f1 commit ce4f192
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 616 deletions.
23 changes: 14 additions & 9 deletions core/src/main/java/com/opensymphony/xwork2/XWorkTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
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
Expand Down Expand Up @@ -78,16 +79,20 @@ protected void loadButAdd(final Class<?> type, final String name, final Object i
@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);
}
}
});
}
Expand Down
76 changes: 20 additions & 56 deletions core/src/test/java/com/opensymphony/xwork2/ognl/OgnlUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@
package com.opensymphony.xwork2.ognl;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionProxyFactory;
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;
Expand Down Expand Up @@ -1312,61 +1312,25 @@ public void testGetExcludedPackageNamePatterns() {
}

private void reloadTestContainerConfiguration(boolean devMode, boolean allowStaticMethod) throws Exception {
super.tearDown();

ConfigurationProvider configurationProvider;
if (devMode == true && allowStaticMethod == true) {
configurationProvider = new XmlConfigurationProvider("com/opensymphony/xwork2/config/providers/xwork-test-allowstatic-devmode-true.xml", true);
}
else if (devMode == true && allowStaticMethod == false) {
configurationProvider = new XmlConfigurationProvider("com/opensymphony/xwork2/config/providers/xwork-test-devmode-true.xml", true);
}
else if (devMode == false && allowStaticMethod == 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()));

loadConfigurationProviders(new StubConfigurationProvider() {
@Override
public void register(ContainerBuilder builder,
LocatableProperties props) throws ConfigurationException {
props.setProperty(StrutsConstants.STRUTS_DEVMODE, "" + devMode);
props.setProperty(StrutsConstants.STRUTS_ALLOW_STATIC_METHOD_ACCESS, "" + allowStaticMethod);
}
});
ognlUtil = container.getInstance(OgnlUtil.class);
}

private void reloadTestContainerConfiguration(boolean allowStaticField) throws Exception {
super.tearDown();

ConfigurationProvider configurationProvider;
if (allowStaticField) {
configurationProvider = new XmlConfigurationProvider("com/opensymphony/xwork2/config/providers/xwork-test-staticfield-true.xml", true);
} else {
configurationProvider = new XmlConfigurationProvider("com/opensymphony/xwork2/config/providers/xwork-test-staticfield-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()));

loadConfigurationProviders(new StubConfigurationProvider() {
@Override
public void register(ContainerBuilder builder,
LocatableProperties props) throws ConfigurationException {
props.setProperty(StrutsConstants.STRUTS_ALLOW_STATIC_FIELD_ACCESS, "" + allowStaticField);
}
});
ognlUtil = container.getInstance(OgnlUtil.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -980,39 +980,6 @@ public void testWarnAboutInvalidProperties() {
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;
Expand Down

This file was deleted.

Loading

0 comments on commit ce4f192

Please sign in to comment.