FileDocCategorySizeDatePackage
TimeTCPClientTest.javaAPI DocApache Commons NET 1.4.1 API6021Sat Dec 03 10:05:48 GMT 2005org.apache.commons.net.time

TimeTCPClientTest

public class TimeTCPClientTest extends TestCase

Fields Summary
private TimeTestSimpleServer
server1
private int
_port
Constructors Summary
Methods Summary
protected voidcloseConnections()
closes all the connections

        try
        {
            server1.stop();
            Thread.sleep(1000);
        } catch (Exception e)
        {
        }
    
public static voidmain(java.lang.String[] args)
main for running the test.

 // default test port

              
        
    
        junit.textui.TestRunner.run(TimeTCPClientTest.class);
    
protected voidopenConnections()
open connections needed for the tests for the test.

	try {
            server1 = new TimeTestSimpleServer(_port);
            server1.connect();
	} catch (IOException ioe)
	{
	    // try again on another port
	    _port = 4000;
            server1 = new TimeTestSimpleServer(_port);
            server1.connect();
	}
        server1.start();
    
public voidtestCompareTimes()
tests the times retrieved via the Time protocol implementation.

        openConnections();

        long time, time2;
        long clientTime, clientTime2;
        try
        {
            TimeTCPClient client = new TimeTCPClient();
            try
            {
                // We want to timeout if a response takes longer than 60 seconds
                client.setDefaultTimeout(60000);
                client.connect(InetAddress.getLocalHost(), _port);
                clientTime = client.getDate().getTime();
                time = System.currentTimeMillis();
            } finally
            {
                client.disconnect();
            }

            try
            {
                // We want to timeout if a response takes longer than 60 seconds
                client.setDefaultTimeout(60000);
                client.connect(InetAddress.getLocalHost(), _port);
                clientTime2 = (client.getTime() - TimeTCPClient.SECONDS_1900_TO_1970)*1000L;
                time2 = System.currentTimeMillis();
            } finally
            {
                client.disconnect();
            }

        } finally
        {
            closeConnections();
        }

      // current time shouldn't differ from time reported via network by 5 seconds
      assertTrue(Math.abs(time - clientTime) < 5000);
      assertTrue(Math.abs(time2 - clientTime2) < 5000);
    
public voidtestInitial()
tests the constant basetime used by TimeClient against tha computed from Calendar class.

        TimeZone utcZone = TimeZone.getTimeZone("UTC");
        Calendar calendar = Calendar.getInstance(utcZone);
        calendar.set(1900, Calendar.JANUARY, 1, 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        long baseTime = calendar.getTime().getTime() / 1000L;

        assertTrue(baseTime == -TimeTCPClient.SECONDS_1900_TO_1970);