freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java
changeset 272 215b417aac98
parent 264 d60370059c3c
child 276 f0a4e8c41419
     1.1 --- a/freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java	Tue Sep 14 08:56:13 2010 +0200
     1.2 +++ b/freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java	Sat Nov 27 07:52:19 2010 +0100
     1.3 @@ -24,8 +24,6 @@
     1.4  import com.sun.jersey.core.util.MultivaluedMapImpl;
     1.5  import com.sun.net.httpserver.HttpServer;
     1.6  import cz.xelfi.quoridor.webidor.resources.Quoridor;
     1.7 -import cz.xelfi.quoridor.statistics.resources.Statistics;
     1.8 -import java.awt.Image;
     1.9  import java.io.File;
    1.10  import java.io.FileOutputStream;
    1.11  import java.io.IOException;
    1.12 @@ -53,6 +51,7 @@
    1.13      private static HttpServer stopAPI;
    1.14      private static HttpServer stopStatistics;
    1.15      private static Callable<Void> stop;
    1.16 +
    1.17      private WebResource webResource;
    1.18      private WebResource apiResource;
    1.19      private WebResource statResource;
    1.20 @@ -66,6 +65,11 @@
    1.21          dir = File.createTempFile("quoridor", ".dir");
    1.22          dir.delete();
    1.23          dir.mkdirs();
    1.24 +        
    1.25 +        File games = new File(dir, "games");
    1.26 +        copyResource("wrong.encoding", new File(games, "fdcc275c-55ad-4b7e-a776-bde3876b2f2c"));
    1.27 +        
    1.28 +        
    1.29          System.setProperty("quoridor.dir", dir.getPath());
    1.30          stopAPI = Quoridor.start(9990);
    1.31          stopStatistics = Quoridor.start(9992);
    1.32 @@ -190,6 +194,9 @@
    1.33      }
    1.34  
    1.35      @Test public void testCreateGameOK() throws Exception {
    1.36 +        int origGames = new File(dir, "games").list().length;
    1.37 +        
    1.38 +        
    1.39          String logTest = apiResource.path("login").
    1.40              queryParam("name", "test").
    1.41              queryParam("password", "pes").
    1.42 @@ -211,7 +218,7 @@
    1.43          assertEquals("OK\n" + txt, ClientResponse.Status.OK, res.getClientResponseStatus());
    1.44  
    1.45          String[] games = new File(dir, "games").list();
    1.46 -        assertEquals("One game exists", 1, games.length);
    1.47 +        assertEquals("One new game", origGames + 1, games.length);
    1.48  
    1.49          if (txt.indexOf(games[0]) == -1) {
    1.50              fail(games[0] + " expected inside of:\n" + txt);
    1.51 @@ -268,5 +275,49 @@
    1.52  //            fail("There shall be difference in the streams. Read bytes " + cnt);
    1.53          }
    1.54      }
    1.55 +    
    1.56 +    @Test public void testGetWrongGame() throws Exception {
    1.57 +        String logTest = apiResource.path("login").
    1.58 +            queryParam("name", "test").
    1.59 +            queryParam("password", "pes").
    1.60 +            accept(MediaType.TEXT_PLAIN).
    1.61 +            put(String.class);
    1.62 +        
    1.63 +        String xml = apiResource.path("games/fdcc275c-55ad-4b7e-a776-bde3876b2f2c").
    1.64 +            queryParam("name", "test").
    1.65 +            queryParam("password", "pes").
    1.66 +            accept(MediaType.TEXT_XML).
    1.67 +            get(String.class);
    1.68 +        
    1.69 +        if (xml.indexOf((char)0x1b) >= 0) {
    1.70 +            fail(xml.replace((char)0x1b, 'X'));
    1.71 +        }
    1.72 +        
    1.73 +        ClientResponse res = webResource.path("games/fdcc275c-55ad-4b7e-a776-bde3876b2f2c").
    1.74 +            queryParam("white", "Jarda").
    1.75 +            queryParam("black", "test").
    1.76 +            cookie(Cookie.valueOf("login=" + logTest)).
    1.77 +            accept("text/html").
    1.78 +            get(ClientResponse.class);
    1.79 +        final String txt = res.getEntity(String.class);
    1.80 +        assertEquals("OK\n" + txt, ClientResponse.Status.OK, res.getClientResponseStatus());
    1.81 +    }
    1.82 +    
    1.83  
    1.84 +    private static void copyResource(String res, File file) throws IOException {
    1.85 +        InputStream is = UITest.class.getResourceAsStream(res);
    1.86 +        file.getParentFile().mkdirs();
    1.87 +        FileOutputStream os = new FileOutputStream(file);
    1.88 +        byte[] arr = new byte[4092];
    1.89 +        for (;;) {
    1.90 +            int len = is.read(arr);
    1.91 +            if (len == -1) {
    1.92 +                break;
    1.93 +            }
    1.94 +            os.write(arr, 0, len);
    1.95 +        }
    1.96 +        is.close();
    1.97 +        os.close();
    1.98 +    }
    1.99 +    
   1.100  }