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.IOException;
040: import java.io.InputStream;
041: import java.io.InputStreamReader;
042: import java.text.ParseException;
043: import java.text.SimpleDateFormat;
044: import java.util.ArrayList;
045: import java.util.Calendar;
046: import java.util.Date;
047: import java.util.GregorianCalendar;
048: import java.util.List;
049: import java.util.StringTokenizer;
050:
051: import net.sourceforge.cruisecontrol.CruiseControlException;
052: import net.sourceforge.cruisecontrol.Modification;
053: import net.sourceforge.cruisecontrol.sourcecontrols.accurev.AccurevCommand;
054: import net.sourceforge.cruisecontrol.sourcecontrols.accurev.AccurevCommandline;
055: import net.sourceforge.cruisecontrol.sourcecontrols.accurev.AccurevInputParser;
056:
057: public class AccurevSourcecontrolTest extends AccurevTest {
058: public AccurevSourcecontrolTest() {
059: super ();
060: }
061:
062: public class LineCollector implements AccurevInputParser {
063: public List lines;
064:
065: public boolean parseStream(InputStream iStream)
066: throws CruiseControlException {
067: BufferedReader reader = new BufferedReader(
068: new InputStreamReader(iStream));
069: lines = new ArrayList();
070: String line;
071: try {
072: while ((line = reader.readLine()) != null) {
073: lines.add(line);
074: }
075: } catch (IOException e) {
076: throw new CruiseControlException(
077: "Error reading Accurev output");
078: }
079: return true;
080: }
081: }
082:
083: private static final SimpleDateFormat DTFM = new SimpleDateFormat(
084: "yyyy/MM/dd HH:mm:ss");
085:
086: private Date parseLogDateFormat(String dateString)
087: throws ParseException {
088: return DTFM.parse(dateString);
089: }
090:
091: Modification createModification(String userName,
092: String emailAddress, String comment, String revision,
093: String modifiedTime, String type) throws ParseException {
094: Modification modification = new Modification();
095: modification.userName = userName;
096: modification.emailAddress = emailAddress;
097: modification.comment = comment;
098: modification.revision = revision;
099: modification.modifiedTime = parseLogDateFormat(modifiedTime);
100: modification.type = type;
101: return modification;
102: }
103:
104: public void testAccurevSourcecontrol() throws Exception {
105: fake("accurev_show_wspaces.txt", 0);
106: fake("accurev_hist_lastbuild_now.txt", 0);
107: Accurev accurev = new Accurev();
108: accurev.setRunner(getMockRunner());
109: accurev.setVerbose(true);
110: accurev.setStream(getTestStreamName());
111: accurev.validate();
112: Calendar ago = new GregorianCalendar();
113: ago.add(Calendar.MONTH, -1);
114: List modifications = accurev.getModifications(ago.getTime(),
115: new GregorianCalendar().getTime());
116: accurev.getProperties();
117:
118: assertNotNull(modifications);
119: assertEquals(6, modifications.size());
120:
121: Modification modification;
122:
123: modification = createModification("norru", null, " Comment\n",
124: "120208", "2005/06/22 10:53:10", "keep");
125: assertEquals(modification, modifications.get(0));
126:
127: modification = createModification("norru", null, "", "120209",
128: "2005/06/22 10:54:44", "defcomp");
129: assertEquals(modification, modifications.get(1));
130:
131: modification = createModification("norru", null, "", "120210",
132: "2005/06/22 10:57:23", "defcomp");
133: assertEquals(modification, modifications.get(2));
134:
135: modification = createModification("norru", null, "", "120214",
136: "2005/06/22 11:01:07", "defcomp");
137: assertEquals(modification, modifications.get(3));
138:
139: modification = createModification("norru", null, "", "120216",
140: "2005/06/22 11:06:55", "defcomp");
141: assertEquals(modification, modifications.get(4));
142:
143: modification = createModification("norru", null, "", "120217",
144: "2005/06/22 11:07:27", "defcomp");
145: assertEquals(modification, modifications.get(5));
146: }
147:
148: /**
149: * Picks the last stream name from a list of streams
150: */
151: private String getTestStreamName() {
152: LineCollector collector = new LineCollector();
153: AccurevCommandline show = AccurevCommand.SHOW
154: .create(getMockRunner());
155: show.addArgument("wspaces");
156: show.setInputParser(collector);
157: show.run();
158: assertNotNull(collector.lines);
159: assertTrue(collector.lines.size() > 1);
160: String lastLine = collector.lines.get(
161: collector.lines.size() - 1).toString();
162: return getNameFromLine(lastLine);
163: }
164:
165: private String getNameFromLine(String lastLine) {
166: return new StringTokenizer(lastLine, " \t").nextToken();
167: }
168:
169: public void testValidate() {
170: Accurev accurev = new Accurev();
171: try {
172: accurev.validate();
173: fail("Validate should throw an exception when the stream is not set");
174: } catch (CruiseControlException e) {
175: }
176: }
177: }
|