View Javadoc

    1   /*
    2    * $Id: CompositeActionMapperTest.java 478625 2006-11-23 17:31:52Z wsmoak $
    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  package org.apache.struts2.dispatcher.mapper;
    22
    23  import java.util.Iterator;
    24  import java.util.LinkedHashMap;
    25  import java.util.List;
    26  import java.util.Map;
    27
    28  import javax.servlet.http.HttpServletRequest;
    29
    30  import org.apache.struts2.StrutsConstants;
    31  import org.springframework.mock.web.MockHttpServletRequest;
    32
    33  import com.mockobjects.dynamic.C;
    34  import com.mockobjects.dynamic.Mock;
    35  import com.opensymphony.xwork2.config.ConfigurationManager;
    36  import com.opensymphony.xwork2.inject.Container;
    37  import com.opensymphony.xwork2.inject.Scope.Strategy;
    38
    39  import junit.framework.TestCase;
    40
    41  /***
    42   *
    43   * @version $Date: 2006-11-23 12:31:52 -0500 (Thu, 23 Nov 2006) $ $Id: CompositeActionMapperTest.java 478625 2006-11-23 17:31:52Z wsmoak $
    44   */
    45  public class CompositeActionMapperTest extends TestCase {
    46
    47      CompositeActionMapper compositeActionMapper;
    48      Mock mockContainer;
    49
    50      public void setUp() throws Exception {
    51          compositeActionMapper = new CompositeActionMapper();
    52          mockContainer = new Mock(Container.class);
    53          compositeActionMapper.setContainer((Container)mockContainer.proxy());
    54      }
    55
    56
    57      public void testGetActionMappingAndUri1() throws Exception {
    58          ActionMapper mapper1 = new InnerActionMapper1();
    59          ActionMapper mapper2 = new InnerActionMapper2();
    60          ActionMapper mapper3 = new InnerActionMapper3();
    61          mockContainer.expectAndReturn("getInstance", C.args(C.eq(ActionMapper.class), C.eq("mapper1")), mapper1);
    62          mockContainer.expectAndReturn("getInstance", C.args(C.eq(ActionMapper.class), C.eq("mapper2")), mapper3);
    63          mockContainer.expectAndReturn("getInstance", C.args(C.eq(ActionMapper.class), C.eq("mapper3")), mapper2);
    64          compositeActionMapper.setActionMappers("mapper1,mapper2,mapper3");
    65
    66          ActionMapping actionMapping = compositeActionMapper.getMapping(new MockHttpServletRequest(), new ConfigurationManager());
    67          String uri = compositeActionMapper.getUriFromActionMapping(new ActionMapping());
    68          mockContainer.verify();
    69
    70          assertNotNull(actionMapping);
    71          assertNotNull(uri);
    72          assertTrue(actionMapping == InnerActionMapper3.actionMapping);
    73          assertTrue(uri == InnerActionMapper3.uri);
    74
    75      }
    76
    77      public void testGetActionMappingAndUri2() throws Exception {
    78          ActionMapper mapper1 = new InnerActionMapper1();
    79          ActionMapper mapper2 = new InnerActionMapper2();
    80          mockContainer.expectAndReturn("getInstance", C.args(C.eq(ActionMapper.class), C.eq("mapper1")), mapper1);
    81          mockContainer.expectAndReturn("getInstance", C.args(C.eq(ActionMapper.class), C.eq("mapper2")), mapper2);
    82          compositeActionMapper.setActionMappers("mapper1,mapper2");
    83
    84          ActionMapping actionMapping = compositeActionMapper.getMapping(new MockHttpServletRequest(), new ConfigurationManager());
    85          String uri = compositeActionMapper.getUriFromActionMapping(new ActionMapping());
    86          mockContainer.verify();
    87
    88          assertNull(actionMapping);
    89          assertNull(uri);
    90      }
    91
    92
    93      public static class InnerActionMapper1 implements ActionMapper {
    94          public static ActionMapping actionMapping = new ActionMapping();
    95          public static String uri="uri1";
    96
    97          public ActionMapping getMapping(HttpServletRequest request, ConfigurationManager configManager) {
    98              return null;
    99          }
    100         public String getUriFromActionMapping(ActionMapping mapping) {
    101             return null;
    102         }
    103     }
    104     public static class InnerActionMapper2 implements ActionMapper {
    105         public static ActionMapping actionMapping = new ActionMapping();
    106         public static String uri="uri2";
    107
    108         public ActionMapping getMapping(HttpServletRequest request, ConfigurationManager configManager) {
    109             return null;
    110         }
    111         public String getUriFromActionMapping(ActionMapping mapping) {
    112             return null;
    113         }
    114     }
    115     public static class InnerActionMapper3 implements ActionMapper {
    116         public static ActionMapping actionMapping = new ActionMapping();
    117         public static String uri = "uri3";
    118
    119         public ActionMapping getMapping(HttpServletRequest request, ConfigurationManager configManager) {
    120             return actionMapping;
    121         }
    122         public String getUriFromActionMapping(ActionMapping mapping) {
    123             return uri;
    124         }
    125     }
    126 }