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.File;
040: import java.io.FileReader;
041: import java.io.IOException;
042: import java.net.URI;
043: import java.net.URISyntaxException;
044: import java.text.SimpleDateFormat;
045: import java.util.ArrayList;
046: import java.util.Calendar;
047: import java.util.Date;
048: import java.util.List;
049: import java.util.Map;
050:
051: import junit.framework.TestCase;
052: import net.sourceforge.cruisecontrol.CruiseControlException;
053: import net.sourceforge.cruisecontrol.Modification;
054: import net.sourceforge.cruisecontrol.sourcecontrols.PVCS.PvcsStreamConsumer;
055: import net.sourceforge.cruisecontrol.util.Commandline;
056:
057: import org.apache.log4j.Logger;
058:
059: public class PVCSTest extends TestCase {
060:
061: private static final Logger LOG = Logger.getLogger(PVCSTest.class);
062:
063: private PVCS pvcs;
064:
065: public void setUp() {
066: pvcs = new PVCS();
067: }
068:
069: public void testValidate() {
070: try {
071: pvcs.validate();
072: fail("PVCS should throw exceptions when required attributes are not set.");
073: } catch (CruiseControlException e) {
074: assertTrue(true);
075: }
076:
077: pvcs.setPvcsproject("project");
078: pvcs.setPvcssubproject("subproject");
079:
080: try {
081: pvcs.validate();
082: assertTrue(true);
083: } catch (CruiseControlException e) {
084: fail("PVCS should not throw exceptions when required attributes are set.");
085: }
086: }
087:
088: public void testGetExecutable() {
089: String testExe = "testexe";
090: assertEquals("Wrong pvcs bin setting w/out bin set.", testExe,
091: pvcs.getExecutable(testExe));
092:
093: pvcs.setPvcsbin("mybindir");
094: assertEquals("Wrong pvcs bin setting w/ bin set.", "mybindir"
095: + File.separator + testExe, pvcs.getExecutable(testExe));
096: }
097:
098: public void testBuildExecCommandWithVersionLabel() {
099: pvcs.setPvcsproject("C:/PVCS-Repos/TestProject/pvcs");
100: pvcs.setPvcssubproject("/TestProject");
101: pvcs.setPvcsversionlabel("Test Version Label");
102:
103: Commandline ccCommand = pvcs.buildExecCommand(
104: "11/23/2004 08:00AM", "11/23/2004 01:00PM");
105: String expectedCommand = pvcs.getExecutable("pcli")
106: + " "
107: + "run -ns -q vlog "
108: + "\"-ds11/23/2004 08:00AM\" \"-de11/23/2004 01:00PM\" "
109: + "-prC:/PVCS-Repos/TestProject/pvcs \"-vTest Version Label\" "
110: + "-z /TestProject";
111:
112: assertEquals("Wrong PVCS command generated!", expectedCommand,
113: ccCommand.toString());
114: }
115:
116: public void testBuildExecCommandWithNullVersionLabel() {
117: pvcs.setPvcsproject("C:/PVCS-Repos/TestProject/pvcs");
118: pvcs.setPvcssubproject("/TestProject");
119: pvcs.setPvcsversionlabel("");
120:
121: Commandline ccCommand = pvcs.buildExecCommand(
122: "11/23/2004 08:00AM", "11/23/2004 01:00PM");
123: String expectedCommand = pvcs.getExecutable("pcli")
124: + " "
125: + "run -ns -q vlog "
126: + "\"-ds11/23/2004 08:00AM\" \"-de11/23/2004 01:00PM\" "
127: + "-prC:/PVCS-Repos/TestProject/pvcs "
128: + "-z /TestProject";
129: assertEquals("Wrong PVCS command generated!", expectedCommand,
130: ccCommand.toString());
131: }
132:
133: public void testBuildExecCommandWithoutVersionLabel() {
134: pvcs.setPvcsproject("C:/PVCS-Repos/TestProject/pvcs");
135: pvcs.setPvcssubproject("/TestProject");
136:
137: Commandline ccCommand = pvcs.buildExecCommand(
138: "11/23/2004 08:00AM", "11/23/2004 01:00PM");
139: String expectedCommand = pvcs.getExecutable("pcli")
140: + " "
141: + "run -ns -q vlog "
142: + "\"-ds11/23/2004 08:00AM\" \"-de11/23/2004 01:00PM\" "
143: + "-prC:/PVCS-Repos/TestProject/pvcs "
144: + "-z /TestProject";
145:
146: assertEquals("Wrong PVCS command generated!", expectedCommand,
147: ccCommand.toString());
148: }
149:
150: public void testBuildExecCommandWithoutLoginId() {
151: pvcs.setPvcsproject("C:/PVCS-Repos/TestProject/pvcs");
152: pvcs.setPvcssubproject("/TestProject");
153:
154: Commandline ccCommand = pvcs.buildExecCommand(
155: "11/23/2004 08:00AM", "11/23/2004 01:00PM");
156: String expectedCommand = pvcs.getExecutable("pcli")
157: + " "
158: + "run -ns -q vlog "
159: + "\"-ds11/23/2004 08:00AM\" \"-de11/23/2004 01:00PM\" "
160: + "-prC:/PVCS-Repos/TestProject/pvcs "
161: + "-z /TestProject";
162:
163: assertEquals("Wrong PVCS command generated!", expectedCommand,
164: ccCommand.toString());
165: }
166:
167: public void testBuildExecCommandWithEmptyLoginId() {
168: pvcs.setPvcsproject("C:/PVCS-Repos/Test Project/pvcs");
169: pvcs.setPvcssubproject("/TestProject");
170: pvcs.setLoginid("");
171:
172: Commandline ccCommand = pvcs.buildExecCommand(
173: "11/23/2004 08:00AM", "11/23/2004 01:00PM");
174: String expectedCommand = pvcs.getExecutable("pcli")
175: + " "
176: + "run -ns -q vlog "
177: + "\"-ds11/23/2004 08:00AM\" \"-de11/23/2004 01:00PM\" "
178: + "\"-prC:/PVCS-Repos/Test Project/pvcs\" "
179: + "-z /TestProject";
180:
181: assertEquals("Wrong PVCS command generated!", expectedCommand,
182: ccCommand.toString());
183: }
184:
185: public void testBuildExecCommandWithLoginId() {
186: pvcs.setPvcsproject("C:/PVCS-Repos/TestProject/pvcs");
187: pvcs.setPvcssubproject("/TestProject");
188: pvcs.setLoginid("TestUser");
189:
190: Commandline ccCommand = pvcs.buildExecCommand(
191: "11/23/2004 08:00AM", "11/23/2004 01:00PM");
192: String expectedCommand = pvcs.getExecutable("pcli")
193: + " "
194: + "run -ns -q vlog "
195: + "-idTestUser \"-ds11/23/2004 08:00AM\" "
196: + "\"-de11/23/2004 01:00PM\" -prC:/PVCS-Repos/TestProject/pvcs "
197: + "-z /TestProject";
198:
199: assertEquals("Wrong PVCS command generated!", expectedCommand,
200: ccCommand.toString());
201: }
202:
203: public void testPvcsStreamConsumer() throws URISyntaxException {
204: Calendar cal = Calendar.getInstance();
205: cal.set(2004, 11, 23);
206: Date date = cal.getTime();
207: PvcsStreamConsumer consumer = new PvcsStreamConsumer(date,
208: new SimpleDateFormat("MMM dd yyyy HH:mm:ss"),
209: "Services", "-arc");
210:
211: BufferedReader brIn;
212: try {
213: brIn = new BufferedReader(new FileReader(new File(
214: new URI(getClass().getResource("vlog.txt")
215: .toExternalForm()))));
216: String line;
217: while ((line = brIn.readLine()) != null) {
218: consumer.consumeLine(line);
219: }
220: brIn.close();
221: } catch (IOException e) {
222: LOG
223: .error(
224: "Error in reading vlog file of PVCS modifications : ",
225: e);
226: }
227:
228: List mods = consumer.getModificationList();
229: assertEquals(7, mods.size());
230: Modification mod1 = (Modification) mods.get(0);
231: assertEquals("Initial revision", mod1.comment);
232: Modification mod2 = (Modification) mods.get(1);
233: assertEquals("Add code for "
234: + System.getProperty("line.separator") + "Sections",
235: mod2.comment);
236:
237: }
238:
239: public void testProperty() throws IOException, InterruptedException {
240: pvcs = new PVCS() {
241: protected void executeCommandline(Commandline command,
242: PvcsStreamConsumer consumer) {
243: // do nothing
244: }
245: };
246: Date lastBuild = new Date();
247: PvcsStreamConsumer consumer = new PvcsStreamConsumer(lastBuild,
248: new SimpleDateFormat("MMM dd yyyy HH:mm:ss"),
249: "C:/PVCS-Repos/TestProject/pvcs", "-arc") {
250: public List getModificationList() {
251: List mods = new ArrayList();
252: mods.add("modification");
253: return mods;
254: }
255: };
256: pvcs.getModifications(lastBuild, new Date(), consumer);
257: pvcs.getModifications(lastBuild, new Date());
258: assertEquals(0, pvcs.getProperties().size());
259:
260: pvcs.setProperty("property");
261: pvcs.getModifications(lastBuild, new Date(), consumer);
262: Map properties = pvcs.getProperties();
263: assertEquals(1, properties.size());
264: assertTrue(properties.containsKey("property"));
265: }
266: }
|