001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.userdetails.memory;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.userdetails.UsernameNotFoundException;
021: import org.acegisecurity.userdetails.memory.InMemoryDaoImpl;
022: import org.acegisecurity.userdetails.memory.UserMap;
023: import org.acegisecurity.userdetails.memory.UserMapEditor;
024:
025: import java.util.Properties;
026:
027: /**
028: * Tests {@link InMemoryDaoImpl}.
029: *
030: * @author Ben Alex
031: * @version $Id: InMemoryDaoTests.java 1496 2006-05-23 13:38:33Z benalex $
032: */
033: public class InMemoryDaoTests extends TestCase {
034: //~ Constructors ===================================================================================================
035:
036: public InMemoryDaoTests() {
037: super ();
038: }
039:
040: public InMemoryDaoTests(String arg0) {
041: super (arg0);
042: }
043:
044: //~ Methods ========================================================================================================
045:
046: public static void main(String[] args) {
047: junit.textui.TestRunner.run(InMemoryDaoTests.class);
048: }
049:
050: private UserMap makeUserMap() {
051: UserMapEditor editor = new UserMapEditor();
052: editor
053: .setAsText("marissa=koala,ROLE_ONE,ROLE_TWO,enabled\r\nscott=wombat,ROLE_ONE,ROLE_TWO,enabled");
054:
055: return (UserMap) editor.getValue();
056: }
057:
058: public final void setUp() throws Exception {
059: super .setUp();
060: }
061:
062: public void testLookupFails() throws Exception {
063: InMemoryDaoImpl dao = new InMemoryDaoImpl();
064: dao.setUserMap(makeUserMap());
065: dao.afterPropertiesSet();
066:
067: try {
068: dao.loadUserByUsername("UNKNOWN_USER");
069: fail("Should have thrown UsernameNotFoundException");
070: } catch (UsernameNotFoundException expected) {
071: assertTrue(true);
072: }
073: }
074:
075: public void testLookupSuccess() throws Exception {
076: InMemoryDaoImpl dao = new InMemoryDaoImpl();
077: dao.setUserMap(makeUserMap());
078: dao.afterPropertiesSet();
079: assertEquals("koala", dao.loadUserByUsername("marissa")
080: .getPassword());
081: assertEquals("wombat", dao.loadUserByUsername("scott")
082: .getPassword());
083: }
084:
085: public void testLookupSuccessWithMixedCase() throws Exception {
086: InMemoryDaoImpl dao = new InMemoryDaoImpl();
087: dao.setUserMap(makeUserMap());
088: dao.afterPropertiesSet();
089: assertEquals("koala", dao.loadUserByUsername("MaRiSSA")
090: .getPassword());
091: assertEquals("wombat", dao.loadUserByUsername("ScOTt")
092: .getPassword());
093: }
094:
095: public void testStartupFailsIfUserMapNotSet() throws Exception {
096: InMemoryDaoImpl dao = new InMemoryDaoImpl();
097:
098: try {
099: dao.afterPropertiesSet();
100: fail("Shoudl have thrown IllegalArgumentException");
101: } catch (IllegalArgumentException expected) {
102: assertTrue(true);
103: }
104: }
105:
106: public void testStartupFailsIfUserMapSetToNull() throws Exception {
107: InMemoryDaoImpl dao = new InMemoryDaoImpl();
108: dao.setUserMap(null);
109:
110: try {
111: dao.afterPropertiesSet();
112: fail("Shoudl have thrown IllegalArgumentException");
113: } catch (IllegalArgumentException expected) {
114: assertTrue(true);
115: }
116: }
117:
118: public void testStartupSuccessIfUserMapSet() throws Exception {
119: InMemoryDaoImpl dao = new InMemoryDaoImpl();
120: dao.setUserMap(makeUserMap());
121: dao.afterPropertiesSet();
122: assertEquals(2, dao.getUserMap().getUserCount());
123: }
124:
125: public void testUseOfExternalPropertiesObject() throws Exception {
126: InMemoryDaoImpl dao = new InMemoryDaoImpl();
127: Properties props = new Properties();
128: props.put("marissa", "koala,ROLE_ONE,ROLE_TWO,enabled");
129: props.put("scott", "wombat,ROLE_ONE,ROLE_TWO,enabled");
130: dao.setUserProperties(props);
131: assertEquals("koala", dao.loadUserByUsername("marissa")
132: .getPassword());
133: assertEquals("wombat", dao.loadUserByUsername("scott")
134: .getPassword());
135: }
136: }
|