FileDocCategorySizeDatePackage
TelnetClientFunctionalTest.javaAPI DocApache Commons NET 1.4.1 API3327Sat Dec 03 10:05:50 GMT 2005org.apache.commons.net.telnet

TelnetClientFunctionalTest

public class TelnetClientFunctionalTest extends TestCase
JUnit functional test for TelnetClient. Connects to the weather forecast service rainmaker.wunderground.com and asks for Los Angeles forecast.

author
Bruno D'Avanzo

Fields Summary
protected TelnetClient
tc1
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)
main for running the test.

        junit.textui.TestRunner.run(TelnetClientFunctionalTest.class);
    
protected voidsetUp()
test setUp

        tc1 = new TelnetClient();
    
public voidtestFunctionalTest()
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

        boolean testresult = false;
        tc1.connect("rainmaker.wunderground.com", 3000);

        InputStream is = tc1.getInputStream();
        OutputStream os = tc1.getOutputStream();

        boolean cont = waitForString(is, "Return to continue:", 30000);
        if (cont)
        {
            os.write("\n".getBytes());
            os.flush();
            cont = waitForString(is, "city code--", 30000);
        }
        if (cont)
        {
            os.write("LAX\n".getBytes());
            os.flush();
            cont = waitForString(is, "Los Angeles", 30000);
        }
        if (cont)
        {
            cont = waitForString(is, "X to exit:", 30000);
        }
        if (cont)
        {
            os.write("X\n".getBytes());
            os.flush();
            tc1.disconnect();
            testresult = true;
        }

        assertTrue(testresult);
    
public booleanwaitForString(java.io.InputStream is, java.lang.String end, long timeout)
Helper method. waits for a string with timeout

        byte buffer[] = new byte[32];
        long starttime = System.currentTimeMillis();

        String readbytes = new String();
        while((readbytes.indexOf(end) < 0) &&
              ((System.currentTimeMillis() - starttime) < timeout))
        {
            if(is.available() > 0)
            {
                int ret_read = is.read(buffer);
                readbytes = readbytes + new String(buffer, 0, ret_read);
            }
            else
            {
                Thread.sleep(500);
            }
        }

        if(readbytes.indexOf(end) >= 0)
        {
            return (true);
        }
        else
        {
            return (false);
        }