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    *      https://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          final long time;
60          final long time2;
61          final long clientTime;
62          final long clientTime2;
63          final TimeTCPClient client = new TimeTCPClient();
64          try {
65              // Not sure why code used to use getLocalHost.
66              final InetAddress localHost = InetAddress.getByName("localhost"); // WAS InetAddress.getLocalHost();
67              try {
68                  // We want to timeout if a response takes longer than 60 seconds
69                  client.setDefaultTimeout(60000);
70                  client.connect(localHost, _port);
71                  clientTime = client.getDate().getTime();
72                  time = System.currentTimeMillis();
73              } catch (final IOException e) { // catch the first connect error; assume second will work if this does
74                  fail("IOError <" + e + "> trying to connect to " + localHost + " " + _port);
75                  throw e;
76              } finally {
77                  if (client.isConnected()) {
78                      client.disconnect();
79                  }
80              }
81  
82              try {
83                  // We want to timeout if a response takes longer than 60 seconds
84                  client.setDefaultTimeout(60000);
85                  client.connect(localHost, _port);
86                  clientTime2 = (client.getTime() - TimeTCPClient.SECONDS_1900_TO_1970) * 1000L;
87                  time2 = System.currentTimeMillis();
88              } finally {
89                  if (client.isConnected()) {
90                      client.disconnect();
91                  }
92              }
93          } finally {
94              closeConnections();
95          }
96  
97          // current time shouldn't differ from time reported via network by 5 seconds
98          assertTrue(Math.abs(time - clientTime) < 5000);
99          assertTrue(Math.abs(time2 - clientTime2) < 5000);
100     }
101 
102     /**
103      * Tests the constant basetime used by TimeClient against tha computed from Calendar class.
104      */
105     public void testInitial() {
106         final TimeZone utcZone = TimeZone.getTimeZone("UTC");
107         final Calendar calendar = Calendar.getInstance(utcZone);
108         calendar.set(1900, Calendar.JANUARY, 1, 0, 0, 0);
109         calendar.set(Calendar.MILLISECOND, 0);
110         final long baseTime = calendar.getTime().getTime() / 1000L;
111 
112         assertEquals(baseTime, -TimeTCPClient.SECONDS_1900_TO_1970);
113     }
114 }