KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > CruiseControlConfigTest


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 package net.sourceforge.cruisecontrol;
38
39 import java.io.File JavaDoc;
40 import java.net.URL JavaDoc;
41 import java.net.URLDecoder JavaDoc;
42 import java.util.Enumeration JavaDoc;
43 import java.util.Iterator JavaDoc;
44 import java.util.List JavaDoc;
45 import java.util.Properties JavaDoc;
46 import java.util.Set JavaDoc;
47
48 import junit.framework.TestCase;
49 import net.sourceforge.cruisecontrol.labelincrementers.DefaultLabelIncrementer;
50 import net.sourceforge.cruisecontrol.listeners.ListenerTestNestedPlugin;
51 import net.sourceforge.cruisecontrol.listeners.ListenerTestOtherNestedPlugin;
52 import net.sourceforge.cruisecontrol.listeners.ListenerTestPlugin;
53 import net.sourceforge.cruisecontrol.listeners.ListenerTestSelfConfiguringPlugin;
54 import net.sourceforge.cruisecontrol.util.Util;
55
56 import org.jdom.Element;
57
58 public class CruiseControlConfigTest extends TestCase {
59
60     private CruiseControlConfig config;
61     private File JavaDoc configFile;
62     private File JavaDoc tempDirectory;
63     private File JavaDoc propertiesFile;
64
65     private static final int ONE_SECOND = 1000;
66
67     protected void setUp() throws Exception JavaDoc {
68         URL JavaDoc url;
69         url = this.getClass().getClassLoader().getResource("net/sourceforge/cruisecontrol/test.properties");
70         propertiesFile = new File JavaDoc(URLDecoder.decode(url.getPath()));
71
72         // Set up a CruiseControl config file for testing
73
url = this.getClass().getClassLoader().getResource("net/sourceforge/cruisecontrol/testconfig.xml");
74         configFile = new File JavaDoc(URLDecoder.decode(url.getPath()));
75         tempDirectory = configFile.getParentFile();
76
77         Element rootElement = Util.loadConfigFile(configFile);
78         Properties JavaDoc globalProperties = new Properties JavaDoc();
79         globalProperties.put("test.properties.dir", propertiesFile.getParentFile().getAbsolutePath());
80         config = new CruiseControlConfig(globalProperties);
81         config.configure(rootElement);
82     }
83
84     protected void tearDown() {
85         // The directory "foo" in the system's temporary file location
86
// is created by CruiseControl when using the config file below.
87
// Specifically because of the line:
88
// <log dir='" + tempDirPath + "/foo' encoding='utf-8' >
89
File JavaDoc fooDirectory = new File JavaDoc(tempDirectory, "foo");
90         fooDirectory.delete();
91     }
92     
93     public void testProjectNamesShouldMatchOrderInFile() {
94         Set JavaDoc names = config.getProjectNames();
95         Iterator JavaDoc iter = names.iterator();
96         assertEquals("project1", (String JavaDoc) iter.next());
97         assertEquals("project2", (String JavaDoc) iter.next());
98         assertEquals("project3", (String JavaDoc) iter.next());
99         assertEquals("project3bis", (String JavaDoc) iter.next());
100         assertEquals("project4", (String JavaDoc) iter.next());
101     }
102
103     public void testGetProjectNames() {
104         assertEquals(16, config.getProjectNames().size());
105     }
106
107     public void testGlobalProperty() throws CruiseControlException {
108         ProjectConfig projConfig = config.getConfig("simpleprops");
109         Properties JavaDoc props = projConfig.getProperties();
110         assertEquals(5, props.size()); // 4 in file, one global passed to consutructor
111
assertEquals("works!", props.getProperty("global"));
112     }
113
114     public void testProjectNameProperty() throws CruiseControlException {
115         ProjectConfig projConfig = config.getConfig("project1");
116         Properties JavaDoc props = projConfig.getProperties();
117         assertEquals(4, props.size());
118         assertEquals("project1", props.getProperty("project.name"));
119     }
120
121     public void testProjectNameInGlobalProperty() throws CruiseControlException {
122         ProjectConfig projConfig = config.getConfig("project1");
123         Properties JavaDoc props = projConfig.getProperties();
124         assertEquals(4, props.size());
125         assertEquals("works!", props.getProperty("global"));
126         assertEquals("project1", props.getProperty("project.name"));
127         assertEquals("project=project1", props.getProperty("project.global"));
128     }
129
130     public void testSimpleProperty() throws CruiseControlException {
131         ProjectConfig projConfig = config.getConfig("simpleprops");
132         Properties JavaDoc props = projConfig.getProperties();
133         assertEquals(5, props.size());
134         assertEquals("success!", props.getProperty("simple"));
135     }
136
137     public void testMultipleProperties() throws CruiseControlException {
138         ProjectConfig projConfig = config.getConfig("multiprops");
139         Properties JavaDoc props = projConfig.getProperties();
140         assertEquals(8, props.size());
141         assertEquals("one", props.getProperty("first"));
142         assertEquals("two", props.getProperty("second"));
143         assertEquals("three", props.getProperty("third"));
144         assertEquals("one.two$three", props.getProperty("multi"));
145     }
146
147     public void testNestedProperties() throws CruiseControlException {
148         ProjectConfig projConfig = config.getConfig("nestedprops");
149         Properties JavaDoc props = projConfig.getProperties();
150         assertEquals(10, props.size());
151         assertEquals("one", props.getProperty("first"));
152         assertEquals("two", props.getProperty("second"));
153         assertEquals("three", props.getProperty("third"));
154         assertEquals("almost", props.getProperty("one.two.three"));
155         assertEquals("threeLevelsDeep", props.getProperty("almost"));
156         assertEquals("threeLevelsDeep", props.getProperty("nested"));
157     }
158
159     public void testPropertyEclipsing() throws CruiseControlException {
160         ProjectConfig projConfig = config.getConfig("eclipseprop");
161         Properties JavaDoc props = projConfig.getProperties();
162         assertEquals(4, props.size());
163         assertEquals("eclipsed", props.getProperty("global"));
164     }
165     
166     public void testLoadPropertiesFromFile() throws CruiseControlException {
167         ProjectConfig projConfig = config.getConfig("propsfromfile");
168         Properties JavaDoc props = projConfig.getProperties();
169         assertEquals(9, props.size());
170         assertEquals("/home/cruise", props.getProperty("dir1"));
171         assertEquals("/home/cruise/logs", props.getProperty("dir2"));
172         assertEquals("temp", props.getProperty("tempdir"));
173         assertEquals("/home/cruise/logs/temp", props.getProperty("multi"));
174     }
175
176     // test that we are capable of resolving properties in all property attributes
177
public void testPropertiesInProperties() throws CruiseControlException {
178         ProjectConfig projConfig = config.getConfig("propsinpropsdef");
179         Properties JavaDoc props = projConfig.getProperties();
180         // these ones where defined normally, shouldn't be any problem
181
assertEquals("true", props.getProperty("env.toupper"));
182         assertEquals("env", props.getProperty("env.prefix"));
183
184         assertEquals("Resolving property file name attribute worked",
185                      "/home/cruise", props.getProperty("dir1"));
186         assertEquals("Resolving property name attribute worked",
187                       "test1", props.getProperty("test1"));
188         int nbEnvPropertiesFound = 0;
189         for (Enumeration JavaDoc propertyNames = props.propertyNames(); propertyNames.hasMoreElements(); ) {
190            String JavaDoc name = (String JavaDoc) propertyNames.nextElement();
191            if (name.startsWith("env.")) {
192              nbEnvPropertiesFound++;
193            }
194         }
195         assertTrue("Resolving environment prefix attribute worked",
196                        nbEnvPropertiesFound > 0);
197         assertNotNull("Resolving environment prefix and touuper attributes worked",
198                        props.getProperty("env.PATH"));
199     }
200
201     // TODO backport
202
/*
203     public void testMissingProperty() {
204         // there's in fact little need to check for both cases.
205         // This will be hardcoded at some point and the default case.
206         // Feel free to scrap the first if when checking it in.
207         if (ProjectXMLHelper.FAIL_UPON_MISSING_PROPERTY) {
208             try {
209                 createProjectXMLHelper("missingprop");
210                 fail("A missing property should cause an exception!");
211             } catch (CruiseControlException expected) {
212             }
213         } else {
214             try {
215                 createProjectXMLHelper("missingprop");
216             } catch (CruiseControlException unexpected) {
217                 fail(unexpected.getMessage());
218             }
219         }
220     }
221     */

222
223     // TODO this a test of the PluginHelper
224
public void testGetPluginConfigNoOverride() throws Exception JavaDoc {
225         ProjectConfig projConfig = config.getConfig("project1");
226         PluginRegistry registry = config.getProjectPlugins("project1");
227
228         assertEquals(ListenerTestNestedPlugin.class, registry.getPluginClass("testnested"));
229
230         final ProjectXMLHelper helper
231             = new ProjectXMLHelper(projConfig.getProperties(), registry);
232
233         PluginXMLHelper pluginHelper = new PluginXMLHelper(helper);
234         Object JavaDoc plugin;
235
236         plugin = helper.getConfiguredPlugin(pluginHelper, "testnested");
237         assertEquals(ListenerTestNestedPlugin.class, plugin.getClass());
238         ListenerTestNestedPlugin plug1 = (ListenerTestNestedPlugin) plugin;
239         assertEquals("default", plug1.getString());
240         assertEquals("otherdefault", plug1.getOtherString());
241
242         plugin = helper.getConfiguredPlugin(pluginHelper, "testselfconfiguring");
243         assertEquals(null, plugin);
244
245         plugin = helper.getConfiguredPlugin(pluginHelper, "testlistener");
246         assertEquals(null, plugin);
247     }
248
249     // TODO this a test of the PluginHelper
250
public void testGetPluginConfig() throws Exception JavaDoc {
251         ProjectConfig projConfig = config.getConfig("project4");
252         PluginRegistry registry = config.getProjectPlugins("project4");
253
254         final ProjectXMLHelper helper
255             = new ProjectXMLHelper(projConfig.getProperties(), registry);
256
257         PluginXMLHelper pluginHelper = new PluginXMLHelper(helper);
258         Object JavaDoc plugin;
259
260         plugin = helper.getConfiguredPlugin(pluginHelper, "testnested");
261         assertEquals(ListenerTestNestedPlugin.class, plugin.getClass());
262         ListenerTestNestedPlugin plug1 = (ListenerTestNestedPlugin) plugin;
263         assertEquals("overriden", plug1.getString());
264         // not overriden
265
assertEquals("otherdefault", plug1.getOtherString());
266
267         plugin = helper.getConfiguredPlugin(pluginHelper, "testselfconfiguring");
268         assertEquals(ListenerTestSelfConfiguringPlugin.class, plugin.getClass());
269         ListenerTestSelfConfiguringPlugin plug2 = (ListenerTestSelfConfiguringPlugin) plugin;
270         assertEquals(null, plug2.getString());
271         assertEquals(null, plug2.getNested());
272
273         plugin = helper.getConfiguredPlugin(pluginHelper, "testlistener");
274         assertEquals(ListenerTestPlugin.class, plugin.getClass());
275         ListenerTestPlugin plug3 = (ListenerTestPlugin) plugin;
276         assertEquals("project4-0", plug3.getString());
277     }
278
279     public void testPluginConfiguration() throws Exception JavaDoc {
280         ProjectConfig projConfig = config.getConfig("project4");
281         PluginRegistry plugins = config.getProjectPlugins("project4");
282
283         assertEquals(ListenerTestPlugin.class, plugins.getPluginClass("testlistener"));
284         assertEquals(ListenerTestNestedPlugin.class, plugins.getPluginClass("testnested"));
285         assertEquals(ListenerTestSelfConfiguringPlugin.class, plugins.getPluginClass("testselfconfiguring"));
286
287         List JavaDoc listeners = projConfig.getListeners();
288         assertEquals(3, listeners.size());
289
290         Listener listener0 = (Listener) listeners.get(0);
291         assertEquals(ListenerTestPlugin.class, listener0.getClass());
292         ListenerTestPlugin testListener0 = (ListenerTestPlugin) listener0;
293         assertEquals("project4-0", testListener0.getString());
294
295         Listener listener1 = (Listener) listeners.get(1);
296         assertEquals(ListenerTestPlugin.class, listener1.getClass());
297         ListenerTestPlugin testListener1 = (ListenerTestPlugin) listener1;
298         assertEquals("listener1", testListener1.getString());
299         assertEquals("wrapper1", testListener1.getStringWrapper().getString());
300
301         Listener listener2 = (Listener) listeners.get(2);
302         assertEquals(ListenerTestPlugin.class, listener2.getClass());
303         ListenerTestPlugin testListener2 = (ListenerTestPlugin) listener2;
304         assertEquals("listener2", testListener2.getString());
305         // note this is in fact undefined behavior!! Because we added twice the stringwrapper
306
// (first for the child, then for the parent).
307
// this could probably fail depending on a different platform, except if Element.setContent()
308
// specifies the order in which children are kept within the element.
309
final String JavaDoc wrapper = testListener2.getStringWrapper().getString();
310         assertTrue("wrapper2-works!", "wrapper2-works!".equals(wrapper)
311                                       || "wrapper1".equals(wrapper));
312     }
313
314     public void testPluginConfigurationClassOverride() throws Exception JavaDoc {
315         ProjectConfig projConfig = config.getConfig("project5");
316         PluginRegistry plugins = config.getProjectPlugins("project5");
317
318         assertEquals(ListenerTestPlugin.class, plugins.getPluginClass("testlistener"));
319         assertEquals(ListenerTestOtherNestedPlugin.class, plugins.getPluginClass("testnested"));
320
321         List JavaDoc listeners = projConfig.getListeners();
322         assertEquals(1, listeners.size());
323
324         Listener listener0 = (Listener) listeners.get(0);
325         assertEquals(ListenerTestPlugin.class, listener0.getClass());
326         ListenerTestPlugin testListener0 = (ListenerTestPlugin) listener0;
327         assertEquals("default", testListener0.getString());
328         ListenerTestNestedPlugin nested = testListener0.getNested();
329         assertTrue(nested instanceof ListenerTestOtherNestedPlugin);
330         assertEquals("notshadowing", ((ListenerTestOtherNestedPlugin) nested).getString());
331         assertEquals(null, ((ListenerTestOtherNestedPlugin) nested).getOtherString());
332         assertEquals("otherother", ((ListenerTestOtherNestedPlugin) nested).getOtherOtherString());
333     }
334
335     // TODO DateFormat management was moved to Project.init()
336
/*
337     public void testDateFormat() throws Exception {
338         final String originalFormat = DateFormatFactory.getFormat();
339         createProjectXMLHelper("dateformatfromproperty");
340         final String formatFromProperty = DateFormatFactory.getFormat();
341         DateFormatFactory.setFormat(DateFormatFactory.DEFAULT_FORMAT);
342
343         assertEquals(DateFormatFactory.DEFAULT_FORMAT, originalFormat);
344         assertEquals("MM/dd/yyyy HH:mm:ss a", formatFromProperty);
345         assertFalse(originalFormat.equals(formatFromProperty));
346     }
347
348     public void testPreconfigureDateFormat() throws Exception {
349         final String originalFormat = DateFormatFactory.getFormat();
350         createProjectXMLHelper("dateformatpreconfigured");
351         final String formatFromProperty = DateFormatFactory.getFormat();
352         DateFormatFactory.setFormat(DateFormatFactory.DEFAULT_FORMAT);
353
354         assertEquals(DateFormatFactory.DEFAULT_FORMAT, originalFormat);
355         assertEquals("MM/dd/yyyy HH:mm:ss a", formatFromProperty);
356         assertFalse(originalFormat.equals(formatFromProperty));
357     }
358     */

359
360     public void testGetBootstrappers() throws CruiseControlException {
361         ProjectConfig projConfig = config.getConfig("project1");
362
363         List JavaDoc bootstrappers = projConfig.getBootstrappers();
364         assertEquals(0, bootstrappers.size());
365
366         projConfig = config.getConfig("project2");
367         bootstrappers = projConfig.getBootstrappers();
368         assertEquals(1, bootstrappers.size());
369     }
370
371     public void testGetSchedule() throws CruiseControlException {
372         ProjectConfig projConfig;
373         // TODO
374
/*
375         projConfig = config.getConfig("project1");
376         try {
377             projConfig.getSchedule();
378             fail("schedule should be a required element");
379         } catch (CruiseControlException expected) {
380         }
381         */

382
383         projConfig = config.getConfig("project2");
384         Schedule schedule = projConfig.getSchedule();
385         assertEquals(20 * ONE_SECOND, schedule.getInterval());
386     }
387
388     public void testGetModificationSet() throws CruiseControlException {
389         ProjectConfig projConfig;
390         // TODO
391
/*
392         projConfig = config.getConfig("project1");
393         try {
394             projConfig.getModificationSet();
395             fail("modificationset should be a required element");
396         } catch (CruiseControlException expected) {
397         }
398         */

399
400         projConfig = config.getConfig("project2");
401         ModificationSet modSet = projConfig.getModificationSet();
402         assertEquals(10 * ONE_SECOND, modSet.getQuietPeriod());
403     }
404
405     public void testGetLabelIncrementer() throws CruiseControlException {
406         Element pluginElement = new Element("plugin");
407         pluginElement.setAttribute("name", CruiseControlConfig.LABEL_INCREMENTER);
408         pluginElement.setAttribute("classname", DefaultLabelIncrementer.class.getName());
409         PluginRegistry.registerToRoot(pluginElement);
410         ProjectConfig projConfig = config.getConfig("project2");
411         DefaultLabelIncrementer incrementer = (DefaultLabelIncrementer) projConfig.getLabelIncrementer();
412         assertTrue(incrementer.isValidLabel("build#9"));
413
414         projConfig = config.getConfig("project1");
415         incrementer = (DefaultLabelIncrementer) projConfig.getLabelIncrementer();
416         assertFalse(incrementer.isValidLabel("build#9"));
417     }
418
419     public void testGetLog() throws CruiseControlException {
420         ProjectConfig projConfig = config.getConfig("project1");
421         assertEquals("logs" + File.separatorChar + "project1", projConfig.getLog().getLogDir());
422
423         projConfig = config.getConfig("project2");
424         assertEquals(tempDirectory.getAbsolutePath() + "/foo", projConfig.getLog().getLogDir());
425
426         projConfig = config.getConfig("project3");
427         assertEquals("logs" + File.separatorChar + "project3", projConfig.getLog().getLogDir());
428
429         projConfig = config.getConfig("project3bis");
430         assertEquals("logs/project3bis", projConfig.getLog().getLogDir());
431         assertNull(projConfig.getLog().getLogXmlEncoding());
432
433         projConfig = config.getConfig("project2");
434         assertEquals("utf-8", projConfig.getLog().getLogXmlEncoding());
435     }
436
437     public void testPreconfigureLog() throws Exception JavaDoc {
438         ProjectConfig projConfig = config.getConfig("logpreconfigured");
439
440         final Log log = projConfig.getLog();
441         assertEquals("mylogs/logpreconfigured", log.getLogDir());
442         assertEquals("utf128", log.getLogXmlEncoding());
443         assertEquals("logpreconfigured", log.getProjectName());
444
445         BuildLogger[] loggers = log.getLoggers();
446         assertEquals(2, loggers.length);
447     }
448
449     public void testGetListeners() throws CruiseControlException {
450         ProjectConfig projConfig = config.getConfig("project1");
451         List JavaDoc listeners = projConfig.getListeners();
452         assertEquals(0, listeners.size());
453
454         projConfig = config.getConfig("project2");
455         listeners = projConfig.getListeners();
456         assertEquals(1, listeners.size());
457     }
458 }
459
Popular Tags