1   /* Copyright 2004 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * 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.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  import org.acegisecurity.intercept.web.FilterInvocation;
26  import org.acegisecurity.util.SimpleMethodInvocation;
27  
28  import org.aopalliance.intercept.MethodInvocation;
29  
30  import java.util.List;
31  import java.util.Vector;
32  
33  
34  /***
35   * Tests {@link AfterInvocationProviderManager}.
36   *
37   * @author Ben Alex
38   * @version $Id: AfterInvocationProviderManagerTests.java,v 1.3 2005/11/25 04:17:24 benalex Exp $
39   */
40  public class AfterInvocationProviderManagerTests extends TestCase {
41      //~ Constructors ===========================================================
42  
43      public AfterInvocationProviderManagerTests() {
44          super();
45      }
46  
47      public AfterInvocationProviderManagerTests(String arg0) {
48          super(arg0);
49      }
50  
51      //~ Methods ================================================================
52  
53      public final void setUp() throws Exception {
54          super.setUp();
55      }
56  
57      public static void main(String[] args) {
58          junit.textui.TestRunner.run(AfterInvocationProviderManagerTests.class);
59      }
60  
61      public void testCorrectOperation() throws Exception {
62          AfterInvocationProviderManager manager = new AfterInvocationProviderManager();
63          List list = new Vector();
64          list.add(new MockAfterInvocationProvider("swap1",
65                  MethodInvocation.class, new SecurityConfig("GIVE_ME_SWAP1")));
66          list.add(new MockAfterInvocationProvider("swap2",
67                  MethodInvocation.class, new SecurityConfig("GIVE_ME_SWAP2")));
68          list.add(new MockAfterInvocationProvider("swap3",
69                  MethodInvocation.class, new SecurityConfig("GIVE_ME_SWAP3")));
70          manager.setProviders(list);
71          assertEquals(list, manager.getProviders());
72          manager.afterPropertiesSet();
73  
74          ConfigAttributeDefinition attr1 = new ConfigAttributeDefinition();
75          attr1.addConfigAttribute(new SecurityConfig("GIVE_ME_SWAP1"));
76  
77          ConfigAttributeDefinition attr2 = new ConfigAttributeDefinition();
78          attr2.addConfigAttribute(new SecurityConfig("GIVE_ME_SWAP2"));
79  
80          ConfigAttributeDefinition attr3 = new ConfigAttributeDefinition();
81          attr3.addConfigAttribute(new SecurityConfig("GIVE_ME_SWAP3"));
82  
83          ConfigAttributeDefinition attr2and3 = new ConfigAttributeDefinition();
84          attr2and3.addConfigAttribute(new SecurityConfig("GIVE_ME_SWAP2"));
85          attr2and3.addConfigAttribute(new SecurityConfig("GIVE_ME_SWAP3"));
86  
87          ConfigAttributeDefinition attr4 = new ConfigAttributeDefinition();
88          attr4.addConfigAttribute(new SecurityConfig("NEVER_CAUSES_SWAP"));
89  
90          assertEquals("swap1",
91              manager.decide(null, new SimpleMethodInvocation(), attr1,
92                  "content-before-swapping"));
93  
94          assertEquals("swap2",
95              manager.decide(null, new SimpleMethodInvocation(), attr2,
96                  "content-before-swapping"));
97  
98          assertEquals("swap3",
99              manager.decide(null, new SimpleMethodInvocation(), attr3,
100                 "content-before-swapping"));
101 
102         assertEquals("content-before-swapping",
103             manager.decide(null, new SimpleMethodInvocation(), attr4,
104                 "content-before-swapping"));
105 
106         assertEquals("swap3",
107             manager.decide(null, new SimpleMethodInvocation(), attr2and3,
108                 "content-before-swapping"));
109     }
110 
111     public void testRejectsEmptyProvidersList() {
112         AfterInvocationProviderManager manager = new AfterInvocationProviderManager();
113         List list = new Vector();
114 
115         try {
116             manager.setProviders(list);
117             fail("Should have thrown IllegalArgumentException");
118         } catch (IllegalArgumentException expected) {
119             assertTrue(true);
120         }
121     }
122 
123     public void testRejectsNonAfterInvocationProviders() {
124         AfterInvocationProviderManager manager = new AfterInvocationProviderManager();
125         List list = new Vector();
126         list.add(new MockAfterInvocationProvider("swap1",
127                 MethodInvocation.class, new SecurityConfig("GIVE_ME_SWAP1")));
128         list.add(new Integer(45));
129         list.add(new MockAfterInvocationProvider("swap3",
130                 MethodInvocation.class, new SecurityConfig("GIVE_ME_SWAP3")));
131 
132         try {
133             manager.setProviders(list);
134             fail("Should have thrown IllegalArgumentException");
135         } catch (IllegalArgumentException expected) {
136             assertTrue(true);
137         }
138     }
139 
140     public void testRejectsNullProvidersList() throws Exception {
141         AfterInvocationProviderManager manager = new AfterInvocationProviderManager();
142 
143         try {
144             manager.afterPropertiesSet();
145             fail("Should have thrown IllegalArgumentException");
146         } catch (IllegalArgumentException expected) {
147             assertTrue(true);
148         }
149     }
150 
151     public void testSupportsConfigAttributeIteration()
152         throws Exception {
153         AfterInvocationProviderManager manager = new AfterInvocationProviderManager();
154         List list = new Vector();
155         list.add(new MockAfterInvocationProvider("swap1",
156                 MethodInvocation.class, new SecurityConfig("GIVE_ME_SWAP1")));
157         list.add(new MockAfterInvocationProvider("swap2",
158                 MethodInvocation.class, new SecurityConfig("GIVE_ME_SWAP2")));
159         list.add(new MockAfterInvocationProvider("swap3",
160                 MethodInvocation.class, new SecurityConfig("GIVE_ME_SWAP3")));
161         manager.setProviders(list);
162         manager.afterPropertiesSet();
163 
164         assertFalse(manager.supports(new SecurityConfig("UNKNOWN_ATTRIB")));
165         assertTrue(manager.supports(new SecurityConfig("GIVE_ME_SWAP2")));
166     }
167 
168     public void testSupportsSecureObjectIteration() throws Exception {
169         AfterInvocationProviderManager manager = new AfterInvocationProviderManager();
170         List list = new Vector();
171         list.add(new MockAfterInvocationProvider("swap1",
172                 MethodInvocation.class, new SecurityConfig("GIVE_ME_SWAP1")));
173         list.add(new MockAfterInvocationProvider("swap2",
174                 MethodInvocation.class, new SecurityConfig("GIVE_ME_SWAP2")));
175         list.add(new MockAfterInvocationProvider("swap3",
176                 MethodInvocation.class, new SecurityConfig("GIVE_ME_SWAP3")));
177         manager.setProviders(list);
178         manager.afterPropertiesSet();
179 
180         assertFalse(manager.supports(FilterInvocation.class));
181         assertTrue(manager.supports(MethodInvocation.class));
182     }
183 
184     //~ Inner Classes ==========================================================
185 
186     /***
187      * Always returns the constructor-defined <code>forceReturnObject</code>,
188      * provided the same configuration attribute was provided. Also stores the
189      * secure object it supports.
190      */
191     private class MockAfterInvocationProvider implements AfterInvocationProvider {
192         private Class secureObject;
193         private ConfigAttribute configAttribute;
194         private Object forceReturnObject;
195 
196         public MockAfterInvocationProvider(Object forceReturnObject,
197             Class secureObject, ConfigAttribute configAttribute) {
198             this.forceReturnObject = forceReturnObject;
199             this.secureObject = secureObject;
200             this.configAttribute = configAttribute;
201         }
202 
203         private MockAfterInvocationProvider() {}
204 
205         public Object decide(Authentication authentication, Object object,
206             ConfigAttributeDefinition config, Object returnedObject)
207             throws AccessDeniedException {
208             if (config.contains(configAttribute)) {
209                 return forceReturnObject;
210             }
211 
212             return returnedObject;
213         }
214 
215         public boolean supports(Class clazz) {
216             return secureObject.isAssignableFrom(clazz);
217         }
218 
219         public boolean supports(ConfigAttribute attribute) {
220             return attribute.equals(configAttribute);
221         }
222     }
223 }