001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.configuration.interpol; 018 019 import static org.junit.Assert.assertEquals; 020 import static org.junit.Assert.assertNull; 021 022 import java.awt.event.KeyEvent; 023 024 import org.junit.After; 025 import org.junit.Before; 026 import org.junit.Test; 027 028 /** 029 * Test class for ConstantLookup. 030 * 031 * @version $Id: TestConstantLookup.java 1225656 2011-12-29 21:09:11Z oheger $ 032 */ 033 public class TestConstantLookup 034 { 035 /** Constant for the name of the test class. */ 036 private static final String CLS_NAME = ConfigurationInterpolator.class 037 .getName() + '.'; 038 039 /** Constant for the name of the test field. */ 040 private static final String FIELD = "PREFIX_CONSTANTS"; 041 042 /** Constant for the test variable name. */ 043 private static final String VARNAME = CLS_NAME + FIELD; 044 045 /** The lookup object to be tested. */ 046 private ConstantLookup lookup; 047 048 @Before 049 public void setUp() throws Exception 050 { 051 lookup = new ConstantLookup(); 052 } 053 054 /** 055 * Clears the test environment. Here the static cache of the constant lookup 056 * class is wiped out. 057 */ 058 @After 059 public void tearDown() throws Exception 060 { 061 ConstantLookup.clear(); 062 } 063 064 /** 065 * Tests resolving a valid constant. 066 */ 067 @Test 068 public void testLookupConstant() 069 { 070 assertEquals("Wrong value of constant", 071 ConfigurationInterpolator.PREFIX_CONSTANTS, lookup 072 .lookup(VARNAME)); 073 } 074 075 /** 076 * Tests resolving a non existing constant. Result should be null. 077 */ 078 @Test 079 public void testLookupNonExisting() 080 { 081 assertNull("Non null return value for non existing constant", lookup 082 .lookup(CLS_NAME + "NO_FIELD")); 083 } 084 085 /** 086 * Tests resolving a private constant. Because a private field cannot be 087 * accessed this should again yield null. 088 */ 089 @Test 090 public void testLookupPrivate() 091 { 092 assertNull("Non null return value for non accessable field", lookup 093 .lookup(CLS_NAME + "PREFIX_SEPARATOR")); 094 } 095 096 /** 097 * Tests resolving a field from an unknown class. 098 */ 099 @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 }