Sockets moved to core-java
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package com.baeldung.socket;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class EchoMultiTest {
|
||||
|
||||
{
|
||||
Executors.newSingleThreadExecutor().submit(() -> new EchoMultiServer().start(5555));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenClient1_whenServerResponds_thenCorrect() {
|
||||
EchoClient client = new EchoClient();
|
||||
client.startConnection("127.0.0.1", 5555);
|
||||
String msg1 = client.sendMessage("hello");
|
||||
String msg2 = client.sendMessage("world");
|
||||
String terminate = client.sendMessage("");
|
||||
assertEquals(msg1, "hello");
|
||||
assertEquals(msg2, "world");
|
||||
assertEquals(terminate, "bye");
|
||||
client.stopConnection();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClient2_whenServerResponds_thenCorrect() {
|
||||
EchoClient client = new EchoClient();
|
||||
client.startConnection("127.0.0.1", 5555);
|
||||
String msg1 = client.sendMessage("hello");
|
||||
String msg2 = client.sendMessage("world");
|
||||
String terminate = client.sendMessage("");
|
||||
assertEquals(msg1, "hello");
|
||||
assertEquals(msg2, "world");
|
||||
assertEquals(terminate, "bye");
|
||||
client.stopConnection();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClient3_whenServerResponds_thenCorrect() {
|
||||
EchoClient client = new EchoClient();
|
||||
client.startConnection("127.0.0.1", 5555);
|
||||
String msg1 = client.sendMessage("hello");
|
||||
String msg2 = client.sendMessage("world");
|
||||
String terminate = client.sendMessage("");
|
||||
assertEquals(msg1, "hello");
|
||||
assertEquals(msg2, "world");
|
||||
assertEquals(terminate, "bye");
|
||||
client.stopConnection();
|
||||
}
|
||||
|
||||
}
|
||||
41
core-java/src/test/java/com/baeldung/socket/EchoTest.java
Normal file
41
core-java/src/test/java/com/baeldung/socket/EchoTest.java
Normal file
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.socket;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class EchoTest {
|
||||
{
|
||||
Executors.newSingleThreadExecutor().submit(() -> new EchoServer().start(4444));
|
||||
}
|
||||
|
||||
EchoClient client = new EchoClient();
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
client.startConnection("127.0.0.1", 4444);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClient_whenServerEchosMessage_thenCorrect() {
|
||||
|
||||
String resp1 = client.sendMessage("hello");
|
||||
String resp2 = client.sendMessage("world");
|
||||
String resp3 = client.sendMessage("!");
|
||||
String resp4 = client.sendMessage("");
|
||||
assertEquals("hello", resp1);
|
||||
assertEquals("world", resp2);
|
||||
assertEquals("!", resp3);
|
||||
assertEquals("good bye", resp4);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
client.stopConnection();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.socket;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class GreetServerTest {
|
||||
|
||||
GreetClient client;
|
||||
|
||||
{
|
||||
Executors.newSingleThreadExecutor().submit(() -> new GreetServer().start(6666));
|
||||
}
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
client = new GreetClient();
|
||||
client.startConnection("127.0.0.1", 6666);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGreetingClient_whenServerRespondsWhenStarted_thenCorrect() {
|
||||
String response = client.sendMessage("hello server");
|
||||
assertEquals("hello client", response);
|
||||
}
|
||||
|
||||
@After
|
||||
public void finish() {
|
||||
client.stopConnection();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user