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 018 package org.apache.commons.configuration; 019 020 import static org.junit.Assert.assertEquals; 021 import static org.junit.Assert.assertFalse; 022 import static org.junit.Assert.assertNotNull; 023 import static org.junit.Assert.assertNull; 024 import static org.junit.Assert.assertSame; 025 import static org.junit.Assert.assertTrue; 026 import static org.junit.Assert.fail; 027 028 import java.util.ArrayList; 029 import java.util.Iterator; 030 import java.util.List; 031 032 import junitx.framework.ListAssert; 033 034 import org.apache.commons.logging.Log; 035 import org.apache.commons.logging.LogFactory; 036 import org.junit.Test; 037 038 /** 039 * Abstract TestCase for implementations of {@link AbstractConfiguration}. 040 * 041 * @author Emmanuel Bourg 042 * @version $Id: TestAbstractConfiguration.java 1222465 2011-12-22 21:32:56Z oheger $ 043 */ 044 public abstract class TestAbstractConfiguration 045 { 046 /** 047 * Return an abstract configuration with the following data:<br> 048 * <pre> 049 * key1 = value1 050 * key2 = value2 051 * list = value1, value2 052 * listesc = value1\\,value2 053 * </pre> 054 */ 055 protected abstract AbstractConfiguration getConfiguration(); 056 057 /** 058 * Return an empty configuration. 059 */ 060 protected abstract AbstractConfiguration getEmptyConfiguration(); 061 062 @Test 063 public void testGetProperty() 064 { 065 Configuration config = getConfiguration(); 066 assertEquals("key1", "value1", config.getProperty("key1")); 067 assertEquals("key2", "value2", config.getProperty("key2")); 068 assertNull("key3", config.getProperty("key3")); 069 } 070 071 @Test 072 public void testList() 073 { 074 Configuration config = getConfiguration(); 075 076 List<?> list = config.getList("list"); 077 assertNotNull("list not found", config.getProperty("list")); 078 assertEquals("list size", 2, list.size()); 079 assertTrue("'value1' is not in the list", list.contains("value1")); 080 assertTrue("'value2' is not in the list", list.contains("value2")); 081 } 082 083 /** 084 * Tests whether the escape character for list delimiters is recocknized and 085 * removed. 086 */ 087 @Test 088 public void testListEscaped() 089 { 090 assertEquals("Wrong value for escaped list", "value1,value2", 091 getConfiguration().getString("listesc")); 092 } 093 094 @Test 095 public void testAddPropertyDirect() 096 { 097 AbstractConfiguration config = getConfiguration(); 098 config.addPropertyDirect("key3", "value3"); 099 assertEquals("key3", "value3", config.getProperty("key3")); 100 101 config.addPropertyDirect("key3", "value4"); 102 config.addPropertyDirect("key3", "value5"); 103 List<Object> list = config.getList("key3"); 104 assertNotNull("no list found for the 'key3' property", list); 105 106 List<Object> expected = new ArrayList<Object>(); 107 expected.add("value3"); 108 expected.add("value4"); 109 expected.add("value5"); 110 111 ListAssert.assertEquals("values for the 'key3' property", expected, list); 112 } 113 114 @Test 115 public void testIsEmpty() 116 { 117 Configuration config = getConfiguration(); 118 assertFalse("the configuration is empty", config.isEmpty()); 119 assertTrue("the configuration is not empty", getEmptyConfiguration().isEmpty()); 120 } 121 122 @Test 123 public void testContainsKey() 124 { 125 Configuration config = getConfiguration(); 126 assertTrue("key1 not found", config.containsKey("key1")); 127 assertFalse("key3 found", config.containsKey("key3")); 128 } 129 130 @Test 131 public void testClearProperty() 132 { 133 Configuration config = getConfiguration(); 134 config.clearProperty("key2"); 135 assertFalse("key2 not cleared", config.containsKey("key2")); 136 } 137 138 @Test 139 public void testGetKeys() 140 { 141 Configuration config = getConfiguration(); 142 Iterator<String> keys = config.getKeys(); 143 144 List<String> expectedKeys = new ArrayList<String>(); 145 expectedKeys.add("key1"); 146 expectedKeys.add("key2"); 147 expectedKeys.add("list"); 148 expectedKeys.add("listesc"); 149 150 assertNotNull("null iterator", keys); 151 assertTrue("empty iterator", keys.hasNext()); 152 153 List<String> actualKeys = new ArrayList<String>(); 154 while (keys.hasNext()) 155 { 156 actualKeys.add(keys.next()); 157 } 158 159 ListAssert.assertEquals("keys", expectedKeys, actualKeys); 160 } 161 162 /** 163 * Tests accessing the configuration's logger. 164 */ 165 @Test 166 public void testSetLogger() 167 { 168 AbstractConfiguration config = getEmptyConfiguration(); 169 assertNotNull("Default logger is null", config.getLogger()); 170 Log log = LogFactory.getLog(config.getClass()); 171 config.setLogger(log); 172 assertSame("Logger was not set", log, config.getLogger()); 173 } 174 175 /** 176 * Tests the exception message triggered by the conversion to BigInteger. 177 * This test is related to CONFIGURATION-357. 178 */ 179 @Test 180 public void testGetBigIntegerConversion() 181 { 182 Configuration config = getConfiguration(); 183 try 184 { 185 config.getBigInteger("key1"); 186 fail("No conversion exception thrown!"); 187 } 188 catch (ConversionException cex) 189 { 190 assertEquals("Wrong exception message", 191 "'key1' doesn't map to a BigInteger object", cex 192 .getMessage()); 193 } 194 } 195 }