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.GrantedAuthority;
021: import org.acegisecurity.GrantedAuthorityImpl;
022:
023: import org.acegisecurity.userdetails.User;
024: import org.acegisecurity.userdetails.UserDetails;
025: import org.acegisecurity.userdetails.UsernameNotFoundException;
026: import org.acegisecurity.userdetails.memory.UserMap;
027:
028: /**
029: * Tests {@link UserMap}.
030: *
031: * @author Ben Alex
032: * @version $Id: UserMapTests.java 1496 2006-05-23 13:38:33Z benalex $
033: */
034: public class UserMapTests extends TestCase {
035: //~ Constructors ===================================================================================================
036:
037: public UserMapTests() {
038: super ();
039: }
040:
041: public UserMapTests(String arg0) {
042: super (arg0);
043: }
044:
045: //~ Methods ========================================================================================================
046:
047: public static void main(String[] args) {
048: junit.textui.TestRunner.run(UserMapTests.class);
049: }
050:
051: public final void setUp() throws Exception {
052: super .setUp();
053: }
054:
055: public void testAddAndRetrieveUser() {
056: UserDetails marissa = new User("marissa", "koala", true, true,
057: true, true, new GrantedAuthority[] {
058: new GrantedAuthorityImpl("ROLE_ONE"),
059: new GrantedAuthorityImpl("ROLE_TWO") });
060: UserDetails scott = new User("scott", "wombat", true, true,
061: true, true, new GrantedAuthority[] {
062: new GrantedAuthorityImpl("ROLE_ONE"),
063: new GrantedAuthorityImpl("ROLE_THREE") });
064: UserDetails peter = new User("peter", "opal", true, true, true,
065: true, new GrantedAuthority[] {
066: new GrantedAuthorityImpl("ROLE_ONE"),
067: new GrantedAuthorityImpl("ROLE_FOUR") });
068: UserMap map = new UserMap();
069: map.addUser(marissa);
070: map.addUser(scott);
071: map.addUser(peter);
072: assertEquals(3, map.getUserCount());
073:
074: assertEquals(marissa, map.getUser("marissa"));
075: assertEquals(scott, map.getUser("scott"));
076: assertEquals(peter, map.getUser("peter"));
077: }
078:
079: public void testNullUserCannotBeAdded() {
080: UserMap map = new UserMap();
081: assertEquals(0, map.getUserCount());
082:
083: try {
084: map.addUser(null);
085: fail("Should have thrown IllegalArgumentException");
086: } catch (IllegalArgumentException expected) {
087: assertTrue(true);
088: }
089: }
090:
091: public void testUnknownUserIsNotRetrieved() {
092: UserDetails marissa = new User("marissa", "koala", true, true,
093: true, true, new GrantedAuthority[] {
094: new GrantedAuthorityImpl("ROLE_ONE"),
095: new GrantedAuthorityImpl("ROLE_TWO") });
096: UserMap map = new UserMap();
097: assertEquals(0, map.getUserCount());
098: map.addUser(marissa);
099: assertEquals(1, map.getUserCount());
100:
101: try {
102: map.getUser("scott");
103: fail("Should have thrown UsernameNotFoundException");
104: } catch (UsernameNotFoundException expected) {
105: assertTrue(true);
106: }
107: }
108: }
|