1 17 package org.alfresco.config.xml; 18 19 import java.util.ArrayList ; 20 import java.util.List ; 21 import java.util.Map ; 22 23 import org.alfresco.config.Config; 24 import org.alfresco.config.ConfigElement; 25 import org.alfresco.config.ConfigException; 26 import org.alfresco.config.ConfigLookupContext; 27 import org.alfresco.config.source.ClassPathConfigSource; 28 import org.alfresco.config.source.FileConfigSource; 29 import org.alfresco.config.source.HTTPConfigSource; 30 import org.alfresco.config.source.UrlConfigSource; 31 import org.alfresco.util.BaseTest; 32 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 34 35 40 public class XMLConfigServiceTest extends BaseTest 41 { 42 private static Log logger = LogFactory.getLog(XMLConfigServiceTest.class); 43 44 47 protected void setUp() throws Exception 48 { 49 super.setUp(); 50 } 51 52 55 public void testConfig() 56 { 57 String configFile = getResourcesDir() + "config.xml"; 59 XMLConfigService svc = new XMLConfigService(new FileConfigSource(configFile)); 60 svc.init(); 61 62 Config global = svc.getGlobalConfig(); 64 ConfigElement globalItem = global.getConfigElement("global-item"); 65 assertNotNull("globalItem should not be null", globalItem); 66 assertEquals("The global-item value should be 'The global value'", "The global value", globalItem.getValue()); 67 68 ConfigElement overrideItem = global.getConfigElement("override"); 70 assertNotNull("overrideItem should not be null", overrideItem); 71 assertEquals("The override item should be false", "false", overrideItem.getValue()); 72 73 Config unitTest = svc.getConfig("Unit Test"); 76 assertNotNull("unitTest config result should not be null", unitTest); 77 ConfigElement item = unitTest.getConfigElement("item"); 78 assertNotNull("item should not be null", item); 79 assertEquals("The item value should be 'The value'", "The value", item.getValue()); 80 81 overrideItem = unitTest.getConfigElement("override"); 83 assertNotNull("overrideItem should not be null", overrideItem); 84 assertEquals("The override item should now be true", "true", overrideItem.getValue()); 85 } 86 87 90 public void testMissingFiles() 91 { 92 String configFile = "file:" + getResourcesDir() + "missing.xml"; 94 XMLConfigService svc = new XMLConfigService(new UrlConfigSource(configFile)); 95 svc.init(); 96 97 Config global = svc.getGlobalConfig(); 99 assertNotNull("Global config should not be null", global); 100 assertEquals("There shouldn't be any config elements for global", 0, 101 global.getConfigElements().size()); 102 103 Config cfg = svc.getConfig("Nothing"); 104 assertNotNull("Config for Nothing should not be null", cfg); 105 assertEquals("There shouldn't be any config elements for 'Nothing'", 0, 106 cfg.getConfigElements().size()); 107 108 configFile = "classpath:alfresco/missing.xml"; 110 svc = new XMLConfigService(new UrlConfigSource(configFile)); 111 svc.init(); 112 113 global = svc.getGlobalConfig(); 115 assertNotNull("Global config should not be null", global); 116 assertEquals("There shouldn't be any config elements for global", 0, 117 global.getConfigElements().size()); 118 119 cfg = svc.getConfig("Nothing"); 120 assertNotNull("Config for Nothing should not be null", cfg); 121 assertEquals("There shouldn't be any config elements for 'Nothing'", 0, 122 cfg.getConfigElements().size()); 123 124 configFile = "http://localhost:8080/missing.xml"; 126 svc = new XMLConfigService(new UrlConfigSource(configFile)); 127 svc.init(); 128 129 global = svc.getGlobalConfig(); 131 assertNotNull("Global config should not be null", global); 132 assertEquals("There shouldn't be any config elements for global", 0, 133 global.getConfigElements().size()); 134 135 cfg = svc.getConfig("Nothing"); 136 assertNotNull("Config for Nothing should not be null", cfg); 137 assertEquals("There shouldn't be any config elements for 'Nothing'", 0, 138 cfg.getConfigElements().size()); 139 } 140 141 144 public void testGetNamedChild() 145 { 146 String configFile = getResourcesDir() + "config.xml"; 148 XMLConfigService svc = new XMLConfigService(new FileConfigSource(configFile)); 149 svc.init(); 150 151 Config cfg = svc.getConfig("Named Child Test"); 153 assertNotNull("Named child test config should not be null", cfg); 154 155 ConfigElement children = cfg.getConfigElement("children"); 157 assertEquals("There should be four children", 4, children.getChildCount()); 159 160 ConfigElement childTwo = children.getChild("child-two"); 162 assertNotNull("Child two config element should not be null", childTwo); 163 assertEquals("Child two value should be 'child two value'", "child two value", 164 childTwo.getValue()); 165 assertEquals("The number of attributes should be 0", 0, childTwo.getAttributeCount()); 166 167 ConfigElement noChild = children.getChild("not-there"); 169 assertNull("The noChild config element should be null", noChild); 170 171 ConfigElement childThree = children.getChild("child-three"); 173 assertNotNull("Child three config element should not be null", childThree); 174 ConfigElement grandKids = childThree.getChild("grand-children"); 175 assertNotNull("Grand child config element should not be null", grandKids); 176 assertEquals("There should be 2 grand child config elements", 2, 177 grandKids.getChildCount()); 178 ConfigElement grandKidOne = grandKids.getChild("grand-child-one"); 179 assertNotNull("Grand child one config element should not be null", grandKidOne); 180 assertEquals("The number of attributes for grand child one should be 1", 181 1, grandKidOne.getAttributeCount()); 182 assertEquals("The number of children for grand child one should be 0", 183 0, grandKidOne.getChildCount()); 184 assertTrue("The attribute 'an-attribute' should be present", 185 grandKidOne.getAttribute("an-attribute") != null); 186 } 187 188 191 public void testReset() 192 { 193 String configFile = getResourcesDir() + "config.xml"; 195 XMLConfigService svc = new XMLConfigService(new FileConfigSource(configFile)); 196 svc.init(); 197 198 Config unitTest = svc.getConfig("Unit Test"); 200 assertNotNull("unitTest should not be null", unitTest); 201 202 svc.reset(); 204 unitTest = svc.getConfig("Unit Test"); 205 assertNotNull("unitTest should not be null", unitTest); 206 } 207 208 213 public void xtestClasspathSource() 214 { 215 String configFile = "org/alfresco/config-classpath.xml"; 216 XMLConfigService svc = new XMLConfigService(new ClassPathConfigSource(configFile)); 217 svc.init(); 218 219 Config config = svc.getGlobalConfig(); 220 assertNotNull(config); 221 } 222 223 228 public void xtestHTTPSource() 229 { 230 List <String > configFile = new ArrayList <String >(1); 231 configFile.add("http://localhost:8080/alfresco/config-http.xml"); 232 XMLConfigService svc = new XMLConfigService(new HTTPConfigSource(configFile)); 233 svc.init(); 234 235 Config config = svc.getGlobalConfig(); 236 assertNotNull(config); 237 } 238 239 243 public void testMultiConfig() 244 { 245 List <String > configFiles = new ArrayList <String >(2); 247 configFiles.add(getResourcesDir() + "config.xml"); 248 configFiles.add(getResourcesDir() + "config-multi.xml"); 249 XMLConfigService svc = new XMLConfigService(new FileConfigSource(configFiles)); 250 svc.init(); 251 252 Config globalSection = svc.getGlobalConfig(); 254 255 ConfigElement globalItem = globalSection.getConfigElement("global-item"); 257 assertNotNull("globalItem should not be null", globalItem); 258 assertEquals("The global-item value should be 'The global value'", "The global value", globalItem.getValue()); 259 260 ConfigElement globalItem2 = globalSection.getConfigElement("another-global-item"); 261 assertNotNull("globalItem2 should not be null", globalItem2); 262 assertEquals("The another-global-item value should be 'Another global value'", "Another global value", 263 globalItem2.getValue()); 264 265 Config unitTest = svc.getConfig("Unit Test"); 270 assertNotNull("unitTest should not be null", unitTest); 271 ConfigElement item = unitTest.getConfigElement("item"); 272 assertNotNull("item should not be null", item); 273 ConfigElement anotherItem = unitTest.getConfigElement("another-item"); 274 assertNotNull("another-item should not be null", anotherItem); 275 } 276 277 280 public void testAreaConfig() 281 { 282 List <String > configFiles = new ArrayList <String >(2); 284 configFiles.add(getResourcesDir() + "config.xml"); 285 configFiles.add(getResourcesDir() + "config-areas.xml"); 286 XMLConfigService svc = new XMLConfigService(new FileConfigSource(configFiles)); 287 svc.init(); 288 289 Config config = svc.getConfig("Restricted Area Test"); 292 ConfigElement restrictedElement = config.getConfigElement("restricted"); 293 ConfigElement availableElement = config.getConfigElement("available"); 294 assertNull("restrictedElement should be null as a global lookup was performed for a section in an area", restrictedElement); 295 assertNotNull("availableElement should not be null as the element is available in the default area", availableElement); 296 297 ConfigLookupContext lookupContext = new ConfigLookupContext(); 300 lookupContext.addArea("test-area"); 301 config = svc.getConfig("Area Specific Config", lookupContext); 302 ConfigElement areaTest = config.getConfigElement("parent-item"); 303 assertNotNull("areaTest should not be null as it is defined in test-area", areaTest); 304 305 config = svc.getConfig("Unit Test", lookupContext); 308 ConfigElement unitTest = config.getConfigElement("item"); 309 assertNull("unitTest should be null as it is not defined in test-area", unitTest); 310 311 try 314 { 315 Config notThere = svc.getConfig("Unit Test", new ConfigLookupContext("not-there")); 316 fail("Retrieving a non existent area should have thrown an exception!"); 317 } 318 catch (ConfigException ce) 319 { 320 } 322 } 323 324 327 public void testMerging() 328 { 329 List <String > configFiles = new ArrayList <String >(2); 331 configFiles.add(getResourcesDir() + "config.xml"); 332 configFiles.add(getResourcesDir() + "config-multi.xml"); 333 XMLConfigService svc = new XMLConfigService(new FileConfigSource(configFiles)); 334 svc.init(); 335 336 Config globalSection = svc.getGlobalConfig(); 338 assertNotNull("global section should not be null", globalSection); 339 340 ConfigElement overrideItem = globalSection.getConfigElement("override"); 343 assertNotNull("overrideItem should not be null", overrideItem); 344 assertEquals("The override item should be true", "true", overrideItem.getValue()); 345 346 ConfigElement mergeChildren = globalSection.getConfigElement("merge-children"); 348 assertNotNull("mergeChildren should not be null", mergeChildren); 349 List <ConfigElement> kids = mergeChildren.getChildren(); 350 assertEquals("There should be 2 children", 2, kids.size()); 351 352 Config mergeTest = svc.getConfig("Merge Test"); 354 assertNotNull("Merge test config should not be null", mergeTest); 355 356 ConfigElement first = mergeTest.getConfigElement("first-item"); 358 ConfigElement second = mergeTest.getConfigElement("second-item"); 359 ConfigElement third = mergeTest.getConfigElement("third-item"); 360 ConfigElement fourth = mergeTest.getConfigElement("fourth-item"); 361 assertNotNull("first should not be null", first); 362 assertNotNull("second should not be null", second); 363 assertNotNull("third should not be null", third); 364 assertNotNull("fourth should not be null", fourth); 365 366 String firstValue = first.getValue(); 368 assertEquals("The first value is wrong", "the overridden first value", firstValue); 369 370 ConfigElement children = mergeTest.getConfigElement("children"); 372 assertNotNull("children should not be null", children); 373 kids = children.getChildren(); 374 assertEquals("There should be 3 children", 3, kids.size()); 375 } 376 377 380 public void testReplace() 381 { 382 List <String > configFiles = new ArrayList <String >(2); 384 configFiles.add(getResourcesDir() + "config.xml"); 385 configFiles.add(getResourcesDir() + "config-replace.xml"); 386 XMLConfigService svc = new XMLConfigService(new FileConfigSource(configFiles)); 387 svc.init(); 388 389 Config globalSection = svc.getGlobalConfig(); 391 assertNotNull("global section should not be null", globalSection); 392 393 ConfigElement globalItem = globalSection.getConfigElement("global-item"); 395 assertEquals("global-item", "The replaced global value", globalItem.getValue()); 396 397 ConfigElement override = globalSection.getConfigElement("override"); 399 assertNotNull("override should not be null", override); 400 assertEquals("override element", "false", override.getValue()); 401 402 ConfigElement childrenReplace = globalSection.getConfigElement("children-replace"); 404 assertNotNull("childrenReplace should not be null", childrenReplace); 405 List <ConfigElement> children = childrenReplace.getChildren(); 406 assertEquals("number of children elements", 1, children.size()); 407 408 ConfigElement customChild = children.get(0); 410 assertEquals("custom child element value", "child custom value", customChild.getValue()); 411 412 Config replaceTestCfg = svc.getConfig("Replace Test"); 414 assertNotNull("Replace Test should not be null", replaceTestCfg); 415 416 Map <String , Object > elements = replaceTestCfg.getConfigElements(); 418 assertEquals("number of elements", 9, elements.size()); 419 420 assertEquals("first-item", "the replaced first value", replaceTestCfg. 422 getConfigElement("first-item").getValue()); 423 424 assertEquals("second-item", "second value", replaceTestCfg. 426 getConfigElement("second-item").getValue()); 427 428 assertEquals("fourth-item", "new fourth value", replaceTestCfg. 430 getConfigElement("fourth-item").getValue()); 431 432 ConfigElement childrenElement = replaceTestCfg.getConfigElement("children"); 434 assertEquals("number of children of children", 2, childrenElement.getChildCount()); 435 436 assertEquals("child two name", "child-two", childrenElement.getChildren().get(0).getName()); 438 assertEquals("child two value", "child two value", childrenElement.getChildren().get(0).getValue()); 439 assertEquals("child three name", "child-three", childrenElement.getChildren().get(1).getName()); 440 assertEquals("child three value", "child three value", childrenElement.getChildren().get(1).getValue()); 441 } 442 } 443 | Popular Tags |