package org.jogger;

import org.apache.http.HttpResponse;
import org.apache.http.client.fluent.Request;
import org.jogger.http.Response;
import org.testng.Assert;
import org.testng.annotations.Test;

public class JoggerServerTest {

	@Test
	public void shouldStartServer() throws Exception {
		JoggerServer joggerServer = new JoggerServer(new Jogger());
		joggerServer.listen(27773);

		try {
			HttpResponse response = Request.Get("http://localhost:27773/").execute().returnResponse();
			Assert.assertEquals(response.getStatusLine().getStatusCode(), 404);
		} finally {
			joggerServer.stop();
		}
	}
	
	@Test
	public void shouldExecuteRoute() throws Exception {
		Jogger app = new Jogger();
		app.get("/", new RouteHandler() {
			@Override
			public void handle(org.jogger.http.Request request, Response response) {
				
			}
		});
		
		JoggerServer joggerServer = new JoggerServer(app);
		joggerServer.listen(27773);

		try {
			HttpResponse response = Request.Get("http://localhost:27773/").execute().returnResponse();
			Assert.assertEquals(response.getStatusLine().getStatusCode(), 200);
		} finally {
			joggerServer.stop();
		}
	}
	
	@Test
	public void shouldHandleException() throws Exception {
		Jogger app = new Jogger();
		app.get("/", new RouteHandler() {
			@Override
			public void handle(org.jogger.http.Request request, Response response) {
				throw new RuntimeException();
			}
		});
		
		JoggerServer joggerServer = new JoggerServer(app);
		joggerServer.listen(27773);

		try {
			HttpResponse response = Request.Get("http://localhost:27773/").execute().returnResponse();
			Assert.assertEquals(response.getStatusLine().getStatusCode(), 500);
		} finally {
			joggerServer.stop();
		}
	}
	
}

@@ -14,16 +14,125 @@
 ogger;%0A%0A
+import java.net.ConnectException;%0Aimport java.net.Socket;%0Aimport java.util.concurrent.atomic.AtomicBoolean;%0A%0A
 import o
@@ -152,32 +152,32 @@
 p.HttpResponse;%0A
-
 import org.apach
@@ -367,16 +367,20 @@
 ldStartS
+topS
 erver() 
@@ -694,32 +694,156 @@
 rServer.stop();%0A
+%09%09%09try %7B%0A%09%09%09%09new Socket(%22localhost%22, 27773);%0A%09%09%09%09Assert.fail(%22Server is still running%22);%0A%09%09%09%7D catch (ConnectException e) %7B%7D%0A
 %09%09%7D%0A%09%7D%0A%09%0A%09@Test%0A
@@ -1924,27 +1924,27 @@
 rServer.stop();%0A
-
 %09%09%7D%0A%09%7D%0A%09%0A%7D%0A
@@ -1941,10 +1941,696 @@
 %09%7D%0A%09%7D%0A%09%0A
+%09@Test%0A%09public void shouldJoinServerThread() throws Exception %7B%0A%09%09final AtomicBoolean running = new AtomicBoolean(false);%0A%09%09%0A%09%09Thread thread = new Thread(new Runnable() %7B%0A%09%09%09@Override%0A%09%09%09public void run() %7B%0A%09%09%09%09JoggerServer joggerServer = new JoggerServer(new Jogger());%0A%09%09%09%09joggerServer.listen(27773);%0A%09%09%09%09%0A%09%09%09%09try %7B%0A%09%09%09%09%09running.getAndSet(true);%0A%09%09%09%09%09%0A%09%09%09%09%09try %7B%0A%09%09%09%09%09%09joggerServer.join();%0A%09%09%09%09%09%7D catch (InterruptedException e) %7B%7D%0A%09%09%09%09%7D finally %7B%0A%09%09%09%09%09joggerServer.stop();%0A%09%09%09%09%09running.getAndSet(false);%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D);%0A%09%09thread.start();%0A%09%09%0A%09%09Thread.sleep(100);%0A%09%09Assert.assertTrue(running.get());%0A%09%09thread.interrupt();%0A%09%09Thread.sleep(100);%0A%09%09Assert.assertFalse(running.get());%0A%09%7D%0A%09%0A
 %7D%0A

Improve tests for JoggerServer