1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.shiro.aop;
20
21 import static org.easymock.EasyMock.createMock;
22 import static org.easymock.EasyMock.expect;
23 import static org.easymock.EasyMock.replay;
24
25 import java.lang.reflect.Method;
26
27 import org.apache.shiro.authz.annotation.RequiresRoles;
28 import org.apache.shiro.authz.annotation.RequiresUser;
29 import org.junit.Test;
30 import static org.junit.Assert.*;
31
32
33 public class AnnotationResolverTest {
34 @SuppressWarnings("unused")
35 @RequiresRoles("root")
36 private class MyFixture {
37 public void operateThis() {}
38 @RequiresUser()
39 public void operateThat() {}
40 }
41
42 DefaultAnnotationResolver annotationResolver = new DefaultAnnotationResolver();
43
44 @Test
45 public void testAnnotationFoundFromClass() throws SecurityException, NoSuchMethodException {
46 MyFixture myFixture = new MyFixture();
47 MethodInvocation methodInvocation = createMock(MethodInvocation.class);
48 Method method = MyFixture.class.getDeclaredMethod("operateThis");
49 expect(methodInvocation.getMethod()).andReturn(method);
50 expect(methodInvocation.getThis()).andReturn(myFixture);
51 replay(methodInvocation);
52 assertNotNull(annotationResolver.getAnnotation(methodInvocation, RequiresRoles.class));
53 }
54
55 @Test
56 public void testAnnotationFoundFromMethod() throws SecurityException, NoSuchMethodException {
57 MethodInvocation methodInvocation = createMock(MethodInvocation.class);
58 Method method = MyFixture.class.getDeclaredMethod("operateThat");
59 expect(methodInvocation.getMethod()).andReturn(method);
60 replay(methodInvocation);
61 assertNotNull(annotationResolver.getAnnotation(methodInvocation, RequiresUser.class));
62 }
63 }
64