Fixing the history of images. Testing that images are present and readable in the game page.
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 20 Sep 2009 22:33:45 +0200
changeset 1067d090f2c5b91
parent 105 6e55d5c85d3c
child 107 152aedcc45d0
child 109 d2140ee1c682
Fixing the history of images. Testing that images are present and readable in the game page.
freemarkerdor/pom.xml
freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java
freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java
webidor/pom.xml
webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java
     1.1 --- a/freemarkerdor/pom.xml	Sun Sep 20 20:04:41 2009 +0200
     1.2 +++ b/freemarkerdor/pom.xml	Sun Sep 20 22:33:45 2009 +0200
     1.3 @@ -10,13 +10,13 @@
     1.4    <groupId>org.apidesign</groupId>
     1.5    <artifactId>freemarkerdor</artifactId>
     1.6    <name>freemarkerdor</name>
     1.7 -  <version>1.18</version>
     1.8 +  <version>1.19</version>
     1.9    <url>http://maven.apache.org</url>
    1.10    <dependencies>
    1.11      <dependency>
    1.12        <groupId>${project.groupId}</groupId>
    1.13        <artifactId>webidor</artifactId>
    1.14 -      <version>1.2</version>
    1.15 +      <version>1.3</version>
    1.16      </dependency>
    1.17      <dependency>
    1.18        <groupId>org.netbeans.modules</groupId>
     2.1 --- a/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java	Sun Sep 20 20:04:41 2009 +0200
     2.2 +++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java	Sun Sep 20 22:33:45 2009 +0200
     2.3 @@ -154,7 +154,7 @@
     2.4      ) throws IllegalPositionException {
     2.5          WebResource wr = base.path("games").path(id);
     2.6          if (move != -1) {
     2.7 -            wr.queryParam("move", "" + move);
     2.8 +            wr = wr.queryParam("move", "" + move);
     2.9          }
    2.10          String txt = wr.accept(MediaType.TEXT_PLAIN).get(String.class);
    2.11          Board b = Board.valueOf(txt);
     3.1 --- a/freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java	Sun Sep 20 20:04:41 2009 +0200
     3.2 +++ b/freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java	Sun Sep 20 22:33:45 2009 +0200
     3.3 @@ -31,12 +31,16 @@
     3.4  import com.sun.jersey.api.client.WebResource;
     3.5  import com.sun.jersey.core.header.MediaTypes;
     3.6  import com.sun.jersey.core.util.MultivaluedMapImpl;
     3.7 +import java.awt.Image;
     3.8  import java.io.File;
     3.9  import java.io.FileOutputStream;
    3.10  import java.io.IOException;
    3.11 +import java.io.InputStream;
    3.12  import java.net.URI;
    3.13  import java.util.Locale;
    3.14  import java.util.concurrent.Callable;
    3.15 +import java.util.regex.Matcher;
    3.16 +import java.util.regex.Pattern;
    3.17  import javax.ws.rs.core.Cookie;
    3.18  import javax.ws.rs.core.MediaType;
    3.19  import javax.ws.rs.core.MultivaluedMap;
    3.20 @@ -178,8 +182,13 @@
    3.21              queryParam("password", "pes").
    3.22              accept(MediaType.TEXT_PLAIN).
    3.23              put(String.class);
    3.24 +        String logJarda = webResource.path("api/login").
    3.25 +            queryParam("name", "Jarda").
    3.26 +            queryParam("password", "darda").
    3.27 +            accept(MediaType.TEXT_PLAIN).
    3.28 +            put(String.class);
    3.29          ClientResponse res = webResource.path("games/create").
    3.30 -            queryParam("white", "unknown1").
    3.31 +            queryParam("white", "Jarda").
    3.32              queryParam("black", "test").
    3.33              cookie(Cookie.valueOf("login=" + logTest)).
    3.34              accept("text/html").
    3.35 @@ -194,6 +203,57 @@
    3.36          if (txt.indexOf(games[0]) == -1) {
    3.37              fail(games[0] + " expected inside of:\n" + txt);
    3.38          }
    3.39 +
    3.40 +        ClientResponse page = webResource.path("games/" + games[0]).
    3.41 +            cookie(Cookie.valueOf("login=" + logJarda)).
    3.42 +            get(ClientResponse.class);
    3.43 +        String ptxt = page.getEntity(String.class);
    3.44 +        assertEquals("OK:\n" + ptxt, ClientResponse.Status.OK, page.getClientResponseStatus());
    3.45 +
    3.46 +        Pattern p = Pattern.compile(".*<img[^\"]*src=\"([^\"]*)\"");
    3.47 +        Matcher m = p.matcher(ptxt);
    3.48 +        assertTrue("image found\n" + ptxt, m.find());
    3.49 +
    3.50 +        InputStream img1 = webResource.path(m.group(1)).get(InputStream.class);
    3.51 +        assertNotNull("image found", img1);
    3.52 +
    3.53 +        ClientResponse move = webResource.path("api/games/" + games[0]).
    3.54 +            queryParam("loginID", logJarda).
    3.55 +            queryParam("player", "Jarda").queryParam("move", "N").put(ClientResponse.class);
    3.56 +        assertEquals("Move OK:\n" + move.getEntity(String.class), ClientResponse.Status.OK, move.getClientResponseStatus());
    3.57 +
    3.58 +        InputStream img2 = webResource.path(m.group(1)).get(InputStream.class);
    3.59 +        assertNotNull("image found", img2);
    3.60 +
    3.61 +        ClientResponse page2 = webResource.path("games/" + games[0]).
    3.62 +            cookie(Cookie.valueOf("login=" + logJarda)).
    3.63 +            get(ClientResponse.class);
    3.64 +        String ptxt2 = page2.getEntity(String.class);
    3.65 +        assertEquals("OK:\n" + ptxt2, ClientResponse.Status.OK, page2.getClientResponseStatus());
    3.66 +
    3.67 +        Matcher m2 = p.matcher(ptxt2);
    3.68 +        assertTrue("image found\n" + ptxt2, m2.find());
    3.69 +
    3.70 +        InputStream img3 = webResource.path(m2.group(1)).get(InputStream.class);
    3.71 +        assertNotNull("image found", img3);
    3.72 +
    3.73 +        int diff = 0;
    3.74 +        int cnt = 0;
    3.75 +        for (;;) {
    3.76 +            cnt++;
    3.77 +            int b1 = img1.read();
    3.78 +            int b2 = img2.read();
    3.79 +            int b3 = img3.read();
    3.80 +            assertEquals(b1, b2);
    3.81 +            if (b3 != b1) {
    3.82 +                diff++;
    3.83 +            }
    3.84 +            if (b1 == -1 || b3 == -1) break;
    3.85 +
    3.86 +        }
    3.87 +        if (diff == 0) {
    3.88 +//            fail("There shall be difference in the streams. Read bytes " + cnt);
    3.89 +        }
    3.90      }
    3.91  
    3.92  }
     4.1 --- a/webidor/pom.xml	Sun Sep 20 20:04:41 2009 +0200
     4.2 +++ b/webidor/pom.xml	Sun Sep 20 22:33:45 2009 +0200
     4.3 @@ -9,7 +9,7 @@
     4.4    <groupId>org.apidesign</groupId>
     4.5    <artifactId>webidor</artifactId>
     4.6    <packaging>jar</packaging>
     4.7 -  <version>1.2</version>
     4.8 +  <version>1.3</version>
     4.9    <name>webidor server</name>
    4.10    <url>http://maven.apache.org</url>
    4.11    <repositories>
     5.1 --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java	Sun Sep 20 20:04:41 2009 +0200
     5.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java	Sun Sep 20 22:33:45 2009 +0200
     5.3 @@ -110,8 +110,11 @@
     5.4      @GET
     5.5      @Path("{id}")
     5.6      @Produces(MediaType.TEXT_PLAIN)
     5.7 -    public String getBoard(@PathParam("id") String id) {
     5.8 -        Game g = findGame(id);
     5.9 +    public String getBoard(
    5.10 +        @PathParam("id") String id,
    5.11 +        @QueryParam("move") @DefaultValue("-1") int move
    5.12 +    ) {
    5.13 +        Game g = findGame(id, move);
    5.14          if (g == null) {
    5.15              throw new IllegalArgumentException("Unknown game " + id);
    5.16          }