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.time;
018
019 import java.net.InetAddress;
020 import java.util.Calendar;
021 import java.io.IOException;
022 import java.util.TimeZone;
023
024 import junit.framework.TestCase;
025
026 public class TimeTCPClientTest extends TestCase
027 {
028 private TimeTestSimpleServer server1;
029
030 private int _port = 3333; // default test port
031
032 /***
033 * main for running the test.
034 ***/
035 public static void main(String[] args)
036 {
037 junit.textui.TestRunner.run(TimeTCPClientTest.class);
038 }
039
040 /***
041 * open connections needed for the tests for the test.
042 ***/
043 protected void openConnections() throws Exception
044 {
045 try {
046 server1 = new TimeTestSimpleServer(_port);
047 server1.connect();
048 } catch (IOException ioe)
049 {
050 // try again on another port
051 _port = 4000;
052 server1 = new TimeTestSimpleServer(_port);
053 server1.connect();
054 }
055 server1.start();
056 }
057
058 /***
059 * tests the constant basetime used by TimeClient against tha
060 * computed from Calendar class.
061 */
062 public void testInitial() {
063 TimeZone utcZone = TimeZone.getTimeZone("UTC");
064 Calendar calendar = Calendar.getInstance(utcZone);
065 calendar.set(1900, Calendar.JANUARY, 1, 0, 0, 0);
066 calendar.set(Calendar.MILLISECOND, 0);
067 long baseTime = calendar.getTime().getTime() / 1000L;
068
069 assertTrue(baseTime == -TimeTCPClient.SECONDS_1900_TO_1970);
070 }
071
072 /***
073 * tests the times retrieved via the Time protocol implementation.
074 ***/
075 public void testCompareTimes() throws Exception
076 {
077 openConnections();
078
079 long time, time2;
080 long clientTime, clientTime2;
081 try
082 {
083 TimeTCPClient client = new TimeTCPClient();
084 try
085 {
086 // We want to timeout if a response takes longer than 60 seconds
087 client.setDefaultTimeout(60000);
088 client.connect(InetAddress.getLocalHost(), _port);
089 clientTime = client.getDate().getTime();
090 time = System.currentTimeMillis();
091 } finally
092 {
093 if(client.isConnected())
094 client.disconnect();
095 }
096
097 try
098 {
099 // We want to timeout if a response takes longer than 60 seconds
100 client.setDefaultTimeout(60000);
101 client.connect(InetAddress.getLocalHost(), _port);
102 clientTime2 = (client.getTime() - TimeTCPClient.SECONDS_1900_TO_1970)*1000L;
103 time2 = System.currentTimeMillis();
104 } finally
105 {
106 if(client.isConnected())
107 client.disconnect();
108 }
109
110 } finally
111 {
112 closeConnections();
113 }
114
115 // current time shouldn't differ from time reported via network by 5 seconds
116 assertTrue(Math.abs(time - clientTime) < 5000);
117 assertTrue(Math.abs(time2 - clientTime2) < 5000);
118 }
119
120 /***
121 * closes all the connections
122 ***/
123 protected void closeConnections()
124 {
125 try
126 {
127 server1.stop();
128 Thread.sleep(1000);
129 } catch (Exception e)
130 {
131 }
132 }
133 }
134