test/AbstractTest.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 17 Oct 2011 21:06:24 +0200
changeset 92 a865136070a0
parent 1 6fceb66e1ad7
permissions -rwxr-xr-x
Drupal: text/plain – h2 bude podtřžené = místo # + podpora značky cite.
     1 /*
     2  *   StarOffice News Server
     3  *   see AUTHORS for the list of contributors
     4  *
     5  *   This program is free software: you can redistribute it and/or modify
     6  *   it under the terms of the GNU General Public License as published by
     7  *   the Free Software Foundation, either version 3 of the License, or
     8  *   (at your option) any later version.
     9  *
    10  *   This program is distributed in the hope that it will be useful,
    11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  *   GNU General Public License for more details.
    14  *
    15  *   You should have received a copy of the GNU General Public License
    16  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 
    19 package test;
    20 
    21 import java.io.BufferedReader;
    22 import java.io.File;
    23 import java.io.IOException;
    24 import java.io.InputStreamReader;
    25 import java.io.PrintWriter;
    26 import java.net.Socket;
    27 import java.net.UnknownHostException;
    28 
    29 /**
    30  * Base class for every test performed by the TestBench. 
    31  * Connects to a NNTP Server and provides basic methods for sending and
    32  * receiving data.
    33  * @author Christian Lins
    34  * @since sonews/0.5.0
    35  */
    36 public abstract class AbstractTest
    37 {
    38 
    39   protected static PrintWriter log;
    40   
    41   static
    42   {
    43     try
    44     {
    45       log = new PrintWriter(new File("test.log"));
    46     }
    47     catch(Exception ex)
    48     {
    49       ex.printStackTrace();
    50     }
    51   }
    52   
    53   protected BufferedReader  in;
    54   protected PrintWriter     out;
    55   protected Socket          socket;
    56   
    57   /**
    58    * Connects to NNTP Server using for
    59    * @param host
    60    * @param port
    61    * @throws java.io.IOException
    62    * @throws java.net.UnknownHostException
    63    */
    64   public void connect(String host, int port)
    65     throws IOException, UnknownHostException
    66   {
    67     socket = new Socket(host, port);
    68     socket.setSoTimeout(10000);
    69     this.in  = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    70     this.out = new PrintWriter(socket.getOutputStream());
    71   }
    72   
    73   protected void println(String line)
    74   {
    75     this.out.println(line);
    76     this.out.flush();
    77     
    78     log.println(">> " + line);
    79     log.flush();
    80   }
    81   
    82   protected String readln()
    83     throws IOException
    84   {
    85     String line = this.in.readLine();
    86     log.println("<< " + line);
    87     log.flush();
    88     return line;
    89   }
    90   
    91   public abstract int runTest() throws Exception;
    92   
    93 }