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;
017:
018: import org.acegisecurity.ITargetObject;
019: import org.acegisecurity.OtherTargetObject;
020: import org.acegisecurity.SecurityConfig;
021: import org.acegisecurity.TargetObject;
022:
023: import org.springframework.metadata.Attributes;
024:
025: import java.lang.reflect.Field;
026: import java.lang.reflect.Method;
027:
028: import java.util.Arrays;
029: import java.util.Collection;
030: import java.util.List;
031:
032: /**
033: * Used by the {@link MethodDefinitionAttributesTests}.
034: *
035: * @author Cameron Braid
036: * @author Ben Alex
037: */
038: public class MockAttributes implements Attributes {
039: //~ Instance fields ================================================================================================
040:
041: List classAttributes = Arrays
042: .asList(new SecurityConfig[] { new SecurityConfig(
043: "MOCK_CLASS") });
044: List classMethodAttributesCountLength = Arrays
045: .asList(new String[] { new String(
046: "MOCK_CLASS_METHOD_COUNT_LENGTH") });
047: List classMethodAttributesMakeLowerCase = Arrays
048: .asList(new SecurityConfig[] { new SecurityConfig(
049: "MOCK_CLASS_METHOD_MAKE_LOWER_CASE") });
050: List classMethodAttributesMakeUpperCase = Arrays
051: .asList(new SecurityConfig[] { new SecurityConfig(
052: "MOCK_CLASS_METHOD_MAKE_UPPER_CASE") });
053: List interfaceAttributes = Arrays
054: .asList(new SecurityConfig[] { new SecurityConfig(
055: "MOCK_INTERFACE") });
056: List interfaceMethodAttributesCountLength = Arrays
057: .asList(new SecurityConfig[] { new SecurityConfig(
058: "MOCK_INTERFACE_METHOD_COUNT_LENGTH") });
059: List interfaceMethodAttributesMakeLowerCase = Arrays
060: .asList(new SecurityConfig[] { new SecurityConfig(
061: "MOCK_INTERFACE_METHOD_MAKE_LOWER_CASE") });
062: List interfaceMethodAttributesMakeUpperCase = Arrays
063: .asList(new SecurityConfig[] {
064: new SecurityConfig(
065: "MOCK_INTERFACE_METHOD_MAKE_UPPER_CASE"),
066: new SecurityConfig("RUN_AS") });
067:
068: //~ Methods ========================================================================================================
069:
070: public Collection getAttributes(Class clazz) {
071: // Emphasise we return null for OtherTargetObject
072: if (clazz.equals(OtherTargetObject.class)) {
073: return null;
074: }
075:
076: // interface
077: if (clazz.equals(ITargetObject.class)) {
078: return interfaceAttributes;
079: }
080:
081: // class
082: if (clazz.equals(TargetObject.class)) {
083: return classAttributes;
084: }
085:
086: return null;
087: }
088:
089: public Collection getAttributes(Method method) {
090: // interface
091: if (method.getDeclaringClass().equals(ITargetObject.class)) {
092: if (method.getName().equals("countLength")) {
093: return interfaceMethodAttributesCountLength;
094: }
095:
096: if (method.getName().equals("makeLowerCase")) {
097: return interfaceMethodAttributesMakeLowerCase;
098: }
099:
100: if (method.getName().equals("makeUpperCase")) {
101: return interfaceMethodAttributesMakeUpperCase;
102: }
103:
104: if (method.getName().equals("publicMakeLowerCase")) {
105: throw new UnsupportedOperationException(
106: "mock support not implemented");
107: }
108: }
109:
110: // class
111: if (method.getDeclaringClass().equals(TargetObject.class)) {
112: if (method.getName().equals("countLength")) {
113: return classMethodAttributesCountLength;
114: }
115:
116: if (method.getName().equals("makeLowerCase")) {
117: return classMethodAttributesMakeLowerCase;
118: }
119:
120: if (method.getName().equals("makeUpperCase")) {
121: return classMethodAttributesMakeUpperCase;
122: }
123:
124: if (method.getName().equals("publicMakeLowerCase")) {
125: throw new UnsupportedOperationException(
126: "mock support not implemented");
127: }
128: }
129:
130: // other target object
131: if (method.getDeclaringClass().equals(OtherTargetObject.class)) {
132: if (method.getName().equals("countLength")) {
133: return classMethodAttributesCountLength;
134: }
135:
136: if (method.getName().equals("makeLowerCase")) {
137: return classMethodAttributesMakeLowerCase;
138: }
139:
140: if (method.getName().equals("makeUpperCase")) {
141: return null; // NB
142: }
143:
144: if (method.getName().equals("publicMakeLowerCase")) {
145: throw new UnsupportedOperationException(
146: "mock support not implemented");
147: }
148: }
149:
150: return null;
151: }
152:
153: public Collection getAttributes(Class arg0, Class arg1) {
154: throw new UnsupportedOperationException(
155: "mock method not implemented");
156: }
157:
158: public Collection getAttributes(Field arg0, Class arg1) {
159: throw new UnsupportedOperationException(
160: "mock method not implemented");
161: }
162:
163: public Collection getAttributes(Field arg0) {
164: throw new UnsupportedOperationException(
165: "mock method not implemented");
166: }
167:
168: public Collection getAttributes(Method arg0, Class arg1) {
169: throw new UnsupportedOperationException(
170: "mock method not implemented");
171: }
172: }
|