1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/test/AbstractTest.java Thu Aug 06 18:34:10 2009 +0200
1.3 @@ -0,0 +1,93 @@
1.4 +/*
1.5 + * StarOffice News Server
1.6 + * see AUTHORS for the list of contributors
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, either version 3 of the License, or
1.11 + * (at your option) any later version.
1.12 + *
1.13 + * This program is distributed in the hope that it will be useful,
1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.16 + * GNU General Public License for more details.
1.17 + *
1.18 + * You should have received a copy of the GNU General Public License
1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
1.20 + */
1.21 +
1.22 +package test;
1.23 +
1.24 +import java.io.BufferedReader;
1.25 +import java.io.File;
1.26 +import java.io.IOException;
1.27 +import java.io.InputStreamReader;
1.28 +import java.io.PrintWriter;
1.29 +import java.net.Socket;
1.30 +import java.net.UnknownHostException;
1.31 +
1.32 +/**
1.33 + * Base class for every test performed by the TestBench.
1.34 + * Connects to a NNTP Server and provides basic methods for sending and
1.35 + * receiving data.
1.36 + * @author Christian Lins
1.37 + * @since sonews/0.5.0
1.38 + */
1.39 +public abstract class AbstractTest
1.40 +{
1.41 +
1.42 + protected static PrintWriter log;
1.43 +
1.44 + static
1.45 + {
1.46 + try
1.47 + {
1.48 + log = new PrintWriter(new File("test.log"));
1.49 + }
1.50 + catch(Exception ex)
1.51 + {
1.52 + ex.printStackTrace();
1.53 + }
1.54 + }
1.55 +
1.56 + protected BufferedReader in;
1.57 + protected PrintWriter out;
1.58 + protected Socket socket;
1.59 +
1.60 + /**
1.61 + * Connects to NNTP Server using for
1.62 + * @param host
1.63 + * @param port
1.64 + * @throws java.io.IOException
1.65 + * @throws java.net.UnknownHostException
1.66 + */
1.67 + public void connect(String host, int port)
1.68 + throws IOException, UnknownHostException
1.69 + {
1.70 + socket = new Socket(host, port);
1.71 + socket.setSoTimeout(10000);
1.72 + this.in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
1.73 + this.out = new PrintWriter(socket.getOutputStream());
1.74 + }
1.75 +
1.76 + protected void println(String line)
1.77 + {
1.78 + this.out.println(line);
1.79 + this.out.flush();
1.80 +
1.81 + log.println(">> " + line);
1.82 + log.flush();
1.83 + }
1.84 +
1.85 + protected String readln()
1.86 + throws IOException
1.87 + {
1.88 + String line = this.in.readLine();
1.89 + log.println("<< " + line);
1.90 + log.flush();
1.91 + return line;
1.92 + }
1.93 +
1.94 + public abstract int runTest() throws Exception;
1.95 +
1.96 +}