1 16 17 package org.apache.commons.configuration; 18 19 import java.io.File ; 20 import java.util.ArrayList ; 21 import java.util.HashSet ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.NoSuchElementException ; 25 import java.util.Set ; 26 27 import junit.framework.TestCase; 28 29 35 public class TestSubsetConfiguration extends TestCase 36 { 37 static final String TEST_DIR = "conf"; 38 static final String TEST_FILE = "testDigesterConfiguration2.xml"; 39 40 public void testGetProperty() 41 { 42 Configuration conf = new BaseConfiguration(); 43 conf.setProperty("test.key1", "value1"); 44 conf.setProperty("testing.key2", "value1"); 45 46 Configuration subset = new SubsetConfiguration(conf, "test", "."); 47 assertFalse("the subset is empty", subset.isEmpty()); 48 assertTrue("'key1' not found in the subset", subset.containsKey("key1")); 49 assertFalse("'ng.key2' found in the subset", subset.containsKey("ng.key2")); 50 } 51 52 public void testSetProperty() 53 { 54 Configuration conf = new BaseConfiguration(); 55 Configuration subset = new SubsetConfiguration(conf, "test", "."); 56 57 subset.setProperty("key1", "value1"); 59 assertEquals("key1 in the subset configuration", "value1", subset.getProperty("key1")); 60 assertEquals("test.key1 in the parent configuration", "value1", conf.getProperty("test.key1")); 61 62 conf.setProperty("test.key2", "value2"); 64 assertEquals("test.key2 in the parent configuration", "value2", conf.getProperty("test.key2")); 65 assertEquals("key2 in the subset configuration", "value2", subset.getProperty("key2")); 66 } 67 68 public void testGetParentKey() 69 { 70 SubsetConfiguration subset = new SubsetConfiguration(null, "prefix", "."); 72 assertEquals("parent key for \"key\"", "prefix.key", subset.getParentKey("key")); 73 assertEquals("parent key for \"\"", "prefix", subset.getParentKey("")); 74 75 subset = new SubsetConfiguration(null, "prefix", null); 77 assertEquals("parent key for \"key\"", "prefixkey", subset.getParentKey("key")); 78 assertEquals("parent key for \"\"", "prefix", subset.getParentKey("")); 79 } 80 81 public void testGetChildKey() 82 { 83 SubsetConfiguration subset = new SubsetConfiguration(null, "prefix", "."); 85 assertEquals("parent key for \"prefixkey\"", "key", subset.getChildKey("prefix.key")); 86 assertEquals("parent key for \"prefix\"", "", subset.getChildKey("prefix")); 87 88 subset = new SubsetConfiguration(null, "prefix", null); 90 assertEquals("parent key for \"prefixkey\"", "key", subset.getChildKey("prefixkey")); 91 assertEquals("parent key for \"prefix\"", "", subset.getChildKey("prefix")); 92 } 93 94 public void testGetKeys() 95 { 96 Configuration conf = new BaseConfiguration(); 97 conf.setProperty("test", "value0"); 98 conf.setProperty("test.key1", "value1"); 99 conf.setProperty("testing.key2", "value1"); 100 101 Configuration subset = new SubsetConfiguration(conf, "test", "."); 102 103 Iterator it = subset.getKeys(); 104 assertEquals("1st key", "", it.next()); 105 assertEquals("2nd key", "key1", it.next()); 106 assertFalse("too many elements", it.hasNext()); 107 } 108 109 public void testGetKeysWithPrefix() 110 { 111 Configuration conf = new BaseConfiguration(); 112 conf.setProperty("test.abc", "value0"); 113 conf.setProperty("test.abc.key1", "value1"); 114 conf.setProperty("test.abcdef.key2", "value1"); 115 116 Configuration subset = new SubsetConfiguration(conf, "test", "."); 117 118 Iterator it = subset.getKeys("abc"); 119 assertEquals("1st key", "abc", it.next()); 120 assertEquals("2nd key", "abc.key1", it.next()); 121 assertFalse("too many elements", it.hasNext()); 122 } 123 124 public void testGetList() 125 { 126 Configuration conf = new BaseConfiguration(); 127 conf.setProperty("test.abc", "value0,value1"); 128 conf.addProperty("test.abc", "value3"); 129 130 Configuration subset = new SubsetConfiguration(conf, "test", "."); 131 List list = subset.getList("abc", new ArrayList ()); 132 assertEquals(3, list.size()); 133 } 134 135 public void testGetParent() 136 { 137 Configuration conf = new BaseConfiguration(); 138 SubsetConfiguration subset = new SubsetConfiguration(conf, "prefix", "."); 139 140 assertEquals("parent", conf, subset.getParent()); 141 } 142 143 public void testGetPrefix() 144 { 145 Configuration conf = new BaseConfiguration(); 146 SubsetConfiguration subset = new SubsetConfiguration(conf, "prefix", "."); 147 148 assertEquals("prefix", "prefix", subset.getPrefix()); 149 } 150 151 public void testSetPrefix() 152 { 153 Configuration conf = new BaseConfiguration(); 154 SubsetConfiguration subset = new SubsetConfiguration(conf, null, "."); 155 subset.setPrefix("prefix"); 156 157 assertEquals("prefix", "prefix", subset.getPrefix()); 158 } 159 160 public void testThrowtExceptionOnMissing() 161 { 162 BaseConfiguration config = new BaseConfiguration(); 163 config.setThrowExceptionOnMissing(true); 164 165 SubsetConfiguration subset = new SubsetConfiguration(config, "prefix"); 166 167 try 168 { 169 subset.getString("foo"); 170 fail("NoSuchElementException expected"); 171 } 172 catch (NoSuchElementException e) 173 { 174 } 176 177 config.setThrowExceptionOnMissing(false); 178 assertNull(subset.getString("foo")); 179 180 181 subset.setThrowExceptionOnMissing(true); 182 try 183 { 184 config.getString("foo"); 185 fail("NoSuchElementException expected"); 186 } 187 catch (NoSuchElementException e) 188 { 189 } 191 } 192 193 public void testNested() throws Exception 194 { 195 ConfigurationFactory factory = new ConfigurationFactory(); 196 File src = new File (new File (TEST_DIR), TEST_FILE); 197 factory.setConfigurationURL(src.toURL()); 198 Configuration config = factory.getConfiguration(); 199 Configuration subConf = config.subset("tables.table(0)"); 200 assertTrue(subConf.getKeys().hasNext()); 201 Configuration subSubConf = subConf.subset("fields.field(1)"); 202 Iterator itKeys = subSubConf.getKeys(); 203 Set keys = new HashSet (); 204 keys.add("name"); 205 keys.add("type"); 206 while(itKeys.hasNext()) 207 { 208 String k = (String ) itKeys.next(); 209 assertTrue(keys.contains(k)); 210 keys.remove(k); 211 } 212 assertTrue(keys.isEmpty()); 213 } 214 215 public void testClear() 216 { 217 Configuration config = new BaseConfiguration(); 218 config.setProperty("test.key1", "value1"); 219 config.setProperty("testing.key2", "value1"); 220 221 Configuration subset = config.subset("test"); 222 subset.clear(); 223 224 assertTrue("the subset is not empty", subset.isEmpty()); 225 assertFalse("the parent configuration is empty", config.isEmpty()); 226 } 227 } 228 | Popular Tags |