1
2
3
4
5
6
7
8
9
10
11
12
13
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
36
37 private Attributes attributes;
38
39
40
41 public void testGetAttributesClass() {
42 Collection attrs = this.attributes.getAttributes(BusinessService.class);
43
44 assertNotNull(attrs);
45
46
47 assertTrue(attrs.size() == 1);
48
49
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
101 assertTrue(attrs.size() == 2);
102
103 boolean user = false;
104 boolean admin = false;
105
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
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
140 this.attributes = new SecurityAnnotationAttributes();
141 }
142 }