001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018:
019: package org.apache.roller.business;
020:
021: import java.util.ArrayList;
022: import java.util.Collection;
023: import java.util.List;
024: import junit.framework.Test;
025: import junit.framework.TestCase;
026: import junit.framework.TestSuite;
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.apache.roller.TestUtils;
030: import org.apache.roller.business.RollerFactory;
031: import org.apache.roller.business.UserManager;
032: import org.apache.roller.pojos.UserData;
033:
034: /**
035: * Test User related business operations.
036: */
037: public class UserTest extends TestCase {
038:
039: public static Log log = LogFactory.getLog(UserTest.class);
040:
041: public UserTest(String name) {
042: super (name);
043: }
044:
045: public static Test suite() {
046: return new TestSuite(UserTest.class);
047: }
048:
049: public void setUp() throws Exception {
050: }
051:
052: public void tearDown() throws Exception {
053: }
054:
055: /**
056: * Test basic persistence operations ... Create, Update, Delete.
057: */
058: public void testUserCRUD() throws Exception {
059:
060: UserManager mgr = RollerFactory.getRoller().getUserManager();
061: UserData user = null;
062:
063: UserData testUser = new UserData();
064: testUser.setUserName("testUser");
065: testUser.setPassword("password");
066: testUser.setFullName("Test User");
067: testUser.setEmailAddress("TestUser@dev.null");
068: testUser.setLocale("en_US");
069: testUser.setTimeZone("America/Los_Angeles");
070: testUser.setDateCreated(new java.util.Date());
071: testUser.setEnabled(Boolean.TRUE);
072:
073: // make sure test user does not exist
074: user = mgr.getUserByUserName(testUser.getUserName());
075: assertNull(user);
076:
077: // add test user
078: mgr.addUser(testUser);
079: String id = testUser.getId();
080: TestUtils.endSession(true);
081:
082: // make sure test user exists
083: user = null;
084: user = mgr.getUser(id);
085: assertNotNull(user);
086: assertEquals(testUser, user);
087:
088: // modify user and save
089: user.setFullName("testtesttest");
090: mgr.saveUser(user);
091: TestUtils.endSession(true);
092:
093: // make sure changes were saved
094: user = null;
095: user = mgr.getUser(id);
096: assertNotNull(user);
097: assertEquals("testtesttest", user.getFullName());
098:
099: // remove test user
100: mgr.removeUser(user);
101: TestUtils.endSession(true);
102:
103: // make sure user no longer exists
104: user = null;
105: user = mgr.getUser(id);
106: assertNull(user);
107: }
108:
109: /**
110: * Test lookup mechanisms.
111: */
112: public void testUserLookups() throws Exception {
113:
114: UserManager mgr = RollerFactory.getRoller().getUserManager();
115: UserData user = null;
116:
117: // add test user
118: UserData testUser = TestUtils.setupUser("userTestUser");
119: TestUtils.endSession(true);
120:
121: // lookup by username
122: user = mgr.getUserByUserName(testUser.getUserName());
123: assertNotNull(user);
124: assertEquals(testUser.getUserName(), user.getUserName());
125:
126: // lookup by id
127: String id = user.getId();
128: user = null;
129: user = mgr.getUser(id);
130: assertNotNull(user);
131: assertEquals(testUser.getUserName(), user.getUserName());
132:
133: // lookup by UserName (part)
134: user = null;
135: List users1 = mgr.getUsersStartingWith(testUser.getUserName()
136: .substring(0, 3), Boolean.TRUE, 0, 1);
137: assertEquals(1, users1.size());
138: user = (UserData) users1.get(0);
139: assertNotNull(user);
140: assertEquals(testUser.getUserName(), user.getUserName());
141:
142: // lookup by Email (part)
143: user = null;
144: List users2 = mgr.getUsersStartingWith(testUser
145: .getEmailAddress().substring(0, 3), Boolean.TRUE, 0, 1);
146: assertEquals(1, users2.size());
147: user = (UserData) users2.get(0);
148: assertNotNull(user);
149: assertEquals(testUser.getUserName(), user.getUserName());
150:
151: // make sure disable users are not returned
152: user.setEnabled(Boolean.FALSE);
153: mgr.saveUser(user);
154: user = null;
155: user = mgr.getUserByUserName(testUser.getUserName());
156: assertNull(user);
157:
158: // remove test user
159: TestUtils.teardownUser(testUser.getId());
160: TestUtils.endSession(true);
161: }
162:
163: /**
164: * Test basic user role persistence ... Add, Remove
165: */
166: public void testRoleCRUD() throws Exception {
167:
168: UserManager mgr = RollerFactory.getRoller().getUserManager();
169: UserData user = null;
170:
171: // add test user
172: UserData testUser = TestUtils.setupUser("roleTestUser");
173: TestUtils.endSession(true);
174:
175: // verify user has 2 roles, admin & editor
176: user = mgr.getUserByUserName(testUser.getUserName());
177: assertNotNull(user);
178: assertEquals(2, user.getRoles().size());
179: assertTrue(user.hasRole("editor"));
180: assertTrue(user.hasRole("admin"));
181:
182: // remove role
183: user.revokeRole("admin");
184: mgr.saveUser(user);
185: TestUtils.endSession(true);
186:
187: // check that role was removed
188: user = null;
189: user = mgr.getUserByUserName(testUser.getUserName());
190: assertNotNull(user);
191: assertEquals(1, user.getRoles().size());
192: assertTrue(user.hasRole("editor"));
193: assertFalse(user.hasRole("admin"));
194:
195: // add role
196: user.grantRole("admin");
197: mgr.saveUser(user);
198: TestUtils.endSession(true);
199:
200: // check that role was added
201: user = null;
202: user = mgr.getUserByUserName(testUser.getUserName());
203: assertNotNull(user);
204: assertEquals(2, user.getRoles().size());
205: assertTrue(user.hasRole("editor"));
206: assertTrue(user.hasRole("admin"));
207:
208: // remove test user
209: TestUtils.teardownUser(testUser.getId());
210: TestUtils.endSession(true);
211: }
212:
213: /**
214: * Test ability to remove a user with a full set of data.
215: */
216: public void testRemoveLoadedUser() throws Exception {
217: // TODO: implement testRemoveLoadedUser
218: }
219:
220: }
|