I have the following 2 files:
---Server.java---
package chatrooms.model.server;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
/**
* Server in the client-server model
*/
public class Server {
public static int SERVER_PORT_NR = 6000;
private final ArrayList<Integer> activePorts = new ArrayList<>();
private boolean constructed;
public Server() {
constructed = true;
}
/**
* Start the server and perform the following steps:
* 1. Wait for a connection from a chatroom/bot
* 2. Create a new Socket for the room/bot to connect to
* 3. Run a new instance of ThreadHandler on a new thread for this socket
*/
public void start() {
try (ServerSocket serverSocket = new ServerSocket(SERVER_PORT_NR)) {
System.out.println("Server waiting for chatroom connections...");
while (true) {
if (!activePorts.isEmpty()) System.out.println(activePorts);
Socket socket = serverSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String typeOfConnection = in.readLine();
if (typeOfConnection.equals("Chat")) {
String roomName = in.readLine();
System.out.println("Chatroom \"" + roomName + "\" is requesting a port number");
int portNr = Integer.parseInt(String.valueOf(in.readLine()));
activePorts.add(portNr);
System.out.println("... " + roomName + " has activated port number: " + portNr);
System.out.println("Current open chatrooms: " + activePorts.size());
socket.close();
in.close();
} else if (typeOfConnection.equals("BotMan")) {
System.out.println("BotManager is connecting to the main server... ");
ObjectOutputStream os = new ObjectOutputStream(socket.getOutputStream());
os.writeObject(activePorts);
System.out.println("... BotManager Successfully connected");
} else throw new ClassNotFoundException(); // There must be a better exception ??
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("IO exception");
} catch (Exception e) {
e.printStackTrace();
System.out.println("Unknown connection type!");
}
}
public boolean isConstructed() {
return constructed;
}
}
---ServerMain.java---
package chatrooms.model.server;
public class ServerMain {
public static void main(String[] args) {
Server server = new Server();
server.start();
}
}
My question is, how am I supposed to do Unit testing and integration testing on these files? Like, what exactly should I test in the start() method and how?
So far my test class looks like that:
package chatrooms.model.server;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class TestServerMain {
@Test
public void testOne(){
Server server = new Server();
assertTrue(server.isConstructed(), " Server is not constructed");
server.start();
}
}
Thank you for any help! I really do not know where should I even start...
Aucun commentaire:
Enregistrer un commentaire