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;
018:
019: import java.security.Principal;
020: import java.util.Iterator;
021:
022: import org.apache.jetspeed.security.util.test.AbstractSecurityTestcase;
023:
024: import junit.framework.Test;
025: import junit.framework.TestSuite;
026:
027: /**
028: * <p>
029: * Unit testing for {@link UserSecurityHandler}.
030: * </p>
031: *
032: * @author <a href="mailto:dlestrat@apache.org">David Le Strat </a>
033: */
034: public class TestUserSecurityHandler extends AbstractSecurityTestcase {
035:
036: /**
037: * @see junit.framework.TestCase#setUp()
038: */
039: protected void setUp() throws Exception {
040: super .setUp();
041: destroyUsers();
042: initUsers();
043: }
044:
045: /**
046: * @see junit.framework.TestCase#tearDown()
047: */
048: public void tearDown() throws Exception {
049: destroyUsers();
050: super .tearDown();
051: }
052:
053: /**
054: * <p>
055: * Constructs the suite.
056: * </p>
057: *
058: * @return The {@Test}.
059: */
060: public static Test suite() {
061: return new TestSuite(TestUserSecurityHandler.class);
062: }
063:
064: /**
065: * <p>
066: * Test <code>getUserPrincipal</code>.
067: * </p>
068: */
069: public void testGetUserPrincipal() throws Exception {
070: Principal principal = ush.getUserPrincipal("testuser1");
071: assertNotNull(principal);
072: assertEquals("testuser1", principal.getName());
073: }
074:
075: /**
076: * <p>
077: * Test <code>getUserPrincipals</code>.
078: * </p>
079: */
080: public void testGetUserPrincipals() throws Exception {
081: Iterator principals = ush.getUserPrincipals("").iterator();
082: boolean foundUser1 = false;
083: boolean foundUser2 = false;
084:
085: while (principals.hasNext()) {
086: Principal principal = (Principal) principals.next();
087: assertNotNull(principal);
088:
089: if (principal.getName().equals("testuser1")) {
090: foundUser1 = true;
091: } else if (principal.getName().equals("testuser2")) {
092: foundUser2 = true;
093: }
094: }
095: assertTrue(foundUser1 && foundUser2);
096: }
097:
098: /**
099: * <p>
100: * Initialize user test object.
101: * </p>
102: */
103: protected void initUsers() throws Exception {
104: ums.addUser("testuser1", "password");
105: ums.addUser("testuser2", "password");
106: }
107:
108: /**
109: * <p>
110: * Destroy user test object.
111: * </p>
112: */
113: protected void destroyUsers() throws Exception {
114: ums.removeUser("testuser1");
115: ums.removeUser("testuser2");
116: }
117:
118: }
|