Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Zombie.js unit test

  • Loading branch information...
commit e6188ee51c93c3d4dd6a374966d165a53e197d5e 1 parent c6a8308
@dgageot dgageot authored
View
1  .gitignore
@@ -1,3 +1,4 @@
+.DS_Store
.idea
*.iml
.project
View
5 pom.xml
@@ -102,5 +102,10 @@
<artifactId>listmaker</artifactId>
<version>1.0.1</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-exec</artifactId>
+ <version>1.1</version>
+ </dependency>
</dependencies>
</project>
View
10 src/main/java/gildedrose/InnHttpServer.java
@@ -2,12 +2,14 @@
import com.google.common.base.*;
import com.sun.jersey.api.container.httpserver.*;
+import com.sun.net.httpserver.HttpServer;
import lombok.*;
import javax.ws.rs.*;
import javax.ws.rs.core.*;
import java.util.*;
+import static java.lang.String.format;
import static net.gageot.listmaker.ListMaker.*;
@Path("/")
@@ -60,7 +62,13 @@ public Response staticResource(@PathParam("path") String path) {
@Getter int index;
}
+ public static HttpServer start(int port) throws Exception {
+ HttpServer httpServer = HttpServerFactory.create(format("http://localhost:%d/", port));
+ httpServer.start();
+ return httpServer;
+ }
+
public static void main(String[] args) throws Exception {
- HttpServerFactory.create("http://localhost:8080/").start();
+ start(9090);
}
}
View
1  src/test/java/gildedrose/InnTest.java
@@ -16,7 +16,6 @@ public void canGetOriginalQuantities() {
assertThat(items).onProperty("name").containsExactly("+5 Dexterity Vest", "Aged Brie", "Elixir of the Mongoose", "Sulfuras, Hand of Ragnaros", "Backstage passes to a TAFKAL80ETC concert", "Conjured Mana Cake");
assertThat(items).onProperty("quality").containsExactly(20, 0, 7, 80, 20, 6);
assertThat(items).onProperty("sellIn").containsExactly(10, 2, 5, 0, 15, 3);
-
}
@Test
View
79 src/test/java/gildedrose/ZombieJsTest.java
@@ -0,0 +1,79 @@
+package gildedrose;
+
+import com.google.common.base.Splitter;
+import com.google.common.collect.ImmutableMap;
+import com.sun.net.httpserver.HttpServer;
+import org.apache.commons.exec.*;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.util.Map;
+import java.util.Random;
+
+import static org.junit.Assert.fail;
+
+// TODO: Random port
+public class ZombieJsTest {
+ 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);
+ }
+ }
+
+ @Test
+ public void test() throws Exception {
+ Map map = ImmutableMap.of("file", new File(".", "test.js"), "port", port);
+
+ CommandLine cmdLine = CommandLine.parse("/usr/local/bin/node ${file} ${port}", map);
+
+ DefaultExecutor executor = new DefaultExecutor();
+ ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);
+ executor.setWatchdog(watchdog);
+
+ ByteArrayOutputStream output = new ByteArrayOutputStream();
+ executor.setStreamHandler(new PumpStreamHandler(output));
+
+ try {
+ executor.execute(cmdLine);
+ } catch (ExecuteException e) {
+ //fail(e.getMessage());
+ }
+
+ for (String line : Splitter.on('\n').split(output.toString())) {
+ if (line.contains("Error: ")) {
+ fail(line);
+ }
+ }
+ }
+}
View
21 test.js
@@ -9,15 +9,22 @@ assert.notExists = function(selector) {
assert.ok(!browser.query(selector));
}
+port = process.argv[2]
+console.log(port);
browser = new Browser()
-browser.visit("http://localhost:8080/", function() {
- assert.ok(browser.success);
- assert.equal(browser.text("title"), "Gilded Rose Inn");
+browser.visit("http://localhost:" + port + "/", function() {
+ should_have_correct_title();
+ should_show_aged_brie_image_when_aged_brie_link_is_clicked();
+});
- assert.exists(".fade img[src='/web/img/Aged Brie.jpg']");
- assert.notExists(".fade.in img[src='/web/img/Aged Brie.jpg']");
+should_have_correct_title = function() {
+ assert.equal(browser.text("title"), "Gilded Rose Inn");
+}
+
+should_show_aged_brie_image_when_aged_brie_link_is_clicked = function() {
+ assert.notExists(".fade.in img[src*='Aged Brie.jpg']");
browser.clickLink("Aged Brie", function() {
- assert.exists(".fade.in img[src='/web/img/Aged Brie.jpg']");
+ assert.exists(".fade.in img[src*='Aged Brie.jpg']");
})
-});
+}
Please sign in to comment.
Something went wrong with that request. Please try again.