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.GroupPrincipal;
022: import org.apache.jetspeed.security.impl.GroupPrincipalImpl;
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 TestLdapGroupSecurityHandler 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.seedGroupData(gpUid1);
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.removeGroupData(gpUid1);
044: }
045:
046: /**
047: * @throws Exception
048: */
049: public void testGetGroupPrincipal() throws Exception {
050: String fullPath = (new GroupPrincipalImpl(gpUid1))
051: .getFullPath();
052: //GroupPrincipal groupPrincipal = grHandler.getGroupPrincipal(fullPath);
053: GroupPrincipal groupPrincipal = grHandler
054: .getGroupPrincipal(gpUid1);
055: assertNotNull("Group was not found.", groupPrincipal);
056: assertEquals(gpUid1, groupPrincipal.getName());
057: assertEquals(fullPath, groupPrincipal.getFullPath());
058: }
059:
060: /**
061: * @throws Exception
062: */
063: public void testAddDuplicateGroupPrincipal() throws Exception {
064: grHandler.setGroupPrincipal(new GroupPrincipalImpl(gpUid1));
065: List groups = grHandler.getGroupPrincipals("");
066: assertEquals(1, groups.size());
067: }
068:
069: /**
070: * @throws Exception
071: */
072: public void testGetNonExistingGroupPrincipal() throws Exception {
073: GroupPrincipal group = grHandler.getGroupPrincipal(gpUid1
074: + "FAKE");
075: assertNull(group);
076: }
077:
078: /**
079: * @throws Exception
080: */
081: public void testRemoveExistantUserPrincipal() throws Exception {
082: GroupPrincipal gp = new GroupPrincipalImpl(gpUid1);
083: grHandler.removeGroupPrincipal(gp);
084: GroupPrincipal groupPrincipal = grHandler.getGroupPrincipal(gp
085: .getFullPath());
086: assertNull("Group was found and should have been removed.",
087: groupPrincipal);
088: List groups = grHandler.getGroupPrincipals("");
089: assertEquals(0, groups.size());
090: }
091:
092: /**
093: * @throws Exception
094: */
095: public void testRemoveNonExistantUserPrincipal() throws Exception {
096: String localUid = Integer.toString(rand.nextInt()).toString();
097: GroupPrincipal localPrin = new GroupPrincipalImpl(localUid);
098: grHandler.removeGroupPrincipal(localPrin);
099: List groups = grHandler.getGroupPrincipals("");
100: assertEquals(1, groups.size());
101: }
102:
103: /**
104: * @throws Exception
105: */
106: public void testGetGroupPrincipals() throws Exception {
107: try {
108: LdapDataHelper.seedGroupData(gpUid2);
109: assertTrue(
110: "getUserPrincipals should have returned more than one user.",
111: grHandler.getGroupPrincipals("*").size() > 1);
112:
113: String fullPath = (new GroupPrincipalImpl(gpUid1))
114: .getFullPath();
115: List groups = grHandler.getGroupPrincipals(fullPath);
116: assertTrue(
117: "getGroupPrincipals should have returned one group.",
118: groups.size() == 1);
119: assertTrue(
120: "List should have consisted of GroupPrincipal objects.",
121: groups.get(0) instanceof GroupPrincipal);
122:
123: String localUid = Integer.toString(rand.nextInt())
124: .toString();
125: assertTrue(
126: "getGroupPrincipals should not have found any groups with the specified filter.",
127: grHandler.getGroupPrincipals(
128: new GroupPrincipalImpl(localUid)
129: .getFullPath()).isEmpty());
130: } finally {
131: LdapDataHelper.removeGroupData(gpUid2);
132: }
133: }
134:
135: }
|