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 package org.apache.commons.configuration2.interpol;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertNull;
21
22 import java.awt.event.KeyEvent;
23
24 import org.junit.jupiter.api.AfterEach;
25 import org.junit.jupiter.api.BeforeEach;
26 import org.junit.jupiter.api.Test;
27
28 /**
29 * Test class for ConstantLookup.
30 */
31 public class TestConstantLookup {
32
33 /** A public field that can be read by the lookup. */
34 public static final String FIELD = "Field that can be read";
35
36 /** A private field that cannot be read by the lookup. */
37 @SuppressWarnings("unused")
38 private static final String PRIVATE_FIELD = "PRIVATE";
39
40 /** The lookup object to be tested. */
41 private ConstantLookup lookup;
42
43 @BeforeEach
44 public void setUp() throws Exception {
45 lookup = new ConstantLookup();
46 }
47
48 /**
49 * Clears the test environment. Here the static cache of the constant lookup class is wiped out.
50 */
51 @AfterEach
52 public void tearDown() {
53 ConstantLookup.clear();
54 }
55
56 /**
57 * Tests accessing the cache by querying a variable twice.
58 */
59 @Test
60 void testLookupCache() {
61 testLookupConstant();
62 testLookupConstant();
63 }
64
65 /**
66 * Tests resolving a valid constant.
67 */
68 @Test
69 void testLookupConstant() {
70 assertEquals(FIELD, lookup.lookup(variable("FIELD")));
71 }
72
73 /**
74 * Tries to resolve a variable with an invalid syntax: The name does not contain a dot as a field separator.
75 */
76 @Test
77 void testLookupInvalidSyntax() {
78 assertNull(lookup.lookup("InvalidVariableName"));
79 }
80
81 /**
82 * Tests resolving a non existing constant. Result should be null.
83 */
84 @Test
85 void testLookupNonExisting() {
86 assertNull(lookup.lookup(variable("NO_FIELD")));
87 }
88
89 /**
90 * Tests resolving a non string constant. Then looks the same variable up from the cache.
91 */
92 @Test
93 void testLookupNonStringFromCache() {
94 final String var = KeyEvent.class.getName() + ".VK_ESCAPE";
95 final Object expected = KeyEvent.VK_ESCAPE;
96 assertEquals(expected, lookup.lookup(var));
97 assertEquals(expected, lookup.lookup(var));
98 }
99
100 /**
101 * Tests looking up a null variable.
102 */
103 @Test
104 void testLookupNull() {
105 assertNull(lookup.lookup(null));
106 }
107
108 /**
109 * Tests resolving a private constant. Because a private field cannot be accessed this should again yield null.
110 */
111 @Test
112 void testLookupPrivate() {
113 assertNull(lookup.lookup(variable("PRIVATE_FIELD")));
114 }
115
116 /**
117 * Tests resolving a field from an unknown class.
118 */
119 @Test
120 void testLookupUnknownClass() {
121 assertNull(lookup.lookup("org.apache.commons.configuration.NonExistingConfig." + FIELD));
122 }
123
124 /**
125 * Generates the name of a variable for a lookup operation based on the given field name of this class.
126 *
127 * @param field the field name
128 * @return the variable for looking up this field
129 */
130 private String variable(final String field) {
131 return getClass().getName() + '.' + field;
132 }
133 }