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.authz.aop;
20  
21  import org.apache.shiro.authz.UnauthenticatedException;
22  import org.apache.shiro.authz.annotation.Logical;
23  import org.apache.shiro.authz.annotation.RequiresPermissions;
24  import org.apache.shiro.test.SecurityManagerTestSupport;
25  import org.junit.Test;
26  
27  import java.lang.annotation.Annotation;
28  
29  /**
30   * Test cases for the {@link PermissionAnnotationHandler} class.
31   */
32  public class PermissionAnnotationHandlerTest extends SecurityManagerTestSupport {
33  
34      //Added to satisfy SHIRO-146
35  
36      @Test(expected = UnauthenticatedException.class)
37      public void testGuestSinglePermissionAssertion() throws Throwable {
38          PermissionAnnotationHandler handler = new PermissionAnnotationHandler();
39  
40          Annotation requiresPermissionAnnotation = new RequiresPermissions() {
41              public String[] value() {
42                  return new String[]{"test:test"};
43              }
44  
45              public Class<? extends Annotation> annotationType() {
46                  return RequiresPermissions.class;
47              }
48  
49  	    public Logical logical() {
50  		return Logical.AND;
51  	    }
52          };
53  
54          handler.assertAuthorized(requiresPermissionAnnotation);
55      }
56  
57      //Added to satisfy SHIRO-146
58  
59      @Test(expected = UnauthenticatedException.class)
60      public void testGuestMultiplePermissionAssertion() throws Throwable {
61          PermissionAnnotationHandler handler = new PermissionAnnotationHandler();
62  
63          Annotation requiresPermissionAnnotation = new RequiresPermissions() {
64              public String[] value() {
65                  return new String[]{"test:test", "test2:test2"};
66              }
67  
68              public Class<? extends Annotation> annotationType() {
69                  return RequiresPermissions.class;
70              }
71              
72  	    public Logical logical() {
73  		return Logical.AND;
74  	    }
75          };
76  
77          handler.assertAuthorized(requiresPermissionAnnotation);
78      }
79  
80  }