1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
107 ch.setPassword("testcred","password","newpassword");
108
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
116 ch.setPassword("testcred","newpassword","anotherpassword");
117
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 }