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;
18  
19  import java.util.Collection;
20  
21  import javax.security.auth.Subject;
22  
23  import junit.framework.Test;
24  import junit.framework.TestSuite;
25  
26  import org.apache.jetspeed.security.impl.GeneralizationHierarchyResolver;
27  import org.apache.jetspeed.security.impl.RolePrincipalImpl;
28  import org.apache.jetspeed.security.impl.UserManagerImpl;
29  import org.apache.jetspeed.security.util.test.AbstractSecurityTestcase;
30  
31  /***
32   * <p>Unit testing for {@link GeneralizationHierarchyResolver}.</p>
33   *
34   * @author <a href="mailto:Artem.Grinshtein@t-systems.com">Artem Grinshtein</a>
35   * @version $Id: TestGeneralizationHierarchy.java 516448 2007-03-09 16:25:47Z ate $
36   */
37  public class TestGeneralizationHierarchy extends AbstractSecurityTestcase
38  {
39  
40      /***
41       * @see junit.framework.TestCase#setUp()
42       */
43      protected void setUp() throws Exception
44      {
45          super.setUp();
46          ums = new UserManagerImpl(securityProvider, new GeneralizationHierarchyResolver(), new GeneralizationHierarchyResolver());
47      }
48     
49      /***
50       * @see junit.framework.TestCase#tearDown()
51       */
52      public void tearDown() throws Exception
53      {       
54          destroyUserObject();
55          super.tearDown();
56      }
57  
58    
59      
60      public static Test suite()
61      {
62             return new TestSuite(TestGeneralizationHierarchy.class);
63      }
64  
65  
66      /***
67       * <p>Test RoleManager.</p>
68       */
69      public void testRoleManager()
70      {
71          
72          User user = null;
73          try
74          {
75              ums.addUser("test", "password");
76              user = ums.getUser("test");
77          }
78          catch (SecurityException sex)
79          {
80              assertTrue("user exists. should not have thrown an exception.", false);
81          }
82          assertNotNull("user is null", user);
83          
84          try
85          {
86              rms.addRole("rootrole");
87              rms.addRole("rootrole.childrole1");
88              rms.addRole("rootrole.childrole2");
89      
90          }
91          catch (SecurityException sex)
92          {
93              assertTrue("add roles. should not have thrown an exception.", false);
94          }
95          
96          try
97          {
98              rms.addRoleToUser("test","rootrole");
99          
100             user = ums.getUser("test");
101             Subject subject = user.getSubject();
102             assertNotNull("subject is null", subject);
103             Collection principals = getPrincipals(subject, RolePrincipal.class);
104             assertEquals("shoud have one principal;", 1 , principals.size());
105             
106             assertTrue(
107                     "should contain rootrole",
108                     principals.contains(new RolePrincipalImpl("rootrole")));
109             
110             rms.removeRoleFromUser("test","rootrole");
111             
112             user = ums.getUser("test");
113             principals= getPrincipals(user.getSubject(),RolePrincipal.class);
114             assertEquals("shoud not have any principals;", 0,principals.size());
115             
116         }
117         catch (SecurityException sex)
118         {
119             assertTrue("test with parent role "+sex.getMessage(), false);
120         }
121         
122         try
123         {
124             rms.addRoleToUser("test","rootrole.childrole1");
125         
126             user = ums.getUser("test");
127             Subject subject = user.getSubject();
128             assertNotNull("subject is null", subject);
129             Collection principals=getPrincipals(subject,RolePrincipal.class);
130             assertEquals("expected 2 principals;", 2,principals.size());
131             
132             assertTrue(
133                     "should contain rootrole",
134                     principals.contains(new RolePrincipalImpl("rootrole")));
135             
136             assertTrue(
137                     "should contain rootrole",
138                     principals.contains(new RolePrincipalImpl("rootrole.childrole1")));
139            
140             rms.removeRoleFromUser("test","rootrole.childrole1");
141             
142             user = ums.getUser("test");
143             principals=getPrincipals(user.getSubject(),RolePrincipal.class);
144             assertEquals("shoud not have any principals;", 0,principals.size());
145             
146         }
147         catch (SecurityException sex)
148         {
149             assertTrue("test with child role "+sex.getMessage(), false);
150         }
151         
152         
153     }
154 
155 
156    
157 
158     /***
159      * <p>Destroy user test object.</p>
160      */
161     protected void destroyUserObject()
162     {
163         try
164         {
165             
166             if (ums.userExists("test")) ums.removeUser("test");
167             if (rms.roleExists("rootrole")) rms.removeRole("rootrole");
168            
169             
170         }
171         catch (SecurityException sex)
172         {
173             System.out.println("could not remove test users. exception caught: " + sex);
174         }
175     }
176 
177 }