001 package org.apache.commons.configuration; 002 003 /* 004 * Licensed to the Apache Software Foundation (ASF) under one or more 005 * contributor license agreements. See the NOTICE file distributed with 006 * this work for additional information regarding copyright ownership. 007 * The ASF licenses this file to You under the Apache License, Version 2.0 008 * (the "License"); you may not use this file except in compliance with 009 * the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019 020 import static org.junit.Assert.assertEquals; 021 import static org.junit.Assert.assertFalse; 022 import static org.junit.Assert.assertTrue; 023 024 import java.util.Iterator; 025 import java.util.List; 026 027 import org.junit.Test; 028 029 /** 030 * Compare the behavior of various methods between CompositeConfiguration 031 * and normal (Properties) Configuration 032 * 033 * @version $Id: TestEqualBehaviour.java 1224638 2011-12-25 19:51:09Z oheger $ 034 */ 035 public class TestEqualBehaviour 036 { 037 private Configuration setupSimpleConfiguration() 038 throws Exception 039 { 040 String simpleConfigurationFile = ConfigurationAssert.getTestFile("testEqual.properties").getAbsolutePath(); 041 return new PropertiesConfiguration(simpleConfigurationFile); 042 } 043 044 @SuppressWarnings("deprecation") 045 private Configuration setupCompositeConfiguration() 046 throws Exception 047 { 048 String compositeConfigurationFile = ConfigurationAssert.getTestFile("testEqualDigester.xml").getAbsolutePath(); 049 050 ConfigurationFactory configurationFactory = new ConfigurationFactory(); 051 configurationFactory.setConfigurationFileName(compositeConfigurationFile); 052 return configurationFactory.getConfiguration(); 053 } 054 055 /** 056 * Checks whether two configurations have the same size, 057 * the same key sequence and contain the same key -> value mappings 058 */ 059 private void checkEquality(String msg, Configuration c1, Configuration c2) 060 { 061 Iterator<String> it1 = c1.getKeys(); 062 Iterator<String> it2 = c2.getKeys(); 063 064 while(it1.hasNext() && it2.hasNext()) 065 { 066 String key1 = it1.next(); 067 String key2 = it2.next(); 068 assertEquals(msg + ", Keys: ", key1, key2); 069 assertEquals(msg + ", Contains: ", c1.containsKey(key1), c2.containsKey(key2)); 070 } 071 assertEquals(msg + ", Iterator: ", it1.hasNext(), it2.hasNext()); 072 } 073 074 /** 075 * Checks whether two configurations have the same key -> value mapping 076 */ 077 private void checkSameKey(String msg, String key, Configuration c1, Configuration c2) 078 { 079 String [] s1 = c1.getStringArray(key); 080 String [] s2 = c2.getStringArray(key); 081 082 assertEquals(msg + ", length: ", s1.length, s2.length); 083 084 for (int i = 0; i < s1.length ; i++) 085 { 086 assertEquals(msg + ", String Array: ", s1[i], s2[i]); 087 } 088 089 List<Object> list1 = c1.getList(key); 090 List<Object> list2 = c2.getList(key); 091 092 assertEquals(msg + ", Size: ", list1.size(), list2.size()); 093 094 Iterator<Object> it1 = list1.iterator(); 095 Iterator<Object> it2 = list2.iterator(); 096 097 while(it1.hasNext() && it2.hasNext()) 098 { 099 String val1 = (String) it1.next(); 100 String val2 = (String) it2.next(); 101 assertEquals(msg + ", List: ", val1, val2); 102 } 103 assertEquals(msg + ", Iterator End: ", it1.hasNext(), it2.hasNext()); 104 } 105 106 /** 107 * Are both configurations equal after loading? 108 */ 109 @Test 110 public void testLoading() throws Exception 111 { 112 Configuration simple = setupSimpleConfiguration(); 113 Configuration composite = setupCompositeConfiguration(); 114 115 checkEquality("testLoading", simple, composite); 116 } 117 118 /** 119 * If we delete a key, does it vanish? Does it leave all 120 * the other keys unchanged? How about an unset key? 121 */ 122 @Test 123 public void testDeletingExisting() throws Exception 124 { 125 Configuration simple = setupSimpleConfiguration(); 126 Configuration composite = setupCompositeConfiguration(); 127 128 String key = "clear.property"; 129 130 assertTrue(simple.containsKey(key)); 131 assertEquals(simple.containsKey(key), composite.containsKey(key)); 132 133 simple.clearProperty(key); 134 composite.clearProperty(key); 135 136 assertFalse(simple.containsKey(key)); 137 assertEquals(simple.containsKey(key), composite.containsKey(key)); 138 139 checkEquality("testDeletingExisting", simple, composite); 140 } 141 142 @Test 143 public void testDeletingNonExisting() throws Exception 144 { 145 Configuration simple = setupSimpleConfiguration(); 146 Configuration composite = setupCompositeConfiguration(); 147 148 String key = "nonexisting.clear.property"; 149 150 assertFalse(simple.containsKey(key)); 151 assertEquals(simple.containsKey(key), composite.containsKey(key)); 152 153 simple.clearProperty(key); 154 composite.clearProperty(key); 155 156 assertFalse(simple.containsKey(key)); 157 assertEquals(simple.containsKey(key), composite.containsKey(key)); 158 159 checkEquality("testDeletingNonExisting", simple, composite); 160 } 161 162 /** 163 * If we set a key, does it work? How about an existing 164 * key? Can we change it? 165 */ 166 @Test 167 public void testSettingNonExisting() throws Exception 168 { 169 Configuration simple = setupSimpleConfiguration(); 170 Configuration composite = setupCompositeConfiguration(); 171 172 String key = "nonexisting.property"; 173 String value = "new value"; 174 175 assertFalse(simple.containsKey(key)); 176 assertEquals(simple.containsKey(key), composite.containsKey(key)); 177 178 simple.setProperty(key, value); 179 composite.setProperty(key, value); 180 181 assertTrue(simple.containsKey(key)); 182 assertEquals(simple.containsKey(key), composite.containsKey(key)); 183 184 checkSameKey("testSettingNonExisting", key, simple, composite); 185 checkEquality("testSettingNonExisting", simple, composite); 186 } 187 188 @Test 189 public void testSettingExisting() throws Exception 190 { 191 Configuration simple = setupSimpleConfiguration(); 192 Configuration composite = setupCompositeConfiguration(); 193 194 String key = "existing.property"; 195 String value = "new value"; 196 197 assertTrue(simple.containsKey(key)); 198 assertFalse(simple.getString(key).equals(value)); 199 assertEquals(simple.containsKey(key), composite.containsKey(key)); 200 201 simple.setProperty(key, value); 202 composite.setProperty(key, value); 203 204 assertTrue(simple.containsKey(key)); 205 assertEquals(simple.getString(key), value); 206 assertEquals(simple.containsKey(key), composite.containsKey(key)); 207 208 checkSameKey("testSettingExisting", key, simple, composite); 209 checkEquality("testSettingExisting", simple, composite); 210 } 211 212 /** 213 * If we add a key, does it work? 214 */ 215 @Test 216 public void testAddingUnset() throws Exception 217 { 218 Configuration simple = setupSimpleConfiguration(); 219 Configuration composite = setupCompositeConfiguration(); 220 221 String key = "nonexisting.property"; 222 String value = "new value"; 223 224 assertFalse(simple.containsKey(key)); 225 assertEquals(simple.containsKey(key), composite.containsKey(key)); 226 227 simple.addProperty(key, value); 228 composite.addProperty(key, value); 229 230 checkSameKey("testAddingUnset", key, simple, composite); 231 checkEquality("testAddingUnset", simple, composite); 232 } 233 234 /** 235 * If we add a to an existing key, does it work? 236 */ 237 @Test 238 public void testAddingSet() throws Exception 239 { 240 Configuration simple = setupSimpleConfiguration(); 241 Configuration composite = setupCompositeConfiguration(); 242 243 String key = "existing.property"; 244 String value = "new value"; 245 246 assertTrue(simple.containsKey(key)); 247 assertEquals(simple.containsKey(key), composite.containsKey(key)); 248 249 simple.addProperty(key, value); 250 composite.addProperty(key, value); 251 252 assertTrue(simple.containsKey(key)); 253 assertEquals(simple.containsKey(key), composite.containsKey(key)); 254 255 checkSameKey("testAddingSet", key, simple, composite); 256 checkEquality("testAddingSet", simple, composite); 257 } 258 }