1 package org.apache.commons.configuration; 2 3 18 19 import java.io.File ; 20 21 import java.util.Iterator ; 22 import java.util.List ; 23 24 import junit.framework.TestCase; 25 26 import org.apache.commons.collections.IteratorUtils; 27 import org.apache.commons.lang.StringUtils; 28 import org.apache.commons.configuration.BaseConfiguration; 29 30 36 public class TestPropertiesSequence extends TestCase 37 { 38 39 public void testConfigurationValuesInSameOrderFromFile() throws Exception 40 { 41 String simpleConfigurationFile = new File ("conf/testSequence.properties").getAbsolutePath(); 42 String compositeConfigurationFile = new File ("conf/testSequenceDigester.xml").getAbsolutePath(); 43 44 Configuration simpleConfiguration = new PropertiesConfiguration(simpleConfigurationFile); 45 46 ConfigurationFactory configurationFactory = new ConfigurationFactory(); 47 configurationFactory.setConfigurationFileName(compositeConfigurationFile); 48 Configuration compositeConfiguration = configurationFactory.getConfiguration(); 49 50 Configuration a = simpleConfiguration.subset("prefix"); 51 Configuration b = compositeConfiguration.subset("prefix"); 52 53 List keysSimpleConfiguration = IteratorUtils.toList(a.getKeys()); 54 List keysCompositeConfiguration = IteratorUtils.toList(b.getKeys()); 55 56 assertTrue("Size:" + keysSimpleConfiguration.size(), keysSimpleConfiguration.size() > 0); 57 assertEquals(keysSimpleConfiguration.size(), keysCompositeConfiguration.size()); 58 59 for (int i = 0; i < keysSimpleConfiguration.size(); i++) 60 { 61 assertEquals(keysSimpleConfiguration.get(i), keysCompositeConfiguration.get(i)); 62 } 63 } 64 65 public void testConfigurationValuesInSameOrderWithManualAdd() throws Exception 66 { 67 String simpleConfigurationFile = new File ("conf/testSequence.properties").getAbsolutePath(); 68 String compositeConfigurationFile = new File ("conf/testSequenceDigester.xml").getAbsolutePath(); 69 70 Configuration simpleConfiguration = new PropertiesConfiguration(simpleConfigurationFile); 71 72 ConfigurationFactory configurationFactory = new ConfigurationFactory(); 73 configurationFactory.setConfigurationFileName(compositeConfigurationFile); 74 Configuration compositeConfiguration = configurationFactory.getConfiguration(); 75 76 simpleConfiguration.setProperty("prefix.Co.test", Boolean.TRUE); 77 simpleConfiguration.setProperty("prefix.Av.test", Boolean.TRUE); 78 79 compositeConfiguration.setProperty("prefix.Co.test", Boolean.TRUE); 80 compositeConfiguration.setProperty("prefix.Av.test", Boolean.TRUE); 81 82 Configuration a = simpleConfiguration.subset("prefix"); 83 Configuration b = compositeConfiguration.subset("prefix"); 84 85 List keysSimpleConfiguration = IteratorUtils.toList(a.getKeys()); 86 List keysCompositeConfiguration = IteratorUtils.toList(b.getKeys()); 87 88 assertTrue("Size:" + keysSimpleConfiguration.size(), keysSimpleConfiguration.size() > 0); 89 assertEquals(keysSimpleConfiguration.size(), keysCompositeConfiguration.size()); 90 91 for (int i = 0; i < keysSimpleConfiguration.size(); i++) 92 { 93 assertEquals(keysSimpleConfiguration.get(i), keysCompositeConfiguration.get(i)); 94 } 95 } 96 97 public void testMappingInSameOrder() throws Exception 98 { 99 String simpleConfigurationFile = new File ("conf/testSequence.properties").getAbsolutePath(); 100 String compositeConfigurationFile = new File ("conf/testSequenceDigester.xml").getAbsolutePath(); 101 102 Configuration simpleConfiguration = new PropertiesConfiguration(simpleConfigurationFile); 103 104 ConfigurationFactory configurationFactory = new ConfigurationFactory(); 105 configurationFactory.setConfigurationFileName(compositeConfigurationFile); 106 Configuration compositeConfiguration = configurationFactory.getConfiguration(); 107 108 Configuration mapping = new BaseConfiguration(); 109 Configuration mapping2 = new BaseConfiguration(); 110 111 for (Iterator keys = simpleConfiguration.getKeys(); keys.hasNext();) 112 { 113 String key = (String ) keys.next(); 114 String [] keyParts = StringUtils.split(key, "."); 115 116 if ((keyParts.length == 3) && keyParts[0].equals("prefix") && keyParts[2].equals("postfix")) 117 { 118 String serviceKey = keyParts[1]; 119 120 if (!mapping.containsKey(serviceKey)) 121 { 122 mapping.setProperty(serviceKey, simpleConfiguration.getString(key)); 123 } 124 } 125 } 126 127 for (Iterator keys = compositeConfiguration.getKeys(); keys.hasNext();) 128 { 129 String key = (String ) keys.next(); 130 String [] keyParts = StringUtils.split(key, "."); 131 132 if ((keyParts.length == 3) && keyParts[0].equals("prefix") && keyParts[2].equals("postfix")) 133 { 134 String serviceKey = keyParts[1]; 135 136 if (!mapping2.containsKey(serviceKey)) 137 { 138 mapping2.setProperty(serviceKey, compositeConfiguration.getString(key)); 139 } 140 } 141 } 142 } 143 } 144 | Popular Tags |