1   /* 
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.security.spi;
18  
19  import java.util.Set;
20  
21  import org.apache.jetspeed.security.PasswordCredential;
22  import org.apache.jetspeed.security.util.test.AbstractSecurityTestcase;
23  
24  import junit.framework.Test;
25  import junit.framework.TestSuite;
26  
27  /***
28   * <p>
29   * Unit testing for {@link UserManager}.
30   * </p>
31   * 
32   * @author <a href="mailto:dlestrat@apache.org">David Le Strat </a>
33   */
34  public class TestCredentialHandler extends AbstractSecurityTestcase
35  {
36  
37      /***
38       * @see junit.framework.TestCase#setUp()
39       */
40      protected void setUp() throws Exception
41      {
42          super.setUp(); 
43          // cleanup for previously failed test
44          destroyUser();
45      }
46  
47      /***
48       * @see junit.framework.TestCase#tearDown()
49       */
50      public void tearDown() throws Exception
51      {
52          super.tearDown();
53      }
54  
55      /***
56       * <p>
57       * Constructs the suite.
58       * </p>
59       * 
60       * @return The {@Test}.
61       */
62      public static Test suite()
63      {
64          return new TestSuite(TestCredentialHandler.class);
65      }
66  
67      /***
68       * <p>
69       * Test <code>getPrivateCredentials</code>..
70       * </p>
71       */
72      public void testGetPrivateCredentials() throws Exception
73      {
74          initUser();
75          Set privateCredentials = ch.getPrivateCredentials("testcred");
76          assertNotNull(privateCredentials);
77          assertEquals(1, privateCredentials.size());
78          PasswordCredential[] pwdCreds = (PasswordCredential[]) privateCredentials.toArray(new PasswordCredential[1]);
79          assertEquals("testcred", pwdCreds[0].getUserName());
80          assertNotNull(new String(pwdCreds[0].getPassword()));
81          destroyUser();
82      }
83      
84      /***
85       * <p>
86       * Test <code>getPublicCredentials</code>..
87       * </p>
88       */
89      public void testGetPublicCredentials() throws Exception
90      {
91          initUser();
92          Set publicCredentials = ch.getPublicCredentials("testcred");
93          assertNotNull(publicCredentials);
94          assertEquals(0, publicCredentials.size());
95          destroyUser();
96      }
97  
98      /***
99       * <p>
100      * Test <code>setPassword</code>..
101      * </p>
102      */
103     public void testSetPassword() throws Exception
104     {
105         initUser();
106         // Replace existing password credential.
107         ch.setPassword("testcred","password","newpassword");
108         // Test that the credential was properly set.
109         Set privateCredentials = ch.getPrivateCredentials("testcred");
110         assertNotNull(privateCredentials);
111         assertEquals(1, privateCredentials.size());
112         PasswordCredential[] pwdCreds = (PasswordCredential[]) privateCredentials.toArray(new PasswordCredential[0]);
113         assertEquals("testcred", pwdCreds[0].getUserName());
114         assertNotNull(new String(pwdCreds[0].getPassword()));
115         // Add password credential.
116         ch.setPassword("testcred","newpassword","anotherpassword");
117         // Test that the credential was properly set.
118         privateCredentials = ch.getPrivateCredentials("testcred");
119         assertNotNull(privateCredentials);
120         assertEquals(1, privateCredentials.size());
121         destroyUser();
122     }
123     
124     /***
125      * <p>
126      * Initialize user test object.
127      * </p>
128      */
129     protected void initUser() throws Exception
130     {
131         ums.addUser("testcred", "password");
132     }
133 
134     /***
135      * <p>
136      * Destroy user test object.
137      * </p>
138      */
139     protected void destroyUser() throws Exception
140     {
141         ums.removeUser("testcred");
142     }
143 }