KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > jdbc > JDBCUserDatabaseTest


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.jdbc;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27
28 import java.util.Arrays JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.List JavaDoc;
31
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34
35 import com.sslexplorer.security.Role;
36 import com.sslexplorer.security.User;
37 import com.sslexplorer.security.UserNotFoundException;
38 import com.sslexplorer.testcontainer.AbstractTest;
39
40 /**
41  * Test the built in database.
42  */

43 public class JDBCUserDatabaseTest extends AbstractTest {
44
45     /**
46      * @throws Exception
47      */

48     @BeforeClass
49     public static void oneTimeSetUp() throws Exception JavaDoc {
50         setUp("");
51     }
52
53     /**
54      * Ensure that only the super user exists.
55      * @throws Exception
56      */

57     @Test
58     public void checkInitialState() throws Exception JavaDoc {
59         User[] listAllUsers = getDefaultUserDatabase().listAllUsers("*");
60         assertEquals("There should only be the one user and he is the super user.", listAllUsers.length, 1);
61     }
62
63     /**
64      * Create and delete a single user, ensuring that
65      * @throws Exception
66      */

67     @Test
68     public void simpleCreateUser() throws Exception JavaDoc {
69         User user = createAccount("jb", "qwqwqw", "james@3sp.com", "James Robinson");
70         assertEquals("There should only be the one user and he is the super user.", getDefaultUserDatabase().listAllUsers("*").length, 2);
71         deleteAccount(user);
72         assertEquals("There should only be the one user and he is the super user.", getDefaultUserDatabase().listAllUsers("*").length, 1);
73     }
74
75     /**
76      * Create a number of users
77      * @throws Exception
78      */

79     @Test
80     public void createNUsers() throws Exception JavaDoc {
81         User user1 = createAccount("jb1", "qwqwqw", "james@3sp.com", "James Robinson1");
82         User user2 = createAccount("jb2", "qwqwqw", "james@3sp.com", "James Robinson2");
83         User user3 = createAccount("jb3", "qwqwqw", "james@3sp.com", "James Robinson3");
84         assertEquals("There should only be the one user and he is the super user.", getDefaultUserDatabase().listAllUsers("*").length, 4);
85         deleteAccount(user1, user2, user3);
86         assertEquals("There should only be the one user and he is the super user.", getDefaultUserDatabase().listAllUsers("*").length, 1);
87     }
88
89     /**
90      * Login succesfull
91      * @throws Exception
92      */

93     @Test
94     public void loginSuccessfull() throws Exception JavaDoc {
95         String JavaDoc username = "jb";
96         String JavaDoc password = "qwqwqw";
97         User user = createAccount(username, password, "james@3sp.com", "James Robinson");
98         User adminUser = getDefaultUserDatabase().getAccount(USERNAME);
99         getDefaultUserDatabase().setPassword(user.getPrincipalName(), password, false, adminUser, PASSWORD);
100         User loggedONUser = getDefaultUserDatabase().logon(username, password);
101         assertNotNull("There should be a valid user.", loggedONUser);
102         deleteAccount(user);
103     }
104
105     /**
106      * Check the passowrd
107      * @throws Exception
108      */

109     @Test
110     public void checkPassword() throws Exception JavaDoc {
111         String JavaDoc username = "jb";
112         String JavaDoc password = "qwqwqw";
113         User user = createAccount(username, password, "james@3sp.com", "James Robinson");
114         User adminUser = getDefaultUserDatabase().getAccount(USERNAME);
115         getDefaultUserDatabase().setPassword(user.getPrincipalName(), password, false, adminUser, PASSWORD);
116         assertTrue("There password should be checked successfully.", getDefaultUserDatabase().checkPassword(username, password));
117         deleteAccount(user);
118     }
119
120     /**
121      * Login failed
122      * @throws Exception
123      */

124     @Test
125     public void loginFailed() throws Exception JavaDoc {
126         String JavaDoc username = "jb";
127         String JavaDoc password = "qwqwqw";
128         User user = createAccount(username, password, "james@3sp.com", "James Robinson");
129         User adminUser = getDefaultUserDatabase().getAccount(USERNAME);
130         getDefaultUserDatabase().setPassword(user.getPrincipalName(), password, false, adminUser, PASSWORD);
131         
132         try {
133             getDefaultUserDatabase().logon("wrong", password);
134             // should never be reached
135
fail();
136         } catch (Exception JavaDoc e) {
137             assertNotNull("An exception should have been thrown", e);
138         }
139         deleteAccount(user);
140     }
141
142     /**
143      * Check the passowrd, using a bad passowrd
144      * @throws Exception
145      */

146     @Test
147     public void checkBadPassword() throws Exception JavaDoc {
148         String JavaDoc username = "jb";
149         String JavaDoc password = "qwqwqw";
150         User user = createAccount(username, password, "james@3sp.com", "James Robinson");
151         User adminUser = getDefaultUserDatabase().getAccount(USERNAME);
152         getDefaultUserDatabase().setPassword(user.getPrincipalName(), password, false, adminUser, PASSWORD);
153         assertFalse("There password should be wrong.", getDefaultUserDatabase().checkPassword(username, "pileof"));
154         deleteAccount(user);
155     }
156
157     /**
158      * Change e-mail address
159      * @throws Exception
160      */

161     @Test
162     public void updateUserChangeEmail() throws Exception JavaDoc {
163         String JavaDoc email = "james@3sp.com";
164         String JavaDoc username = "jb";
165         User user = createAccount(username, "qwqwqw", email, "James Robinson");
166         assertTrue("The email should be set.", user.getEmail().equals(email));
167         String JavaDoc newEmail = "jb@3sp.com";
168         updateAccount(user, newEmail, user.getFullname(), user.getRoles());
169         user = getAccount(username);
170         assertTrue("The email should be set.", user.getEmail().equals(newEmail));
171         deleteAccount(user);
172     }
173
174     /**
175      * Can't change the username
176      * @throws Exception
177      */

178     @Test
179     public void cantChangeUsername() throws Exception JavaDoc {
180         String JavaDoc username = "jb";
181         User user = createAccount(username, "qwqwqw", "ja,es@3sp.com", "James Robninson");
182         assertTrue("The user name should be set.", user.getPrincipalName().equals(username));
183         String JavaDoc newUsername = "jimbob";
184         updateAccount(user, user.getFullname(), user.getEmail(), user.getRoles());
185         user = getAccount(username);
186         assertTrue("The user name should be set.", !user.getPrincipalName().equals(newUsername));
187         assertTrue("The user name should be set.", user.getPrincipalName().equals(username));
188         deleteAccount(user);
189     }
190
191     /**
192      * Change full name
193      * @throws Exception
194      */

195     @Test
196     public void updateUserChangeFullName() throws Exception JavaDoc {
197         String JavaDoc fullName = "James Robninson";
198         String JavaDoc username = "jb";
199         User user = createAccount(username, "qwqwqw", "ja,es@3sp.com", fullName);
200         assertTrue("The full name should be set.", user.getFullname().equals(fullName));
201         String JavaDoc newFullName = "James Douglas Robinson";
202         updateAccount(user, user.getFullname(), newFullName, user.getRoles());
203         user = getAccount(username);
204         assertTrue("The full name should be set.", user.getFullname().equals(newFullName));
205         deleteAccount(user);
206     }
207
208     /**
209      * Delete an unknown user and ensure an exception is thrown
210      * @throws Exception
211      */

212     @Test(expected = UserNotFoundException.class)
213     public void deleteUnknownUser() throws Exception JavaDoc {
214         String JavaDoc fullName = "James Robninson";
215         String JavaDoc username = "jb";
216         User user = createAccount(username, "qwqwqw", "ja,es@3sp.com", fullName);
217         user = getAccount(username);
218         deleteAccount(user, user);
219         fail("Delete user should have thrown an exception");
220     }
221     
222     /**
223      * Create a role
224      * @throws Exception
225      */

226     @Test
227     public void createNormalRole() throws Exception JavaDoc {
228         Role[] currentRoles = getDefaultUserDatabase().listAllRoles("*");
229         int currentNumberOfRoles = currentRoles.length;
230         String JavaDoc roleName = "jb";
231         Role role = createRole(roleName);
232         assertEquals("There should only be the one user and he is the super user.", getDefaultUserDatabase().listAllRoles("*").length, currentNumberOfRoles + 1);
233         deleteRole(role);
234         assertEquals("There should only be the one user and he is the super user.", getDefaultUserDatabase().listAllRoles("*").length, currentNumberOfRoles);
235     }
236     
237     /**
238      * Assign a user to a role and unassign, ensuring that the user is in the role.
239      * @throws Exception
240      */

241     @Test
242     public void assignRolesToUser() throws Exception JavaDoc{
243         String JavaDoc userName = "jb";
244         Role role = createRole("Group1");
245         User user = createAccount(userName, "qwqwqw", "james@3sp.com", "James Robninson");
246         user = updateAccountRoles(user, Collections.singleton(role));
247         assertEquals("The roles should be the same.", role.getPrincipalName(), user.getRoles()[0].getPrincipalName());
248         user = updateAccountRoles(user, Collections.<Role>emptyList());
249         User[] usersInRole = getUserService().getDefaultUserDatabase().getUsersInRole(role);
250         assertEquals("There should be the user in the list.", usersInRole.length, 0);
251         User retrievedUser2 = getUserService().getDefaultUserDatabase().getAccount(user.getPrincipalName());
252         List JavaDoc<Role> usersRoles2 = Arrays.asList(retrievedUser2.getRoles());
253         assertTrue("The role should not have any users.", usersRoles2.isEmpty());
254         deleteAccount(user);
255         deleteRole(role);
256     }
257     
258     /**
259      * @throws Exception
260      */

261     @Test
262     public void listingUsers() throws Exception JavaDoc {
263         User[] currentUsers = getDefaultUserDatabase().listAllUsers("*");
264         int currentNumberOfUsers = currentUsers.length;
265         assertEquals("There should only be the one user and he is the super user.", getDefaultUserDatabase().listAllUsers("*").length, 1);
266         // create 6 users
267
User user1 = createAccount("aaaa", "aaaa", "aaaa@3sp.com", "AAAA");
268         User user2 = createAccount("abbb", "abbb", "abbb@3sp.com", "ABBB");
269         User user3 = createAccount("aabb", "aabb", "aabb@3sp.com", "AABB");
270         User user4 = createAccount("aaab", "aaab", "aaab@3sp.com", "AAAB");
271         User user5 = createAccount("bbbb", "bbbb", "bbbb@3sp.com", "BBBB");
272         User user6 = createAccount("xaax", "xaax", "xaax@3sp.com", "XAAX");
273         assertEquals("There should be the seven users.", getDefaultUserDatabase().listAllUsers("*").length, currentNumberOfUsers + 6);
274         assertEquals("There should be the four users.", getDefaultUserDatabase().listAllUsers("a*").length, 4);
275         assertEquals("There should be the three users.", getDefaultUserDatabase().listAllUsers("aa*").length, 3);
276         assertEquals("There should be the two users.", getDefaultUserDatabase().listAllUsers("aaa*").length, 2);
277         assertEquals("There should be the three users.", getDefaultUserDatabase().listAllUsers("*ab*").length, 3);
278         assertEquals("There should be the zero users.", getDefaultUserDatabase().listAllUsers("*z*").length, 0);
279         assertEquals("There should be the one users.", getDefaultUserDatabase().listAllUsers("xa*").length, 1);
280         deleteAccount(user1, user2, user3, user4, user5, user6);
281         assertEquals("There should only be the one user and he is the super user.", getDefaultUserDatabase().listAllUsers("*").length, 1);
282     }
283
284     /**
285      * @throws Exception
286      */

287     @Test
288     public void listingRoles() throws Exception JavaDoc {
289         Role[] currentRoles = getDefaultUserDatabase().listAllRoles("*");
290         int currentNumberOfRoles = currentRoles.length;
291         assertEquals("There should only be the 1 role 'Users'.", getDefaultUserDatabase().listAllRoles("*").length, 1);
292         // create 6 roles
293
Role role1 = createRole("aaaa");
294         Role role2 = createRole("abbb");
295         Role role3 = createRole("aabb");
296         Role role4 = createRole("aaab");
297         Role role5 = createRole("bbbb");
298         Role role6 = createRole("xaax");
299         assertEquals("There should be the seven roles.", getDefaultUserDatabase().listAllRoles("*").length, currentNumberOfRoles + 6);
300         assertEquals("There should be the five roles.", getDefaultUserDatabase().listAllRoles("a*").length, 4);
301         assertEquals("There should be the three roles.", getDefaultUserDatabase().listAllRoles("aa*").length, 3);
302         assertEquals("There should be the two roles.", getDefaultUserDatabase().listAllRoles("aaa*").length, 2);
303         assertEquals("There should be the three roles.", getDefaultUserDatabase().listAllRoles("*ab*").length, 3);
304         assertEquals("There should be the zero roles.", getDefaultUserDatabase().listAllRoles("*z*").length, 0);
305         assertEquals("There should be the one roles.", getDefaultUserDatabase().listAllRoles("xa*").length, 1);
306         deleteRole(role1, role2, role3, role4, role5, role6);
307         assertEquals("There should only be the 1 role users.", getDefaultUserDatabase().listAllRoles("*").length, 1);
308     }
309 }
Popular Tags