View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.net.ntp;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  import static org.junit.jupiter.api.Assertions.fail;
23  
24  import java.util.Calendar;
25  import java.util.Date;
26  import java.util.TimeZone;
27  
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Test class that validates assertions for the basic TimeStamp operations and comparisons.
32   */
33  class TimeStampTest {
34  
35      private static final String TIME1 = "c1a9ae1c.cf6ac48d"; // Tue, Dec 17 2002 14:07:24.810 UTC
36      private static final String TIME2 = "c1a9ae1c.cf6ac48f"; // Tue, Dec 17 2002 14:07:24.810 UTC
37      private static final String TIME3 = "c1a9ae1d.cf6ac48e"; // Tue, Dec 17 2002 14:07:25.810 UTC
38  
39      @Test
40      void testCompare() {
41  
42          final TimeStamp ts1 = new TimeStamp(TIME1); // Tue, Dec 17 2002 14:07:24.810 UTC
43          final TimeStamp ts2 = new TimeStamp(TIME1);
44          final TimeStamp ts3 = new TimeStamp(TIME2); // Tue, Dec 17 2002 14:07:24.810 UTC
45          final TimeStamp ts4 = new TimeStamp(TIME3); // Tue, Dec 17 2002 14:07:25.810 UTC
46  
47          // do assertion tests on TimeStamp class
48          assertEquals(ts1, ts2, "equals(1,2)");
49          assertEquals(0, ts1.compareTo(ts2), "compareTo(1,2)");
50          assertEquals(ts1.ntpValue(), ts2.ntpValue(), "ntpValue(1,2)");
51          assertEquals(ts1.hashCode(), ts2.hashCode(), "hashCode(1,2)");
52          assertEquals(ts1, ts1, "ts1==ts1");
53  
54          // timestamps in ts1 (TIME1) and ts3 (TIME2) are only off by the smallest
55          // fraction of a second (~200 picoseconds) so the times are not equal but
56          // when converted to Java dates (in milliseconds) they will be equal.
57          assertFalse(ts1.equals(ts3), "ts1 != ts3");
58          assertEquals(-1, ts1.compareTo(ts3), "compareTo(1,3)");
59          assertEquals(ts1.getSeconds(), ts3.getSeconds(), "seconds");
60          assertTrue(ts1.getFraction() != ts3.getFraction(), "fraction");
61          assertTrue(ts1.ntpValue() != ts3.ntpValue(), "ntpValue(1,3)");
62          assertTrue(ts1.hashCode() != ts3.hashCode(), "hashCode(1,3)");
63          final long time1 = ts1.getTime();
64          final long time3 = ts3.getTime();
65          assertEquals(time1, time3, "equals(time1,3)"); // ntpTime1 != ntpTime3 but JavaTime(t1) == JavaTime(t3)...
66  
67          assertFalse(ts3.equals(ts4), "ts3 != ts4");
68          assertTrue(time3 != ts4.getTime(), "time3 != ts4.time");
69      }
70  
71      @Test
72      void testDateConversion() {
73          // convert current date to NtpTimeStamp then compare Java date
74          // computed from NTP timestamp with original Java date.
75          final Calendar refCal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
76          final Date refDate = refCal.getTime();
77          final TimeStamp ts = new TimeStamp(refDate);
78          assertEquals(refDate.getTime(), ts.getTime(), "refDate.getTime()");
79          final Date tsDate = ts.getDate();
80          assertEquals(refDate, tsDate);
81      }
82  
83      @Test
84      void testNotSame() {
85          final TimeStamp time = TimeStamp.getCurrentTime();
86          Object other = Integer.valueOf(0);
87          if (time.equals(other)) {
88              fail("TimeStamp cannot equal Date");
89          }
90          other = null;
91          if (time.equals(other)) {
92              fail("TimeStamp cannot equal null");
93          }
94      }
95  
96      @Test
97      void testUTCString() {
98          final TimeStamp ts1 = new TimeStamp(TIME1); // Tue, Dec 17 2002 14:07:24.810 UTC
99          final String actual = ts1.toUTCString();
100         assertEquals("Tue, Dec 17 2002 14:07:24.810 UTC", actual);
101     }
102 
103 }