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 net.sourceforge.cruisecontrol.util.Util;
042:
043: import java.io.File;
044: import java.io.IOException;
045: import java.io.StringReader;
046: import java.util.Date;
047: import java.util.List;
048: import java.util.ArrayList;
049: import java.util.Map;
050:
051: /**
052: * @see <a href="http://git.or.cz/">git.or.cz</a>
053: * @author <a href="rschiele@gmail.com">Robert Schiele</a>
054: */
055: public class GitTest extends TestCase {
056: private static final String NEWLINE = System
057: .getProperty("line.separator");
058: private Git git;
059:
060: protected void setUp() {
061: git = new Git();
062: }
063:
064: public void testValidate() throws IOException {
065: try {
066: git.validate();
067: fail("should throw an exception when no attributes are set");
068: } catch (CruiseControlException e) {
069: // expected
070: }
071:
072: git = new Git();
073: git.setLocalWorkingCopy("invalid directory");
074: try {
075: git.validate();
076: fail("should throw an exception when an invalid "
077: + "'localWorkingCopy' attribute is set");
078: } catch (CruiseControlException e) {
079: // expected
080: }
081:
082: File tempFile = File.createTempFile("temp", "txt");
083: tempFile.deleteOnExit();
084:
085: git = new Git();
086: git.setLocalWorkingCopy(tempFile.getParent());
087: try {
088: git.validate();
089: } catch (CruiseControlException e) {
090: fail("should not throw an exception when at least a valid "
091: + "'localWorkingCopy' attribute is set");
092: }
093:
094: git = new Git();
095: git.setLocalWorkingCopy(tempFile.getAbsolutePath());
096: try {
097: git.validate();
098: fail("should throw an exception when 'localWorkingCopy' is "
099: + "file instead of directory.");
100: } catch (CruiseControlException e) {
101: // expected
102: }
103: }
104:
105: public void testGitRevision() {
106: Date date = new Date(1190000000000L);
107: if (Util.isWindows()) {
108: assertEquals("\"@{ 1190000000}\"", Git.gitRevision(date));
109: } else {
110: assertEquals("@{ 1190000000}", Git.gitRevision(date));
111: }
112: }
113:
114: public void testParseLog() throws IOException {
115: String gitLog = "commit 0a033e6b51bdae14c787fc915d96441c18e3a717\n"
116: + "author Robert Schiele <rschiele@gmail.com> 1190000297 -0700\n"
117: + "\n"
118: + " latest commit\n"
119: + " \n"
120: + " this is a multi line commit message\n"
121: + "\n"
122: + "diff --git a/README.txt b/README.txt\n"
123: + "index 43c6998..0235ed7 100644\n"
124: + "diff --git a/oldfile.txt b/oldfile.txt\n"
125: + "deleted file mode 100644\n"
126: + "index 43c6998..0000000\n"
127: + "diff --git a/newfile.txt b/newfile.txt\n"
128: + "new file mode 100644\n"
129: + "index 0000000..0235ed7\n"
130: + "\n"
131: + "commit 1111111111111111111111111111111111111111\n"
132: + "author Robert Schiele <rschiele@gmail.com> 1190000197 -0700\n"
133: + "\n"
134: + " merge commit\n"
135: + "\n"
136: + "commit 2222222222222222222222222222222222222222\n"
137: + "author Robert Schiele <rschiele@gmail.com> 1190000097 -0700\n"
138: + "\n"
139: + " first commit\n"
140: + "\n"
141: + "diff --git a/README.txt b/README.txt\n"
142: + "index 43c6998..0235ed7 100644\n";
143: List mods = new ArrayList();
144: SourceControlProperties props = new SourceControlProperties();
145: props.assignPropertyName("hasChanges?");
146: props.assignPropertyOnDeleteName("hasDeletions?");
147: Git.parseLog(new StringReader(gitLog), mods, props);
148: assertEquals(3, mods.size());
149:
150: Modification modref = new Modification("git");
151: modref.modifiedTime = new Date(1190000297000L);
152: modref.userName = "Robert Schiele";
153: modref.emailAddress = "rschiele@gmail.com";
154: modref.comment = "latest commit" + NEWLINE + NEWLINE
155: + "this is a multi line commit message" + NEWLINE;
156: modref.revision = "1190000297";
157: Modification mod = (Modification) mods.get(0);
158: assertEquals(modref, mod);
159: List mf = mod.getModifiedFiles();
160: assertEquals(3, mf.size());
161: assertEquals("README.txt", ((Modification.ModifiedFile) mf
162: .get(0)).fileName);
163: assertEquals("modified",
164: ((Modification.ModifiedFile) mf.get(0)).action);
165: assertEquals("1190000297", ((Modification.ModifiedFile) mf
166: .get(0)).revision);
167: assertEquals("oldfile.txt", ((Modification.ModifiedFile) mf
168: .get(1)).fileName);
169: assertEquals("deleted",
170: ((Modification.ModifiedFile) mf.get(1)).action);
171: assertEquals("1190000297", ((Modification.ModifiedFile) mf
172: .get(1)).revision);
173: assertEquals("newfile.txt", ((Modification.ModifiedFile) mf
174: .get(2)).fileName);
175: assertEquals("added",
176: ((Modification.ModifiedFile) mf.get(2)).action);
177: assertEquals("1190000297", ((Modification.ModifiedFile) mf
178: .get(2)).revision);
179:
180: modref = new Modification("git");
181: modref.modifiedTime = new Date(1190000197000L);
182: modref.userName = "Robert Schiele";
183: modref.emailAddress = "rschiele@gmail.com";
184: modref.comment = "merge commit" + NEWLINE;
185: modref.revision = "1190000197";
186: mod = (Modification) mods.get(1);
187: assertEquals(modref, mod);
188: mf = mod.getModifiedFiles();
189: assertEquals(0, mf.size());
190:
191: modref = new Modification("git");
192: modref.modifiedTime = new Date(1190000097000L);
193: modref.userName = "Robert Schiele";
194: modref.emailAddress = "rschiele@gmail.com";
195: modref.comment = "first commit" + NEWLINE;
196: modref.revision = "1190000097";
197: mod = (Modification) mods.get(2);
198: assertEquals(modref, mod);
199: mf = mod.getModifiedFiles();
200: assertEquals(1, mf.size());
201: assertEquals("README.txt", ((Modification.ModifiedFile) mf
202: .get(0)).fileName);
203: assertEquals("modified",
204: ((Modification.ModifiedFile) mf.get(0)).action);
205: assertEquals("1190000097", ((Modification.ModifiedFile) mf
206: .get(0)).revision);
207:
208: Map pm = props.getPropertiesAndReset();
209: assertEquals("true", pm.get("hasChanges?"));
210: assertEquals("true", pm.get("hasDeletions?"));
211: assertEquals("0a033e6b51bdae14c787fc915d96441c18e3a717", pm
212: .get("gitcommitid"));
213: }
214:
215: public void testParseEmptyLog() throws IOException {
216: List mods = new ArrayList();
217: SourceControlProperties props = new SourceControlProperties();
218: props.assignPropertyName("hasChanges?");
219: props.assignPropertyOnDeleteName("hasDeletions?");
220: Git.parseLog(new StringReader(""), mods, props);
221: assertEquals(0, mods.size());
222: Map pm = props.getPropertiesAndReset();
223: assertEquals(null, pm.get("hasChanges?"));
224: assertEquals(null, pm.get("hasDeletions?"));
225: assertEquals(null, pm.get("gitcommitid"));
226: }
227: }
|