KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > users > filesystem > UserTest


1 /*
2 Copyright (c) 2003 eInnovation Inc. All rights reserved
3
4 This library is free software; you can redistribute it and/or modify it under the terms
5 of the GNU Lesser General Public License as published by the Free Software Foundation;
6 either version 2.1 of the License, or (at your option) any later version.
7
8 This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the GNU Lesser General Public License for more details.
11 */

12
13 package com.openedit.users.filesystem;
14
15 import java.util.HashMap JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import com.openedit.users.BaseUserTest;
19 import com.openedit.users.Group;
20 import com.openedit.users.User;
21
22
23
24 /**
25  * This is an abstract test for {@link User} implementations. Concrete {@link User}s should
26  * subclass this testcase and add their own tests.
27  *
28  * @author Eric Galluzzo
29  */

30 public abstract class UserTest extends BaseUserTest
31 {
32     protected User fieldUser;
33
34     public UserTest(String JavaDoc inName)
35     {
36         super(inName);
37     }
38
39     public void testHasPermission() throws Exception JavaDoc
40     {
41         assertTrue("User should have permission1", fieldUser.hasPermission("permission1"));
42         assertTrue("User should have permission3", fieldUser.hasPermission("permission3"));
43     }
44
45     public void testHasPermission_Negative() throws Exception JavaDoc
46     {
47         assertTrue(
48             "User should not have nosuchpermission", !fieldUser.hasPermission("nosuchpermission"));
49     }
50
51     public void testPut() throws Exception JavaDoc
52     {
53         fieldUser.put("foo", "bar");
54         assertEquals("bar", fieldUser.get("foo"));
55     }
56
57     public void testPutAll() throws Exception JavaDoc
58     {
59         Map JavaDoc map = new HashMap JavaDoc();
60         map.put( "foo", "bar" );
61         map.put( "baz", "wibble" );
62         fieldUser.putAll(map);
63         assertEquals("bar", fieldUser.get("foo"));
64         assertEquals("wibble", fieldUser.get("baz"));
65     }
66
67     public void testSetPassword() throws Exception JavaDoc
68     {
69         fieldUser.setPassword("newpwd");
70         assertTrue(
71             "Should have authenticated with new password successfully",
72             fieldUserManager.authenticate(fieldUser, "newpwd"));
73         assertTrue(
74             "Should not have authenticated with old password",
75             !fieldUserManager.authenticate(fieldUser, "testpwd"));
76     }
77     
78     public void testGetCreationDate() throws Exception JavaDoc
79     {
80         long currentTime = System.currentTimeMillis();
81         // There shouldn't be more than about 2 seconds between the two dates,
82
// even if we're using a really slow user manager like a database-backed
83
// one.
84
assertTrue( "Dates are not within 2 seconds of each other",
85             currentTime - fieldUser.getCreationDate().getTime() <= 2000 );
86     }
87
88     /*
89      * @see TestCase#setUp()
90      */

91     protected void setUp() throws Exception JavaDoc
92     {
93         fieldUserManager = createUserManager();
94         fieldUser = fieldUserManager.createUser("testuser", "testpwd");
95
96         Group group = fieldUserManager.createGroup("testgroup");
97         group.addPermission("permission1");
98         fieldUser.addGroup(group );
99         fieldUserManager.saveGroup(group);
100         Group group2 = fieldUserManager.createGroup("testgroup2");
101         group2.addPermission("permission2");
102         group2.addPermission("permission3");
103         fieldUser.addGroup(group2);
104         fieldUserManager.saveGroup(group2);
105         fieldUserManager.saveUser(fieldUser);
106     }
107
108     /*
109      * @see TestCase#tearDown()
110      */

111     protected void tearDown() throws Exception JavaDoc
112     {
113         deleteUserManager(fieldUserManager);
114     }
115 }
116
Popular Tags