001: /********************************************************************************
002: * CruiseControl, a Continuous Integration Toolkit
003: * Copyright (c) 2007, 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 junit.framework.TestCase;
039: import net.sourceforge.cruisecontrol.CruiseControlException;
040: import net.sourceforge.cruisecontrol.Modification;
041: import org.jdom.JDOMException;
042:
043: import java.io.File;
044: import java.io.IOException;
045: import java.io.StringReader;
046: import java.text.ParseException;
047: import java.util.ArrayList;
048: import java.util.Calendar;
049: import java.util.Date;
050: import java.util.GregorianCalendar;
051: import java.util.List;
052: import java.util.TimeZone;
053:
054: /**
055: * @see <a href="http://smalltalk.cincom.com/">smalltalk.cincom.com</a>
056: * @author <a href="rcoulman@gmail.com">Randy Coulman</a>
057: */
058: public class StoreTest extends TestCase {
059: private Store store;
060: private TimeZone originalTimeZone;
061:
062: protected void setUp() throws Exception {
063: store = new Store();
064: originalTimeZone = TimeZone.getDefault();
065: }
066:
067: protected void tearDown() throws Exception {
068: TimeZone.setDefault(originalTimeZone);
069: }
070:
071: public void testValidate() throws IOException {
072: try {
073: store.validate();
074: fail("should throw an exception when no attributes are set");
075: } catch (CruiseControlException e) {
076: // expected
077: }
078:
079: store.setWorkingDirectory(".");
080: store.setProfile("local");
081: store.setPackages("PackageA,PackageB");
082:
083: store.setScript("notThere");
084: try {
085: store.validate();
086: fail("should throw an exception when an invalid 'script' attribute is set");
087: } catch (CruiseControlException e) {
088: // expected
089: }
090:
091: File tempFile = File.createTempFile("temp", "sh");
092: tempFile.deleteOnExit();
093:
094: store.setScript(tempFile.getAbsolutePath());
095: try {
096: store.validate();
097: } catch (CruiseControlException e) {
098: fail("should not throw an exception when a valid 'script' "
099: + "attribute is set");
100: }
101:
102: store.setWorkingDirectory("notthere");
103: try {
104: store.validate();
105: fail("should throw an exception when an invalid 'workingDirectory' attribute is set");
106: } catch (CruiseControlException e) {
107: // expected
108: }
109:
110: store.setProfile(null);
111: try {
112: store.validate();
113: fail("should throw an exception when no profile is set");
114: } catch (CruiseControlException e) {
115: // expected
116: }
117:
118: store = new Store();
119: store.setScript(tempFile.getAbsolutePath());
120: store.setProfile("local");
121:
122: try {
123: store.validate();
124: fail("should throw an exception when no packages are set");
125: } catch (CruiseControlException e) {
126: // expected
127: }
128:
129: store.setPackages("");
130: try {
131: store.validate();
132: fail("should throw an exception when an empty package list is set");
133: } catch (CruiseControlException e) {
134: // expected
135: }
136: }
137:
138: public void testBuildHistoryCommand() throws CruiseControlException {
139: store.setScript("store");
140: store.setProfile("local");
141: store.setPackages("PackageA,Package B,Package C");
142:
143: Date checkTime = new Date();
144: long tenMinutes = 10 * 60 * 1000;
145: Date lastBuild = new Date(checkTime.getTime() - tenMinutes);
146:
147: String[] expectedCmd = new String[] { "store", "-profile",
148: "local", "-packages", "PackageA", "Package B",
149: "Package C", "-lastBuild", Store.formatDate(lastBuild),
150: "-now", Store.formatDate(checkTime), "-check" };
151: String[] actualCmd = store.buildCommand(lastBuild, checkTime)
152: .getCommandline();
153: assertArraysEquals(expectedCmd, actualCmd);
154:
155: store.setVersionRegex("7.5 [0-9]+");
156:
157: expectedCmd = new String[] { "store", "-profile", "local",
158: "-packages", "PackageA", "Package B", "Package C",
159: "-versionRegex", "7.5 [0-9]+", "-lastBuild",
160: Store.formatDate(lastBuild), "-now",
161: Store.formatDate(checkTime), "-check" };
162: actualCmd = store.buildCommand(lastBuild, checkTime)
163: .getCommandline();
164: assertArraysEquals(expectedCmd, actualCmd);
165:
166: store.setMinimumBlessingLevel("Work In Progress");
167:
168: expectedCmd = new String[] { "store", "-profile", "local",
169: "-packages", "PackageA", "Package B", "Package C",
170: "-versionRegex", "7.5 [0-9]+", "-blessedAtLeast",
171: "Work In Progress", "-lastBuild",
172: Store.formatDate(lastBuild), "-now",
173: Store.formatDate(checkTime), "-check" };
174: actualCmd = store.buildCommand(lastBuild, checkTime)
175: .getCommandline();
176: assertArraysEquals(expectedCmd, actualCmd);
177:
178: store.setParcelBuilderFile("/path/to/parcelsToBuild");
179:
180: expectedCmd = new String[] { "store", "-profile", "local",
181: "-packages", "PackageA", "Package B", "Package C",
182: "-versionRegex", "7.5 [0-9]+", "-blessedAtLeast",
183: "Work In Progress", "-lastBuild",
184: Store.formatDate(lastBuild), "-now",
185: Store.formatDate(checkTime), "-parcelBuilderFile",
186: "/path/to/parcelsToBuild", "-check" };
187: actualCmd = store.buildCommand(lastBuild, checkTime)
188: .getCommandline();
189: assertArraysEquals(expectedCmd, actualCmd);
190: }
191:
192: public void testParseModifications() throws JDOMException,
193: ParseException, IOException {
194: String log = "<?xml version=\"1.0\"?>\n"
195: + "<log>\n"
196: + " <package action=\"modified\" name=\"PackageA\" version=\"44\">\n"
197: + " <blessing timestamp=\"04/27/2007 15:05:52.000\" user=\"fred\">blah</blessing>\n"
198: + " </package>\n"
199: + " <package action=\"modified\" name=\"PackageA\" version=\"43\">\n"
200: + " <blessing timestamp=\"04/27/2007 5:58:47.000\" user=\"fred\">"
201: + "Replicated by: fred from: psql_public_cst to: local</blessing>\n"
202: + " <blessing timestamp=\"03/27/2007 5:05:52.000\" user=\"barney\">did something</blessing>\n"
203: + " </package>\n"
204: + " <package action=\"added\" name=\"PackageB\" version=\"7.5 1\">\n"
205: + " <blessing timestamp=\"04/26/2007 17:16:23.000\" user=\"wilma\">initial</blessing>\n"
206: + " </package>\n" + "</log>";
207:
208: List modifications = Store.StoreLogXMLParser
209: .parse(new StringReader(log));
210: assertEquals(4, modifications.size());
211:
212: Modification modification = createModification(Store
213: .getDateFormatter().parse("04/27/2007 15:05:52.000"),
214: "fred", "blah", "44", "PackageA", "modified");
215: assertEquals(modification, modifications.get(0));
216:
217: modification = createModification(Store.getDateFormatter()
218: .parse("04/27/2007 5:58:47.000"), "fred",
219: "Replicated by: fred from: psql_public_cst to: local",
220: "43", "PackageA", "modified");
221: assertEquals(modification, modifications.get(1));
222:
223: modification = createModification(Store.getDateFormatter()
224: .parse("03/27/2007 5:05:52.000"), "barney",
225: "did something", "43", "PackageA", "modified");
226: assertEquals(modification, modifications.get(2));
227:
228: modification = createModification(Store.getDateFormatter()
229: .parse("04/26/2007 17:16:23.000"), "wilma", "initial",
230: "7.5 1", "PackageB", "added");
231: assertEquals(modification, modifications.get(3));
232: }
233:
234: public void testParseEmptyModifications() throws JDOMException,
235: ParseException, IOException {
236: String log = "<?xml version=\"1.0\"?>\n " + "<log>\n"
237: + "</log>";
238:
239: List modifications = Store.StoreLogXMLParser
240: .parse(new StringReader(log));
241: assertEquals(0, modifications.size());
242: }
243:
244: public void testFormatDatesForSvnLog() {
245: TimeZone.setDefault(TimeZone.getTimeZone("GMT+10:00"));
246:
247: Date maySeventeenSixPM2001 = new GregorianCalendar(2001,
248: Calendar.MAY, 17, 18, 0, 0).getTime();
249: assertEquals("05/17/2001 08:00:00.000", Store
250: .formatDate(maySeventeenSixPM2001));
251:
252: Date maySeventeenEightAM2001 = new GregorianCalendar(2001,
253: Calendar.MAY, 17, 8, 0, 0).getTime();
254: assertEquals("05/16/2001 22:00:00.000", Store
255: .formatDate(maySeventeenEightAM2001));
256:
257: TimeZone.setDefault(TimeZone.getTimeZone("GMT-10:00"));
258:
259: Date marchTwelfFourPM2003 = new GregorianCalendar(2003,
260: Calendar.MARCH, 12, 16, 0, 0).getTime();
261: assertEquals("03/13/2003 02:00:00.000", Store
262: .formatDate(marchTwelfFourPM2003));
263:
264: Date marchTwelfTenAM2003 = new GregorianCalendar(2003,
265: Calendar.MARCH, 12, 10, 0, 0).getTime();
266: assertEquals("03/12/2003 20:00:00.000", Store
267: .formatDate(marchTwelfTenAM2003));
268: }
269:
270: public void testSetProperty() throws ParseException {
271: store.setProperty("hasChanges?");
272:
273: List noModifications = new ArrayList();
274: store.fillPropertiesIfNeeded(noModifications);
275: assertEquals(null, store.getProperties().get("hasChanges?"));
276:
277: List hasModifications = new ArrayList();
278: hasModifications.add(createModification(Store
279: .getDateFormatter().parse("04/27/2007 15:05:52.000"),
280: "fred", "blah", "44", "PackageA", "modified"));
281: store.fillPropertiesIfNeeded(hasModifications);
282: assertEquals("true", store.getProperties().get("hasChanges?"));
283: }
284:
285: private static Modification createModification(Date date,
286: String user, String comment, String revision, String file,
287: String type) {
288: Modification modification = new Modification("store");
289: Modification.ModifiedFile modifiedFile = modification
290: .createModifiedFile(file, "");
291: modifiedFile.action = type;
292: modifiedFile.revision = revision;
293:
294: modification.modifiedTime = date;
295: modification.userName = user;
296: modification.comment = comment;
297: modification.revision = revision;
298: return modification;
299: }
300:
301: private static void assertArraysEquals(Object[] expected,
302: Object[] actual) {
303: assertEquals("array lengths mismatch!", expected.length,
304: actual.length);
305: for (int i = 0; i < expected.length; i++) {
306: assertEquals(expected[i], actual[i]);
307: }
308: }
309: }
|