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