View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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      @Test
65      public void testNullMethodInvocation() throws SecurityException, NoSuchMethodException {
66          MethodInvocation methodInvocation = createMock(MethodInvocation.class);
67          Method method = MyFixture.class.getDeclaredMethod("operateThis");
68          expect(methodInvocation.getMethod()).andReturn(method);
69          expect(methodInvocation.getThis()).andReturn(null);
70          replay(methodInvocation);
71          assertNull(annotationResolver.getAnnotation(methodInvocation, RequiresUser.class));
72      }
73  }
74