1   package org.apache.turbine.services.security;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Hashtable;
23  import java.util.List;
24  
25  import junit.framework.Test;
26  import junit.framework.TestSuite;
27  
28  import org.apache.turbine.om.security.User;
29  import org.apache.turbine.test.BaseTurbineHsqlTest;
30  import org.apache.turbine.util.security.DataBackendException;
31  import org.apache.turbine.util.security.EntityExistsException;
32  import org.apache.turbine.util.security.UnknownEntityException;
33  
34  public class TestSecurityUser
35          extends BaseTurbineHsqlTest
36  {
37      public TestSecurityUser(String name)
38              throws Exception
39      {
40          super(name, "conf/test/TurbineResources.properties");
41      }
42  
43      public static Test suite()
44      {
45          return new TestSuite(TestSecurityUser.class);
46      }
47  
48      private void checkUserList()
49              throws Exception
50      {
51          SecurityService ss = TurbineSecurity.getService();
52          assertEquals("User added to storage!", ss.getUserList(new org.apache.torque.util.Criteria()).size(), 2);
53      }
54  
55      public void testInit()
56      {
57          SecurityService ss = TurbineSecurity.getService();
58          assertTrue("Service failed to initialize", ss.getInit());
59      }
60  
61      // Make sure that our database contains what we need
62      public void testDatabase()
63      	throws Exception
64      {
65          SecurityService ss = TurbineSecurity.getService();
66  
67          List users = ss.getUserList(new org.apache.torque.util.Criteria());
68  
69          assertEquals("User DB Wrong!", users.size(), 2);
70      }
71  
72      public void testUsers()
73      	throws Exception
74      {
75          SecurityService ss = TurbineSecurity.getService();
76          UserManager um = ss.getUserManager();
77  
78          User u = um.retrieve("admin");
79          assertNotNull("No Admin found!", u);
80          assertEquals("Admin Id wrong!", u.getId(), 1);
81  
82          // Check Logged in
83          assertFalse(u.hasLoggedIn());
84          u.setHasLoggedIn(Boolean.TRUE);
85          assertTrue(u.hasLoggedIn());
86          u.setHasLoggedIn(Boolean.FALSE);
87          assertFalse(u.hasLoggedIn());
88  
89          // Check perm and temp storage
90          assertEquals(u.getPermStorage().getClass(), Hashtable.class);
91          assertEquals(u.getTempStorage().getClass(), Hashtable.class);
92  
93          Hashtable permStorage = u.getPermStorage();
94  
95          int access = u.getAccessCounter();
96          u.incrementAccessCounter();
97  
98          um.store(u);
99  
100         u = null;
101 
102         User u2 = um.retrieve("admin");
103 
104 
105         // Hashtable has changed
106         assertNotSame(permStorage, u2.getPermStorage());
107 
108         // But the Count should be the same
109         assertEquals(access + 1 , u2.getAccessCounter());
110 
111         checkUserList();
112     }
113 
114     public void testAddUser()
115     	throws Exception
116     {
117         SecurityService ss = TurbineSecurity.getService();
118 
119         User newbie = TurbineSecurity.getUserInstance();
120         newbie.setName("newbie");
121 
122         newbie.setFirstName("John");
123         newbie.setLastName("Doe");
124 
125         ss.addUser(newbie, "newbie");
126 
127         List users = ss.getUserList(new org.apache.torque.util.Criteria());
128         assertEquals("User was not added", users.size(), 3);
129 
130         try
131         {
132             User admin = ss.getUser("admin");
133 
134             ss.addUser(admin, "admin");
135             fail("Existing User could be added!");
136         }
137         catch (Exception e)
138         {
139             assertEquals("Wrong Exception thrown: " + e.getClass().getName(), e.getClass(), EntityExistsException.class);
140         }
141 
142         try
143         {
144             User empty = TurbineSecurity.getUserInstance();
145 
146             ss.addUser(empty, "empty");
147             fail("User with empty Username could be added!");
148         }
149         catch (Exception e)
150         {
151             assertEquals("Wrong Exception thrown: " + e.getClass().getName(), e.getClass(), DataBackendException.class);
152         }
153 
154         assertEquals("User was not added", users.size(), 3);
155     }
156 
157     public void testRemoveUser()
158     	throws Exception
159     {
160         SecurityService ss = TurbineSecurity.getService();
161 
162         User newbie = ss.getUser("newbie");
163         assertNotNull(newbie);
164 
165         ss.removeUser(newbie);
166 
167         try
168         {
169             User foo = TurbineSecurity.getUserInstance();
170             foo.setName("foo");
171 
172             ss.removeUser(foo);
173             fail("Non Existing User could be deleted!");
174         }
175         catch (Exception e)
176         {
177             assertEquals("Wrong Exception thrown: " + e.getClass().getName(), e.getClass(), UnknownEntityException.class);
178         }
179 
180         checkUserList();
181     }
182 
183     public void testRetrieve()
184             throws Exception
185     {
186         SecurityService ss = TurbineSecurity.getService();
187         UserManager um = ss.getUserManager();
188 
189         User u = um.retrieve("admin");
190         assertNotNull("No Admin found!", u);
191         assertEquals("Admin Id wrong!", u.getId(), 1);
192 
193         User u2 = um.retrieveById(new Integer(1));
194         assertNotNull("No Admin found!", u2);
195         assertEquals("Admin Name wrong!", u.getName(), "admin");
196 
197         assertEquals("Two different User objects retrieved!", u, u2);
198     }
199 }
200