Гость
Целевая тема:
Создать новую тему:
Автор:
Форумы / Java [игнор отключен] [закрыт для гостей] / Как сделать jUnit-тест для Sockets-соединения??? / 3 сообщений из 3, страница 1 из 1
26.04.2012, 20:58:37
    #37773059
_webdev_
Гость
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Как сделать jUnit-тест для Sockets-соединения???
Собственно вопрос понятен из темы.
Есть клиент и сервер - нужно протестировать с помощью jUnit Socket-соединение, ну или если у кого есть другие примеры то все-равно с помощью чего, главное понять принцип как это сделать.

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

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

Client

Код: java
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
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
Код: java
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
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();
        }
    }
}
...
Рейтинг: 0 / 0
29.04.2012, 06:13:57
    #37776582
ivanov-void
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Как сделать jUnit-тест для Sockets-соединения???
_webdev_,

Например, такой вариант:

Код: java
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import org.junit.Before;
import org.junit.Test;

public class TestSomethingVersion1 {

        @Before
        public void before() {
                System.out.println("@Before");
                Thread myThread = new Thread() {
                        public void run() {
                                System.out.println("@Before myThread run()");
                                Socket myServerSocket = null;
                                try {
                                        ServerSocket myServer = new ServerSocket(3000);
                                        System.out
                                                        .println("@Before myThread run() - server socket created.");
                                        myServerSocket = myServer.accept();
                                        System.out
                                                        .println("@Before myThread run() - accepted connection");
                                } catch (IOException e) {
                                        e.printStackTrace();
                                }
                        }
                };
                myThread.start();
        }

        @Test
        public void test1() throws Exception {
                System.out.println("test1");
                synchronized (this) {
                        try {
                                wait(1000);
                        } catch (InterruptedException e) {
                                // do nothing.
                        }
                }
                System.out.println("test1 - after wait");
                new Socket("localhost", 3000);
                synchronized (this) {
                        try {
                                wait(1000);
                        } catch (InterruptedException e) {
                                // do nothing.
                        }
                }
                System.out.println("test1 - after second wait");
        }
} 
...
Рейтинг: 0 / 0
29.04.2012, 21:44:44
    #37777010
_webdev_
Гость
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Как сделать jUnit-тест для Sockets-соединения???
Интересно - спасибо, завтра испробую.
...
Рейтинг: 0 / 0
Форумы / Java [игнор отключен] [закрыт для гостей] / Как сделать jUnit-тест для Sockets-соединения??? / 3 сообщений из 3, страница 1 из 1
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]