1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.net.telnet;
18
19 import junit.framework.TestCase;
20 import java.io.InputStream;
21 import java.io.OutputStream;
22
23
24
25
26
27
28
29
30 public class TelnetClientFunctionalTest extends TestCase
31 {
32 protected TelnetClient tc1;
33
34
35
36
37 @Override
38 protected void setUp()
39 {
40 tc1 = new TelnetClient();
41 }
42
43
44
45
46
47
48
49
50 public void testFunctionalTest() throws Exception
51 {
52 boolean testresult = false;
53 tc1.connect("rainmaker.wunderground.com", 3000);
54
55 InputStream is = tc1.getInputStream();
56 OutputStream os = tc1.getOutputStream();
57
58 boolean cont = waitForString(is, "Return to continue:", 30000);
59 if (cont)
60 {
61 os.write("\n".getBytes());
62 os.flush();
63 cont = waitForString(is, "city code--", 30000);
64 }
65 if (cont)
66 {
67 os.write("LAX\n".getBytes());
68 os.flush();
69 cont = waitForString(is, "Los Angeles", 30000);
70 }
71 if (cont)
72 {
73 cont = waitForString(is, "X to exit:", 30000);
74 }
75 if (cont)
76 {
77 os.write("X\n".getBytes());
78 os.flush();
79 tc1.disconnect();
80 testresult = true;
81 }
82
83 assertTrue(testresult);
84 }
85
86
87
88
89
90 public boolean waitForString(InputStream is, String end, long timeout) throws Exception
91 {
92 byte buffer[] = new byte[32];
93 long starttime = System.currentTimeMillis();
94
95 String readbytes = "";
96 while((readbytes.indexOf(end) < 0) &&
97 ((System.currentTimeMillis() - starttime) < timeout))
98 {
99 if(is.available() > 0)
100 {
101 int ret_read = is.read(buffer);
102 readbytes = readbytes + new String(buffer, 0, ret_read);
103 }
104 else
105 {
106 Thread.sleep(500);
107 }
108 }
109
110 if(readbytes.indexOf(end) >= 0)
111 {
112 return (true);
113 }
114 else
115 {
116 return (false);
117 }
118 }
119 }