View Javadoc

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