| 1 16 package org.outerj.daisy.repository.test; 17 18 import java.util.Date ; 19 20 import junit.framework.TestCase; 21 22 import org.outerj.daisy.repository.commonimpl.AuthenticatedUser; 23 import org.outerj.daisy.repository.commonimpl.user.*; 24 import org.outerj.daisy.repository.user.*; 25 import org.outerj.daisy.repository.RepositoryException; 26 import org.outerx.daisy.x10.RoleDocument; 27 import org.outerx.daisy.x10.RolesDocument; 28 import org.outerx.daisy.x10.UserDocument; 29 import org.outerx.daisy.x10.UserInfoDocument; 30 31 34 public class UserRoleImplTest extends TestCase { 35 private static final String TESTROLE_NAME = "WhiteDwarfCreator"; 36 private UserManagementStrategy dummyStrategy = new DummyUserManagementStrategy(); 37 UserImpl user; 38 RoleImpl role; 39 42 protected void setUp() throws Exception { 43 super.setUp(); 44 user = new UserImpl(dummyStrategy, "blah", new DummyAuthenticatedUser() ); 45 role = new RoleImpl(dummyStrategy, TESTROLE_NAME, new DummyAuthenticatedUser()); 46 role.setDescription("ElementaryMyDearWatson"); 47 49 role.save(); 50 51 52 user.setEmail("warshan@tspg.org"); 53 user.setFirstName("Pelle"); 54 user.setLastName("Knark"); 55 56 user.addToRole(role); 57 user.setDefaultRole(role); 58 user.save(); 59 60 61 } 62 63 66 protected void tearDown() throws Exception { 67 super.tearDown(); 68 } 69 70 public void testDefaultRole() throws Exception { 71 Role defRole = new RoleImpl(dummyStrategy, TESTROLE_NAME, new DummyAuthenticatedUser()); 73 defRole.setDescription("ElementaryMyDearWatson"); 74 defRole.save(); 76 77 user.addToRole(defRole); 78 user.setDefaultRole(defRole); 79 } 80 81 public void testRolesUtil() { 82 Role[] roleArray = new Role[5]; 83 roleArray[0] = this.role; 84 85 roleArray[1] = new RoleImpl(new DummyUserManagementStrategy(), "qsdfqsdfqdsfsqdqsf", new DummyAuthenticatedUser()); 86 Role frequentFlyer = new RoleImpl(new DummyUserManagementStrategy(), "TheFrequentFlyer", new DummyAuthenticatedUser()); 87 roleArray[2] = frequentFlyer; 88 roleArray[3] = new RoleImpl(new DummyUserManagementStrategy(), "H�lsristningarna", new DummyAuthenticatedUser()); 89 Role sfd = new RoleImpl(new DummyUserManagementStrategy(), "SummerFestivalDweller", new DummyAuthenticatedUser()); 90 roleArray[4] = sfd; 91 92 93 94 Roles roles = new RolesImpl(roleArray); 96 Role r = RolesUtil.getRole(roles, this.role.getName()); 97 assertEquals(r, this.role); 98 99 r = RolesUtil.getRole(roles, "TheFrequentFlyer"); 101 assertEquals(r, frequentFlyer); 102 103 r = RolesUtil.getRole(roles, "SummerFestivalDweller"); 105 assertEquals(r, sfd); 106 107 roleArray = new Role[0]; 108 roles = new RolesImpl(roleArray); 109 110 r = RolesUtil.getRole(roles, "mqsdfmdsj"); 111 assertNull(r); 112 } 113 114 public void testGetRoleXml() { 115 RoleDocument roleDoc = role.getXml(); 116 RoleDocument.Role r = roleDoc.getRole(); 117 assertNotNull(r.getName()); 118 assertNotNull(r.getDescription()); 119 } 120 121 public void testUserRoles() { 122 Roles r = user.getAllRoles(); 123 Role[] roleArr = r.getArray(); 124 assertEquals(roleArr.length, 1); 125 } 126 127 public void testGetUserXml() { 128 UserDocument userDoc = user.getXml(); 129 assertNotNull(userDoc); 130 UserDocument.User u = userDoc.getUser(); 131 assertNotNull(u); 132 assertNotNull(u.getRole()); 133 assertNotNull(u.getEmail()); 134 assertNotNull(u.getFirstName()); 135 assertNotNull(u.getLastName()); 136 137 RolesDocument.Roles r = u.getRoles(); 139 RoleDocument.Role[] roleArr = r.getRoleArray(); 140 assertEquals(roleArr.length, 1); 141 RoleDocument.Role userRole = roleArr[0]; 142 assertEquals(userRole.getName(), TESTROLE_NAME); 143 144 RoleDocument.Role defaultRole = u.getRole(); 146 assertEquals(defaultRole.getId(), role.getId()); 147 assertEquals(defaultRole.getName(), role.getName()); 148 149 } 150 151 152 private class DummyAuthenticatedUser implements AuthenticatedUser { 153 public long getId() { 154 155 return 0; 156 } 157 158 public String getLogin() { 159 return null; 160 } 161 162 public String getPassword() { 163 164 return null; 165 } 166 167 public long[] getActiveRoleIds() { 168 169 return new long[0]; 170 } 171 172 public long[] getAvailableRoleIds() { 173 174 return new long[0]; 175 } 176 177 public boolean isInAdministratorRole() { 178 return false; 179 } 180 181 public void setActiveRoleIds(long[] roleIds) { 182 } 183 184 public UserInfoDocument getXml() { 185 return null; 186 } 187 188 public boolean isInRole(long roleId) { 189 return false; 190 } 191 192 } 193 194 private class DummyUserManagementStrategy implements UserManagementStrategy { 195 private long nextUserId=4321; 196 private long nextRoleId=1234; 197 198 public Users loadUsers(AuthenticatedUser requestingUser) throws UserManagementException { 199 200 return null; 201 } 202 203 public long[] getUserIds(AuthenticatedUser requestingUser) throws UserManagementException { 204 return new long[0]; 205 } 206 207 public Roles loadRoles(AuthenticatedUser requestingUser) throws UserManagementException { 208 209 return null; 210 } 211 212 public void deleteUser(long userId, AuthenticatedUser requestingUser) throws UserManagementException { 213 214 215 } 216 217 public void deleteRole(long roleId, AuthenticatedUser requestingUser) throws UserManagementException { 218 219 220 } 221 222 public UserImpl getUser(String login, AuthenticatedUser requestingUser) throws UserManagementException { 223 224 return null; 225 } 226 227 public RoleImpl getRole(String roleId, AuthenticatedUser requestingUser) throws UserManagementException { 228 229 return null; 230 } 231 232 public void store(UserImpl user) throws UserManagementException { 233 UserImpl.IntimateAccess userInt = user.getIntimateAccess(DummyUserManagementStrategy.this); 235 userInt.saved(nextUserId++, user.getFirstName(), user.getLastName(), user.getEmail(), getNowDate(), 1534, user.getUpdateCount() + 1); 236 } 237 238 public void store(RoleImpl role) throws UserManagementException { 239 RoleImpl.IntimateAccess roleInt = role.getIntimateAccess(DummyUserManagementStrategy.this); 241 roleInt.setLastModified(getNowDate()); 242 roleInt.setLastModifier(1534); 243 roleInt.saved(nextRoleId++, role.getName(), role.getDescription(), role.getUpdateCount() + 1); 244 } 245 246 private Date getNowDate() { 247 Date d = new Date (System.currentTimeMillis()); 248 return d; 249 } 250 251 public UserImpl getUser(long userId, AuthenticatedUser user) throws UserManagementException { 252 return null; 253 } 254 255 public RoleImpl getRole(long roleId, AuthenticatedUser user) throws UserManagementException { 256 return null; 257 } 258 259 public UsersImpl getUsersByEmail(String email, AuthenticatedUser user) throws RepositoryException { 260 return null; 261 } 262 263 public AuthenticationSchemeInfos getAuthenticationSchemes(AuthenticatedUser requestingUser) throws RepositoryException { 264 return null; 265 } 266 } 267 } 268 | Popular Tags |