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:
043: import junit.framework.TestCase;
044: import net.sourceforge.cruisecontrol.CruiseControlException;
045: import net.sourceforge.cruisecontrol.sourcecontrols.accurev.AccurevCommand;
046: import net.sourceforge.cruisecontrol.sourcecontrols.accurev.AccurevCommandline;
047: import net.sourceforge.cruisecontrol.sourcecontrols.accurev.AccurevInputParser;
048: import net.sourceforge.cruisecontrol.sourcecontrols.accurev.DateTimespec;
049: import net.sourceforge.cruisecontrol.sourcecontrols.accurev.Timespec;
050: import net.sourceforge.cruisecontrol.sourcecontrols.accurev.TransactionNumberTimespec;
051: import net.sourceforge.cruisecontrol.util.Commandline;
052:
053: /**
054: * Basic test cases for the accurev command line utilities
055: *
056: * AccurevBootstrapper has its own test in
057: * net.sourceforge.cruisecontrol.bootstrappers.AccurevBootstrapperTest
058: *
059: * Accurev sourcecontrol has its own test in
060: * net.sourceforge.cruisecontrol.sourcecontrols.AccurevSourcecontrolTest
061: *
062: * @author <a href="mailto:Nicola_Orru@scee.net">Nicola Orru'</a>
063: */
064: public class AccurevTest extends TestCase implements AccurevInputParser {
065: private AccurevMockRunner mockRunner;
066:
067: public void setUp() {
068: mockRunner = new AccurevMockRunner();
069: mockRunner
070: .setScriptRoot("net/sourceforge/cruisecontrol/sourcecontrols");
071: }
072:
073: protected void tearDown() throws Exception {
074: mockRunner = null;
075: }
076:
077: /**
078: * Tests common "accurev hist" commandline configurations
079: */
080: public void testCommandLineHist() {
081: AccurevCommandline hist;
082: fake("accurev_hist_now.txt", 0);
083: fake("accurev_hist_now_highest.txt", 0);
084: fake("accurev_blank.txt", 1);
085: fake("accurev_blank.txt", 1);
086: fake("accurev_hist_highest.txt", 0);
087: fake("accurev_hist_1-now.txt", 0);
088: fake("accurev_syntax_error.txt", 0);
089: hist = AccurevCommand.HIST.create(getMockRunner());
090: hist.setTransactionRange(DateTimespec.NOW);
091: hist.run();
092: assertTrue(hist.isSuccess());
093: hist = AccurevCommand.HIST.create(getMockRunner());
094: hist.setTransactionRange(DateTimespec.NOW,
095: TransactionNumberTimespec.HIGHEST);
096: hist.run();
097: assertTrue(hist.isSuccess());
098: hist = AccurevCommand.HIST.create(getMockRunner());
099: hist.setTransactionRange(new TransactionNumberTimespec(0),
100: DateTimespec.NOW);
101: hist.run();
102: assertFalse(hist.isSuccess());
103: hist = AccurevCommand.HIST.create(getMockRunner());
104: hist.setTransactionRange(new TransactionNumberTimespec(0));
105: hist.run();
106: assertFalse(hist.isSuccess());
107: hist = AccurevCommand.HIST.create(getMockRunner());
108: hist.setTransactionRange(TransactionNumberTimespec.HIGHEST);
109: hist.run();
110: assertTrue(hist.isSuccess());
111: hist = AccurevCommand.HIST.create(getMockRunner());
112: hist.setTransactionRange(new TransactionNumberTimespec(1),
113: DateTimespec.NOW);
114: hist.run();
115: assertTrue(hist.isSuccess());
116: try {
117: AccurevCommand.HIST.create().setWorkspaceLocalPath(
118: "ThisDirectoryIsNotSupposedToExist");
119: fail("setWorkspace should throw an exception when the workspace is not valid");
120: } catch (CruiseControlException e) {
121: // An exception must be thrown.
122: }
123: hist = AccurevCommand.HIST.create(getMockRunner());
124: assertFalse(hist.isSuccess());
125: AccurevCommand.HIST.create(getMockRunner());
126: hist.addArgument("--thisoptiondoesnotexist");
127: hist.run();
128: assertFalse(hist.isSuccess());
129: }
130:
131: /**
132: * Checks the command line is built as expected
133: */
134: public void testCommandLineBuild() {
135: Timespec d1 = new DateTimespec(-24);
136: Timespec d2 = new DateTimespec(0);
137: AccurevCommandline cmdKeep = AccurevCommand.KEEP.create();
138: cmdKeep.selectModified();
139: cmdKeep.setTransactionRange(d1, d2);
140: cmdKeep.setComment("Automatic keep");
141: assertEquals("accurev keep -m -t \"" + d1.toString() + "-"
142: + d2.toString() + "\" -c \"Automatic keep\"", cmdKeep
143: .toString());
144: AccurevCommandline cmdHist = AccurevCommand.HIST.create();
145: cmdHist.setTransactionRange(d1, d2);
146: assertEquals("accurev hist -t \"" + d1.toString() + "-"
147: + d2.toString() + "\"", cmdHist.toString());
148: Commandline cmdUpdate = AccurevCommand.UPDATE.create();
149: assertEquals("accurev update", cmdUpdate.toString());
150: Commandline cmdSynctime = AccurevCommand.SYNCTIME.create();
151: assertEquals("accurev synctime", cmdSynctime.toString());
152: }
153:
154: /**
155: * Tests common "accurev keep" commandline configuration
156: */
157: public void testCommandLineKeep() {
158: fake("accurev_keep_nofiles.txt", 0);
159: fake("accurev_keep.txt", 0);
160: AccurevCommandline keep;
161: keep = AccurevCommand.KEEP.create(getMockRunner());
162: assertFalse(keep.isSuccess());
163: keep = AccurevCommand.KEEP.create(getMockRunner());
164: keep.selectModified();
165: keep.setComment("Automatic keep");
166: keep.setVerbose(true);
167: keep.run();
168: assertTrue(keep.isSuccess());
169: keep = AccurevCommand.KEEP.create(getMockRunner());
170: keep.selectModified();
171: keep.setComment("Automatic keep");
172: keep.setVerbose(true);
173: keep.run();
174: assertTrue(keep.isSuccess());
175: }
176:
177: /**
178: * Runs "accurev help" and looks for the support@accurev.com string. parseStream is defined as the
179: * parsing callback
180: */
181: public void testCommandLineParse() {
182: fake("accurev_help.txt", 0);
183: AccurevCommandline help = AccurevCommand.HELP
184: .create(getMockRunner());
185: help.setInputParser(this );
186: help.run();
187: assertTrue(help.isSuccess());
188: }
189:
190: /**
191: * Helper for testCommandLineParse
192: */
193: public boolean parseStream(InputStream iStream)
194: throws CruiseControlException {
195: BufferedReader reader = new BufferedReader(
196: new InputStreamReader(iStream));
197: String line;
198: boolean accurevSupportFound = false;
199: try {
200: while ((line = reader.readLine()) != null) {
201: if (line.indexOf("support@accurev.com") >= 0) {
202: accurevSupportFound = true;
203: }
204: }
205: } catch (IOException e) {
206: throw new CruiseControlException(
207: "Error reading Accurev output");
208: }
209: return accurevSupportFound;
210: }
211:
212: /**
213: * Runs "accurev synctime"
214: */
215: public void testCommandLineSynctime() {
216: fake("accurev_synctime.txt", 0);
217: // you only have success after run
218: AccurevCommandline synctime;
219: synctime = AccurevCommand.SYNCTIME.create(getMockRunner());
220: assertFalse(synctime.isSuccess());
221: synctime = AccurevCommand.SYNCTIME.create(getMockRunner());
222: synctime.run();
223: assertTrue(synctime.isSuccess());
224: }
225:
226: /**
227: * Runs "accurev update" in the default workspace
228: */
229: public void testCommandLineUpdate() {
230: fake("accurev_update.txt", 0);
231: AccurevCommandline update;
232: update = AccurevCommand.UPDATE.create(getMockRunner());
233: assertFalse(update.isSuccess());
234: update = AccurevCommand.UPDATE.create(getMockRunner());
235: update.run();
236: assertTrue(update.isSuccess());
237: }
238:
239: protected AccurevMockRunner getMockRunner() {
240: return mockRunner;
241: }
242:
243: protected void setMockRunner(AccurevMockRunner mockRunner) {
244: this .mockRunner = mockRunner;
245: }
246:
247: public void fake(String path, int returnCode) {
248: mockRunner.addScript(path, returnCode);
249: }
250:
251: }
|