1 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 ; 48 import java.io.ByteArrayInputStream ; 49 import java.io.IOException ; 50 import java.io.InputStream ; 51 import java.io.PipedInputStream ; 52 import java.io.PipedOutputStream ; 53 import java.text.ParseException ; 54 import java.text.SimpleDateFormat ; 55 import java.util.Collections ; 56 import java.util.Date ; 57 import java.util.GregorianCalendar ; 58 import java.util.HashMap ; 59 import java.util.Hashtable ; 60 import java.util.List ; 61 import java.util.Map ; 62 import java.util.TimeZone ; 63 64 65 69 public class ConcurrentVersionsSystemTest extends TestCase { 70 private TimeZone originalTimeZone; 71 static final String [] CVS_VERSION_COMMANDLINE = new String []{"cvs", "version"}; 72 73 protected void setUp() throws Exception { 74 originalTimeZone = TimeZone.getDefault(); 75 } 76 77 protected void tearDown() throws Exception { 78 TimeZone.setDefault(originalTimeZone); 79 originalTimeZone = null; 80 } 81 82 private Date parseCVSDateFormat(String dateString) throws ParseException { 83 return DateUtil.parseCVSDate(dateString); 84 } 85 86 private Date parseLogDateFormat(String dateString) throws ParseException { 87 return new SimpleDateFormat (ConcurrentVersionsSystem.LOG_DATE_FORMAT).parse(dateString); 88 } 89 90 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 cvsVersion) { 106 return new ConcurrentVersionsSystem.Version(ConcurrentVersionsSystem.OFFICIAL_CVS_NAME, cvsVersion); 107 } 108 109 113 static class InputBasedCommandLineMockCVS extends ConcurrentVersionsSystem { 114 private final InputStream inputStream; 115 private final String [] expectedCommandline; 116 private final String expectedWorkingDirectory; 117 118 public InputBasedCommandLineMockCVS(final InputStream inputStream, 119 final String [] expectedCommandLine, 120 final String expectedWorkingDirectory) { 121 this.inputStream = inputStream; 122 expectedCommandline = expectedCommandLine; 123 this.expectedWorkingDirectory = expectedWorkingDirectory; 124 } 125 126 protected Commandline getCommandline() { 128 final MockCommandline mockCommandline = new MockCommandline(); 129 mockCommandline.setExpectedCommandline(expectedCommandline); 130 mockCommandline.setExpectedWorkingDirectory(expectedWorkingDirectory); 131 mockCommandline.setProcessErrorStream(new PipedInputStream ()); 132 mockCommandline.setProcessInputStream(inputStream); 133 mockCommandline.setProcessOutputStream(new PipedOutputStream ()); 134 return mockCommandline; 135 } 136 } 137 138 static class MockOSEnvironment extends OSEnvironment { 139 private Map myVariables = new HashMap (); 140 141 public void add(String variable, String value) { 142 myVariables.put(variable, value); 143 } 144 145 public String getVariable(String variable) { 146 return (String ) 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 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 loadTestLog(String name) { 230 InputStream 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 , ParseException { 236 final String cvsVersion = "1.11.16"; 238 ConcurrentVersionsSystem cvs = new MockVersionCVS(getOfficialCVSVersion(cvsVersion)); 239 Hashtable emailAliases = new Hashtable (); 240 emailAliases.put("alden", "alden@users.sourceforge.net"); 241 emailAliases.put("tim", "tim@tim.net"); 242 cvs.setMailAliases(emailAliases); 243 244 BufferedInputStream input = 245 new BufferedInputStream (loadTestLog("cvslog1-11.txt")); 246 List 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 , ParseException { 308 final String 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 emailAliases = new Hashtable (); 314 emailAliases.put("alden", "alden@users.sourceforge.net"); 315 emailAliases.put("tim", "tim@tim.net"); 316 cvs.setMailAliases(emailAliases); 317 318 BufferedInputStream input = 319 new BufferedInputStream (loadTestLog("cvslog1-11-remote.txt")); 320 List 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 , ParseException { 382 ConcurrentVersionsSystem cvs = new MockVersionCVS(getOfficialCVSVersion("1.12.9")); 383 Hashtable emailAliases = new Hashtable (); 384 emailAliases.put("jerome", "jerome@coffeebreaks.org"); 385 cvs.setMailAliases(emailAliases); 386 387 BufferedInputStream input = 388 new BufferedInputStream (loadTestLog("cvslog1-12.txt")); 389 List 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 , ParseException { 411 ConcurrentVersionsSystem cvs = new MockVersionCVS(getOfficialCVSVersion("1.11.16")); 413 Hashtable emailAliases = new Hashtable (); 414 emailAliases.put("alden", "alden@users.sourceforge.net"); 415 cvs.setMailAliases(emailAliases); 416 417 cvs.setTag("BRANCH_TEST_BUILD"); 418 BufferedInputStream input = 419 new BufferedInputStream (loadTestLog("cvslog1-11branch.txt")); 420 List 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 , ParseException { 472 ConcurrentVersionsSystem cvs = new MockVersionCVS(getOfficialCVSVersion("1.12.9")); 474 Hashtable emailAliases = new Hashtable (); 475 cvs.setMailAliases(emailAliases); 476 477 cvs.setTag("TEST"); 478 BufferedInputStream input = 479 new BufferedInputStream (loadTestLog("cvslog1-12tagnobranch.txt")); 480 List 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 { 501 ConcurrentVersionsSystem cvs = new MockVersionCVS(getOfficialCVSVersion("1.11.16")); 503 cvs.setMailAliases(new Hashtable ()); 504 cvs.setProperty("property"); 505 cvs.setPropertyOnDelete("propertyOnDelete"); 506 507 String logName = "cvslog1-11.txt"; 508 BufferedInputStream input = 509 new BufferedInputStream (loadTestLog(logName)); 510 cvs.parseStream(input); 511 input.close(); 512 513 Map 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 ConcurrentVersionsSystem cvs2 = new MockVersionCVS(getOfficialCVSVersion("1.11.16")); 525 cvs2.setMailAliases(new Hashtable ()); 526 input = new BufferedInputStream (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 { 537 ConcurrentVersionsSystem cvs = new MockVersionCVS(getOfficialCVSVersion("1.11.16")); 538 cvs.setMailAliases(new Hashtable ()); 539 cvs.setProperty("property"); 540 cvs.setPropertyOnDelete("propertyOnDelete"); 541 String logName = "cvslog1-11noMods.txt"; 542 BufferedInputStream input = 543 new BufferedInputStream (loadTestLog(logName)); 544 cvs.parseStream(input); 545 input.close(); 546 547 Map 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 { 554 ConcurrentVersionsSystem cvs = new MockVersionCVS(getOfficialCVSVersion("1.11.16")); 556 cvs.setMailAliases(new Hashtable ()); 557 cvs.setProperty("property"); 558 cvs.setPropertyOnDelete("propertyOnDelete"); 559 String logName = "cvslog1-11mods.txt"; 560 BufferedInputStream input = 561 new BufferedInputStream (loadTestLog(logName)); 562 cvs.parseStream(input); 563 input.close(); 564 565 Map 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 ConcurrentVersionsSystem cvs2 = new MockVersionCVS(getOfficialCVSVersion("1.11.16")); 574 cvs2.setMailAliases(new Hashtable ()); 575 cvs2.setPropertyOnDelete("propertyOnDelete"); 576 input = new BufferedInputStream (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 { 587 ConcurrentVersionsSystem cvs = new MockVersionCVS(getOfficialCVSVersion("1.11.16")); 589 cvs.setMailAliases(new Hashtable ()); 590 cvs.setPropertyOnDelete("propertyOnDelete"); 591 String logName = "cvslog1-11del.txt"; 592 BufferedInputStream input = 593 new BufferedInputStream (loadTestLog(logName)); 594 cvs.parseStream(input); 595 input.close(); 596 597 Map 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 ConcurrentVersionsSystem cvs2 = new MockVersionCVS(getOfficialCVSVersion("1.11.16")); 607 cvs2.setMailAliases(new Hashtable ()); 608 input = new BufferedInputStream (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 checkTime = new Date (); 620 long tenMinutes = 10 * 60 * 1000; 621 Date lastBuildTime = new Date (checkTime.getTime() - tenMinutes); 622 623 ConcurrentVersionsSystem element = new ConcurrentVersionsSystem(); 624 element.setCvsRoot("cvsroot"); 625 element.setLocalWorkingCopy("."); 626 627 String [] expectedCommand = 628 new String []{ 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 [] noTagCommand = element.buildHistoryCommand(lastBuildTime, checkTime).getCommandline(); 640 assertCommandsEqual(expectedCommand, noTagCommand); 641 642 element.setTag(""); 643 String [] emptyStringTagCommand = element.buildHistoryCommand(lastBuildTime, checkTime).getCommandline(); 644 assertCommandsEqual(expectedCommand, emptyStringTagCommand); 645 646 element.setTag("HEAD"); 647 String [] headTagCommand = element.buildHistoryCommand(lastBuildTime, checkTime).getCommandline(); 648 assertCommandsEqual(expectedCommand, headTagCommand); 649 } 650 651 655 private void assertCommandsEqual(String [] expectedCommand, String [] 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 lastBuildTime = new Date (); 665 666 ConcurrentVersionsSystem element = new ConcurrentVersionsSystem(); 667 element.setCvsRoot("cvsroot"); 668 element.setLocalWorkingCopy("."); 669 element.setTag("sometag"); 670 671 String [] expectedCommand = 672 new String []{ 673 "cvs", 674 "-d", 675 "cvsroot", 676 "-q", 677 "log", 678 "-d" + ConcurrentVersionsSystem.formatCVSDate(lastBuildTime) + "<" 679 + ConcurrentVersionsSystem.formatCVSDate(lastBuildTime), 680 "-rsometag"}; 681 682 String [] actualCommand = 683 element.buildHistoryCommand(lastBuildTime, lastBuildTime).getCommandline(); 684 685 assertCommandsEqual(expectedCommand, actualCommand); 686 } 687 688 public void testHistoryCommandNullLocal() throws CruiseControlException { 689 Date lastBuildTime = new Date (); 690 691 ConcurrentVersionsSystem element = new ConcurrentVersionsSystem(); 692 element.setCvsRoot("cvsroot"); 693 element.setModule("module"); 694 element.setLocalWorkingCopy(null); 695 696 String [] expectedCommand = 697 new String []{ 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 [] actualCommand = 710 element.buildHistoryCommand(lastBuildTime, lastBuildTime).getCommandline(); 711 712 assertCommandsEqual(expectedCommand, actualCommand); 713 } 714 715 public void testHistoryCommandNullCVSROOT() throws CruiseControlException { 716 Date lastBuildTime = new Date (); 717 718 ConcurrentVersionsSystem element = new ConcurrentVersionsSystem(); 719 element.setCvsRoot(null); 720 element.setLocalWorkingCopy("."); 721 722 String [] expectedCommand = 723 new String []{ 724 "cvs", 725 "-q", 726 "log", 727 "-N", 728 "-d" + ConcurrentVersionsSystem.formatCVSDate(lastBuildTime) + "<" 729 + ConcurrentVersionsSystem.formatCVSDate(lastBuildTime), 730 "-b"}; 731 732 String [] actualCommand = 733 element.buildHistoryCommand(lastBuildTime, lastBuildTime).getCommandline(); 734 assertCommandsEqual(expectedCommand, actualCommand); 735 } 736 737 public void testParseLogDate() throws ParseException { 738 TimeZone tz = TimeZone.getDefault(); 739 Date may18SixPM2001 = new GregorianCalendar (2001, 4, 18, 18, 0, 0).getTime(); 740 assertEquals(may18SixPM2001, 741 new SimpleDateFormat (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 mayEighteenSixPM2001 = 748 new GregorianCalendar (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 mayEighteenSixPM2001 = 757 new GregorianCalendar (2001, 4, 18, 18, 0, 0).getTime(); 758 assertEquals("2001-05-18 08:00:00 GMT", 759 ConcurrentVersionsSystem.formatCVSDate(mayEighteenSixPM2001)); 760 Date may18EightAM2001 = 761 new GregorianCalendar (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 may18SixPM2001 = 769 new GregorianCalendar (2001, 4, 18, 18, 0, 0).getTime(); 770 assertEquals("2001-05-19 04:00:00 GMT", 771 ConcurrentVersionsSystem.formatCVSDate(may18SixPM2001)); 772 Date may18EightAM2001 = 773 new GregorianCalendar (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 aliasMap = new Hashtable (); 781 cvs.setMailAliases(aliasMap); 782 String 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 { 798 String logName = "cvslog1-1xversion.txt"; 799 final BufferedInputStream input = 800 new BufferedInputStream (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 { 810 String logName = "cvslog1-11version.txt"; 811 final BufferedInputStream input = 812 new BufferedInputStream (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 { 822 String logName = "cvslog1-12version.txt"; 823 final BufferedInputStream input = 824 new BufferedInputStream (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 { 834 String logName = "cvsntlog2-0xversion.txt"; 835 final BufferedInputStream input = 836 new BufferedInputStream (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 [] array = new Object []{ 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 b = (Boolean ) array[i + 1]; 858 assertEquals("output format " + cvs.getCvsServerVersion() + " is new?", 859 b.booleanValue(), cvs.isCvsNewOutputFormat()); 860 } 861 } 862 863 867 public void testGetCvsServerVersion1_10version() throws IOException { 868 869 String logContent = ""; 870 final InputStream input = new ByteArrayInputStream (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 883 public void testGetCvsServerVersion_brokenOutput() throws IOException { 884 885 String logContent = "Server: Concurrent Versions System (CVS) "; 886 final InputStream input = new ByteArrayInputStream (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 |