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    *      http://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 java.util.Date;
20  import java.util.Calendar;
21  import junit.framework.TestCase;
22  import org.apache.commons.net.ntp.TimeStamp;
23  
24  /**
25   * Test class that validates assertions for the basic TimeStamp operations and comparisons.
26   *
27   * @author Jason Mathews, MITRE Corp
28   */
29  public class TimeStampTest extends TestCase {
30  
31      private static final String TIME1 = "c1a9ae1c.cf6ac48d";  // Tue, Dec 17 2002 14:07:24.810 UTC
32      private static final String TIME2 = "c1a9ae1c.cf6ac48f";  // Tue, Dec 17 2002 14:07:24.810 UTC
33      private static final String TIME3 = "c1a9ae1d.cf6ac48e";  // Tue, Dec 17 2002 14:07:25.810 UTC
34  
35      /***
36       * main for running the test.
37       ***/
38      public static void main(String args[])
39      {
40          junit.textui.TestRunner.run(TimeStampTest.class);
41      }
42  
43      public void testCompare() {
44  
45          TimeStamp ts1 = new TimeStamp(TIME1);   // Tue, Dec 17 2002 14:07:24.810 UTC
46          TimeStamp ts2 = new TimeStamp(TIME1);
47          TimeStamp ts3 = new TimeStamp(TIME2);   // Tue, Dec 17 2002 14:07:24.810 UTC
48          TimeStamp ts4 = new TimeStamp(TIME3);   // Tue, Dec 17 2002 14:07:25.810 UTC
49  
50          // do assertion tests on TimeStamp class
51          assertEquals("equals(1,2)", ts1, ts2);
52          assertTrue("compareTo(1,2)", ts1.compareTo(ts2) == 0);
53          assertEquals("ntpValue(1,2)", ts1.ntpValue(), ts2.ntpValue());
54          assertEquals("hashCode(1,2)", ts1.hashCode(), ts2.hashCode());
55          assertEquals("ts1==ts1", ts1, ts1);
56  
57      // timestamps in ts1 (TIME1) and ts3 (TIME2) are only off by the smallest
58      // fraction of a second (~200 picoseconds) so the times are not equal but
59      // when converted to Java dates (in milliseconds) they will be equal.
60          assertTrue("ts1 != ts3", !ts1.equals(ts3));
61          assertTrue("compareTo(1,3)", ts1.compareTo(ts3) == -1);
62          assertEquals("seconds", ts1.getSeconds(), ts3.getSeconds());
63          assertTrue("fraction", ts1.getFraction() != ts3.getFraction());
64          assertTrue("ntpValue(1,3)", ts1.ntpValue() != ts3.ntpValue());
65          assertTrue("hashCode(1,3)", ts1.hashCode() != ts3.hashCode());
66          long time1 = ts1.getTime();
67          long time3 = ts3.getTime();
68          assertEquals("equals(time1,3)", time1, time3); // ntpTime1 != ntpTime3 but JavaTime(t1) == JavaTime(t3)...
69  
70          assertTrue("ts3 != ts4", !ts3.equals(ts4));
71          assertTrue("time3 != ts4.time", time3 != ts4.getTime());
72      }
73  
74      public void testUTCString() {
75          TimeStamp ts1 = new TimeStamp(TIME1);   // Tue, Dec 17 2002 14:07:24.810 UTC
76      String actual = ts1.toUTCString();
77      assertEquals("Tue, Dec 17 2002 14:07:24.810 UTC", actual);
78      }
79  
80      public void testDateConversion() {
81      // convert current date to NtpTimeStamp then compare Java date
82      // computed from NTP timestamp with original Java date.
83      Calendar refCal = Calendar.getInstance(java.util.TimeZone.getTimeZone("UTC"));
84      Date refDate = refCal.getTime();
85      TimeStamp ts = new TimeStamp(refDate);
86      assertEquals("refDate.getTime()", refDate.getTime(), ts.getTime());
87      Date tsDate = ts.getDate();
88      assertEquals(refDate, tsDate);
89      }
90  
91  }