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: package org.acegisecurity.annotation;
016:
017: import junit.framework.TestCase;
018:
019: import org.acegisecurity.SecurityConfig;
020:
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023:
024: import org.springframework.metadata.Attributes;
025:
026: import java.lang.reflect.Field;
027: import java.lang.reflect.Method;
028:
029: import java.util.Collection;
030:
031: /**
032: * Tests for {@link org.acegisecurity.annotation.SecurityAnnotationAttributes}
033: *
034: * @author Mark St.Godard
035: * @author Joe Scalise
036: * @version $Id: SecurityAnnotationAttributesTests.java 1756 2006-11-17 02:17:45Z benalex $
037: */
038: public class SecurityAnnotationAttributesTests extends TestCase {
039: //~ Instance fields ================================================================================================
040:
041: private Attributes attributes;
042: private Log logger = LogFactory
043: .getLog(SecurityAnnotationAttributesTests.class);
044:
045: //~ Methods ========================================================================================================
046:
047: protected void setUp() throws Exception {
048: // create the Annotations impl
049: this .attributes = new SecurityAnnotationAttributes();
050: }
051:
052: public void testGenericsSuperclassDeclarationsAreIncludedWhenSubclassesOverride() {
053: Method method = null;
054:
055: try {
056: method = DepartmentServiceImpl.class
057: .getMethod("someUserMethod3",
058: new Class[] { Department.class });
059: } catch (NoSuchMethodException unexpected) {
060: fail("Should be a superMethod called 'someUserMethod3' on class!");
061: }
062:
063: Collection attrs = this .attributes.getAttributes(method);
064:
065: if (logger.isDebugEnabled()) {
066: logger.debug("attrs: ");
067: logger.debug(attrs);
068: }
069:
070: assertNotNull(attrs);
071:
072: // expect 1 attribute
073: assertTrue("Did not find 1 attribute", attrs.size() == 1);
074:
075: // should have 1 SecurityConfig
076: for (Object obj : attrs) {
077: assertTrue(obj instanceof SecurityConfig);
078:
079: SecurityConfig sc = (SecurityConfig) obj;
080: assertEquals("Found an incorrect role", "ROLE_ADMIN", sc
081: .getAttribute());
082: }
083:
084: Method super Method = null;
085:
086: try {
087: super Method = DepartmentServiceImpl.class.getMethod(
088: "someUserMethod3", new Class[] { Entity.class });
089: } catch (NoSuchMethodException unexpected) {
090: fail("Should be a superMethod called 'someUserMethod3' on class!");
091: }
092:
093: System.out.println(super Method);
094:
095: Collection super Attrs = this .attributes
096: .getAttributes(super Method);
097:
098: if (logger.isDebugEnabled()) {
099: logger.debug("superAttrs: ");
100: logger.debug(super Attrs);
101: }
102:
103: assertNotNull(super Attrs);
104:
105: // TODO: Enable this part of the test once we can build against Spring 2.0+ and above only (SEC-274)
106: /*
107: // expect 1 attribute
108: assertTrue("Did not find 1 attribute", superAttrs.size() == 1);
109: // should have 1 SecurityConfig
110: for (Object obj : superAttrs) {
111: assertTrue(obj instanceof SecurityConfig);
112: SecurityConfig sc = (SecurityConfig) obj;
113: assertEquals("Found an incorrect role", "ROLE_ADMIN", sc.getAttribute());
114: }
115: */
116: }
117:
118: public void testGetAttributesClass() {
119: Collection attrs = this .attributes
120: .getAttributes(BusinessService.class);
121:
122: assertNotNull(attrs);
123:
124: // expect 1 annotation
125: assertTrue(attrs.size() == 1);
126:
127: // should have 1 SecurityConfig
128: SecurityConfig sc = (SecurityConfig) attrs.iterator().next();
129:
130: assertTrue(sc.getAttribute().equals("ROLE_USER"));
131: }
132:
133: public void testGetAttributesClassClass() {
134: try {
135: this .attributes.getAttributes(BusinessService.class, null);
136: fail("Unsupported method should have thrown an exception!");
137: } catch (UnsupportedOperationException expected) {
138: }
139: }
140:
141: public void testGetAttributesField() {
142: try {
143: Field field = null;
144: this .attributes.getAttributes(field);
145: fail("Unsupported method should have thrown an exception!");
146: } catch (UnsupportedOperationException expected) {
147: }
148: }
149:
150: public void testGetAttributesFieldClass() {
151: try {
152: Field field = null;
153: this .attributes.getAttributes(field, null);
154: fail("Unsupported method should have thrown an exception!");
155: } catch (UnsupportedOperationException expected) {
156: }
157: }
158:
159: public void testGetAttributesMethod() {
160: Method method = null;
161:
162: try {
163: method = BusinessService.class.getMethod(
164: "someUserAndAdminMethod", new Class[] {});
165: } catch (NoSuchMethodException unexpected) {
166: fail("Should be a method called 'someUserAndAdminMethod' on class!");
167: }
168:
169: Collection attrs = this .attributes.getAttributes(method);
170:
171: assertNotNull(attrs);
172:
173: // expect 2 attributes
174: assertTrue(attrs.size() == 2);
175:
176: boolean user = false;
177: boolean admin = false;
178:
179: // should have 2 SecurityConfigs
180: for (Object obj : attrs) {
181: assertTrue(obj instanceof SecurityConfig);
182:
183: SecurityConfig sc = (SecurityConfig) obj;
184:
185: if (sc.getAttribute().equals("ROLE_USER")) {
186: user = true;
187: } else if (sc.getAttribute().equals("ROLE_ADMIN")) {
188: admin = true;
189: }
190: }
191:
192: // expect to have ROLE_USER and ROLE_ADMIN
193: assertTrue(user && admin);
194: }
195:
196: public void testGetAttributesMethodClass() {
197: Method method = null;
198:
199: try {
200: method = BusinessService.class.getMethod(
201: "someUserAndAdminMethod", new Class[] {});
202: } catch (NoSuchMethodException unexpected) {
203: fail("Should be a method called 'someUserAndAdminMethod' on class!");
204: }
205:
206: try {
207: this .attributes.getAttributes(method, null);
208: fail("Unsupported method should have thrown an exception!");
209: } catch (UnsupportedOperationException expected) {
210: }
211: }
212: }
|