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    *      https://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  
18  package org.apache.commons.jexl3.internal;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertNotNull;
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  
24  import java.util.Arrays;
25  import java.util.Collections;
26  import java.util.List;
27  
28  import org.apache.commons.jexl3.JexlBuilder;
29  import org.apache.commons.jexl3.JexlException;
30  import org.apache.commons.jexl3.JexlTestCase;
31  import org.apache.commons.jexl3.internal.introspection.Uberspect;
32  import org.apache.commons.jexl3.introspection.JexlPermissions;
33  import org.apache.commons.jexl3.introspection.JexlUberspect;
34  import org.junit.jupiter.api.AfterEach;
35  import org.junit.jupiter.api.BeforeEach;
36  import org.junit.jupiter.api.Test;
37  
38  
39  
40  
41  public class FqcnTest extends JexlTestCase {
42    public static final int FORTYTWO = 42;
43    public FqcnTest() {
44      super("FqcnTest");
45    }
46  
47    @BeforeEach
48    @Override
49    public void setUp() throws Exception {
50      // ensure jul logging is only error
51      java.util.logging.Logger.getLogger(org.apache.commons.jexl3.JexlEngine.class.getName()).setLevel(java.util.logging.Level.SEVERE);
52    }
53  
54    @AfterEach
55    @Override
56    public void tearDown() throws Exception {
57      super.tearDown();
58    }
59  
60    public enum FqcnScope {
61      UNDEFINED, UNDECLARED, GLOBAL, LOCAL, THIS, SUPER;
62    }
63  
64    Object getConstant(final List<String> imports, final String name) throws Exception {
65      final JexlUberspect uber = new Uberspect(null, null, JexlPermissions.UNRESTRICTED);
66      final FqcnResolver resolver = new FqcnResolver(uber, imports);
67      return resolver.resolveConstant(name);
68    }
69  
70    @Test
71    void testBadImport0() {
72      final List<String> imports = Collections.singletonList("org.apache.commons.jexl4242");
73      assertThrows(JexlException.class, () -> new JexlBuilder().imports(imports).create());
74    }
75  
76    @Test
77    void testBadImport1() {
78      // permissions will not allow this import
79      final List<String> imports = Collections.singletonList("org.apache.commons.jexl.JexlEngine");
80      assertThrows(JexlException.class, () -> new JexlBuilder().imports(imports).create());
81    }
82  
83    @Test
84    public void testFqcn() throws Exception {
85      final List<String> imports = Arrays.asList("org.apache.commons.jexl3.internal.FqcnTest", "org.apache.commons.jexl3.internal", "java.lang");
86      Object c = getConstant(imports, "FqcnScope.UNDEFINED");
87      assertNotNull(c);
88      assertEquals(FqcnScope.UNDEFINED, c);
89      c = getConstant(imports, "FqcnScope.SUPER");
90      assertEquals(FqcnScope.SUPER, c);
91      c = getConstant(imports, "FqcnScope.SUPER");
92      assertEquals(FqcnScope.SUPER, c);
93      c = getConstant(imports, "FqcnTest.FORTYTWO");
94      assertEquals(42, c);
95    }
96  }