Getting ready for separate execution of API as well as UI
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 11 Oct 2009 14:20:19 +0200
changeset 12195dfb04fcee1
parent 120 6bf820453a5f
child 122 e0ecef0c421b
Getting ready for separate execution of API as well as UI
freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java
freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java
webidor/all-zip.xml
webidor/pom.xml
webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java
webidor/src/test/java/cz/xelfi/quoridor/webidor/resources/ChatTest.java
     1.1 --- a/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java	Sat Oct 10 17:15:35 2009 +0200
     1.2 +++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java	Sun Oct 11 14:20:19 2009 +0200
     1.3 @@ -36,6 +36,7 @@
     1.4  import com.sun.net.httpserver.HttpServer;
     1.5  import cz.xelfi.quoridor.Board;
     1.6  import cz.xelfi.quoridor.IllegalPositionException;
     1.7 +import cz.xelfi.quoridor.webidor.resources.Quoridor;
     1.8  import java.awt.Image;
     1.9  import java.io.File;
    1.10  import java.io.IOException;
    1.11 @@ -306,13 +307,14 @@
    1.12      //
    1.13  
    1.14      public static void main(String[] args) throws Exception {
    1.15 -        int port = 9998;
    1.16 +        int port = 9333;
    1.17          if (args.length > 1) {
    1.18              port = Integer.parseInt(args[0]);
    1.19          }
    1.20 +        String remoteAPI = args.length > 2 ? args[1] : null;
    1.21  
    1.22  
    1.23 -        Callable<Void> r = startServers(port);
    1.24 +        Callable<Void> r = startServers(port, remoteAPI);
    1.25  
    1.26          if (args.length < 2 || !args[1].equals("--kill")) {
    1.27              System.out.println("Hit enter to stop it...");
    1.28 @@ -326,22 +328,23 @@
    1.29          System.exit(0);
    1.30      }
    1.31  
    1.32 -    static Callable<Void> startServers(int port) throws Exception {
    1.33 +    static Callable<Void> startServers(int port, String remoteAPI) throws Exception {
    1.34 +        Client client = new Client();
    1.35  
    1.36 -        if (System.getProperty("quoridor.dir") == null) {
    1.37 -            File home = new File(System.getProperty("user.home"));
    1.38 -            File quoridor = new File(home, ".quoridor");
    1.39 -            System.setProperty("quoridor.dir", quoridor.getPath());
    1.40 +        final HttpServer apiServer;
    1.41 +        if (remoteAPI == null) {
    1.42 +            int localAPIPort = port - 1;
    1.43 +            apiServer = Quoridor.start(localAPIPort);
    1.44 +            base = client.resource(new URI("http://localhost:" + localAPIPort + "/api/"));
    1.45 +        } else {
    1.46 +            base = client.resource(new URI(remoteAPI));
    1.47 +            apiServer = null;
    1.48          }
    1.49  
    1.50          ResourceConfig rc = new PackagesResourceConfig(
    1.51 -            "cz.xelfi.quoridor.webidor",
    1.52              "cz.xelfi.quoridor.freemarkerdor"
    1.53          );
    1.54  
    1.55 -        Client client = new Client();
    1.56 -        base = client.resource(new URI("http://localhost:" + port + "/api/"));
    1.57 -
    1.58          final String baseUri = "http://localhost:" + port + "/";
    1.59          final HttpServer server = HttpServerFactory.create(baseUri, rc);
    1.60          server.start();
    1.61 @@ -349,6 +352,9 @@
    1.62  
    1.63          return new Callable<Void>() {
    1.64              public Void call() throws Exception {
    1.65 +                if (apiServer != null) {
    1.66 +                    apiServer.stop(0);
    1.67 +                }
    1.68                  server.stop(0);
    1.69                  return null;
    1.70              }
     2.1 --- a/freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java	Sat Oct 10 17:15:35 2009 +0200
     2.2 +++ b/freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java	Sun Oct 11 14:20:19 2009 +0200
     2.3 @@ -58,6 +58,7 @@
     2.4      private static File dir;
     2.5      private static Callable<Void> stop;
     2.6      private WebResource webResource;
     2.7 +    private WebResource apiResource;
     2.8  
     2.9      public UITest() throws Exception {
    2.10      }
    2.11 @@ -69,7 +70,7 @@
    2.12          dir.delete();
    2.13          dir.mkdirs();
    2.14          System.setProperty("quoridor.dir", dir.getPath());
    2.15 -        stop = UI.startServers(9991);
    2.16 +        stop = UI.startServers(9991, null);
    2.17  
    2.18          File passwd = new File(dir, "passwd");
    2.19          FileOutputStream os = new FileOutputStream(passwd);
    2.20 @@ -82,6 +83,7 @@
    2.21  
    2.22          Client client = new Client();
    2.23          webResource = client.resource(new URI("http://localhost:9991/"));
    2.24 +        apiResource = client.resource(new URI("http://localhost:9990/api/"));
    2.25      }
    2.26  
    2.27      @AfterClass
    2.28 @@ -116,7 +118,7 @@
    2.29      }
    2.30  
    2.31      @Test public void testGetIndexPage() throws Exception {
    2.32 -        String logJarda = webResource.path("api/login").
    2.33 +        String logJarda = apiResource.path("login").
    2.34              queryParam("name", "Jarda").
    2.35              queryParam("password", "darda").
    2.36              accept(MediaType.TEXT_PLAIN).
    2.37 @@ -158,7 +160,7 @@
    2.38  
    2.39  
    2.40      @Test public void testCreateGameWrongUsers() throws Exception {
    2.41 -        String logTest = webResource.path("api/login").
    2.42 +        String logTest = apiResource.path("login").
    2.43              queryParam("name", "test").
    2.44              queryParam("password", "pes").
    2.45              accept(MediaType.TEXT_PLAIN).
    2.46 @@ -177,12 +179,12 @@
    2.47      }
    2.48  
    2.49      @Test public void testCreateGameOK() throws Exception {
    2.50 -        String logTest = webResource.path("api/login").
    2.51 +        String logTest = apiResource.path("login").
    2.52              queryParam("name", "test").
    2.53              queryParam("password", "pes").
    2.54              accept(MediaType.TEXT_PLAIN).
    2.55              put(String.class);
    2.56 -        String logJarda = webResource.path("api/login").
    2.57 +        String logJarda = apiResource.path("login").
    2.58              queryParam("name", "Jarda").
    2.59              queryParam("password", "darda").
    2.60              accept(MediaType.TEXT_PLAIN).
    2.61 @@ -217,7 +219,7 @@
    2.62          InputStream img1 = webResource.path(m.group(1)).get(InputStream.class);
    2.63          assertNotNull("image found", img1);
    2.64  
    2.65 -        ClientResponse move = webResource.path("api/games/" + games[0]).
    2.66 +        ClientResponse move = apiResource.path("games/" + games[0]).
    2.67              queryParam("loginID", logJarda).
    2.68              queryParam("player", "Jarda").queryParam("move", "N").put(ClientResponse.class);
    2.69          assertEquals("Move OK:\n" + move.getEntity(String.class), ClientResponse.Status.OK, move.getClientResponseStatus());
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/webidor/all-zip.xml	Sun Oct 11 14:20:19 2009 +0200
     3.3 @@ -0,0 +1,19 @@
     3.4 +<?xml version="1.0" encoding="UTF-8"?>
     3.5 +<assembly>
     3.6 + <id>all</id>
     3.7 +  <formats>
     3.8 +    <format>zip</format>
     3.9 +  </formats>
    3.10 +  <dependencySets>
    3.11 +    <dependencySet>
    3.12 +        <useProjectArtifact>false</useProjectArtifact>
    3.13 +        <outputDirectory>lib</outputDirectory>
    3.14 +    </dependencySet>
    3.15 +  </dependencySets>
    3.16 +  <files>
    3.17 +    <file>
    3.18 +      <source>target/webidor.jar</source>
    3.19 +      <outputDirectory>/</outputDirectory>
    3.20 +    </file>
    3.21 +  </files>
    3.22 +</assembly>
     4.1 --- a/webidor/pom.xml	Sat Oct 10 17:15:35 2009 +0200
     4.2 +++ b/webidor/pom.xml	Sun Oct 11 14:20:19 2009 +0200
     4.3 @@ -5,7 +5,7 @@
     4.4          <artifactId>all-quoridor</artifactId>
     4.5          <groupId>org.apidesign</groupId>
     4.6          <version>1.0</version>
     4.7 -    </parent>
     4.8 +  </parent>
     4.9    <groupId>org.apidesign</groupId>
    4.10    <artifactId>webidor</artifactId>
    4.11    <packaging>jar</packaging>
    4.12 @@ -88,7 +88,39 @@
    4.13          <groupId>org.codehaus.mojo</groupId>
    4.14          <artifactId>exec-maven-plugin</artifactId>
    4.15          <configuration>
    4.16 -            <mainClass>cz.xelfi.quoridor.webidor.Quoridor</mainClass>
    4.17 +            <mainClass>cz.xelfi.quoridor.webidor.resources.Quoridor</mainClass>
    4.18 +        </configuration>
    4.19 +      </plugin>
    4.20 +      <plugin>
    4.21 +        <artifactId>maven-assembly-plugin</artifactId>
    4.22 +        <version>2.2-beta-2</version>
    4.23 +        <executions>
    4.24 +          <execution>
    4.25 +            <id>create-executable-jar</id>
    4.26 +            <phase>package</phase>
    4.27 +            <goals>
    4.28 +              <goal>single</goal>
    4.29 +            </goals>
    4.30 +            <configuration>
    4.31 +              <descriptors>
    4.32 +                <descriptor>all-zip.xml</descriptor>
    4.33 +              </descriptors>
    4.34 +              <finalName>webidor-${version}</finalName>
    4.35 +            </configuration>
    4.36 +          </execution>
    4.37 +        </executions>
    4.38 +      </plugin>
    4.39 +      <plugin>
    4.40 +        <groupId>org.apache.maven.plugins</groupId>
    4.41 +        <artifactId>maven-jar-plugin</artifactId>
    4.42 +        <configuration>
    4.43 +            <archive>
    4.44 +                <manifest>
    4.45 +                    <addClasspath>true</addClasspath>
    4.46 +                    <classpathPrefix>lib/</classpathPrefix>
    4.47 +                    <mainClass>cz.xelfi.quoridor.webidor.resources.Quoridor</mainClass>
    4.48 +                </manifest>
    4.49 +            </archive>
    4.50          </configuration>
    4.51        </plugin>
    4.52      </plugins>
     5.1 --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java	Sat Oct 10 17:15:35 2009 +0200
     5.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java	Sun Oct 11 14:20:19 2009 +0200
     5.3 @@ -121,9 +121,15 @@
     5.4      //
     5.5  
     5.6      public static void main(String[] args) throws IOException {
     5.7 -        HttpServer s = start(9998);
     5.8 +        int port = 9222;
     5.9 +        try {
    5.10 +            port = Integer.parseInt(args[0]);
    5.11 +        } catch (Exception ex) {
    5.12 +            // OK
    5.13 +        }
    5.14 +        HttpServer s = start(port);
    5.15          System.out.println(
    5.16 -            "Quoridor started at port 9998\n" + "Hit enter to stop it..."
    5.17 +            "Quoridor started at port " + port + "\n" + "Hit enter to stop it..."
    5.18          );
    5.19          System.in.read();
    5.20          s.stop(0);
    5.21 @@ -138,10 +144,11 @@
    5.22          }
    5.23          final String baseUri = "http://localhost:" + port + "/";
    5.24  
    5.25 -        File home = new File(System.getProperty("user.home"));
    5.26 -        File quoridor = new File(home, ".quoridor");
    5.27 -
    5.28 -        System.setProperty("quoridor.dir", quoridor.getPath());
    5.29 +        if (System.getProperty("quoridor.dir") == null) {
    5.30 +            File home = new File(System.getProperty("user.home"));
    5.31 +            File quoridor = new File(home, ".quoridor");
    5.32 +            System.setProperty("quoridor.dir", quoridor.getPath());
    5.33 +        }
    5.34  
    5.35          ResourceConfig rc = new PackagesResourceConfig("cz.xelfi.quoridor.webidor");
    5.36          HttpServer server = HttpServerFactory.create(baseUri, rc);
     6.1 --- a/webidor/src/test/java/cz/xelfi/quoridor/webidor/resources/ChatTest.java	Sat Oct 10 17:15:35 2009 +0200
     6.2 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/resources/ChatTest.java	Sun Oct 11 14:20:19 2009 +0200
     6.3 @@ -175,10 +175,15 @@
     6.4              fail();
     6.5          }
     6.6  
     6.7 +        Thread.sleep(1500);
     6.8 +
     6.9          File tmp = File.createTempFile("test-board", "game");
    6.10          read.storeGame(rg, tmp);
    6.11  
    6.12 -        assertEquals("Newly written file remains the same", readFile(game), readFile(tmp));
    6.13 +        String sss1 = readFile(game);
    6.14 +        Thread.sleep(1500);
    6.15 +
    6.16 +        assertEquals("Newly written file remains the same", sss1, readFile(tmp));
    6.17  
    6.18          String sGame = webResource.path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(String.class);
    6.19          Game readGame = webResource.path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(Game.class);