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.intercept.method;
17  
18  import org.acegisecurity.ITargetObject;
19  import org.acegisecurity.OtherTargetObject;
20  import org.acegisecurity.SecurityConfig;
21  import org.acegisecurity.TargetObject;
22  
23  import org.springframework.metadata.Attributes;
24  
25  import java.lang.reflect.Field;
26  import java.lang.reflect.Method;
27  
28  import java.util.Arrays;
29  import java.util.Collection;
30  import java.util.List;
31  
32  
33  /***
34   * Used by the {@link MethodDefinitionAttributesTests}.
35   *
36   * @author Cameron Braid
37   * @author Ben Alex
38   */
39  public class MockAttributes implements Attributes {
40      //~ Instance fields ========================================================
41  
42      List classAttributes = Arrays.asList(new SecurityConfig[] {new SecurityConfig(
43                      "MOCK_CLASS")});
44      List classMethodAttributesCountLength = Arrays.asList(new String[] {new String(
45                      "MOCK_CLASS_METHOD_COUNT_LENGTH")});
46      List classMethodAttributesMakeLowerCase = Arrays.asList(new SecurityConfig[] {new SecurityConfig(
47                      "MOCK_CLASS_METHOD_MAKE_LOWER_CASE")});
48      List classMethodAttributesMakeUpperCase = Arrays.asList(new SecurityConfig[] {new SecurityConfig(
49                      "MOCK_CLASS_METHOD_MAKE_UPPER_CASE")});
50      List interfaceAttributes = Arrays.asList(new SecurityConfig[] {new SecurityConfig(
51                      "MOCK_INTERFACE")});
52      List interfaceMethodAttributesCountLength = Arrays.asList(new SecurityConfig[] {new SecurityConfig(
53                      "MOCK_INTERFACE_METHOD_COUNT_LENGTH")});
54      List interfaceMethodAttributesMakeLowerCase = Arrays.asList(new SecurityConfig[] {new SecurityConfig(
55                      "MOCK_INTERFACE_METHOD_MAKE_LOWER_CASE")});
56      List interfaceMethodAttributesMakeUpperCase = Arrays.asList(new SecurityConfig[] {new SecurityConfig(
57                      "MOCK_INTERFACE_METHOD_MAKE_UPPER_CASE"), new SecurityConfig(
58                      "RUN_AS")});
59  
60      //~ Methods ================================================================
61  
62      public Collection getAttributes(Class clazz) {
63          // Emphasise we return null for OtherTargetObject
64          if (clazz.equals(OtherTargetObject.class)) {
65              return null;
66          }
67  
68          // interface
69          if (clazz.equals(ITargetObject.class)) {
70              return interfaceAttributes;
71          }
72  
73          // class
74          if (clazz.equals(TargetObject.class)) {
75              return classAttributes;
76          }
77  
78          return null;
79      }
80  
81      public Collection getAttributes(Method method) {
82          // interface
83          if (method.getDeclaringClass().equals(ITargetObject.class)) {
84              if (method.getName().equals("countLength")) {
85                  return interfaceMethodAttributesCountLength;
86              }
87  
88              if (method.getName().equals("makeLowerCase")) {
89                  return interfaceMethodAttributesMakeLowerCase;
90              }
91  
92              if (method.getName().equals("makeUpperCase")) {
93                  return interfaceMethodAttributesMakeUpperCase;
94              }
95  
96              if (method.getName().equals("publicMakeLowerCase")) {
97                  throw new UnsupportedOperationException(
98                      "mock support not implemented");
99              }
100         }
101 
102         // class
103         if (method.getDeclaringClass().equals(TargetObject.class)) {
104             if (method.getName().equals("countLength")) {
105                 return classMethodAttributesCountLength;
106             }
107 
108             if (method.getName().equals("makeLowerCase")) {
109                 return classMethodAttributesMakeLowerCase;
110             }
111 
112             if (method.getName().equals("makeUpperCase")) {
113                 return classMethodAttributesMakeUpperCase;
114             }
115 
116             if (method.getName().equals("publicMakeLowerCase")) {
117                 throw new UnsupportedOperationException(
118                     "mock support not implemented");
119             }
120         }
121 
122         // other target object
123         if (method.getDeclaringClass().equals(OtherTargetObject.class)) {
124             if (method.getName().equals("countLength")) {
125                 return classMethodAttributesCountLength;
126             }
127 
128             if (method.getName().equals("makeLowerCase")) {
129                 return classMethodAttributesMakeLowerCase;
130             }
131 
132             if (method.getName().equals("makeUpperCase")) {
133                 return null; // NB
134             }
135 
136             if (method.getName().equals("publicMakeLowerCase")) {
137                 throw new UnsupportedOperationException(
138                     "mock support not implemented");
139             }
140         }
141 
142         return null;
143     }
144 
145     public Collection getAttributes(Class arg0, Class arg1) {
146         throw new UnsupportedOperationException("mock method not implemented");
147     }
148 
149     public Collection getAttributes(Field arg0, Class arg1) {
150         throw new UnsupportedOperationException("mock method not implemented");
151     }
152 
153     public Collection getAttributes(Field arg0) {
154         throw new UnsupportedOperationException("mock method not implemented");
155     }
156 
157     public Collection getAttributes(Method arg0, Class arg1) {
158         throw new UnsupportedOperationException("mock method not implemented");
159     }
160 }