1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.afterinvocation;
17
18 import junit.framework.TestCase;
19
20 import org.acegisecurity.AccessDeniedException;
21 import org.acegisecurity.Authentication;
22 import org.acegisecurity.ConfigAttribute;
23 import org.acegisecurity.ConfigAttributeDefinition;
24 import org.acegisecurity.SecurityConfig;
25
26 import org.acegisecurity.intercept.web.FilterInvocation;
27
28 import org.acegisecurity.util.SimpleMethodInvocation;
29
30 import org.aopalliance.intercept.MethodInvocation;
31
32 import java.util.List;
33 import java.util.Vector;
34
35
36
37
38
39
40
41
42 public class AfterInvocationProviderManagerTests extends TestCase {
43
44
45 public AfterInvocationProviderManagerTests() {
46 super();
47 }
48
49 public AfterInvocationProviderManagerTests(String arg0) {
50 super(arg0);
51 }
52
53
54
55 public static void main(String[] args) {
56 junit.textui.TestRunner.run(AfterInvocationProviderManagerTests.class);
57 }
58
59 public final void setUp() throws Exception {
60 super.setUp();
61 }
62
63 public void testCorrectOperation() throws Exception {
64 AfterInvocationProviderManager manager = new AfterInvocationProviderManager();
65 List list = new Vector();
66 list.add(new MockAfterInvocationProvider("swap1", MethodInvocation.class, new SecurityConfig("GIVE_ME_SWAP1")));
67 list.add(new MockAfterInvocationProvider("swap2", MethodInvocation.class, new SecurityConfig("GIVE_ME_SWAP2")));
68 list.add(new MockAfterInvocationProvider("swap3", MethodInvocation.class, new SecurityConfig("GIVE_ME_SWAP3")));
69 manager.setProviders(list);
70 assertEquals(list, manager.getProviders());
71 manager.afterPropertiesSet();
72
73 ConfigAttributeDefinition attr1 = new ConfigAttributeDefinition();
74 attr1.addConfigAttribute(new SecurityConfig("GIVE_ME_SWAP1"));
75
76 ConfigAttributeDefinition attr2 = new ConfigAttributeDefinition();
77 attr2.addConfigAttribute(new SecurityConfig("GIVE_ME_SWAP2"));
78
79 ConfigAttributeDefinition attr3 = new ConfigAttributeDefinition();
80 attr3.addConfigAttribute(new SecurityConfig("GIVE_ME_SWAP3"));
81
82 ConfigAttributeDefinition attr2and3 = new ConfigAttributeDefinition();
83 attr2and3.addConfigAttribute(new SecurityConfig("GIVE_ME_SWAP2"));
84 attr2and3.addConfigAttribute(new SecurityConfig("GIVE_ME_SWAP3"));
85
86 ConfigAttributeDefinition attr4 = new ConfigAttributeDefinition();
87 attr4.addConfigAttribute(new SecurityConfig("NEVER_CAUSES_SWAP"));
88
89 assertEquals("swap1", manager.decide(null, new SimpleMethodInvocation(), attr1, "content-before-swapping"));
90
91 assertEquals("swap2", manager.decide(null, new SimpleMethodInvocation(), attr2, "content-before-swapping"));
92
93 assertEquals("swap3", manager.decide(null, new SimpleMethodInvocation(), attr3, "content-before-swapping"));
94
95 assertEquals("content-before-swapping",
96 manager.decide(null, new SimpleMethodInvocation(), attr4, "content-before-swapping"));
97
98 assertEquals("swap3", manager.decide(null, new SimpleMethodInvocation(), attr2and3, "content-before-swapping"));
99 }
100
101 public void testRejectsEmptyProvidersList() {
102 AfterInvocationProviderManager manager = new AfterInvocationProviderManager();
103 List list = new Vector();
104
105 try {
106 manager.setProviders(list);
107 fail("Should have thrown IllegalArgumentException");
108 } catch (IllegalArgumentException expected) {
109 assertTrue(true);
110 }
111 }
112
113 public void testRejectsNonAfterInvocationProviders() {
114 AfterInvocationProviderManager manager = new AfterInvocationProviderManager();
115 List list = new Vector();
116 list.add(new MockAfterInvocationProvider("swap1", MethodInvocation.class, new SecurityConfig("GIVE_ME_SWAP1")));
117 list.add(new Integer(45));
118 list.add(new MockAfterInvocationProvider("swap3", MethodInvocation.class, new SecurityConfig("GIVE_ME_SWAP3")));
119
120 try {
121 manager.setProviders(list);
122 fail("Should have thrown IllegalArgumentException");
123 } catch (IllegalArgumentException expected) {
124 assertTrue(true);
125 }
126 }
127
128 public void testRejectsNullProvidersList() throws Exception {
129 AfterInvocationProviderManager manager = new AfterInvocationProviderManager();
130
131 try {
132 manager.afterPropertiesSet();
133 fail("Should have thrown IllegalArgumentException");
134 } catch (IllegalArgumentException expected) {
135 assertTrue(true);
136 }
137 }
138
139 public void testSupportsConfigAttributeIteration()
140 throws Exception {
141 AfterInvocationProviderManager manager = new AfterInvocationProviderManager();
142 List list = new Vector();
143 list.add(new MockAfterInvocationProvider("swap1", MethodInvocation.class, new SecurityConfig("GIVE_ME_SWAP1")));
144 list.add(new MockAfterInvocationProvider("swap2", MethodInvocation.class, new SecurityConfig("GIVE_ME_SWAP2")));
145 list.add(new MockAfterInvocationProvider("swap3", MethodInvocation.class, new SecurityConfig("GIVE_ME_SWAP3")));
146 manager.setProviders(list);
147 manager.afterPropertiesSet();
148
149 assertFalse(manager.supports(new SecurityConfig("UNKNOWN_ATTRIB")));
150 assertTrue(manager.supports(new SecurityConfig("GIVE_ME_SWAP2")));
151 }
152
153 public void testSupportsSecureObjectIteration() throws Exception {
154 AfterInvocationProviderManager manager = new AfterInvocationProviderManager();
155 List list = new Vector();
156 list.add(new MockAfterInvocationProvider("swap1", MethodInvocation.class, new SecurityConfig("GIVE_ME_SWAP1")));
157 list.add(new MockAfterInvocationProvider("swap2", MethodInvocation.class, new SecurityConfig("GIVE_ME_SWAP2")));
158 list.add(new MockAfterInvocationProvider("swap3", MethodInvocation.class, new SecurityConfig("GIVE_ME_SWAP3")));
159 manager.setProviders(list);
160 manager.afterPropertiesSet();
161
162 assertFalse(manager.supports(FilterInvocation.class));
163 assertTrue(manager.supports(MethodInvocation.class));
164 }
165
166
167
168
169
170
171
172 private class MockAfterInvocationProvider implements AfterInvocationProvider {
173 private Class secureObject;
174 private ConfigAttribute configAttribute;
175 private Object forceReturnObject;
176
177 public MockAfterInvocationProvider(Object forceReturnObject, Class secureObject, ConfigAttribute configAttribute) {
178 this.forceReturnObject = forceReturnObject;
179 this.secureObject = secureObject;
180 this.configAttribute = configAttribute;
181 }
182
183 private MockAfterInvocationProvider() {}
184
185 public Object decide(Authentication authentication, Object object, ConfigAttributeDefinition config,
186 Object returnedObject) throws AccessDeniedException {
187 if (config.contains(configAttribute)) {
188 return forceReturnObject;
189 }
190
191 return returnedObject;
192 }
193
194 public boolean supports(Class clazz) {
195 return secureObject.isAssignableFrom(clazz);
196 }
197
198 public boolean supports(ConfigAttribute attribute) {
199 return attribute.equals(configAttribute);
200 }
201 }
202 }