001: package org.apache.turbine.services.security;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.util.Hashtable;
023: import java.util.List;
024:
025: import junit.framework.Test;
026: import junit.framework.TestSuite;
027:
028: import org.apache.turbine.om.security.User;
029: import org.apache.turbine.test.BaseTurbineHsqlTest;
030: import org.apache.turbine.util.security.DataBackendException;
031: import org.apache.turbine.util.security.EntityExistsException;
032: import org.apache.turbine.util.security.UnknownEntityException;
033:
034: public class TestSecurityUser extends BaseTurbineHsqlTest {
035: public TestSecurityUser(String name) throws Exception {
036: super (name, "conf/test/TurbineResources.properties");
037: }
038:
039: public static Test suite() {
040: return new TestSuite(TestSecurityUser.class);
041: }
042:
043: private void checkUserList() throws Exception {
044: SecurityService ss = TurbineSecurity.getService();
045: assertEquals("User added to storage!", ss.getUserList(
046: new org.apache.torque.util.Criteria()).size(), 2);
047: }
048:
049: public void testInit() {
050: SecurityService ss = TurbineSecurity.getService();
051: assertTrue("Service failed to initialize", ss.getInit());
052: }
053:
054: // Make sure that our database contains what we need
055: public void testDatabase() throws Exception {
056: SecurityService ss = TurbineSecurity.getService();
057:
058: List users = ss
059: .getUserList(new org.apache.torque.util.Criteria());
060:
061: assertEquals("User DB Wrong!", users.size(), 2);
062: }
063:
064: public void testUsers() throws Exception {
065: SecurityService ss = TurbineSecurity.getService();
066: UserManager um = ss.getUserManager();
067:
068: User u = um.retrieve("admin");
069: assertNotNull("No Admin found!", u);
070: assertEquals("Admin Id wrong!", u.getId(), 1);
071:
072: // Check Logged in
073: assertFalse(u.hasLoggedIn());
074: u.setHasLoggedIn(Boolean.TRUE);
075: assertTrue(u.hasLoggedIn());
076: u.setHasLoggedIn(Boolean.FALSE);
077: assertFalse(u.hasLoggedIn());
078:
079: // Check perm and temp storage
080: assertEquals(u.getPermStorage().getClass(), Hashtable.class);
081: assertEquals(u.getTempStorage().getClass(), Hashtable.class);
082:
083: Hashtable permStorage = u.getPermStorage();
084:
085: int access = u.getAccessCounter();
086: u.incrementAccessCounter();
087:
088: um.store(u);
089:
090: u = null;
091:
092: User u2 = um.retrieve("admin");
093:
094: // Hashtable has changed
095: assertNotSame(permStorage, u2.getPermStorage());
096:
097: // But the Count should be the same
098: assertEquals(access + 1, u2.getAccessCounter());
099:
100: checkUserList();
101: }
102:
103: public void testAddUser() throws Exception {
104: SecurityService ss = TurbineSecurity.getService();
105:
106: User newbie = TurbineSecurity.getUserInstance();
107: newbie.setName("newbie");
108:
109: newbie.setFirstName("John");
110: newbie.setLastName("Doe");
111:
112: ss.addUser(newbie, "newbie");
113:
114: List users = ss
115: .getUserList(new org.apache.torque.util.Criteria());
116: assertEquals("User was not added", users.size(), 3);
117:
118: try {
119: User admin = ss.getUser("admin");
120:
121: ss.addUser(admin, "admin");
122: fail("Existing User could be added!");
123: } catch (Exception e) {
124: assertEquals("Wrong Exception thrown: "
125: + e.getClass().getName(), e.getClass(),
126: EntityExistsException.class);
127: }
128:
129: try {
130: User empty = TurbineSecurity.getUserInstance();
131:
132: ss.addUser(empty, "empty");
133: fail("User with empty Username could be added!");
134: } catch (Exception e) {
135: assertEquals("Wrong Exception thrown: "
136: + e.getClass().getName(), e.getClass(),
137: DataBackendException.class);
138: }
139:
140: assertEquals("User was not added", users.size(), 3);
141: }
142:
143: public void testRemoveUser() throws Exception {
144: SecurityService ss = TurbineSecurity.getService();
145:
146: User newbie = ss.getUser("newbie");
147: assertNotNull(newbie);
148:
149: ss.removeUser(newbie);
150:
151: try {
152: User foo = TurbineSecurity.getUserInstance();
153: foo.setName("foo");
154:
155: ss.removeUser(foo);
156: fail("Non Existing User could be deleted!");
157: } catch (Exception e) {
158: assertEquals("Wrong Exception thrown: "
159: + e.getClass().getName(), e.getClass(),
160: UnknownEntityException.class);
161: }
162:
163: checkUserList();
164: }
165:
166: public void testRetrieve() throws Exception {
167: SecurityService ss = TurbineSecurity.getService();
168: UserManager um = ss.getUserManager();
169:
170: User u = um.retrieve("admin");
171: assertNotNull("No Admin found!", u);
172: assertEquals("Admin Id wrong!", u.getId(), 1);
173:
174: User u2 = um.retrieveById(new Integer(1));
175: assertNotNull("No Admin found!", u2);
176: assertEquals("Admin Name wrong!", u.getName(), "admin");
177:
178: assertEquals("Two different User objects retrieved!", u, u2);
179: }
180: }
|