From 699f5d6c79ec12978c6fa33ea4e8b5cef70c616f Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Tue, 18 Dec 2018 13:25:42 +0100 Subject: [PATCH] [WW-4938] Uses container to create an instance of the class (#295) * WW-4938 Uses container to create an instance of the class * WW-4938 Adds a test to proof that @Inject works on constructor * WW-4938 Adds missing header * Update core/src/test/java/com/opensymphony/xwork2/mock/InjectableAction.java Co-Authored-By: lukaszlenart * Update core/src/test/java/com/opensymphony/xwork2/mock/DummyTextProvider.java Co-Authored-By: lukaszlenart --- .../opensymphony/xwork2/ObjectFactory.java | 8 +- .../xwork2/spring/SpringObjectFactory.java | 8 +- .../xwork2/ObjectFactoryTest.java | 46 ++++++++++ .../xwork2/mock/DummyTextProvider.java | 87 +++++++++++++++++++ .../xwork2/mock/InjectableAction.java | 36 ++++++++ .../PackageBasedActionConfigBuilderTest.java | 6 +- 6 files changed, 178 insertions(+), 13 deletions(-) create mode 100644 core/src/test/java/com/opensymphony/xwork2/ObjectFactoryTest.java create mode 100644 core/src/test/java/com/opensymphony/xwork2/mock/DummyTextProvider.java create mode 100644 core/src/test/java/com/opensymphony/xwork2/mock/InjectableAction.java diff --git a/core/src/main/java/com/opensymphony/xwork2/ObjectFactory.java b/core/src/main/java/com/opensymphony/xwork2/ObjectFactory.java index a5d91c68a..6dac12f5d 100644 --- a/core/src/main/java/com/opensymphony/xwork2/ObjectFactory.java +++ b/core/src/main/java/com/opensymphony/xwork2/ObjectFactory.java @@ -151,7 +151,7 @@ public class ObjectFactory implements Serializable { * @throws Exception in case of any error */ public Object buildBean(Class clazz, Map extraContext) throws Exception { - return clazz.newInstance(); + return container.inject(clazz); } /** @@ -188,11 +188,7 @@ public class ObjectFactory implements Serializable { */ public Object buildBean(String className, Map extraContext, boolean injectInternal) throws Exception { Class clazz = getClassInstance(className); - Object obj = buildBean(clazz, extraContext); - if (injectInternal) { - injectInternalBeans(obj); - } - return obj; + return buildBean(clazz, extraContext); } /** diff --git a/core/src/main/java/com/opensymphony/xwork2/spring/SpringObjectFactory.java b/core/src/main/java/com/opensymphony/xwork2/spring/SpringObjectFactory.java index 9dead67ee..0d3df9f22 100644 --- a/core/src/main/java/com/opensymphony/xwork2/spring/SpringObjectFactory.java +++ b/core/src/main/java/com/opensymphony/xwork2/spring/SpringObjectFactory.java @@ -168,9 +168,7 @@ public class SpringObjectFactory extends ObjectFactory implements ApplicationCon Class beanClazz = getClassInstance(beanName); o = buildBean(beanClazz, extraContext); } - if (injectInternal) { - injectInternalBeans(o); - } + return o; } @@ -224,9 +222,7 @@ public class SpringObjectFactory extends ObjectFactory implements ApplicationCon } injectApplicationContext(bean); - injectInternalBeans(bean); - - return bean; + return injectInternalBeans(bean); } private void injectApplicationContext(Object bean) { diff --git a/core/src/test/java/com/opensymphony/xwork2/ObjectFactoryTest.java b/core/src/test/java/com/opensymphony/xwork2/ObjectFactoryTest.java new file mode 100644 index 000000000..036926e2f --- /dev/null +++ b/core/src/test/java/com/opensymphony/xwork2/ObjectFactoryTest.java @@ -0,0 +1,46 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package com.opensymphony.xwork2; + +import com.opensymphony.xwork2.mock.DummyTextProvider; +import com.opensymphony.xwork2.mock.InjectableAction; +import org.apache.struts2.StrutsInternalTestCase; + +import java.util.HashMap; + +public class ObjectFactoryTest extends StrutsInternalTestCase { + + @Override + public void setUp() throws Exception { + super.setUp(); + this.loadButAdd(TextProvider.class, new DummyTextProvider()); + } + + public void testCreatingActionsWithInjectableParametersInConstructor() throws Exception { + // given + ObjectFactory of = container.getInstance(ObjectFactory.class); + + // when + InjectableAction action = (InjectableAction) of.buildBean(InjectableAction.class, new HashMap()); + + // then + assertNotNull(action.getTextProvider()); + assertTrue(action.getTextProvider() instanceof DummyTextProvider); + } +} \ No newline at end of file diff --git a/core/src/test/java/com/opensymphony/xwork2/mock/DummyTextProvider.java b/core/src/test/java/com/opensymphony/xwork2/mock/DummyTextProvider.java new file mode 100644 index 000000000..01bc270a0 --- /dev/null +++ b/core/src/test/java/com/opensymphony/xwork2/mock/DummyTextProvider.java @@ -0,0 +1,87 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + package com.opensymphony.xwork2.mock; + +import com.opensymphony.xwork2.TextProvider; +import com.opensymphony.xwork2.util.ValueStack; + +import java.util.List; +import java.util.ResourceBundle; + +public class DummyTextProvider implements TextProvider { + @Override + public boolean hasKey(String key) { + return false; + } + + @Override + public String getText(String key) { + return null; + } + + @Override + public String getText(String key, String defaultValue) { + return null; + } + + @Override + public String getText(String key, String defaultValue, String obj) { + return null; + } + + @Override + public String getText(String key, List args) { + return null; + } + + @Override + public String getText(String key, String[] args) { + return null; + } + + @Override + public String getText(String key, String defaultValue, List args) { + return null; + } + + @Override + public String getText(String key, String defaultValue, String[] args) { + return null; + } + + @Override + public String getText(String key, String defaultValue, List args, ValueStack stack) { + return null; + } + + @Override + public String getText(String key, String defaultValue, String[] args, ValueStack stack) { + return null; + } + + @Override + public ResourceBundle getTexts(String bundleName) { + return null; + } + + @Override + public ResourceBundle getTexts() { + return null; + } +} diff --git a/core/src/test/java/com/opensymphony/xwork2/mock/InjectableAction.java b/core/src/test/java/com/opensymphony/xwork2/mock/InjectableAction.java new file mode 100644 index 000000000..9ef1d35ba --- /dev/null +++ b/core/src/test/java/com/opensymphony/xwork2/mock/InjectableAction.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + package com.opensymphony.xwork2.mock; + +import com.opensymphony.xwork2.TextProvider; +import com.opensymphony.xwork2.inject.Inject; + +public class InjectableAction { + + private TextProvider textProvider; + + @Inject + public InjectableAction(TextProvider textProvider) { + this.textProvider = textProvider; + } + + public TextProvider getTextProvider() { + return textProvider; + } +} diff --git a/plugins/convention/src/test/java/org/apache/struts2/convention/PackageBasedActionConfigBuilderTest.java b/plugins/convention/src/test/java/org/apache/struts2/convention/PackageBasedActionConfigBuilderTest.java index a19be9bed..02a487815 100644 --- a/plugins/convention/src/test/java/org/apache/struts2/convention/PackageBasedActionConfigBuilderTest.java +++ b/plugins/convention/src/test/java/org/apache/struts2/convention/PackageBasedActionConfigBuilderTest.java @@ -921,7 +921,11 @@ public class PackageBasedActionConfigBuilderTest extends TestCase { } public T inject(Class implementation) { - return null; + try { + return implementation.newInstance(); + } catch (Exception e) { + return null; + } } public void removeScopeStrategy() {