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    *      https://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 static org.junit.jupiter.api.Assertions.assertTrue;
20  
21  import java.io.InputStream;
22  import java.io.OutputStream;
23  
24  import org.junit.jupiter.api.BeforeEach;
25  import org.junit.jupiter.api.Test;
26  
27  /**
28   * JUnit functional test for TelnetClient. Connects to the weather forecast service rainmaker.wunderground.com and asks for Los Angeles forecast.
29   */
30  class TelnetClientFunctionalTest {
31      protected TelnetClient tc1;
32  
33      /**
34       * test setUp
35       */
36      @BeforeEach
37      protected void setUp() {
38          tc1 = new TelnetClient();
39      }
40  
41      /*
42       * Do the functional test: - connect to the weather service - press return on the first menu - send LAX on the second menu - send X to exit
43       */
44      @Test
45      void testFunctionalTest() throws Exception {
46          boolean testresult = false;
47          tc1.connect("rainmaker.wunderground.com", 3000);
48  
49          try (InputStream is = tc1.getInputStream(); final OutputStream os = tc1.getOutputStream()) {
50  
51              boolean cont = waitForString(is, "Return to continue:", 30000);
52              if (cont) {
53                  os.write("\n".getBytes());
54                  os.flush();
55                  cont = waitForString(is, "city code--", 30000);
56              }
57              if (cont) {
58                  os.write("LAX\n".getBytes());
59                  os.flush();
60                  cont = waitForString(is, "Los Angeles", 30000);
61              }
62              if (cont) {
63                  cont = waitForString(is, "X to exit:", 30000);
64              }
65              if (cont) {
66                  os.write("X\n".getBytes());
67                  os.flush();
68                  tc1.disconnect();
69                  testresult = true;
70              }
71  
72              assertTrue(testresult);
73          }
74      }
75  
76      /*
77       * Helper method. waits for a string with timeout
78       */
79      public boolean waitForString(final InputStream is, final String end, final long timeout) throws Exception {
80          final byte[] buffer = new byte[32];
81          final long starttime = System.currentTimeMillis();
82  
83          String readbytes = "";
84          while (!readbytes.contains(end) && System.currentTimeMillis() - starttime < timeout) {
85              if (is.available() > 0) {
86                  final int retRead = is.read(buffer);
87                  readbytes += new String(buffer, 0, retRead);
88              } else {
89                  Thread.sleep(500);
90              }
91          }
92  
93          return readbytes.indexOf(end) >= 0;
94      }
95  }