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.time;
18  
19  import java.io.IOException;
20  import java.net.InetAddress;
21  import java.util.Calendar;
22  import java.util.TimeZone;
23  
24  import junit.framework.TestCase;
25  
26  public class TimeTCPClientTest extends TestCase {
27      private TimeTestSimpleServer server1;
28  
29      private int _port = 3333; // default test port
30  
31      protected void closeConnections() {
32          try {
33              server1.stop();
34              Thread.sleep(1000);
35          } catch (final Exception e) {
36              // ignored
37          }
38      }
39  
40      protected void openConnections() throws Exception {
41          try {
42              server1 = new TimeTestSimpleServer(_port);
43              server1.connect();
44          } catch (final IOException ioe) {
45              // try again on another port
46              _port = 4000;
47              server1 = new TimeTestSimpleServer(_port);
48              server1.connect();
49          }
50          server1.start();
51      }
52  
53      /*
54       * tests the times retrieved via the Time protocol implementation.
55       */
56      public void testCompareTimes() throws Exception {
57          openConnections();
58  
59          long time, time2;
60          long clientTime, clientTime2;
61          final TimeTCPClient client = new TimeTCPClient();
62          try {
63              // Not sure why code used to use getLocalHost.
64              final InetAddress localHost = InetAddress.getByName("localhost"); // WAS InetAddress.getLocalHost();
65              try {
66                  // We want to timeout if a response takes longer than 60 seconds
67                  client.setDefaultTimeout(60000);
68                  client.connect(localHost, _port);
69                  clientTime = client.getDate().getTime();
70                  time = System.currentTimeMillis();
71              } catch (final IOException e) { // catch the first connect error; assume second will work if this does
72                  fail("IOError <" + e + "> trying to connect to " + localHost + " " + _port);
73                  throw e;
74              } finally {
75                  if (client.isConnected()) {
76                      client.disconnect();
77                  }
78              }
79  
80              try {
81                  // We want to timeout if a response takes longer than 60 seconds
82                  client.setDefaultTimeout(60000);
83                  client.connect(localHost, _port);
84                  clientTime2 = (client.getTime() - TimeTCPClient.SECONDS_1900_TO_1970) * 1000L;
85                  time2 = System.currentTimeMillis();
86              } finally {
87                  if (client.isConnected()) {
88                      client.disconnect();
89                  }
90              }
91          } finally {
92              closeConnections();
93          }
94  
95          // current time shouldn't differ from time reported via network by 5 seconds
96          assertTrue(Math.abs(time - clientTime) < 5000);
97          assertTrue(Math.abs(time2 - clientTime2) < 5000);
98      }
99  
100     /*
101      * tests the constant basetime used by TimeClient against tha computed from Calendar class.
102      */
103     public void testInitial() {
104         final TimeZone utcZone = TimeZone.getTimeZone("UTC");
105         final Calendar calendar = Calendar.getInstance(utcZone);
106         calendar.set(1900, Calendar.JANUARY, 1, 0, 0, 0);
107         calendar.set(Calendar.MILLISECOND, 0);
108         final long baseTime = calendar.getTime().getTime() / 1000L;
109 
110         assertEquals(baseTime, -TimeTCPClient.SECONDS_1900_TO_1970);
111     }
112 }