KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > sourcecontrols > VssTest


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 package net.sourceforge.cruisecontrol.sourcecontrols;
38
39 import java.io.File JavaDoc;
40 import java.text.ParseException JavaDoc;
41 import java.text.SimpleDateFormat JavaDoc;
42 import java.util.ArrayList JavaDoc;
43 import java.util.Date JavaDoc;
44 import java.util.List JavaDoc;
45 import java.util.Map JavaDoc;
46
47 import junit.framework.TestCase;
48 import net.sourceforge.cruisecontrol.CruiseControlException;
49 import net.sourceforge.cruisecontrol.Modification;
50
51 /**
52  * @author <a HREF="mailto:jcyip@thoughtworks.com">Jason Yip</a>
53  * @author Arun Aggarwal
54  */

55 public class VssTest extends TestCase {
56
57     private Vss vss;
58
59     private static final String JavaDoc DATE_TIME_STRING = "Date: 6/20/01 Time: 10:36a";
60     private static final String JavaDoc ALTERNATE_DATE_TIME_STRING = "Date: 20/6/01 Time: 10:36";
61
62     protected void setUp() {
63         vss = new Vss();
64     }
65
66     protected void tearDown() {
67         vss = null;
68     }
69
70     public void testValidate() {
71         try {
72             vss.validate();
73             fail("Vss should throw exceptions when required fields are not set.");
74         } catch (CruiseControlException e) {
75             assertTrue(true);
76         }
77
78         vss.setVsspath("vsspath");
79         vss.setLogin("login");
80
81         try {
82             vss.validate();
83             assertTrue(true);
84         } catch (CruiseControlException e) {
85             fail("Vss should not throw exceptions when required fields are set.");
86         }
87     }
88
89     public void testParseUserSingleCharName() {
90         String JavaDoc testName = "1";
91         assertEquals(testName, vss.parseUser(createVSSLine(testName, DATE_TIME_STRING)));
92     }
93
94     public void testParseDateSingleCharName() {
95         String JavaDoc testName = "1";
96         try {
97             assertEquals(
98              vss.getVssDateTimeFormat().parse(DATE_TIME_STRING.trim() + "m"),
99              vss.parseDate(createVSSLine(testName, DATE_TIME_STRING)));
100
101         } catch (ParseException JavaDoc e) {
102             fail("Could not parse date string: " + e.getMessage());
103         }
104     }
105
106     /**
107      * Parse a user supplied date format.
108      */

109     public void testParseDateAlternate() {
110         String JavaDoc testName = "1";
111         vss.setDateFormat("dd/MM/yy");
112         vss.setTimeFormat("HH:mm");
113         try {
114             assertEquals(
115               vss.getVssDateTimeFormat().parse(ALTERNATE_DATE_TIME_STRING.trim()),
116               vss.parseDate(createVSSLine(testName, ALTERNATE_DATE_TIME_STRING)));
117         } catch (ParseException JavaDoc e) {
118             fail("Could not parse date string: " + e.getMessage());
119         }
120     }
121
122     /**
123      * Some people are seeing strange date outputs from their VSS history that
124      * looks like this:
125      * User: Aaggarwa Date: 6/29/:1 Time: 3:40p
126      * Note the ":" rather than a "0"
127      */

128     public void testParseDateStrangeDate() {
129         String JavaDoc strangeDateLine = "User: Aaggarwa Date: 6/20/:1 Time: 10:36a";
130         try {
131             assertEquals(
132              vss.getVssDateTimeFormat().parse(DATE_TIME_STRING.trim() + "m"),
133              vss.parseDate(strangeDateLine));
134         } catch (ParseException JavaDoc e) {
135             fail("Could not parse strange date string: " + e.getMessage());
136         }
137     }
138
139     public void testParseUser10CharName() {
140         String JavaDoc testName = "1234567890";
141         assertEquals(testName, vss.parseUser(createVSSLine(testName, DATE_TIME_STRING)));
142     }
143
144     public void testParseUser20CharName() {
145         String JavaDoc testName = "12345678900987654321";
146         assertEquals(testName, vss.parseUser(createVSSLine(testName, DATE_TIME_STRING)));
147     }
148
149     public void testHandleEntryWithLabel() {
150
151         // need to adjust for cases where Label: line exists
152
// and there is also an action.
153

154         List JavaDoc entry = new ArrayList JavaDoc();
155         entry.add("***** DateChooser.java *****");
156         entry.add("Version 8");
157         entry.add("Label: \"Completely new version!\"");
158         entry.add("User: Arass Date: 10/21/02 Time: 12:48p");
159         entry.add("Checked in $/code/development/src/org/ets/cbtidg/common/gui");
160         entry.add("Comment: This is where I add a completely new, but alot nicer version of the date chooser.");
161
162         Modification modification = vss.handleEntry(entry);
163         assertEquals("DateChooser.java", modification.getFileName());
164
165         assertEquals("/code/development/src/org/ets/cbtidg/common/gui", modification.getFolderName());
166         assertEquals(
167             "Comment: This is where I add a completely new, but alot nicer version of the date chooser.",
168             modification.comment);
169         assertEquals("Arass", modification.userName);
170
171         Modification.ModifiedFile modfile = (Modification.ModifiedFile) modification.files.get(0);
172         assertEquals("checkin", modfile.action);
173     }
174
175     public void testHandleEntryCheckinWithComment() {
176         List JavaDoc entry = new ArrayList JavaDoc();
177         entry.add("***** ttyp_direct.properties *****");
178         entry.add("Version 10");
179         entry.add("User: Etucker Date: 7/03/01 Time: 3:24p");
180         entry.add("Checked in $/Eclipse/src/main/com/itxc/eclipse/some/path/here");
181         entry.add("Comment: updated country codes for Colombia and Slovokia");
182
183         Modification mod = vss.handleEntry(entry);
184         assertEquals(mod.getFileName(), "ttyp_direct.properties");
185         assertEquals(mod.getFolderName(), "/Eclipse/src/main/com/itxc/eclipse/some/path/here");
186         assertEquals(mod.comment, "Comment: updated country codes for Colombia and Slovokia");
187         assertEquals(mod.userName, "Etucker");
188
189         Modification.ModifiedFile modfile = (Modification.ModifiedFile) mod.files.get(0);
190         assertEquals(modfile.action, "checkin");
191     }
192
193     public void testHandleEntryShared() {
194         List JavaDoc entry = new ArrayList JavaDoc();
195         vss.setVsspath("\\vsspath");
196         entry.add("***** a *****");
197         entry.add("Version 19");
198         entry.add("User: Etucker Date: 7/03/01 Time: 11:16a");
199         entry.add("$/Users/jfredrick/test1/b/move.file.2 shared");
200
201         Modification mod = vss.handleEntry(entry);
202         assertEquals(mod.getFileName(), "$/Users/jfredrick/test1/b/move.file.2");
203         assertEquals(mod.userName, "Etucker");
204         assertEquals(mod.getFolderName(), "$\\vsspath\\a");
205
206         Modification.ModifiedFile modfile = (Modification.ModifiedFile) mod.files.get(0);
207         assertEquals(modfile.action, "share");
208     }
209
210     public void testHandleEntryDirBranched() {
211
212         List JavaDoc entry = new ArrayList JavaDoc();
213         vss.setVsspath("\\vsspath");
214         entry.add("***** core *****");
215         entry.add("Version 19");
216         entry.add("User: Etucker Date: 7/03/01 Time: 11:16a");
217         entry.add("SessionIdGenerator.java branched");
218
219         Modification mod = vss.handleEntry(entry);
220         assertEquals(mod.getFileName(), "SessionIdGenerator.java");
221         assertEquals(mod.userName, "Etucker");
222         assertEquals(mod.getFolderName(), "$\\vsspath\\core");
223         assertEquals(mod.type, "vss");
224
225         Modification.ModifiedFile modfile = (Modification.ModifiedFile) mod.files.get(0);
226         assertEquals(modfile.action, "branch");
227     }
228
229     public void testHandleEntryFileBranched() {
230
231          List JavaDoc entry = new ArrayList JavaDoc();
232          vss.setVsspath("\\vsspath");
233          entry.add("***** branch.file.1 *****");
234          entry.add("Version 19");
235          entry.add("User: Etucker Date: 7/03/01 Time: 11:16a");
236          entry.add("Branched");
237
238          Modification mod = vss.handleEntry(entry);
239          assertNull(mod);
240
241      }
242
243
244     public void testHandleEntryAdded() {
245         List JavaDoc entry = new ArrayList JavaDoc();
246         vss.setVsspath("\\vsspath");
247         entry.add("***** core *****");
248         entry.add("Version 19");
249         entry.add("User: Etucker Date: 7/03/01 Time: 11:16a");
250         entry.add("SessionIdGenerator.java added");
251
252         Modification mod = vss.handleEntry(entry);
253         assertEquals(mod.getFileName(), "SessionIdGenerator.java");
254         assertEquals(mod.userName, "Etucker");
255         assertEquals(mod.getFolderName(), "$\\vsspath\\core");
256
257         Modification.ModifiedFile modfile = (Modification.ModifiedFile) mod.files.get(0);
258         assertEquals(modfile.action, "add");
259     }
260
261     public void testHandleEntryAddedInRoot() {
262 /* ***************** Version 9 *****************
263         User: Jfredrick Date: 7/09/02 Time: 12:55p
264         branch.file.2 added
265 */

266
267            List JavaDoc entry = new ArrayList JavaDoc();
268            vss.setVsspath("\\vsspath");
269            entry.add("***************** Version 19 *****************");
270            entry.add("User: MStave Date: 7/03/01 Time: 11:16a");
271            entry.add("SessionIdGenerator.java added");
272
273            Modification mod = vss.handleEntry(entry);
274            assertEquals(mod.getFileName(), "SessionIdGenerator.java");
275            assertEquals(mod.userName, "MStave");
276            assertEquals(mod.getFolderName(), "$\\vsspath");
277
278            Modification.ModifiedFile modfile = (Modification.ModifiedFile) mod.files.get(0);
279            assertEquals(modfile.action, "add");
280        }
281
282
283         public void testHandleEntryRename() {
284             vss.setPropertyOnDelete("setThis");
285             List JavaDoc entry = new ArrayList JavaDoc();
286             entry.add("***** core *****");
287             entry.add("Version 19");
288             entry.add("User: Etucker Date: 7/03/01 Time: 11:16a");
289             entry.add("SessionIdGenerator.java renamed to SessionId.java");
290
291             Modification modification = vss.handleEntry(entry);
292             assertEquals(
293                 "SessionIdGenerator.java renamed to SessionId.java",
294                 modification.getFileName());
295             assertEquals("Etucker", modification.userName);
296
297             Modification.ModifiedFile modfile = (Modification.ModifiedFile) modification.files.get(0);
298             assertEquals("rename", modfile.action);
299
300             Map JavaDoc properties = vss.getProperties();
301             String JavaDoc setThisValue = (String JavaDoc) properties.get("setThis");
302             assertEquals("true", setThisValue);
303         }
304
305     public void testHandleEntryDestroyed() {
306         vss.setPropertyOnDelete("setThis");
307         List JavaDoc entry = new ArrayList JavaDoc();
308         entry.add("***** installer_vms *****");
309         entry.add("Version 42");
310         entry.add("User: Sfrolich Date: 11/19/02 Time: 1:40p");
311         entry.add("IBMJDK130AIX_with_xerces.jar.vm destroyed");
312
313         Modification modification = vss.handleEntry(entry);
314         assertEquals("IBMJDK130AIX_with_xerces.jar.vm", modification.getFileName());
315         assertEquals("Sfrolich", modification.userName);
316
317         Modification.ModifiedFile modfile = (Modification.ModifiedFile) modification.files.get(0);
318         assertEquals("destroy", modfile.action);
319
320         Map JavaDoc properties = vss.getProperties();
321         String JavaDoc setThisValue = (String JavaDoc) properties.get("setThis");
322         assertEquals("true", setThisValue);
323     }
324
325     public void testHandleEntryLabel() {
326         List JavaDoc entry = new ArrayList JavaDoc();
327         entry.add("**********************");
328         entry.add("Label: \"KonaBuild_452\"");
329         entry.add("User: Cm Date: 4/29/03 Time: 12:03a");
330         entry.add("Labeled");
331         entry.add("Label comment: AutoBuild KonaBuild_452");
332
333         Modification modification = vss.handleEntry(entry);
334         assertNull(modification);
335         
336         // Labeled root directory
337
entry = new ArrayList JavaDoc();
338         entry.add("***************** Version 222 *****************");
339         entry.add("Label: \"build.83\"");
340         entry.add("User: Fabricator Date: 7/16/03 Time: 10:29a");
341         entry.add("Labeled");
342         entry.add(" ");
343
344         modification = vss.handleEntry(entry);
345         assertNull(modification);
346
347         // Labled subdirectory
348
entry = new ArrayList JavaDoc();
349         entry.add("***** built *****");
350         entry.add("Version 4");
351         entry.add("Label: \"autobuild_test\"");
352         entry.add("User: Etucker Date: 6/26/01 Time: 11:53a");
353         entry.add("Labeled");
354
355         modification = vss.handleEntry(entry);
356         assertNull(modification);
357     }
358
359     public void testGetCommandLine() throws Exception JavaDoc {
360         vss.setVsspath("vsspath");
361         vss.setLogin("login,password");
362
363         Date JavaDoc lastBuild = createDate("2002/08/03 09:30:00"); //Set date to Aug 3, 2002 9:30am
364
Date JavaDoc now = createDate("2002/08/04 13:15:00"); //Set date to Aug 4, 2002 1:15pm
365
String JavaDoc[] expectedCommand =
366             {
367                 "ss.exe",
368                 "history",
369                 "$vsspath",
370                 "-R",
371                 "-Vd08/04/02;01:15P~08/03/02;09:30A",
372                 "-Ylogin,password",
373                 "-I-N",
374                 "-Ovsspath.tmp" };
375         String JavaDoc[] actualCommand = vss.getCommandLine(lastBuild, now);
376         for (int i = 0; i < expectedCommand.length; i++) {
377             assertEquals(expectedCommand[i], actualCommand[i]);
378         }
379
380         vss.setDateFormat("dd/MM/yy");
381         String JavaDoc[] expectedCommandWithDate =
382             {
383                 "ss.exe",
384                 "history",
385                 "$vsspath",
386                 "-R",
387                 "-Vd04/08/02;01:15P~03/08/02;09:30A",
388                 "-Ylogin,password",
389                 "-I-N",
390                 "-Ovsspath.tmp" };
391         String JavaDoc[] actualCommandWithDate = vss.getCommandLine(lastBuild, now);
392         for (int i = 0; i < expectedCommandWithDate.length; i++) {
393             assertEquals(expectedCommandWithDate[i], actualCommandWithDate[i]);
394         }
395
396         File JavaDoc execFile = new File JavaDoc("..", "ss.exe");
397         vss.setSsDir("..");
398         System.out.println(execFile.getCanonicalPath());
399         String JavaDoc[] expectedCommandWithSsdir =
400             {
401                 execFile.getCanonicalPath(),
402                 "history",
403                 "$vsspath",
404                 "-R",
405                 "-Vd04/08/02;01:15P~03/08/02;09:30A",
406                 "-Ylogin,password",
407                 "-I-N",
408                 "-Ovsspath.tmp" };
409         String JavaDoc[] actualCommandWithSsdir = vss.getCommandLine(lastBuild, now);
410         for (int i = 0; i < expectedCommandWithSsdir.length; i++) {
411             assertEquals(expectedCommandWithSsdir[i], actualCommandWithSsdir[i]);
412         }
413
414         vss.setTimeFormat("HH:mm");
415         String JavaDoc[] expectedCommandWithTimeFormat =
416             {
417                 execFile.getCanonicalPath(),
418                 "history",
419                 "$vsspath",
420                 "-R",
421                 "-Vd04/08/02;13:15~03/08/02;09:30",
422                 "-Ylogin,password",
423                 "-I-N",
424                 "-Ovsspath.tmp" };
425         String JavaDoc[] actualCommandWithTimeFormat = vss.getCommandLine(lastBuild, now);
426
427         for (int i = 0; i < expectedCommandWithTimeFormat.length; i++) {
428             assertEquals(expectedCommandWithTimeFormat[i], actualCommandWithTimeFormat[i]);
429         }
430     }
431     
432     public void testCreateFileNameFromVssPath() {
433         vss.setVsspath("//foo/bar");
434         assertEquals("foo_bar.tmp", vss.createFileNameFromVssPath());
435     }
436
437     /**
438      * Utility method to easily create a given date.
439      */

440     private Date JavaDoc createDate(String JavaDoc dateString) throws ParseException JavaDoc {
441         SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc("yyyy/MM/dd HH:mm:ss");
442         return formatter.parse(dateString);
443     }
444
445     /**
446      * Produces a VSS line that looks something like this:
447      * User: Username Date: 6/14/01 Time: 6:39p
448      *
449      * @param testName Replaces the Username in above example
450      */

451     private String JavaDoc createVSSLine(String JavaDoc testName, String JavaDoc dateTimeString) {
452         return "User: " + testName + " " + dateTimeString;
453     }
454
455     public static void main(String JavaDoc[] args) {
456         junit.textui.TestRunner.run(VssTest.class);
457     }
458
459 }
460
Popular Tags