    1   package org.apache.commons.net.time;
    2
    3   /* ====================================================================
    4    *
    5    * The Apache Software License, Version 1.1
    6    *
    7    * Copyright (c) 2003 The Apache Software Foundation.  All rights
    8    * reserved.
    9    *
    10   * Redistribution and use in source and binary forms, with or without
    11   * modification, are permitted provided that the following conditions
    12   * are met:
    13   *
    14   * 1. Redistributions of source code must retain the above copyright
    15   *    notice, this list of conditions and the following disclaimer.
    16   *
    17   * 2. Redistributions in binary form must reproduce the above copyright
    18   *    notice, this list of conditions and the following disclaimer in
    19   *    the documentation and/or other materials provided with the
    20   *    distribution.
    21   *
    22   * 3. The end-user documentation included with the redistribution, if
    23   *    any, must include the following acknowlegement:
    24   *       "This product includes software developed by the
    25   *        Apache Software Foundation (http://www.apache.org/)."
    26   *    Alternately, this acknowlegement may appear in the software itself,
    27   *    if and wherever such third-party acknowlegements normally appear.
    28   *
    29   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
    30   *    Foundation" must not be used to endorse or promote products derived
    31   *    from this software without prior written permission. For written
    32   *    permission, please contact apache@apache.org.
    33   *
    34   * 5. Products derived from this software may not be called "Apache"
    35   *    nor may "Apache" appear in their names without prior written
    36   *    permission of the Apache Group.
    37   *
    38   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
    39   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    40   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    41   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
    42   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    43   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    44   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
    45   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    46   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    47   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
    48   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    49   * SUCH DAMAGE.
    50   * ====================================================================
    51   *
    52   * This software consists of voluntary contributions made by many
    53   * individuals on behalf of the Apache Software Foundation.  For more
    54   * information on the Apache Software Foundation, please see
    55   * <http://www.apache.org/>.
    56   */
    57
    58  import java.net.InetAddress;
    59  import java.util.Calendar;
    60  import java.io.IOException;
    61  import java.util.TimeZone;
    62
    63  import junit.framework.TestCase;
    64  import org.apache.commons.net.TimeTCPClient;
    65
    66  public class TimeTCPClientTest extends TestCase
    67  {
    68      private TimeTestSimpleServer server1;
    69
    70      private int _port = 3333; // default test port
    71
    72      /****
    73       * main for running the test.
    74       ***/
    75      public static void main(String[] args)
    76      {
    77          junit.textui.TestRunner.run(TimeTCPClientTest.class);
    78      }
    79
    80      /****
    81       * open connections needed for the tests for the test.
    82       ***/
    83      protected void openConnections() throws Exception
    84      {
    85  	try {
    86              server1 = new TimeTestSimpleServer(_port);
    87              server1.connect();
    88  	} catch (IOException ioe)
    89  	{
    90  	    // try again on another port
    91  	    _port = 4000;
    92              server1 = new TimeTestSimpleServer(_port);
    93              server1.connect();
    94  	}
    95          server1.start();
    96      }
    97
    98      /****
    99       *  tests the constant basetime used by TimeClient against tha
    100      *  computed from Calendar class.
    101      */
    102     public void testInitial() {
    103         TimeZone utcZone = TimeZone.getTimeZone("UTC");
    104         Calendar calendar = Calendar.getInstance(utcZone);
    105         calendar.set(1900, Calendar.JANUARY, 1, 0, 0, 0);
    106         calendar.set(Calendar.MILLISECOND, 0);
    107         long baseTime = calendar.getTime().getTime() / 1000L;
    108
    109         assertTrue(baseTime == -TimeTCPClient.SECONDS_1900_TO_1970);
    110     }
    111
    112     /****
    113      * tests the times retrieved via the Time protocol implementation.
    114      ***/
    115     public void testCompareTimes() throws Exception
    116     {
    117         openConnections();
    118
    119         long time, time2;
    120         long clientTime, clientTime2;
    121         try
    122         {
    123             TimeTCPClient client = new TimeTCPClient();
    124             try
    125             {
    126                 // We want to timeout if a response takes longer than 60 seconds
    127                 client.setDefaultTimeout(60000);
    128                 client.connect(InetAddress.getLocalHost(), _port);
    129                 clientTime = client.getDate().getTime();
    130                 time = System.currentTimeMillis();
    131             } finally
    132             {
    133                 client.disconnect();
    134             }
    135
    136             try
    137             {
    138                 // We want to timeout if a response takes longer than 60 seconds
    139                 client.setDefaultTimeout(60000);
    140                 client.connect(InetAddress.getLocalHost(), _port);
    141                 clientTime2 = (client.getTime() - TimeTCPClient.SECONDS_1900_TO_1970)*1000L;
    142                 time2 = System.currentTimeMillis();
    143             } finally
    144             {
    145                 client.disconnect();
    146             }
    147
    148         } finally
    149         {
    150             closeConnections();
    151         }
    152
    153       // current time shouldn't differ from time reported via network by 5 seconds
    154       assertTrue(Math.abs(time - clientTime) < 5000);
    155       assertTrue(Math.abs(time2 - clientTime2) < 5000);
    156     }
    157
    158     /****
    159      * closes all the connections
    160      ***/
    161     protected void closeConnections()
    162     {
    163         try
    164         {
    165             server1.stop();
    166             Thread.sleep(1000);
    167         } catch (Exception e)
    168         {
    169         }
    170     }
    171 }
    172