1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.net.ntp;
18
19 import java.util.Calendar;
20 import java.util.Date;
21 import java.util.TimeZone;
22
23 import junit.framework.TestCase;
24
25
26
27
28 public class TimeStampTest extends TestCase {
29
30 private static final String TIME1 = "c1a9ae1c.cf6ac48d";
31 private static final String TIME2 = "c1a9ae1c.cf6ac48f";
32 private static final String TIME3 = "c1a9ae1d.cf6ac48e";
33
34 public void testCompare() {
35
36 final TimeStamp ts1 = new TimeStamp(TIME1);
37 final TimeStamp ts2 = new TimeStamp(TIME1);
38 final TimeStamp ts3 = new TimeStamp(TIME2);
39 final TimeStamp ts4 = new TimeStamp(TIME3);
40
41
42 assertEquals("equals(1,2)", ts1, ts2);
43 assertEquals("compareTo(1,2)", 0, ts1.compareTo(ts2));
44 assertEquals("ntpValue(1,2)", ts1.ntpValue(), ts2.ntpValue());
45 assertEquals("hashCode(1,2)", ts1.hashCode(), ts2.hashCode());
46 assertEquals("ts1==ts1", ts1, ts1);
47
48
49
50
51 assertFalse("ts1 != ts3", ts1.equals(ts3));
52 assertEquals("compareTo(1,3)", -1, ts1.compareTo(ts3));
53 assertEquals("seconds", ts1.getSeconds(), ts3.getSeconds());
54 assertTrue("fraction", ts1.getFraction() != ts3.getFraction());
55 assertTrue("ntpValue(1,3)", ts1.ntpValue() != ts3.ntpValue());
56 assertTrue("hashCode(1,3)", ts1.hashCode() != ts3.hashCode());
57 final long time1 = ts1.getTime();
58 final long time3 = ts3.getTime();
59 assertEquals("equals(time1,3)", time1, time3);
60
61 assertFalse("ts3 != ts4", ts3.equals(ts4));
62 assertTrue("time3 != ts4.time", time3 != ts4.getTime());
63 }
64
65 public void testDateConversion() {
66
67
68 final Calendar refCal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
69 final Date refDate = refCal.getTime();
70 final TimeStamp ts = new TimeStamp(refDate);
71 assertEquals("refDate.getTime()", refDate.getTime(), ts.getTime());
72 final Date tsDate = ts.getDate();
73 assertEquals(refDate, tsDate);
74 }
75
76 public void testNotSame() {
77 final TimeStamp time = TimeStamp.getCurrentTime();
78 Object other = Integer.valueOf(0);
79 if (time.equals(other)) {
80 fail("TimeStamp cannot equal Date");
81 }
82 other = null;
83 if (time.equals(other)) {
84 fail("TimeStamp cannot equal null");
85 }
86 }
87
88 public void testUTCString() {
89 final TimeStamp ts1 = new TimeStamp(TIME1);
90 final String actual = ts1.toUTCString();
91 assertEquals("Tue, Dec 17 2002 14:07:24.810 UTC", actual);
92 }
93
94 }