1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.intercept.method.aopalliance;
17
18 import junit.framework.TestCase;
19
20 import org.acegisecurity.TargetObject;
21 import org.acegisecurity.intercept.method.MethodDefinitionMap;
22 import org.acegisecurity.intercept.method.MethodDefinitionSourceEditor;
23
24 import org.springframework.aop.framework.AopConfigException;
25
26 import java.lang.reflect.Method;
27
28
29 /***
30 * Tests {@link MethodDefinitionSourceAdvisor}.
31 *
32 * @author Ben Alex
33 * @version $Id: MethodDefinitionSourceAdvisorTests.java,v 1.3 2005/11/17 00:56:28 benalex Exp $
34 */
35 public class MethodDefinitionSourceAdvisorTests extends TestCase {
36
37
38 public MethodDefinitionSourceAdvisorTests() {
39 super();
40 }
41
42 public MethodDefinitionSourceAdvisorTests(String arg0) {
43 super(arg0);
44 }
45
46
47
48 public final void setUp() throws Exception {
49 super.setUp();
50 }
51
52 public static void main(String[] args) {
53 junit.textui.TestRunner.run(MethodDefinitionSourceAdvisorTests.class);
54 }
55
56 public void testAdvisorReturnsFalseWhenMethodInvocationNotDefined()
57 throws Exception {
58 Class clazz = TargetObject.class;
59 Method method = clazz.getMethod("makeLowerCase",
60 new Class[] {String.class});
61
62 MethodDefinitionSourceAdvisor advisor = new MethodDefinitionSourceAdvisor(getInterceptor());
63 assertFalse(advisor.matches(method, clazz));
64 }
65
66 public void testAdvisorReturnsTrueWhenMethodInvocationIsDefined()
67 throws Exception {
68 Class clazz = TargetObject.class;
69 Method method = clazz.getMethod("countLength",
70 new Class[] {String.class});
71
72 MethodDefinitionSourceAdvisor advisor = new MethodDefinitionSourceAdvisor(getInterceptor());
73 assertTrue(advisor.matches(method, clazz));
74 }
75
76 public void testDetectsImproperlyConfiguredAdvice() {
77 MethodSecurityInterceptor msi = new MethodSecurityInterceptor();
78
79 try {
80 new MethodDefinitionSourceAdvisor(msi);
81 fail(
82 "Should have detected null ObjectDefinitionSource and thrown AopConfigException");
83 } catch (AopConfigException expected) {
84 assertTrue(true);
85 }
86 }
87
88 public void testUnsupportedOperations() throws Throwable {
89 Class clazz = TargetObject.class;
90 Method method = clazz.getMethod("countLength",
91 new Class[] {String.class});
92
93 MethodDefinitionSourceAdvisor.InternalMethodInvocation imi = new MethodDefinitionSourceAdvisor(getInterceptor()).new InternalMethodInvocation(method);
94
95 try {
96 imi.getArguments();
97 fail("Should have thrown UnsupportedOperationException");
98 } catch (UnsupportedOperationException expected) {
99 assertTrue(true);
100 }
101
102 try {
103 imi.getStaticPart();
104 fail("Should have thrown UnsupportedOperationException");
105 } catch (UnsupportedOperationException expected) {
106 assertTrue(true);
107 }
108
109 try {
110 imi.getThis();
111 fail("Should have thrown UnsupportedOperationException");
112 } catch (UnsupportedOperationException expected) {
113 assertTrue(true);
114 }
115
116 try {
117 imi.proceed();
118 fail("Should have thrown UnsupportedOperationException");
119 } catch (UnsupportedOperationException expected) {
120 assertTrue(true);
121 }
122
123 try {
124 new MethodDefinitionSourceAdvisor(getInterceptor()).new InternalMethodInvocation();
125 fail("Should have thrown UnsupportedOperationException");
126 } catch (UnsupportedOperationException expected) {
127 assertTrue(true);
128 }
129 }
130
131 private MethodSecurityInterceptor getInterceptor() {
132 MethodDefinitionSourceEditor editor = new MethodDefinitionSourceEditor();
133 editor.setAsText(
134 "org.acegisecurity.TargetObject.countLength=ROLE_NOT_USED");
135
136 MethodDefinitionMap map = (MethodDefinitionMap) editor.getValue();
137
138 MethodSecurityInterceptor msi = new MethodSecurityInterceptor();
139 msi.setObjectDefinitionSource(map);
140
141 return msi;
142 }
143 }