    1   /* Copyright 2004, 2005 Acegi Technology Pty Limited
    2    *
    3    * Licensed under the Apache License, Version 2.0 (the "License");
    4    * you may not use this file except in compliance with the License.
    5    * You may obtain a copy of the License at
    6    *
    7    *     http://www.apache.org/licenses/LICENSE-2.0
    8    *
    9    * Unless required by applicable law or agreed to in writing, software
    10   * distributed under the License is distributed on an "AS IS" BASIS,
    11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12   * See the License for the specific language governing permissions and
    13   * limitations under the License.
    14   */
    15
    16  package org.acegisecurity.userdetails.memory;
    17
    18  import junit.framework.TestCase;
    19
    20  import org.acegisecurity.GrantedAuthority;
    21  import org.acegisecurity.GrantedAuthorityImpl;
    22  import org.acegisecurity.userdetails.User;
    23  import org.acegisecurity.userdetails.UserDetails;
    24  import org.acegisecurity.userdetails.UsernameNotFoundException;
    25  import org.acegisecurity.userdetails.memory.UserMap;
    26
    27
    28  /***
    29   * Tests {@link UserMap}.
    30   *
    31   * @author Ben Alex
    32   * @version $Id: UserMapTests.java,v 1.1 2005/11/29 13:10:10 benalex Exp $
    33   */
    34  public class UserMapTests extends TestCase {
    35      //~ Constructors ===========================================================
    36
    37      public UserMapTests() {
    38          super();
    39      }
    40
    41      public UserMapTests(String arg0) {
    42          super(arg0);
    43      }
    44
    45      //~ Methods ================================================================
    46
    47      public final void setUp() throws Exception {
    48          super.setUp();
    49      }
    50
    51      public static void main(String[] args) {
    52          junit.textui.TestRunner.run(UserMapTests.class);
    53      }
    54
    55      public void testAddAndRetrieveUser() {
    56          UserDetails marissa = new User("marissa", "koala", true, true, true,
    57                  true,
    58                  new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
    59                          "ROLE_TWO")});
    60          UserDetails scott = new User("scott", "wombat", true, true, true, true,
    61                  new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
    62                          "ROLE_THREE")});
    63          UserDetails peter = new User("peter", "opal", true, true, true, true,
    64                  new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
    65                          "ROLE_FOUR")});
    66          UserMap map = new UserMap();
    67          map.addUser(marissa);
    68          map.addUser(scott);
    69          map.addUser(peter);
    70          assertEquals(3, map.getUserCount());
    71
    72          assertEquals(marissa, map.getUser("marissa"));
    73          assertEquals(scott, map.getUser("scott"));
    74          assertEquals(peter, map.getUser("peter"));
    75      }
    76
    77      public void testNullUserCannotBeAdded() {
    78          UserMap map = new UserMap();
    79          assertEquals(0, map.getUserCount());
    80
    81          try {
    82              map.addUser(null);
    83              fail("Should have thrown IllegalArgumentException");
    84          } catch (IllegalArgumentException expected) {
    85              assertTrue(true);
    86          }
    87      }
    88
    89      public void testUnknownUserIsNotRetrieved() {
    90          UserDetails marissa = new User("marissa", "koala", true, true, true,
    91                  true,
    92                  new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
    93                          "ROLE_TWO")});
    94          UserMap map = new UserMap();
    95          assertEquals(0, map.getUserCount());
    96          map.addUser(marissa);
    97          assertEquals(1, map.getUserCount());
    98
    99          try {
    100             map.getUser("scott");
    101             fail("Should have thrown UsernameNotFoundException");
    102         } catch (UsernameNotFoundException expected) {
    103             assertTrue(true);
    104         }
    105     }
    106 }