Author: lukaszlenart Date: Thu Apr 18 09:31:24 2013 New Revision: 1469245 URL: http://svn.apache.org/r1469245 Log: WW-4048 Solves problem with instating a class and adds additional test
Added: struts/struts2/trunk/core/src/test/java/org/apache/struts2/ClassInstantiationTest.java Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/mapper/CompositeActionMapper.java struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/mapper/CompositeActionMapperTest.java Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/mapper/CompositeActionMapper.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/mapper/CompositeActionMapper.java?rev=1469245&r1=1469244&r2=1469245&view=diff ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/mapper/CompositeActionMapper.java (original) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/mapper/CompositeActionMapper.java Thu Apr 18 09:31:24 2013 @@ -29,7 +29,7 @@ import com.opensymphony.xwork2.util.logg import org.apache.struts2.StrutsConstants; import javax.servlet.http.HttpServletRequest; -import java.util.ArrayList; +import java.util.LinkedList; import java.util.List; /** @@ -37,42 +37,8 @@ import java.util.List; * * A composite action mapper that is capable of delegating to a series of {@link ActionMapper} if the former * failed to obtained a valid {@link ActionMapping} or uri. - * <p/> - * It is configured through struts.properties. - * <p/> - * For example, with the following entries in struts.properties - * <p/> - * <pre> - * <bean type="org.apache.struts2.dispatcher.mapper.ActionMapper" name="struts" - * class="org.apache.struts2.dispatcher.mapper.CompositeActionMapper" /> - * <constant name="struts.mapper.composite" - * value="org.apache.struts2.dispatcher.mapper.DefaultActionMapper,org.apache.struts2.dispatcher.mapper.RestfulActionMapper,org.apache.struts2.dispatcher.mapper.Restful2ActionMapper" /> - * </pre> - * <p/> - * When {@link CompositeActionMapper#getMapping(HttpServletRequest, ConfigurationManager)} or - * {@link CompositeActionMapper#getUriFromActionMapping(ActionMapping)} is invoked, - * {@link CompositeActionMapper} would go through these {@link ActionMapper}s in sequence - * starting from {@link ActionMapper} identified by 'struts.mapper.composite.1', followed by - * 'struts.mapper.composite.2' and finally 'struts.mapper.composite.3' (in this case) until either - * one of the {@link ActionMapper} return a valid result (not null) or it runs out of {@link ActionMapper} - * in which case it will just return null for both - * {@link CompositeActionMapper#getMapping(HttpServletRequest, ConfigurationManager)} and - * {@link CompositeActionMapper#getUriFromActionMapping(ActionMapping)} methods. - * <p/> * - * For example with the following in struts-*.xml :- - * <pre> - * <bean type="org.apache.struts2.dispatcher.mapper.ActionMapper" name="struts" - * class="org.apache.struts2.dispatcher.mapper.CompositeActionMapper" /> - * <constant name="struts.mapper.composite" - * value="org.apache.struts2.dispatcher.mapper.DefaultActionMapper,foo.bar.MyActionMapper,foo.bar.MyAnotherActionMapper" /> - * </pre> - * <p/> - * <code>CompositeActionMapper</code> will be configured with 3 ActionMapper, namely - * "DefaultActionMapper", "MyActionMapper" and "MyAnotherActionMapper". - * <code>CompositeActionMapper</code> would consult each of them in order described above. - * - * <!-- END SNIPPET: description --> + * More details: http://struts.apache.org/2.x/docs/actionmapper.html * * @see ActionMapper * @see ActionMapping @@ -83,16 +49,11 @@ public class CompositeActionMapper imple private static final Logger LOG = LoggerFactory.getLogger(CompositeActionMapper.class); - protected Container container; - - protected List<ActionMapper> actionMappers = new ArrayList<ActionMapper>(); - - public CompositeActionMapper(@Inject Container container) { - this.container = container; - } + protected List<ActionMapper> actionMappers = new LinkedList<ActionMapper>(); - @Inject(StrutsConstants.STRUTS_MAPPER_COMPOSITE) - public void setActionMappers(String list) { + @Inject + public CompositeActionMapper(Container container, + @Inject(value = StrutsConstants.STRUTS_MAPPER_COMPOSITE) String list) { if (list != null) { String[] arr = list.split(","); for (String name : arr) { @@ -104,7 +65,6 @@ public class CompositeActionMapper imple } } - public ActionMapping getMapping(HttpServletRequest request, ConfigurationManager configManager) { for (ActionMapper actionMapper : actionMappers) { Added: struts/struts2/trunk/core/src/test/java/org/apache/struts2/ClassInstantiationTest.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/ClassInstantiationTest.java?rev=1469245&view=auto ============================================================================== --- struts/struts2/trunk/core/src/test/java/org/apache/struts2/ClassInstantiationTest.java (added) +++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/ClassInstantiationTest.java Thu Apr 18 09:31:24 2013 @@ -0,0 +1,47 @@ +package org.apache.struts2; + +import com.opensymphony.xwork2.inject.Container; +import org.apache.struts2.dispatcher.Dispatcher; +import org.apache.struts2.dispatcher.mapper.ActionMapper; + +import java.util.HashMap; + +/** + * Test class instantiation with Container + */ +public class ClassInstantiationTest extends StrutsTestCase { + + public void testCompositeActionMapperInstantiationWithList() throws Exception { + // given + Dispatcher du = initDispatcher(new HashMap<String, String>() {{ + put(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8"); + put(StrutsConstants.STRUTS_MAPPER_COMPOSITE, "struts,restful"); + }}); + Container container = du.getContainer(); + + // when + ActionMapper instance = container.getInstance(ActionMapper.class, "composite"); + + // then + assertNotNull(instance); + } + + public void testCompositeActionMapperInstantiationWithoutList() throws Exception { + // given + Dispatcher du = initDispatcher(new HashMap<String, String>() {{ + put(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8"); + }}); + Container container = du.getContainer(); + + // when + try { + container.getInstance(ActionMapper.class, "composite"); + fail(); + }catch (Exception e) { + // then + // You cannot use CompositeActionMapper without defined list of "struts.mapper.composite" + assertTrue(e.getMessage().contains("No mapping found for dependency [type=java.lang.String, name='struts.mapper.composite']")); + } + } + +} Modified: struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/mapper/CompositeActionMapperTest.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/mapper/CompositeActionMapperTest.java?rev=1469245&r1=1469244&r2=1469245&view=diff ============================================================================== --- struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/mapper/CompositeActionMapperTest.java (original) +++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/mapper/CompositeActionMapperTest.java Thu Apr 18 09:31:24 2013 @@ -36,15 +36,12 @@ import javax.servlet.http.HttpServletReq */ public class CompositeActionMapperTest extends TestCase { - CompositeActionMapper compositeActionMapper; Mock mockContainer; public void setUp() throws Exception { mockContainer = new Mock(Container.class); - compositeActionMapper = new CompositeActionMapper((Container)mockContainer.proxy()); } - public void testGetActionMappingAndUri1() throws Exception { ActionMapper mapper1 = new InnerActionMapper1(); ActionMapper mapper2 = new InnerActionMapper2(); @@ -52,7 +49,7 @@ public class CompositeActionMapperTest e mockContainer.expectAndReturn("getInstance", C.args(C.eq(ActionMapper.class), C.eq("mapper1")), mapper1); mockContainer.expectAndReturn("getInstance", C.args(C.eq(ActionMapper.class), C.eq("mapper2")), mapper3); mockContainer.expectAndReturn("getInstance", C.args(C.eq(ActionMapper.class), C.eq("mapper3")), mapper2); - compositeActionMapper.setActionMappers("mapper1,mapper2,mapper3"); + CompositeActionMapper compositeActionMapper = new CompositeActionMapper((Container) mockContainer.proxy(), "mapper1,mapper2,mapper3"); ActionMapping actionMapping = compositeActionMapper.getMapping(new MockHttpServletRequest(), new ConfigurationManager()); String uri = compositeActionMapper.getUriFromActionMapping(new ActionMapping()); @@ -70,7 +67,7 @@ public class CompositeActionMapperTest e ActionMapper mapper2 = new InnerActionMapper2(); mockContainer.expectAndReturn("getInstance", C.args(C.eq(ActionMapper.class), C.eq("mapper1")), mapper1); mockContainer.expectAndReturn("getInstance", C.args(C.eq(ActionMapper.class), C.eq("mapper2")), mapper2); - compositeActionMapper.setActionMappers("mapper1,mapper2"); + CompositeActionMapper compositeActionMapper = new CompositeActionMapper((Container) mockContainer.proxy(), "mapper1,mapper2"); ActionMapping actionMapping = compositeActionMapper.getMapping(new MockHttpServletRequest(), new ConfigurationManager()); String uri = compositeActionMapper.getUriFromActionMapping(new ActionMapping());