KickJava   Java API By Example, From Geeks To Geeks.

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


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 junit.framework.TestCase;
40 import net.sourceforge.cruisecontrol.CruiseControlException;
41 import net.sourceforge.cruisecontrol.Modification;
42 import net.sourceforge.cruisecontrol.util.Commandline;
43 import net.sourceforge.cruisecontrol.util.DateUtil;
44 import net.sourceforge.cruisecontrol.util.MockCommandline;
45 import net.sourceforge.cruisecontrol.util.OSEnvironment;
46
47 import java.io.BufferedInputStream JavaDoc;
48 import java.io.ByteArrayInputStream JavaDoc;
49 import java.io.IOException JavaDoc;
50 import java.io.InputStream JavaDoc;
51 import java.io.PipedInputStream JavaDoc;
52 import java.io.PipedOutputStream JavaDoc;
53 import java.text.ParseException JavaDoc;
54 import java.text.SimpleDateFormat JavaDoc;
55 import java.util.Collections JavaDoc;
56 import java.util.Date JavaDoc;
57 import java.util.GregorianCalendar JavaDoc;
58 import java.util.HashMap JavaDoc;
59 import java.util.Hashtable JavaDoc;
60 import java.util.List JavaDoc;
61 import java.util.Map JavaDoc;
62 import java.util.TimeZone JavaDoc;
63
64
65 /**
66  * @author Robert Watkins
67  * @author <a HREF="mailto:jcyip@thoughtworks.com">Jason Yip</a>
68  */

69 public class ConcurrentVersionsSystemTest extends TestCase {
70     private TimeZone JavaDoc originalTimeZone;
71     static final String JavaDoc[] CVS_VERSION_COMMANDLINE = new String JavaDoc[]{"cvs", "version"};
72
73     protected void setUp() throws Exception JavaDoc {
74         originalTimeZone = TimeZone.getDefault();
75     }
76
77     protected void tearDown() throws Exception JavaDoc {
78         TimeZone.setDefault(originalTimeZone);
79         originalTimeZone = null;
80     }
81
82     private Date JavaDoc parseCVSDateFormat(String JavaDoc dateString) throws ParseException JavaDoc {
83         return DateUtil.parseCVSDate(dateString);
84     }
85
86     private Date JavaDoc parseLogDateFormat(String JavaDoc dateString) throws ParseException JavaDoc {
87         return new SimpleDateFormat JavaDoc(ConcurrentVersionsSystem.LOG_DATE_FORMAT).parse(dateString);
88     }
89
90     /**
91      * Mocks a CVS class by returning a specific CVS version
92      */

93     static class MockVersionCVS extends ConcurrentVersionsSystem {
94         private final Version vers;
95
96         public MockVersionCVS(Version cvsVersion) {
97             this.vers = cvsVersion;
98         }
99
100         protected Version getCvsServerVersion() {
101             return vers;
102         }
103     }
104
105     private ConcurrentVersionsSystem.Version getOfficialCVSVersion(final String JavaDoc cvsVersion) {
106         return new ConcurrentVersionsSystem.Version(ConcurrentVersionsSystem.OFFICIAL_CVS_NAME, cvsVersion);
107     }
108
109     /**
110      * Overrides the getCommandLine() method by returnning a MockCommandLine whose
111      * process input stream will read its contents from the specific input stream.
112      */

113     static class InputBasedCommandLineMockCVS extends ConcurrentVersionsSystem {
114         private final InputStream JavaDoc inputStream;
115         private final String JavaDoc[] expectedCommandline;
116         private final String JavaDoc expectedWorkingDirectory;
117
118         public InputBasedCommandLineMockCVS(final InputStream JavaDoc inputStream,
119                                             final String JavaDoc[] expectedCommandLine,
120                                             final String JavaDoc expectedWorkingDirectory) {
121             this.inputStream = inputStream;
122             expectedCommandline = expectedCommandLine;
123             this.expectedWorkingDirectory = expectedWorkingDirectory;
124         }
125
126         // factory method for mock...
127
protected Commandline getCommandline() {
128             final MockCommandline mockCommandline = new MockCommandline();
129             mockCommandline.setExpectedCommandline(expectedCommandline);
130             mockCommandline.setExpectedWorkingDirectory(expectedWorkingDirectory);
131             mockCommandline.setProcessErrorStream(new PipedInputStream JavaDoc());
132             mockCommandline.setProcessInputStream(inputStream);
133             mockCommandline.setProcessOutputStream(new PipedOutputStream JavaDoc());
134             return mockCommandline;
135         }
136     }
137
138     static class MockOSEnvironment extends OSEnvironment {
139         private Map JavaDoc myVariables = new HashMap JavaDoc();
140
141         public void add(String JavaDoc variable, String JavaDoc value) {
142             myVariables.put(variable, value);
143         }
144
145         public String JavaDoc getVariable(String JavaDoc variable) {
146             return (String JavaDoc) myVariables.get(variable);
147         }
148     }
149
150
151     public void testValidate() {
152         final MockOSEnvironment env = new MockOSEnvironment();
153         ConcurrentVersionsSystem cvs = new ConcurrentVersionsSystem() {
154             protected OSEnvironment getOSEnvironment() {
155                 return env;
156             }
157         };
158
159         env.add("CVSROOT", null);
160
161         try {
162             cvs.validate();
163             fail("CVS should throw exceptions when required fields are not set.");
164         } catch (CruiseControlException e) {
165         }
166
167         env.add("CVSROOT", "something");
168
169         try {
170             cvs.validate();
171             fail("CVS should throw exceptions when required fields are not set.");
172         } catch (CruiseControlException e) {
173         }
174
175         env.add("CVSROOT", null);
176
177         cvs.setCvsRoot("cvsroot");
178
179         try {
180             cvs.validate();
181             fail("CVS should throw exceptions when required fields are not set.");
182         } catch (CruiseControlException e) {
183         }
184
185         cvs.setModule("module");
186
187         try {
188             cvs.validate();
189         } catch (CruiseControlException e) {
190             fail("CVS should not throw exceptions when required fields are set: " + e.getMessage());
191         }
192
193         cvs.setCvsRoot(null);
194
195         try {
196             cvs.validate();
197             fail("CVS should throw exceptions when required fields are not set.");
198         } catch (CruiseControlException e) {
199         }
200
201         cvs = new ConcurrentVersionsSystem();
202         cvs.setLocalWorkingCopy(System.getProperty("java.io.tmpdir"));
203
204         try {
205             cvs.validate();
206         } catch (CruiseControlException e) {
207             fail("CVS should not throw exceptions when required fields are set: " + e.getMessage());
208         }
209
210         cvs.setModule("module");
211
212         try {
213             cvs.validate();
214             fail("CVS should not throw exceptions when excluding fields are set.");
215         } catch (CruiseControlException e) {
216         }
217
218         cvs.setModule(null);
219
220         String JavaDoc badDirName = "z:/foo/foo/foo/bar";
221         cvs.setLocalWorkingCopy(badDirName);
222         try {
223             cvs.validate();
224             fail("CVS.validate should throw exception on non-existant directory.");
225         } catch (CruiseControlException e) {
226         }
227     }
228
229     private InputStream JavaDoc loadTestLog(String JavaDoc name) {
230         InputStream JavaDoc testStream = getClass().getResourceAsStream(name);
231         assertNotNull("failed to load resource " + name + " in class " + getClass().getName(), testStream);
232         return testStream;
233     }
234
235     public void testParseStream() throws IOException JavaDoc, ParseException JavaDoc {
236         // ensure CVS version and simulated outputs are in sync
237
final String JavaDoc cvsVersion = "1.11.16";
238         ConcurrentVersionsSystem cvs = new MockVersionCVS(getOfficialCVSVersion(cvsVersion));
239         Hashtable JavaDoc emailAliases = new Hashtable JavaDoc();
240         emailAliases.put("alden", "alden@users.sourceforge.net");
241         emailAliases.put("tim", "tim@tim.net");
242         cvs.setMailAliases(emailAliases);
243
244         BufferedInputStream JavaDoc input =
245                 new BufferedInputStream JavaDoc(loadTestLog("cvslog1-11.txt"));
246         List JavaDoc modifications = cvs.parseStream(input);
247         input.close();
248         Collections.sort(modifications);
249
250         assertEquals("Should have returned 5 modifications.",
251                 5,
252                 modifications.size());
253
254         Modification mod1 = new Modification("cvs");
255         Modification.ModifiedFile mod1file = mod1.createModifiedFile("log4j.properties", null);
256         mod1file.action = "modified";
257         mod1.revision = "1.2";
258         mod1.modifiedTime = parseLogDateFormat("2002/03/13 13:45:50 GMT-6:00");
259         mod1.userName = "alden";
260         mod1.comment =
261                 "Shortening ConversionPattern so we don't use up all of the available screen space.";
262         mod1.emailAddress = "alden@users.sourceforge.net";
263
264         Modification mod2 = new Modification("cvs");
265         Modification.ModifiedFile mod2file = mod2.createModifiedFile("build.xml", null);
266         mod2file.action = "modified";
267         mod2.revision = "1.41";
268         mod2.modifiedTime = parseLogDateFormat("2002/03/13 19:56:34 GMT-6:00");
269         mod2.userName = "alden";
270         mod2.comment = "Added target to clean up test results.";
271         mod2.emailAddress = "alden@users.sourceforge.net";
272
273         Modification mod3 = new Modification("cvs");
274         Modification.ModifiedFile mod3file = mod3.createModifiedFile("build.xml", "main");
275         mod3file.action = "modified";
276         mod3.revision = "1.42";
277         mod3.modifiedTime = parseLogDateFormat("2002/03/15 13:20:28 GMT-6:00");
278         mod3.userName = "alden";
279         mod3.comment = "enabled debug info when compiling tests.";
280         mod3.emailAddress = "alden@users.sourceforge.net";
281
282         Modification mod4 = new Modification("cvs");
283         Modification.ModifiedFile mod4file = mod4.createModifiedFile("kungfu.xml", "main");
284         mod4file.action = "deleted";
285         mod4.revision = "1.2";
286         mod4.modifiedTime = parseLogDateFormat("2002/03/13 13:45:42 GMT-6:00");
287         mod4.userName = "alden";
288         mod4.comment = "Hey, look, a deleted file.";
289         mod4.emailAddress = "alden@users.sourceforge.net";
290
291         Modification mod5 = new Modification("cvs");
292         Modification.ModifiedFile mod5file = mod5.createModifiedFile("stuff.xml", "main");
293         mod5file.action = "deleted";
294         mod5.revision = "1.4";
295         mod5.modifiedTime = parseLogDateFormat("2002/03/13 13:38:42 GMT-6:00");
296         mod5.userName = "alden";
297         mod5.comment = "Hey, look, another deleted file.";
298         mod5.emailAddress = "alden@users.sourceforge.net";
299
300         assertEquals(mod5, modifications.get(0));
301         assertEquals(mod4, modifications.get(1));
302         assertEquals(mod1, modifications.get(2));
303         assertEquals(mod2, modifications.get(3));
304         assertEquals(mod3, modifications.get(4));
305     }
306
307     public void testParseStreamRemote() throws IOException JavaDoc, ParseException JavaDoc {
308         // ensure CVS version and simulated outputs are in sync
309
final String JavaDoc cvsVersion = "1.11.16";
310         ConcurrentVersionsSystem cvs = new MockVersionCVS(getOfficialCVSVersion(cvsVersion));
311         cvs.setModule("cruisecontrol");
312         cvs.setCvsRoot(":pserver:anonymous@cvs.sourceforge.net:/cvsroot/cruisecontrol");
313         Hashtable JavaDoc emailAliases = new Hashtable JavaDoc();
314         emailAliases.put("alden", "alden@users.sourceforge.net");
315         emailAliases.put("tim", "tim@tim.net");
316         cvs.setMailAliases(emailAliases);
317
318         BufferedInputStream JavaDoc input =
319                 new BufferedInputStream JavaDoc(loadTestLog("cvslog1-11-remote.txt"));
320         List JavaDoc modifications = cvs.parseStream(input);
321         input.close();
322         Collections.sort(modifications);
323
324         assertEquals("Should have returned 5 modifications.",
325                 5,
326                 modifications.size());
327
328         Modification mod1 = new Modification("cvs");
329         Modification.ModifiedFile mod1file = mod1.createModifiedFile("log4j.properties", null);
330         mod1file.action = "modified";
331         mod1.revision = "1.2";
332         mod1.modifiedTime = parseLogDateFormat("2002/03/13 13:45:50 GMT-6:00");
333         mod1.userName = "alden";
334         mod1.comment =
335                 "Shortening ConversionPattern so we don't use up all of the available screen space.";
336         mod1.emailAddress = "alden@users.sourceforge.net";
337
338         Modification mod2 = new Modification("cvs");
339         Modification.ModifiedFile mod2file = mod2.createModifiedFile("build.xml", null);
340         mod2file.action = "modified";
341         mod2.revision = "1.41";
342         mod2.modifiedTime = parseLogDateFormat("2002/03/13 19:56:34 GMT-6:00");
343         mod2.userName = "alden";
344         mod2.comment = "Added target to clean up test results.";
345         mod2.emailAddress = "alden@users.sourceforge.net";
346
347         Modification mod3 = new Modification("cvs");
348         Modification.ModifiedFile mod3file = mod3.createModifiedFile("build.xml", "main");
349         mod3file.action = "modified";
350         mod3.revision = "1.42";
351         mod3.modifiedTime = parseLogDateFormat("2002/03/15 13:20:28 GMT-6:00");
352         mod3.userName = "alden";
353         mod3.comment = "enabled debug info when compiling tests.";
354         mod3.emailAddress = "alden@users.sourceforge.net";
355
356         Modification mod4 = new Modification("cvs");
357         Modification.ModifiedFile mod4file = mod4.createModifiedFile("kungfu.xml", "main");
358         mod4file.action = "deleted";
359         mod4.revision = "1.2";
360         mod4.modifiedTime = parseLogDateFormat("2002/03/13 13:45:42 GMT-6:00");
361         mod4.userName = "alden";
362         mod4.comment = "Hey, look, a deleted file.";
363         mod4.emailAddress = "alden@users.sourceforge.net";
364
365         Modification mod5 = new Modification("cvs");
366         Modification.ModifiedFile mod5file = mod5.createModifiedFile("stuff.xml", "main");
367         mod5file.action = "deleted";
368         mod5.revision = "1.4";
369         mod5.modifiedTime = parseLogDateFormat("2002/03/13 13:38:42 GMT-6:00");
370         mod5.userName = "alden";
371         mod5.comment = "Hey, look, another deleted file.";
372         mod5.emailAddress = "alden@users.sourceforge.net";
373
374         assertEquals(mod5, modifications.get(0));
375         assertEquals(mod4, modifications.get(1));
376         assertEquals(mod1, modifications.get(2));
377         assertEquals(mod2, modifications.get(3));
378         assertEquals(mod3, modifications.get(4));
379     }
380
381     public void testParseStreamNewFormat() throws IOException JavaDoc, ParseException JavaDoc {
382         ConcurrentVersionsSystem cvs = new MockVersionCVS(getOfficialCVSVersion("1.12.9"));
383         Hashtable JavaDoc emailAliases = new Hashtable JavaDoc();
384         emailAliases.put("jerome", "jerome@coffeebreaks.org");
385         cvs.setMailAliases(emailAliases);
386
387         BufferedInputStream JavaDoc input =
388                 new BufferedInputStream JavaDoc(loadTestLog("cvslog1-12.txt"));
389         List JavaDoc modifications = cvs.parseStream(input);
390         input.close();
391         Collections.sort(modifications);
392
393         assertEquals("Should have returned 1 modification.",
394                 1,
395                 modifications.size());
396
397         Modification mod1 = new Modification("cvs");
398         Modification.ModifiedFile mod1file = mod1.createModifiedFile("log4j.properties", null);
399         mod1file.action = "modified";
400         mod1.revision = "1.1";
401         mod1.modifiedTime = parseCVSDateFormat("2004-03-25 00:58:49 GMT");
402         mod1.userName = "jerome";
403         mod1.comment =
404                 "initial checkin";
405         mod1.emailAddress = "jerome@coffeebreaks.org";
406
407         assertEquals(mod1, modifications.get(0));
408     }
409
410     public void testParseStreamBranch() throws IOException JavaDoc, ParseException JavaDoc {
411         // ensure CVS version and simulated outputs are in sync
412
ConcurrentVersionsSystem cvs = new MockVersionCVS(getOfficialCVSVersion("1.11.16"));
413         Hashtable JavaDoc emailAliases = new Hashtable JavaDoc();
414         emailAliases.put("alden", "alden@users.sourceforge.net");
415         cvs.setMailAliases(emailAliases);
416
417         cvs.setTag("BRANCH_TEST_BUILD");
418         BufferedInputStream JavaDoc input =
419                 new BufferedInputStream JavaDoc(loadTestLog("cvslog1-11branch.txt"));
420         List JavaDoc modifications = cvs.parseStream(input);
421         input.close();
422         Collections.sort(modifications);
423
424         assertEquals("Should have returned 4 modifications.",
425                 4,
426                 modifications.size());
427
428         Modification mod1 = new Modification("cvs");
429         mod1.revision = "1.1.2.4";
430         mod1.modifiedTime = parseLogDateFormat("2002/10/03 16:05:23 GMT");
431         mod1.userName = "tim";
432         mod1.comment = "Test commit once more";
433         Modification.ModifiedFile mod1file = mod1.createModifiedFile("test.version", null);
434         mod1file.action = "modified";
435         mod1file.revision = mod1.revision;
436
437         Modification mod2 = new Modification("cvs");
438         mod2.revision = "1.1.2.3";
439         mod2.modifiedTime = parseLogDateFormat("2002/10/03 14:24:17 GMT");
440         mod2.userName = "tim";
441         mod2.comment = "Test commit";
442         Modification.ModifiedFile mod2file = mod2.createModifiedFile("test.version", null);
443         mod2file.action = "modified";
444         mod2file.revision = mod2.revision;
445
446         Modification mod3 = new Modification("cvs");
447         mod3.revision = "1.1.2.2";
448         mod3.modifiedTime = parseLogDateFormat("2002/10/02 21:54:44 GMT");
449         mod3.userName = "tim";
450         mod3.comment = "Update parameters for test";
451         Modification.ModifiedFile mod3file = mod3.createModifiedFile("test.version", null);
452         mod3file.action = "modified";
453         mod3file.revision = mod3.revision;
454
455         Modification mod4 = new Modification("cvs");
456         mod4.revision = "1.1.2.1";
457         mod4.modifiedTime = parseLogDateFormat("2002/10/02 21:49:31 GMT");
458         mod4.userName = "tim";
459         mod4.comment = "Add parameters for test";
460         Modification.ModifiedFile mod4file = mod4.createModifiedFile("test.version", null);
461         mod4file.action = "modified";
462         mod4file.revision = mod4.revision;
463
464
465         assertEquals(mod4, modifications.get(0));
466         assertEquals(mod3, modifications.get(1));
467         assertEquals(mod2, modifications.get(2));
468         assertEquals(mod1, modifications.get(3));
469     }
470
471     public void testParseStreamTagNoBranch() throws IOException JavaDoc, ParseException JavaDoc {
472         // ensure CVS version and simulated outputs are in sync
473
ConcurrentVersionsSystem cvs = new MockVersionCVS(getOfficialCVSVersion("1.12.9"));
474         Hashtable JavaDoc emailAliases = new Hashtable JavaDoc();
475         cvs.setMailAliases(emailAliases);
476
477         cvs.setTag("TEST");
478         BufferedInputStream JavaDoc input =
479                 new BufferedInputStream JavaDoc(loadTestLog("cvslog1-12tagnobranch.txt"));
480         List JavaDoc modifications = cvs.parseStream(input);
481         input.close();
482         Collections.sort(modifications);
483
484         assertEquals("Should have returned 1 modification.",
485                 1,
486                 modifications.size());
487
488         Modification mod1 = new Modification("cvs");
489         mod1.revision = "1.49";
490         mod1.modifiedTime = parseLogDateFormat("2005/08/22 17:28:13 GMT");
491         mod1.userName = "jerome";
492         mod1.comment = "Test commit";
493         Modification.ModifiedFile mod1file = mod1.createModifiedFile("test.version", null);
494         mod1file.action = "modified";
495         mod1file.revision = mod1.revision;
496
497         assertEquals(mod1, modifications.get(0));
498     }
499
500     public void testGetProperties() throws IOException JavaDoc {
501         // ensure CVS version and simulated outputs are in sync
502
ConcurrentVersionsSystem cvs = new MockVersionCVS(getOfficialCVSVersion("1.11.16"));
503         cvs.setMailAliases(new Hashtable JavaDoc());
504         cvs.setProperty("property");
505         cvs.setPropertyOnDelete("propertyOnDelete");
506
507         String JavaDoc logName = "cvslog1-11.txt";
508         BufferedInputStream JavaDoc input =
509                 new BufferedInputStream JavaDoc(loadTestLog(logName));
510         cvs.parseStream(input);
511         input.close();
512
513         Map JavaDoc table = cvs.getProperties();
514         assertNotNull("Table of properties shouldn't be null.", table);
515
516         assertEquals("Should be two properties.", 2, table.size());
517
518         assertTrue("Property was not set.", table.containsKey("property"));
519         assertTrue("PropertyOnDelete was not set.",
520                 table.containsKey("propertyOnDelete"));
521
522         //negative test
523
// ensure CVS version and simulated outputs are in sync
524
ConcurrentVersionsSystem cvs2 = new MockVersionCVS(getOfficialCVSVersion("1.11.16"));
525         cvs2.setMailAliases(new Hashtable JavaDoc());
526         input = new BufferedInputStream JavaDoc(loadTestLog(logName));
527         cvs2.parseStream(input);
528         input.close();
529
530         table = cvs2.getProperties();
531         assertNotNull("Table of properties shouldn't be null.", table);
532
533         assertEquals("Shouldn't be any properties.", 0, table.size());
534     }
535
536     public void testGetPropertiesNoModifications() throws IOException JavaDoc {
537         ConcurrentVersionsSystem cvs = new MockVersionCVS(getOfficialCVSVersion("1.11.16"));
538         cvs.setMailAliases(new Hashtable JavaDoc());
539         cvs.setProperty("property");
540         cvs.setPropertyOnDelete("propertyOnDelete");
541         String JavaDoc logName = "cvslog1-11noMods.txt";
542         BufferedInputStream JavaDoc input =
543                 new BufferedInputStream JavaDoc(loadTestLog(logName));
544         cvs.parseStream(input);
545         input.close();
546
547         Map JavaDoc table = cvs.getProperties();
548         assertNotNull("Table of properties shouldn't be null.", table);
549
550         assertEquals("Shouldn't be any properties.", 0, table.size());
551     }
552
553     public void testGetPropertiesOnlyModifications() throws IOException JavaDoc {
554         // ensure CVS version and simulated outputs are in sync
555
ConcurrentVersionsSystem cvs = new MockVersionCVS(getOfficialCVSVersion("1.11.16"));
556         cvs.setMailAliases(new Hashtable JavaDoc());
557         cvs.setProperty("property");
558         cvs.setPropertyOnDelete("propertyOnDelete");
559         String JavaDoc logName = "cvslog1-11mods.txt";
560         BufferedInputStream JavaDoc input =
561                 new BufferedInputStream JavaDoc(loadTestLog(logName));
562         cvs.parseStream(input);
563         input.close();
564
565         Map JavaDoc table = cvs.getProperties();
566         assertNotNull("Table of properties shouldn't be null.", table);
567
568         assertEquals("Should be one property.", 1, table.size());
569         assertTrue("Property was not set.", table.containsKey("property"));
570
571         //negative test
572
// ensure CVS version and simulated outputs are in sync
573
ConcurrentVersionsSystem cvs2 = new MockVersionCVS(getOfficialCVSVersion("1.11.16"));
574         cvs2.setMailAliases(new Hashtable JavaDoc());
575         cvs2.setPropertyOnDelete("propertyOnDelete");
576         input = new BufferedInputStream JavaDoc(loadTestLog(logName));
577         cvs2.parseStream(input);
578         input.close();
579
580         table = cvs2.getProperties();
581         assertNotNull("Table of properties shouldn't be null.", table);
582
583         assertEquals("Shouldn't be any properties.", 0, table.size());
584     }
585
586     public void testGetPropertiesOnlyDeletions() throws IOException JavaDoc {
587         // ensure CVS version and simulated outputs are in sync
588
ConcurrentVersionsSystem cvs = new MockVersionCVS(getOfficialCVSVersion("1.11.16"));
589         cvs.setMailAliases(new Hashtable JavaDoc());
590         cvs.setPropertyOnDelete("propertyOnDelete");
591         String JavaDoc logName = "cvslog1-11del.txt";
592         BufferedInputStream JavaDoc input =
593                 new BufferedInputStream JavaDoc(loadTestLog(logName));
594         cvs.parseStream(input);
595         input.close();
596
597         Map JavaDoc table = cvs.getProperties();
598         assertNotNull("Table of properties shouldn't be null.", table);
599
600         assertEquals("Should be one property.", 1, table.size());
601         assertTrue("PropertyOnDelete was not set.",
602                 table.containsKey("propertyOnDelete"));
603
604         //negative test
605
// ensure CVS version and simulated outputs are in sync
606
ConcurrentVersionsSystem cvs2 = new MockVersionCVS(getOfficialCVSVersion("1.11.16"));
607         cvs2.setMailAliases(new Hashtable JavaDoc());
608         input = new BufferedInputStream JavaDoc(loadTestLog(logName));
609         cvs2.parseStream(input);
610         input.close();
611
612         table = cvs2.getProperties();
613         assertNotNull("Table of properties shouldn't be null.", table);
614
615         assertEquals("Shouldn't be any properties.", 0, table.size());
616     }
617
618     public void testBuildHistoryCommand() throws CruiseControlException {
619         Date JavaDoc checkTime = new Date JavaDoc();
620         long tenMinutes = 10 * 60 * 1000;
621         Date JavaDoc lastBuildTime = new Date JavaDoc(checkTime.getTime() - tenMinutes);
622
623         ConcurrentVersionsSystem element = new ConcurrentVersionsSystem();
624         element.setCvsRoot("cvsroot");
625         element.setLocalWorkingCopy(".");
626
627         String JavaDoc[] expectedCommand =
628                 new String JavaDoc[]{
629                         "cvs",
630                         "-d",
631                         "cvsroot",
632                         "-q",
633                         "log",
634                         "-N",
635                         "-d" + ConcurrentVersionsSystem.formatCVSDate(lastBuildTime) + "<"
636                                 + ConcurrentVersionsSystem.formatCVSDate(checkTime),
637                         "-b"};
638
639         String JavaDoc[] noTagCommand = element.buildHistoryCommand(lastBuildTime, checkTime).getCommandline();
640         assertCommandsEqual(expectedCommand, noTagCommand);
641
642         element.setTag("");
643         String JavaDoc[] emptyStringTagCommand = element.buildHistoryCommand(lastBuildTime, checkTime).getCommandline();
644         assertCommandsEqual(expectedCommand, emptyStringTagCommand);
645
646         element.setTag("HEAD");
647         String JavaDoc[] headTagCommand = element.buildHistoryCommand(lastBuildTime, checkTime).getCommandline();
648         assertCommandsEqual(expectedCommand, headTagCommand);
649     }
650
651     /**
652      * @param expectedCommand
653      * @param actualCommand
654      */

655     private void assertCommandsEqual(String JavaDoc[] expectedCommand, String JavaDoc[] actualCommand) {
656         assertEquals("Mismatched lengths!", expectedCommand.length, actualCommand.length);
657         for (int i = 0; i < expectedCommand.length; i++) {
658             assertEquals(expectedCommand[i], actualCommand[i]);
659         }
660     }
661
662     public void testBuildHistoryCommandWithTag()
663             throws CruiseControlException {
664         Date JavaDoc lastBuildTime = new Date JavaDoc();
665
666         ConcurrentVersionsSystem element = new ConcurrentVersionsSystem();
667         element.setCvsRoot("cvsroot");
668         element.setLocalWorkingCopy(".");
669         element.setTag("sometag");
670
671         String JavaDoc[] expectedCommand =
672                 new String JavaDoc[]{
673                         "cvs",
674                         "-d",
675                         "cvsroot",
676                         "-q",
677                         "log",
678                         "-d" + ConcurrentVersionsSystem.formatCVSDate(lastBuildTime) + "<"
679                                 + ConcurrentVersionsSystem.formatCVSDate(lastBuildTime),
680                         "-rsometag"};
681
682         String JavaDoc[] actualCommand =
683                 element.buildHistoryCommand(lastBuildTime, lastBuildTime).getCommandline();
684
685         assertCommandsEqual(expectedCommand, actualCommand);
686     }
687
688     public void testHistoryCommandNullLocal() throws CruiseControlException {
689         Date JavaDoc lastBuildTime = new Date JavaDoc();
690
691         ConcurrentVersionsSystem element = new ConcurrentVersionsSystem();
692         element.setCvsRoot("cvsroot");
693         element.setModule("module");
694         element.setLocalWorkingCopy(null);
695
696         String JavaDoc[] expectedCommand =
697                 new String JavaDoc[]{
698                         "cvs",
699                         "-d",
700                         "cvsroot",
701                         "-q",
702                         "rlog",
703                         "-N",
704                         "-d" + ConcurrentVersionsSystem.formatCVSDate(lastBuildTime) + "<"
705                                 + ConcurrentVersionsSystem.formatCVSDate(lastBuildTime),
706                         "-b",
707                         "module"};
708
709         String JavaDoc[] actualCommand =
710                 element.buildHistoryCommand(lastBuildTime, lastBuildTime).getCommandline();
711
712         assertCommandsEqual(expectedCommand, actualCommand);
713     }
714
715     public void testHistoryCommandNullCVSROOT() throws CruiseControlException {
716         Date JavaDoc lastBuildTime = new Date JavaDoc();
717
718         ConcurrentVersionsSystem element = new ConcurrentVersionsSystem();
719         element.setCvsRoot(null);
720         element.setLocalWorkingCopy(".");
721
722         String JavaDoc[] expectedCommand =
723                 new String JavaDoc[]{
724                         "cvs",
725                         "-q",
726                         "log",
727                         "-N",
728                         "-d" + ConcurrentVersionsSystem.formatCVSDate(lastBuildTime) + "<"
729                                 + ConcurrentVersionsSystem.formatCVSDate(lastBuildTime),
730                         "-b"};
731
732         String JavaDoc[] actualCommand =
733                 element.buildHistoryCommand(lastBuildTime, lastBuildTime).getCommandline();
734         assertCommandsEqual(expectedCommand, actualCommand);
735     }
736
737     public void testParseLogDate() throws ParseException JavaDoc {
738         TimeZone JavaDoc tz = TimeZone.getDefault();
739         Date JavaDoc may18SixPM2001 = new GregorianCalendar JavaDoc(2001, 4, 18, 18, 0, 0).getTime();
740         assertEquals(may18SixPM2001,
741                 new SimpleDateFormat JavaDoc(ConcurrentVersionsSystem.LOG_DATE_FORMAT).parse("2001/05/18 18:00:00 "
742                         + tz.getDisplayName(tz.inDaylightTime(may18SixPM2001), TimeZone.SHORT)));
743     }
744
745     public void testFormatCVSDateGMTPlusZero() {
746         TimeZone.setDefault(TimeZone.getTimeZone("GMT+0:00"));
747         Date JavaDoc mayEighteenSixPM2001 =
748                 new GregorianCalendar JavaDoc(2001, 4, 18, 18, 0, 0).getTime();
749         assertEquals("2001-05-18 18:00:00 GMT",
750                 ConcurrentVersionsSystem.formatCVSDate(mayEighteenSixPM2001));
751     }
752
753     public void testFormatCVSDateGMTPlusTen() {
754         TimeZone.setDefault(TimeZone.getTimeZone("GMT+10:00"));
755
756         Date JavaDoc mayEighteenSixPM2001 =
757                 new GregorianCalendar JavaDoc(2001, 4, 18, 18, 0, 0).getTime();
758         assertEquals("2001-05-18 08:00:00 GMT",
759                 ConcurrentVersionsSystem.formatCVSDate(mayEighteenSixPM2001));
760         Date JavaDoc may18EightAM2001 =
761                 new GregorianCalendar JavaDoc(2001, 4, 18, 8, 0, 0).getTime();
762         assertEquals("2001-05-17 22:00:00 GMT",
763                 ConcurrentVersionsSystem.formatCVSDate(may18EightAM2001));
764     }
765
766     public void testFormatCVSDateGMTMinusTen() {
767         TimeZone.setDefault(TimeZone.getTimeZone("GMT-10:00"));
768         Date JavaDoc may18SixPM2001 =
769                 new GregorianCalendar JavaDoc(2001, 4, 18, 18, 0, 0).getTime();
770         assertEquals("2001-05-19 04:00:00 GMT",
771                 ConcurrentVersionsSystem.formatCVSDate(may18SixPM2001));
772         Date JavaDoc may18EightAM2001 =
773                 new GregorianCalendar JavaDoc(2001, 4, 18, 8, 0, 0).getTime();
774         assertEquals("2001-05-18 18:00:00 GMT",
775                 ConcurrentVersionsSystem.formatCVSDate(may18EightAM2001));
776     }
777
778     public void testAddAliasToMap() {
779         ConcurrentVersionsSystem cvs = new ConcurrentVersionsSystem();
780         Hashtable JavaDoc aliasMap = new Hashtable JavaDoc();
781         cvs.setMailAliases(aliasMap);
782         String JavaDoc userline = "roberto:'Roberto DaMana <damana@cs.unipr.it>'";
783         cvs.addAliasToMap(userline);
784         userline = "hill:hill@cs.unipr.it";
785         cvs.addAliasToMap(userline);
786         userline = "zolo:zolo";
787         cvs.addAliasToMap(userline);
788         assertEquals("'Roberto DaMana <damana@cs.unipr.it>'", aliasMap.get("roberto"));
789         assertEquals("hill@cs.unipr.it", aliasMap.get("hill"));
790         assertEquals("zolo", aliasMap.get("zolo"));
791
792         userline = "me";
793         cvs.addAliasToMap(userline);
794         assertNull(aliasMap.get("me"));
795     }
796
797     public void testGetCvsServerVersionDifferingClientServerVersions() throws IOException JavaDoc {
798         String JavaDoc logName = "cvslog1-1xversion.txt";
799         final BufferedInputStream JavaDoc input =
800                 new BufferedInputStream JavaDoc(loadTestLog(logName));
801
802         final ConcurrentVersionsSystem cvs = new InputBasedCommandLineMockCVS(input, CVS_VERSION_COMMANDLINE, null);
803         assertEquals("differing client & server version",
804                 getOfficialCVSVersion("1.11.16"), cvs.getCvsServerVersion());
805         assertEquals("differing client & server version", false, cvs.isCvsNewOutputFormat());
806         input.close();
807     }
808
809     public void testGetCvsServerVersionIdenticalClientServerVersions1() throws IOException JavaDoc {
810         String JavaDoc logName = "cvslog1-11version.txt";
811         final BufferedInputStream JavaDoc input =
812                 new BufferedInputStream JavaDoc(loadTestLog(logName));
813
814         final ConcurrentVersionsSystem cvs = new InputBasedCommandLineMockCVS(input, CVS_VERSION_COMMANDLINE, null);
815         assertEquals("identical client & server version 1.11.16",
816                 getOfficialCVSVersion("1.11.16"), cvs.getCvsServerVersion());
817         assertEquals("old output format", false, cvs.isCvsNewOutputFormat());
818         input.close();
819     }
820
821     public void testGetCvsServerVersionIdenticalClientServerVersions2() throws IOException JavaDoc {
822         String JavaDoc logName = "cvslog1-12version.txt";
823         final BufferedInputStream JavaDoc input =
824                 new BufferedInputStream JavaDoc(loadTestLog(logName));
825
826         final ConcurrentVersionsSystem cvs = new InputBasedCommandLineMockCVS(input, CVS_VERSION_COMMANDLINE, null);
827         assertEquals("identical client & server version 1.12.9",
828                 getOfficialCVSVersion("1.12.9"), cvs.getCvsServerVersion());
829         assertEquals("new output format", true, cvs.isCvsNewOutputFormat());
830         input.close();
831     }
832
833     public void testGetCvsNTServerVersionDifferingClientServerVersions() throws IOException JavaDoc {
834         String JavaDoc logName = "cvsntlog2-0xversion.txt";
835         final BufferedInputStream JavaDoc input =
836                 new BufferedInputStream JavaDoc(loadTestLog(logName));
837
838         final ConcurrentVersionsSystem cvs = new InputBasedCommandLineMockCVS(input, CVS_VERSION_COMMANDLINE, null);
839         assertEquals("differing client & server version",
840                 new ConcurrentVersionsSystem.Version("CVSNT", "2.0.14"), cvs.getCvsServerVersion());
841         assertEquals("differing client & server version", false, cvs.isCvsNewOutputFormat());
842         input.close();
843     }
844
845     public void testIsCVSNewVersion() {
846
847         Object JavaDoc[] array = new Object JavaDoc[]{
848                 new MockVersionCVS(getOfficialCVSVersion("1.11.16")), Boolean.FALSE,
849                 new MockVersionCVS(getOfficialCVSVersion("1.12.8")), Boolean.FALSE,
850                 new MockVersionCVS(getOfficialCVSVersion("1.12.9")), Boolean.TRUE,
851                 new MockVersionCVS(getOfficialCVSVersion("1.12.81")), Boolean.TRUE,
852                 new MockVersionCVS(new ConcurrentVersionsSystem.Version("cvsnt", "2.0.14")), Boolean.FALSE
853         };
854
855         for (int i = 0; i < array.length; i += 2) {
856             MockVersionCVS cvs = (MockVersionCVS) array[i];
857             Boolean JavaDoc b = (Boolean JavaDoc) array[i + 1];
858             assertEquals("output format " + cvs.getCvsServerVersion() + " is new?",
859                     b.booleanValue(), cvs.isCvsNewOutputFormat());
860         }
861     }
862
863     /**
864      * on 1.10 version, "version" argument doesn't exist
865      * hence the output is empty.
866      */

867     public void testGetCvsServerVersion1_10version() throws IOException JavaDoc {
868
869         String JavaDoc logContent = "";
870         final InputStream JavaDoc input = new ByteArrayInputStream JavaDoc(logContent.getBytes());
871
872         final ConcurrentVersionsSystem cvs = new InputBasedCommandLineMockCVS(input, CVS_VERSION_COMMANDLINE, null);
873         assertEquals("assuming default cvs version upon empty output",
874                 ConcurrentVersionsSystem.DEFAULT_CVS_SERVER_VERSION, cvs.getCvsServerVersion());
875         assertEquals("assuming old format upon empty output", false, cvs.isCvsNewOutputFormat());
876         input.close();
877     }
878
879     /**
880      * What if the output is broken? This can happen for various reasons.
881      * It is simulated here by truncating the output.
882      */

883     public void testGetCvsServerVersion_brokenOutput() throws IOException JavaDoc {
884
885         String JavaDoc logContent = "Server: Concurrent Versions System (CVS) ";
886         final InputStream JavaDoc input = new ByteArrayInputStream JavaDoc(logContent.getBytes());
887
888         final ConcurrentVersionsSystem cvs = new InputBasedCommandLineMockCVS(input, CVS_VERSION_COMMANDLINE, null);
889         assertEquals("assuming default cvs version upon broken output",
890                 ConcurrentVersionsSystem.DEFAULT_CVS_SERVER_VERSION, cvs.getCvsServerVersion());
891         assertEquals("assuming old format upon broken output", false, cvs.isCvsNewOutputFormat());
892         input.close();
893     }
894
895 }
896
Popular Tags