    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.userdetails.UsernameNotFoundException;
    21  import org.acegisecurity.userdetails.memory.InMemoryDaoImpl;
    22  import org.acegisecurity.userdetails.memory.UserMap;
    23  import org.acegisecurity.userdetails.memory.UserMapEditor;
    24
    25  import java.util.Properties;
    26
    27
    28  /***
    29   * Tests {@link InMemoryDaoImpl}.
    30   *
    31   * @author Ben Alex
    32   * @version $Id: InMemoryDaoTests.java,v 1.1 2005/11/29 13:10:10 benalex Exp $
    33   */
    34  public class InMemoryDaoTests extends TestCase {
    35      //~ Constructors ===========================================================
    36
    37      public InMemoryDaoTests() {
    38          super();
    39      }
    40
    41      public InMemoryDaoTests(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(InMemoryDaoTests.class);
    53      }
    54
    55      public void testLookupFails() throws Exception {
    56          InMemoryDaoImpl dao = new InMemoryDaoImpl();
    57          dao.setUserMap(makeUserMap());
    58          dao.afterPropertiesSet();
    59
    60          try {
    61              dao.loadUserByUsername("UNKNOWN_USER");
    62              fail("Should have thrown UsernameNotFoundException");
    63          } catch (UsernameNotFoundException expected) {
    64              assertTrue(true);
    65          }
    66      }
    67
    68      public void testLookupSuccess() throws Exception {
    69          InMemoryDaoImpl dao = new InMemoryDaoImpl();
    70          dao.setUserMap(makeUserMap());
    71          dao.afterPropertiesSet();
    72          assertEquals("koala", dao.loadUserByUsername("marissa").getPassword());
    73          assertEquals("wombat", dao.loadUserByUsername("scott").getPassword());
    74      }
    75
    76      public void testLookupSuccessWithMixedCase() throws Exception {
    77          InMemoryDaoImpl dao = new InMemoryDaoImpl();
    78          dao.setUserMap(makeUserMap());
    79          dao.afterPropertiesSet();
    80          assertEquals("koala", dao.loadUserByUsername("MaRiSSA").getPassword());
    81          assertEquals("wombat", dao.loadUserByUsername("ScOTt").getPassword());
    82      }
    83
    84      public void testStartupFailsIfUserMapNotSet() throws Exception {
    85          InMemoryDaoImpl dao = new InMemoryDaoImpl();
    86
    87          try {
    88              dao.afterPropertiesSet();
    89              fail("Shoudl have thrown IllegalArgumentException");
    90          } catch (IllegalArgumentException expected) {
    91              assertTrue(true);
    92          }
    93      }
    94
    95      public void testStartupFailsIfUserMapSetToNull() throws Exception {
    96          InMemoryDaoImpl dao = new InMemoryDaoImpl();
    97          dao.setUserMap(null);
    98
    99          try {
    100             dao.afterPropertiesSet();
    101             fail("Shoudl have thrown IllegalArgumentException");
    102         } catch (IllegalArgumentException expected) {
    103             assertTrue(true);
    104         }
    105     }
    106
    107     public void testStartupSuccessIfUserMapSet() throws Exception {
    108         InMemoryDaoImpl dao = new InMemoryDaoImpl();
    109         dao.setUserMap(makeUserMap());
    110         dao.afterPropertiesSet();
    111         assertEquals(2, dao.getUserMap().getUserCount());
    112     }
    113
    114     public void testUseOfExternalPropertiesObject() throws Exception {
    115         InMemoryDaoImpl dao = new InMemoryDaoImpl();
    116         Properties props = new Properties();
    117         props.put("marissa", "koala,ROLE_ONE,ROLE_TWO,enabled");
    118         props.put("scott", "wombat,ROLE_ONE,ROLE_TWO,enabled");
    119         dao.setUserProperties(props);
    120         assertEquals("koala", dao.loadUserByUsername("marissa").getPassword());
    121         assertEquals("wombat", dao.loadUserByUsername("scott").getPassword());
    122     }
    123
    124     private UserMap makeUserMap() {
    125         UserMapEditor editor = new UserMapEditor();
    126         editor.setAsText(
    127             "marissa=koala,ROLE_ONE,ROLE_TWO,enabled\r\nscott=wombat,ROLE_ONE,ROLE_TWO,enabled");
    128
    129         return (UserMap) editor.getValue();
    130     }
    131 }