001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: /* $Id: FileUserTest.java 485769 2006-12-11 17:41:23Z andreas $ */
020:
021: package org.apache.lenya.ac.file;
022:
023: import java.io.File;
024: import java.util.HashMap;
025: import java.util.Iterator;
026: import java.util.Map;
027:
028: import org.apache.avalon.framework.container.ContainerUtil;
029: import org.apache.lenya.ac.AccessControlException;
030: import org.apache.lenya.ac.Group;
031: import org.apache.lenya.ac.User;
032: import org.apache.lenya.ac.UserType;
033: import org.apache.lenya.ac.impl.AbstractAccessControlTest;
034:
035: /**
036: * File user test.
037: *
038: * @version $Id: FileUserTest.java 485769 2006-12-11 17:41:23Z andreas $
039: */
040: public class FileUserTest extends AbstractAccessControlTest {
041: private HashMap groups = new HashMap();
042:
043: /**
044: * (non-Javadoc)
045: * @see junit.framework.TestCase#setUp()
046: */
047: public void setUp() throws Exception {
048: super .setUp();
049: }
050:
051: /**
052: * Get all Groups
053: *
054: * @return A map of the groups
055: */
056: final public Map getGroups() {
057: return this .groups;
058: }
059:
060: /**
061: * Create and save a user
062: *
063: * @param userName The user name
064: * @param fullName The full name
065: * @param email The email
066: * @param password The password
067: *
068: * @return a <code>FileUser</code>
069: *
070: * @throws AccessControlException if an error occurs
071: */
072: final public FileUser createAndSaveUser(String userName,
073: String fullName, String email, String password)
074: throws AccessControlException {
075:
076: String editorGroupName = "editorGroup";
077: String adminGroupName = "adminGroup";
078:
079: FileGroup editorGroup = new FileGroup(getAccreditableManager()
080: .getGroupManager(), getLogger(), editorGroupName);
081: ContainerUtil.enableLogging(editorGroup, getLogger());
082: FileGroup adminGroup = new FileGroup(getAccreditableManager()
083: .getGroupManager(), getLogger(), adminGroupName);
084: ContainerUtil.enableLogging(adminGroup, getLogger());
085: this .groups.put(editorGroupName, editorGroup);
086: this .groups.put(adminGroupName, adminGroup);
087:
088: FileUser user = new FileUser(getAccreditableManager()
089: .getUserManager(), getLogger(), userName, fullName,
090: email, password);
091: ContainerUtil.enableLogging(user, getLogger());
092:
093: editorGroup.add(user);
094: adminGroup.add(user);
095:
096: editorGroup.save();
097: adminGroup.save();
098: user.save();
099:
100: FileUserManager _manager = getUserManager();
101: _manager.add(user);
102:
103: return user;
104: }
105:
106: /**
107: * Returns the file user manager.
108: * @return A file user manager.
109: * @throws AccessControlException if an error occurs.
110: */
111: protected FileUserManager getUserManager()
112: throws AccessControlException {
113: UserType[] userTypes = { FileAccreditableManager
114: .getDefaultUserType() };
115: FileUserManager _manager = FileUserManager.instance(
116: getAccreditableManager(), getAccreditablesDirectory(),
117: userTypes, getLogger());
118: return _manager;
119: }
120:
121: /**
122: * Load a user.
123: *
124: * @param userName the name of the user
125: *
126: * @return a <code>FileUser</code>
127: *
128: * @throws AccessControlException if an error occurs
129: */
130: final public FileUser loadUser(String userName)
131: throws AccessControlException {
132: FileUserManager _manager = getUserManager();
133: return (FileUser) _manager.getUser(userName);
134: }
135:
136: /**
137: * Test save
138: *
139: * @throws AccessControlException if an error occurs
140: */
141: final public void testSave() throws AccessControlException {
142: String userName = "aliceTest";
143: createAndSaveUser(userName, "Alice Wonderland",
144: "alice@wonderland.org", "secret");
145:
146: File configDir = getAccreditablesDirectory();
147: File xmlFile = new File(configDir, userName + ".iml");
148: assertTrue(xmlFile.exists());
149: }
150:
151: /**
152: * Test getEmail
153: *
154: * @throws AccessControlException if an error occurs
155: */
156: final public void testGetEmail() throws AccessControlException {
157: String userID = "aliceTest";
158: String email = "alice@wonderland.org";
159: User user = createAndSaveUser(userID, "Alice Wonderland",
160: email, "secret");
161: assertTrue(user.getEmail().equals(email));
162: user = loadUser(userID);
163: assertTrue(user.getEmail().equals(email));
164: }
165:
166: /**
167: * Test getName
168: *
169: * @throws AccessControlException if an error occurs
170: */
171: final public void testGetName() throws AccessControlException {
172: String userID = "aliceTest";
173: String userName = "Alice Wonderland";
174: FileUser user = createAndSaveUser(userID, userName,
175: "alice@wonderland.org", "secret");
176: assertTrue(user.getName().equals(userName));
177: user = loadUser(userID);
178: assertTrue(user.getName().equals(userName));
179: }
180:
181: /**
182: * Test getGroups
183: *
184: * @throws AccessControlException if an error occurs
185: */
186: final public void testGetGroups() throws AccessControlException {
187: FileUser user = createAndSaveUser("aliceTest",
188: "Alice Wonderland", "alice@wonderland.org", "secret");
189:
190: for (Iterator i = getGroups().values().iterator(); i.hasNext();) {
191: Group group = (Group) i.next();
192: assertTrue(group.contains(user));
193: }
194: }
195:
196: /**
197: * Test getId
198: *
199: * @throws AccessControlException if an error occurs
200: */
201: final public void testGetId() throws AccessControlException {
202: String id = "aliceTest";
203: FileUser user = createAndSaveUser(id, "Alice Wonderland",
204: "alice@wonderland.org", "secret");
205: assertTrue(user.getId().equals(id));
206: }
207:
208: /**
209: * Test delete
210: *
211: * @throws AccessControlException if an error occurs
212: */
213: final public void testDelete() throws AccessControlException {
214: String id = "albert";
215: FileUser user = createAndSaveUser(id, "Albert Einstein",
216: "albert@physics.org", "secret");
217: FileUserManager _manager = getUserManager();
218: assertNotNull(_manager);
219:
220: assertNotNull(_manager.getUser(id));
221: user.delete();
222: _manager.remove(user);
223: assertNull(_manager.getUser(id));
224: }
225:
226: /**
227: * Test authenticate
228: *
229: * @throws AccessControlException if an error occurs
230: */
231: final public void testAuthenticate() throws AccessControlException {
232: String password = "daisy";
233: FileUser user = createAndSaveUser("mickey", "Mickey Mouse",
234: "mickey@mouse.com", password);
235: assertTrue(user.authenticate(password));
236:
237: FileUserManager _manager = getUserManager();
238: assertNotNull(_manager);
239:
240: User lenya = _manager.getUser("lenya");
241: assertNotNull(lenya);
242: assertTrue(lenya.authenticate("levi"));
243: }
244: }
|