KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > ModificationSetTest


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
38 package net.sourceforge.cruisecontrol;
39
40 import java.text.DateFormat JavaDoc;
41 import java.text.ParseException JavaDoc;
42 import java.text.SimpleDateFormat JavaDoc;
43 import java.util.ArrayList JavaDoc;
44 import java.util.Date JavaDoc;
45 import java.util.Hashtable JavaDoc;
46 import java.util.Iterator JavaDoc;
47 import java.util.List JavaDoc;
48
49 import junit.framework.TestCase;
50 import net.sourceforge.cruisecontrol.sourcecontrols.MockSourceControl;
51 import net.sourceforge.cruisecontrol.sourcecontrols.Vss;
52
53 import org.jdom.Element;
54 import org.jdom.output.XMLOutputter;
55
56 public class ModificationSetTest extends TestCase {
57
58     private ModificationSet modSet;
59
60     public void testIsLastModificationInQuietPeriod() throws ParseException JavaDoc {
61         SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc("yyyyMMddHHmmss");
62         Modification mod1 = new Modification();
63         mod1.modifiedTime = formatter.parse("20020621140000");
64         Modification mod2 = new Modification();
65         mod2.modifiedTime = formatter.parse("20020621140100");
66
67         // When a change is put into source control with a bad date in the
68
// future, we should still build
69
Modification modInFuture = new Modification();
70         modInFuture.modifiedTime = formatter.parse("30020731150000");
71
72         List JavaDoc mods1 = new ArrayList JavaDoc();
73         mods1.add(mod1);
74         mods1.add(mod2);
75
76         List JavaDoc mods2 = new ArrayList JavaDoc();
77         mods2.add(mod1);
78
79         List JavaDoc hasModInFuture = new ArrayList JavaDoc();
80         hasModInFuture.add(mod1);
81         hasModInFuture.add(mod2);
82         hasModInFuture.add(modInFuture);
83
84         Date JavaDoc now = formatter.parse("20020621140103");
85
86         modSet.setQuietPeriod(5);
87
88         assertEquals(true, modSet.isLastModificationInQuietPeriod(now, mods1));
89         assertEquals(false, modSet.isLastModificationInQuietPeriod(now, mods2));
90         assertEquals(false, modSet.isLastModificationInQuietPeriod(now, hasModInFuture));
91     }
92
93     public void testGetLastModificationMillis() throws ParseException JavaDoc {
94         SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc("yyyyMMddHHmmss");
95         Modification mod1 = new Modification();
96         mod1.modifiedTime = formatter.parse("20020621140000");
97         Modification mod2 = new Modification();
98         mod2.modifiedTime = formatter.parse("20020621140100");
99
100         List JavaDoc mods1 = new ArrayList JavaDoc();
101         mods1.add(mod2);
102         mods1.add(mod1);
103
104         assertEquals(mod2.modifiedTime.getTime(), modSet.getLastModificationMillis(mods1));
105     }
106
107     public void testGetQuietPeriodDifference() throws ParseException JavaDoc {
108         SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc("yyyyMMddHHmmss");
109         Date JavaDoc now = formatter.parse("20020621140103");
110         Modification mod1 = new Modification();
111         mod1.modifiedTime = formatter.parse("20020621140000");
112         Modification mod2 = new Modification();
113         mod2.modifiedTime = formatter.parse("20020621140100");
114
115         List JavaDoc mods1 = new ArrayList JavaDoc();
116         mods1.add(mod1);
117
118         List JavaDoc mods2 = new ArrayList JavaDoc();
119         mods2.add(mod2);
120
121         modSet.setQuietPeriod(5);
122
123         assertEquals(0, modSet.getQuietPeriodDifference(now, mods1));
124         assertEquals(2000, modSet.getQuietPeriodDifference(now, mods2));
125     }
126
127     public void testGetModifications() throws Exception JavaDoc {
128         MockSourceControl mock1 = new MockSourceControl();
129         mock1.setType(1);
130         MockSourceControl mock2 = new MockSourceControl();
131         mock2.setType(2);
132
133         modSet.add(mock1);
134         modSet.add(mock2);
135
136         Element modSetResults = modSet.getModifications(new Date JavaDoc()); //mock source controls don't care about the date
137

138         DateFormat JavaDoc formatter = DateFormatFactory.getDateFormat();
139         Element modificationsElement = new Element("modifications");
140         Iterator JavaDoc mock1ModificationsIterator = mock1.getModifications(new Date JavaDoc(), new Date JavaDoc()).iterator();
141         while (mock1ModificationsIterator.hasNext()) {
142             Modification modification = (Modification) mock1ModificationsIterator.next();
143             modificationsElement.addContent(modification.toElement(formatter));
144         }
145         Iterator JavaDoc mock2ModificationsIterator = mock2.getModifications(new Date JavaDoc(), new Date JavaDoc()).iterator();
146         while (mock2ModificationsIterator.hasNext()) {
147             Modification modification = (Modification) mock2ModificationsIterator.next();
148             modificationsElement.addContent(modification.toElement(formatter));
149         }
150
151         XMLOutputter outputter = new XMLOutputter();
152         assertEquals("XML data differ",
153                 outputter.outputString(modificationsElement),
154                 outputter.outputString(modSetResults));
155     }
156
157     /**
158      * This test will give modificationset two different types of
159      * modifications. One regular, based on the object, and one with Element data.
160      * Uses inline sourcecontrol implementation instead of mock.
161      */

162     public void testGetMixedModifications() throws ParseException JavaDoc {
163         DateFormat JavaDoc formatter = DateFormatFactory.getDateFormat();
164
165         Modification mod1 = new Modification();
166         mod1.userName = "user3";
167         mod1.modifiedTime = formatter.parse("04/04/2004 17:23:50");
168         mod1.comment = "comment3";
169
170         Modification.ModifiedFile mod1file = mod1.createModifiedFile("file3", "dir3");
171         mod1file.action = "Checkin";
172
173         Modification mod2 = new Modification();
174         mod2.userName = "user4";
175         mod2.modifiedTime = formatter.parse("02/02/2002 17:23:50");
176         mod2.comment = "comment4";
177
178         Modification.ModifiedFile mod2file = mod1.createModifiedFile("file4", "dir4");
179         mod2file.action = "Checkin";
180
181         final List JavaDoc result = new ArrayList JavaDoc();
182         result.add(mod1.toElement(formatter));
183         result.add(mod2);
184
185         assertEquals(mod1.modifiedTime.getTime(), modSet.getLastModificationMillis(result));
186
187         modSet.add(new MockSourceControl() {
188             public List JavaDoc getModifications(Date JavaDoc lastBuild, Date JavaDoc now) {
189                 return result;
190             }
191         });
192
193         Element actual = modSet.getModifications(new Date JavaDoc());
194
195         Element expected = new Element("modifications");
196         expected.addContent(mod1.toElement(formatter));
197         expected.addContent(mod2.toElement(formatter));
198
199         XMLOutputter outputter = new XMLOutputter();
200         assertEquals("XML data differ", outputter.outputString(expected), outputter.outputString(actual));
201
202     }
203
204     public void testGetProperties() throws Exception JavaDoc {
205         MockSourceControl mock1 = new MockSourceControl();
206         mock1.setType(1);
207         MockSourceControl mock2 = new MockSourceControl();
208         mock2.setType(2);
209
210         modSet.add(mock1);
211         modSet.add(mock2);
212
213         modSet.getModifications(new Date JavaDoc()); //mock source controls don't care about the date
214

215         Hashtable JavaDoc table = modSet.getProperties();
216         assertNotNull("Properties shouldn't be null.", table);
217         assertEquals("Properties should be empty.", 0, table.size());
218
219         modSet = new ModificationSet();
220         modSet.setQuietPeriod(0);
221         mock1 = new MockSourceControl();
222         mock2 = new MockSourceControl();
223         mock1.setType(1);
224         mock2.setType(2);
225         mock1.setProperty("property");
226         mock2.setPropertyOnDelete("propertyOnDelete");
227
228         modSet.add(mock1);
229         modSet.add(mock2);
230
231         modSet.getModifications(new Date JavaDoc()); //mock source controls don't care about the date
232

233         table = modSet.getProperties();
234         assertNotNull("Properties shouldn't be null.", table);
235         assertEquals("Properties should should have 2 entries.", 2, table.size());
236         assertTrue("Property not found.", table.containsKey("property"));
237         assertTrue("PropertyOnDelete not found.", table.containsKey("propertyOnDelete"));
238
239         modSet = new ModificationSet();
240         modSet.setQuietPeriod(0);
241         mock1 = new MockSourceControl();
242         mock1.setType(1);
243         mock1.setProperty("property");
244
245         modSet.add(mock1);
246         modSet.getModifications(new Date JavaDoc()); //mock source controls don't care about the date
247
table = modSet.getProperties();
248         assertNotNull("Properties shouldn't be null.", table);
249         assertEquals("Properties should should have 1 entry.", 1, table.size());
250         assertTrue("Property not found.", table.containsKey("property"));
251     }
252
253     public void testValidate() throws CruiseControlException {
254         try {
255             modSet.validate();
256             fail("modificationset should require at least one sourcecontrol");
257         } catch (CruiseControlException e) {
258         }
259
260         modSet.add(new Vss());
261         modSet.validate();
262     }
263
264     public void testSetRequireModification() {
265         modSet.getModifications(new Date JavaDoc());
266         assertFalse(modSet.isModified());
267         modSet.setRequireModification(false);
268         assertTrue(modSet.isModified());
269     }
270
271     public void testSetIgnoreFiles() {
272
273         final String JavaDoc correctPattern = "*.txt,dir1/*/file*.txt";
274         try {
275             modSet.setIgnoreFiles(correctPattern);
276         } catch (CruiseControlException e) {
277             fail ("Exception while setting pattern");
278         }
279
280         final List JavaDoc globPatterns = modSet.getIgnoreFiles();
281         assertEquals("The number of parsed patterns is not correct", 2, globPatterns.size());
282
283     }
284
285     public void testFilterIgnoredFiles() throws CruiseControlException, ParseException JavaDoc {
286
287         final DateFormat JavaDoc formatter = DateFormatFactory.getDateFormat();
288         final List JavaDoc modifications = new ArrayList JavaDoc();
289
290         final Modification mod1 = new Modification();
291         mod1.type = "Checkin";
292         mod1.userName = "user1";
293         mod1.modifiedTime = formatter.parse("02/02/2002 17:23:50");
294         mod1.comment = "comment1";
295         mod1.createModifiedFile("file1", "dir1");
296         modifications.add(mod1);
297
298         final Modification mod2 = new Modification();
299         mod2.type = "Checkin";
300         mod2.userName = "user2";
301         mod2.modifiedTime = formatter.parse("02/02/2002 17:23:50");
302         mod2.comment = "comment2";
303         mod2.createModifiedFile("file1", "dir2");
304         modifications.add(mod2);
305
306         final Modification mod3 = new Modification();
307         mod3.type = "Checkin";
308         mod3.userName = "user3";
309         mod3.modifiedTime = formatter.parse("02/02/2002 17:23:50");
310         mod3.comment = "comment1";
311         mod3.createModifiedFile("file3", "dir1");
312         modifications.add(mod3);
313
314         modSet.filterIgnoredModifications(modifications);
315         assertEquals ("No modification should have been filtered out", 3, modifications.size());
316
317         // Now set a filter
318
modSet.setIgnoreFiles("dir2/file3,di?1/f*3");
319         modSet.filterIgnoredModifications(modifications);
320         assertEquals ("No modification have been filtered out", 2, modifications.size());
321
322         final List JavaDoc expectedModifications = new ArrayList JavaDoc();
323         expectedModifications.add(mod1);
324         expectedModifications.add(mod2);
325
326         assertEquals ("The wrong modification has been filtered out", expectedModifications, modifications);
327
328     }
329
330     protected void setUp() throws Exception JavaDoc {
331         modSet = new ModificationSet();
332         modSet.setQuietPeriod(0);
333     }
334
335     protected void tearDown() throws Exception JavaDoc {
336         modSet = null;
337     }
338
339 }
340
Popular Tags