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.net.InetAddress;
20  import java.util.Calendar;
21  import java.io.IOException;
22  import java.util.TimeZone;
23  
24  import junit.framework.TestCase;
25  
26  public class TimeTCPClientTest extends TestCase
27  {
28      private TimeTestSimpleServer server1;
29  
30      private int _port = 3333; // default test port
31  
32      /***
33       * main for running the test.
34       ***/
35      public static void main(String[] args)
36      {
37          junit.textui.TestRunner.run(TimeTCPClientTest.class);
38      }
39  
40      /***
41       * open connections needed for the tests for the test.
42       ***/
43      protected void openConnections() throws Exception
44      {
45      try {
46              server1 = new TimeTestSimpleServer(_port);
47              server1.connect();
48      } catch (IOException ioe)
49      {
50          // try again on another port
51          _port = 4000;
52              server1 = new TimeTestSimpleServer(_port);
53              server1.connect();
54      }
55          server1.start();
56      }
57  
58      /***
59       *  tests the constant basetime used by TimeClient against tha
60       *  computed from Calendar class.
61       */
62      public void testInitial() {
63          TimeZone utcZone = TimeZone.getTimeZone("UTC");
64          Calendar calendar = Calendar.getInstance(utcZone);
65          calendar.set(1900, Calendar.JANUARY, 1, 0, 0, 0);
66          calendar.set(Calendar.MILLISECOND, 0);
67          long baseTime = calendar.getTime().getTime() / 1000L;
68  
69          assertTrue(baseTime == -TimeTCPClient.SECONDS_1900_TO_1970);
70      }
71  
72      /***
73       * tests the times retrieved via the Time protocol implementation.
74       ***/
75      public void testCompareTimes() throws Exception
76      {
77          openConnections();
78  
79          long time, time2;
80          long clientTime, clientTime2;
81          try
82          {
83              TimeTCPClient client = new TimeTCPClient();
84              try
85              {
86                  // We want to timeout if a response takes longer than 60 seconds
87                  client.setDefaultTimeout(60000);
88                  client.connect(InetAddress.getLocalHost(), _port);
89                  clientTime = client.getDate().getTime();
90                  time = System.currentTimeMillis();
91              } finally
92              {
93                if(client.isConnected())
94                  client.disconnect();
95              }
96  
97              try
98              {
99                  // 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