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.spi.ldap;
018:
019: import java.util.List;
020:
021: import org.apache.jetspeed.security.RolePrincipal;
022: import org.apache.jetspeed.security.impl.RolePrincipalImpl;
023:
024: /**
025: * @author <a href="mailto:mike.long@dataline.com">Mike Long </a>, <a
026: * href="mailto:dlestrat@apache.org">David Le Strat</a>
027: */
028: public class TestLdapRoleSecurityHandler extends AbstractLdapTest {
029:
030: /**
031: * @see org.apache.jetspeed.security.spi.ldap.AbstractLdapTest#setUp()
032: */
033: protected void setUp() throws Exception {
034: super .setUp();
035: LdapDataHelper.seedRoleData(roleUid1);
036: }
037:
038: /**
039: * @see org.apache.jetspeed.security.spi.ldap.AbstractLdapTest#tearDown()
040: */
041: protected void tearDown() throws Exception {
042: super .tearDown();
043: LdapDataHelper.removeRoleData(roleUid1);
044: }
045:
046: /**
047: * @throws Exception
048: */
049: public void testGetRolePrincipal() throws Exception {
050: String fullPath = (new RolePrincipalImpl(roleUid1))
051: .getFullPath();
052: RolePrincipal rolePrincipal = roleHandler
053: .getRolePrincipal(roleUid1);
054: assertNotNull("Role was not found.", rolePrincipal);
055: assertEquals(roleUid1, rolePrincipal.getName());
056: assertEquals(fullPath, rolePrincipal.getFullPath());
057: }
058:
059: /**
060: * @throws Exception
061: */
062: public void testAddDuplicateRolePrincipal() throws Exception {
063: roleHandler.setRolePrincipal(new RolePrincipalImpl(roleUid1));
064: List roles = roleHandler.getRolePrincipals("");
065: assertEquals(1, roles.size());
066: }
067:
068: /**
069: * @throws Exception
070: */
071: public void testGetNonExistingRolePrincipal() throws Exception {
072: RolePrincipal role = roleHandler.getRolePrincipal(roleUid1
073: + "FAKE");
074: assertNull(role);
075: }
076:
077: /**
078: * @throws Exception
079: */
080: public void testRemoveExistantUserPrincipal() throws Exception {
081: RolePrincipal gp = new RolePrincipalImpl(roleUid1);
082: roleHandler.removeRolePrincipal(gp);
083: RolePrincipal rolePrincipal = roleHandler.getRolePrincipal(gp
084: .getFullPath());
085: assertNull("Role was found and should have been removed.",
086: rolePrincipal);
087: List roles = roleHandler.getRolePrincipals("");
088: assertEquals(0, roles.size());
089: }
090:
091: /**
092: * @throws Exception
093: */
094: public void testRemoveNonExistantUserPrincipal() throws Exception {
095: String localUid = Integer.toString(rand.nextInt()).toString();
096: RolePrincipal localPrin = new RolePrincipalImpl(localUid);
097: roleHandler.removeRolePrincipal(localPrin);
098: List roles = roleHandler.getRolePrincipals("");
099: assertEquals(1, roles.size());
100: }
101:
102: /**
103: * @throws Exception
104: */
105: public void testGetRolePrincipals() throws Exception {
106: try {
107: LdapDataHelper.seedRoleData(gpUid2);
108: assertTrue(
109: "getUserPrincipals should have returned more than one user.",
110: roleHandler.getRolePrincipals("*").size() > 1);
111:
112: String fullPath = (new RolePrincipalImpl(roleUid1))
113: .getFullPath();
114: List roles = roleHandler.getRolePrincipals(fullPath);
115: assertTrue(
116: "getRolePrincipals should have returned one role.",
117: roles.size() == 1);
118: assertTrue(
119: "List should have consisted of RolePrincipal objects.",
120: roles.get(0) instanceof RolePrincipal);
121:
122: String localUid = Integer.toString(rand.nextInt())
123: .toString();
124: assertTrue(
125: "getRolePrincipals should not have found any roles with the specified filter.",
126: roleHandler.getRolePrincipals(
127: new RolePrincipalImpl(localUid)
128: .getFullPath()).isEmpty());
129: } finally {
130: LdapDataHelper.removeRoleData(gpUid2);
131: }
132: }
133:
134: }
|