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.providers;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.GrantedAuthority;
021: import org.acegisecurity.GrantedAuthorityImpl;
022:
023: /**
024: * Tests {@link UsernamePasswordAuthenticationToken}.
025: *
026: * @author Ben Alex
027: * @version $Id: UsernamePasswordAuthenticationTokenTests.java 1496 2006-05-23 13:38:33Z benalex $
028: */
029: public class UsernamePasswordAuthenticationTokenTests extends TestCase {
030: //~ Constructors ===================================================================================================
031:
032: public UsernamePasswordAuthenticationTokenTests() {
033: super ();
034: }
035:
036: public UsernamePasswordAuthenticationTokenTests(String arg0) {
037: super (arg0);
038: }
039:
040: //~ Methods ========================================================================================================
041:
042: public static void main(String[] args) {
043: junit.textui.TestRunner
044: .run(UsernamePasswordAuthenticationTokenTests.class);
045: }
046:
047: public final void setUp() throws Exception {
048: super .setUp();
049: }
050:
051: public void testAuthenticated() {
052: UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
053: "Test", "Password", null);
054:
055: // check default given we passed some GrantedAuthorty[]s (well, we passed null)
056: assertTrue(token.isAuthenticated());
057:
058: // check explicit set to untrusted (we can safely go from trusted to untrusted, but not the reverse)
059: token.setAuthenticated(false);
060: assertTrue(!token.isAuthenticated());
061:
062: // Now let's create a UsernamePasswordAuthenticationToken without any GrantedAuthorty[]s (different constructor)
063: token = new UsernamePasswordAuthenticationToken("Test",
064: "Password");
065:
066: assertTrue(!token.isAuthenticated());
067:
068: // check we're allowed to still set it to untrusted
069: token.setAuthenticated(false);
070: assertTrue(!token.isAuthenticated());
071:
072: // check denied changing it to trusted
073: try {
074: token.setAuthenticated(true);
075: fail("Should have prohibited setAuthenticated(true)");
076: } catch (IllegalArgumentException expected) {
077: assertTrue(true);
078: }
079: }
080:
081: public void testGetters() {
082: UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
083: "Test", "Password", new GrantedAuthority[] {
084: new GrantedAuthorityImpl("ROLE_ONE"),
085: new GrantedAuthorityImpl("ROLE_TWO") });
086: assertEquals("Test", token.getPrincipal());
087: assertEquals("Password", token.getCredentials());
088: assertEquals("ROLE_ONE", token.getAuthorities()[0]
089: .getAuthority());
090: assertEquals("ROLE_TWO", token.getAuthorities()[1]
091: .getAuthority());
092: }
093:
094: public void testNoArgConstructorDoesntExist() {
095: Class clazz = UsernamePasswordAuthenticationToken.class;
096:
097: try {
098: clazz.getDeclaredConstructor((Class[]) null);
099: fail("Should have thrown NoSuchMethodException");
100: } catch (NoSuchMethodException expected) {
101: assertTrue(true);
102: }
103: }
104: }
|