001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.taskdefs.optional.vss;
019:
020: import java.io.File;
021: import java.text.SimpleDateFormat;
022: import java.util.Calendar;
023: import java.util.Date;
024: import java.util.TimeZone;
025:
026: import org.apache.tools.ant.BuildFileTest;
027: import org.apache.tools.ant.Location;
028: import org.apache.tools.ant.Project;
029: import org.apache.tools.ant.taskdefs.Tstamp;
030: import org.apache.tools.ant.types.Commandline;
031: import org.apache.tools.ant.types.Path;
032:
033: /**
034: * Testcase to ensure that command line generation and required attributes are correct.
035: *
036: */
037: public class MSVSSTest extends BuildFileTest implements MSVSSConstants {
038:
039: private Commandline commandline;
040:
041: private static final String VSS_SERVER_PATH = "\\\\server\\vss\\srcsafe.ini";
042: private static final String VSS_PROJECT_PATH = "/SourceRoot/Project";
043: private static final String DS_VSS_PROJECT_PATH = "$/SourceRoot/Project";
044: private static final String VSS_USERNAME = "ant";
045: private static final String VSS_PASSWORD = "rocks";
046: private static final String LOCAL_PATH = "testdir";
047: private static final String SRC_FILE = "Class1.java";
048: private static final String SRC_LABEL = "label1";
049: private static final String LONG_LABEL = "123456789012345678901234567890";
050: private static final String SRC_COMMENT = "I fixed a bug";
051: private static final String VERSION = "007";
052: private static final String DATE = "00-00-00";
053: private static final String DATE2 = "01-01-01";
054: private static final String OUTPUT = "output.log";
055: private static final String SS_DIR = "c:/winnt".replace('/',
056: File.separatorChar);
057:
058: /**
059: * Constructor for the MSVSSTest object
060: *
061: * @param s Test name
062: */
063: public MSVSSTest(String s) {
064: super (s);
065: }
066:
067: /**
068: * The JUnit setup method
069: *
070: * @throws Exception
071: */
072: protected void setUp() throws Exception {
073: project = new Project();
074: project.setBasedir(".");
075: }
076:
077: /**
078: * The teardown method for JUnit
079: *
080: * @throws Exception
081: */
082: protected void tearDown() throws Exception {
083: File file = new File(project.getBaseDir(), LOCAL_PATH);
084: if (file.exists()) {
085: file.delete();
086: }
087: }
088:
089: /** Tests VSSGet commandline generation. */
090: public void testGetCommandLine() {
091: String[] sTestCmdLine = {
092: MSVSS.SS_EXE,
093: MSVSS.COMMAND_GET,
094: DS_VSS_PROJECT_PATH,
095: MSVSS.FLAG_OVERRIDE_WORKING_DIR
096: + project.getBaseDir().getAbsolutePath()
097: + File.separator + LOCAL_PATH,
098: MSVSS.FLAG_AUTORESPONSE_DEF, MSVSS.FLAG_RECURSION,
099: MSVSS.FLAG_VERSION + VERSION,
100: MSVSS.FLAG_LOGIN + VSS_USERNAME + "," + VSS_PASSWORD,
101: FLAG_FILETIME_UPDATED, FLAG_SKIP_WRITABLE };
102:
103: // Set up a VSSGet task
104: MSVSSGET vssGet = new MSVSSGET();
105: vssGet.setProject(project);
106: vssGet.setRecursive(true);
107: vssGet.setLocalpath(new Path(project, LOCAL_PATH));
108: vssGet.setLogin(VSS_USERNAME + "," + VSS_PASSWORD);
109: vssGet.setVersion(VERSION);
110: vssGet.setQuiet(false);
111: vssGet.setDate(DATE);
112: vssGet.setLabel(SRC_LABEL);
113: vssGet.setVsspath(VSS_PROJECT_PATH);
114: MSVSS.CurrentModUpdated cmu = new MSVSS.CurrentModUpdated();
115: cmu.setValue(TIME_UPDATED);
116: vssGet.setFileTimeStamp(cmu);
117: MSVSS.WritableFiles wf = new MSVSS.WritableFiles();
118: wf.setValue(WRITABLE_SKIP);
119: vssGet.setWritableFiles(wf);
120:
121: commandline = vssGet.buildCmdLine();
122:
123: checkCommandLines(sTestCmdLine, commandline.getCommandline());
124: }
125:
126: /** Tests VSSGet required attributes. */
127: public void testGetExceptions() {
128: configureProject("src/etc/testcases/taskdefs/optional/vss/vss.xml");
129: expectSpecificBuildException("vssget.1", "some cause",
130: "vsspath attribute must be set!");
131: }
132:
133: /** Tests Label commandline generation. */
134: public void testLabelCommandLine1() {
135: String[] sTestCmdLine = { MSVSS.SS_EXE, MSVSS.COMMAND_LABEL,
136: DS_VSS_PROJECT_PATH, MSVSS.FLAG_COMMENT + SRC_COMMENT,
137: MSVSS.FLAG_AUTORESPONSE_YES,
138: MSVSS.FLAG_LABEL + SRC_LABEL,
139: MSVSS.FLAG_VERSION + VERSION,
140: MSVSS.FLAG_LOGIN + VSS_USERNAME + "," + VSS_PASSWORD };
141:
142: // Set up a VSSLabel task
143: MSVSSLABEL vssLabel = new MSVSSLABEL();
144: vssLabel.setProject(project);
145: vssLabel.setComment(SRC_COMMENT);
146: vssLabel.setLogin(VSS_USERNAME + "," + VSS_PASSWORD);
147: vssLabel.setVersion(VERSION);
148: vssLabel.setAutoresponse("Y");
149: vssLabel.setLabel(SRC_LABEL);
150: vssLabel.setVsspath(VSS_PROJECT_PATH);
151:
152: commandline = vssLabel.buildCmdLine();
153:
154: checkCommandLines(sTestCmdLine, commandline.getCommandline());
155: }
156:
157: /** Tests Label commandline generation with a label of more than 31 chars. */
158: public void testLabelCommandLine2() {
159: String[] sTestCmdLine = { MSVSS.SS_EXE, MSVSS.COMMAND_LABEL,
160: DS_VSS_PROJECT_PATH, MSVSS.FLAG_COMMENT + SRC_COMMENT,
161: MSVSS.FLAG_AUTORESPONSE_DEF,
162: MSVSS.FLAG_LABEL + LONG_LABEL,
163: MSVSS.FLAG_LOGIN + VSS_USERNAME + "," + VSS_PASSWORD };
164:
165: // Set up a VSSLabel task
166: MSVSSLABEL vssLabel = new MSVSSLABEL();
167: vssLabel.setProject(project);
168: vssLabel.setComment(SRC_COMMENT);
169: vssLabel.setLogin(VSS_USERNAME + "," + VSS_PASSWORD);
170: vssLabel.setLabel(LONG_LABEL + "blahblah");
171: vssLabel.setVsspath(VSS_PROJECT_PATH);
172:
173: commandline = vssLabel.buildCmdLine();
174:
175: checkCommandLines(sTestCmdLine, commandline.getCommandline());
176: }
177:
178: /**
179: * Test VSSLabel required attributes.
180: */
181: public void testLabelExceptions() {
182: configureProject("src/etc/testcases/taskdefs/optional/vss/vss.xml");
183: expectSpecificBuildException("vsslabel.1", "some cause",
184: "vsspath attribute must be set!");
185: expectSpecificBuildException("vsslabel.2", "some cause",
186: "label attribute must be set!");
187: }
188:
189: /** Tests VSSHistory commandline generation with from label. */
190: public void testHistoryCommandLine1() {
191: String[] sTestCmdLine = {
192: MSVSS.SS_EXE,
193: MSVSS.COMMAND_HISTORY,
194: DS_VSS_PROJECT_PATH,
195: MSVSS.FLAG_AUTORESPONSE_DEF,
196: MSVSS.FLAG_VERSION_LABEL + LONG_LABEL
197: + MSVSS.VALUE_FROMLABEL + SRC_LABEL,
198: MSVSS.FLAG_LOGIN + VSS_USERNAME + "," + VSS_PASSWORD,
199: MSVSS.FLAG_OUTPUT
200: + project.getBaseDir().getAbsolutePath()
201: + File.separator + OUTPUT };
202:
203: // Set up a VSSHistory task
204: MSVSSHISTORY vssHistory = new MSVSSHISTORY();
205: vssHistory.setProject(project);
206:
207: vssHistory.setLogin(VSS_USERNAME + "," + VSS_PASSWORD);
208:
209: vssHistory.setFromLabel(SRC_LABEL);
210: vssHistory.setToLabel(LONG_LABEL + "blahblah");
211: vssHistory.setVsspath(VSS_PROJECT_PATH);
212: vssHistory.setRecursive(false);
213: vssHistory.setOutput(new File(project.getBaseDir()
214: .getAbsolutePath(), OUTPUT));
215:
216: commandline = vssHistory.buildCmdLine();
217:
218: checkCommandLines(sTestCmdLine, commandline.getCommandline());
219: }
220:
221: /** Tests VSSHistory commandline generation with from date. */
222: public void testHistoryCommandLine2() {
223: String[] sTestCmdLine = {
224: MSVSS.SS_EXE,
225: MSVSS.COMMAND_HISTORY,
226: DS_VSS_PROJECT_PATH,
227: MSVSS.FLAG_AUTORESPONSE_DEF,
228: MSVSS.FLAG_VERSION_DATE + DATE + MSVSS.VALUE_FROMDATE
229: + DATE2, MSVSS.FLAG_RECURSION,
230: MSVSS.FLAG_LOGIN + VSS_USERNAME + "," + VSS_PASSWORD };
231:
232: // Set up a VSSHistory task
233: MSVSSHISTORY vssHistory = new MSVSSHISTORY();
234: vssHistory.setProject(project);
235: vssHistory.setLogin(VSS_USERNAME + "," + VSS_PASSWORD);
236: vssHistory.setFromDate(DATE2);
237: vssHistory.setToDate(DATE);
238: vssHistory.setVsspath(VSS_PROJECT_PATH);
239: vssHistory.setRecursive(true);
240:
241: commandline = vssHistory.buildCmdLine();
242:
243: checkCommandLines(sTestCmdLine, commandline.getCommandline());
244: }
245:
246: /** Tests VSSHistory commandline generation with date calculation. */
247: public void testHistoryCommandLine3() {
248: // Set up a Timestamp
249: Tstamp tstamp = new Tstamp();
250: Location location = new Location(
251: "src/etc/testcases/taskdefs/optional/vss/vss.xml");
252: tstamp.setLocation(location);
253: tstamp.setProject(project);
254: Tstamp.CustomFormat format = tstamp.createFormat();
255: format.setProperty("today");
256: format.setPattern("HH:mm:ss z");
257: format.setTimezone("GMT");
258: Date date = Calendar.getInstance().getTime();
259: format.execute(project, date, location);
260: String today = project.getProperty("today");
261:
262: // Get today's date
263: SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss z");
264: sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
265: String expected = sdf.format(date);
266:
267: // Set up a VSSHistory task
268: MSVSSHISTORY vssHistory = new MSVSSHISTORY();
269: vssHistory.setProject(project);
270: vssHistory.setLogin(VSS_USERNAME);
271: vssHistory.setToDate(today);
272: vssHistory.setVsspath(VSS_PROJECT_PATH);
273:
274: String[] sTestCmdLine = { MSVSS.SS_EXE, MSVSS.COMMAND_HISTORY,
275: DS_VSS_PROJECT_PATH, MSVSS.FLAG_AUTORESPONSE_DEF,
276: MSVSS.FLAG_VERSION_DATE + expected,
277: MSVSS.FLAG_LOGIN + VSS_USERNAME };
278:
279: commandline = vssHistory.buildCmdLine();
280:
281: checkCommandLines(sTestCmdLine, commandline.getCommandline());
282: }
283:
284: /**
285: * Tests VSSHistory required attributes.
286: */
287: public void testHistoryExceptions() {
288: configureProject("src/etc/testcases/taskdefs/optional/vss/vss.xml");
289: expectSpecificBuildException("vsshistory.1", "some cause",
290: "vsspath attribute must be set!");
291: }
292:
293: /** Tests CheckIn commandline generation. */
294: public void testCheckinCommandLine() {
295: String[] sTestCmdLine = { MSVSS.SS_EXE, MSVSS.COMMAND_CHECKIN,
296: DS_VSS_PROJECT_PATH, MSVSS.FLAG_AUTORESPONSE_NO,
297: MSVSS.FLAG_WRITABLE, MSVSS.FLAG_LOGIN + VSS_USERNAME,
298: MSVSS.FLAG_COMMENT + SRC_COMMENT };
299:
300: // Set up a VSSCheckIn task
301: MSVSSCHECKIN vssCheckin = new MSVSSCHECKIN();
302: vssCheckin.setProject(project);
303: vssCheckin.setComment(SRC_COMMENT);
304: vssCheckin.setLogin(VSS_USERNAME);
305: vssCheckin.setAutoresponse("N");
306: vssCheckin.setVsspath(VSS_PROJECT_PATH);
307: vssCheckin.setWritable(true);
308:
309: commandline = vssCheckin.buildCmdLine();
310:
311: checkCommandLines(sTestCmdLine, commandline.getCommandline());
312: }
313:
314: /**
315: * Test VSSCheckIn required attributes.
316: */
317: public void testCheckinExceptions() {
318: configureProject("src/etc/testcases/taskdefs/optional/vss/vss.xml");
319: expectSpecificBuildException("vsscheckin.1", "some cause",
320: "vsspath attribute must be set!");
321: }
322:
323: /** Tests CheckOut commandline generation. */
324: public void testCheckoutCommandLine() {
325: String[] sTestCmdLine = {
326: SS_DIR + File.separator + MSVSS.SS_EXE,
327: MSVSS.COMMAND_CHECKOUT, DS_VSS_PROJECT_PATH,
328: MSVSS.FLAG_AUTORESPONSE_DEF, MSVSS.FLAG_RECURSION,
329: MSVSS.FLAG_VERSION_DATE + DATE,
330: MSVSS.FLAG_LOGIN + VSS_USERNAME,
331: FLAG_FILETIME_MODIFIED, FLAG_NO_GET };
332:
333: // Set up a VSSCheckOut task
334: MSVSSCHECKOUT vssCheckout = new MSVSSCHECKOUT();
335: vssCheckout.setProject(project);
336: vssCheckout.setLogin(VSS_USERNAME);
337: vssCheckout.setVsspath(DS_VSS_PROJECT_PATH);
338: vssCheckout.setRecursive(true);
339: vssCheckout.setDate(DATE);
340: vssCheckout.setLabel(SRC_LABEL);
341: vssCheckout.setSsdir(SS_DIR);
342: MSVSS.CurrentModUpdated cmu = new MSVSS.CurrentModUpdated();
343: cmu.setValue(TIME_MODIFIED);
344: vssCheckout.setFileTimeStamp(cmu);
345: vssCheckout.setGetLocalCopy(false);
346:
347: commandline = vssCheckout.buildCmdLine();
348:
349: checkCommandLines(sTestCmdLine, commandline.getCommandline());
350: }
351:
352: /**
353: * Test VSSCheckout required attributes.
354: */
355: public void testCheckoutExceptions() {
356: configureProject("src/etc/testcases/taskdefs/optional/vss/vss.xml");
357: expectSpecificBuildException("vsscheckout.1", "some cause",
358: "vsspath attribute must be set!");
359: expectSpecificBuildException("vsscheckout.2", "some cause",
360: "blah is not a legal value for this attribute");
361: }
362:
363: /** Tests Add commandline generation. */
364: public void testAddCommandLine() {
365: String[] sTestCmdLine = {
366: SS_DIR + File.separator + MSVSS.SS_EXE,
367: MSVSS.COMMAND_ADD,
368: project.getBaseDir().getAbsolutePath() + File.separator
369: + LOCAL_PATH, MSVSS.FLAG_AUTORESPONSE_DEF,
370: MSVSS.FLAG_RECURSION,
371: MSVSS.FLAG_LOGIN + VSS_USERNAME + "," + VSS_PASSWORD,
372: MSVSS.FLAG_COMMENT + "-" };
373:
374: // Set up a VSSAdd task
375: MSVSSADD vssAdd = new MSVSSADD();
376: vssAdd.setProject(project);
377: vssAdd.setLogin(VSS_USERNAME + "," + VSS_PASSWORD);
378: vssAdd.setVsspath(DS_VSS_PROJECT_PATH);
379: vssAdd.setRecursive(true);
380: vssAdd.setSsdir(SS_DIR);
381: vssAdd.setWritable(false);
382: vssAdd.setLocalpath(new Path(project, LOCAL_PATH));
383:
384: commandline = vssAdd.buildCmdLine();
385:
386: checkCommandLines(sTestCmdLine, commandline.getCommandline());
387: }
388:
389: /**
390: * Test VSSAdd required attributes.
391: */
392: public void testAddExceptions() {
393: configureProject("src/etc/testcases/taskdefs/optional/vss/vss.xml");
394: expectSpecificBuildException("vssadd.1", "some cause",
395: "localPath attribute must be set!");
396: }
397:
398: /** Tests CP commandline generation. */
399: public void testCpCommandLine() {
400: String[] sTestCmdLine = { MSVSS.SS_EXE, MSVSS.COMMAND_CP,
401: DS_VSS_PROJECT_PATH, MSVSS.FLAG_AUTORESPONSE_DEF,
402: MSVSS.FLAG_LOGIN + VSS_USERNAME };
403:
404: // Set up a VSSCp task
405: MSVSSCP vssCp = new MSVSSCP();
406: vssCp.setProject(project);
407: vssCp.setLogin(VSS_USERNAME);
408: vssCp.setVsspath(DS_VSS_PROJECT_PATH);
409:
410: commandline = vssCp.buildCmdLine();
411:
412: checkCommandLines(sTestCmdLine, commandline.getCommandline());
413: }
414:
415: /**
416: * Test VSSCP required attributes.
417: */
418: public void testCpExceptions() {
419: configureProject("src/etc/testcases/taskdefs/optional/vss/vss.xml");
420: expectSpecificBuildException("vsscp.1", "some cause",
421: "vsspath attribute must be set!");
422: }
423:
424: /** Tests Create commandline generation. */
425: public void testCreateCommandLine() {
426: String[] sTestCmdLine = { MSVSS.SS_EXE, MSVSS.COMMAND_CREATE,
427: DS_VSS_PROJECT_PATH, MSVSS.FLAG_COMMENT + SRC_COMMENT,
428: MSVSS.FLAG_AUTORESPONSE_NO, MSVSS.FLAG_QUIET,
429: MSVSS.FLAG_LOGIN + VSS_USERNAME };
430:
431: // Set up a VSSCreate task
432: MSVSSCREATE vssCreate = new MSVSSCREATE();
433: vssCreate.setProject(project);
434: vssCreate.setComment(SRC_COMMENT);
435: vssCreate.setLogin(VSS_USERNAME);
436: vssCreate.setVsspath(DS_VSS_PROJECT_PATH);
437: vssCreate.setFailOnError(true);
438: vssCreate.setAutoresponse("N");
439: vssCreate.setQuiet(true);
440:
441: commandline = vssCreate.buildCmdLine();
442:
443: checkCommandLines(sTestCmdLine, commandline.getCommandline());
444: }
445:
446: /**
447: * Test VSSCreate required attributes.
448: */
449: public void testCreateExceptions() {
450: configureProject("src/etc/testcases/taskdefs/optional/vss/vss.xml");
451: expectSpecificBuildException("vsscreate.1", "some cause",
452: "vsspath attribute must be set!");
453: }
454:
455: /**
456: * Iterate through the generated command line comparing it to reference one.
457: * @param sTestCmdLine The reference command line;
458: * @param sGeneratedCmdLine The generated command line;
459: */
460: private void checkCommandLines(String[] sTestCmdLine,
461: String[] sGeneratedCmdLine) {
462: int testLength = sTestCmdLine.length;
463: int genLength = sGeneratedCmdLine.length;
464:
465: int genIndex = 0;
466: int testIndex = 0;
467:
468: while (testIndex < testLength) {
469: try {
470: if (sGeneratedCmdLine[genIndex] == "") {
471: genIndex++;
472: continue;
473: }
474: assertEquals("arg # " + testIndex,
475: sTestCmdLine[testIndex],
476: sGeneratedCmdLine[genIndex]);
477: testIndex++;
478: genIndex++;
479: } catch (ArrayIndexOutOfBoundsException aioob) {
480: fail("missing arg " + sTestCmdLine[testIndex]);
481: }
482: }
483:
484: // Count the number of empty strings
485: int cnt = 0;
486: for (int i = 0; i < genLength; i++) {
487: if (sGeneratedCmdLine[i] == "") {
488: cnt++;
489: }
490: }
491: if (genLength - cnt > sTestCmdLine.length) {
492: // We have extra elements
493: fail("extra args");
494: }
495: }
496: }
|