1 package org.apache.commons.net.ntp; 2 3 57 58 import java.util.Date ; 59 import java.util.Calendar ; 60 import junit.framework.TestCase; 61 import org.apache.commons.net.ntp.TimeStamp; 62 63 68 public class TimeStampTest extends TestCase { 69 70 private static final String TIME1 = "c1a9ae1c.cf6ac48d"; private static final String TIME2 = "c1a9ae1c.cf6ac48f"; private static final String TIME3 = "c1a9ae1d.cf6ac48e"; 74 77 public static void main(String args[]) 78 { 79 junit.textui.TestRunner.run(TimeStampTest.class); 80 } 81 82 public void testCompare() { 83 84 TimeStamp ts1 = new TimeStamp(TIME1); TimeStamp ts2 = new TimeStamp(TIME1); 86 TimeStamp ts3 = new TimeStamp(TIME2); TimeStamp ts4 = new TimeStamp(TIME3); 89 assertEquals("equals(1,2)", ts1, ts2); 91 assertTrue("compareTo(1,2)", ts1.compareTo(ts2) == 0); 92 assertEquals("ntpValue(1,2)", ts1.ntpValue(), ts2.ntpValue()); 93 assertEquals("hashCode(1,2)", ts1.hashCode(), ts2.hashCode()); 94 assertEquals("ts1==ts1", ts1, ts1); 95 96 assertTrue("ts1 != ts3", !ts1.equals(ts3)); 100 assertTrue("compareTo(1,3)", ts1.compareTo(ts3) == -1); 101 assertEquals("seconds", ts1.getSeconds(), ts3.getSeconds()); 102 assertTrue("fraction", ts1.getFraction() != ts3.getFraction()); 103 assertTrue("ntpValue(1,3)", ts1.ntpValue() != ts3.ntpValue()); 104 assertTrue("hashCode(1,3)", ts1.hashCode() != ts3.hashCode()); 105 long time1 = ts1.getTime(); 106 long time3 = ts3.getTime(); 107 assertEquals("equals(time1,3)", time1, time3); 109 assertTrue("ts3 != ts4", !ts3.equals(ts4)); 110 assertTrue("time3 != ts4.time", time3 != ts4.getTime()); 111 } 112 113 public void testUTCString() { 114 TimeStamp ts1 = new TimeStamp(TIME1); String actual = ts1.toUTCString(); 116 assertEquals("Tue, Dec 17 2002 14:07:24.810 UTC", actual); 117 } 118 119 public void testDateConversion() { 120 Calendar refCal = Calendar.getInstance(java.util.TimeZone.getTimeZone("UTC")); 123 Date refDate = refCal.getTime(); 124 TimeStamp ts = new TimeStamp(refDate); 125 assertEquals("refDate.getTime()", refDate.getTime(), ts.getTime()); 126 Date tsDate = ts.getDate(); 127 assertEquals(refDate, tsDate); 128 } 129 130 } 131 | Popular Tags |