View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.jexl3.internal.introspection;
18  
19  import static org.junit.jupiter.api.Assertions.assertFalse;
20  import static org.junit.jupiter.api.Assertions.assertNotNull;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import java.lang.reflect.Constructor;
24  import java.lang.reflect.Field;
25  import java.lang.reflect.Method;
26  
27  import org.apache.commons.jexl3.annotations.NoJexl;
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Checks the CacheMap.MethodKey implementation
32   */
33  
34  public class NoJexlTest {
35  
36      public static class A {
37          public int i;
38          public A() {}
39          public int method() { return 0; }
40      }
41  
42      public static class A0 extends A implements InterNoJexl0 {
43          @NoJexl public int i0;
44          @NoJexl public A0() {}
45          @Override public int method() { return 1; }
46      }
47  
48      public static class A1 extends A implements InterNoJexl1 {
49          private int i1;
50          @NoJexl public A1() {}
51          @Override public int method() { return 2; }
52      }
53  
54      @NoJexl
55      public static class A2 extends A  {
56          public A2() {}
57          @Override public int method() { return 3; }
58      }
59  
60      protected static class A3 {
61          protected int i3;
62          protected A3() {}
63          int method() { return 4; }
64      }
65  
66      public static class A5 implements InterNoJexl5 {
67          public A5() {}
68          @Override public int method() { return 0; }
69      }
70  
71      @NoJexl
72      public interface InterNoJexl0 {
73          int method();
74      }
75  
76      public interface InterNoJexl1 {
77          @NoJexl
78          int method();
79      }
80  
81      @NoJexl
82      public interface InterNoJexl5 {
83          int method();
84      }
85  
86      @Test
87      public void testNoJexlPermissions() throws Exception {
88          final Permissions p = Permissions.UNRESTRICTED;
89          assertFalse(p.allow((Field) null));
90          assertFalse(p.allow((Package) null));
91          assertFalse(p.allow((Method) null));
92          assertFalse(p.allow((Constructor<?>) null));
93          assertFalse(p.allow((Class<?>) null));
94  
95          assertFalse(p.allow(A2.class));
96          assertTrue(p.allow(A3.class));
97          assertTrue(p.allow(A5.class));
98  
99          final Method mA = A.class.getMethod("method");
100         assertNotNull(mA);
101         final Method mA0 = A0.class.getMethod("method");
102         assertNotNull(mA0);
103         final Method mA1 = A1.class.getMethod("method");
104         assertNotNull(mA1);
105         final Method mA2 = A2.class.getMethod("method");
106         assertNotNull(mA2);
107         final Method mA3 = A2.class.getDeclaredMethod("method");
108         assertNotNull(mA3);
109 
110         assertTrue(p.allow(mA));
111         assertFalse(p.allow(mA0));
112         assertFalse(p.allow(mA1));
113         assertFalse(p.allow(mA2));
114         assertFalse(p.allow(mA3));
115 
116         final Field fA = A.class.getField("i");
117         assertNotNull(fA);
118         assertTrue(p.allow(fA));
119 
120         final Field fA0 = A0.class.getField("i0");
121         assertNotNull(fA0);
122         assertFalse(p.allow(fA0));
123         final Field fA1 = A1.class.getDeclaredField("i1");
124         assertNotNull(fA1);
125         assertFalse(p.allow(fA0));
126 
127         final Constructor<?> cA = A.class.getConstructor();
128         assertNotNull(cA);
129         assertTrue(p.allow(cA));
130 
131         final Constructor<?> cA0 = A0.class.getConstructor();
132         assertNotNull(cA0);
133         assertFalse(p.allow(cA0));
134 
135         final Constructor<?> cA3 = A3.class.getDeclaredConstructor();
136         assertNotNull(cA3);
137         assertFalse(p.allow(cA3));
138     }
139 
140 }