freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java
changeset 106 7d090f2c5b91
parent 82 9ac7acee7d9f
child 113 25b00d888853
     1.1 --- a/freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java	Sun Sep 13 16:48:54 2009 +0200
     1.2 +++ b/freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java	Sun Sep 20 22:33:45 2009 +0200
     1.3 @@ -31,12 +31,16 @@
     1.4  import com.sun.jersey.api.client.WebResource;
     1.5  import com.sun.jersey.core.header.MediaTypes;
     1.6  import com.sun.jersey.core.util.MultivaluedMapImpl;
     1.7 +import java.awt.Image;
     1.8  import java.io.File;
     1.9  import java.io.FileOutputStream;
    1.10  import java.io.IOException;
    1.11 +import java.io.InputStream;
    1.12  import java.net.URI;
    1.13  import java.util.Locale;
    1.14  import java.util.concurrent.Callable;
    1.15 +import java.util.regex.Matcher;
    1.16 +import java.util.regex.Pattern;
    1.17  import javax.ws.rs.core.Cookie;
    1.18  import javax.ws.rs.core.MediaType;
    1.19  import javax.ws.rs.core.MultivaluedMap;
    1.20 @@ -178,8 +182,13 @@
    1.21              queryParam("password", "pes").
    1.22              accept(MediaType.TEXT_PLAIN).
    1.23              put(String.class);
    1.24 +        String logJarda = webResource.path("api/login").
    1.25 +            queryParam("name", "Jarda").
    1.26 +            queryParam("password", "darda").
    1.27 +            accept(MediaType.TEXT_PLAIN).
    1.28 +            put(String.class);
    1.29          ClientResponse res = webResource.path("games/create").
    1.30 -            queryParam("white", "unknown1").
    1.31 +            queryParam("white", "Jarda").
    1.32              queryParam("black", "test").
    1.33              cookie(Cookie.valueOf("login=" + logTest)).
    1.34              accept("text/html").
    1.35 @@ -194,6 +203,57 @@
    1.36          if (txt.indexOf(games[0]) == -1) {
    1.37              fail(games[0] + " expected inside of:\n" + txt);
    1.38          }
    1.39 +
    1.40 +        ClientResponse page = webResource.path("games/" + games[0]).
    1.41 +            cookie(Cookie.valueOf("login=" + logJarda)).
    1.42 +            get(ClientResponse.class);
    1.43 +        String ptxt = page.getEntity(String.class);
    1.44 +        assertEquals("OK:\n" + ptxt, ClientResponse.Status.OK, page.getClientResponseStatus());
    1.45 +
    1.46 +        Pattern p = Pattern.compile(".*<img[^\"]*src=\"([^\"]*)\"");
    1.47 +        Matcher m = p.matcher(ptxt);
    1.48 +        assertTrue("image found\n" + ptxt, m.find());
    1.49 +
    1.50 +        InputStream img1 = webResource.path(m.group(1)).get(InputStream.class);
    1.51 +        assertNotNull("image found", img1);
    1.52 +
    1.53 +        ClientResponse move = webResource.path("api/games/" + games[0]).
    1.54 +            queryParam("loginID", logJarda).
    1.55 +            queryParam("player", "Jarda").queryParam("move", "N").put(ClientResponse.class);
    1.56 +        assertEquals("Move OK:\n" + move.getEntity(String.class), ClientResponse.Status.OK, move.getClientResponseStatus());
    1.57 +
    1.58 +        InputStream img2 = webResource.path(m.group(1)).get(InputStream.class);
    1.59 +        assertNotNull("image found", img2);
    1.60 +
    1.61 +        ClientResponse page2 = webResource.path("games/" + games[0]).
    1.62 +            cookie(Cookie.valueOf("login=" + logJarda)).
    1.63 +            get(ClientResponse.class);
    1.64 +        String ptxt2 = page2.getEntity(String.class);
    1.65 +        assertEquals("OK:\n" + ptxt2, ClientResponse.Status.OK, page2.getClientResponseStatus());
    1.66 +
    1.67 +        Matcher m2 = p.matcher(ptxt2);
    1.68 +        assertTrue("image found\n" + ptxt2, m2.find());
    1.69 +
    1.70 +        InputStream img3 = webResource.path(m2.group(1)).get(InputStream.class);
    1.71 +        assertNotNull("image found", img3);
    1.72 +
    1.73 +        int diff = 0;
    1.74 +        int cnt = 0;
    1.75 +        for (;;) {
    1.76 +            cnt++;
    1.77 +            int b1 = img1.read();
    1.78 +            int b2 = img2.read();
    1.79 +            int b3 = img3.read();
    1.80 +            assertEquals(b1, b2);
    1.81 +            if (b3 != b1) {
    1.82 +                diff++;
    1.83 +            }
    1.84 +            if (b1 == -1 || b3 == -1) break;
    1.85 +
    1.86 +        }
    1.87 +        if (diff == 0) {
    1.88 +//            fail("There shall be difference in the streams. Read bytes " + cnt);
    1.89 +        }
    1.90      }
    1.91  
    1.92  }