You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by "Mathews, Jason" <ma...@mitre.org> on 2014/11/08 15:12:47 UTC

[net] Patch: Added NTP Timestamp tests

Hi.



I've committed the NTP implementation to the net-commons project many years ago.



The TimeStamp junit test was far from complete so added a few more tests to cover the bases.



Here's the patch:



Index: src/test/java/org/apache/commons/net/ntp/TimeStampTest.java

===================================================================

--- src/test/java/org/apache/commons/net/ntp/TimeStampTest.java         (revision 1632160)

+++ src/test/java/org/apache/commons/net/ntp/TimeStampTest.java       (working copy)

@@ -18,6 +18,7 @@

import java.util.Date;

import java.util.Calendar;

+import java.util.TimeZone;

import junit.framework.TestCase;

/**

@@ -29,6 +30,26 @@

     private static final String TIME2 = "c1a9ae1c.cf6ac48f";  // Tue, Dec 17 2002 14:07:24.810 UTC

     private static final String TIME3 = "c1a9ae1d.cf6ac48e";  // Tue, Dec 17 2002 14:07:25.810 UTC

+    public void testEquals() {

+        TimeStamp time = TimeStamp.getCurrentTime();

+        TimeStamp time2 = new TimeStamp(time.getDate());

+        assertEquals(time.ntpValue(), time2.ntpValue());

+        assertEquals(time.getSeconds(), time2.getSeconds());

+        assertEquals(time.getFraction(), time2.getFraction());

+        assertEquals(time.hashCode(), time2.hashCode());

+        assertEquals(time, time2);

+    }

+

+    public void testFraction() {

+        TimeStamp time = TimeStamp.getCurrentTime();

+        TimeStamp time2 = new TimeStamp(time.ntpValue());

+        assertEquals(time, time2);

+        long seconds = time.getSeconds();

+        long fraction = time.getFraction();

+        long ntpTime = seconds << 32 | fraction;

+        assertEquals(time.ntpValue(), ntpTime);

+    }

+

     public void testCompare() {

         TimeStamp ts1 = new TimeStamp(TIME1);   // Tue, Dec 17 2002 14:07:24.810 UTC

@@ -60,6 +81,35 @@

         assertTrue("time3 != ts4.time", time3 != ts4.getTime());

     }

+    public void testBaseTimes() {

+        // use base 2036 time

+        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));

+        cal.set(2036, Calendar.MARCH, 1, 0, 0, 0);

+        cal.set(Calendar.MILLISECOND, 1);

+        TimeStamp time = new TimeStamp(cal.getTime());

+        String timeString = time.toString();

+        assertTrue(timeString.startsWith("0"));

+

+        /*

+             Sat, Mar 01 2036 00:00:00.001 UTC 1df78000418937 [001df780.00418937]

+                1963904 . 4294967

+               001df780 . 00418937

+

+             Sat, Mar 01 2014 00:00:00.001 UTC d6bba18000418937 [d6bba180.00418937]

+             3602620800 . 4294967

+               d6bba180 . 00418937

+        */

+

+        cal.set(2014, Calendar.MARCH, 1, 0, 0, 0);

+        TimeStamp timeEarly = new TimeStamp(cal.getTime());

+

+        // time after Feb-2036 uses basis of seconds after 2036 and time before 2036 uses seconds since 1900

+        assertTrue(timeEarly.getSeconds() > time.getSeconds());

+

+        // java time uses same epoch time: ms since 1970

+        assertTrue(timeEarly.getTime() < time.getTime());

+    }

+

     public void testUTCString() {

         TimeStamp ts1 = new TimeStamp(TIME1);   // Tue, Dec 17 2002 14:07:24.810 UTC

     String actual = ts1.toUTCString();

--jason mathews