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