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.BufferedReader;
039: import java.io.InputStreamReader;
040: import java.io.IOException;
041: import java.io.InputStream;
042: import java.text.ParseException;
043: import java.text.SimpleDateFormat;
044: import java.util.Date;
045: import java.util.List;
046: import java.util.ArrayList;
047: import java.util.Map;
048:
049: import junit.framework.TestCase;
050: import net.sourceforge.cruisecontrol.CruiseControlException;
051: import net.sourceforge.cruisecontrol.Modification;
052: import net.sourceforge.cruisecontrol.util.ManagedCommandline;
053:
054: /**
055: * The unit test for an AlienBrain source control interface for
056: * CruiseControl
057: *
058: * @author <a href="mailto:scottj+cc@escherichia.net">Scott Jacobs</a>
059: */
060: public class AlienBrainTest extends TestCase {
061:
062: private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat(
063: "M/d/yyyy z");
064: private static final Date NT_TIME_ZERO;
065: private static final Date JAVA_TIME_ZERO;
066:
067: static {
068: try {
069: NT_TIME_ZERO = DATE_FORMAT.parse("1/1/1601 UTC");
070: JAVA_TIME_ZERO = DATE_FORMAT.parse("1/1/1970 UTC");
071: } catch (ParseException e) {
072: throw new RuntimeException(e.getMessage());
073: }
074: }
075:
076: /**
077: * Just want to see if the AlienBrain class can even be found.
078: */
079: public void testConstruction() {
080: new AlienBrain();
081: }
082:
083: public void testProperty() {
084: AlienBrain ab = new AlienBrain() {
085:
086: protected List getModificationsFromAlienBrain(
087: Date lastBuild, Date now) throws IOException,
088: CruiseControlException {
089: List mods = new ArrayList();
090: mods.add("modification");
091: return mods;
092: }
093:
094: };
095: ab.setPath("path");
096: ab.getModifications(new Date(), new Date());
097: assertEquals(0, ab.getProperties().size());
098:
099: ab.setProperty("property");
100: ab.getModifications(new Date(), new Date());
101: Map properties = ab.getProperties();
102: assertEquals(1, properties.size());
103: assertTrue(properties.containsKey("property"));
104: }
105:
106: /**
107: */
108: public void testValidate() {
109: AlienBrain ab = new AlienBrain();
110:
111: try {
112: ab.validate();
113: fail("AlienBrain should throw exceptions when required "
114: + "attributes are not set.");
115: } catch (CruiseControlException expected) {
116: }
117:
118: ab.setPath("Module1");
119:
120: try {
121: ab.validate();
122: } catch (CruiseControlException expected) {
123: fail("AlienBrain should not throw exceptions when required "
124: + "attributes are set.\n" + expected);
125: }
126:
127: }
128:
129: public void testDateToFiletime() throws ParseException {
130: assertEquals(0L, AlienBrain.dateToFiletime(NT_TIME_ZERO));
131: assertEquals(116444736000000000L, AlienBrain
132: .dateToFiletime(JAVA_TIME_ZERO));
133: assertEquals(127610208000000000L, AlienBrain
134: .dateToFiletime(DATE_FORMAT.parse("5/20/2005 UTC")));
135: }
136:
137: public void testFiletimeToDate() throws ParseException {
138: assertEquals(NT_TIME_ZERO, AlienBrain.filetimeToDate(0L));
139: assertEquals(JAVA_TIME_ZERO, AlienBrain
140: .filetimeToDate(116444736000000000L));
141: assertEquals(DATE_FORMAT.parse("5/20/2005 UTC"), AlienBrain
142: .filetimeToDate(127610208000000000L));
143:
144: Date now = new Date();
145: assertEquals(now, AlienBrain.filetimeToDate(AlienBrain
146: .dateToFiletime(now)));
147: }
148:
149: public void testBuildGetModificationsCommand()
150: throws ParseException {
151: AlienBrain ab = new AlienBrain();
152:
153: ab.setUser("FooUser");
154: ab.setPath("FooProject");
155:
156: Date date = DATE_FORMAT.parse("5/20/2005 -0400");
157: ManagedCommandline cmdLine = ab.buildGetModificationsCommand(
158: date, date);
159:
160: assertEquals(
161: "ab -u FooUser find FooProject -regex \"SCIT > "
162: + "127610352000000000\" "
163: + "-format \"#SCIT#|#DbPath#|#Changed By#|#CheckInComment#\"",
164: cmdLine.toString());
165: }
166:
167: public void testParseModificationDescription()
168: throws ParseException {
169: Modification m = AlienBrain
170: .parseModificationDescription("127610352000000000|/a/path/to/a/file.cpp|sjacobs|"
171: + "A change that probably breaks everything.");
172:
173: assertEquals(DATE_FORMAT.parse("5/20/2005 -0400"),
174: m.modifiedTime);
175: assertEquals("sjacobs", m.userName);
176: assertEquals("A change that probably breaks everything.",
177: m.comment);
178: //The CC AlienBrain SourceControl class does not yet support changesets.
179: //therefore each modified file results in a modification containing
180: //one file.
181: assertEquals(1, m.files.size());
182: assertEquals("/a/path/to/a/file.cpp",
183: ((Modification.ModifiedFile) (m.files.get(0))).fileName);
184: }
185:
186: /**
187: * Returns a file as a List of Strings, one String per line.
188: */
189: private List loadTestLog(String name) throws IOException {
190: InputStream testStream = getClass().getResourceAsStream(name);
191: assertNotNull("failed to load resource " + name + " in class "
192: + getClass().getName(), testStream);
193:
194: List lines = new ArrayList();
195: String line;
196: BufferedReader reader = new BufferedReader(
197: new InputStreamReader(testStream));
198: while ((line = reader.readLine()) != null) {
199: lines.add(line);
200: }
201:
202: return lines;
203: }
204:
205: public void testParseModifications() throws IOException,
206: ParseException {
207: List results = loadTestLog("alienbrain_modifications.txt");
208:
209: AlienBrain ab = new AlienBrain();
210:
211: List modifications = ab.parseModifications(results);
212:
213: assertEquals("Returned wrong number of modifications.", 7,
214: modifications.size());
215:
216: SimpleDateFormat dateFormat = new SimpleDateFormat(
217: "M/d/yyyy HH:mm:ss z");
218: assertEquals("Wrong modification time", dateFormat
219: .parse("4/19/2005 16:51:55 -0400"),
220: ((Modification) modifications.get(0)).modifiedTime);
221:
222: assertEquals(
223: "Wrong path",
224: "/FooProject/Code/Vehicles/Src/Position.cpp",
225: ((Modification.ModifiedFile) (((Modification) modifications
226: .get(0)).files.get(0))).fileName);
227:
228: assertEquals("Wrong user", "User 1",
229: ((Modification) modifications.get(0)).userName);
230:
231: assertEquals("Wrong comment", "Passenger Animatoin",
232: ((Modification) modifications.get(0)).comment);
233:
234: assertEquals("Wrong modification time", dateFormat
235: .parse("5/7/2005 7:44:45 -0400"),
236: ((Modification) modifications.get(6)).modifiedTime);
237:
238: assertEquals(
239: "Wrong path",
240: "/FooProject/Code/Vehicles/Src/Materialnfo.cpp",
241: ((Modification.ModifiedFile) (((Modification) modifications
242: .get(6)).files.get(0))).fileName);
243:
244: assertEquals("Wrong user", "User 1",
245: ((Modification) modifications.get(6)).userName);
246:
247: assertEquals("Wrong comment", "Import from 2004",
248: ((Modification) modifications.get(6)).comment);
249: }
250:
251: /**
252: */
253: public void testParseNoModifications() throws IOException {
254: List results = loadTestLog("alienbrain_nomodifications.txt");
255:
256: AlienBrain ab = new AlienBrain();
257:
258: List modifications = ab.parseModifications(results);
259: assertEquals(0, modifications.size());
260: }
261:
262: //The following tests all actually use the AlienBrain executable and
263: //may need to access a server. Therefore they can only be run if you
264: //have a licensed command-line client and access to a server.
265: /*
266: //In order for some of the following tests to pass, these members must
267: //be assigned values valid for your AlienBrain server.
268: private static final String TESTING_PATH = "alienbrain://Projects/Code/Engine/Inc";
269: private static final String TESTING_BRANCH = "Root Branch/SubBranch";
270: // Set any of the following to null if you do not want to
271: // override any NXN_AB_* environment variables you may be using.
272: private static final String TESTING_USERNAME = null; //"sjacobs";
273: private static final String TESTING_PASSWORD = null; //"pass123";
274: private static final String TESTING_SERVER = null; //"abserver";
275: private static final String TESTING_DATABASE = null; //"StudioVault";
276:
277: public void testGetModifications() throws Exception {
278: AlienBrain ab = new AlienBrain();
279:
280: ab.setServer(TESTING_SERVER);
281: ab.setDatabase(TESTING_DATABASE);
282: ab.setUser(TESTING_USERNAME);
283: ab.setPassword(TESTING_PASSWORD);
284: ab.setPath(TESTING_PATH);
285:
286: List modifications = ab.getModifications(new Date(0), new Date());
287: assertTrue("I would have expected the AlienBrain database "
288: + "to have at least one file modified since 1970!",
289: 0 != modifications.size());
290:
291: for (java.util.Iterator it = modifications.iterator(); it.hasNext(); ) {
292: Modification m = (Modification) it.next();
293: System.out.println(m);
294: }
295: }
296:
297: public static void main(String[] args) {
298: junit.textui.TestRunner.run(AlienBrainTest.class);
299: }
300: */// End of tests that require an actual AlienBrain installation.
301: }
|