KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > repository > test > UserRoleImplTest


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
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 package org.outerj.daisy.repository.test;
17
18 import java.util.Date JavaDoc;
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 /**
32  * Test class for the UserImpl and RoleImpl user management objects.
33  */

34 public class UserRoleImplTest extends TestCase {
35     private static final String JavaDoc TESTROLE_NAME = "WhiteDwarfCreator";
36     private UserManagementStrategy dummyStrategy = new DummyUserManagementStrategy();
37     UserImpl user;
38     RoleImpl role;
39     /*
40      * @see TestCase#setUp()
41      */

42     protected void setUp() throws Exception JavaDoc {
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         //definitely need to save first because we want add this role to a user
48

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     /*
64      * @see TestCase#tearDown()
65      */

66     protected void tearDown() throws Exception JavaDoc {
67         super.tearDown();
68     }
69     
70     public void testDefaultRole() throws Exception JavaDoc {
71         //create a role which we want to assign as default
72
Role defRole = new RoleImpl(dummyStrategy, TESTROLE_NAME, new DummyAuthenticatedUser());
73         defRole.setDescription("ElementaryMyDearWatson");
74         //definitely need to save first because we want add this role to a user
75
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         //test first element
95
Roles roles = new RolesImpl(roleArray);
96         Role r = RolesUtil.getRole(roles, this.role.getName());
97         assertEquals(r, this.role);
98         
99         //test random element
100
r = RolesUtil.getRole(roles, "TheFrequentFlyer");
101         assertEquals(r, frequentFlyer);
102         
103         //test last element
104
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         //ok, and now the role stuff
138
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         //default role stuff
145
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 JavaDoc getLogin() {
159             return null;
160         }
161
162         public String JavaDoc 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 JavaDoc login, AuthenticatedUser requestingUser) throws UserManagementException {
223             
224             return null;
225         }
226
227         public RoleImpl getRole(String JavaDoc roleId, AuthenticatedUser requestingUser) throws UserManagementException {
228             
229             return null;
230         }
231
232         public void store(UserImpl user) throws UserManagementException {
233             //let this dummy behave like the real deal
234
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             //let this dummy behave like the real deal
240
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 JavaDoc getNowDate() {
247             Date JavaDoc d = new Date JavaDoc(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 JavaDoc 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