KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > listeners > CurrentBuildStatusListenerTest


1 package net.sourceforge.cruisecontrol.listeners;
2
3 import junit.framework.TestCase;
4 import net.sourceforge.cruisecontrol.CruiseControlException;
5 import net.sourceforge.cruisecontrol.ProjectState;
6 import net.sourceforge.cruisecontrol.util.Util;
7
8 import java.io.File JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.text.SimpleDateFormat JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.Date JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15
16 /**
17  * .
18  * User: jfredrick
19  * Date: Sep 6, 2004
20  * Time: 10:58:41 PM
21  */

22 public class CurrentBuildStatusListenerTest extends TestCase {
23     private static final String JavaDoc TEST_DIR = "tmp";
24     private final List JavaDoc filesToClear = new ArrayList JavaDoc();
25     private CurrentBuildStatusListener listener;
26
27     protected void setUp() throws Exception JavaDoc {
28         listener = new CurrentBuildStatusListener();
29     }
30
31     protected void tearDown() {
32         listener = null;
33         for (Iterator JavaDoc iterator = filesToClear.iterator(); iterator.hasNext();) {
34             File JavaDoc file = (File JavaDoc) iterator.next();
35             if (file.exists()) {
36                 file.delete();
37             }
38         }
39         filesToClear.clear();
40     }
41
42     public void testValidate() throws CruiseControlException {
43         try {
44             listener.validate();
45             fail("'file' should be a required attribute");
46         } catch (CruiseControlException cce) {
47         }
48
49         listener.setFile("somefile");
50         listener.validate();
51
52         listener.setFile(System.getProperty("java.io.tmpdir") + File.separator + "filename");
53         listener.validate();
54     }
55
56     public void testWritingStatus() throws CruiseControlException, IOException JavaDoc {
57         final String JavaDoc fileName = TEST_DIR + File.separator + "_testCurrentBuildStatus.txt";
58         listener.setFile(fileName);
59         filesToClear.add(new File JavaDoc(fileName));
60
61         checkResultForState(fileName, ProjectState.WAITING);
62         checkResultForState(fileName, ProjectState.IDLE);
63         checkResultForState(fileName, ProjectState.QUEUED);
64         checkResultForState(fileName, ProjectState.BOOTSTRAPPING);
65         checkResultForState(fileName, ProjectState.MODIFICATIONSET);
66         checkResultForState(fileName, ProjectState.BUILDING);
67         checkResultForState(fileName, ProjectState.MERGING_LOGS);
68         checkResultForState(fileName, ProjectState.PUBLISHING);
69         checkResultForState(fileName, ProjectState.PAUSED);
70         checkResultForState(fileName, ProjectState.STOPPED);
71     }
72
73     private void checkResultForState(final String JavaDoc fileName, ProjectState state)
74             throws CruiseControlException, IOException JavaDoc {
75         // This should be equivalent to the date used in listener at seconds precision
76
Date JavaDoc date = new Date JavaDoc();
77         listener.handleEvent(new ProjectStateChangedEvent("projName", state));
78         SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc("MM/dd/yyyy HH:mm:ss");
79         final String JavaDoc dateString = formatter.format(date);
80         final String JavaDoc description = state.getDescription();
81         String JavaDoc expected = description + " since\n" + dateString;
82         assertEquals(expected, Util.readFileToString(fileName));
83     }
84 }
85
Popular Tags