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