001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.intercept.method.aopalliance;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.TargetObject;
021:
022: import org.acegisecurity.intercept.method.MethodDefinitionMap;
023: import org.acegisecurity.intercept.method.MethodDefinitionSourceEditor;
024:
025: import org.springframework.aop.framework.AopConfigException;
026:
027: import java.lang.reflect.Method;
028:
029: /**
030: * Tests {@link MethodDefinitionSourceAdvisor}.
031: *
032: * @author Ben Alex
033: * @version $Id: MethodDefinitionSourceAdvisorTests.java 1496 2006-05-23 13:38:33Z benalex $
034: */
035: public class MethodDefinitionSourceAdvisorTests extends TestCase {
036: //~ Constructors ===================================================================================================
037:
038: public MethodDefinitionSourceAdvisorTests() {
039: super ();
040: }
041:
042: public MethodDefinitionSourceAdvisorTests(String arg0) {
043: super (arg0);
044: }
045:
046: //~ Methods ========================================================================================================
047:
048: private MethodSecurityInterceptor getInterceptor() {
049: MethodDefinitionSourceEditor editor = new MethodDefinitionSourceEditor();
050: editor
051: .setAsText("org.acegisecurity.TargetObject.countLength=ROLE_NOT_USED");
052:
053: MethodDefinitionMap map = (MethodDefinitionMap) editor
054: .getValue();
055:
056: MethodSecurityInterceptor msi = new MethodSecurityInterceptor();
057: msi.setObjectDefinitionSource(map);
058:
059: return msi;
060: }
061:
062: public static void main(String[] args) {
063: junit.textui.TestRunner
064: .run(MethodDefinitionSourceAdvisorTests.class);
065: }
066:
067: public final void setUp() throws Exception {
068: super .setUp();
069: }
070:
071: public void testAdvisorReturnsFalseWhenMethodInvocationNotDefined()
072: throws Exception {
073: Class clazz = TargetObject.class;
074: Method method = clazz.getMethod("makeLowerCase",
075: new Class[] { String.class });
076:
077: MethodDefinitionSourceAdvisor advisor = new MethodDefinitionSourceAdvisor(
078: getInterceptor());
079: assertFalse(advisor.matches(method, clazz));
080: }
081:
082: public void testAdvisorReturnsTrueWhenMethodInvocationIsDefined()
083: throws Exception {
084: Class clazz = TargetObject.class;
085: Method method = clazz.getMethod("countLength",
086: new Class[] { String.class });
087:
088: MethodDefinitionSourceAdvisor advisor = new MethodDefinitionSourceAdvisor(
089: getInterceptor());
090: assertTrue(advisor.matches(method, clazz));
091: }
092:
093: public void testDetectsImproperlyConfiguredAdvice() {
094: MethodSecurityInterceptor msi = new MethodSecurityInterceptor();
095:
096: try {
097: new MethodDefinitionSourceAdvisor(msi);
098: fail("Should have detected null ObjectDefinitionSource and thrown AopConfigException");
099: } catch (AopConfigException expected) {
100: assertTrue(true);
101: }
102: }
103:
104: public void testUnsupportedOperations() throws Throwable {
105: Class clazz = TargetObject.class;
106: Method method = clazz.getMethod("countLength",
107: new Class[] { String.class });
108:
109: MethodDefinitionSourceAdvisor.InternalMethodInvocation imi = new MethodDefinitionSourceAdvisor(
110: getInterceptor()).new InternalMethodInvocation(method);
111:
112: try {
113: imi.getArguments();
114: fail("Should have thrown UnsupportedOperationException");
115: } catch (UnsupportedOperationException expected) {
116: assertTrue(true);
117: }
118:
119: try {
120: imi.getStaticPart();
121: fail("Should have thrown UnsupportedOperationException");
122: } catch (UnsupportedOperationException expected) {
123: assertTrue(true);
124: }
125:
126: try {
127: imi.getThis();
128: fail("Should have thrown UnsupportedOperationException");
129: } catch (UnsupportedOperationException expected) {
130: assertTrue(true);
131: }
132:
133: try {
134: imi.proceed();
135: fail("Should have thrown UnsupportedOperationException");
136: } catch (UnsupportedOperationException expected) {
137: assertTrue(true);
138: }
139:
140: try {
141: new MethodDefinitionSourceAdvisor(getInterceptor()).new InternalMethodInvocation();
142: fail("Should have thrown UnsupportedOperationException");
143: } catch (UnsupportedOperationException expected) {
144: assertTrue(true);
145: }
146: }
147: }
|