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