KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > business > UserManagerTest


1
2 package org.roller.business;
3
4 import java.io.FileInputStream JavaDoc;
5 import java.sql.Connection JavaDoc;
6 import java.sql.DriverManager JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.List JavaDoc;
9 import java.util.Map JavaDoc;
10 import java.util.Properties JavaDoc;
11
12 import junit.framework.Test;
13 import junit.framework.TestSuite;
14
15 import org.roller.RollerException;
16 import org.roller.RollerPermissionsException;
17 import org.roller.model.UserManager;
18 import org.roller.model.WeblogManager;
19 import org.roller.pojos.FolderData;
20 import org.roller.pojos.PageData;
21 import org.roller.pojos.UserData;
22 import org.roller.pojos.WeblogCategoryData;
23 import org.roller.pojos.WebsiteData;
24 import org.roller.RollerTestBase;
25
26 ///////////////////////////////////////////////////////////////////////////////
27
/**
28  * Test Roller User Management.
29  */

30 public class UserManagerTest extends RollerTestBase
31 {
32     UserData enabledUser = null;
33     UserData disabledUser = null;
34     String JavaDoc enabledUserName = "enabledUser";
35     String JavaDoc disabledUserName = "disabledUser";
36
37     //------------------------------------------------------------------------
38
public UserManagerTest()
39     {
40         super();
41     }
42
43     //------------------------------------------------------------------------
44
public UserManagerTest(String JavaDoc name)
45     {
46         super(name);
47     }
48
49     //------------------------------------------------------------------------
50
public static Test suite()
51     {
52         return new TestSuite(UserManagerTest.class);
53     }
54
55     //------------------------------------------------------------------------
56
public static void main(String JavaDoc args[])
57     {
58         junit.textui.TestRunner.run(UserManagerTest.class);
59     }
60
61     /**
62      * @see junit.framework.TestCase#setUp()
63      */

64     protected void setUp() throws Exception JavaDoc
65     {
66         super.setUp();
67
68         getRoller().begin(UserData.SYSTEM_USER);
69         UserManager umgr = getRoller().getUserManager();
70
71         enabledUser = createUser(umgr,
72                                  enabledUserName,
73                                  "password",
74                                  "EnabledUser",
75                                  "enabledUser@example.com"
76         );
77
78         disabledUser = createUser(umgr,
79                                  disabledUserName,
80                                  "password",
81                                  "DisabledUser",
82                                  "disabledUser@example.com"
83         );
84         umgr.getWebsite(disabledUserName,false).setIsEnabled(Boolean.FALSE);
85
86         getRoller().commit();
87     }
88
89     /**
90      * @see junit.framework.TestCase#tearDown()
91      */

92     public void tearDown() throws Exception JavaDoc
93     {
94         super.tearDown();
95
96         getRoller().begin(UserData.SYSTEM_USER);
97         UserManager umgr = getRoller().getUserManager();
98
99         enabledUser = umgr.retrieveUser(enabledUser.getId());
100         enabledUser.remove();
101
102         disabledUser = umgr.retrieveUser(disabledUser.getId());
103         disabledUser.remove();
104
105         getRoller().commit();
106     }
107
108     //------------------------------------------------------------------------
109
/** Tests storage and removal of user. */
110     public void testUserStorage() throws RollerException
111     {
112         UserManager umgr = getRoller().getUserManager();
113         UserData stored = null;
114
115         getRoller().begin(UserData.SYSTEM_USER);
116         stored = new UserData(
117             null,
118             "testUserStorage",
119             "password2",
120             "TestUser2",
121             "testuser2@example.com",
122             new java.util.Date JavaDoc());
123         umgr.storeUser(stored);
124         getRoller().commit();
125
126         getRoller().begin(UserData.SYSTEM_USER);
127         UserData retrieved = umgr.retrieveUser(stored.getId());
128         assertEquals(stored, retrieved);
129         getRoller().release();
130
131         getRoller().begin(UserData.SYSTEM_USER);
132         retrieved = umgr.retrieveUser(stored.getId());
133         retrieved.remove();
134         getRoller().commit();
135
136         getRoller().begin(UserData.SYSTEM_USER);
137         assertNull(umgr.retrieveUser(stored.getId()));
138         getRoller().release();
139     }
140
141     //------------------------------------------------------------------------
142
/** Tests storage and removal of website. */
143     public void testWebsiteStorage() throws RollerException
144     {
145         UserData user = null;
146         WebsiteData stored = null;
147         UserManager umgr = getRoller().getUserManager();
148
149         getRoller().begin(UserData.SYSTEM_USER);
150
151         user = new UserData(
152             null,
153             "testWebsiteStorage",
154             "password3",
155             "TestUser3",
156             "testuser3@example.com",
157             new java.util.Date JavaDoc());
158         umgr.storeUser( user );
159
160         stored = new WebsiteData(
161             null,
162             "testsite",
163             "Testsite",
164             user,
165             "dpid",
166             "wpid",
167             Boolean.FALSE,
168             null,
169             null,
170             "edit-weblog.jsp",
171             "ignore",
172             Boolean.TRUE,
173             Boolean.FALSE,
174             null,
175             Boolean.TRUE);
176         umgr.storeWebsite(stored);
177
178         FolderData rootFolder = getRoller().getBookmarkManager()
179             .createFolder(null, "root", "root", stored);
180         rootFolder.save();
181
182         WeblogCategoryData rootCategory = getRoller().getWeblogManager()
183             .createWeblogCategory(stored, null, "root", "root", "");
184         rootCategory.save();
185
186         stored.setBloggerCategory(rootCategory);
187         stored.setDefaultCategory(rootCategory);
188         stored.save();
189
190         getRoller().commit();
191
192         getRoller().begin(UserData.SYSTEM_USER);
193         WebsiteData retrieved = umgr.retrieveWebsite(stored.getId());
194         assertEquals(stored, retrieved);
195         getRoller().release();
196
197         getRoller().begin(UserData.SYSTEM_USER);
198         user = umgr.retrieveUser(user.getId());
199         user.remove();
200         getRoller().commit();
201
202         getRoller().begin(UserData.SYSTEM_USER);
203         assertNull(umgr.retrieveUser(user.getId()));
204         assertNull(umgr.retrieveWebsite(stored.getId()));
205         getRoller().rollback();
206     }
207
208     public void testAddRemoveUser() throws RollerException
209     {
210         UserManager umgr = getRoller().getUserManager();
211
212         // Add a user
213

214         getRoller().begin(UserData.SYSTEM_USER);
215         UserData user = new UserData(
216             null,
217             "testAddRemoveUser",
218             "password4",
219             "TestUser4",
220             "testuser4@example.com",
221             new java.util.Date JavaDoc());
222         Map JavaDoc pages = new HashMap JavaDoc();
223         pages.put("Weblog","Weblog page content");
224         pages.put("_day","Day page content");
225         pages.put("css","CSS page content");
226         umgr.addUser(user, pages, "basic", "en_US_WIN", "America/Los_Angeles");
227         getRoller().commit();
228
229         // Verify that user has all the goodies
230
getRoller().begin(UserData.SYSTEM_USER);
231         UserData user1 = umgr.retrieveUser(user.getId());
232         assertNotNull(user1);
233
234         WebsiteData website = umgr.getWebsite(user.getUserName());
235         assertNotNull(website);
236
237         FolderData root = getRoller().getBookmarkManager().getRootFolder(website);
238         assertNotNull(root);
239
240         List JavaDoc pages1 = getRoller().getUserManager().getPages(website);
241         assertEquals(3, pages1.size());
242         getRoller().rollback();
243
244         // Remove the user
245

246         getRoller().begin(UserData.SYSTEM_USER);
247         user = umgr.retrieveUser(user.getId());
248         user.remove();
249         getRoller().commit();
250
251         // Verify that user was completely deleted
252
getRoller().begin(UserData.SYSTEM_USER);
253         UserData user2 = umgr.retrieveUser(user.getId());
254         assertNull(user2);
255
256         WebsiteData website2 = umgr.getWebsite(user.getUserName());
257         assertNull(website2);
258
259         assertNull(getRoller().getBookmarkManager().retrieveFolder(root.getId()));
260
261         List JavaDoc pages2 = getRoller().getUserManager().getPages(website);
262         assertEquals(0, pages2.size());
263         getRoller().rollback();
264     }
265
266     public void testGetWebsite() throws RollerException
267     {
268         // can get testuser0 who is enabled
269
assertNotNull(getRoller().getUserManager().getWebsite(enabledUserName));
270
271         // can't get testuser1, who is disabled
272
assertNull(getRoller().getUserManager().getWebsite(disabledUserName));
273
274         // can get testuser1 with enabledOnly flag set to false
275
assertNotNull(getRoller().getUserManager().getWebsite(disabledUserName,false));
276     }
277
278     public void testGetUser() throws RollerException
279     {
280         // can get testuser0 who is enabled
281
getRoller().begin(UserData.SYSTEM_USER);
282         
283         UserData user = getRoller().getUserManager().getUser(enabledUserName);
284         assertNotNull(user);
285         
286         // can't get testuser1, who is disabled
287
assertNull(getRoller().getUserManager().getUser(disabledUserName));
288
289         // can get testuser1 with enabledOnly flag set to false
290
assertNotNull(getRoller().getUserManager().getUser(disabledUserName,false));
291         
292         getRoller().release();
293     }
294
295     public void testGetUsers() throws RollerException
296     {
297         // There are users
298
int userCountEnabled = getRoller().getUserManager().getUsers().size();
299         assertTrue(userCountEnabled > 0);
300
301         // At least one user is disabled
302
int userCountAll = getRoller().getUserManager().getUsers(false).size();
303         assertTrue(userCountAll > userCountEnabled);
304     }
305
306     public void testUserRoles() throws Exception JavaDoc {
307         
308         UserData user = null;
309         
310         // check existing roles within a session
311
getRoller().begin(UserData.SYSTEM_USER);
312         user = getRoller().getUserManager().getUser(enabledUserName);
313         assertNotNull(user);
314         assertNotNull(user.getRoles());
315         assertEquals(1, user.getRoles().size());
316         getRoller().release();
317         
318         // test hasRole within a session
319
getRoller().begin(UserData.SYSTEM_USER);
320         user = getRoller().getUserManager().getUser(enabledUserName);
321         assertTrue(user.hasRole("editor"));
322         getRoller().release();
323         
324         // and without a session
325
assertTrue(user.hasRole("editor"));
326
327         // test revokeRole within session
328
getRoller().begin(UserData.SYSTEM_USER);
329         user = getRoller().getUserManager().getUser(enabledUserName);
330         user.revokeRole("editor");
331         assertFalse(user.hasRole("editor"));
332         getRoller().commit();
333         
334         // role has been removed after commit?
335
assertFalse(user.hasRole("editor"));
336         
337         // restore role
338
getRoller().begin(UserData.SYSTEM_USER);
339         user = getRoller().getUserManager().getUser(enabledUserName);
340         user.grantRole("editor");
341         assertTrue(user.hasRole("editor"));
342         getRoller().commit();
343
344         assertTrue(user.hasRole("editor"));
345     }
346     
347     public void testGetPageByName() throws RollerException
348     {
349         WebsiteData wd0 = getRoller().getUserManager().getWebsite(enabledUserName);
350         assertNotNull(getRoller().getUserManager().getPageByName(wd0,"Weblog"));
351     }
352
353     public void testGetPageByLink() throws RollerException
354     {
355         WebsiteData wd0 = getRoller().getUserManager().getWebsite(enabledUserName);
356         assertNotNull(getRoller().getUserManager().getPageByLink(wd0,"Weblog"));
357     }
358
359     public void testGetPages() throws RollerException
360     {
361         // testuser0 is enabled and has 3 pages
362
WebsiteData wd0 = getRoller().getUserManager().getWebsite(enabledUserName);
363         assertEquals(3, getRoller().getUserManager().getPages(wd0).size());
364     }
365
366     public void hide_testUpdateIfNeeded() throws Exception JavaDoc
367     {
368         UserManager umgr = getRoller().getUserManager();
369         WeblogManager wmgr = getRoller().getWeblogManager();
370         PersistenceStrategy pstrategy = getRoller().getPersistenceStrategy();
371
372         // create cats without a root
373
getRoller().begin(UserData.SYSTEM_USER);
374
375         WebsiteData website = umgr.retrieveWebsite(mWebsite.getId());
376         WeblogCategoryData origRoot = wmgr.getRootWeblogCategory(mWebsite);
377         website.setBloggerCategory(null);
378         website.setDefaultCategory(null);
379
380         if (null != origRoot) origRoot.remove();
381
382         WeblogCategoryData cat1 = wmgr.createWeblogCategory(
383             website, null, "cat1 name", "cat1 desc", null);
384         pstrategy.store(cat1);
385
386         WeblogCategoryData cat2 = wmgr.createWeblogCategory(
387             website, null, "cat2 name", "cat2 desc", null);
388         pstrategy.store(cat2);
389
390         WeblogCategoryData cat3 = wmgr.createWeblogCategory(
391             website, null, "cat3 name", "cat3 desc", null);
392         pstrategy.store(cat3);
393
394         getRoller().commit();
395
396         // upgrade site
397

398         // We need a database connection and the hibernate.properties file
399
// is easier to parse than the Castor database.xml file.
400
Properties JavaDoc hibernateProperties = new Properties JavaDoc();
401         hibernateProperties.load(new FileInputStream JavaDoc("hibernate.properties"));
402         String JavaDoc driverClass = hibernateProperties.getProperty("hibernate.connection.driver_class");
403         String JavaDoc connectionUrl = hibernateProperties.getProperty("hibernate.connection.url");
404         Class.forName(driverClass);
405         Connection JavaDoc con = DriverManager.getConnection(connectionUrl);
406
407         getRoller().upgradeDatabase(con);
408
409         // verify that upgrade created a root and assigned it to cats
410
getRoller().begin(UserData.SYSTEM_USER);
411         WeblogCategoryData root = wmgr.getRootWeblogCategory(mWebsite);
412         assertNotNull(root);
413
414         cat1 = wmgr.retrieveWeblogCategory(cat1.getId());
415         assertEquals(root, cat1.getParent());
416
417         cat2 = wmgr.retrieveWeblogCategory(cat2.getId());
418         assertEquals(root, cat2.getParent());
419
420         cat3 = wmgr.retrieveWeblogCategory(cat3.getId());
421         assertEquals(root, cat3.getParent());
422
423         getRoller().release();
424     }
425
426
427 }
428
Popular Tags