001: /*
002: * This file is part of the QuickServer library
003: * Copyright (C) 2003-2005 QuickServer.org
004: *
005: * Use, modification, copying and distribution of this software is subject to
006: * the terms and conditions of the GNU Lesser General Public License.
007: * You should have received a copy of the GNU LGP License along with this
008: * library; if not, you can download a copy from <http://www.quickserver.org/>.
009: *
010: * For questions, suggestions, bug-reports, enhancement-requests etc.
011: * visit http://www.quickserver.org
012: *
013: */
014:
015: package echoserver;
016:
017: import java.io.*;
018: import java.net.*;
019:
020: /**
021: * EchoServer - readLine() testing under non-blocking mode.
022: * @author Akshathkumar Shetty
023: */
024: public class TestEchoServerReadLine {
025: private Socket socket;
026: private BufferedReader in;
027: private PrintWriter out;
028:
029: public boolean testEchoServer(String host, int port, String newLine) {
030: try {
031: socket = new Socket(host, port);
032: in = new BufferedReader(new InputStreamReader(socket
033: .getInputStream()));
034: out = new PrintWriter(new BufferedWriter(
035: new OutputStreamWriter(socket.getOutputStream())));
036:
037: //banner
038: for (int i = 0; i < 5; i++)
039: in.readLine();
040:
041: //username
042: in.readLine();
043: out.print("user");
044: out.print(newLine);
045: out.flush();
046:
047: //password
048: in.readLine();
049: out.print("user1");
050: out.flush();
051: Thread.sleep(2000);
052: out.print(newLine);
053: out.flush();
054:
055: //Auth Failed
056: in.readLine();
057:
058: //username
059: in.readLine();
060: out.print("user");
061: out.print(newLine);
062: out.flush();
063:
064: //password
065: in.readLine();
066: out.print("user");
067: Thread.sleep(200);
068: out.print(newLine);
069: out.flush();
070:
071: //ok
072: in.readLine();
073:
074: out.print("test string");
075: out.flush();
076: Thread.sleep(6000);
077: out.print(newLine);
078: out.flush();
079: String data = in.readLine();
080: if (data.equals("Echo : test string") == false)
081: System.err.println("Not equals!");
082:
083: out.print("test string1");
084: out.print(newLine);
085: out.print("test string2");
086: out.print(newLine);
087: out.flush();
088: data = in.readLine();
089: if (data.equals("Echo : test string1") == false)
090: System.err.println("Not equals=1!");
091: data = in.readLine();
092: if (data.equals("Echo : test string2") == false)
093: System.err.println("Not equals=2!");
094:
095: //quit
096: out.print("quit");
097: out.print(newLine);
098: out.flush();
099: data = in.readLine();
100: if (data.equals("Bye ;-)") == false)
101: System.err.println("bye not got!");
102:
103: return true;
104: } catch (Exception e) {
105: System.err.println("Socket failed : " + e);
106: return false;
107: } finally {
108: try {
109: if (socket != null)
110: socket.close();
111: } catch (IOException e) {
112: System.err.println("Socket not closed: " + e);
113: }
114: }
115: }
116:
117: public static void main(String[] args) throws Exception {
118: TestEchoServerReadLine main = new TestEchoServerReadLine();
119:
120: int port = 4123;
121: String host = "localhost";
122:
123: if (args.length > 0) {
124: try {
125: port = Integer.parseInt(args[0]);
126: } catch (NumberFormatException nfe) {
127: }
128: }
129:
130: if (args.length >= 2) {
131: host = args[1];
132: }
133:
134: System.out.print("\\r\\n Test: ");
135: System.out.println(main.testEchoServer(host, port, "\r\n"));
136: Thread.sleep(30000);
137:
138: System.out.print("\\n Test: ");
139: System.out.println(main.testEchoServer(host, port, "\n"));
140:
141: Thread.sleep(30000);
142: System.out.println("\\r Test: ");
143: System.out.println(main.testEchoServer(host, port, "\r"));
144: }
145:
146: }
|