1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * JUnit functional test for TelnetClient.
25   * Connects to the weather forecast service
26   * rainmaker.wunderground.com and asks for Los Angeles forecast.
27   * <p>
28   * @author Bruno D'Avanzo
29   ***/
30  public class TelnetClientFunctionalTest extends TestCase
31  {
32      protected TelnetClient tc1;
33  
34      /***
35       * main for running the test.
36       ***/
37      public static void main(String args[])
38      {
39          junit.textui.TestRunner.run(TelnetClientFunctionalTest.class);
40      }
41  
42      /***
43       * test setUp
44       ***/
45      @Override
46      protected void setUp()
47      {
48          tc1 = new TelnetClient();
49      }
50  
51      /***
52       * Do the functional test:
53       * - connect to the weather service
54       * - press return on the first menu
55       * - send LAX on the second menu
56       * - send X to exit
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       * Helper method. waits for a string with timeout
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 }