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