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 junit.framework.TestCase;
20  
21  /***
22   * Test class for ConstantLookup.
23   *
24   * @version $Id: TestConstantLookup.java 490375 2006-12-26 21:28:04Z oheger $
25   */
26  public class TestConstantLookup extends TestCase
27  {
28      /*** Constant for the name of the test class. */
29      private static final String CLS_NAME = ConfigurationInterpolator.class
30              .getName() + '.';
31  
32      /*** Constant for the name of the test field. */
33      private static final String FIELD = "PREFIX_CONSTANTS";
34  
35      /*** Constant for the test variable name. */
36      private static final String VARNAME = CLS_NAME + FIELD;
37  
38      /*** The lookup object to be tested. */
39      private ConstantLookup lookup;
40  
41      protected void setUp() throws Exception
42      {
43          super.setUp();
44          lookup = new ConstantLookup();
45      }
46  
47      /***
48       * Clears the test environment. Here the static cache of the constant lookup
49       * class is wiped out.
50       */
51      protected void tearDown() throws Exception
52      {
53          ConstantLookup.clear();
54          super.tearDown();
55      }
56  
57      /***
58       * Tests resolving a valid constant.
59       */
60      public void testLookupConstant()
61      {
62          assertEquals("Wrong value of constant",
63                  ConfigurationInterpolator.PREFIX_CONSTANTS, lookup
64                          .lookup(VARNAME));
65      }
66  
67      /***
68       * Tests resolving a non existing constant. Result should be null.
69       */
70      public void testLookupNonExisting()
71      {
72          assertNull("Non null return value for non existing constant", lookup
73                  .lookup(CLS_NAME + "NO_FIELD"));
74      }
75  
76      /***
77       * Tests resolving a private constant. Because a private field cannot be
78       * accessed this should again yield null.
79       */
80      public void testLookupPrivate()
81      {
82          assertNull("Non null return value for non accessable field", lookup
83                  .lookup(CLS_NAME + "PREFIX_SEPARATOR"));
84      }
85  
86      /***
87       * Tests resolving a field from an unknown class.
88       */
89      public void testLookupUnknownClass()
90      {
91          assertNull("Non null return value for unknown class", lookup
92                  .lookup("org.apache.commons.configuration.NonExistingConfig."
93                          + FIELD));
94      }
95  
96      /***
97       * Tries to resolve a variable with an invalid syntax: The name does not
98       * contain a dot as a field separator.
99       */
100     public void testLookupInvalidSyntax()
101     {
102         assertNull("Non null return value for invalid variable name", lookup
103                 .lookup("InvalidVariableName"));
104     }
105 
106     /***
107      * Tests looking up a null variable.
108      */
109     public void testLookupNull()
110     {
111         assertNull("Non null return value for null variable", lookup
112                 .lookup(null));
113     }
114 
115     /***
116      * Tests accessing the cache by querying a variable twice.
117      */
118     public void testLookupCache()
119     {
120         testLookupConstant();
121         testLookupConstant();
122     }
123 }