1   /* Copyright 2004, 2005 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.annotation;
17  
18  import java.lang.reflect.Field;
19  import java.lang.reflect.Method;
20  import java.util.Collection;
21  
22  import junit.framework.TestCase;
23  import org.acegisecurity.SecurityConfig;
24  
25  import org.springframework.metadata.Attributes;
26  
27  
28  /***
29   * Tests for {@link org.acegisecurity.annotation.SecurityAnnotationAttributes}
30   *
31   * @author Mark St.Godard
32   * @version $Revision: 1.2 $
33   */
34  public class SecurityAnnotationAttributesTests extends TestCase {
35      //~ Instance fields ========================================================
36  
37      private Attributes attributes;
38  
39      //~ Methods ================================================================
40  
41      public void testGetAttributesClass() {
42          Collection attrs = this.attributes.getAttributes(BusinessService.class);
43  
44          assertNotNull(attrs);
45          
46          // expect 1 annotation
47          assertTrue(attrs.size() == 1);
48  
49          // should have 1 SecurityConfig 
50          SecurityConfig sc = (SecurityConfig) attrs.iterator().next();
51  
52          assertTrue(sc.getAttribute().equals("ROLE_USER"));
53      }
54  
55      public void testGetAttributesClassClass() {
56      	try{
57      		this.attributes.getAttributes(BusinessService.class, null);
58      		fail("Unsupported method should have thrown an exception!");
59      		
60      	}catch(UnsupportedOperationException expected){
61      	}
62      }
63  
64      public void testGetAttributesField() {
65      	try{
66      		Field field = null;
67      		this.attributes.getAttributes(field);
68      		fail("Unsupported method should have thrown an exception!");
69      		
70      	}catch(UnsupportedOperationException expected){
71      		
72      	}
73      	
74      }
75  
76      public void testGetAttributesFieldClass() {
77      	try{
78      		Field field = null;
79      		this.attributes.getAttributes(field, null);
80      		fail("Unsupported method should have thrown an exception!");
81      		
82      	}catch(UnsupportedOperationException expected){
83      		
84      	}
85      	
86      }
87  
88      public void testGetAttributesMethod() {
89      	
90      	Method method = null;
91      	try{
92      		method = BusinessService.class.getMethod("someUserAndAdminMethod",new Class[] {});
93      	}catch(NoSuchMethodException unexpected){
94      		fail("Should be a method called 'someUserAndAdminMethod' on class!");
95      	}
96          Collection attrs = this.attributes.getAttributes(method);
97  
98          assertNotNull(attrs);
99          
100         // expect 2 attributes
101         assertTrue(attrs.size() == 2);
102 
103         boolean user = false;
104         boolean admin = false;
105         // should have 2 SecurityConfigs 
106         for(Object obj: attrs){
107         	assertTrue(obj instanceof SecurityConfig);
108         	SecurityConfig sc = (SecurityConfig)obj;
109         	if(sc.getAttribute().equals("ROLE_USER")){
110         		user = true;
111         	}else if(sc.getAttribute().equals("ROLE_ADMIN")){
112         		admin = true;
113         	}
114         }
115         // expect to have ROLE_USER and ROLE_ADMIN
116         assertTrue(user && admin);
117     }
118 
119     public void testGetAttributesMethodClass() {
120     	
121     	Method method = null;
122     	try{
123     		method = BusinessService.class.getMethod("someUserAndAdminMethod",new Class[] {});
124     	}catch(NoSuchMethodException unexpected){
125     		fail("Should be a method called 'someUserAndAdminMethod' on class!");
126     	}
127     	
128     	try{
129     		this.attributes.getAttributes(method,null);
130     		fail("Unsupported method should have thrown an exception!");
131     		
132     	}catch(UnsupportedOperationException expected){
133     		
134     	}
135     	
136     }
137 
138     protected void setUp() throws Exception {
139         // create the Annotations impl
140         this.attributes = new SecurityAnnotationAttributes();
141     }
142 }