1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
30
31 public class TestConstantLookup {
32
33 public static final String FIELD = "Field that can be read";
34
35
36 @SuppressWarnings("unused")
37 private static final String PRIVATE_FIELD = "PRIVATE";
38
39
40 private ConstantLookup lookup;
41
42 @BeforeEach
43 public void setUp() throws Exception {
44 lookup = new ConstantLookup();
45 }
46
47
48
49
50 @AfterEach
51 public void tearDown() {
52 ConstantLookup.clear();
53 }
54
55
56
57
58 @Test
59 public void testLookupCache() {
60 testLookupConstant();
61 testLookupConstant();
62 }
63
64
65
66
67 @Test
68 public void testLookupConstant() {
69 assertEquals(FIELD, lookup.lookup(variable("FIELD")));
70 }
71
72
73
74
75 @Test
76 public void testLookupInvalidSyntax() {
77 assertNull(lookup.lookup("InvalidVariableName"));
78 }
79
80
81
82
83 @Test
84 public void testLookupNonExisting() {
85 assertNull(lookup.lookup(variable("NO_FIELD")));
86 }
87
88
89
90
91 @Test
92 public void testLookupNonStringFromCache() {
93 final String var = KeyEvent.class.getName() + ".VK_ESCAPE";
94 final Object expected = KeyEvent.VK_ESCAPE;
95 assertEquals(expected, lookup.lookup(var));
96 assertEquals(expected, lookup.lookup(var));
97 }
98
99
100
101
102 @Test
103 public void testLookupNull() {
104 assertNull(lookup.lookup(null));
105 }
106
107
108
109
110 @Test
111 public void testLookupPrivate() {
112 assertNull(lookup.lookup(variable("PRIVATE_FIELD")));
113 }
114
115
116
117
118 @Test
119 public void testLookupUnknownClass() {
120 assertNull(lookup.lookup("org.apache.commons.configuration.NonExistingConfig." + FIELD));
121 }
122
123
124
125
126
127
128
129 private String variable(final String field) {
130 return getClass().getName() + '.' + field;
131 }
132 }