01: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
02: *
03: * Licensed under the Apache License, Version 2.0 (the "License");
04: * you may not use this file except in compliance with the License.
05: * You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * 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.runas;
17:
18: import junit.framework.TestCase;
19:
20: import org.acegisecurity.GrantedAuthority;
21: import org.acegisecurity.GrantedAuthorityImpl;
22:
23: import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
24:
25: /**
26: * Tests {@link RunAsUserToken}.
27: *
28: * @author Ben Alex
29: * @version $Id: RunAsUserTokenTests.java 1496 2006-05-23 13:38:33Z benalex $
30: */
31: public class RunAsUserTokenTests extends TestCase {
32: //~ Constructors ===================================================================================================
33:
34: public RunAsUserTokenTests() {
35: super ();
36: }
37:
38: public RunAsUserTokenTests(String arg0) {
39: super (arg0);
40: }
41:
42: //~ Methods ========================================================================================================
43:
44: public static void main(String[] args) {
45: junit.textui.TestRunner.run(RunAsUserTokenTests.class);
46: }
47:
48: public final void setUp() throws Exception {
49: super .setUp();
50: }
51:
52: public void testAuthenticationSetting() {
53: RunAsUserToken token = new RunAsUserToken("my_password",
54: "Test", "Password", new GrantedAuthority[] {
55: new GrantedAuthorityImpl("ROLE_ONE"),
56: new GrantedAuthorityImpl("ROLE_TWO") },
57: UsernamePasswordAuthenticationToken.class);
58: assertTrue(token.isAuthenticated());
59: token.setAuthenticated(false);
60: assertTrue(!token.isAuthenticated());
61: }
62:
63: public void testGetters() {
64: RunAsUserToken token = new RunAsUserToken("my_password",
65: "Test", "Password", new GrantedAuthority[] {
66: new GrantedAuthorityImpl("ROLE_ONE"),
67: new GrantedAuthorityImpl("ROLE_TWO") },
68: UsernamePasswordAuthenticationToken.class);
69: assertEquals("Test", token.getPrincipal());
70: assertEquals("Password", token.getCredentials());
71: assertEquals("my_password".hashCode(), token.getKeyHash());
72: assertEquals(UsernamePasswordAuthenticationToken.class, token
73: .getOriginalAuthentication());
74: }
75:
76: public void testNoArgConstructorDoesntExist() {
77: Class clazz = RunAsUserToken.class;
78:
79: try {
80: clazz.getDeclaredConstructor((Class[]) null);
81: fail("Should have thrown NoSuchMethodException");
82: } catch (NoSuchMethodException expected) {
83: assertTrue(true);
84: }
85: }
86:
87: public void testToString() {
88: RunAsUserToken token = new RunAsUserToken("my_password",
89: "Test", "Password", new GrantedAuthority[] {
90: new GrantedAuthorityImpl("ROLE_ONE"),
91: new GrantedAuthorityImpl("ROLE_TWO") },
92: UsernamePasswordAuthenticationToken.class);
93: assertTrue(token.toString().lastIndexOf("Original Class:") != -1);
94: }
95: }
|