Amend test to execute service in thread

This commit is contained in:
Alex Theedom 2016-08-23 21:59:44 +01:00
parent 50afba45f7
commit 683f1768b7
7 changed files with 67 additions and 33 deletions

View File

@ -12,8 +12,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>7</source>
<target>7</target>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>

View File

@ -15,7 +15,7 @@ public class EchoClient {
in = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
} catch (IOException e) {
System.out.print(e);
}
}

View File

@ -41,6 +41,6 @@ public class GreetClient {
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

View File

@ -21,7 +21,7 @@ public class GreetServer {
if ("hello server".equals(greeting))
out.println("hello client");
else
out.println("unrecognised greetin");
out.println("unrecognised greeting");
} catch (IOException e) {
e.printStackTrace();
}

View File

@ -1,17 +1,36 @@
package com.baeldung.socket;
import static org.junit.Assert.*;
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));
}
EchoClient client = new EchoClient();
@Before
public void init() {
client.startConnection("127.0.0.1", 5555);
}
@After
public void finish(){
client.stopConnection();
}
@Test
public void givenClient1_whenServerResponds_thenCorrect() {
EchoClient client1 = new EchoClient();
client1.startConnection("127.0.0.1", 5555);
String msg1 = client1.sendMessage("hello");
String msg2 = client1.sendMessage("world");
String terminate = client1.sendMessage(".");
String msg1 = client.sendMessage("hello");
String msg2 = client.sendMessage("world");
String terminate = client.sendMessage(".");
assertEquals(msg1, "hello");
assertEquals(msg2, "world");
assertEquals(terminate, "bye");
@ -19,11 +38,9 @@ public class EchoMultiTest {
@Test
public void givenClient2_whenServerResponds_thenCorrect() {
EchoClient client1 = new EchoClient();
client1.startConnection("127.0.0.1", 5555);
String msg1 = client1.sendMessage("hello");
String msg2 = client1.sendMessage("world");
String terminate = client1.sendMessage(".");
String msg1 = client.sendMessage("hello");
String msg2 = client.sendMessage("world");
String terminate = client.sendMessage(".");
assertEquals(msg1, "hello");
assertEquals(msg2, "world");
assertEquals(terminate, "bye");
@ -31,11 +48,9 @@ public class EchoMultiTest {
@Test
public void givenClient3_whenServerResponds_thenCorrect() {
EchoClient client1 = new EchoClient();
client1.startConnection("127.0.0.1", 5555);
String msg1 = client1.sendMessage("hello");
String msg2 = client1.sendMessage("world");
String terminate = client1.sendMessage(".");
String msg1 = client.sendMessage("hello");
String msg2 = client.sendMessage("world");
String terminate = client.sendMessage(".");
assertEquals(msg1, "hello");
assertEquals(msg2, "world");
assertEquals(terminate, "bye");

View File

@ -1,11 +1,11 @@
package com.baeldung.socket;
import static org.junit.Assert.assertEquals;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class EchoTest {
EchoClient client = null;

View File

@ -1,16 +1,35 @@
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;
import org.junit.Test;
public class GreetServerTest {
GreetClient client;
{
Executors.newSingleThreadExecutor().submit(() -> new GreetServer().start(6666));
}
@Before
public void init() {
client = new GreetClient();
}
@Test
public void givenGreetingClient_whenServerRespondsWhenStarted_thenCorrect() {
GreetClient client = new GreetClient();
client.startConnection("127.0.0.1", 6666);
String response = client.sendMessage("hello server");
assertEquals("hello client", response);
}
@After
public void finish() {
client.stopConnection();
}
}