1   /* 
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.security.spi;
18  
19  import java.util.Set;
20  
21  import org.apache.jetspeed.security.util.test.AbstractSecurityTestcase;
22  
23  import junit.framework.Test;
24  import junit.framework.TestSuite;
25  
26  /***
27   * <p>
28   * Unit testing for {@link GroupSecurityHandler}.
29   * </p>
30   * 
31   * @author <a href="mailto:dlestrat@apache.org">David Le Strat </a>
32   */
33  public class TestSecurityMappingHandler extends AbstractSecurityTestcase
34  {
35  
36     
37  
38      /***
39       * @see junit.framework.TestCase#setUp()
40       */
41      protected void setUp() throws Exception
42      {
43          super.setUp();
44          destroyGroupUser();
45          destroyRoleUser();
46          initGroupUser();
47          initRoleUser();
48      }
49  
50      /***
51       * @see junit.framework.TestCase#tearDown()
52       */
53      public void tearDown() throws Exception
54      {
55          destroyGroupUser();
56          destroyRoleUser();
57          super.tearDown();
58      }
59  
60      /***
61       * <p>
62       * Constructs the suite.
63       * </p>
64       * 
65       * @return The {@Test}.
66       */
67      public static Test suite()
68      {
69          return new TestSuite(TestSecurityMappingHandler.class);
70      }
71  
72      /***
73       * <p>
74       * Test <code>getRolePrincipals</code>.
75       * </p>
76       */
77      public void testGetRolePrincipals() throws Exception
78      {
79  
80          Set principals = smh.getRolePrincipals("testuser");
81          assertNotNull(principals);
82          // Hierarchy by generalization should return 3 roles.
83          assertEquals(3, principals.size());
84  
85      }
86      
87      /***
88       * <p>
89       * Test <code>getUserPrincipal</code>.
90       * </p>
91       */
92      public void testGetGroupPrincipals() throws Exception
93      {
94    
95          Set principals = smh.getGroupPrincipals("testuser");
96          assertNotNull(principals);
97          // Hierarchy by generalization should return 3 roles.
98          assertEquals(3, principals.size());
99  
100     }
101 
102     /***
103      * <p>
104      * Initialize user test object.
105      * </p>
106      */
107     protected void initGroupUser() throws Exception
108     {
109         ums.addUser("testuser", "password");
110         gms.addGroup("testusertogroup1");
111         gms.addGroup("testusertogroup2.group1");
112         gms.addUserToGroup("testuser", "testusertogroup1");
113         gms.addUserToGroup("testuser", "testusertogroup2.group1");
114     }
115 
116     /***
117      * <p>
118      * Destroy user test object.
119      * </p>
120      */
121     protected void destroyGroupUser() throws Exception
122     {
123         ums.removeUser("testuser");
124         gms.removeGroup("testusertogroup1");
125         gms.removeGroup("testusertogroup2");
126         gms.removeGroup("testusertogroup2.group1");
127     }
128     
129     /***
130      * <p>
131      * Initialize user test object.
132      * </p>
133      */
134     protected void initRoleUser() throws Exception
135     {
136         
137         rms.addRole("testusertorole1");
138         rms.addRole("testusertorole2.role1");
139         rms.addRoleToUser("testuser", "testusertorole1");
140         rms.addRoleToUser("testuser", "testusertorole2.role1");
141     }
142 
143     /***
144      * <p>
145      * Destroy user test object.
146      * </p>
147      */
148     protected void destroyRoleUser() throws Exception
149     {
150 
151         rms.removeRole("testusertorole1");
152         rms.removeRole("testusertorole2");
153         rms.removeRole("testusertorole2.role1");
154     }
155 
156 }