KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > distributed > BuildAgentServiceImplTest


1 package net.sourceforge.cruisecontrol.distributed;
2
3 import junit.framework.TestCase;
4
5 import java.util.Arrays JavaDoc;
6 import java.util.Date JavaDoc;
7 import java.util.Map JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.io.File JavaDoc;
10 import java.rmi.RemoteException JavaDoc;
11
12 import org.jdom.Element;
13 import org.apache.log4j.Logger;
14 import net.sourceforge.cruisecontrol.util.Util;
15 import net.sourceforge.cruisecontrol.distributed.util.PropertiesHelper;
16 import net.sourceforge.cruisecontrol.CruiseControlException;
17 import net.sourceforge.cruisecontrol.builders.DistributedMasterBuilderTest;
18
19 /**
20  * Created by IntelliJ IDEA.
21  * User: drollo
22  * Date: May 25, 2005
23  * Time: 1:54:38 PM
24  * To change this template use File | Settings | File Templates.
25  */

26 public class BuildAgentServiceImplTest extends TestCase {
27
28     private static final Logger LOG = Logger.getLogger(BuildAgentServiceImplTest.class);
29
30     public static final File JavaDoc TEST_CONFIG_FILE = new File JavaDoc("test/testdist.config.xml");
31     public static final String JavaDoc TEST_AGENT_PROPERTIES_FILE = "testdist.agent.properties";
32     public static final String JavaDoc TEST_USER_DEFINED_PROPERTIES_FILE = "testdist.user-defined.properties";
33
34     private static final File JavaDoc DIR_LOGS = new File JavaDoc(PropertiesHelper.RESULT_TYPE_LOGS);
35     private static final File JavaDoc DIR_OUTPUT = new File JavaDoc(PropertiesHelper.RESULT_TYPE_OUTPUT);
36
37     protected void setUp() throws Exception JavaDoc {
38         DIR_LOGS.delete();
39         DIR_OUTPUT.delete();
40     }
41
42     protected void tearDown() throws Exception JavaDoc {
43         deleteDirConfirm(DIR_LOGS);
44         deleteDirConfirm(DIR_OUTPUT);
45     }
46
47     private static void deleteDirConfirm(final File JavaDoc dirToDelete) {
48         if (dirToDelete.exists()) {
49             assertTrue("Error cleaning up test directory: " + dirToDelete.getAbsolutePath()
50                     + "\nDir Contents:\n" + Arrays.asList(dirToDelete.listFiles()),
51                     dirToDelete.delete());
52         }
53     }
54
55
56     private static class MyAgentStatusListener implements BuildAgent.AgentStatusListener
57     {
58         private int agentStatusChangeCount;
59
60         public void statusChanged(BuildAgentService buildAgentServiceImpl) {
61             agentStatusChangeCount++;
62         }
63
64         int getAgentStatusChangeCount() {
65             return agentStatusChangeCount;
66         }
67
68         void setAgentStatusChangeCount(int agentStatusChangeCount) {
69             this.agentStatusChangeCount = agentStatusChangeCount;
70         }
71     }
72
73     /**
74      * Re-use of builder caused problems with null value in overrideTarget.
75      * This test verifies null values in the Map are allowed.
76      */

77      public void testGetPropertiesMap() throws Exception JavaDoc {
78         final BuildAgentServiceImpl agentImpl = new BuildAgentServiceImpl();
79         agentImpl.setAgentPropertiesFilename(TEST_AGENT_PROPERTIES_FILE);
80
81         MyAgentStatusListener agentListener = new MyAgentStatusListener();
82         agentImpl.addAgentStatusListener(agentListener);
83         
84         String JavaDoc agentAsString = agentImpl.asString();
85         assertTrue("Wrong value: " + agentAsString,
86                 agentAsString.startsWith("Machine Name: "));
87         assertTrue("Wrong value: " + agentAsString,
88                 agentAsString.endsWith("Busy: false;\tSince: null;\tModule: null\n\t"
89                 + "Pending Restart: false;\tPending Restart Since: null\n\t"
90                 + "Pending Kill: false;\tPending Kill Since: null"));
91
92         
93         final Map JavaDoc distributedAgentProps = new HashMap JavaDoc();
94         final String JavaDoc testModuleName = "testModuleName";
95         distributedAgentProps.put(PropertiesHelper.DISTRIBUTED_MODULE, testModuleName);
96
97         distributedAgentProps.put(PropertiesHelper.DISTRIBUTED_OVERRIDE_TARGET, null);
98         try {
99             // fails at old props.putAll() and now at projectPropertiesMap.toString(),
100
// doesn't fire 2nd agent status event
101
agentImpl.doBuild(null, null, distributedAgentProps);
102             fail("should fail w/ NPE");
103         } catch (NullPointerException JavaDoc e) {
104             assertEquals(null, e.getMessage());
105         }
106         assertEquals("Wrong agent status", 1, agentListener.getAgentStatusChangeCount());
107
108         
109         distributedAgentProps.remove(PropertiesHelper.DISTRIBUTED_OVERRIDE_TARGET);
110         agentImpl.setBusy(false);
111         agentListener.setAgentStatusChangeCount(0);
112         try {
113             // gets far enough to fire 2nd agent status change
114
agentImpl.doBuild(null, new HashMap JavaDoc(), distributedAgentProps);
115             fail("should fail w/ NPE");
116         } catch (NullPointerException JavaDoc e) {
117             assertEquals(null, e.getMessage());
118         }
119         assertEquals("Wrong agent status", 2, agentListener.getAgentStatusChangeCount());
120
121         agentAsString = agentImpl.asString();
122         assertTrue("Wrong value: " + agentAsString,
123                 agentAsString.startsWith("Machine Name: "));
124         assertTrue("Wrong value: " + agentAsString,
125                 agentAsString.indexOf("Busy: true;\tSince: ") > -1);
126         assertTrue("Wrong value: " + agentAsString,
127                 agentAsString.endsWith(";\tModule: " + testModuleName
128                 + "\n\tPending Restart: false;\tPending Restart Since: null\n\t"
129                 + "Pending Kill: false;\tPending Kill Since: null"));
130     }
131     
132     public void testAsString() throws Exception JavaDoc {
133         final BuildAgentServiceImpl agentImpl = new BuildAgentServiceImpl();
134         agentImpl.setAgentPropertiesFilename(TEST_AGENT_PROPERTIES_FILE);
135
136         String JavaDoc agentAsString = agentImpl.asString();
137         assertTrue("Wrong value: " + agentAsString,
138                 agentAsString.startsWith("Machine Name: "));
139         assertTrue("Wrong value: " + agentAsString,
140                 agentAsString.endsWith("Busy: false;\tSince: null;\tModule: null\n\t"
141                 + "Pending Restart: false;\tPending Restart Since: null\n\t"
142                 + "Pending Kill: false;\tPending Kill Since: null"));
143
144         final Map JavaDoc projectProps = null;
145         
146         final Map JavaDoc distributedAgentProps = new HashMap JavaDoc();
147         final String JavaDoc testModuleName = "testModuleName";
148         distributedAgentProps.put(PropertiesHelper.DISTRIBUTED_MODULE, testModuleName);
149
150         try {
151             agentImpl.doBuild(null, projectProps, distributedAgentProps); // gets far enough to set Module name...
152
fail("should fail w/ NPE");
153         } catch (NullPointerException JavaDoc e) {
154             assertEquals(null, e.getMessage());
155         }
156
157         agentAsString = agentImpl.asString();
158         assertTrue("Wrong value: " + agentAsString,
159                 agentAsString.startsWith("Machine Name: "));
160         assertTrue("Wrong value: " + agentAsString,
161                 agentAsString.indexOf("Busy: true;\tSince: ") > -1);
162         assertTrue("Wrong value: " + agentAsString,
163                 agentAsString.endsWith(";\tModule: " + testModuleName
164                 + "\n\tPending Restart: false;\tPending Restart Since: null\n\t"
165                 + "Pending Kill: false;\tPending Kill Since: null"));
166
167         agentImpl.kill(true);
168         agentAsString = agentImpl.asString();
169         assertTrue("Wrong value: " + agentAsString,
170                 agentAsString.indexOf("Busy: true;\tSince: ") > -1);
171         assertTrue("Wrong value: " + agentAsString,
172                 agentAsString.indexOf(";\tModule: " + testModuleName
173                 + "\n\tPending Restart: false;\tPending Restart Since: null\n\t"
174                 + "Pending Kill: true;\tPending Kill Since: ") > -1);
175         assertNotNull(agentImpl.getPendingKillSince());
176
177         agentImpl.setBusy(false); // fake build finish
178
agentAsString = agentImpl.asString();
179         assertTrue("Wrong value: " + agentAsString,
180                 agentAsString.indexOf("Busy: false;\tSince: null;\tModule: null\n\t"
181                 + "Pending Restart: false;\tPending Restart Since: null\n\t"
182                 + "Pending Kill: true;\tPending Kill Since: ") > -1);
183         assertNotNull(agentImpl.getPendingKillSince());
184     }
185
186     public void testGetBuildingModule() throws Exception JavaDoc {
187         final BuildAgentServiceImpl agentImpl = new BuildAgentServiceImpl();
188         agentImpl.setAgentPropertiesFilename(TEST_AGENT_PROPERTIES_FILE);
189
190         assertNull(agentImpl.getModule());
191
192         final Map JavaDoc projectProps = null;
193
194         final Map JavaDoc distributedAgentProps = new HashMap JavaDoc();
195         final String JavaDoc testModuleName = "testModuleName";
196         distributedAgentProps.put(PropertiesHelper.DISTRIBUTED_MODULE, testModuleName);
197
198         try {
199             agentImpl.doBuild(null, projectProps, distributedAgentProps); // gets far enough to set Module name...
200
fail("should fail w/ NPE");
201         } catch (NullPointerException JavaDoc e) {
202             assertEquals(null, e.getMessage());
203         }
204
205         assertEquals(testModuleName, agentImpl.getModule());
206
207         agentImpl.setBusy(false); // fake build finish
208
assertNull(agentImpl.getModule());
209     }
210
211     public void testKillNoWait() throws Exception JavaDoc {
212         final BuildAgentServiceImpl agentImpl = new BuildAgentServiceImpl();
213         agentImpl.setAgentPropertiesFilename(TEST_AGENT_PROPERTIES_FILE);
214
215         assertFalse(agentImpl.isBusy());
216         assertFalse(agentImpl.isPendingKill());
217         assertNull(agentImpl.getPendingKillSince());
218         assertFalse(agentImpl.isPendingRestart());
219         assertNull(agentImpl.getPendingRestartSince());
220         // make agent think it's building now
221
agentImpl.claim();
222         assertTrue(agentImpl.isBusy());
223         assertFalse(agentImpl.isPendingKill());
224         assertFalse(agentImpl.isPendingRestart());
225         agentImpl.kill(false);
226         assertTrue(agentImpl.isBusy());
227         assertTrue(agentImpl.isPendingKill());
228         assertNotNull(agentImpl.getPendingKillSince());
229         assertFalse(agentImpl.isPendingRestart());
230         assertNull(agentImpl.getPendingRestartSince());
231
232         // fake finish agent build - not really needed here
233
agentImpl.setBusy(false);
234     }
235
236     public void testRestartNoWait() throws Exception JavaDoc {
237         final BuildAgentServiceImpl agentImpl = new BuildAgentServiceImpl();
238         agentImpl.setAgentPropertiesFilename(TEST_AGENT_PROPERTIES_FILE);
239
240         assertFalse(agentImpl.isBusy());
241         assertFalse(agentImpl.isPendingKill());
242         assertNull(agentImpl.getPendingKillSince());
243         assertFalse(agentImpl.isPendingRestart());
244         assertNull(agentImpl.getPendingRestartSince());
245         // make agent think it's building now
246
agentImpl.claim();
247         assertTrue(agentImpl.isBusy());
248         assertFalse(agentImpl.isPendingKill());
249         assertFalse(agentImpl.isPendingRestart());
250         // fake finish agent build
251
try {
252             agentImpl.restart(false);
253             fail("Restart should fail outside of webstart");
254         } catch (RuntimeException JavaDoc e) {
255             assertEquals("Couldn't find webstart Basic Service. Is Agent running outside of webstart?",
256                     e.getMessage());
257         }
258         assertTrue(agentImpl.isBusy());
259         assertFalse(agentImpl.isPendingKill());
260         assertNull(agentImpl.getPendingKillSince());
261         assertTrue(agentImpl.isPendingRestart());
262         assertNotNull(agentImpl.getPendingRestartSince());
263     }
264
265     public void testKillWithWait() throws Exception JavaDoc {
266         final BuildAgentServiceImpl agentImpl = new BuildAgentServiceImpl();
267         agentImpl.setAgentPropertiesFilename(TEST_AGENT_PROPERTIES_FILE);
268
269         assertFalse(agentImpl.isBusy());
270         assertFalse(agentImpl.isPendingKill());
271         assertNull(agentImpl.getPendingKillSince());
272         assertFalse(agentImpl.isPendingRestart());
273         assertNull(agentImpl.getPendingRestartSince());
274         // make agent think it's building now
275
agentImpl.claim();
276         assertTrue(agentImpl.isBusy());
277         assertFalse(agentImpl.isPendingKill());
278         assertFalse(agentImpl.isPendingRestart());
279         agentImpl.kill(true);
280         assertTrue(agentImpl.isBusy());
281         assertTrue(agentImpl.isPendingKill());
282         assertNotNull(agentImpl.getPendingKillSince());
283         assertFalse(agentImpl.isPendingRestart());
284
285         // fake finish agent build
286
agentImpl.setBusy(false);
287     }
288
289     public void testRestartWithWait() throws Exception JavaDoc {
290         final BuildAgentServiceImpl agentImpl = new BuildAgentServiceImpl();
291         agentImpl.setAgentPropertiesFilename(TEST_AGENT_PROPERTIES_FILE);
292
293         assertFalse(agentImpl.isBusy());
294         assertFalse(agentImpl.isPendingKill());
295         assertNull(agentImpl.getPendingKillSince());
296         assertFalse(agentImpl.isPendingRestart());
297         assertNull(agentImpl.getPendingRestartSince());
298         // make agent think it's building now
299
agentImpl.claim();
300         assertTrue(agentImpl.isBusy());
301         assertFalse(agentImpl.isPendingKill());
302         assertNull(agentImpl.getPendingRestartSince());
303         assertFalse(agentImpl.isPendingRestart());
304         assertNull(agentImpl.getPendingRestartSince());
305         agentImpl.restart(true);
306         assertTrue(agentImpl.isBusy());
307         assertFalse(agentImpl.isPendingKill());
308         assertTrue(agentImpl.isPendingRestart());
309         assertNotNull(agentImpl.getPendingRestartSince());
310
311         // fake finish agent build
312
try {
313             agentImpl.setBusy(false);
314             fail("Restart should fail outside of webstart");
315         } catch (RuntimeException JavaDoc e) {
316             assertEquals("Couldn't find webstart Basic Service. Is Agent running outside of webstart?",
317                     e.getMessage());
318         }
319     }
320
321     public void testRetrieveResultsWithAgentLogDir() throws Exception JavaDoc {
322         final File JavaDoc testLogDir = new File JavaDoc(DIR_LOGS, "myTestLogDir");
323         testLogDir.deleteOnExit();
324         testLogDir.mkdirs();
325         final File JavaDoc testLog = new File JavaDoc(testLogDir, "myTestLog");
326         testLog.createNewFile();
327         testLog.deleteOnExit();
328         
329         final Map JavaDoc distributedAgentProps = new HashMap JavaDoc();
330         distributedAgentProps.put(PropertiesHelper.DISTRIBUTED_AGENT_LOGDIR,
331                 testLogDir.getAbsolutePath());
332         
333         final BuildAgentServiceImpl agentImpl = createTestAgent(false, distributedAgentProps);
334
335         // clear out default log dir
336
final File JavaDoc tmSuccessDir = new File JavaDoc(DIR_LOGS, "testmodule-success");
337         new File JavaDoc(tmSuccessDir, "TEST-bogustestclassSuccess.xml").delete();
338         deleteDirConfirm(tmSuccessDir);
339         
340         try {
341             // make sure agent looks in myTestLog dir for files
342
assertTrue(agentImpl.resultsExist(PropertiesHelper.RESULT_TYPE_LOGS));
343             
344             assertTrue(agentImpl.resultsExist(PropertiesHelper.RESULT_TYPE_OUTPUT));
345             assertTrue("Agent should be busy until build results are retrived and cleared.",
346                     agentImpl.isBusy());
347         } finally {
348             // cleanup left over files
349
agentImpl.clearOutputFiles();
350             testLog.delete();
351             testLogDir.delete();
352         }
353         assertFalse(agentImpl.isBusy());
354     }
355
356     public void testRetrieveResultsAsZipBuildSuccess() throws Exception JavaDoc {
357         final BuildAgentServiceImpl agentImpl = createTestAgent(false, new HashMap JavaDoc());
358         try {
359             assertTrue(agentImpl.resultsExist(PropertiesHelper.RESULT_TYPE_LOGS));
360             assertTrue(agentImpl.resultsExist(PropertiesHelper.RESULT_TYPE_OUTPUT));
361             assertTrue("Agent should be busy until build results are retrived and cleared.",
362                     agentImpl.isBusy());
363         } finally {
364             // cleanup left over files
365
agentImpl.clearOutputFiles();
366         }
367         assertFalse(agentImpl.isBusy());
368     }
369
370     public void testRetrieveResultsAsZipBuildFail() throws Exception JavaDoc {
371         final BuildAgentServiceImpl agentImpl = createTestAgent(true, new HashMap JavaDoc());
372         try {
373             assertTrue(agentImpl.resultsExist(PropertiesHelper.RESULT_TYPE_LOGS));
374             assertFalse(agentImpl.resultsExist(PropertiesHelper.RESULT_TYPE_OUTPUT));
375             assertTrue("Agent should be busy until build results are retrived and cleared.",
376                     agentImpl.isBusy());
377         } finally {
378             // cleanup left over files
379
agentImpl.clearOutputFiles();
380         }
381         assertFalse(agentImpl.isBusy());
382     }
383
384     private static BuildAgentServiceImpl createTestAgent(boolean isBuildFailure,
385                                                          final Map JavaDoc distributedAgentProps)
386             throws CruiseControlException, RemoteException JavaDoc {
387
388         final BuildAgentServiceImpl agentImpl = new BuildAgentServiceImpl();
389         agentImpl.setAgentPropertiesFilename(TEST_AGENT_PROPERTIES_FILE);
390
391         // @todo Fix issues where AntBuilder fails if java is not on os path
392
callTestDoBuild(isBuildFailure, agentImpl, distributedAgentProps);
393
394         return agentImpl;
395     }
396
397     
398     /** Call doBuild() on the given agent using test settings */
399     public static Element callTestDoBuild(boolean isBuildFailure,
400                                        final BuildAgentService agent)
401             throws CruiseControlException, RemoteException JavaDoc {
402         
403         return callTestDoBuild(isBuildFailure, agent, new HashMap JavaDoc());
404     }
405     
406     /** Call doBuild() on the given agent using test settings */
407     public static Element callTestDoBuild(boolean isBuildFailure,
408                                        final BuildAgentService agent,
409                                        final Map JavaDoc distributedAgentProps)
410             throws CruiseControlException, RemoteException JavaDoc {
411         
412         final Element rootElement = Util.loadConfigFile(TEST_CONFIG_FILE);
413
414         final Element project = (Element) rootElement.getChildren("project").get(0);
415         assertEquals("testproject", project.getAttributeValue("name"));
416
417         final Element schedule = (Element) project.getChildren("schedule").get(0);
418         final int distributedElementIndex;
419         if (isBuildFailure) {
420             distributedElementIndex = 0;
421         } else {
422             distributedElementIndex = 1;
423         }
424         final Element distributed = ((Element) schedule.getChildren("distributed").get(distributedElementIndex));
425         DistributedMasterBuilderTest.addMissingPluginDefaults(distributed);
426
427         final String JavaDoc expectedModuleName;
428         if (isBuildFailure) {
429             expectedModuleName = "testmodule-fail";
430         } else {
431             expectedModuleName = "testmodule-success";
432         }
433         final String JavaDoc moduleName = distributed.getAttributeValue("module");
434         assertEquals(expectedModuleName, moduleName);
435         
436         distributedAgentProps.put(PropertiesHelper.DISTRIBUTED_MODULE, moduleName);
437         
438         // handle re-use case of DMB when overrideTarget is null
439
distributedAgentProps.put(PropertiesHelper.DISTRIBUTED_OVERRIDE_TARGET, null);
440
441         final Element antBuilderElement = (Element) distributed.getChildren().get(0);
442         DistributedMasterBuilderTest.addMissingPluginDefaults(antBuilderElement);
443
444         // @todo Find way to make this work when JAVA_HOME/bin is NOT on the path
445
// @todo Fix to use expanded env.* settings from config file
446
if (getANT_HOME() != null) {
447             antBuilderElement.setAttribute("anthome", getANT_HOME());
448         } else {
449             LOG.warn("Unit Test couldn't find ANT_HOME env var. Might work if java/bin is in the path. Here goes...");
450         }
451
452         final Map JavaDoc projectProps = new HashMap JavaDoc();
453         
454         final Element buildResult = agent.doBuild(antBuilderElement, projectProps, distributedAgentProps);
455         return buildResult;
456     }
457
458     public static String JavaDoc getANT_HOME() {
459         return DistributedMasterBuilderTest.OS_ENV.getVariable("ANT_HOME");
460     }
461
462     public void testClaim() {
463         final BuildAgentServiceImpl agentImpl = new BuildAgentServiceImpl();
464         agentImpl.setAgentPropertiesFilename(TEST_AGENT_PROPERTIES_FILE);
465
466         assertFalse(agentImpl.isBusy());
467         assertNull(agentImpl.getDateClaimed());
468         agentImpl.claim();
469         assertTrue(agentImpl.isBusy());
470         assertNotNull(agentImpl.getDateClaimed());
471         final Date JavaDoc firstClaimDate = agentImpl.getDateClaimed();
472
473         try {
474             agentImpl.claim();
475             fail("Should throw exception if attempt is made to claim a busy agent");
476         } catch (IllegalStateException JavaDoc e) {
477             assertTrue("Unexpected error message w/ invalid call to claim(): " + e.getMessage(),
478                     e.getMessage().startsWith("Cannot claim agent on "));
479             assertTrue("Unexpected error message w/ invalid call to claim(): " + e.getMessage(),
480                     e.getMessage().indexOf("that is busy building module: null") > 0);
481         }
482         assertEquals(firstClaimDate, agentImpl.getDateClaimed());
483
484         // @todo should agent expose a release() method to clear busy flag?
485
try {
486             agentImpl.doBuild(null, null, null);
487             fail("Should have failed to build");
488         } catch (NullPointerException JavaDoc e) {
489             assertEquals("Unexpected build error: " + e.getMessage(), null, e.getMessage());
490         } catch (RemoteException JavaDoc e) {
491             assertTrue("Unexpected build error: " + e.getMessage(),
492                     e.getMessage().startsWith("Failed to complete build on agent; nested exception is: \n"
493                     + "\tnet.sourceforge.cruisecontrol.CruiseControlException: ant logfile "));
494         }
495         assertEquals(firstClaimDate, agentImpl.getDateClaimed());
496
497         // we now avoid NPE error during cleanup since logDir and outputDir can be null
498
agentImpl.clearOutputFiles();
499         assertFalse("Expected agent busy flag to be false after cleanup call.", agentImpl.isBusy());
500         assertNull(agentImpl.getDateClaimed());
501     }
502 }
503
Popular Tags