001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.net.ntp; 018 019 import java.util.Date; 020 import java.util.Calendar; 021 import junit.framework.TestCase; 022 import org.apache.commons.net.ntp.TimeStamp; 023 024 /** 025 * Test class that validates assertions for the basic TimeStamp operations and comparisons. 026 * 027 * @author Jason Mathews, MITRE Corp 028 */ 029 public class TimeStampTest extends TestCase { 030 031 private static final String TIME1 = "c1a9ae1c.cf6ac48d"; // Tue, Dec 17 2002 14:07:24.810 UTC 032 private static final String TIME2 = "c1a9ae1c.cf6ac48f"; // Tue, Dec 17 2002 14:07:24.810 UTC 033 private static final String TIME3 = "c1a9ae1d.cf6ac48e"; // Tue, Dec 17 2002 14:07:25.810 UTC 034 035 /*** 036 * main for running the test. 037 ***/ 038 public static void main(String args[]) 039 { 040 junit.textui.TestRunner.run(TimeStampTest.class); 041 } 042 043 public void testCompare() { 044 045 TimeStamp ts1 = new TimeStamp(TIME1); // Tue, Dec 17 2002 14:07:24.810 UTC 046 TimeStamp ts2 = new TimeStamp(TIME1); 047 TimeStamp ts3 = new TimeStamp(TIME2); // Tue, Dec 17 2002 14:07:24.810 UTC 048 TimeStamp ts4 = new TimeStamp(TIME3); // Tue, Dec 17 2002 14:07:25.810 UTC 049 050 // do assertion tests on TimeStamp class 051 assertEquals("equals(1,2)", ts1, ts2); 052 assertTrue("compareTo(1,2)", ts1.compareTo(ts2) == 0); 053 assertEquals("ntpValue(1,2)", ts1.ntpValue(), ts2.ntpValue()); 054 assertEquals("hashCode(1,2)", ts1.hashCode(), ts2.hashCode()); 055 assertEquals("ts1==ts1", ts1, ts1); 056 057 // timestamps in ts1 (TIME1) and ts3 (TIME2) are only off by the smallest 058 // fraction of a second (~200 picoseconds) so the times are not equal but 059 // when converted to Java dates (in milliseconds) they will be equal. 060 assertTrue("ts1 != ts3", !ts1.equals(ts3)); 061 assertTrue("compareTo(1,3)", ts1.compareTo(ts3) == -1); 062 assertEquals("seconds", ts1.getSeconds(), ts3.getSeconds()); 063 assertTrue("fraction", ts1.getFraction() != ts3.getFraction()); 064 assertTrue("ntpValue(1,3)", ts1.ntpValue() != ts3.ntpValue()); 065 assertTrue("hashCode(1,3)", ts1.hashCode() != ts3.hashCode()); 066 long time1 = ts1.getTime(); 067 long time3 = ts3.getTime(); 068 assertEquals("equals(time1,3)", time1, time3); // ntpTime1 != ntpTime3 but JavaTime(t1) == JavaTime(t3)... 069 070 assertTrue("ts3 != ts4", !ts3.equals(ts4)); 071 assertTrue("time3 != ts4.time", time3 != ts4.getTime()); 072 } 073 074 public void testUTCString() { 075 TimeStamp ts1 = new TimeStamp(TIME1); // Tue, Dec 17 2002 14:07:24.810 UTC 076 String actual = ts1.toUTCString(); 077 assertEquals("Tue, Dec 17 2002 14:07:24.810 UTC", actual); 078 } 079 080 public void testDateConversion() { 081 // convert current date to NtpTimeStamp then compare Java date 082 // computed from NTP timestamp with original Java date. 083 Calendar refCal = Calendar.getInstance(java.util.TimeZone.getTimeZone("UTC")); 084 Date refDate = refCal.getTime(); 085 TimeStamp ts = new TimeStamp(refDate); 086 assertEquals("refDate.getTime()", refDate.getTime(), ts.getTime()); 087 Date tsDate = ts.getDate(); 088 assertEquals(refDate, tsDate); 089 } 090 091 }