KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > config > xml > XMLConfigServiceTest


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.config.xml;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
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 /**
36  * Unit tests for the XML based configuration service
37  *
38  * @author gavinc
39  */

40 public class XMLConfigServiceTest extends BaseTest
41 {
42     private static Log logger = LogFactory.getLog(XMLConfigServiceTest.class);
43     
44     /**
45      * @see junit.framework.TestCase#setUp()
46      */

47     protected void setUp() throws Exception JavaDoc
48     {
49         super.setUp();
50     }
51
52     /**
53      * Tests the config.xml file
54      */

55     public void testConfig()
56     {
57         // setup the config service
58
String JavaDoc configFile = getResourcesDir() + "config.xml";
59         XMLConfigService svc = new XMLConfigService(new FileConfigSource(configFile));
60         svc.init();
61
62         // try and get the global item
63
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         // try and get the override item
69
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         // test the string evaluator by getting the item config element
74
// in the "Unit Test" config section
75
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         // make sure the override value has changed when retrieved from item
82
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     /**
88      * Tests the absence of referenced files
89      */

90     public void testMissingFiles()
91     {
92         // setup the config service using a missing file source
93
String JavaDoc configFile = "file:" + getResourcesDir() + "missing.xml";
94         XMLConfigService svc = new XMLConfigService(new UrlConfigSource(configFile));
95         svc.init();
96
97         // make sure attempts to retrieve config don't fail
98
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         // do the same test for a classpath resource
109
configFile = "classpath:alfresco/missing.xml";
110         svc = new XMLConfigService(new UrlConfigSource(configFile));
111         svc.init();
112
113         // make sure attempts to retrieve config don't fail
114
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         // do the same test for a HTTP resource
125
configFile = "http://localhost:8080/missing.xml";
126         svc = new XMLConfigService(new UrlConfigSource(configFile));
127         svc.init();
128
129         // make sure attempts to retrieve config don't fail
130
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     /**
142      * Tests the retrieval of a named child
143      */

144     public void testGetNamedChild()
145     {
146        // setup the config service
147
String JavaDoc configFile = getResourcesDir() + "config.xml";
148         XMLConfigService svc = new XMLConfigService(new FileConfigSource(configFile));
149         svc.init();
150         
151         // get the "Named Child Test" config
152
Config cfg = svc.getConfig("Named Child Test");
153         assertNotNull("Named child test config should not be null", cfg);
154         
155         // get the children config element
156
ConfigElement children = cfg.getConfigElement("children");
157         // check the getNumberOfChildren method works
158
assertEquals("There should be four children", 4, children.getChildCount());
159         
160         // try and get a named child
161
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         // try and get a non existent child and check its null
168
ConfigElement noChild = children.getChild("not-there");
169         assertNull("The noChild config element should be null", noChild);
170         
171         // test the retrieval of grand children
172
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     /**
189      * Tests the config service's ability to reset
190      */

191     public void testReset()
192     {
193        // setup the config service
194
String JavaDoc configFile = getResourcesDir() + "config.xml";
195         XMLConfigService svc = new XMLConfigService(new FileConfigSource(configFile));
196         svc.init();
197
198         // try and get the global item
199
Config unitTest = svc.getConfig("Unit Test");
200         assertNotNull("unitTest should not be null", unitTest);
201         
202         // reset the config service then try to retrieve some config again
203
svc.reset();
204         unitTest = svc.getConfig("Unit Test");
205         assertNotNull("unitTest should not be null", unitTest);
206     }
207
208     /**
209      * Tests the use of the class path source config
210      *
211      * TODO: Enable this test when we have a classpath config resource to load!
212      */

213     public void xtestClasspathSource()
214     {
215         String JavaDoc 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     /**
224      * Tests the use of the HTTP source config
225      *
226      * TODO: Enable this test when we have an HTTP config resource to load!
227      */

228     public void xtestHTTPSource()
229     {
230         List JavaDoc<String JavaDoc> configFile = new ArrayList JavaDoc<String JavaDoc>(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     /**
240      * Tests the config service's ability to load multiple files and merge the
241      * results
242      */

243     public void testMultiConfig()
244     {
245         // setup the config service
246
List JavaDoc<String JavaDoc> configFiles = new ArrayList JavaDoc<String JavaDoc>(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         // try and get the global config section
253
Config globalSection = svc.getGlobalConfig();
254
255         // try and get items from the global section defined in each file
256
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         // lookup the "Unit Test" section, this should match a section in each
266
// file so
267
// we should be able to get hold of config elements "item" and
268
// "another-item"
269
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     /**
278      * Tests the config service's ability to restrict searches to a named area
279      */

280     public void testAreaConfig()
281     {
282         // setup the config service
283
List JavaDoc<String JavaDoc> configFiles = new ArrayList JavaDoc<String JavaDoc>(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         // try and get a section defined in an area (without restricting the
290
// area), the result should be null
291
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         // try and get an section defined in an area (with an area restricted
298
// search)
299
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         // try and find a section defined outside an area with an area
306
// restricted search
307
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 and find some config in area that has not been defined, ensure we
312
// get an error
313
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             // expected to get this error
321
}
322     }
323
324     /**
325      * Tests the merge features of the config service
326      */

327     public void testMerging()
328     {
329         // setup the config service
330
List JavaDoc<String JavaDoc> configFiles = new ArrayList JavaDoc<String JavaDoc>(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         // try and get the global config section
337
Config globalSection = svc.getGlobalConfig();
338         assertNotNull("global section should not be null", globalSection);
339         
340         // make sure that the override config value got overridden in the global
341
// section
342
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         // make sure the global section gets merged properly
347
ConfigElement mergeChildren = globalSection.getConfigElement("merge-children");
348         assertNotNull("mergeChildren should not be null", mergeChildren);
349         List JavaDoc<ConfigElement> kids = mergeChildren.getChildren();
350         assertEquals("There should be 2 children", 2, kids.size());
351         
352         // get the merge test config section
353
Config mergeTest = svc.getConfig("Merge Test");
354         assertNotNull("Merge test config should not be null", mergeTest);
355         
356         // check that there is a first, second, thrid and fourth config element
357
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         // test that the first-item got overridden
367
String JavaDoc firstValue = first.getValue();
368         assertEquals("The first value is wrong", "the overridden first value", firstValue);
369         
370         // test that there are two child items under the children config element
371
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     /**
378      * Tests the replace feature of the config service
379      */

380     public void testReplace()
381     {
382         // setup the config service
383
List JavaDoc<String JavaDoc> configFiles = new ArrayList JavaDoc<String JavaDoc>(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         // try and get the global config section
390
Config globalSection = svc.getGlobalConfig();
391         assertNotNull("global section should not be null", globalSection);
392
393         // make sure the global-item value has changed
394
ConfigElement globalItem = globalSection.getConfigElement("global-item");
395         assertEquals("global-item", "The replaced global value", globalItem.getValue());
396         
397         // make sure the override element is still present with the same value
398
ConfigElement override = globalSection.getConfigElement("override");
399         assertNotNull("override should not be null", override);
400         assertEquals("override element", "false", override.getValue());
401         
402         // make sure the children-replace element only has 1 child and not 4
403
ConfigElement childrenReplace = globalSection.getConfigElement("children-replace");
404         assertNotNull("childrenReplace should not be null", childrenReplace);
405         List JavaDoc<ConfigElement> children = childrenReplace.getChildren();
406         assertEquals("number of children elements", 1, children.size());
407         
408         // make sure the child is the correct one
409
ConfigElement customChild = children.get(0);
410         assertEquals("custom child element value", "child custom value", customChild.getValue());
411         
412         // make sure the config section is still present
413
Config replaceTestCfg = svc.getConfig("Replace Test");
414         assertNotNull("Replace Test should not be null", replaceTestCfg);
415         
416         // make sure there are 9 elements in the replaced section (including the global section)
417
Map JavaDoc<String JavaDoc, Object JavaDoc> elements = replaceTestCfg.getConfigElements();
418         assertEquals("number of elements", 9, elements.size());
419         
420         // make sure first-item is different
421
assertEquals("first-item", "the replaced first value", replaceTestCfg.
422               getConfigElement("first-item").getValue());
423         
424         // make sure second-item is the same
425
assertEquals("second-item", "second value", replaceTestCfg.
426               getConfigElement("second-item").getValue());
427         
428         // make sure there is a fourth-item is now present
429
assertEquals("fourth-item", "new fourth value", replaceTestCfg.
430               getConfigElement("fourth-item").getValue());
431         
432         // make sure the children config now has 2 children
433
ConfigElement childrenElement = replaceTestCfg.getConfigElement("children");
434         assertEquals("number of children of children", 2, childrenElement.getChildCount());
435         
436         // make sure the two child elements are correct
437
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