View Javadoc

    1   /*
    2    * $Id: RolesInterceptorTest.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.interceptor;
    22
    23  import java.util.List;
    24
    25  import org.apache.struts2.StrutsTestCase;
    26
    27  import com.mockobjects.servlet.MockHttpServletRequest;
    28  import com.mockobjects.servlet.MockHttpServletResponse;
    29
    30  public class RolesInterceptorTest extends StrutsTestCase {
    31
    32      private RolesInterceptor interceptor = new RolesInterceptor();
    33
    34      public void setUp() throws Exception {
    35          super.setUp();
    36          interceptor = new RolesInterceptor();
    37      }
    38
    39      public void testStringToList() {
    40          List list = interceptor.stringToList("foo");
    41          assertNotNull(list);
    42          assertEquals(1, list.size());
    43
    44          list = interceptor.stringToList("foo,bar");
    45          assertEquals(2, list.size());
    46          assertEquals("foo", (String)list.get(0));
    47
    48          list = interceptor.stringToList("foo, bar");
    49          assertEquals(2, list.size());
    50          assertEquals("bar", (String)list.get(1));
    51
    52          list = interceptor.stringToList("foo  , bar");
    53          assertEquals(2, list.size());
    54          assertEquals("bar", (String)list.get(1));
    55      }
    56
    57      public void testIsAllowed() throws Exception {
    58          MockHttpServletRequest request = new MockHttpServletRequest() {
    59              public boolean isUserInRole(String role) {
    60                  return "admin".equals(role);
    61              }
    62          };
    63          interceptor.setAllowedRoles("admin");
    64          assertTrue(interceptor.isAllowed(request, null));
    65
    66          interceptor.setAllowedRoles("bar, admin");
    67          assertTrue(interceptor.isAllowed(request, null));
    68
    69          interceptor.setAllowedRoles(null);
    70          assertTrue(interceptor.isAllowed(request, null));
    71
    72          interceptor.setDisallowedRoles("bar");
    73          assertTrue(interceptor.isAllowed(request, null));
    74
    75          interceptor.setDisallowedRoles("bar, admin");
    76          assertTrue(!interceptor.isAllowed(request, null));
    77
    78      }
    79
    80      public void testHandleRejection() throws Exception {
    81          MockHttpServletResponse response = new MockHttpServletResponse();
    82          response.setExpectedError(response.SC_FORBIDDEN);
    83          interceptor.handleRejection(null, response);
    84          response.verify();
    85      }
    86  }