View Javadoc

1   /*
2    * $Id: CompositeActionMapperTest.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.struts2.dispatcher.mapper;
23  
24  import java.util.Iterator;
25  import java.util.LinkedHashMap;
26  import java.util.List;
27  import java.util.Map;
28  
29  import javax.servlet.http.HttpServletRequest;
30  
31  import org.apache.struts2.StrutsConstants;
32  import org.springframework.mock.web.MockHttpServletRequest;
33  
34  import com.mockobjects.dynamic.C;
35  import com.mockobjects.dynamic.Mock;
36  import com.opensymphony.xwork2.config.ConfigurationManager;
37  import com.opensymphony.xwork2.inject.Container;
38  import com.opensymphony.xwork2.inject.Scope.Strategy;
39  
40  import junit.framework.TestCase;
41  
42  /***
43   *
44   * @version $Date: 2008-04-27 08:41:38 -0500 (Sun, 27 Apr 2008) $ $Id: CompositeActionMapperTest.java 651946 2008-04-27 13:41:38Z apetrelli $
45   */
46  public class CompositeActionMapperTest extends TestCase {
47  
48      CompositeActionMapper compositeActionMapper;
49      Mock mockContainer;
50      
51      public void setUp() throws Exception {
52          compositeActionMapper = new CompositeActionMapper();
53          mockContainer = new Mock(Container.class);
54          compositeActionMapper.setContainer((Container)mockContainer.proxy());
55      }
56      
57  
58      public void testGetActionMappingAndUri1() throws Exception {
59          ActionMapper mapper1 = new InnerActionMapper1();
60          ActionMapper mapper2 = new InnerActionMapper2();
61          ActionMapper mapper3 = new InnerActionMapper3();
62          mockContainer.expectAndReturn("getInstance", C.args(C.eq(ActionMapper.class), C.eq("mapper1")), mapper1);
63          mockContainer.expectAndReturn("getInstance", C.args(C.eq(ActionMapper.class), C.eq("mapper2")), mapper3);
64          mockContainer.expectAndReturn("getInstance", C.args(C.eq(ActionMapper.class), C.eq("mapper3")), mapper2);
65          compositeActionMapper.setActionMappers("mapper1,mapper2,mapper3");
66          
67          ActionMapping actionMapping = compositeActionMapper.getMapping(new MockHttpServletRequest(), new ConfigurationManager());
68          String uri = compositeActionMapper.getUriFromActionMapping(new ActionMapping());
69          mockContainer.verify();
70          
71          assertNotNull(actionMapping);
72          assertNotNull(uri);
73          assertTrue(actionMapping == InnerActionMapper3.actionMapping);
74          assertTrue(uri == InnerActionMapper3.uri);
75          
76      }
77  
78      public void testGetActionMappingAndUri2() throws Exception {
79          ActionMapper mapper1 = new InnerActionMapper1();
80          ActionMapper mapper2 = new InnerActionMapper2();
81          mockContainer.expectAndReturn("getInstance", C.args(C.eq(ActionMapper.class), C.eq("mapper1")), mapper1);
82          mockContainer.expectAndReturn("getInstance", C.args(C.eq(ActionMapper.class), C.eq("mapper2")), mapper2);
83          compositeActionMapper.setActionMappers("mapper1,mapper2");
84  
85          ActionMapping actionMapping = compositeActionMapper.getMapping(new MockHttpServletRequest(), new ConfigurationManager());
86          String uri = compositeActionMapper.getUriFromActionMapping(new ActionMapping());
87          mockContainer.verify();
88  
89          assertNull(actionMapping);
90          assertNull(uri);
91      }
92  
93  
94      public static class InnerActionMapper1 implements ActionMapper {
95          public static ActionMapping actionMapping = new ActionMapping();
96          public static String uri="uri1";
97  
98          public ActionMapping getMapping(HttpServletRequest request, ConfigurationManager configManager) {
99              return null;
100         }
101 
102         public ActionMapping getMappingFromActionName(String actionName) {
103             return null;  //To change body of implemented methods use File | Settings | File Templates.
104         }
105 
106         public String getUriFromActionMapping(ActionMapping mapping) {
107             return null;
108         }
109     }
110     public static class InnerActionMapper2 implements ActionMapper {
111         public static ActionMapping actionMapping = new ActionMapping();
112         public static String uri="uri2";
113 
114         public ActionMapping getMapping(HttpServletRequest request, ConfigurationManager configManager) {
115             return null;
116         }
117 
118         public ActionMapping getMappingFromActionName(String actionName) {
119             return null;  //To change body of implemented methods use File | Settings | File Templates.
120         }
121 
122         public String getUriFromActionMapping(ActionMapping mapping) {
123             return null;
124         }
125     }
126     public static class InnerActionMapper3 implements ActionMapper {
127         public static ActionMapping actionMapping = new ActionMapping();
128         public static String uri = "uri3";
129 
130         public ActionMapping getMapping(HttpServletRequest request, ConfigurationManager configManager) {
131             return actionMapping;
132         }
133 
134         public ActionMapping getMappingFromActionName(String actionName) {
135             return null;  //To change body of implemented methods use File | Settings | File Templates.
136         }
137 
138         public String getUriFromActionMapping(ActionMapping mapping) {
139             return uri;
140         }
141     }
142 }