title: Java Chat Server (Socket) - how to write unit test?
body: <p>I have written a Java Chat Server program. 
  This is a simple standalone program for Server. 
  I have to run this then run Client to get Chat working. 
  What are some possible Unit Test scenarios for the server program? Can anyone show me some example of unit test based on this code? 
  I have never written a unit test code before and I can't really think of what needs to be tested here.. I think testing Connection can be one but what else? (and how to?)</p>
  
  <pre><code>public class SimpleChatServer {
  
      static final Logger logger = LogManager.getLogger(SimpleChatServer.class);      
      ArrayList&lt;PrintWriter&gt; clientOutputStreams;
      private BufferedReader reader;
      private Socket sock;
      private ServerSocket serverSock;
  
      public class ClientHandler implements Runnable{
  
          public ClientHandler(Socket clientSocket){  // Socket Connection
              try {
                  sock = clientSocket;
                  InputStreamReader isReader = new InputStreamReader(sock.getInputStream());
                  reader = new BufferedReader(isReader);
              } catch(Exception ex) {
                  logger.trace(ex);
              }
          }
  
              public void run() {
                  String message;
                  try {
                      while ((message = reader.readLine()) != null) {
                          System.out.println("read " + message);
                          tellEveryone(message);
                      }
                  } catch(Exception ex) {
                      logger.trace(ex);
                  }
              } //close run
  
      } //close ClientHandler
  
          public static void main (String[] args) throws Exception
          {
              new SimpleChatServer().listen();
          }
  
          @SuppressWarnings("resource")
          public void listen()
          {
              clientOutputStreams = new ArrayList&lt;PrintWriter&gt;();
              try {
  
                  ServerSocket serverSock = new ServerSocket(8000); //port number 8000 was used
                  while(true) {
                      Socket clientSocket = serverSock.accept();
                      PrintWriter writer = new PrintWriter(clientSocket.getOutputStream());
                      clientOutputStreams.add(writer);
  
                      Thread t = new Thread(new ClientHandler(clientSocket));
                      t.start();
                  }
              } catch (Exception ex) {
                  logger.trace("Server Error", ex);
              } finally {
                  try
                  {
                      serverSock.close();
                  }
                  catch(Exception e){}
              }
  
          } // close go
  
          public void tellEveryone(String message)
          {
              Iterator&lt;PrintWriter&gt; it = clientOutputStreams.iterator();
              while(it.hasNext()) {
                  try {
                      PrintWriter writer = (PrintWriter) it.next();
                      writer.println(message);
                      writer.flush();
                  } catch (Exception ex) {
                      logger.trace(ex);
                  }
              } // end while
          } // close tellEveryone
  
  }
  </code></pre>