1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
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 }