View Javadoc

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       * test setUp
36       ***/
37      @Override
38      protected void setUp()
39      {
40          tc1 = new TelnetClient();
41      }
42  
43      /***
44       * Do the functional test:
45       * - connect to the weather service
46       * - press return on the first menu
47       * - send LAX on the second menu
48       * - send X to exit
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       * Helper method. waits for a string with timeout
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 }