1 18 19 package org.apache.roller.business; 20 21 import java.util.ArrayList ; 22 import java.util.Collection ; 23 import java.util.List ; 24 import junit.framework.Test; 25 import junit.framework.TestCase; 26 import junit.framework.TestSuite; 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.apache.roller.TestUtils; 30 import org.apache.roller.model.RollerFactory; 31 import org.apache.roller.model.UserManager; 32 import org.apache.roller.pojos.UserData; 33 34 35 38 public class UserTest extends TestCase { 39 40 public static Log log = LogFactory.getLog(UserTest.class); 41 42 43 public UserTest(String name) { 44 super(name); 45 } 46 47 48 public static Test suite() { 49 return new TestSuite(UserTest.class); 50 } 51 52 53 public void setUp() throws Exception { 54 } 55 56 public void tearDown() throws Exception { 57 } 58 59 60 63 public void testUserCRUD() throws Exception { 64 65 UserManager mgr = RollerFactory.getRoller().getUserManager(); 66 UserData user = null; 67 68 UserData testUser = new UserData(); 69 testUser.setUserName("testUser"); 70 testUser.setPassword("password"); 71 testUser.setFullName("Test User"); 72 testUser.setEmailAddress("TestUser@dev.null"); 73 testUser.setLocale("en_US"); 74 testUser.setTimeZone("America/Los_Angeles"); 75 testUser.setDateCreated(new java.util.Date ()); 76 testUser.setEnabled(Boolean.TRUE); 77 78 user = mgr.getUserByUserName(testUser.getUserName()); 80 assertNull(user); 81 82 mgr.addUser(testUser); 84 String id = testUser.getId(); 85 TestUtils.endSession(true); 86 87 user = null; 89 user = mgr.getUser(id); 90 assertNotNull(user); 91 assertEquals(testUser, user); 92 93 user.setFullName("testtesttest"); 95 mgr.saveUser(user); 96 TestUtils.endSession(true); 97 98 user = null; 100 user = mgr.getUser(id); 101 assertNotNull(user); 102 assertEquals("testtesttest", user.getFullName()); 103 104 mgr.removeUser(user); 106 TestUtils.endSession(true); 107 108 user = null; 110 user = mgr.getUser(id); 111 assertNull(user); 112 } 113 114 115 118 public void testUserLookups() throws Exception { 119 120 UserManager mgr = RollerFactory.getRoller().getUserManager(); 121 UserData user = null; 122 123 UserData testUser = TestUtils.setupUser("userTestUser"); 125 TestUtils.endSession(true); 126 127 user = mgr.getUserByUserName(testUser.getUserName()); 129 assertNotNull(user); 130 assertEquals(testUser.getUserName(), user.getUserName()); 131 132 String id = user.getId(); 134 user = null; 135 user = mgr.getUser(id); 136 assertNotNull(user); 137 assertEquals(testUser.getUserName(), user.getUserName()); 138 139 user = null; 141 List users1 = mgr.getUsersStartingWith(testUser.getUserName().substring(0, 3), Boolean.TRUE, 0, 1); 142 assertEquals(1, users1.size()); 143 user = (UserData) users1.get(0); 144 assertNotNull(user); 145 assertEquals(testUser.getUserName(), user.getUserName()); 146 147 user = null; 149 List users2 = mgr.getUsersStartingWith(testUser.getEmailAddress().substring(0, 3), Boolean.TRUE, 0, 1); 150 assertEquals(1, users2.size()); 151 user = (UserData) users2.get(0); 152 assertNotNull(user); 153 assertEquals(testUser.getUserName(), user.getUserName()); 154 155 user.setEnabled(Boolean.FALSE); 157 mgr.saveUser(user); 158 user = null; 159 user = mgr.getUserByUserName(testUser.getUserName()); 160 assertNull(user); 161 162 TestUtils.teardownUser(testUser.getId()); 164 TestUtils.endSession(true); 165 } 166 167 168 171 public void testRoleCRUD() throws Exception { 172 173 UserManager mgr = RollerFactory.getRoller().getUserManager(); 174 UserData user = null; 175 176 UserData testUser = TestUtils.setupUser("roleTestUser"); 178 TestUtils.endSession(true); 179 180 user = mgr.getUserByUserName(testUser.getUserName()); 182 assertNotNull(user); 183 assertEquals(2, user.getRoles().size()); 184 assertTrue(user.hasRole("editor")); 185 assertTrue(user.hasRole("admin")); 186 187 user.revokeRole("admin"); 189 mgr.saveUser(user); 190 TestUtils.endSession(true); 191 192 user = null; 194 user = mgr.getUserByUserName(testUser.getUserName()); 195 assertNotNull(user); 196 assertEquals(1, user.getRoles().size()); 197 assertTrue(user.hasRole("editor")); 198 assertFalse(user.hasRole("admin")); 199 200 user.grantRole("admin"); 202 mgr.saveUser(user); 203 TestUtils.endSession(true); 204 205 user = null; 207 user = mgr.getUserByUserName(testUser.getUserName()); 208 assertNotNull(user); 209 assertEquals(2, user.getRoles().size()); 210 assertTrue(user.hasRole("editor")); 211 assertTrue(user.hasRole("admin")); 212 213 TestUtils.teardownUser(testUser.getId()); 215 TestUtils.endSession(true); 216 } 217 218 219 222 public void testRemoveLoadedUser() throws Exception { 223 } 225 226 } 227 | Popular Tags |