Merge pull request #302 from lukaszlenart/WW-4938-test-and-fix

[WW-4938] Adds missing test case and properly fixes injecting internal beans
This commit is contained in:
Yasser Zamani
2018-12-29 20:19:11 +03:30
committed by GitHub
3 changed files with 35 additions and 12 deletions
@@ -160,6 +160,7 @@ public class ObjectFactory implements Serializable {
*/
protected Object injectInternalBeans(Object obj) {
if (obj != null && container != null) {
LOG.debug("Injecting internal beans into [{}]", obj.getClass().getSimpleName());
container.inject(obj);
}
return obj;
@@ -164,13 +164,13 @@ public class SpringObjectFactory extends ObjectFactory implements ApplicationCon
if (appContext.containsBean(beanName)) {
o = appContext.getBean(beanName);
if (injectInternal) {
injectInternalBeans(o);
}
} else {
Class beanClazz = getClassInstance(beanName);
o = buildBean(beanClazz, extraContext);
}
if (injectInternal) {
injectInternalBeans(o);
}
return o;
}
@@ -24,8 +24,10 @@ import com.opensymphony.xwork2.config.entities.ActionConfig;
import com.opensymphony.xwork2.config.entities.InterceptorConfig;
import com.opensymphony.xwork2.config.entities.ResultConfig;
import com.opensymphony.xwork2.inject.ContainerBuilder;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor;
import com.opensymphony.xwork2.mock.DummyTextProvider;
import com.opensymphony.xwork2.test.StubConfigurationProvider;
import com.opensymphony.xwork2.util.location.LocatableProperties;
import com.opensymphony.xwork2.validator.Validator;
@@ -45,16 +47,10 @@ import org.springframework.context.support.StaticApplicationContext;
import java.util.HashMap;
import java.util.Map;
// TODO: Document properly
/**
* @author Simon Stewart
*/
public class SpringObjectFactoryTest extends XWorkTestCase {
StaticApplicationContext sac;
SpringObjectFactory objectFactory;
private StaticApplicationContext sac;
private SpringObjectFactory objectFactory;
@Override
public void setUp() throws Exception {
@@ -70,6 +66,7 @@ public class SpringObjectFactoryTest extends XWorkTestCase {
// No registered beans during initialization; They will be registered in each test, runtime.
props.setProperty("applicationContextPath", "com/opensymphony/xwork2/spring/emptyContext-spring.xml");
builder.factory(TextProvider.class, DummyTextProvider.class);
builder.factory(ObjectFactory.class, SpringObjectFactory.class);
}
@@ -228,7 +225,7 @@ public class SpringObjectFactoryTest extends XWorkTestCase {
}
public void testShouldUseConstructorBasedInjectionWhenCreatingABeanFromAClassName() throws Exception {
SpringObjectFactory factory = (SpringObjectFactory) objectFactory;
SpringObjectFactory factory = objectFactory;
objectFactory.setAlwaysRespectAutowireStrategy(false);
sac.registerSingleton("actionBean", SimpleAction.class, new MutablePropertyValues());
@@ -312,6 +309,31 @@ public class SpringObjectFactoryTest extends XWorkTestCase {
assertTrue("Action should have been advised", action instanceof Advised);
}
public void testInjectingInternalStrutsBeans() throws Exception {
sac.registerPrototype("injectable", InternalBeansInjectable.class, new MutablePropertyValues());
InternalBeansInjectable action = (InternalBeansInjectable) objectFactory.buildBean("injectable", null, true);
assertNotNull("TextProvider should be injected by internal DI", action.getTextProvider());
}
public static class InternalBeansInjectable {
private TextProvider textProvider;
public InternalBeansInjectable() {
}
public TextProvider getTextProvider() {
return textProvider;
}
@Inject
public void setTextProvider(TextProvider textProvider) {
this.textProvider = textProvider;
}
}
public static class ConstructorBean {
private SimpleAction action;