KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > users > filesystem > UserManagerTest


1 /*
2 Copyright (c) 2003 eInnovation Inc. All rights reserved
3
4 This library is free software; you can redistribute it and/or modify it under the terms
5 of the GNU Lesser General Public License as published by the Free Software Foundation;
6 either version 2.1 of the License, or (at your option) any later version.
7
8 This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the GNU Lesser General Public License for more details.
11 */

12
13 package com.openedit.users.filesystem;
14
15 import java.util.Collection JavaDoc;
16
17 import com.openedit.hittracker.HitTracker;
18 import com.openedit.users.BaseUserTest;
19 import com.openedit.users.Group;
20 import com.openedit.users.User;
21 import com.openedit.users.UserManager;
22
23
24 /**
25  * This is an abstract test for {@link UserManager} implementations. Concrete {@link UserManager}s
26  * should subclass this testcase and add their own tests.
27  *
28  * @author Eric Galluzzo
29  */

30 public class UserManagerTest extends BaseUserTest
31 {
32
33     public UserManagerTest(String JavaDoc inName)
34     {
35         super(inName);
36     }
37
38     /**
39      * DOCUMENT ME!
40      *
41      * @throws Exception
42      */

43     public void testAuthenticate() throws Exception JavaDoc
44     {
45         assertTrue(
46             "Should have authenticated user1",
47             fieldUserManager.authenticate(fieldUserManager.getUser("user1"), "user1pwd"));
48     }
49
50     /**
51      * DOCUMENT ME!
52      *
53      * @throws Exception
54      */

55     public void testAuthenticate_Negative() throws Exception JavaDoc
56     {
57         assertTrue(
58             "Should not have authenticated user1",
59             !fieldUserManager.authenticate(fieldUserManager.getUser("user1"), "thisiswrong"));
60     }
61
62     public void testLoadPermissions() throws Exception JavaDoc
63     {
64 // int size = fieldUserManager.getPermissionsManager().getAllPermissions().size();
65
// assertTrue( size > 0);
66
String JavaDoc v = "this is;: a test";
67         String JavaDoc[] two = v.split(";");
68         assertEquals( two.length , 2);
69
70         two = v.split(":");
71         assertEquals( two.length , 2);
72
73     }
74     
75     /**
76      * DOCUMENT ME!
77      *
78      * @throws Exception
79      */

80     public void testCreateUser() throws Exception JavaDoc
81     {
82         User newUser = fieldUserManager.createUser("newuser", "mypassword");
83         assertEquals("Username", "newuser", newUser.getUserName());
84         assertEquals("Number of groups", 0, newUser.getGroups().size());
85         assertEquals("Number of users", 4, fieldUserManager.getUsers().size());
86     }
87
88     /**
89      * DOCUMENT ME!
90      *
91      * @throws Exception
92      */

93     public void testDeleteGroup() throws Exception JavaDoc
94     {
95         Group group1 = fieldUserManager.getGroup("group1");
96         fieldUserManager.deleteGroup(group1);
97         assertEquals("Number of groups", 2, fieldUserManager.getGroups().size());
98         assertEquals(
99             "Number of groups of which user1 is a member", 1,
100             fieldUserManager.getUser("user1").getGroups().size());
101         assertEquals(
102             "Number of groups of which user2 is a member", 1,
103             fieldUserManager.getUser("user2").getGroups().size());
104         assertTrue(
105             "user1 should not be in group1",
106             !fieldUserManager.getUser("user1").getGroups().contains(group1));
107         assertTrue(
108             "user2 should not be in group1",
109             !fieldUserManager.getUser("user2").getGroups().contains(group1));
110     }
111
112     /**
113      * DOCUMENT ME!
114      *
115      * @throws Exception
116      */

117     public void testDeleteUser() throws Exception JavaDoc
118     {
119         User user1 = fieldUserManager.getUser("user1");
120         fieldUserManager.deleteUser(fieldUserManager.getUser("user1"));
121         assertEquals("Number of users", 2, fieldUserManager.getUsers().size());
122         assertEquals(
123             "Number of users in group1", 0, fieldUserManager.getUsersInGroup("group1").size());
124         assertEquals(
125             "Number of users in group2", 1, fieldUserManager.getUsersInGroup("group2").size());
126         assertTrue(
127             "group1 should not still contain user1",
128             !fieldUserManager.getUsersInGroup("group1").contains(user1));
129         assertTrue(
130             "group2 should not still contain user1",
131             !fieldUserManager.getUsersInGroup("group2").contains(user1));
132     }
133     
134
135     /**
136      * DOCUMENT ME!
137      *
138      * @throws Exception
139      */

140     public void testGetGroup_Group1() throws Exception JavaDoc
141     {
142         Group group1 = fieldUserManager.getGroup("group1");
143         assertEquals("Group name", "group1", group1.getName());
144         assertEquals("Number of users", 1, fieldUserManager.getUsersInGroup(group1).size());
145         assertTrue(
146             "group1 should contain user1",
147             fieldUserManager.getUsersInGroup("group1").contains(fieldUserManager.getUser("user1")));
148         assertTrue(
149             "group1 should contain user2",
150             fieldUserManager.getUsersInGroup("group2").contains(fieldUserManager.getUser("user2")));
151     }
152
153     /**
154      * DOCUMENT ME!
155      *
156      * @throws Exception
157      */

158     public void testGetGroup_Group2() throws Exception JavaDoc
159     {
160         Group group2 = fieldUserManager.getGroup("group2");
161         assertEquals("Group name", "group2", group2.getName());
162         assertTrue(
163             "group2 should contain user1",
164             fieldUserManager.getUsersInGroup("group2").contains(fieldUserManager.getUser("user1")));
165     }
166
167     /**
168      * DOCUMENT ME!
169      *
170      * @throws Exception
171      */

172     public void testGetGroup_Group3() throws Exception JavaDoc
173     {
174         Group group3 = fieldUserManager.getGroup("group3");
175         assertEquals("Group name", "group3", group3.getName());
176         assertEquals("Number of users", 1, fieldUserManager.getUsersInGroup(group3).size());
177     }
178
179     /**
180      * DOCUMENT ME!
181      *
182      * @throws Exception
183      */

184     public void testGetGroups() throws Exception JavaDoc
185     {
186         assertEquals("Number of groups", 3, fieldUserManager.getGroups().size());
187
188         for (int i = 1; i <= 3; i++)
189         {
190             assertTrue(
191                 "group" + i + " exists",
192                 fieldUserManager.getGroups().contains(fieldUserManager.getGroup("group" + i)));
193         }
194     }
195
196     /**
197      * DOCUMENT ME!
198      *
199      * @throws Exception
200      */

201     public void testGetUser_User1() throws Exception JavaDoc
202     {
203         User user1 = fieldUserManager.getUser("user1");
204         assertNotNull(user1);
205         assertEquals("Username", "user1", user1.getUserName());
206
207         Collection JavaDoc groups = user1.getGroups();
208         assertEquals("Number of groups", 2, groups.size());
209         assertTrue(
210             "user1 should be in group1", groups.contains(fieldUserManager.getGroup("group1")));
211         assertTrue(
212             "user1 should be in group2", groups.contains(fieldUserManager.getGroup("group2")));
213     }
214
215     /**
216      * DOCUMENT ME!
217      *
218      * @throws Exception
219      */

220     public void testGetUser_User2() throws Exception JavaDoc
221     {
222         User user2 = fieldUserManager.getUser("user2");
223         assertNotNull(user2);
224         assertEquals("Username", "user2", user2.getUserName());
225
226         Collection JavaDoc groups = user2.getGroups();
227         assertEquals("Number of groups", 1, groups.size());
228         assertTrue(
229             "user2 should be in group2", groups.contains(fieldUserManager.getGroup("group2")));
230     }
231
232     /**
233      * DOCUMENT ME!
234      *
235      * @throws Exception
236      */

237     public void testGetUser_User3() throws Exception JavaDoc
238     {
239         User user3 = fieldUserManager.getUser("user3");
240         assertNotNull(user3);
241         assertEquals("Username", "user3", user3.getUserName());
242         assertEquals("Number of groups", 1, user3.getGroups().size());
243     }
244
245     /**
246      * DOCUMENT ME!
247      *
248      * @throws Exception
249      */

250     public void testGetUsers() throws Exception JavaDoc
251     {
252         HitTracker users = fieldUserManager.getUsers();
253         assertEquals("Number of users", 3, users.size());
254
255         for (int i = 1; i <= 3; i++)
256         {
257             User found = fieldUserManager.getUser("user" + i);
258             assertTrue(
259                 "user" + i + " exists", users.contains(found.getUserName()));
260         }
261     }
262
263     /*
264      * @see TestCase#setUp()
265      */

266     protected void setUp() throws Exception JavaDoc
267     {
268             fieldUserManager = createUserManager();
269     
270             // Add the following users and groups:
271
//
272
// user1 => group1, group2
273
// user2 => group1
274
// user3 => (no groups)
275
//
276
// group1 => user1, user2
277
// group2 => user1
278
// group3 => (no users)
279
Group group1 = fieldUserManager.createGroup("group1");
280             Group group2 = fieldUserManager.createGroup("group2");
281             Group group3 = fieldUserManager.createGroup("group3");
282             User user1 = fieldUserManager.createUser("user1", "user1pwd");
283             User user2 = fieldUserManager.createUser("user2", "user2pwd");
284             User user3 = fieldUserManager.createUser("user3", "user3pwd");
285     
286             user1.addGroup(group1);
287             user1.addGroup(group2);
288             
289             user2.addGroup(group2);
290             user3.addGroup(group3);
291     }
292     protected void tearDown() throws Exception JavaDoc
293     {
294         deleteUserManager(fieldUserManager);
295     }
296 }
297
Popular Tags