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.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       * open connections needed for the tests for the test.
34       ***/
35      protected void openConnections() throws Exception
36      {
37      try {
38              server1 = new TimeTestSimpleServer(_port);
39              server1.connect();
40      } catch (IOException ioe)
41      {
42          // try again on another port
43          _port = 4000;
44              server1 = new TimeTestSimpleServer(_port);
45              server1.connect();
46      }
47          server1.start();
48      }
49  
50      /***
51       *  tests the constant basetime used by TimeClient against tha
52       *  computed from Calendar class.
53       */
54      public void testInitial() {
55          TimeZone utcZone = TimeZone.getTimeZone("UTC");
56          Calendar calendar = Calendar.getInstance(utcZone);
57          calendar.set(1900, Calendar.JANUARY, 1, 0, 0, 0);
58          calendar.set(Calendar.MILLISECOND, 0);
59          long baseTime = calendar.getTime().getTime() / 1000L;
60  
61          assertEquals(baseTime, -TimeTCPClient.SECONDS_1900_TO_1970);
62      }
63  
64      /***
65       * tests the times retrieved via the Time protocol implementation.
66       ***/
67      public void testCompareTimes() throws Exception
68      {
69          openConnections();
70  
71          long time, time2;
72          long clientTime, clientTime2;
73          try
74          {
75              TimeTCPClient client = new TimeTCPClient();
76              try
77              {
78                  // We want to timeout if a response takes longer than 60 seconds
79                  client.setDefaultTimeout(60000);
80                  client.connect(InetAddress.getLocalHost(), _port);
81                  clientTime = client.getDate().getTime();
82                  time = System.currentTimeMillis();
83              } finally
84              {
85                if(client.isConnected()) {
86                    client.disconnect();
87                }
88              }
89  
90              try
91              {
92                  // We want to timeout if a response takes longer than 60 seconds
93                  client.setDefaultTimeout(60000);
94                  client.connect(InetAddress.getLocalHost(), _port);
95                  clientTime2 = (client.getTime() - TimeTCPClient.SECONDS_1900_TO_1970)*1000L;
96                  time2 = System.currentTimeMillis();
97              } finally
98              {
99                if(client.isConnected()) {
100                   client.disconnect();
101               }
102             }
103 
104         } finally
105         {
106             closeConnections();
107         }
108 
109       // current time shouldn't differ from time reported via network by 5 seconds
110       assertTrue(Math.abs(time - clientTime) < 5000);
111       assertTrue(Math.abs(time2 - clientTime2) < 5000);
112     }
113 
114     /***
115      * closes all the connections
116      ***/
117     protected void closeConnections()
118     {
119         try
120         {
121             server1.stop();
122             Thread.sleep(1000);
123         } catch (Exception e)
124         {
125         }
126     }
127 }
128