KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.FileWriter JavaDoc;
41 import java.io.IOException JavaDoc;
42 import java.util.ArrayList JavaDoc;
43 import java.util.List JavaDoc;
44
45 import junit.framework.TestCase;
46 import net.sourceforge.cruisecontrol.config.XMLConfigManager;
47 import net.sourceforge.cruisecontrol.listeners.ListenerTestPlugin;
48
49 /**
50  *
51  * @author <a HREF="mailto:robertdw@users.sourceforge.net">Robert Watkins</a>
52  */

53 public class CruiseControlControllerTest extends TestCase {
54
55     private File JavaDoc dir = new File JavaDoc("target");
56     private File JavaDoc configFile = new File JavaDoc(dir, "_tempConfigFile");
57     private File JavaDoc configFile2 = new File JavaDoc(dir, "_tempConfigFile2");
58     private CruiseControlController ccController;
59
60     protected void setUp() throws CruiseControlException {
61         dir.mkdirs();
62         ccController = new CruiseControlController();
63         ensureFileDoesntExist(configFile);
64         ensureFileDoesntExist(configFile2);
65     }
66
67     public void tearDown() {
68         ccController = null;
69         ensureFileDoesntExist(configFile);
70         ensureFileDoesntExist(configFile2);
71     }
72
73     private void ensureFileDoesntExist(File JavaDoc file) {
74         final long start = System.currentTimeMillis();
75         final long oneMinute = 60 * 1000;
76         while (file.exists()) {
77           file.delete();
78           if (System.currentTimeMillis() > (start + oneMinute)) {
79               fail("unable to delete file " + file.getPath());
80           }
81         }
82         assertFalse(file.exists());
83     }
84
85     public void testSetFileFailsWithNull() {
86         try {
87             ccController.setConfigFile(null);
88             fail("Allowed to not set a config file");
89         } catch (CruiseControlException expected) {
90             assertEquals("No config file", expected.getMessage());
91         }
92     }
93
94     public void testSetFileFailsIfFileDoesntExist() {
95         try {
96             ccController.setConfigFile(configFile);
97             fail("Config file must exist");
98         } catch (CruiseControlException expected) {
99             assertEquals("Config file not found: " + configFile.getAbsolutePath(), expected.getMessage());
100         }
101     }
102
103     public void testLoadEmptyProjects() throws IOException JavaDoc, CruiseControlException {
104         FileWriter JavaDoc configOut = new FileWriter JavaDoc(configFile);
105         writeHeader(configOut);
106         writeFooterAndClose(configOut);
107
108         ccController.setConfigFile(configFile);
109         assertEquals(configFile, ccController.getConfigFile());
110         assertTrue(ccController.getProjects().isEmpty());
111     }
112
113     public void testLoadThreadCount() throws IOException JavaDoc, CruiseControlException {
114         FileWriter JavaDoc configOut = new FileWriter JavaDoc(configFile);
115         writeHeader(configOut);
116         configOut.write("<system><configuration>"
117                 + "<threads count=\"2\"/>"
118                 + "</configuration></system>");
119         writeFooterAndClose(configOut);
120
121         ccController.setConfigFile(configFile);
122         assertEquals(configFile, ccController.getConfigFile());
123         assertTrue(ccController.getProjects().isEmpty());
124     }
125
126     public void testLoadSomeProjects() throws IOException JavaDoc, CruiseControlException {
127         ccController = new CruiseControlController();
128
129         FileWriter JavaDoc configOut = new FileWriter JavaDoc(configFile);
130         writeHeader(configOut);
131         writeProjectDetails(configOut, "testProject1", 30);
132         writeProjectDetails(configOut, "testProject2", 30);
133         writeFooterAndClose(configOut);
134
135         ccController.setConfigFile(configFile);
136         assertEquals(configFile, ccController.getConfigFile());
137         assertEquals(2, ccController.getProjects().size());
138     }
139
140     public void testSetConfigFileShouldFailWithDuplicateProjects() throws IOException JavaDoc, CruiseControlException {
141         ccController = new CruiseControlController();
142
143         FileWriter JavaDoc configOut = new FileWriter JavaDoc(configFile);
144         writeHeader(configOut);
145         writeProjectDetails(configOut, "testProject1", 30);
146         writeProjectDetails(configOut, "testProject1", 30);
147         writeFooterAndClose(configOut);
148
149         try {
150             ccController.setConfigFile(configFile);
151             fail("duplicate project names should fail");
152         } catch (CruiseControlException expected) {
153             assertEquals("Duplicate entries in config file for project name testProject1", expected.getMessage());
154         }
155     }
156
157     // FIXME this is a test for the XMLConfigManager
158
public void testLoadSomeProjectsWithParametrizedNames() throws IOException JavaDoc, CruiseControlException {
159         ccController = new CruiseControlController();
160
161         FileWriter JavaDoc configOut = new FileWriter JavaDoc(configFile);
162         writeHeader(configOut);
163         // a property that defines the project name.
164
configOut.write(" <property name='name' value='testProject'/>\n");
165         configOut.write(" <property name='encoding' value='utf8'/>\n");
166         // this to test that plugin preconfiguration still works
167
configOut.write(" <plugin name='testlistener' "
168             + "classname='net.sourceforge.cruisecontrol.listeners.ListenerTestPlugin' "
169             + "string='listener for ${project.name}'/>\n");
170
171         // this to test that project name can be parametrized
172
configOut.write(" <project name='${name}1'>\n");
173         configOut.write(" <modificationset><alwaysbuild/></modificationset>\n");
174         configOut.write(" <schedule><ant/></schedule>\n");
175         configOut.write(" <listeners><testlistener/></listeners>\n");
176         configOut.write(" <log dir='logs/${project.name}' encoding='${encoding}'/>\n");
177         configOut.write(" </project>\n");
178         writeFooterAndClose(configOut);
179
180         ccController.setConfigFile(configFile);
181         assertEquals(configFile, ccController.getConfigFile());
182         assertEquals(1, ccController.getProjects().size());
183         final Project project = ((Project) ccController.getProjects().get(0));
184         assertEquals("project name can be resolved", "testProject1", project.getName());
185
186         assertEquals("project name can be resolved", "testProject1", project.getLog().getProjectName());
187
188         List JavaDoc listeners = project.getListeners();
189         assertEquals(1, listeners.size());
190         Listener listener = (Listener) listeners.get(0);
191         assertEquals(ListenerTestPlugin.class, listener.getClass());
192         assertEquals("listener for testProject1", ((ListenerTestPlugin) listener).getString());
193
194         assertEquals("logs/testProject1", project.getLogDir());
195         assertEquals("utf8", project.getLog().getLogXmlEncoding());
196     }
197
198     public void testConfigReloading() throws IOException JavaDoc, CruiseControlException {
199         MyListener listener = new MyListener();
200
201         ccController = new CruiseControlController();
202
203         ccController.addListener(listener);
204         FileWriter JavaDoc configOut = new FileWriter JavaDoc(configFile);
205         writeHeader(configOut);
206         writeProjectDetails(configOut, "testProject1", 30);
207         writeProjectDetails(configOut, "testProject2", 30);
208         writeFooterAndClose(configOut);
209
210         ccController.setConfigFile(configFile);
211
212         assertEquals(configFile, ccController.getConfigFile());
213         assertEquals(2, ccController.getProjects().size());
214         assertEquals(2, listener.added.size());
215         assertEquals(0, listener.removed.size());
216
217         listener.clear();
218
219         // no change - no reload
220
assertFalse(ccController.parseConfigFileIfNecessary());
221
222         // nothing happened
223
assertEquals(0, listener.added.size());
224         assertEquals(0, listener.removed.size());
225
226         // add a project:
227

228         listener.clear();
229
230         sleep(1200);
231         configOut = new FileWriter JavaDoc(configFile);
232         writeHeader(configOut);
233         writeProjectDetails(configOut, "testProject1", 30);
234         writeProjectDetails(configOut, "testProject2", 30);
235         writeProjectDetails(configOut, "testProject3", 30);
236         writeFooterAndClose(configOut);
237
238         assertTrue(ccController.parseConfigFileIfNecessary());
239
240         assertEquals(3, ccController.getProjects().size());
241         assertEquals(1, listener.added.size());
242         assertEquals(0, listener.removed.size());
243
244         // remove 2 projects
245

246         listener.clear();
247
248         sleep(1200);
249         configOut = new FileWriter JavaDoc(configFile);
250         writeHeader(configOut);
251         writeProjectDetails(configOut, "testProject3", 30);
252         writeFooterAndClose(configOut);
253
254 // ccController.reloadConfigFile();
255
assertTrue(ccController.parseConfigFileIfNecessary());
256
257         assertEquals(1, ccController.getProjects().size());
258         assertEquals(0, listener.added.size());
259         assertEquals(2, listener.removed.size());
260     }
261
262     public void testConfigReloadingWithXmlInclude() throws IOException JavaDoc, CruiseControlException {
263         MyListener listener = new MyListener();
264
265         ccController = new CruiseControlController();
266
267         ccController.addListener(listener);
268
269         FileWriter JavaDoc configOut2 = new FileWriter JavaDoc(configFile2);
270         writeProjectDetails(configOut2, "testProject1", 30);
271         writeProjectDetails(configOut2, "testProject2", 30);
272         configOut2.close();
273
274         FileWriter JavaDoc wrapperConfigOut = new FileWriter JavaDoc(configFile);
275         wrapperConfigOut.write("<?xml version=\"1.0\" ?>\n");
276         wrapperConfigOut.write("<!DOCTYPE cruisecontrol [ \n");
277         wrapperConfigOut.write("<!ENTITY projects SYSTEM \"" + configFile2.getName() + "\"> \n");
278         wrapperConfigOut.write("]> \n");
279         wrapperConfigOut.write("<cruisecontrol>\n");
280         wrapperConfigOut.write("&projects;");
281         writeFooterAndClose(wrapperConfigOut);
282
283         ccController.setConfigFile(configFile);
284
285         assertEquals(configFile, ccController.getConfigFile());
286         assertEquals(2, ccController.getProjects().size());
287         assertEquals(2, listener.added.size());
288         assertEquals(0, listener.removed.size());
289
290         listener.clear();
291
292         // no change - no reload
293
assertFalse(ccController.parseConfigFileIfNecessary());
294
295         // nothing happened
296
assertEquals(0, listener.added.size());
297         assertEquals(0, listener.removed.size());
298
299         // add a project:
300

301         listener.clear();
302
303         sleep(1200);
304         configOut2 = new FileWriter JavaDoc(configFile2);
305         writeProjectDetails(configOut2, "testProject1", 30);
306         writeProjectDetails(configOut2, "testProject2", 30);
307         writeProjectDetails(configOut2, "testProject3", 30);
308         configOut2.close();
309
310         assertTrue(ccController.parseConfigFileIfNecessary());
311
312         assertEquals(3, ccController.getProjects().size());
313         assertEquals(1, listener.added.size());
314         assertEquals(0, listener.removed.size());
315
316         // remove 2 projects
317

318         listener.clear();
319
320         sleep(1200);
321         configOut2 = new FileWriter JavaDoc(configFile2);
322         writeProjectDetails(configOut2, "testProject3", 30);
323         configOut2.close();
324
325 // ccController.reloadConfigFile();
326
assertTrue(ccController.parseConfigFileIfNecessary());
327
328         assertEquals(1, ccController.getProjects().size());
329         assertEquals(0, listener.added.size());
330         assertEquals(2, listener.removed.size());
331     }
332
333     public void testShouldReloadConfigurationWhenPluginAttributesChange() throws Exception JavaDoc {
334         ccController = new CruiseControlController();
335
336         FileWriter JavaDoc configOut = new FileWriter JavaDoc(configFile);
337         writeHeader(configOut);
338         writeProjectDetails(configOut, "testProject1", 30);
339         writeFooterAndClose(configOut);
340
341         ccController.setConfigFile(configFile);
342
343         // no change - no reload
344
assertFalse(ccController.parseConfigFileIfNecessary());
345
346         sleep(1200);
347         configOut = new FileWriter JavaDoc(configFile);
348         writeHeader(configOut);
349         writeProjectDetails(configOut, "testProject1", 60);
350         writeFooterAndClose(configOut);
351
352         assertTrue(ccController.parseConfigFileIfNecessary());
353     }
354
355     public void testReadProject() {
356         String JavaDoc tempDir = System.getProperty("java.io.tmpdir");
357         Project project = ccController.readProject(tempDir);
358         assertNotNull(project);
359         assertTrue(project.getBuildForced());
360     }
361
362     public void testRegisterPlugins() throws IOException JavaDoc, CruiseControlException {
363         FileWriter JavaDoc configOut = new FileWriter JavaDoc(configFile);
364         writeHeader(configOut);
365         configOut.write(" <plugin name='testname' "
366                         + "classname='net.sourceforge.cruisecontrol.CruiseControllerTest'/>\n");
367         configOut.write(" <plugin name='labelincrementer' classname='my.global.Incrementer'/>\n");
368         writeFooterAndClose(configOut);
369
370         ccController.setConfigFile(configFile);
371         XMLConfigManager configManager = (XMLConfigManager) ccController.getConfigManager();
372         CruiseControlConfig config = configManager.getCruiseControlConfig();
373
374         PluginRegistry newRegistry = config.getRootPlugins();
375         assertTrue(newRegistry.isPluginRegistered("testname"));
376         assertFalse(newRegistry.isPluginRegistered("unknown_plugin"));
377         assertEquals(newRegistry.getPluginClassname("labelincrementer"), "my.global.Incrementer");
378     }
379
380     public void testSetConfigFileShouldValidateAllElements() throws IOException JavaDoc {
381         FileWriter JavaDoc configOut = new FileWriter JavaDoc(configFile);
382         writeHeader(configOut);
383         configOut.write(" <project name='buildlogger'>\n");
384         configOut.write(" <modificationset><alwaysbuild/></modificationset>\n");
385         configOut.write(" <schedule><ant/></schedule>\n");
386         configOut.write(" <log>\n");
387         configOut.write(" <merge/>\n");
388          configOut.write(" </log>\n");
389         configOut.write(" </project>\n");
390         writeFooterAndClose(configOut);
391
392         try {
393             ccController.setConfigFile(configFile);
394             fail("BuildLogger.validate() was not called");
395         } catch (CruiseControlException ccex) {
396             assertEquals("one of file or dir are required attributes",
397                     ccex.getMessage());
398         }
399     }
400
401     private void writeHeader(FileWriter JavaDoc configOut) throws IOException JavaDoc {
402         configOut.write("<?xml version=\"1.0\" ?>\n");
403         configOut.write("<cruisecontrol>\n");
404     }
405
406     private void writeFooterAndClose(FileWriter JavaDoc configOut) throws IOException JavaDoc {
407         configOut.write("</cruisecontrol>\n");
408         configOut.close();
409     }
410
411     private void sleep(long l) {
412         try {
413             Thread.sleep(l);
414         } catch (InterruptedException JavaDoc dontCare) {
415             System.out.println("dontCare happened");
416         }
417     }
418
419     private void writeProjectDetails(FileWriter JavaDoc configOut, final String JavaDoc projectName, int interval) throws IOException JavaDoc {
420         configOut.write("<project name='" + projectName + "'>\n");
421         configOut.write(" <modificationset><alwaysbuild/></modificationset>\n");
422         configOut.write(" <schedule interval=\"" + interval + "\"><ant/></schedule>\n");
423         configOut.write("</project>\n");
424     }
425
426     class MyListener implements CruiseControlController.Listener {
427         private List JavaDoc added = new ArrayList JavaDoc();
428         private List JavaDoc removed = new ArrayList JavaDoc();
429         public void clear() {
430             added.clear();
431             removed.clear();
432         }
433         public void projectAdded(Project project) {
434             added.add(project);
435         }
436         public void projectRemoved(Project project) {
437             removed.add(project);
438         }
439     }
440 }
441
Popular Tags