KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > sourcecontrols > BuildStatusTest


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
38 package net.sourceforge.cruisecontrol.sourcecontrols;
39
40 import java.io.File JavaDoc;
41 import java.util.Calendar JavaDoc;
42 import java.util.Date JavaDoc;
43 import java.util.List JavaDoc;
44 import junit.framework.TestCase;
45 import net.sourceforge.cruisecontrol.CruiseControlException;
46 import net.sourceforge.cruisecontrol.Log;
47 import net.sourceforge.cruisecontrol.util.DateUtil;
48
49 /**
50  * Unit tests for BuildStatus.java.
51  *
52  *@author Garrick Olson
53  */

54 public class BuildStatusTest extends TestCase {
55     private BuildStatus buildStatus;
56
57     protected void setUp() throws Exception JavaDoc {
58         buildStatus = new BuildStatus();
59     }
60
61     protected void tearDown() throws Exception JavaDoc {
62         buildStatus = null;
63     }
64
65     /**
66      * Make sure the validate() method works properly.
67      */

68     public void testValidate() throws Exception JavaDoc {
69         // Verify log directory is mandatory
70
try {
71             buildStatus.validate();
72             fail("Should have thrown exception indicating log directory is mandatory");
73         } catch (CruiseControlException e) {
74             assertEquals("Wrong exception", "'logdir' is required for BuildStatus", e.getMessage());
75         }
76
77         // Verify log directory must exist
78
buildStatus.setLogDir("does not exist");
79
80         try {
81             buildStatus.validate();
82             fail("Should have thrown exception indicating log directory must exist");
83         } catch (CruiseControlException e) {
84             assertTrue("Wrong exception", e.getMessage().startsWith("Log directory does not exist"));
85         }
86         
87         // Verify log directory must be a directory
88
File JavaDoc tempFile = File.createTempFile("temp", "txt");
89         tempFile.deleteOnExit();
90         buildStatus.setLogDir(tempFile.getAbsolutePath());
91
92         try {
93             buildStatus.validate();
94             fail("Should have thrown exception indicating log directory must be a directory");
95         } catch (CruiseControlException e) {
96             assertTrue("Wrong exception", e.getMessage().startsWith("Log directory is not a directory"));
97         }
98
99         // Set it to a directory and everything should be okay
100
buildStatus.setLogDir(tempFile.getParentFile().getAbsolutePath());
101         buildStatus.validate();
102     }
103
104     /**
105      * Verify the getModifications() method works properly.
106      */

107     public void testGetModifications() throws Exception JavaDoc {
108         File JavaDoc tempDir = new File JavaDoc(System.getProperty("java.io.tmpdir"));
109
110         buildStatus.setLogDir(tempDir.getAbsolutePath());
111         buildStatus.validate();
112
113         Calendar JavaDoc calendar = Calendar.getInstance();
114         Date JavaDoc today = calendar.getTime();
115         calendar.add(Calendar.DATE, -1);
116         Date JavaDoc yesterday = calendar.getTime();
117         calendar.add(Calendar.DATE, -1);
118         Date JavaDoc twoDaysAgo = calendar.getTime();
119
120         // Should be no modifications at this point
121
List JavaDoc modifications = buildStatus.getModifications(twoDaysAgo, null);
122         assertEquals("Wrong number of modifications", 0, modifications.size());
123
124         // Verify an unsuccessful build does not show up
125
File JavaDoc yesterdayLog = new File JavaDoc(tempDir, Log.formatLogFileName(yesterday));
126         yesterdayLog.createNewFile();
127         try {
128             modifications = buildStatus.getModifications(twoDaysAgo, null);
129             assertEquals("Wrong number of modifications", 0, modifications.size());
130         } finally {
131             yesterdayLog.delete();
132         }
133
134         // Verify a successful build shows up
135
File JavaDoc yesterdayLog2 = new File JavaDoc(tempDir, Log.formatLogFileName(yesterday, "good.1"));
136         yesterdayLog2.createNewFile();
137         try {
138             modifications = buildStatus.getModifications(twoDaysAgo, null);
139             assertEquals("Wrong number of modifications", 1, modifications.size());
140
141             // Verify properties were set correctly
142
assertEquals("Property was not set correctly", tempDir.getAbsolutePath(),
143                          buildStatus.getProperties().get(BuildStatus.MOST_RECENT_LOGDIR_KEY));
144             assertEquals("Property was not set correctly", yesterdayLog2.getName(),
145                          buildStatus.getProperties().get(BuildStatus.MOST_RECENT_LOGFILE_KEY));
146             assertEquals("Property was not set correctly", DateUtil.getFormattedTime(yesterday),
147                          buildStatus.getProperties().get(BuildStatus.MOST_RECENT_LOGTIME_KEY));
148             assertEquals("Property was not set correctly", "good.1",
149                          buildStatus.getProperties().get(BuildStatus.MOST_RECENT_LOGLABEL_KEY));
150
151             // Verify date range works
152
modifications = buildStatus.getModifications(today, null);
153             assertEquals("Wrong number of modifications", 0, modifications.size());
154
155             // Verify properties are set correctly when there are multiple modifications
156
File JavaDoc todayLog = new File JavaDoc(tempDir, Log.formatLogFileName(today, "good.2"));
157             todayLog.createNewFile();
158             try {
159                 modifications = buildStatus.getModifications(twoDaysAgo, null);
160                 assertEquals("Wrong number of modifications", 2, modifications.size());
161
162                 // Verify properties were set correctly
163
assertEquals("Property was not set correctly", tempDir.getAbsolutePath(),
164                              buildStatus.getProperties().get(BuildStatus.MOST_RECENT_LOGDIR_KEY));
165                 assertEquals("Property was not set correctly", todayLog.getName(),
166                              buildStatus.getProperties().get(BuildStatus.MOST_RECENT_LOGFILE_KEY));
167                 assertEquals("Property was not set correctly", DateUtil.getFormattedTime(today),
168                              buildStatus.getProperties().get(BuildStatus.MOST_RECENT_LOGTIME_KEY));
169                 assertEquals("Property was not set correctly", "good.2",
170                              buildStatus.getProperties().get(BuildStatus.MOST_RECENT_LOGLABEL_KEY));
171             } finally {
172                 todayLog.delete();
173             }
174         } finally {
175             yesterdayLog2.delete();
176         }
177     }
178 }
179
Popular Tags