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
25 import org.acegisecurity.providers.TestingAuthenticationToken;
26
27
28
29
30
31
32
33
34 public class AnonymousAuthenticationProviderTests extends TestCase {
35
36
37 public AnonymousAuthenticationProviderTests() {
38 super();
39 }
40
41 public AnonymousAuthenticationProviderTests(String arg0) {
42 super(arg0);
43 }
44
45
46
47 public static void main(String[] args) {
48 junit.textui.TestRunner.run(AnonymousAuthenticationProviderTests.class);
49 }
50
51 public final void setUp() throws Exception {
52 super.setUp();
53 }
54
55 public void testDetectsAnInvalidKey() throws Exception {
56 AnonymousAuthenticationProvider aap = new AnonymousAuthenticationProvider();
57 aap.setKey("qwerty");
58
59 AnonymousAuthenticationToken token = new AnonymousAuthenticationToken("WRONG_KEY", "Test",
60 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
61
62 try {
63 Authentication result = aap.authenticate(token);
64 fail("Should have thrown BadCredentialsException");
65 } catch (BadCredentialsException expected) {
66 assertEquals("The presented AnonymousAuthenticationToken does not contain the expected key",
67 expected.getMessage());
68 }
69 }
70
71 public void testDetectsMissingKey() throws Exception {
72 AnonymousAuthenticationProvider aap = new AnonymousAuthenticationProvider();
73
74 try {
75 aap.afterPropertiesSet();
76 fail("Should have thrown IllegalArgumentException");
77 } catch (IllegalArgumentException expected) {
78 assertTrue(true);
79 }
80 }
81
82 public void testGettersSetters() throws Exception {
83 AnonymousAuthenticationProvider aap = new AnonymousAuthenticationProvider();
84 aap.setKey("qwerty");
85 aap.afterPropertiesSet();
86 assertEquals("qwerty", aap.getKey());
87 }
88
89 public void testIgnoresClassesItDoesNotSupport() throws Exception {
90 AnonymousAuthenticationProvider aap = new AnonymousAuthenticationProvider();
91 aap.setKey("qwerty");
92
93 TestingAuthenticationToken token = new TestingAuthenticationToken("user", "password",
94 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_A")});
95 assertFalse(aap.supports(TestingAuthenticationToken.class));
96
97
98 assertNull(aap.authenticate(token));
99 }
100
101 public void testNormalOperation() throws Exception {
102 AnonymousAuthenticationProvider aap = new AnonymousAuthenticationProvider();
103 aap.setKey("qwerty");
104
105 AnonymousAuthenticationToken token = new AnonymousAuthenticationToken("qwerty", "Test",
106 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl("ROLE_TWO")});
107
108 Authentication result = aap.authenticate(token);
109
110 assertEquals(result, token);
111 }
112
113 public void testSupports() {
114 AnonymousAuthenticationProvider aap = new AnonymousAuthenticationProvider();
115 assertTrue(aap.supports(AnonymousAuthenticationToken.class));
116 assertFalse(aap.supports(TestingAuthenticationToken.class));
117 }
118 }