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