1 37 package net.sourceforge.cruisecontrol.util; 38 39 import junit.framework.TestCase; 40 import java.util.Calendar ; 41 42 45 public class DateUtilTest extends TestCase { 46 47 private Calendar cal; 48 49 private Calendar cal2; 50 51 public void setUp() { 52 cal = Calendar.getInstance(); 54 cal.set(2001, Calendar.NOVEMBER, 22, 10, 01, 01); 55 cal2 = Calendar.getInstance(); 56 cal2.set(2001, Calendar.NOVEMBER, 22, 11, 01, 01); 57 } 58 59 public void testGetTimeFromDate() { 60 assertEquals(DateUtil.getTimeFromDate(cal.getTime()), 1001); 61 assertEquals(DateUtil.getTimeFromDate(cal2.getTime()), 1101); 62 } 63 64 public void testMilliTimeDifference() { 65 int noon = 1200; 66 int elevenThirty = 1130; 67 int oneFifteen = 1315; 68 69 long thirtyMinutes = 30 * 60 * 1000; 70 long hourFifteenMinutes = (60 + 15) * 60 * 1000; 71 long hourFortyFiveMinutes = (60 + 45) * 60 * 1000; 72 73 assertEquals(thirtyMinutes, DateUtil.milliTimeDifference(elevenThirty, noon)); 74 assertEquals(hourFifteenMinutes, DateUtil.milliTimeDifference(noon, oneFifteen)); 75 assertEquals(hourFortyFiveMinutes, DateUtil.milliTimeDifference(elevenThirty, oneFifteen)); 76 } 77 78 public void testConvertToMillis() { 79 int noon = 1200; 80 int oneAM = 100; 81 int elevenFifteenPM = 2315; 82 83 long noonMillis = 12 * 60 * 60 * 1000; 84 long oneAMmillis = 1 * 60 * 60 * 1000; 85 long elevenFifteenPMmillis = (23 * 60 + 15) * 60 * 1000; 86 87 assertEquals(noonMillis, DateUtil.convertToMillis(noon)); 88 assertEquals(oneAMmillis, DateUtil.convertToMillis(oneAM)); 89 assertEquals(elevenFifteenPMmillis, DateUtil.convertToMillis(elevenFifteenPM)); 90 } 91 92 public void testFormatTime() { 93 long fiveSeconds = 5 * 1000; 94 long oneHour = 60 * DateUtil.ONE_MINUTE; 95 long oneHourFiftyNineMinutes = 2 * oneHour - DateUtil.ONE_MINUTE; 96 97 String seconds = "5 seconds"; 98 String hoursMinutesSeconds = "1 hours 59 minutes 5 seconds"; 99 String negativeTime = "-1 hours -59 minutes -5 seconds"; 100 101 assertEquals(seconds, DateUtil.formatTime(fiveSeconds)); 102 assertEquals(hoursMinutesSeconds, DateUtil.formatTime(oneHourFiftyNineMinutes + fiveSeconds)); 103 assertEquals(negativeTime, DateUtil.formatTime(-1 * (oneHourFiftyNineMinutes + fiveSeconds))); 104 } 105 106 public void testGetBuildTimeAsString() { 107 int minutes = 50; 108 int seconds = 30; 109 int millis = 123; 110 long timeInMillis = millis + seconds * 1000 + minutes * 60 * 1000; 111 assertEquals("50 minute(s) 30 second(s)", 112 DateUtil.getDurationAsString(timeInMillis)); 113 } 114 } 115 | Popular Tags |