001: /********************************************************************************
002: * CruiseControl, a Continuous Integration Toolkit
003: * Copyright (c) 2001, ThoughtWorks, Inc.
004: * 200 E. Randolph, 25th Floor
005: * Chicago, IL 60601 USA
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * + Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * + Redistributions in binary form must reproduce the above
016: * copyright notice, this list of conditions and the following
017: * disclaimer in the documentation and/or other materials provided
018: * with the distribution.
019: *
020: * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
021: * names of its contributors may be used to endorse or promote
022: * products derived from this software without specific prior
023: * written permission.
024: *
025: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
026: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
027: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
028: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
029: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
030: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
031: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
032: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
033: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
034: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
035: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
036: ********************************************************************************/package net.sourceforge.cruisecontrol.sourcecontrols;
037:
038: import java.io.File;
039: import java.util.Calendar;
040: import java.util.Date;
041: import java.util.List;
042: import java.util.Map;
043:
044: import junit.framework.TestCase;
045: import net.sourceforge.cruisecontrol.CruiseControlException;
046: import net.sourceforge.cruisecontrol.Log;
047: import net.sourceforge.cruisecontrol.testutil.TestUtil.FilesToDelete;
048: import net.sourceforge.cruisecontrol.util.DateUtil;
049:
050: /**
051: * Unit tests for BuildStatus.java.
052: *
053: *@author Garrick Olson
054: */
055: public class BuildStatusTest extends TestCase {
056: private BuildStatus buildStatus;
057: private final FilesToDelete filesToDelete = new FilesToDelete();
058:
059: protected void setUp() throws Exception {
060: buildStatus = new BuildStatus();
061: }
062:
063: protected void tearDown() throws Exception {
064: buildStatus = null;
065: filesToDelete.delete();
066: }
067:
068: /**
069: * Make sure the validate() method works properly.
070: */
071: public void testValidate() throws Exception {
072: // Verify log directory is mandatory
073: try {
074: buildStatus.validate();
075: fail("Should have thrown exception indicating log directory is mandatory");
076: } catch (CruiseControlException e) {
077: assertEquals("Wrong exception",
078: "'logdir' is required for BuildStatus", e
079: .getMessage());
080: }
081:
082: // Verify log directory must exist
083: buildStatus.setLogDir("does not exist");
084:
085: try {
086: buildStatus.validate();
087: fail("Should have thrown exception indicating log directory must exist");
088: } catch (CruiseControlException e) {
089: assertTrue("Wrong exception", e.getMessage().startsWith(
090: "Log directory does not exist"));
091: }
092:
093: // Verify log directory must be a directory
094: File tempFile = File.createTempFile("temp", "txt");
095: tempFile.deleteOnExit();
096: filesToDelete.add(tempFile);
097: buildStatus.setLogDir(tempFile.getAbsolutePath());
098:
099: try {
100: buildStatus.validate();
101: fail("Should have thrown exception indicating log directory must be a directory");
102: } catch (CruiseControlException e) {
103: assertTrue("Wrong exception", e.getMessage().startsWith(
104: "Log directory is not a directory"));
105: }
106:
107: // Set it to a directory and everything should be okay
108: buildStatus.setLogDir(tempFile.getParentFile()
109: .getAbsolutePath());
110: buildStatus.validate();
111: }
112:
113: /**
114: * Verify the getModifications() method works properly.
115: */
116: public void testGetModifications() throws Exception {
117: File tempDir = new File(System.getProperty("java.io.tmpdir"));
118:
119: buildStatus.setLogDir(tempDir.getAbsolutePath());
120: buildStatus.validate();
121:
122: Calendar calendar = Calendar.getInstance();
123: Date today = calendar.getTime();
124: calendar.add(Calendar.DATE, -1);
125: Date yesterday = calendar.getTime();
126: calendar.add(Calendar.DATE, -1);
127: Date twoDaysAgo = calendar.getTime();
128:
129: // Should be no modifications at this point
130: List modifications = buildStatus.getModifications(twoDaysAgo,
131: null);
132: assertEquals("Wrong number of modifications", 0, modifications
133: .size());
134:
135: // Verify an unsuccessful build does not show up
136: File yesterdayLog = new File(tempDir, Log
137: .formatLogFileName(yesterday));
138: yesterdayLog.createNewFile();
139: filesToDelete.add(yesterdayLog);
140: modifications = buildStatus.getModifications(twoDaysAgo, null);
141: assertEquals("Wrong number of modifications", 0, modifications
142: .size());
143:
144: // Verify a successful build shows up
145: File yesterdayLog2 = new File(tempDir, Log.formatLogFileName(
146: yesterday, "good.1"));
147: yesterdayLog2.createNewFile();
148: filesToDelete.add(yesterdayLog2);
149:
150: modifications = buildStatus.getModifications(twoDaysAgo, null);
151: assertEquals("Wrong number of modifications", 1, modifications
152: .size());
153:
154: Map properties = buildStatus.getProperties();
155: // Verify properties were set correctly
156: assertEquals("Property was not set correctly", tempDir
157: .getAbsolutePath(), properties
158: .get(BuildStatus.MOST_RECENT_LOGDIR_KEY));
159: assertEquals("Property was not set correctly", yesterdayLog2
160: .getName(), properties
161: .get(BuildStatus.MOST_RECENT_LOGFILE_KEY));
162: assertEquals("Property was not set correctly", DateUtil
163: .getFormattedTime(yesterday), properties
164: .get(BuildStatus.MOST_RECENT_LOGTIME_KEY));
165: assertEquals("Property was not set correctly", "good.1",
166: properties.get(BuildStatus.MOST_RECENT_LOGLABEL_KEY));
167: assertEquals(4, properties.size());
168:
169: // Verify date range works
170: modifications = buildStatus.getModifications(today, null);
171: assertEquals("Wrong number of modifications", 0, modifications
172: .size());
173:
174: // Verify properties are set correctly when there are multiple modifications
175: File todayLog = new File(tempDir, Log.formatLogFileName(today,
176: "good.2"));
177: todayLog.createNewFile();
178: filesToDelete.add(todayLog);
179:
180: buildStatus.setProperty("property");
181: modifications = buildStatus.getModifications(twoDaysAgo, null);
182: assertEquals("Wrong number of modifications", 2, modifications
183: .size());
184:
185: properties = buildStatus.getProperties();
186: // Verify properties were set correctly
187: assertEquals("Property was not set correctly", tempDir
188: .getAbsolutePath(), properties
189: .get(BuildStatus.MOST_RECENT_LOGDIR_KEY));
190: assertEquals("Property was not set correctly", todayLog
191: .getName(), properties
192: .get(BuildStatus.MOST_RECENT_LOGFILE_KEY));
193: assertEquals("Property was not set correctly", DateUtil
194: .getFormattedTime(today), properties
195: .get(BuildStatus.MOST_RECENT_LOGTIME_KEY));
196: assertEquals("Property was not set correctly", "good.2",
197: properties.get(BuildStatus.MOST_RECENT_LOGLABEL_KEY));
198: assertEquals("true", properties.get("property"));
199: assertEquals(5, properties.size());
200: }
201: }
|