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 public static void main(String args[])
38 {
39 junit.textui.TestRunner.run(TelnetClientFunctionalTest.class);
40 }
41
42
43
44
45 @Override
46 protected void setUp()
47 {
48 tc1 = new TelnetClient();
49 }
50
51
52
53
54
55
56
57
58 public void testFunctionalTest() throws Exception
59 {
60 boolean testresult = false;
61 tc1.connect("rainmaker.wunderground.com", 3000);
62
63 InputStream is = tc1.getInputStream();
64 OutputStream os = tc1.getOutputStream();
65
66 boolean cont = waitForString(is, "Return to continue:", 30000);
67 if (cont)
68 {
69 os.write("\n".getBytes());
70 os.flush();
71 cont = waitForString(is, "city code--", 30000);
72 }
73 if (cont)
74 {
75 os.write("LAX\n".getBytes());
76 os.flush();
77 cont = waitForString(is, "Los Angeles", 30000);
78 }
79 if (cont)
80 {
81 cont = waitForString(is, "X to exit:", 30000);
82 }
83 if (cont)
84 {
85 os.write("X\n".getBytes());
86 os.flush();
87 tc1.disconnect();
88 testresult = true;
89 }
90
91 assertTrue(testresult);
92 }
93
94
95
96
97
98 public boolean waitForString(InputStream is, String end, long timeout) throws Exception
99 {
100 byte buffer[] = new byte[32];
101 long starttime = System.currentTimeMillis();
102
103 String readbytes = "";
104 while((readbytes.indexOf(end) < 0) &&
105 ((System.currentTimeMillis() - starttime) < timeout))
106 {
107 if(is.available() > 0)
108 {
109 int ret_read = is.read(buffer);
110 readbytes = readbytes + new String(buffer, 0, ret_read);
111 }
112 else
113 {
114 Thread.sleep(500);
115 }
116 }
117
118 if(readbytes.indexOf(end) >= 0)
119 {
120 return (true);
121 }
122 else
123 {
124 return (false);
125 }
126 }
127 }