Java - Как сделать jUnit-тест для Sockets-соединения???

Ответить
Аватара пользователя
Killer_13

Java - Как сделать jUnit-тест для Sockets-соединения???

Сообщение Killer_13 »

Собственно вопрос понятен из темы.

Есть клиент и сервер - нужно протестировать с помощью jUnit Socket-соединение, ну или если у кого есть другие примеры то все-равно с помощью чего, главное понять принцип как это сделать.



В инете нашел материал, но что-то под свой пример заточить не смог.



Спасибо за подсказки.



Client



Код:

Код: Выделить всё

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
	public static void main(String[] args) throws UnknownHostException,
			IOException {
		new Client();
	}
	private Socket clientSocket;
	private InputStream inputStream;
	private OutputStream outputStream;
	private DataInputStream dataInputStream;
	private DataOutputStream dataOutputStream;
	private final String hostName = "localhost";
	private final int PORT = 6700;
	private BufferedReader bufReader;
	private String requestMessage = null;
	private String responseMessage = null;
	public Client() {
		System.out.println("-CLIENT-");
		try {
			this.clientSocket = new Socket(this.hostName, this.PORT);
			this.bufReader = new BufferedReader(
					new InputStreamReader(System.in));
			this.inputStream = clientSocket.getInputStream();
			this.outputStream = clientSocket.getOutputStream();
			this.dataInputStream = new DataInputStream(inputStream);
			this.dataOutputStream = new DataOutputStream(outputStream);
			do {
				this.requestMessage = bufReader.readLine();
				sendMessage(this.requestMessage);
				this.responseMessage = dataInputStream.readUTF();
				System.out.println(this.responseMessage);
			} while ((!(this.requestMessage.compareTo("quit") == 0)));
			System.out.println("ClientStopped!");
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				this.inputStream.close();
				this.outputStream.close();
				this.clientSocket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	private void sendMessage(String msg) {
		try {
			this.dataOutputStream.writeUTF(msg);
			this.dataOutputStream.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}



Server



Код:

Код: Выделить всё

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
	public static void main(String[] args) throws IOException {
		new Server();
	}
	private ServerSocket serverSocket;
	private Socket connectionSocket;
	private InputStream inputStream;
	private OutputStream outputStream;
	private DataInputStream dataInputStream;
	private DataOutputStream dataOutputStream;
	private final int PORT = 6700;
	private int parkPlatzCounter = 5;
	private String responseMessage = null;
	public Server() {
		System.out.println("-SERVER-");
		try {
			this.serverSocket = new ServerSocket(this.PORT);
			this.connectionSocket = serverSocket.accept();
			this.inputStream = connectionSocket.getInputStream();
			this.outputStream = connectionSocket.getOutputStream();
			this.dataInputStream = new DataInputStream(inputStream);
			this.dataOutputStream = new DataOutputStream(outputStream);
			do {
				this.responseMessage = this.dataInputStream.readUTF();
				if (this.responseMessage.compareTo("quit") == 0) {
					sendMessage("Server Stopped!");
				} else if (this.responseMessage.compareTo("in") == 0) {
					autoEin();
				} else if (this.responseMessage.compareTo("out") == 0) {
					autoAus();
				} else if (this.responseMessage.compareTo("free") == 0) {
					free();
				} else {
					sendMessage("---Falsche Eingabe!---");
				}
			} while ((!(this.responseMessage.compareTo("quit") == 0)));
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				this.inputStream.close();
				this.outputStream.close();
				this.serverSocket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	private void free() {
		sendMessage("> " + this.parkPlatzCounter);
	}
	private void autoEin() {
		if (this.parkPlatzCounter > 0) {
			this.parkPlatzCounter--;
			sendMessage("> ok");
		} else {
			sendMessage("> fail");
		}
	}
	private void autoAus() {
		if (this.parkPlatzCounter < 5) {
			this.parkPlatzCounter++;
			sendMessage("> ok");
		} else {
			sendMessage("> fail");
		}
	}
	private void sendMessage(String msg) {
		try {
			this.dataOutputStream.writeUTF(msg);
			this.dataOutputStream.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
Ответить

Вернуться в «Программирование и базы данных»