1 17 18 19 20 package org.apache.lenya.ac.file; 21 22 import java.io.File ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.Map ; 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 39 public class FileUserTest extends AccessControlTest { 40 private HashMap groups = new HashMap (); 41 42 46 public FileUserTest(String arg0) { 47 super(arg0); 48 } 49 50 55 public static void main(String [] args) { 56 PublicationHelper.extractPublicationArguments(args); 57 junit.textui.TestRunner.run(FileUserTest.class); 58 } 59 60 64 protected void setUp() throws Exception { 65 super.setUp(); 66 } 67 68 73 final public Map getGroups() { 74 return groups; 75 } 76 77 89 final public FileUser createAndSaveUser(String userName, String fullName, String email, 90 String password) throws AccessControlException { 91 File configDir = getAccreditablesDirectory(); 92 93 String editorGroupName = "editorGroup"; 94 String 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 126 final public FileUser loadUser(String userName) throws AccessControlException { 127 File configDir = getAccreditablesDirectory(); 128 UserType[] userTypes = { FileAccreditableManager.getDefaultUserType() }; 129 FileUserManager manager = FileUserManager.instance(configDir, userTypes); 130 131 return (FileUser) manager.getUser(userName); 132 } 133 134 139 final public void testSave() throws AccessControlException { 140 String userName = "alice"; 141 createAndSaveUser(userName, "Alice Wonderland", "alice@wonderland.org", "secret"); 142 143 File configDir = getAccreditablesDirectory(); 144 File xmlFile = new File (configDir, userName + ".iml"); 145 assertTrue(xmlFile.exists()); 146 } 147 148 153 final public void testGetEmail() throws AccessControlException { 154 String userID = "alice"; 155 String 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 167 final public void testGetFullName() throws AccessControlException { 168 String userID = "alice"; 169 String 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 181 final public void testGetGroups() throws AccessControlException { 182 FileUser user = createAndSaveUser("alice", "Alice Wonderland", "alice@wonderland.org", 183 "secret"); 184 185 for (Iterator i = getGroups().values().iterator(); i.hasNext(); ) { 186 Group group = (Group) i.next(); 187 assertTrue(group.contains(user)); 188 } 189 } 190 191 196 final public void testGetId() throws AccessControlException { 197 String id = "alice"; 198 FileUser user = createAndSaveUser(id, "Alice Wonderland", "alice@wonderland.org", "secret"); 199 assertTrue(user.getId().equals(id)); 200 } 201 202 207 final public void testDelete() throws AccessControlException { 208 String id = "albert"; 209 FileUser user = createAndSaveUser(id, "Albert Einstein", "albert@physics.org", "secret"); 210 File 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 226 final public void testAuthenticate() throws AccessControlException { 227 String password = "daisy"; 228 FileUser user = createAndSaveUser("mickey", "Mickey Mouse", "mickey@mouse.com", password); 229 assertTrue(user.authenticate(password)); 230 231 File 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 |