Permalink
Browse files

Refactor ZombieJS tests

  • Loading branch information...
1 parent 61dc234 commit 94d66c991eb09167510771b5c73454374dbe7272 @dgageot dgageot committed Mar 28, 2012
@@ -1,47 +0,0 @@
-package gildedrose;
-
-import com.sun.net.httpserver.HttpServer;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-
-import java.util.Random;
-
-public abstract class AbstractHttpServerTest {
- private static final int TRY_COUNT = 10;
- private static final int DEFAULT_PORT = 8183;
- private static final Random RANDOM = new Random();
-
- private static HttpServer httpServer;
- private static int port;
-
- @BeforeClass
- public static void startHttpServer() throws Exception {
- for (int i = 0; i < TRY_COUNT; i++) {
- try {
- port = getRandomPort();
- httpServer = InnHttpServer.start(port);
- return;
- } catch (Exception e) {
- System.err.println("Unable to bind server: " + e);
- }
- }
- throw new IllegalStateException("Unable to start server");
- }
-
- private static int getRandomPort() {
- synchronized (RANDOM) {
- return DEFAULT_PORT + RANDOM.nextInt(1000);
- }
- }
-
- @AfterClass
- public static void stopHttpServer() throws Exception {
- if (httpServer != null) {
- httpServer.stop(0);
- }
- }
-
- protected int port() {
- return port;
- }
-}
@@ -1,41 +0,0 @@
-package gildedrose;
-
-import com.google.common.base.Splitter;
-import com.google.common.collect.ImmutableMap;
-import org.apache.commons.exec.*;
-
-import java.io.ByteArrayOutputStream;
-import java.io.File;
-import java.util.Map;
-
-import static org.junit.Assert.fail;
-
-public abstract class AbstractZombieJsTest extends AbstractHttpServerTest {
- protected void assertWithZombieJs(String jsPath) {
- Executor executor = new DefaultExecutor();
- executor.setWatchdog(new ExecuteWatchdog(60000));
-
- ByteArrayOutputStream output = new ByteArrayOutputStream();
- executor.setStreamHandler(new PumpStreamHandler(output));
-
- Map map = ImmutableMap.of("file", new File(".", jsPath), "port", port());
- CommandLine cmdLine = CommandLine.parse("/usr/local/bin/node ${file} ${port}", map);
-
- Exception failure = null;
- try {
- executor.execute(cmdLine);
- } catch (Exception e) {
- failure = e;
- }
-
- for (String line : Splitter.on('\n').split(output.toString())) {
- if (line.contains("Error: ")) {
- fail(line);
- }
- }
-
- if (null != failure) {
- fail(failure.getMessage());
- }
- }
-}
@@ -0,0 +1,16 @@
+package gildedrose;
+
+import org.junit.*;
+import test.utils.*;
+
+import static test.utils.ZombieJsAssertions.*;
+
+public class InnFunctionalTest {
+ @ClassRule
+ public static HttpServerRule httpServer = new HttpServerRule();
+
+ @Test
+ public void canTestInn() {
+ assertThat(httpServer).canRun("testInn.js");
+ }
+}
@@ -1,10 +0,0 @@
-package gildedrose;
-
-import org.junit.Test;
-
-public class ZombieJsTest extends AbstractZombieJsTest {
- @Test
- public void canTest() {
- assertWithZombieJs("test.js");
- }
-}
@@ -0,0 +1,50 @@
+package test.utils;
+
+import com.sun.net.httpserver.*;
+import gildedrose.*;
+import org.junit.rules.*;
+
+import java.util.*;
+
+public class HttpServerRule extends ExternalResource {
+ private static final int TRY_COUNT = 10;
+ private static final int DEFAULT_PORT = 8183;
+
+ private final Random RANDOM = new Random();
+ private HttpServer httpServer;
+
+ @Override
+ protected void before() {
+ httpServer = startHttpServer();
+ }
+
+ @Override
+ protected void after() {
+ stopHttpServer();
+ }
+
+ public HttpServer getHttpServer() {
+ return httpServer;
+ }
+
+ private HttpServer startHttpServer() {
+ for (int i = 0; i < TRY_COUNT; i++) {
+ try {
+ return InnHttpServer.start(getRandomPort());
+ } catch (Exception e) {
+ System.err.println("Unable to bind server: " + e);
+ }
+ }
+ throw new IllegalStateException("Unable to start server");
+ }
+
+ private void stopHttpServer() {
+ if (httpServer != null) {
+ httpServer.stop(0);
+ }
+ }
+
+ private synchronized int getRandomPort() {
+ return DEFAULT_PORT + RANDOM.nextInt(1000);
+ }
+}
@@ -0,0 +1,55 @@
+package test.utils;
+
+import com.google.common.base.*;
+import com.google.common.collect.*;
+import org.apache.commons.exec.*;
+
+import java.io.*;
+import java.util.*;
+
+import static org.junit.Assert.*;
+
+public final class ZombieJsAssertions {
+ private ZombieJsAssertions() {
+ // Static class
+ }
+
+ public static HttpServerAssert assertThat(HttpServerRule httpServer) {
+ return new HttpServerAssert(httpServer);
+ }
+
+ public static class HttpServerAssert {
+ private final HttpServerRule httpServer;
+
+ HttpServerAssert(HttpServerRule httpServer) {
+ this.httpServer = httpServer;
+ }
+
+ public HttpServerAssert canRun(String jsPath) {
+ try {
+ ByteArrayOutputStream output = new ByteArrayOutputStream();
+
+ Map map = ImmutableMap.of("file", new File(jsPath), "port", getPort());
+
+ Executor executor = new DefaultExecutor();
+ executor.setWatchdog(new ExecuteWatchdog(60000));
+ executor.setStreamHandler(new PumpStreamHandler(output));
+ executor.execute(CommandLine.parse("/usr/local/bin/node ${file} ${port}", map));
+
+ for (String line : Splitter.on('\n').split(output.toString())) {
+ if (line.contains("Error: ")) {
+ fail(line);
+ }
+ }
+ } catch (Exception e) {
+ fail(e.getMessage());
+ }
+
+ return this;
+ }
+
+ private int getPort() {
+ return httpServer.getHttpServer().getAddress().getPort();
+ }
+ }
+}
View
File renamed without changes.

0 comments on commit 94d66c9

Please sign in to comment.