This is an automated email from the ASF dual-hosted git repository.

yasserzamani pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/struts.git


The following commit(s) were added to refs/heads/master by this push:
     new 699f5d6  [WW-4938] Uses container to create an instance of the class 
(#295)
699f5d6 is described below

commit 699f5d6c79ec12978c6fa33ea4e8b5cef70c616f
Author: Lukasz Lenart <lukasz.len...@gmail.com>
AuthorDate: Tue Dec 18 13:25:42 2018 +0100

    [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 <lukasz.len...@gmail.com>
    
    * Update 
core/src/test/java/com/opensymphony/xwork2/mock/DummyTextProvider.java
    
    Co-Authored-By: lukaszlenart <lukasz.len...@gmail.com>
---
 .../com/opensymphony/xwork2/ObjectFactory.java     |  8 +-
 .../xwork2/spring/SpringObjectFactory.java         |  8 +-
 .../com/opensymphony/xwork2/ObjectFactoryTest.java | 46 ++++++++++++
 .../xwork2/mock/DummyTextProvider.java             | 87 ++++++++++++++++++++++
 .../opensymphony/xwork2/mock/InjectableAction.java | 36 +++++++++
 .../PackageBasedActionConfigBuilderTest.java       |  6 +-
 6 files changed, 178 insertions(+), 13 deletions(-)

diff --git a/core/src/main/java/com/opensymphony/xwork2/ObjectFactory.java 
b/core/src/main/java/com/opensymphony/xwork2/ObjectFactory.java
index a5d91c6..6dac12f 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<String, Object> 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<String, Object> 
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 9dead67..0d3df9f 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 0000000..036926e
--- /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<String, Object>());
+
+        // 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 0000000..01bc270
--- /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 0000000..9ef1d35
--- /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 a19be9b..02a4878 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> T inject(Class<T> implementation) {
-            return null;
+            try {
+                return implementation.newInstance();
+            } catch (Exception e) {
+                return null;
+            }
         }
 
         public void removeScopeStrategy() {

Reply via email to