Package pdp.scrabble.test.multiplayer

Source Code of pdp.scrabble.test.multiplayer.ClientTest

package pdp.scrabble.test.multiplayer;

import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import pdp.scrabble.Game;
import pdp.scrabble.Language;
import pdp.scrabble.multiplayer.Client;
import pdp.scrabble.multiplayer.Server;
import static org.junit.Assert.fail;
import static pdp.scrabble.Factory.FACTORY;

public class ClientTest {
    private static final String LANG = "English";
    private static final Game GAME = FACTORY.createGame(Language.init(LANG), null);

    public ClientTest() {
    }

    @BeforeClass
    public static void setUpClass() throws Exception {
  GAME.create();
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }

    @Before
    public void setUp() {
    }

    @After
    public void tearDown() {
    }

    @Test
    public void ClientTester() {
  Client client = FACTORY.createClient(null, LANG);

  // Connect without server
  try {
      client.connect(null, "localhost", "1099", "test", LANG);
      fail("Connect to inexistant server not expected");
  }
  catch (RemoteException ex) {
      System.out.println("Can't connect to null server");
  }
  catch (NotBoundException ex) {
      fail("NotBoundException was not expected");
  }
  catch (MalformedURLException ex) {
      fail("MalformedURLException was not expected");
  }

  // sendMessage without server
  try {
      client.sendMessage("Src", "Message");
      fail("Connect to inexistant server not expected");
  }
  catch (NullPointerException ex) {
      System.out.println("NullPointerException expected, can't send to null server");
  }
  catch (RemoteException ex) {
      fail("RemoteException not expected");
  }

  // Test with server
  Server server = FACTORY.createServer(1099, LANG);
  try {
      client.connect(null, "localhost", "1099", "test", LANG);
      System.out.println("Connect to localhost on port 1099");
  }
  catch (RemoteException ex) {
      fail("RemoteException was not expected");
  }
  catch (NotBoundException ex) {
      fail("NotBoundException was not expected");
  }
  catch (MalformedURLException ex) {
      fail("MalformedURLException was not expected");
  }

  // Connect with bad adress
  try {
      client.connect(null, "test", "1099", "test", LANG);
      fail("Connect to bad address not expected");
  }
  catch (RemoteException ex) {
      System.out.println("Can't connect to bad address");
  }
  catch (NotBoundException ex) {
      fail("NotBoundException was not expected");
  }
  catch (MalformedURLException ex) {
      fail("MalformedURLException was not expected");
  }

  // Connect with bad port
  try {
      client.connect(null, "localhost", "22", "test", LANG);
      fail("Connect to localhost on port 22 not expected");
  }
  catch (RemoteException ex) {
      System.out.println("Can't connect on port 22");
  }
  catch (NotBoundException ex) {
      fail("NotBoundException was not expected");
  }
  catch (MalformedURLException ex) {
      fail("MalformedURLException was not expected");
  }

  //50 connections/disconnections
  System.out.println("50 Connections/disconnections...");
  for (int i = 0; i < 50; i++) {
      try {
    client.connect(null, "localhost", "1099", "test" + i, LANG);
    server.disconnect(0);
      }
      catch (RemoteException ex) {
    fail("RemoteException was not expected");
      }
      catch (NotBoundException ex) {
    fail("NotBoundException was not expected");
      }
      catch (MalformedURLException ex) {
    fail("MalformedURLException was not expected");
      }
  }
    }
}
TOP

Related Classes of pdp.scrabble.test.multiplayer.ClientTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.