KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit Copyright (c) 2001,
3  * ThoughtWorks, Inc. 651 W Washington Ave. Suite 600 Chicago, IL 60661 USA All
4  * rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  * + Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * + Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the names of
14  * its contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  ******************************************************************************/

28 package net.sourceforge.cruisecontrol.sourcecontrols;
29
30 import java.util.Calendar JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Date JavaDoc;
33
34 import junit.framework.TestCase;
35 import net.sourceforge.cruisecontrol.CruiseControlException;
36 import net.sourceforge.cruisecontrol.Modification;
37
38 /**
39  * @version $Id: TimeBuildTest.java,v 1.4 2006/02/27 10:58:07 jchyip Exp $
40  */

41 public class TimeBuildTest extends TestCase {
42
43     public void testValidate() {
44         TimeBuild timeBuild = new TimeBuild();
45         try {
46             timeBuild.validate();
47             fail("TimeBuild should throw exceptions when required attributes are not set.");
48         } catch (CruiseControlException e) {
49             assertEquals("the 'time' attribute is mandatory", e.getMessage());
50         }
51     }
52
53     public void testTimes() {
54         Calendar JavaDoc calender1400 = Calendar.getInstance();
55         calender1400.set(2002, Calendar.DECEMBER, 23, 14, 00, 00);
56
57         Calendar JavaDoc calender1600 = Calendar.getInstance();
58         calender1600.set(2002, Calendar.DECEMBER, 23, 16, 00, 00);
59
60         Calendar JavaDoc calender1601 = Calendar.getInstance();
61         calender1601.set(2002, Calendar.DECEMBER, 23, 16, 01, 00);
62         calender1601.set(Calendar.MILLISECOND, 0);
63
64         Calendar JavaDoc calender1602 = Calendar.getInstance();
65         calender1602.set(2002, Calendar.DECEMBER, 23, 16, 02, 00);
66
67         Calendar JavaDoc calender1603 = Calendar.getInstance();
68         calender1603.set(2002, Calendar.DECEMBER, 23, 16, 03, 00);
69
70         String JavaDoc modifierUserName = "epugh";
71         TimeBuild timeBuild = createTimeBuildForTime(modifierUserName, "1601");
72
73         assertSinceLastBuildNoModificationsAtTime(calender1400, calender1600, timeBuild);
74         assertSinceLastBuildNoModificationsAtTime(calender1400, calender1601, timeBuild);
75
76         List JavaDoc modifications = timeBuild.getModifications(calender1400.getTime(), calender1602.getTime());
77         assertHasSingleModificationThatMatchesNameAndTime(modifications, modifierUserName, calender1601.getTime());
78
79         assertSinceLastBuildNoModificationsAtTime(calender1602, calender1603, timeBuild);
80     }
81
82     private void assertSinceLastBuildNoModificationsAtTime(Calendar JavaDoc lastBuild, Calendar JavaDoc now, TimeBuild timeBuild) {
83         List JavaDoc modifications = timeBuild.getModifications(lastBuild.getTime(), now.getTime());
84         assertEquals(0, modifications.size());
85     }
86
87     private TimeBuild createTimeBuildForTime(String JavaDoc userName, String JavaDoc buildTime) {
88         TimeBuild timeBuild = new TimeBuild();
89         timeBuild.setTime(buildTime);
90         timeBuild.setUserName(userName);
91         return timeBuild;
92     }
93
94     public void testTimeBuildWorksAcrossDays() {
95         Calendar JavaDoc calender2000Previousday = Calendar.getInstance();
96         calender2000Previousday.set(2002, Calendar.DECEMBER, 22, 20, 00, 00);
97
98         Calendar JavaDoc calender1400 = Calendar.getInstance();
99         calender1400.set(2002, Calendar.DECEMBER, 23, 14, 00, 00);
100
101         Calendar JavaDoc calender1600 = Calendar.getInstance();
102         calender1600.set(2002, Calendar.DECEMBER, 23, 16, 00, 00);
103         calender1600.set(Calendar.MILLISECOND, 0);
104
105         Calendar JavaDoc calender1601 = Calendar.getInstance();
106         calender1601.set(2002, Calendar.DECEMBER, 23, 16, 01, 00);
107
108         Calendar JavaDoc calender1603 = Calendar.getInstance();
109         calender1603.set(2002, Calendar.DECEMBER, 23, 16, 03, 00);
110
111         // Schedule a timed build for 16:00
112
TimeBuild timeBuild = new TimeBuild();
113         timeBuild.setTime("1600");
114         timeBuild.setUserName("epugh");
115
116         Date JavaDoc lastBuildEightPMYesterday = calender2000Previousday.getTime();
117
118         // No "modifications" at 14:00
119
List JavaDoc modifications = timeBuild.getModifications(lastBuildEightPMYesterday, calender1400.getTime());
120         assertEquals(0, modifications.size());
121
122         // No "modifications" at 16:00
123
modifications = timeBuild.getModifications(lastBuildEightPMYesterday, calender1600.getTime());
124         assertEquals(0, modifications.size());
125
126         // Should have one "modification" at 16:01 which is the TimedBuild
127
// modification for 16:00
128
modifications = timeBuild.getModifications(lastBuildEightPMYesterday, calender1601.getTime());
129         assertEquals(1, modifications.size());
130         assertHasSingleModificationThatMatchesNameAndTime(modifications, "epugh", calender1600.getTime());
131
132         // No "modifications" from 16:01 to 16:03
133
modifications = timeBuild.getModifications(calender1601.getTime(), calender1603.getTime());
134         assertEquals(0, modifications.size());
135     }
136
137     private void assertHasSingleModificationThatMatchesNameAndTime(List JavaDoc modifications, String JavaDoc userName,
138             Date JavaDoc expectedModifiedTime) {
139         assertEquals(1, modifications.size());
140         Modification modification = (Modification) modifications.get(0);
141         assertEquals(userName, modification.userName);
142         assertEquals(expectedModifiedTime, modification.modifiedTime);
143     }
144 }
145
Popular Tags