import static org.junit.Assert.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.concurrent.CountDownLatch;
import org.junit.Before;
import org.junit.Test;
public class Program2Test {
@Before
public void wait2SecondsBeforeEachTest() {
try {
Thread.sleep( 2000 );
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
@Test
public void test0() {
Runnable client = new Runnable() {
@Override
public void run() {
Program2.goClient();
}
};
final CountDownLatch gate = new CountDownLatch( 1 );
Runnable server = new Runnable() {
@Override
public void run() {
ServerSocket server = null;
try {
server = new ServerSocket( 4321 );
Socket client = server.accept();
PrintWriter out = new PrintWriter( client.getOutputStream(), true );
Scanner in = new Scanner ( client.getInputStream() );
out.println( "Lorem" );
String actual = in.nextLine();
assertEquals( "Incorrect result", "L", actual );
out.println( "o" );
actual = in.nextLine();
assertEquals( "Incorrect result", "r", actual );
out.println( "e" );
actual = in.nextLine();
assertEquals( "Incorrect result", "m", actual );
out.println( "done!" );
assertFalse( "Client hasn't closed its socket", in.hasNext() );
client.close();
gate.countDown();
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
if (server != null) {
server.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
};
Thread serverThread = new Thread( server );
serverThread.start();
Thread clientThread = new Thread( client );
clientThread.start();
int count = 0;
while (serverThread.isAlive() && count++ < 300) {
try {
Thread.sleep( 10 );
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
assertTrue( "Client did not arrive or did not finish correctly", gate.getCount() == 0 );
}
@Test
public void test1() {
Runnable client = new Runnable() {
@Override
public void run() {
Program2.goClient();
}
};
final CountDownLatch gate = new CountDownLatch( 1 );
Runnable server = new Runnable() {
@Override
public void run() {
ServerSocket server = null;
try {
server = new ServerSocket( 4321 );
Socket client = server.accept();
PrintWriter out = new PrintWriter( client.getOutputStream(), true );
Scanner in = new Scanner ( client.getInputStream() );
out.println( "Let it be." );
String actual = in.nextLine();
assertEquals( "Incorrect result", "L", actual );
out.println( "e" );
actual = in.nextLine();
assertEquals( "Incorrect result", "t", actual );
out.println( " " );
actual = in.nextLine();
assertEquals( "Incorrect result", "i", actual );
out.println( "t" );
actual = in.nextLine();
assertEquals( "Incorrect result", " ", actual );
out.println( "b" );
actual = in.nextLine();
assertEquals( "Incorrect result", "e", actual );
out.println( "." );
actual = in.nextLine();
assertEquals( "Incorrect result", "done!", actual );
assertFalse( "Client hasn't closed its socket", in.hasNext() );
client.close();
gate.countDown();
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
if (server != null) {
server.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
};
Thread serverThread = new Thread( server );
serverThread.start();
Thread clientThread = new Thread( client );
clientThread.start();
int count = 0;
while (serverThread.isAlive() && count++ < 300) {
try {
Thread.sleep( 10 );
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
assertTrue( "Client did not arrive or did not finish correctly", gate.getCount() == 0 );
}
@Test
public void test2() {
Runnable client = new Runnable() {
@Override
public void run() {
Program2.goClient();
}
};
final CountDownLatch gate = new CountDownLatch( 1 );
Runnable server = new Runnable() {
@Override
public void run() {
ServerSocket server = null;
try {
server = new ServerSocket( 4321 );
Socket client = server.accept();
PrintWriter out = new PrintWriter( client.getOutputStream(), true );
Scanner in = new Scanner ( client.getInputStream() );
out.println( "Upside Down" );
String actual = in.nextLine();
assertEquals( "Incorrect result", "U", actual );
out.println( "p" );
actual = in.nextLine();
assertEquals( "Incorrect result", "s", actual );
out.println( "i" );
actual = in.nextLine();
assertEquals( "Incorrect result", "d", actual );
out.println( "e" );
actual = in.nextLine();
assertEquals( "Incorrect result", " ", actual );
out.println( "D" );
actual = in.nextLine();
assertEquals( "Incorrect result", "o", actual );
out.println( "w" );
actual = in.nextLine();
assertEquals( "Incorrect result", "n", actual );
out.println( "done!" );
assertFalse( "Client hasn't closed its socket", in.hasNext() );
client.close();
gate.countDown();
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
if (server != null) {
server.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
};
Thread serverThread = new Thread( server );
serverThread.start();
Thread clientThread = new Thread( client );
clientThread.start();
int count = 0;
while (serverThread.isAlive() && count++ < 300) {
try {
Thread.sleep( 10 );
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
assertTrue( "Client did not arrive or did not finish correctly", gate.getCount() == 0 );
}
}