1
2
3
4
5
6
7
8
9
10
11
12
13
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 import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
23
24
25 /***
26 * Tests {@link RunAsUserToken}.
27 *
28 * @author Ben Alex
29 * @version $Id: RunAsUserTokenTests.java,v 1.3 2005/11/17 00:56:28 benalex Exp $
30 */
31 public class RunAsUserTokenTests extends TestCase {
32
33
34 public RunAsUserTokenTests() {
35 super();
36 }
37
38 public RunAsUserTokenTests(String arg0) {
39 super(arg0);
40 }
41
42
43
44 public final void setUp() throws Exception {
45 super.setUp();
46 }
47
48 public static void main(String[] args) {
49 junit.textui.TestRunner.run(RunAsUserTokenTests.class);
50 }
51
52 public void testAuthenticationSetting() {
53 RunAsUserToken token = new RunAsUserToken("my_password", "Test",
54 "Password",
55 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
56 "ROLE_TWO")}, UsernamePasswordAuthenticationToken.class);
57 assertTrue(token.isAuthenticated());
58 token.setAuthenticated(false);
59 assertTrue(!token.isAuthenticated());
60 }
61
62 public void testGetters() {
63 RunAsUserToken token = new RunAsUserToken("my_password", "Test",
64 "Password",
65 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
66 "ROLE_TWO")}, UsernamePasswordAuthenticationToken.class);
67 assertEquals("Test", token.getPrincipal());
68 assertEquals("Password", token.getCredentials());
69 assertEquals("my_password".hashCode(), token.getKeyHash());
70 assertEquals(UsernamePasswordAuthenticationToken.class,
71 token.getOriginalAuthentication());
72 }
73
74 public void testNoArgsConstructor() {
75 try {
76 new RunAsUserToken();
77 fail("Should have thrown IllegalArgumentException");
78 } catch (IllegalArgumentException expected) {
79 assertTrue(true);
80 }
81 }
82
83 public void testToString() {
84 RunAsUserToken token = new RunAsUserToken("my_password", "Test",
85 "Password",
86 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
87 "ROLE_TWO")}, UsernamePasswordAuthenticationToken.class);
88 assertTrue(token.toString().lastIndexOf("Original Class:") != -1);
89 }
90 }