1 package org.apache.commons.net.time; 2 3 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; 72 75 public static void main(String [] args) 76 { 77 junit.textui.TestRunner.run(TimeTCPClientTest.class); 78 } 79 80 83 protected void openConnections() throws Exception 84 { 85 try { 86 server1 = new TimeTestSimpleServer(_port); 87 server1.connect(); 88 } catch (IOException ioe) 89 { 90 _port = 4000; 92 server1 = new TimeTestSimpleServer(_port); 93 server1.connect(); 94 } 95 server1.start(); 96 } 97 98 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 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 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 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 assertTrue(Math.abs(time - clientTime) < 5000); 155 assertTrue(Math.abs(time2 - clientTime2) < 5000); 156 } 157 158 161 protected void closeConnections() 162 { 163 try 164 { 165 server1.stop(); 166 Thread.sleep(1000); 167 } catch (Exception e) 168 { 169 } 170 } 171 } 172 173 | Popular Tags |