1 28 package net.sourceforge.cruisecontrol.sourcecontrols; 29 30 import java.util.Calendar ; 31 import java.util.List ; 32 import java.util.Date ; 33 34 import junit.framework.TestCase; 35 import net.sourceforge.cruisecontrol.CruiseControlException; 36 import net.sourceforge.cruisecontrol.Modification; 37 38 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 calender1400 = Calendar.getInstance(); 55 calender1400.set(2002, Calendar.DECEMBER, 23, 14, 00, 00); 56 57 Calendar calender1600 = Calendar.getInstance(); 58 calender1600.set(2002, Calendar.DECEMBER, 23, 16, 00, 00); 59 60 Calendar calender1601 = Calendar.getInstance(); 61 calender1601.set(2002, Calendar.DECEMBER, 23, 16, 01, 00); 62 calender1601.set(Calendar.MILLISECOND, 0); 63 64 Calendar calender1602 = Calendar.getInstance(); 65 calender1602.set(2002, Calendar.DECEMBER, 23, 16, 02, 00); 66 67 Calendar calender1603 = Calendar.getInstance(); 68 calender1603.set(2002, Calendar.DECEMBER, 23, 16, 03, 00); 69 70 String modifierUserName = "epugh"; 71 TimeBuild timeBuild = createTimeBuildForTime(modifierUserName, "1601"); 72 73 assertSinceLastBuildNoModificationsAtTime(calender1400, calender1600, timeBuild); 74 assertSinceLastBuildNoModificationsAtTime(calender1400, calender1601, timeBuild); 75 76 List 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 lastBuild, Calendar now, TimeBuild timeBuild) { 83 List modifications = timeBuild.getModifications(lastBuild.getTime(), now.getTime()); 84 assertEquals(0, modifications.size()); 85 } 86 87 private TimeBuild createTimeBuildForTime(String userName, String 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 calender2000Previousday = Calendar.getInstance(); 96 calender2000Previousday.set(2002, Calendar.DECEMBER, 22, 20, 00, 00); 97 98 Calendar calender1400 = Calendar.getInstance(); 99 calender1400.set(2002, Calendar.DECEMBER, 23, 14, 00, 00); 100 101 Calendar calender1600 = Calendar.getInstance(); 102 calender1600.set(2002, Calendar.DECEMBER, 23, 16, 00, 00); 103 calender1600.set(Calendar.MILLISECOND, 0); 104 105 Calendar calender1601 = Calendar.getInstance(); 106 calender1601.set(2002, Calendar.DECEMBER, 23, 16, 01, 00); 107 108 Calendar calender1603 = Calendar.getInstance(); 109 calender1603.set(2002, Calendar.DECEMBER, 23, 16, 03, 00); 110 111 TimeBuild timeBuild = new TimeBuild(); 113 timeBuild.setTime("1600"); 114 timeBuild.setUserName("epugh"); 115 116 Date lastBuildEightPMYesterday = calender2000Previousday.getTime(); 117 118 List modifications = timeBuild.getModifications(lastBuildEightPMYesterday, calender1400.getTime()); 120 assertEquals(0, modifications.size()); 121 122 modifications = timeBuild.getModifications(lastBuildEightPMYesterday, calender1600.getTime()); 124 assertEquals(0, modifications.size()); 125 126 modifications = timeBuild.getModifications(lastBuildEightPMYesterday, calender1601.getTime()); 129 assertEquals(1, modifications.size()); 130 assertHasSingleModificationThatMatchesNameAndTime(modifications, "epugh", calender1600.getTime()); 131 132 modifications = timeBuild.getModifications(calender1601.getTime(), calender1603.getTime()); 134 assertEquals(0, modifications.size()); 135 } 136 137 private void assertHasSingleModificationThatMatchesNameAndTime(List modifications, String userName, 138 Date 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 |