1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.providers.anonymous;
17
18 import junit.framework.TestCase;
19
20 import org.acegisecurity.Authentication;
21 import org.acegisecurity.BadCredentialsException;
22 import org.acegisecurity.GrantedAuthority;
23 import org.acegisecurity.GrantedAuthorityImpl;
24 import org.acegisecurity.providers.TestingAuthenticationToken;
25
26
27 /***
28 * Tests {@link AnonymousAuthenticationProvider}.
29 *
30 * @author Ben Alex
31 * @version $Id: AnonymousAuthenticationProviderTests.java,v 1.4 2005/11/30 01:23:35 benalex Exp $
32 */
33 public class AnonymousAuthenticationProviderTests extends TestCase {
34
35
36 public AnonymousAuthenticationProviderTests() {
37 super();
38 }
39
40 public AnonymousAuthenticationProviderTests(String arg0) {
41 super(arg0);
42 }
43
44
45
46 public final void setUp() throws Exception {
47 super.setUp();
48 }
49
50 public static void main(String[] args) {
51 junit.textui.TestRunner.run(AnonymousAuthenticationProviderTests.class);
52 }
53
54 public void testDetectsAnInvalidKey() throws Exception {
55 AnonymousAuthenticationProvider aap = new AnonymousAuthenticationProvider();
56 aap.setKey("qwerty");
57
58 AnonymousAuthenticationToken token = new AnonymousAuthenticationToken("WRONG_KEY",
59 "Test",
60 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
61 "ROLE_TWO")});
62
63 try {
64 Authentication result = aap.authenticate(token);
65 fail("Should have thrown BadCredentialsException");
66 } catch (BadCredentialsException expected) {
67 assertEquals("The presented AnonymousAuthenticationToken does not contain the expected key",
68 expected.getMessage());
69 }
70 }
71
72 public void testDetectsMissingKey() throws Exception {
73 AnonymousAuthenticationProvider aap = new AnonymousAuthenticationProvider();
74
75 try {
76 aap.afterPropertiesSet();
77 fail("Should have thrown IllegalArgumentException");
78 } catch (IllegalArgumentException expected) {
79 assertTrue(true);
80 }
81 }
82
83 public void testGettersSetters() throws Exception {
84 AnonymousAuthenticationProvider aap = new AnonymousAuthenticationProvider();
85 aap.setKey("qwerty");
86 aap.afterPropertiesSet();
87 assertEquals("qwerty", aap.getKey());
88 }
89
90 public void testIgnoresClassesItDoesNotSupport() throws Exception {
91 AnonymousAuthenticationProvider aap = new AnonymousAuthenticationProvider();
92 aap.setKey("qwerty");
93
94 TestingAuthenticationToken token = new TestingAuthenticationToken("user",
95 "password",
96 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_A")});
97 assertFalse(aap.supports(TestingAuthenticationToken.class));
98
99
100 assertNull(aap.authenticate(token));
101 }
102
103 public void testNormalOperation() throws Exception {
104 AnonymousAuthenticationProvider aap = new AnonymousAuthenticationProvider();
105 aap.setKey("qwerty");
106
107 AnonymousAuthenticationToken token = new AnonymousAuthenticationToken("qwerty",
108 "Test",
109 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
110 "ROLE_TWO")});
111
112 Authentication result = aap.authenticate(token);
113
114 assertEquals(result, token);
115 }
116
117 public void testSupports() {
118 AnonymousAuthenticationProvider aap = new AnonymousAuthenticationProvider();
119 assertTrue(aap.supports(AnonymousAuthenticationToken.class));
120 assertFalse(aap.supports(TestingAuthenticationToken.class));
121 }
122 }