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.util.Set;
020:
021: import org.apache.jetspeed.security.PasswordCredential;
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 UserManager}.
030: * </p>
031: *
032: * @author <a href="mailto:dlestrat@apache.org">David Le Strat </a>
033: */
034: public class TestCredentialHandler extends AbstractSecurityTestcase {
035:
036: /**
037: * @see junit.framework.TestCase#setUp()
038: */
039: protected void setUp() throws Exception {
040: super .setUp();
041: // cleanup for previously failed test
042: destroyUser();
043: }
044:
045: /**
046: * @see junit.framework.TestCase#tearDown()
047: */
048: public void tearDown() throws Exception {
049: super .tearDown();
050: }
051:
052: /**
053: * <p>
054: * Constructs the suite.
055: * </p>
056: *
057: * @return The {@Test}.
058: */
059: public static Test suite() {
060: return new TestSuite(TestCredentialHandler.class);
061: }
062:
063: /**
064: * <p>
065: * Test <code>getPrivateCredentials</code>..
066: * </p>
067: */
068: public void testGetPrivateCredentials() throws Exception {
069: initUser();
070: Set privateCredentials = ch.getPrivateCredentials("testcred");
071: assertNotNull(privateCredentials);
072: assertEquals(1, privateCredentials.size());
073: PasswordCredential[] pwdCreds = (PasswordCredential[]) privateCredentials
074: .toArray(new PasswordCredential[1]);
075: assertEquals("testcred", pwdCreds[0].getUserName());
076: assertNotNull(new String(pwdCreds[0].getPassword()));
077: destroyUser();
078: }
079:
080: /**
081: * <p>
082: * Test <code>getPublicCredentials</code>..
083: * </p>
084: */
085: public void testGetPublicCredentials() throws Exception {
086: initUser();
087: Set publicCredentials = ch.getPublicCredentials("testcred");
088: assertNotNull(publicCredentials);
089: assertEquals(0, publicCredentials.size());
090: destroyUser();
091: }
092:
093: /**
094: * <p>
095: * Test <code>setPassword</code>..
096: * </p>
097: */
098: public void testSetPassword() throws Exception {
099: initUser();
100: // Replace existing password credential.
101: ch.setPassword("testcred", "password", "newpassword");
102: // Test that the credential was properly set.
103: Set privateCredentials = ch.getPrivateCredentials("testcred");
104: assertNotNull(privateCredentials);
105: assertEquals(1, privateCredentials.size());
106: PasswordCredential[] pwdCreds = (PasswordCredential[]) privateCredentials
107: .toArray(new PasswordCredential[0]);
108: assertEquals("testcred", pwdCreds[0].getUserName());
109: assertNotNull(new String(pwdCreds[0].getPassword()));
110: // Add password credential.
111: ch.setPassword("testcred", "newpassword", "anotherpassword");
112: // Test that the credential was properly set.
113: privateCredentials = ch.getPrivateCredentials("testcred");
114: assertNotNull(privateCredentials);
115: assertEquals(1, privateCredentials.size());
116: destroyUser();
117: }
118:
119: /**
120: * <p>
121: * Initialize user test object.
122: * </p>
123: */
124: protected void initUser() throws Exception {
125: ums.addUser("testcred", "password");
126: }
127:
128: /**
129: * <p>
130: * Destroy user test object.
131: * </p>
132: */
133: protected void destroyUser() throws Exception {
134: ums.removeUser("testcred");
135: }
136: }
|