    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.providers;
    17
    18  import junit.framework.TestCase;
    19
    20  import org.acegisecurity.GrantedAuthority;
    21  import org.acegisecurity.GrantedAuthorityImpl;
    22
    23
    24  /***
    25   * Tests {@link UsernamePasswordAuthenticationToken}.
    26   *
    27   * @author Ben Alex
    28   * @version $Id: UsernamePasswordAuthenticationTokenTests.java,v 1.3 2005/11/17 00:55:48 benalex Exp $
    29   */
    30  public class UsernamePasswordAuthenticationTokenTests extends TestCase {
    31      //~ Constructors ===========================================================
    32
    33      public UsernamePasswordAuthenticationTokenTests() {
    34          super();
    35      }
    36
    37      public UsernamePasswordAuthenticationTokenTests(String arg0) {
    38          super(arg0);
    39      }
    40
    41      //~ Methods ================================================================
    42
    43      public final void setUp() throws Exception {
    44          super.setUp();
    45      }
    46
    47      public static void main(String[] args) {
    48          junit.textui.TestRunner.run(UsernamePasswordAuthenticationTokenTests.class);
    49      }
    50
    51      public void testAuthenticated() {
    52          UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("Test",
    53                  "Password", null);
    54
    55          // check default given we passed some GrantedAuthorty[]s (well, we passed null)
    56          assertTrue(token.isAuthenticated());
    57
    58          // check explicit set to untrusted (we can safely go from trusted to untrusted, but not the reverse)
    59          token.setAuthenticated(false);
    60          assertTrue(!token.isAuthenticated());
    61
    62          // Now let's create a UsernamePasswordAuthenticationToken without any GrantedAuthorty[]s (different constructor)
    63          token = new UsernamePasswordAuthenticationToken("Test", "Password");
    64
    65          assertTrue(!token.isAuthenticated());
    66
    67          // check we're allowed to still set it to untrusted
    68          token.setAuthenticated(false);
    69          assertTrue(!token.isAuthenticated());
    70
    71          // check denied changing it to trusted
    72          try {
    73              token.setAuthenticated(true);
    74              fail("Should have prohibited setAuthenticated(true)");
    75          } catch (IllegalArgumentException expected) {
    76              assertTrue(true);
    77          }
    78      }
    79
    80      public void testGetters() {
    81          UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("Test",
    82                  "Password",
    83                  new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
    84                          "ROLE_TWO")});
    85          assertEquals("Test", token.getPrincipal());
    86          assertEquals("Password", token.getCredentials());
    87          assertEquals("ROLE_ONE", token.getAuthorities()[0].getAuthority());
    88          assertEquals("ROLE_TWO", token.getAuthorities()[1].getAuthority());
    89      }
    90
    91      public void testNoArgConstructor() {
    92          try {
    93              new UsernamePasswordAuthenticationToken();
    94              fail("Should have thrown IllegalArgumentException");
    95          } catch (IllegalArgumentException expected) {
    96              assertTrue(true);
    97          }
    98      }
    99  }