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 org.jdom.JDOMException; 43 44 import java.io.File ; 45 import java.io.IOException ; 46 import java.io.StringReader ; 47 import java.text.ParseException ; 48 import java.util.ArrayList ; 49 import java.util.Calendar ; 50 import java.util.Date ; 51 import java.util.GregorianCalendar ; 52 import java.util.List ; 53 import java.util.TimeZone ; 54 55 59 public class SVNTest extends TestCase { 60 private SVN svn; 61 private TimeZone originalTimeZone; 62 63 protected void setUp() throws Exception { 64 svn = new SVN(); 65 originalTimeZone = TimeZone.getDefault(); 66 } 67 68 protected void tearDown() throws Exception { 69 TimeZone.setDefault(originalTimeZone); 70 svn = null; 71 originalTimeZone = null; 72 } 73 74 public void testValidate() throws CruiseControlException, IOException { 75 try { 76 svn.validate(); 77 fail("should throw an exception when no attributes are set"); 78 } catch (CruiseControlException e) { 79 } 81 82 svn.setRepositoryLocation("http://svn.collab.net/repos/svn"); 83 try { 84 svn.validate(); 85 } catch (CruiseControlException e) { 86 fail( 87 "should not throw an exception when at least the 'repositoryLocation' attribute " 88 + "is set"); 89 } 90 91 svn = new SVN(); 92 svn.setLocalWorkingCopy("invalid directory"); 93 try { 94 svn.validate(); 95 fail("should throw an exception when an invalid 'localWorkingCopy' attribute is set"); 96 } catch (CruiseControlException e) { 97 } 99 100 File tempFile = File.createTempFile("temp", "txt"); 101 tempFile.deleteOnExit(); 102 103 svn = new SVN(); 104 svn.setLocalWorkingCopy(tempFile.getParent()); 105 try { 106 svn.validate(); 107 } catch (CruiseControlException e) { 108 fail( 109 "should not throw an exception when at least a valid 'localWorkingCopy' " 110 + "attribute is set"); 111 } 112 113 svn = new SVN(); 114 svn.setLocalWorkingCopy(tempFile.getAbsolutePath()); 115 try { 116 svn.validate(); 117 fail("should throw an exception when 'localWorkingCopy' is file instead of directory."); 118 } catch (CruiseControlException e) { 119 } 121 } 122 123 public void testBuildHistoryCommand() throws CruiseControlException { 124 svn.setLocalWorkingCopy("."); 125 126 Date checkTime = new Date (); 127 long tenMinutes = 10 * 60 * 1000; 128 Date lastBuild = new Date (checkTime.getTime() - tenMinutes); 129 130 String [] expectedCmd = 131 new String [] { 132 "svn", 133 "log", 134 "--non-interactive", 135 "--xml", 136 "-v", 137 "-r", 138 "{" + SVN.formatSVNDate(lastBuild) + "}:{" + SVN.formatSVNDate(checkTime) + "}"}; 139 String [] actualCmd = svn.buildHistoryCommand(lastBuild, checkTime).getCommandline(); 140 assertArraysEquals(expectedCmd, actualCmd); 141 142 svn.setRepositoryLocation("http://svn.collab.net/repos/svn"); 143 144 expectedCmd = 145 new String [] { 146 "svn", 147 "log", 148 "--non-interactive", 149 "--xml", 150 "-v", 151 "-r", 152 "{" + SVN.formatSVNDate(lastBuild) + "}:{" + SVN.formatSVNDate(checkTime) + "}", 153 "http://svn.collab.net/repos/svn" }; 154 actualCmd = svn.buildHistoryCommand(lastBuild, checkTime).getCommandline(); 155 assertArraysEquals(expectedCmd, actualCmd); 156 157 svn.setUsername("lee"); 158 svn.setPassword("secret"); 159 160 expectedCmd = 161 new String [] { 162 "svn", 163 "log", 164 "--non-interactive", 165 "--xml", 166 "-v", 167 "-r", 168 "{" + SVN.formatSVNDate(lastBuild) + "}:{" + SVN.formatSVNDate(checkTime) + "}", 169 "--username", 170 "lee", 171 "--password", 172 "secret", 173 "http://svn.collab.net/repos/svn" }; 174 actualCmd = svn.buildHistoryCommand(lastBuild, checkTime).getCommandline(); 175 assertArraysEquals(expectedCmd, actualCmd); 176 } 177 178 public void testParseModifications() throws JDOMException, ParseException , IOException { 179 String svnLog = 180 "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" 181 + "<log>\n" 182 + " <logentry revision=\"663\">\n" 183 + " <author>lee</author>\n" 184 + " <date>2003-04-30T10:01:42.349105Z</date>\n" 185 + " <paths>\n" 186 + " <path action=\"A\">/trunk/playground/aaa/ccc</path>\n" 187 + " <path action=\"M\">/trunk/playground/aaa/ccc/d.txt</path>\n" 188 + " <path action=\"A\">/trunk/playground/bbb</path>\n" 189 + " </paths>\n" 190 + " <msg>bli</msg>\n" 191 + " </logentry>\n" 192 + " <logentry revision=\"664\">\n" 193 + " <author>etienne</author>\n" 194 + " <date>2003-04-30T10:03:14.100900Z</date>\n" 195 + " <paths>\n" 196 + " <path action=\"A\">/trunk/playground/aaa/f.txt</path>\n" 197 + " </paths>\n" 198 + " <msg>bla</msg>\n" 199 + " </logentry>\n" 200 + " <logentry revision=\"665\">\n" 201 + " <author>martin</author>\n" 202 + " <date>2003-04-30T10:04:48.050619Z</date>\n" 203 + " <paths>\n" 204 + " <path action=\"D\">/trunk/playground/bbb</path>\n" 205 + " </paths>\n" 206 + " <msg>blo</msg>\n" 207 + " </logentry>\n" 208 + "</log>"; 209 210 Modification[] modifications = SVN.SVNLogXMLParser.parse(new StringReader (svnLog)); 211 assertEquals(5, modifications.length); 212 213 Modification modification = 214 createModification( 215 SVN.getOutDateFormatter().parse("2003-04-30T10:01:42.349"), 216 "lee", 217 "bli", 218 "663", 219 "", 220 "/trunk/playground/aaa/ccc", 221 "added"); 222 assertEquals(modification, modifications[0]); 223 224 modification = 225 createModification( 226 SVN.getOutDateFormatter().parse("2003-04-30T10:01:42.349"), 227 "lee", 228 "bli", 229 "663", 230 "", 231 "/trunk/playground/aaa/ccc/d.txt", 232 "modified"); 233 assertEquals(modification, modifications[1]); 234 235 modification = 236 createModification( 237 SVN.getOutDateFormatter().parse("2003-04-30T10:01:42.349"), 238 "lee", 239 "bli", 240 "663", 241 "", 242 "/trunk/playground/bbb", 243 "added"); 244 assertEquals(modification, modifications[2]); 245 246 modification = 247 createModification( 248 SVN.getOutDateFormatter().parse("2003-04-30T10:03:14.100"), 249 "etienne", 250 "bla", 251 "664", 252 "", 253 "/trunk/playground/aaa/f.txt", 254 "added"); 255 assertEquals(modification, modifications[3]); 256 257 modification = 258 createModification( 259 SVN.getOutDateFormatter().parse("2003-04-30T10:04:48.050"), 260 "martin", 261 "blo", 262 "665", 263 "", 264 "/trunk/playground/bbb", 265 "deleted"); 266 assertEquals(modification, modifications[4]); 267 } 268 269 public void testConvertDateIllegalArgument() { 270 try { 271 Date d = SVN.SVNLogXMLParser.convertDate("2003-04-30T10:01:42.349105"); 272 fail("expected ParseException for date without Z but got " + d); 273 } catch (ParseException e) { 274 assertTrue(true); 275 } 276 } 277 278 public void testParseEmptyModifications() throws JDOMException, ParseException , IOException { 279 String svnLog = 280 "<?xml version=\"1.0\" encoding = \"ISO-8859-1\"?>\n " + "<log>\n" + "</log>"; 281 282 Modification[] modifications = SVN.SVNLogXMLParser.parse(new StringReader (svnLog)); 283 assertEquals(0, modifications.length); 284 } 285 286 public void testChangeWithoutReadAccessToChangedFileShouldResultInNoModificationReported() 287 throws ParseException , JDOMException, IOException { 288 String svnLog = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" 289 + "<log>\n" 290 + " <logentry revision=\"1234\">\n" 291 + " <msg></msg>\n" 292 + " </logentry>\n" 293 + "</log>"; 294 Modification[] modifications = SVN.SVNLogXMLParser.parse(new StringReader (svnLog)); 295 assertEquals(0, modifications.length); 296 } 297 298 public void testParseAndFilter() throws ParseException , JDOMException, IOException { 299 String svnLog = 300 "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" 301 + "<log>\n" 302 + " <logentry revision=\"663\">\n" 303 + " <author>lee</author>\n" 304 + " <date>2003-08-02T10:01:13.349105Z</date>\n" 305 + " <paths>\n" 306 + " <path action=\"A\">/trunk/playground/bbb</path>\n" 307 + " </paths>\n" 308 + " <msg>bli</msg>\n" 309 + " </logentry>\n" 310 + " <logentry revision=\"664\">\n" 311 + " <author>etienne</author>\n" 312 + " <date>2003-07-29T17:45:12.100900Z</date>\n" 313 + " <paths>\n" 314 + " <path action=\"A\">/trunk/playground/aaa/f.txt</path>\n" 315 + " </paths>\n" 316 + " <msg>bla</msg>\n" 317 + " </logentry>\n" 318 + " <logentry revision=\"665\">\n" 319 + " <author>martin</author>\n" 320 + " <date>2003-07-29T18:15:11.100900Z</date>\n" 321 + " <paths>\n" 322 + " <path action=\"D\">/trunk/playground/ccc</path>\n" 323 + " </paths>\n" 324 + " <msg>blo</msg>\n" 325 + " </logentry>\n" 326 + "</log>"; 327 328 TimeZone.setDefault(TimeZone.getTimeZone("GMT+0:00")); 329 Date julyTwentynineSixPM2003 = 330 new GregorianCalendar (2003, Calendar.JULY, 29, 18, 0, 0).getTime(); 331 332 List modifications = SVN.SVNLogXMLParser.parseAndFilter(new StringReader (svnLog), julyTwentynineSixPM2003); 333 assertEquals(2, modifications.size()); 334 335 Modification modification = 336 createModification( 337 SVN.getOutDateFormatter().parse("2003-08-02T10:01:13.349"), 338 "lee", 339 "bli", 340 "663", 341 "", 342 "/trunk/playground/bbb", 343 "added"); 344 assertEquals(modification, modifications.get(0)); 345 346 modification = 347 createModification( 348 SVN.getOutDateFormatter().parse("2003-07-29T18:15:11.100"), 349 "martin", 350 "blo", 351 "665", 352 "", 353 "/trunk/playground/ccc", 354 "deleted"); 355 assertEquals(modification, modifications.get(1)); 356 357 Date julyTwentyeightZeroPM2003 = 358 new GregorianCalendar (2003, Calendar.JULY, 28, 0, 0, 0).getTime(); 359 360 modifications = SVN.SVNLogXMLParser.parseAndFilter(new StringReader (svnLog), julyTwentyeightZeroPM2003); 361 assertEquals(3, modifications.size()); 362 } 363 364 public void testFormatDatesForSvnLog() { 365 TimeZone.setDefault(TimeZone.getTimeZone("GMT+10:00")); 366 367 Date maySeventeenSixPM2001 = 368 new GregorianCalendar (2001, Calendar.MAY, 17, 18, 0, 0).getTime(); 369 assertEquals( 370 "2001-05-17T08:00:00Z", 371 SVN.formatSVNDate(maySeventeenSixPM2001)); 372 373 Date maySeventeenEightAM2001 = 374 new GregorianCalendar (2001, Calendar.MAY, 17, 8, 0, 0).getTime(); 375 assertEquals( 376 "2001-05-16T22:00:00Z", 377 SVN.formatSVNDate(maySeventeenEightAM2001)); 378 379 TimeZone.setDefault(TimeZone.getTimeZone("GMT-10:00")); 380 381 Date marchTwelfFourPM2003 = 382 new GregorianCalendar (2003, Calendar.MARCH, 12, 16, 0, 0).getTime(); 383 assertEquals( 384 "2003-03-13T02:00:00Z", 385 SVN.formatSVNDate(marchTwelfFourPM2003)); 386 387 Date marchTwelfTenAM2003 = 388 new GregorianCalendar (2003, Calendar.MARCH, 12, 10, 0, 0).getTime(); 389 assertEquals("2003-03-12T20:00:00Z", SVN.formatSVNDate(marchTwelfTenAM2003)); 390 } 391 392 public void testSetProperty() throws ParseException { 393 svn.setProperty("hasChanges?"); 394 395 List noModifications = new ArrayList (); 396 svn.fillPropertiesIfNeeded(noModifications); 397 assertEquals(null, svn.getProperties().get("hasChanges?")); 398 399 List hasModifications = new ArrayList (); 400 hasModifications.add(createModification( 401 SVN.getOutDateFormatter().parse("2003-08-02T10:01:13.349"), 402 "lee", 403 "bli", 404 "663", 405 "", 406 "/trunk/playground/bbb", 407 "added")); 408 svn.fillPropertiesIfNeeded(hasModifications); 409 assertEquals("true", svn.getProperties().get("hasChanges?")); 410 } 411 412 public void testSetPropertyOnDelete() throws ParseException { 413 svn.setPropertyOnDelete("hasDeletions?"); 414 415 List noModifications = new ArrayList (); 416 svn.fillPropertiesIfNeeded(noModifications); 417 assertEquals(null, svn.getProperties().get("hasDeletions?")); 418 419 List noDeletions = new ArrayList (); 420 noDeletions.add(createModification( 421 SVN.getOutDateFormatter().parse("2003-08-02T10:01:13.349"), 422 "lee", 423 "bli", 424 "663", 425 "", 426 "/trunk/playground/bbb", 427 "added")); 428 svn.fillPropertiesIfNeeded(noDeletions); 429 assertEquals(null, svn.getProperties().get("hasDeletions?")); 430 431 List hasDeletions = new ArrayList (); 432 hasDeletions.add(createModification( 433 SVN.getOutDateFormatter().parse("2003-08-02T10:01:13.349"), 434 "lee", 435 "bli", 436 "663", 437 "", 438 "/trunk/playground/aaa", 439 "added")); 440 hasDeletions.add(createModification( 441 SVN.getOutDateFormatter().parse("2003-08-02T10:01:13.349"), 442 "lee", 443 "bli", 444 "663", 445 "", 446 "/trunk/playground/bbb", 447 "deleted")); 448 svn.fillPropertiesIfNeeded(hasDeletions); 449 assertEquals("true", svn.getProperties().get("hasDeletions?")); 450 } 451 452 453 private static Modification createModification( 454 Date date, 455 String user, 456 String comment, 457 String revision, 458 String folder, 459 String file, 460 String type) { 461 Modification modification = new Modification("svn"); 462 Modification.ModifiedFile modifiedFile = modification.createModifiedFile(file, folder); 463 modifiedFile.action = type; 464 modifiedFile.revision = revision; 465 466 modification.modifiedTime = date; 467 modification.userName = user; 468 modification.comment = comment; 469 modification.revision = revision; 470 return modification; 471 } 472 473 private static void assertArraysEquals(Object [] expected, Object [] actual) { 474 assertEquals("array lengths mismatch!", expected.length, actual.length); 475 for (int i = 0; i < expected.length; i++) { 476 assertEquals(expected[i], actual[i]); 477 } 478 } 479 } 480 | Popular Tags |