KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > ac > file > FileUserTest


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: FileUserTest.java 43670 2004-09-10 14:29:22Z andreas $ */
19
20 package org.apache.lenya.ac.file;
21
22 import java.io.File JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import org.apache.lenya.ac.AccessControlException;
28 import org.apache.lenya.ac.Group;
29 import org.apache.lenya.ac.User;
30 import org.apache.lenya.ac.UserType;
31 import org.apache.lenya.ac.impl.AccessControlTest;
32 import org.apache.lenya.cms.PublicationHelper;
33
34 /**
35  * File user test.
36  *
37  * @version $Id: FileUserTest.java 43670 2004-09-10 14:29:22Z andreas $
38  */

39 public class FileUserTest extends AccessControlTest {
40     private HashMap JavaDoc groups = new HashMap JavaDoc();
41
42     /**
43      * Constructor for FileUserTest.
44      * @param arg0 command line args
45      */

46     public FileUserTest(String JavaDoc arg0) {
47         super(arg0);
48     }
49
50     /**
51      * DOCUMENT ME!
52      *
53      * @param args DOCUMENT ME!
54      */

55     public static void main(String JavaDoc[] args) {
56         PublicationHelper.extractPublicationArguments(args);
57         junit.textui.TestRunner.run(FileUserTest.class);
58     }
59
60     /**
61      * (non-Javadoc)
62      * @see junit.framework.TestCase#setUp()
63      */

64     protected void setUp() throws Exception JavaDoc {
65         super.setUp();
66     }
67
68     /**
69      * DOCUMENT ME!
70      *
71      * @return DOCUMENT ME!
72      */

73     final public Map JavaDoc getGroups() {
74         return groups;
75     }
76
77     /**
78      * Create and save a user
79      *
80      * @param userName DOCUMENT ME!
81      * @param fullName DOCUMENT ME!
82      * @param email DOCUMENT ME!
83      * @param password DOCUMENT ME!
84      *
85      * @return a <code>FileUser</code>
86      *
87      * @throws AccessControlException if an error occurs
88      */

89     final public FileUser createAndSaveUser(String JavaDoc userName, String JavaDoc fullName, String JavaDoc email,
90         String JavaDoc password) throws AccessControlException {
91         File JavaDoc configDir = getAccreditablesDirectory();
92
93         String JavaDoc editorGroupName = "editorGroup";
94         String JavaDoc adminGroupName = "adminGroup";
95
96         FileGroup editorGroup = new FileGroup(configDir, editorGroupName);
97         FileGroup adminGroup = new FileGroup(configDir, adminGroupName);
98         this.groups.put(editorGroupName, editorGroup);
99         this.groups.put(adminGroupName, adminGroup);
100
101         FileUser user = new FileUser(configDir, userName, fullName, email, password);
102
103         editorGroup.add(user);
104         adminGroup.add(user);
105
106         editorGroup.save();
107         adminGroup.save();
108         user.save();
109         
110         UserType[] userTypes = { FileAccreditableManager.getDefaultUserType() };
111         FileUserManager manager = FileUserManager.instance(configDir, userTypes);
112         manager.add(user);
113
114         return user;
115     }
116
117     /**
118      * Load a user.
119      *
120      * @param userName the name of the user
121      *
122      * @return a <code>FileUser</code>
123      *
124      * @throws AccessControlException if an error occurs
125      */

126     final public FileUser loadUser(String JavaDoc userName) throws AccessControlException {
127         File JavaDoc configDir = getAccreditablesDirectory();
128         UserType[] userTypes = { FileAccreditableManager.getDefaultUserType() };
129         FileUserManager manager = FileUserManager.instance(configDir, userTypes);
130
131         return (FileUser) manager.getUser(userName);
132     }
133
134     /**
135      * Test save
136      *
137      * @throws AccessControlException if an error occurs
138      */

139     final public void testSave() throws AccessControlException {
140         String JavaDoc userName = "alice";
141         createAndSaveUser(userName, "Alice Wonderland", "alice@wonderland.org", "secret");
142
143         File JavaDoc configDir = getAccreditablesDirectory();
144         File JavaDoc xmlFile = new File JavaDoc(configDir, userName + ".iml");
145         assertTrue(xmlFile.exists());
146     }
147
148     /**
149      * Test getEmail
150      *
151      * @throws AccessControlException if an error occurs
152      */

153     final public void testGetEmail() throws AccessControlException {
154         String JavaDoc userID = "alice";
155         String JavaDoc email = "alice@wonderland.org";
156         User user = createAndSaveUser(userID, "Alice Wonderland", email, "secret");
157         assertTrue(user.getEmail().equals(email));
158         user = loadUser(userID);
159         assertTrue(user.getEmail().equals(email));
160     }
161
162     /**
163      * Test getFullName
164      *
165      * @throws AccessControlException if an error occurs
166      */

167     final public void testGetFullName() throws AccessControlException {
168         String JavaDoc userID = "alice";
169         String JavaDoc userName = "Alice Wonderland";
170         FileUser user = createAndSaveUser(userID, userName, "alice@wonderland.org", "secret");
171         assertTrue(user.getName().equals(userName));
172         user = loadUser(userID);
173         assertTrue(user.getName().equals(userName));
174     }
175
176     /**
177      * Test getGroups
178      *
179      * @throws AccessControlException if an error occurs
180      */

181     final public void testGetGroups() throws AccessControlException {
182         FileUser user = createAndSaveUser("alice", "Alice Wonderland", "alice@wonderland.org",
183                 "secret");
184                 
185         for (Iterator JavaDoc i = getGroups().values().iterator(); i.hasNext(); ) {
186             Group group = (Group) i.next();
187             assertTrue(group.contains(user));
188         }
189     }
190
191     /**
192      * Test getId
193      *
194      * @throws AccessControlException if an error occurs
195      */

196     final public void testGetId() throws AccessControlException {
197         String JavaDoc id = "alice";
198         FileUser user = createAndSaveUser(id, "Alice Wonderland", "alice@wonderland.org", "secret");
199         assertTrue(user.getId().equals(id));
200     }
201
202     /**
203      * Test delete
204      *
205      * @throws AccessControlException if an error occurs
206      */

207     final public void testDelete() throws AccessControlException {
208         String JavaDoc id = "albert";
209         FileUser user = createAndSaveUser(id, "Albert Einstein", "albert@physics.org", "secret");
210         File JavaDoc configDir = getAccreditablesDirectory();
211         UserType[] userTypes = { FileAccreditableManager.getDefaultUserType() };
212         FileUserManager manager = FileUserManager.instance(configDir, userTypes);
213         assertNotNull(manager);
214
215         assertNotNull(manager.getUser(id));
216         user.delete();
217         manager.remove(user);
218         assertNull(manager.getUser(id));
219     }
220
221     /**
222      * Test authenticate
223      *
224      * @throws AccessControlException if an error occurs
225      */

226     final public void testAuthenticate() throws AccessControlException {
227         String JavaDoc password = "daisy";
228         FileUser user = createAndSaveUser("mickey", "Mickey Mouse", "mickey@mouse.com", password);
229         assertTrue(user.authenticate(password));
230
231         File JavaDoc configDir = getAccreditablesDirectory();
232         UserType[] userTypes = { FileAccreditableManager.getDefaultUserType() };
233         FileUserManager manager = FileUserManager.instance(configDir, userTypes);
234         assertNotNull(manager);
235
236         User lenya = manager.getUser("lenya");
237         assertNotNull(lenya);
238         assertTrue(lenya.authenticate("levi"));
239     }
240 }
241
Popular Tags