001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.security;
018:
019: import java.util.Collection;
020:
021: import javax.security.auth.Subject;
022:
023: import junit.framework.Test;
024: import junit.framework.TestSuite;
025:
026: import org.apache.jetspeed.security.impl.GeneralizationHierarchyResolver;
027: import org.apache.jetspeed.security.impl.RolePrincipalImpl;
028: import org.apache.jetspeed.security.impl.UserManagerImpl;
029: import org.apache.jetspeed.security.util.test.AbstractSecurityTestcase;
030:
031: /**
032: * <p>Unit testing for {@link GeneralizationHierarchyResolver}.</p>
033: *
034: * @author <a href="mailto:Artem.Grinshtein@t-systems.com">Artem Grinshtein</a>
035: * @version $Id: TestGeneralizationHierarchy.java 516448 2007-03-09 16:25:47Z ate $
036: */
037: public class TestGeneralizationHierarchy extends
038: AbstractSecurityTestcase {
039:
040: /**
041: * @see junit.framework.TestCase#setUp()
042: */
043: protected void setUp() throws Exception {
044: super .setUp();
045: ums = new UserManagerImpl(securityProvider,
046: new GeneralizationHierarchyResolver(),
047: new GeneralizationHierarchyResolver());
048: }
049:
050: /**
051: * @see junit.framework.TestCase#tearDown()
052: */
053: public void tearDown() throws Exception {
054: destroyUserObject();
055: super .tearDown();
056: }
057:
058: public static Test suite() {
059: return new TestSuite(TestGeneralizationHierarchy.class);
060: }
061:
062: /**
063: * <p>Test RoleManager.</p>
064: */
065: public void testRoleManager() {
066:
067: User user = null;
068: try {
069: ums.addUser("test", "password");
070: user = ums.getUser("test");
071: } catch (SecurityException sex) {
072: assertTrue(
073: "user exists. should not have thrown an exception.",
074: false);
075: }
076: assertNotNull("user is null", user);
077:
078: try {
079: rms.addRole("rootrole");
080: rms.addRole("rootrole.childrole1");
081: rms.addRole("rootrole.childrole2");
082:
083: } catch (SecurityException sex) {
084: assertTrue(
085: "add roles. should not have thrown an exception.",
086: false);
087: }
088:
089: try {
090: rms.addRoleToUser("test", "rootrole");
091:
092: user = ums.getUser("test");
093: Subject subject = user.getSubject();
094: assertNotNull("subject is null", subject);
095: Collection principals = getPrincipals(subject,
096: RolePrincipal.class);
097: assertEquals("shoud have one principal;", 1, principals
098: .size());
099:
100: assertTrue("should contain rootrole", principals
101: .contains(new RolePrincipalImpl("rootrole")));
102:
103: rms.removeRoleFromUser("test", "rootrole");
104:
105: user = ums.getUser("test");
106: principals = getPrincipals(user.getSubject(),
107: RolePrincipal.class);
108: assertEquals("shoud not have any principals;", 0,
109: principals.size());
110:
111: } catch (SecurityException sex) {
112: assertTrue("test with parent role " + sex.getMessage(),
113: false);
114: }
115:
116: try {
117: rms.addRoleToUser("test", "rootrole.childrole1");
118:
119: user = ums.getUser("test");
120: Subject subject = user.getSubject();
121: assertNotNull("subject is null", subject);
122: Collection principals = getPrincipals(subject,
123: RolePrincipal.class);
124: assertEquals("expected 2 principals;", 2, principals.size());
125:
126: assertTrue("should contain rootrole", principals
127: .contains(new RolePrincipalImpl("rootrole")));
128:
129: assertTrue("should contain rootrole", principals
130: .contains(new RolePrincipalImpl(
131: "rootrole.childrole1")));
132:
133: rms.removeRoleFromUser("test", "rootrole.childrole1");
134:
135: user = ums.getUser("test");
136: principals = getPrincipals(user.getSubject(),
137: RolePrincipal.class);
138: assertEquals("shoud not have any principals;", 0,
139: principals.size());
140:
141: } catch (SecurityException sex) {
142: assertTrue("test with child role " + sex.getMessage(),
143: false);
144: }
145:
146: }
147:
148: /**
149: * <p>Destroy user test object.</p>
150: */
151: protected void destroyUserObject() {
152: try {
153:
154: if (ums.userExists("test"))
155: ums.removeUser("test");
156: if (rms.roleExists("rootrole"))
157: rms.removeRole("rootrole");
158:
159: } catch (SecurityException sex) {
160: System.out
161: .println("could not remove test users. exception caught: "
162: + sex);
163: }
164: }
165:
166: }
|