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.configuration.interpol;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertNull;
21  
22  import java.awt.event.KeyEvent;
23  
24  import org.junit.After;
25  import org.junit.Before;
26  import org.junit.Test;
27  
28  /**
29   * Test class for ConstantLookup.
30   *
31   * @version $Id: TestConstantLookup.java 1225656 2011-12-29 21:09:11Z oheger $
32   */
33  public class TestConstantLookup
34  {
35      /** Constant for the name of the test class. */
36      private static final String CLS_NAME = ConfigurationInterpolator.class
37              .getName() + '.';
38  
39      /** Constant for the name of the test field. */
40      private static final String FIELD = "PREFIX_CONSTANTS";
41  
42      /** Constant for the test variable name. */
43      private static final String VARNAME = CLS_NAME + FIELD;
44  
45      /** The lookup object to be tested. */
46      private ConstantLookup lookup;
47  
48      @Before
49      public void setUp() throws Exception
50      {
51          lookup = new ConstantLookup();
52      }
53  
54      /**
55       * Clears the test environment. Here the static cache of the constant lookup
56       * class is wiped out.
57       */
58      @After
59      public void tearDown() throws Exception
60      {
61          ConstantLookup.clear();
62      }
63  
64      /**
65       * Tests resolving a valid constant.
66       */
67      @Test
68      public void testLookupConstant()
69      {
70          assertEquals("Wrong value of constant",
71                  ConfigurationInterpolator.PREFIX_CONSTANTS, lookup
72                          .lookup(VARNAME));
73      }
74  
75      /**
76       * Tests resolving a non existing constant. Result should be null.
77       */
78      @Test
79      public void testLookupNonExisting()
80      {
81          assertNull("Non null return value for non existing constant", lookup
82                  .lookup(CLS_NAME + "NO_FIELD"));
83      }
84  
85      /**
86       * Tests resolving a private constant. Because a private field cannot be
87       * accessed this should again yield null.
88       */
89      @Test
90      public void testLookupPrivate()
91      {
92          assertNull("Non null return value for non accessable field", lookup
93                  .lookup(CLS_NAME + "PREFIX_SEPARATOR"));
94      }
95  
96      /**
97       * Tests resolving a field from an unknown class.
98       */
99      @Test
100     public void testLookupUnknownClass()
101     {
102         assertNull("Non null return value for unknown class", lookup
103                 .lookup("org.apache.commons.configuration.NonExistingConfig."
104                         + FIELD));
105     }
106 
107     /**
108      * Tries to resolve a variable with an invalid syntax: The name does not
109      * contain a dot as a field separator.
110      */
111     @Test
112     public void testLookupInvalidSyntax()
113     {
114         assertNull("Non null return value for invalid variable name", lookup
115                 .lookup("InvalidVariableName"));
116     }
117 
118     /**
119      * Tests looking up a null variable.
120      */
121     @Test
122     public void testLookupNull()
123     {
124         assertNull("Non null return value for null variable", lookup
125                 .lookup(null));
126     }
127 
128     /**
129      * Tests accessing the cache by querying a variable twice.
130      */
131     @Test
132     public void testLookupCache()
133     {
134         testLookupConstant();
135         testLookupConstant();
136     }
137 
138     /**
139      * Tests resolving a non string constant. Then looks the same variable up
140      * from the cache.
141      */
142     @Test
143     public void testLookupNonStringFromCache()
144     {
145         final String var = KeyEvent.class.getName() + ".VK_ESCAPE";
146         final String expected = String.valueOf(KeyEvent.VK_ESCAPE);
147         assertEquals("Wrong result of first lookup", expected, lookup
148                 .lookup(var));
149         assertEquals("Wrong result of 2nd lookup", expected, lookup.lookup(var));
150     }
151 }