webidor/src/test/java/cz/xelfi/quoridor/webidor/QuoridorTest.java
changeset 258 935118a5831a
parent 179 c5fbddc4c590
child 264 d60370059c3c
     1.1 --- a/webidor/src/test/java/cz/xelfi/quoridor/webidor/QuoridorTest.java	Thu Jan 07 22:51:17 2010 +0100
     1.2 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/QuoridorTest.java	Sun Sep 12 00:06:44 2010 +0200
     1.3 @@ -26,12 +26,14 @@
     1.4  
     1.5  package cz.xelfi.quoridor.webidor;
     1.6  
     1.7 +import com.sun.jersey.test.framework.AppDescriptor;
     1.8  import cz.xelfi.quoridor.IllegalPositionException;
     1.9  import java.io.StringWriter;
    1.10  import com.sun.jersey.api.client.GenericType;
    1.11  import com.sun.jersey.api.client.UniformInterfaceException;
    1.12  import com.sun.jersey.core.header.MediaTypes;
    1.13  import com.sun.jersey.test.framework.JerseyTest;
    1.14 +import com.sun.jersey.test.framework.WebAppDescriptor;
    1.15  import cz.xelfi.quoridor.Board;
    1.16  import cz.xelfi.quoridor.Move;
    1.17  import cz.xelfi.quoridor.webidor.resources.Games;
    1.18 @@ -57,22 +59,24 @@
    1.19      }
    1.20      private File dir;
    1.21  
    1.22 -    public QuoridorTest() throws Exception {
    1.23 -        super("cz.xelfi.quoridor.webidor.resources");
    1.24 +    @Override
    1.25 +    protected AppDescriptor configure() {
    1.26 +        try {
    1.27 +            dir = File.createTempFile("quoridor", ".dir");
    1.28 +            dir.delete();
    1.29 +            System.setProperty("quoridor.dir", dir.getPath());
    1.30 +            dir.mkdirs();
    1.31 +            File passwd = new File(dir, "passwd");
    1.32 +            FileOutputStream os = new FileOutputStream(passwd);
    1.33 +            os.write("Jarda=heslo\nJirka=pesko\n".getBytes("UTF-8"));
    1.34 +            os.close();
    1.35 +        } catch (Exception ex) {
    1.36 +            throw new IllegalStateException(ex);
    1.37 +        }
    1.38 +        return new WebAppDescriptor.Builder("cz.xelfi.quoridor.webidor.resources").build();
    1.39      }
    1.40  
    1.41 -    @Override
    1.42 -    public void setUp() throws Exception {
    1.43 -        dir = File.createTempFile("quoridor", ".dir");
    1.44 -        dir.delete();
    1.45 -        System.setProperty("quoridor.dir", dir.getPath());
    1.46 -        dir.mkdirs();
    1.47 -        File passwd = new File(dir, "passwd");
    1.48 -        FileOutputStream os = new FileOutputStream(passwd);
    1.49 -        os.write("Jarda=heslo\nJirka=pesko\n".getBytes("UTF-8"));
    1.50 -        os.close();
    1.51 -        super.setUp();
    1.52 -    }
    1.53 +
    1.54  
    1.55      @Override
    1.56      public void tearDown() throws Exception {
    1.57 @@ -94,24 +98,24 @@
    1.58      }
    1.59  
    1.60      @Test public void testApplicationWadl() {
    1.61 -        String serviceWadl = webResource.path("application.wadl").
    1.62 +        String serviceWadl = resource().path("application.wadl").
    1.63                  accept(MediaTypes.WADL).get(String.class);
    1.64          assertTrue(serviceWadl.length() > 0);
    1.65      }
    1.66  
    1.67      @Test public void testCreateAGame() throws Exception {
    1.68 -        String logJarda = webResource.path("login").
    1.69 +        String logJarda = resource().path("login").
    1.70              queryParam("name", "Jarda").
    1.71              queryParam("password", "heslo").
    1.72              accept(MediaType.TEXT_PLAIN).
    1.73              put(String.class);
    1.74 -        String logJirka = webResource.path("login").
    1.75 +        String logJirka = resource().path("login").
    1.76              queryParam("name", "Jirka").
    1.77              queryParam("password", "pesko").
    1.78              accept(MediaType.TEXT_PLAIN).
    1.79              put(String.class);
    1.80  
    1.81 -        User uJirka = webResource.path("users").
    1.82 +        User uJirka = resource().path("users").
    1.83              queryParam("loginID", logJirka).
    1.84              accept(MediaType.TEXT_XML).
    1.85              get(User.class);
    1.86 @@ -119,7 +123,7 @@
    1.87          assertEquals("Jirka", uJirka.getId());
    1.88  
    1.89  
    1.90 -    GameId s = webResource.path("games").queryParam("loginID", logJarda).
    1.91 +    GameId s = resource().path("games").queryParam("loginID", logJarda).
    1.92                  queryParam("white", "Jarda")
    1.93                  .queryParam("black", "Jirka").post(GameId.class);
    1.94  
    1.95 @@ -130,21 +134,21 @@
    1.96          }
    1.97          Thread.sleep(100);
    1.98  
    1.99 -        String msg = webResource.path("games").get(String.class);
   1.100 -        //List<Game> games =  webResource.path("games").get(new GenericType<List<Game>>() {});
   1.101 +        String msg = resource().path("games").get(String.class);
   1.102 +        //List<Game> games =  resource().path("games").get(new GenericType<List<Game>>() {});
   1.103  
   1.104          GenericType<List<GameId>> gType = new GenericType<List<GameId>>() {};
   1.105  
   1.106 -        List<GameId> games = webResource.path("games").accept("application/json").get(gType);
   1.107 +        List<GameId> games = resource().path("games").accept("application/json").get(gType);
   1.108          assertEquals("One game", 1, games.size());
   1.109          assertEquals("Same white", "Jarda", games.get(0).getWhite());
   1.110          assertEquals("Same black", "Jirka", games.get(0).getBlack());
   1.111  
   1.112 -        GameId s1 = webResource.path("games/" + s.getId()).
   1.113 +        GameId s1 = resource().path("games/" + s.getId()).
   1.114              queryParam("loginID", logJarda).
   1.115              queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
   1.116          try {
   1.117 -            GameId s2 = webResource.path("games/" + s.getId()).
   1.118 +            GameId s2 = resource().path("games/" + s.getId()).
   1.119                  queryParam("loginID", logJarda).
   1.120                  queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
   1.121              fail("Not Jarda's turn, previous call shall fail");
   1.122 @@ -152,22 +156,22 @@
   1.123              // OK
   1.124          }
   1.125          try {
   1.126 -            GameId s2 = webResource.path("games/" + s.getId()).
   1.127 +            GameId s2 = resource().path("games/" + s.getId()).
   1.128                  queryParam("loginID", logJirka).
   1.129                  queryParam("player", "Jirka").queryParam("move", "NONSENCE").put(GameId.class);
   1.130              fail("Invalid move");
   1.131          } catch (UniformInterfaceException ex) {
   1.132              // OK
   1.133          }
   1.134 -        GameId s2 = webResource.path("games/" + s.getId()).
   1.135 +        GameId s2 = resource().path("games/" + s.getId()).
   1.136              queryParam("loginID", logJirka).
   1.137              queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
   1.138          assertNotNull("Successful move", s2);
   1.139          if (s2.getModified() <= now) {
   1.140              fail("The game is newly modified");
   1.141          }
   1.142 -        Game snapshot = webResource.path("games/" + s.getId()).queryParam("move", "0").accept(MediaType.TEXT_XML).get(Game.class);
   1.143 -        String ssnapshot = webResource.path("games/" + s.getId()).queryParam("move", "0").accept(MediaType.TEXT_XML).get(String.class);
   1.144 +        Game snapshot = resource().path("games/" + s.getId()).queryParam("move", "0").accept(MediaType.TEXT_XML).get(Game.class);
   1.145 +        String ssnapshot = resource().path("games/" + s.getId()).queryParam("move", "0").accept(MediaType.TEXT_XML).get(String.class);
   1.146          assertEquals("All moves listed:\n" + ssnapshot, 2, snapshot.getMoves().size());
   1.147          assertEquals("Current move", 0, snapshot.getCurrentMove());
   1.148          assertEquals("Position 0", 0, snapshot.getBoard().getPlayers().get(0).getRow());
   1.149 @@ -203,13 +207,13 @@
   1.150          assertEquals(Move.SOUTH, readGames.get(0).getMoves().get(1).getMove());
   1.151  
   1.152          class GMap extends GenericType<Map<String,Object>>{}
   1.153 -        String text = webResource.path("games").path(s.getId()).accept(MediaType.TEXT_PLAIN).get(String.class);
   1.154 +        String text = resource().path("games").path(s.getId()).accept(MediaType.TEXT_PLAIN).get(String.class);
   1.155          text = (boardToPicture(Board.valueOf(text)));
   1.156          if (text.indexOf("-----") == -1) {
   1.157              fail("Expecting board:\n" + text);
   1.158          }
   1.159 -        Game readGame = webResource.path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
   1.160 -        String sGame = webResource.path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(String.class);
   1.161 +        Game readGame = resource().path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
   1.162 +        String sGame = resource().path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(String.class);
   1.163          assertNotNull("Game really returned", readGame);
   1.164  //        assertEquals("Same game as in text representation", readGame.getBoard(), Board.valueOf(text));
   1.165          assertEquals("Same game as in text representation", readGame.getBoard(), picture2board(text));