# HG changeset patch # User Jaroslav Tulach # Date 1255263619 -7200 # Node ID 95dfb04fcee1dcd4acd1411884331f7befcb4d22 # Parent 6bf820453a5f6c0b98f84fb7580d6abcd41f2097 Getting ready for separate execution of API as well as UI diff -r 6bf820453a5f -r 95dfb04fcee1 freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java --- a/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java Sat Oct 10 17:15:35 2009 +0200 +++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java Sun Oct 11 14:20:19 2009 +0200 @@ -36,6 +36,7 @@ import com.sun.net.httpserver.HttpServer; import cz.xelfi.quoridor.Board; import cz.xelfi.quoridor.IllegalPositionException; +import cz.xelfi.quoridor.webidor.resources.Quoridor; import java.awt.Image; import java.io.File; import java.io.IOException; @@ -306,13 +307,14 @@ // public static void main(String[] args) throws Exception { - int port = 9998; + int port = 9333; if (args.length > 1) { port = Integer.parseInt(args[0]); } + String remoteAPI = args.length > 2 ? args[1] : null; - Callable r = startServers(port); + Callable r = startServers(port, remoteAPI); if (args.length < 2 || !args[1].equals("--kill")) { System.out.println("Hit enter to stop it..."); @@ -326,22 +328,23 @@ System.exit(0); } - static Callable startServers(int port) throws Exception { + static Callable startServers(int port, String remoteAPI) throws Exception { + Client client = new Client(); - if (System.getProperty("quoridor.dir") == null) { - File home = new File(System.getProperty("user.home")); - File quoridor = new File(home, ".quoridor"); - System.setProperty("quoridor.dir", quoridor.getPath()); + final HttpServer apiServer; + if (remoteAPI == null) { + int localAPIPort = port - 1; + apiServer = Quoridor.start(localAPIPort); + base = client.resource(new URI("http://localhost:" + localAPIPort + "/api/")); + } else { + base = client.resource(new URI(remoteAPI)); + apiServer = null; } ResourceConfig rc = new PackagesResourceConfig( - "cz.xelfi.quoridor.webidor", "cz.xelfi.quoridor.freemarkerdor" ); - Client client = new Client(); - base = client.resource(new URI("http://localhost:" + port + "/api/")); - final String baseUri = "http://localhost:" + port + "/"; final HttpServer server = HttpServerFactory.create(baseUri, rc); server.start(); @@ -349,6 +352,9 @@ return new Callable() { public Void call() throws Exception { + if (apiServer != null) { + apiServer.stop(0); + } server.stop(0); return null; } diff -r 6bf820453a5f -r 95dfb04fcee1 freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java --- a/freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java Sat Oct 10 17:15:35 2009 +0200 +++ b/freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java Sun Oct 11 14:20:19 2009 +0200 @@ -58,6 +58,7 @@ private static File dir; private static Callable stop; private WebResource webResource; + private WebResource apiResource; public UITest() throws Exception { } @@ -69,7 +70,7 @@ dir.delete(); dir.mkdirs(); System.setProperty("quoridor.dir", dir.getPath()); - stop = UI.startServers(9991); + stop = UI.startServers(9991, null); File passwd = new File(dir, "passwd"); FileOutputStream os = new FileOutputStream(passwd); @@ -82,6 +83,7 @@ Client client = new Client(); webResource = client.resource(new URI("http://localhost:9991/")); + apiResource = client.resource(new URI("http://localhost:9990/api/")); } @AfterClass @@ -116,7 +118,7 @@ } @Test public void testGetIndexPage() throws Exception { - String logJarda = webResource.path("api/login"). + String logJarda = apiResource.path("login"). queryParam("name", "Jarda"). queryParam("password", "darda"). accept(MediaType.TEXT_PLAIN). @@ -158,7 +160,7 @@ @Test public void testCreateGameWrongUsers() throws Exception { - String logTest = webResource.path("api/login"). + String logTest = apiResource.path("login"). queryParam("name", "test"). queryParam("password", "pes"). accept(MediaType.TEXT_PLAIN). @@ -177,12 +179,12 @@ } @Test public void testCreateGameOK() throws Exception { - String logTest = webResource.path("api/login"). + String logTest = apiResource.path("login"). queryParam("name", "test"). queryParam("password", "pes"). accept(MediaType.TEXT_PLAIN). put(String.class); - String logJarda = webResource.path("api/login"). + String logJarda = apiResource.path("login"). queryParam("name", "Jarda"). queryParam("password", "darda"). accept(MediaType.TEXT_PLAIN). @@ -217,7 +219,7 @@ InputStream img1 = webResource.path(m.group(1)).get(InputStream.class); assertNotNull("image found", img1); - ClientResponse move = webResource.path("api/games/" + games[0]). + ClientResponse move = apiResource.path("games/" + games[0]). queryParam("loginID", logJarda). queryParam("player", "Jarda").queryParam("move", "N").put(ClientResponse.class); assertEquals("Move OK:\n" + move.getEntity(String.class), ClientResponse.Status.OK, move.getClientResponseStatus()); diff -r 6bf820453a5f -r 95dfb04fcee1 webidor/all-zip.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/webidor/all-zip.xml Sun Oct 11 14:20:19 2009 +0200 @@ -0,0 +1,19 @@ + + + all + + zip + + + + false + lib + + + + + target/webidor.jar + / + + + diff -r 6bf820453a5f -r 95dfb04fcee1 webidor/pom.xml --- a/webidor/pom.xml Sat Oct 10 17:15:35 2009 +0200 +++ b/webidor/pom.xml Sun Oct 11 14:20:19 2009 +0200 @@ -5,7 +5,7 @@ all-quoridor org.apidesign 1.0 - + org.apidesign webidor jar @@ -88,7 +88,39 @@ org.codehaus.mojo exec-maven-plugin - cz.xelfi.quoridor.webidor.Quoridor + cz.xelfi.quoridor.webidor.resources.Quoridor + + + + maven-assembly-plugin + 2.2-beta-2 + + + create-executable-jar + package + + single + + + + all-zip.xml + + webidor-${version} + + + + + + org.apache.maven.plugins + maven-jar-plugin + + + + true + lib/ + cz.xelfi.quoridor.webidor.resources.Quoridor + + diff -r 6bf820453a5f -r 95dfb04fcee1 webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java Sat Oct 10 17:15:35 2009 +0200 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java Sun Oct 11 14:20:19 2009 +0200 @@ -121,9 +121,15 @@ // public static void main(String[] args) throws IOException { - HttpServer s = start(9998); + int port = 9222; + try { + port = Integer.parseInt(args[0]); + } catch (Exception ex) { + // OK + } + HttpServer s = start(port); System.out.println( - "Quoridor started at port 9998\n" + "Hit enter to stop it..." + "Quoridor started at port " + port + "\n" + "Hit enter to stop it..." ); System.in.read(); s.stop(0); @@ -138,10 +144,11 @@ } final String baseUri = "http://localhost:" + port + "/"; - File home = new File(System.getProperty("user.home")); - File quoridor = new File(home, ".quoridor"); - - System.setProperty("quoridor.dir", quoridor.getPath()); + if (System.getProperty("quoridor.dir") == null) { + File home = new File(System.getProperty("user.home")); + File quoridor = new File(home, ".quoridor"); + System.setProperty("quoridor.dir", quoridor.getPath()); + } ResourceConfig rc = new PackagesResourceConfig("cz.xelfi.quoridor.webidor"); HttpServer server = HttpServerFactory.create(baseUri, rc); diff -r 6bf820453a5f -r 95dfb04fcee1 webidor/src/test/java/cz/xelfi/quoridor/webidor/resources/ChatTest.java --- a/webidor/src/test/java/cz/xelfi/quoridor/webidor/resources/ChatTest.java Sat Oct 10 17:15:35 2009 +0200 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/resources/ChatTest.java Sun Oct 11 14:20:19 2009 +0200 @@ -175,10 +175,15 @@ fail(); } + Thread.sleep(1500); + File tmp = File.createTempFile("test-board", "game"); read.storeGame(rg, tmp); - assertEquals("Newly written file remains the same", readFile(game), readFile(tmp)); + String sss1 = readFile(game); + Thread.sleep(1500); + + assertEquals("Newly written file remains the same", sss1, readFile(tmp)); String sGame = webResource.path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(String.class); Game readGame = webResource.path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(Game.class);