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> <artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version> <version>3.3</version>
<configuration> <configuration>
<source>7</source> <source>8</source>
<target>7</target> <target>8</target>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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