001: /*
002: * Copyright 2003-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.net.telnet;
017:
018: import junit.framework.TestCase;
019: import java.io.InputStream;
020: import java.io.OutputStream;
021:
022: /***
023: * JUnit functional test for TelnetClient.
024: * Connects to the weather forecast service
025: * rainmaker.wunderground.com and asks for Los Angeles forecast.
026: * <p>
027: * @author Bruno D'Avanzo
028: ***/
029: public class TelnetClientFunctionalTest extends TestCase {
030: protected TelnetClient tc1;
031:
032: /***
033: * main for running the test.
034: ***/
035: public static void main(String args[]) {
036: junit.textui.TestRunner.run(TelnetClientFunctionalTest.class);
037: }
038:
039: /***
040: * test setUp
041: ***/
042: protected void setUp() {
043: tc1 = new TelnetClient();
044: }
045:
046: /***
047: * Do the functional test:
048: * - connect to the weather service
049: * - press return on the first menu
050: * - send LAX on the second menu
051: * - send X to exit
052: ***/
053: public void testFunctionalTest() throws Exception {
054: boolean testresult = false;
055: tc1.connect("rainmaker.wunderground.com", 3000);
056:
057: InputStream is = tc1.getInputStream();
058: OutputStream os = tc1.getOutputStream();
059:
060: boolean cont = waitForString(is, "Return to continue:", 30000);
061: if (cont) {
062: os.write("\n".getBytes());
063: os.flush();
064: cont = waitForString(is, "city code--", 30000);
065: }
066: if (cont) {
067: os.write("LAX\n".getBytes());
068: os.flush();
069: cont = waitForString(is, "Los Angeles", 30000);
070: }
071: if (cont) {
072: cont = waitForString(is, "X to exit:", 30000);
073: }
074: if (cont) {
075: os.write("X\n".getBytes());
076: os.flush();
077: tc1.disconnect();
078: testresult = true;
079: }
080:
081: assertTrue(testresult);
082: }
083:
084: /***
085: * Helper method. waits for a string with timeout
086: ***/
087: public boolean waitForString(InputStream is, String end,
088: long timeout) throws Exception {
089: byte buffer[] = new byte[32];
090: long starttime = System.currentTimeMillis();
091:
092: String readbytes = new String();
093: while ((readbytes.indexOf(end) < 0)
094: && ((System.currentTimeMillis() - starttime) < timeout)) {
095: if (is.available() > 0) {
096: int ret_read = is.read(buffer);
097: readbytes = readbytes + new String(buffer, 0, ret_read);
098: } else {
099: Thread.sleep(500);
100: }
101: }
102:
103: if (readbytes.indexOf(end) >= 0) {
104: return (true);
105: } else {
106: return (false);
107: }
108: }
109: }
|