KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > services > security > TestSecurityUser


1 package org.apache.turbine.services.security;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License")
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.util.Hashtable JavaDoc;
20 import java.util.List JavaDoc;
21
22 import junit.framework.Test;
23 import junit.framework.TestSuite;
24
25 import org.apache.turbine.om.security.User;
26 import org.apache.turbine.test.BaseTurbineHsqlTest;
27 import org.apache.turbine.util.security.DataBackendException;
28 import org.apache.turbine.util.security.EntityExistsException;
29 import org.apache.turbine.util.security.UnknownEntityException;
30
31 public class TestSecurityUser
32         extends BaseTurbineHsqlTest
33 {
34     public TestSecurityUser(String JavaDoc name)
35             throws Exception JavaDoc
36     {
37         super(name, "conf/test/TurbineResources.properties");
38     }
39
40     public static Test suite()
41     {
42         return new TestSuite(TestSecurityUser.class);
43     }
44
45     private void checkUserList()
46             throws Exception JavaDoc
47     {
48         SecurityService ss = TurbineSecurity.getService();
49         assertEquals("User added to storage!", ss.getUserList(new org.apache.torque.util.Criteria()).size(), 2);
50     }
51
52     public void testInit()
53     {
54         SecurityService ss = TurbineSecurity.getService();
55         assertTrue("Service failed to initialize", ss.getInit());
56     }
57
58     // Make sure that our database contains what we need
59
public void testDatabase()
60         throws Exception JavaDoc
61     {
62         SecurityService ss = TurbineSecurity.getService();
63
64         List JavaDoc users = ss.getUserList(new org.apache.torque.util.Criteria());
65
66         assertEquals("User DB Wrong!", users.size(), 2);
67     }
68
69     public void testUsers()
70         throws Exception JavaDoc
71     {
72         SecurityService ss = TurbineSecurity.getService();
73         UserManager um = ss.getUserManager();
74
75         User u = um.retrieve("admin");
76         assertNotNull("No Admin found!", u);
77         assertEquals("Admin Id wrong!", u.getId(), 1);
78
79         // Check Logged in
80
assertFalse(u.hasLoggedIn());
81         u.setHasLoggedIn(Boolean.TRUE);
82         assertTrue(u.hasLoggedIn());
83         u.setHasLoggedIn(Boolean.FALSE);
84         assertFalse(u.hasLoggedIn());
85
86         // Check perm and temp storage
87
assertEquals(u.getPermStorage().getClass(), Hashtable JavaDoc.class);
88         assertEquals(u.getTempStorage().getClass(), Hashtable JavaDoc.class);
89
90         Hashtable JavaDoc permStorage = u.getPermStorage();
91
92         int access = u.getAccessCounter();
93         u.incrementAccessCounter();
94
95         um.store(u);
96
97         u = null;
98
99         User u2 = um.retrieve("admin");
100
101
102         // Hashtable has changed
103
assertNotSame(permStorage, u2.getPermStorage());
104
105         // But the Count should be the same
106
assertEquals(access + 1 , u2.getAccessCounter());
107
108         checkUserList();
109     }
110
111     public void testAddUser()
112         throws Exception JavaDoc
113     {
114         SecurityService ss = TurbineSecurity.getService();
115
116         User newbie = TurbineSecurity.getUserInstance();
117         newbie.setName("newbie");
118
119         newbie.setFirstName("John");
120         newbie.setLastName("Doe");
121
122         ss.addUser(newbie, "newbie");
123
124         List JavaDoc users = ss.getUserList(new org.apache.torque.util.Criteria());
125         assertEquals("User was not added", users.size(), 3);
126
127         try
128         {
129             User admin = ss.getUser("admin");
130
131             ss.addUser(admin, "admin");
132             fail("Existing User could be added!");
133         }
134         catch (Exception JavaDoc e)
135         {
136             assertEquals("Wrong Exception thrown: " + e.getClass().getName(), e.getClass(), EntityExistsException.class);
137         }
138
139         try
140         {
141             User empty = TurbineSecurity.getUserInstance();
142
143             ss.addUser(empty, "empty");
144             fail("User with empty Username could be added!");
145         }
146         catch (Exception JavaDoc e)
147         {
148             assertEquals("Wrong Exception thrown: " + e.getClass().getName(), e.getClass(), DataBackendException.class);
149         }
150
151         assertEquals("User was not added", users.size(), 3);
152     }
153
154     public void testRemoveUser()
155         throws Exception JavaDoc
156     {
157         SecurityService ss = TurbineSecurity.getService();
158
159         User newbie = ss.getUser("newbie");
160         assertNotNull(newbie);
161
162         ss.removeUser(newbie);
163
164         try
165         {
166             User foo = TurbineSecurity.getUserInstance();
167             foo.setName("foo");
168
169             ss.removeUser(foo);
170             fail("Non Existing User could be deleted!");
171         }
172         catch (Exception JavaDoc e)
173         {
174             assertEquals("Wrong Exception thrown: " + e.getClass().getName(), e.getClass(), UnknownEntityException.class);
175         }
176
177         checkUserList();
178     }
179
180     public void testRetrieve()
181             throws Exception JavaDoc
182     {
183         SecurityService ss = TurbineSecurity.getService();
184         UserManager um = ss.getUserManager();
185
186         User u = um.retrieve("admin");
187         assertNotNull("No Admin found!", u);
188         assertEquals("Admin Id wrong!", u.getId(), 1);
189
190         User u2 = um.retrieveById(new Integer JavaDoc(1));
191         assertNotNull("No Admin found!", u2);
192         assertEquals("Admin Name wrong!", u.getName(), "admin");
193
194         assertEquals("Two different User objects retrieved!", u, u2);
195     }
196 }
197
198
Popular Tags