001: package org.apache.commons.net.time;
002:
003: /* ====================================================================
004: *
005: * The Apache Software License, Version 1.1
006: *
007: * Copyright (c) 2003 The Apache Software Foundation. All rights
008: * reserved.
009: *
010: * Redistribution and use in source and binary forms, with or without
011: * modification, are permitted provided that the following conditions
012: * are met:
013: *
014: * 1. Redistributions of source code must retain the above copyright
015: * notice, this list of conditions and the following disclaimer.
016: *
017: * 2. Redistributions in binary form must reproduce the above copyright
018: * notice, this list of conditions and the following disclaimer in
019: * the documentation and/or other materials provided with the
020: * distribution.
021: *
022: * 3. The end-user documentation included with the redistribution, if
023: * any, must include the following acknowlegement:
024: * "This product includes software developed by the
025: * Apache Software Foundation (http://www.apache.org/)."
026: * Alternately, this acknowlegement may appear in the software itself,
027: * if and wherever such third-party acknowlegements normally appear.
028: *
029: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
030: * Foundation" must not be used to endorse or promote products derived
031: * from this software without prior written permission. For written
032: * permission, please contact apache@apache.org.
033: *
034: * 5. Products derived from this software may not be called "Apache"
035: * nor may "Apache" appear in their names without prior written
036: * permission of the Apache Group.
037: *
038: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
039: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
040: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
041: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
042: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
043: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
044: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
045: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
046: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
047: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
048: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
049: * SUCH DAMAGE.
050: * ====================================================================
051: *
052: * This software consists of voluntary contributions made by many
053: * individuals on behalf of the Apache Software Foundation. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: import java.net.InetAddress;
059: import java.util.Calendar;
060: import java.io.IOException;
061: import java.util.TimeZone;
062:
063: import junit.framework.TestCase;
064: import org.apache.commons.net.TimeTCPClient;
065:
066: public class TimeTCPClientTest extends TestCase {
067: private TimeTestSimpleServer server1;
068:
069: private int _port = 3333; // default test port
070:
071: /***
072: * main for running the test.
073: ***/
074: public static void main(String[] args) {
075: junit.textui.TestRunner.run(TimeTCPClientTest.class);
076: }
077:
078: /***
079: * open connections needed for the tests for the test.
080: ***/
081: protected void openConnections() throws Exception {
082: try {
083: server1 = new TimeTestSimpleServer(_port);
084: server1.connect();
085: } catch (IOException ioe) {
086: // try again on another port
087: _port = 4000;
088: server1 = new TimeTestSimpleServer(_port);
089: server1.connect();
090: }
091: server1.start();
092: }
093:
094: /***
095: * tests the constant basetime used by TimeClient against tha
096: * computed from Calendar class.
097: */
098: public void testInitial() {
099: TimeZone utcZone = TimeZone.getTimeZone("UTC");
100: Calendar calendar = Calendar.getInstance(utcZone);
101: calendar.set(1900, Calendar.JANUARY, 1, 0, 0, 0);
102: calendar.set(Calendar.MILLISECOND, 0);
103: long baseTime = calendar.getTime().getTime() / 1000L;
104:
105: assertTrue(baseTime == -TimeTCPClient.SECONDS_1900_TO_1970);
106: }
107:
108: /***
109: * tests the times retrieved via the Time protocol implementation.
110: ***/
111: public void testCompareTimes() throws Exception {
112: openConnections();
113:
114: long time, time2;
115: long clientTime, clientTime2;
116: try {
117: TimeTCPClient client = new TimeTCPClient();
118: try {
119: // We want to timeout if a response takes longer than 60 seconds
120: client.setDefaultTimeout(60000);
121: client.connect(InetAddress.getLocalHost(), _port);
122: clientTime = client.getDate().getTime();
123: time = System.currentTimeMillis();
124: } finally {
125: client.disconnect();
126: }
127:
128: try {
129: // We want to timeout if a response takes longer than 60 seconds
130: client.setDefaultTimeout(60000);
131: client.connect(InetAddress.getLocalHost(), _port);
132: clientTime2 = (client.getTime() - TimeTCPClient.SECONDS_1900_TO_1970) * 1000L;
133: time2 = System.currentTimeMillis();
134: } finally {
135: client.disconnect();
136: }
137:
138: } finally {
139: closeConnections();
140: }
141:
142: // current time shouldn't differ from time reported via network by 5 seconds
143: assertTrue(Math.abs(time - clientTime) < 5000);
144: assertTrue(Math.abs(time2 - clientTime2) < 5000);
145: }
146:
147: /***
148: * closes all the connections
149: ***/
150: protected void closeConnections() {
151: try {
152: server1.stop();
153: Thread.sleep(1000);
154: } catch (Exception e) {
155: }
156: }
157: }
|