001/* 002 * Copyright (c) 2011 The Regents of the University of California. 003 * All rights reserved. 004 * 005 * 006 * Permission is hereby granted, without written agreement and without 007 * license or royalty fees, to use, copy, modify, and distribute this 008 * software and its documentation for any purpose, provided that the above 009 * copyright notice and the following two paragraphs appear in all copies 010 * of this software. 011 * 012 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 013 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 014 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 015 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 016 * SUCH DAMAGE. 017 * 018 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 019 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 020 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 021 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 022 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 023 * ENHANCEMENTS, OR MODIFICATIONS. 024 * 025 */ 026 027package org.kepler.configuration; 028 029import java.io.File; 030import java.util.ArrayList; 031import java.util.HashMap; 032import java.util.Iterator; 033import java.util.List; 034import java.util.Map; 035 036import org.apache.commons.configuration.XMLConfiguration; 037import org.kepler.build.modules.Module; 038 039/** 040 * Created by David Welker. 041 */ 042public class ConfigurationTest 043{ 044 public static String trimmedKey(String key) 045 { 046 if( !key.contains(".") ) 047 return key; 048 return key.substring(key.indexOf('.') + 1, key.length()); 049 } 050 051 public static boolean addMatch(List<String> addKeys, List<String> addedKeys, XMLConfiguration addXmlConfig, XMLConfiguration addedXmlConfig) 052 { 053 if( addKeys.size() != addedKeys.size() ) 054 return false; 055 056 HashMap<String, String> matchedKeys = new HashMap<String,String>(); 057 for( String addKey : addKeys ) 058 { 059 boolean keyMatchFound = false; 060 for( String addedKey : addedKeys ) 061 { 062 if( trimmedKey(addKey).equals(trimmedKey(addedKey)) ) 063 { 064 keyMatchFound = true; 065 matchedKeys.put(addKey, addedKey); 066 } 067 } 068 if( !keyMatchFound ) 069 return false; 070 } 071 072 for( Map.Entry<String,String> entry : matchedKeys.entrySet() ) 073 { 074 Object addProperty = addXmlConfig.getProperty(entry.getKey()); 075 Object addedProperty = addedXmlConfig.getProperty(entry.getValue()); 076 077 if( !addProperty.equals(addedProperty) ) 078 return false; 079 } 080 081 return true; 082 } 083 084 public static void addTest() throws Exception 085 { 086 System.out.println("AddTest"); 087 File configDir = Module.make("configuration-manager").getConfigurationsDir(); 088 File configDirectivesDir = new File( configDir.getParentFile(), "config-directives" ); 089 System.out.println(configDir); 090 System.out.println(configDirectivesDir); 091 System.out.println(); 092 093 File originalXml = new File(configDir, "original.xml"); 094 File addXml = new File(configDirectivesDir, "add.xml"); 095 File addedXml = new File(configDirectivesDir, "added.xml"); 096 File newXml = new File(configDirectivesDir, "new.xml"); 097 098 XMLConfiguration xmlconfig = new XMLConfiguration(); 099 xmlconfig.setDelimiterParsingDisabled(true); 100 xmlconfig.load(originalXml); 101 102 Iterator i; 103 104 XMLConfiguration addXmlConfig = new XMLConfiguration(); 105 addXmlConfig.setDelimiterParsingDisabled(true); 106 addXmlConfig.load(addXml); 107 108 XMLConfiguration addedXmlConfig = new XMLConfiguration(); 109 addedXmlConfig.setDelimiterParsingDisabled(true); 110 if( addedXml.exists() ) 111 addedXmlConfig.load(addedXml); 112 113 i = addXmlConfig.getKeys(); 114 if( !i.hasNext() ) 115 return; 116 117 List<String> firstParts = new ArrayList<String>(); 118 while( i.hasNext() ) 119 { 120 String key = (String)i.next(); 121 if( key.contains(".") ) 122 { 123 String candidate = key.substring(0, key.indexOf('.')); 124 if( !firstParts.contains(candidate)) 125 firstParts.add(candidate); 126 } 127 } 128 129 for( String firstPart : firstParts ) 130 { 131 System.out.println("firstPart = " + firstPart); 132 int maxAddIndex = addXmlConfig.getMaxIndex(firstPart); 133 int maxAddedIndex = addedXmlConfig.getMaxIndex(firstPart); 134 int addIndex = xmlconfig.getMaxIndex(firstPart) + 1; 135 136 List<String> removeKeys = new ArrayList<String>(); 137 for( int j = 0; j <= maxAddIndex; j++ ) 138 { 139 List<String> addKeys = new ArrayList<String>(); 140 Iterator x1 = addXmlConfig.getKeys(firstPart+"("+j+")"); 141 while( x1.hasNext() ) 142 { 143 String key = (String)x1.next(); 144 addKeys.add(key); 145 } 146 for( int k = 0; k <= maxAddedIndex; k++ ) 147 { 148 List<String> addedKeys = new ArrayList<String>(); 149 Iterator x2 = addedXmlConfig.getKeys(firstPart+"("+k+")"); 150 while( x2.hasNext() ) 151 { 152 String key = (String)x2.next(); 153 addedKeys.add(key); 154 } 155 156 if( addMatch(addKeys, addedKeys, addXmlConfig, addedXmlConfig) ) 157 { 158 for( String addKey : addKeys ) 159 removeKeys.add(addKey); 160 } 161 } 162 } 163 for( int j = removeKeys.size() - 1; j >= 0; j-- ) 164 { 165 String removeKey = removeKeys.get(j); 166 addXmlConfig.clearProperty(removeKey); 167 } 168 169 System.out.println("Adding config..."); 170 for( int j = 0; j <= maxAddIndex; j++ ) 171 { 172 String addXMLKey = firstPart + "("+j+")"; 173 i = addXmlConfig.getKeys(addXMLKey); 174 while( i.hasNext() ) 175 { 176 String addXmlConfigKey = (String)i.next(); 177 String lastPart = addXmlConfigKey.substring(addXmlConfigKey.indexOf('.')+1,addXmlConfigKey.length()); 178 String originalXmlConfigKey = firstPart + "("+(addIndex+j)+")."+lastPart; 179 String addedXmlConfigKey = firstPart + "("+(maxAddedIndex+1+j)+")."+lastPart; 180 xmlconfig.addProperty(originalXmlConfigKey, addXmlConfig.getProperty(addXmlConfigKey)); 181 addedXmlConfig.addProperty(addedXmlConfigKey, addXmlConfig.getProperty(addXmlConfigKey)); 182 } 183 } 184 } 185 186 System.out.println("Simple adds..."); 187 List<String> addedKeys = new ArrayList<String>(); 188 i = addedXmlConfig.getKeys(); 189 while( i.hasNext() ) 190 addedKeys.add((String)i.next()); 191 192 i = addXmlConfig.getKeys(); 193 while( i.hasNext() ) 194 { 195 String addKey = (String)i.next(); 196 if( addKey.contains(".") ) 197 continue; 198 Object value = addXmlConfig.getProperty(addKey); 199 if( addedKeys.contains(addKey) ) 200 { 201 if( addedXmlConfig.getProperty(addKey).equals(value) ) 202 continue; 203 } 204 205 xmlconfig.addProperty(addKey, value); 206 addedXmlConfig.addProperty(addKey, value); 207 } 208 209 210 addedXmlConfig.save(addedXml); 211 xmlconfig.save(newXml); 212 } 213 214 public static void changeTest() throws Exception 215 { 216 217 System.out.println("ChangeTest"); 218 File configDir = Module.make("configuration-manager").getConfigurationsDir(); 219 File configDirectivesDir = new File( configDir.getParentFile(), "config-directives" ); 220 System.out.println(configDir); 221 System.out.println(configDirectivesDir); 222 System.out.println(); 223 224 File originalXml = new File(configDir, "original.xml"); 225 File changeXml = new File(configDirectivesDir, "change.xml"); 226 File changedXml = new File(configDirectivesDir, "changed.xml"); 227 228 XMLConfiguration originalXmlConfig = new XMLConfiguration(); 229 originalXmlConfig.setDelimiterParsingDisabled(true); 230 originalXmlConfig.load(originalXml); 231 232 Iterator i; 233 234 i = originalXmlConfig.getKeys(); 235 236 System.out.println("Original.xml:"); 237 while( i.hasNext() ) 238 { 239 String key = (String)i.next(); 240 System.out.println(key + " = " + originalXmlConfig.getProperty(key)); 241 } 242 System.out.println(); 243 244 XMLConfiguration changeXmlConfig = new XMLConfiguration(); 245 changeXmlConfig.setDelimiterParsingDisabled(true); 246 changeXmlConfig.load(changeXml); 247 248 XMLConfiguration changedXmlConfig = new XMLConfiguration(); 249 changedXmlConfig.setDelimiterParsingDisabled(true); 250 if( changedXml.exists() ) 251 changedXmlConfig.load(changedXml); 252 253 i = changeXmlConfig.getKeys(); 254 255 System.out.println("Change.xml:"); 256 while( i.hasNext() ) 257 { 258 String key = (String)i.next(); 259 System.out.println(key + " = " + changeXmlConfig.getProperty(key)); 260 } 261 System.out.println(); 262 263 i = changeXmlConfig.getKeys(); 264 while( i.hasNext() ) 265 { 266 String key = (String)i.next(); 267 Object value = changeXmlConfig.getProperty(key); 268 Object changed = changedXmlConfig.getProperty(key); 269 if( changed == null || !value.equals(changed) ) 270 { 271 originalXmlConfig.setProperty(key,value); 272 changedXmlConfig.setProperty(key,value); 273 } 274 } 275 276 i = originalXmlConfig.getKeys(); 277 278 System.out.println("New Config:"); 279 while( i.hasNext() ) 280 { 281 String key = (String)i.next(); 282 System.out.println(key + " = " + originalXmlConfig.getProperty(key)); 283 } 284 System.out.println(); 285 286 i = changedXmlConfig.getKeys(); 287 288 System.out.println("Changed.xml"); 289 while( i.hasNext() ) 290 { 291 String key = (String)i.next(); 292 System.out.println(key + " = " + changedXmlConfig.getProperty(key)); 293 } 294 System.out.println(); 295 296 changedXmlConfig.save(changedXml); 297 } 298 299 public static void removeTest() throws Exception 300 { 301 302 System.out.println("RemoveTest"); 303 File configDir = Module.make("configuration-manager").getConfigurationsDir(); 304 File configDirectivesDir = new File( configDir.getParentFile(), "config-directives" ); 305 System.out.println(configDir); 306 System.out.println(configDirectivesDir); 307 System.out.println(); 308 309 File originalXml = new File(configDir, "original.xml"); 310 File removeXml = new File(configDirectivesDir, "remove.xml"); 311 File removedXml = new File(configDirectivesDir, "removed.xml"); 312 File newXml = new File(configDirectivesDir, "new.xml"); 313 314 XMLConfiguration originalXmlConfig = new XMLConfiguration(); 315 originalXmlConfig.setDelimiterParsingDisabled(true); 316 originalXmlConfig.load(originalXml); 317 318 Iterator i; 319 320 XMLConfiguration removeXmlConfig = new XMLConfiguration(); 321 removeXmlConfig.setDelimiterParsingDisabled(true); 322 removeXmlConfig.load(removeXml); 323 324 XMLConfiguration removedXmlConfig = new XMLConfiguration(); 325 removedXmlConfig.setDelimiterParsingDisabled(true); 326 if( removedXml.exists() ) 327 removedXmlConfig.load(removedXml); 328 329 i = removeXmlConfig.getKeys(); 330 while( i.hasNext() ) 331 { 332 String key = (String)i.next(); 333 Object value = removeXmlConfig.getProperty(key); 334 Object removed = removedXmlConfig.getProperty(key); 335 if( removed == null || !value.equals(removed) ) 336 { 337 originalXmlConfig.clearProperty(key); 338 removedXmlConfig.setProperty(key,value); 339 } 340 } 341 342 removedXmlConfig.save(removedXml); 343 originalXmlConfig.save(newXml); 344 } 345 346 347 public static void main(String[] args) throws Exception 348 { 349 addTest(); 350 //changeTest(); 351 //removeTest(); 352 } 353 354}