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.File;
039: import java.io.IOException;
040: import java.io.InputStream;
041: import java.text.ParseException;
042: import java.text.SimpleDateFormat;
043: import java.util.Date;
044: import java.util.List;
045:
046: import junit.framework.TestCase;
047: import net.sourceforge.cruisecontrol.CruiseControlException;
048: import net.sourceforge.cruisecontrol.Modification;
049: import net.sourceforge.cruisecontrol.Modification.ModifiedFile;
050:
051: public class PlasticSCMTest extends TestCase {
052:
053: private static final String QUERY_RESULT = "plasticscm-query.txt";
054:
055: private final SimpleDateFormat dateFormat = new SimpleDateFormat(
056: PlasticSCM.DATEFORMAT);
057:
058: private PlasticSCM plasticscm;
059:
060: private final String workingDir = "plastictestdir";
061: private File fileWorkingDir = new File(workingDir);
062:
063: protected void setUp() {
064: plasticscm = new PlasticSCM();
065: if (fileWorkingDir.exists()) {
066: fileWorkingDir.delete();
067: }
068: }
069:
070: public void tearDown() {
071: plasticscm = null;
072: fileWorkingDir.delete();
073: }
074:
075: public void testValidate() throws CruiseControlException {
076:
077: try {
078: plasticscm.validate();
079: fail("PlasticSCM should throw exceptions when required fields are not set.");
080: } catch (CruiseControlException e) {
081: }
082:
083: plasticscm = new PlasticSCM();
084: plasticscm.setBranch("br:/main");
085: try {
086: plasticscm.validate();
087: fail("PlasticSCM should throw an exception when the field wkspath is not set");
088: } catch (CruiseControlException e) {
089: }
090:
091: plasticscm = new PlasticSCM();
092: plasticscm.setWkspath(workingDir);
093: try {
094: plasticscm.validate();
095: fail("PlasticSCM should throw an exception when the field branch is not set");
096: } catch (CruiseControlException e) {
097: }
098:
099: plasticscm = new PlasticSCM();
100: plasticscm.setWkspath(workingDir);
101: plasticscm.setBranch("br:/main");
102: try {
103: plasticscm.validate();
104: fail("PlasticSCM should throw an exception when wkspath not exists.");
105: } catch (CruiseControlException e) {
106: }
107:
108: plasticscm = new PlasticSCM();
109: fileWorkingDir.mkdir();
110: plasticscm.setWkspath(workingDir);
111: plasticscm.setBranch("br:/main");
112: plasticscm.validate();
113:
114: plasticscm = new PlasticSCM();
115: plasticscm.setWkspath(workingDir);
116: plasticscm.setBranch("br:/main");
117: plasticscm.setRepository("mainrep");
118: plasticscm.validate();
119: }
120:
121: public void testBuildFindCommand() throws CruiseControlException,
122: ParseException {
123:
124: fileWorkingDir.mkdir();
125:
126: plasticscm.setWkspath(workingDir);
127: plasticscm.setBranch("br:/main");
128: Date lastBuild = dateFormat.parse("01.01.2007.00.00.01");
129: Date now = dateFormat.parse("03.03.2007.22.55.43");
130: String command = "cm find revision where branch = 'br:/main' "
131: + "and revno != 'CO' "
132: + "and date between '01.01.2007.00.00.01' and '03.03.2007.22.55.43' ";
133: String format = "--dateformat=\"" + PlasticSCM.DATEFORMAT
134: + "\" " + "--format=\"" + PlasticSCM.QUERYFORMAT + "\"";
135:
136: assertEquals(command + format, plasticscm.buildFindCommand(
137: lastBuild, now).toStringNoQuoting());
138:
139: plasticscm.setRepository("mainrep");
140:
141: assertEquals(command + "on repository 'mainrep' " + format,
142: plasticscm.buildFindCommand(lastBuild, now)
143: .toStringNoQuoting());
144: }
145:
146: /**
147: * Tests the result returned by a Plastic SCM query.
148: */
149: public void testParseQueryResult() throws IOException,
150: ParseException {
151: InputStream testStream = getClass().getResourceAsStream(
152: "plasticscm-query.txt");
153: assertNotNull("failed to load resource plasticscm-query.txt",
154: testStream);
155:
156: fileWorkingDir.mkdir();
157:
158: File srcDir = new File(workingDir + "/src");
159: File helloFile = new File(workingDir + "/src/HelloWorld.java");
160: File buildFile = new File(workingDir + "/build.xml");
161:
162: if (!srcDir.exists()) {
163: srcDir.mkdir();
164: }
165:
166: if (!helloFile.exists()) {
167: helloFile.createNewFile();
168: }
169:
170: if (!buildFile.exists()) {
171: buildFile.createNewFile();
172: }
173:
174: List list = plasticscm.parseStream(testStream);
175: assertEquals(2, list.size());
176:
177: Modification mod = new Modification("plasticscm");
178: ModifiedFile modfile = mod.createModifiedFile(
179: "HelloWorld.java", (new File(workingDir + "/src"))
180: .getPath());
181: mod.userName = "testing01";
182: mod.modifiedTime = dateFormat.parse("23.05.2007.15.41.30");
183: assertEquals(mod, list.get(0));
184:
185: mod = new Modification("plasticscm");
186: modfile = mod.createModifiedFile("build.xml", fileWorkingDir
187: .getPath());
188: mod.userName = "testing01";
189: mod.modifiedTime = dateFormat.parse("23.05.2007.15.41.40");
190: assertEquals(mod, list.get(1));
191:
192: buildFile.delete();
193: helloFile.delete();
194: srcDir.delete();
195:
196: }
197: }
|