KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.jdom.JDOMException;
43
44 import java.io.File JavaDoc;
45 import java.io.IOException JavaDoc;
46 import java.io.StringReader JavaDoc;
47 import java.text.ParseException JavaDoc;
48 import java.util.ArrayList JavaDoc;
49 import java.util.Calendar JavaDoc;
50 import java.util.Date JavaDoc;
51 import java.util.GregorianCalendar JavaDoc;
52 import java.util.List JavaDoc;
53 import java.util.TimeZone JavaDoc;
54
55 /**
56  * @see <a HREF="http://subversion.tigris.org/">subversion.tigris.org</a>
57  * @author <a HREF="etienne.studer@canoo.com">Etienne Studer</a>
58  */

59 public class SVNTest extends TestCase {
60     private SVN svn;
61     private TimeZone JavaDoc originalTimeZone;
62
63     protected void setUp() throws Exception JavaDoc {
64         svn = new SVN();
65         originalTimeZone = TimeZone.getDefault();
66     }
67
68     protected void tearDown() throws Exception JavaDoc {
69         TimeZone.setDefault(originalTimeZone);
70         svn = null;
71         originalTimeZone = null;
72     }
73
74     public void testValidate() throws CruiseControlException, IOException JavaDoc {
75         try {
76             svn.validate();
77             fail("should throw an exception when no attributes are set");
78         } catch (CruiseControlException e) {
79             // expected
80
}
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             // expected
98
}
99
100         File JavaDoc 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             // expected
120
}
121     }
122
123     public void testBuildHistoryCommand() throws CruiseControlException {
124         svn.setLocalWorkingCopy(".");
125
126         Date JavaDoc checkTime = new Date JavaDoc();
127         long tenMinutes = 10 * 60 * 1000;
128         Date JavaDoc lastBuild = new Date JavaDoc(checkTime.getTime() - tenMinutes);
129
130         String JavaDoc[] expectedCmd =
131             new String JavaDoc[] {
132                 "svn",
133                 "log",
134                 "--non-interactive",
135                 "--xml",
136                 "-v",
137                 "-r",
138                 "{" + SVN.formatSVNDate(lastBuild) + "}:{" + SVN.formatSVNDate(checkTime) + "}"};
139         String JavaDoc[] 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 JavaDoc[] {
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 JavaDoc[] {
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 JavaDoc, IOException JavaDoc {
179         String JavaDoc 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 JavaDoc(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 JavaDoc d = SVN.SVNLogXMLParser.convertDate("2003-04-30T10:01:42.349105");
272             fail("expected ParseException for date without Z but got " + d);
273         } catch (ParseException JavaDoc e) {
274             assertTrue(true);
275         }
276     }
277
278     public void testParseEmptyModifications() throws JDOMException, ParseException JavaDoc, IOException JavaDoc {
279         String JavaDoc svnLog =
280             "<?xml version=\"1.0\" encoding = \"ISO-8859-1\"?>\n " + "<log>\n" + "</log>";
281
282         Modification[] modifications = SVN.SVNLogXMLParser.parse(new StringReader JavaDoc(svnLog));
283         assertEquals(0, modifications.length);
284     }
285     
286     public void testChangeWithoutReadAccessToChangedFileShouldResultInNoModificationReported()
287           throws ParseException JavaDoc, JDOMException, IOException JavaDoc {
288         String JavaDoc 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 JavaDoc(svnLog));
295         assertEquals(0, modifications.length);
296     }
297
298     public void testParseAndFilter() throws ParseException JavaDoc, JDOMException, IOException JavaDoc {
299         String JavaDoc 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 JavaDoc julyTwentynineSixPM2003 =
330             new GregorianCalendar JavaDoc(2003, Calendar.JULY, 29, 18, 0, 0).getTime();
331
332         List JavaDoc modifications = SVN.SVNLogXMLParser.parseAndFilter(new StringReader JavaDoc(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 JavaDoc julyTwentyeightZeroPM2003 =
358                 new GregorianCalendar JavaDoc(2003, Calendar.JULY, 28, 0, 0, 0).getTime();
359
360         modifications = SVN.SVNLogXMLParser.parseAndFilter(new StringReader JavaDoc(svnLog), julyTwentyeightZeroPM2003);
361         assertEquals(3, modifications.size());
362     }
363
364     public void testFormatDatesForSvnLog() {
365         TimeZone.setDefault(TimeZone.getTimeZone("GMT+10:00"));
366
367         Date JavaDoc maySeventeenSixPM2001 =
368             new GregorianCalendar JavaDoc(2001, Calendar.MAY, 17, 18, 0, 0).getTime();
369         assertEquals(
370             "2001-05-17T08:00:00Z",
371             SVN.formatSVNDate(maySeventeenSixPM2001));
372
373         Date JavaDoc maySeventeenEightAM2001 =
374             new GregorianCalendar JavaDoc(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 JavaDoc marchTwelfFourPM2003 =
382             new GregorianCalendar JavaDoc(2003, Calendar.MARCH, 12, 16, 0, 0).getTime();
383         assertEquals(
384             "2003-03-13T02:00:00Z",
385             SVN.formatSVNDate(marchTwelfFourPM2003));
386
387         Date JavaDoc marchTwelfTenAM2003 =
388             new GregorianCalendar JavaDoc(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 JavaDoc {
393         svn.setProperty("hasChanges?");
394
395         List JavaDoc noModifications = new ArrayList JavaDoc();
396         svn.fillPropertiesIfNeeded(noModifications);
397         assertEquals(null, svn.getProperties().get("hasChanges?"));
398
399         List JavaDoc hasModifications = new ArrayList JavaDoc();
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 JavaDoc {
413         svn.setPropertyOnDelete("hasDeletions?");
414
415         List JavaDoc noModifications = new ArrayList JavaDoc();
416         svn.fillPropertiesIfNeeded(noModifications);
417         assertEquals(null, svn.getProperties().get("hasDeletions?"));
418
419         List JavaDoc noDeletions = new ArrayList JavaDoc();
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 JavaDoc hasDeletions = new ArrayList JavaDoc();
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 JavaDoc date,
455         String JavaDoc user,
456         String JavaDoc comment,
457         String JavaDoc revision,
458         String JavaDoc folder,
459         String JavaDoc file,
460         String JavaDoc 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 JavaDoc[] expected, Object JavaDoc[] 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