webidor/src/test/java/cz/xelfi/quoridor/webidor/resources/ChatTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 20 Aug 2011 17:14:23 +0200
branchglassfish
changeset 285 bc4ddef89763
parent 264 d60370059c3c
permissions -rw-r--r--
Adjusting tests to use different path
     1 /*
     2  * Quoridor server and related libraries
     3  * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://www.gnu.org/licenses/.
    17  */
    18 
    19 package cz.xelfi.quoridor.webidor.resources;
    20 
    21 import com.sun.jersey.test.framework.WebAppDescriptor;
    22 import com.sun.jersey.test.framework.AppDescriptor;
    23 import com.sun.jersey.test.framework.JerseyTest;
    24 import com.sun.jersey.test.framework.WebAppDescriptor.Builder;
    25 import cz.xelfi.quoridor.Board;
    26 import cz.xelfi.quoridor.Move;
    27 import cz.xelfi.quoridor.webidor.Game;
    28 import cz.xelfi.quoridor.webidor.GameId;
    29 import cz.xelfi.quoridor.webidor.Note;
    30 import java.io.File;
    31 import java.io.FileInputStream;
    32 import java.io.FileNotFoundException;
    33 import java.io.FileOutputStream;
    34 import java.io.IOException;
    35 import java.io.InputStreamReader;
    36 import java.io.Reader;
    37 import java.util.List;
    38 import java.util.ResourceBundle;
    39 import javax.ws.rs.core.MediaType;
    40 import org.junit.Test;
    41 import static org.junit.Assert.*;
    42 
    43 /**
    44  *
    45  * @author Jaroslav Tulach <jtulach@netbeans.org>
    46  */
    47 public class ChatTest extends JerseyTest {
    48     static {
    49         System.setProperty("JERSEY_HTTP_PORT", "33437");
    50     }
    51     private File dir;
    52 
    53     @Override
    54     protected AppDescriptor configure() {
    55         try {
    56             dir = File.createTempFile("quoridor", ".dir");
    57             dir.delete();
    58             System.setProperty("quoridor.dir", dir.getPath());
    59             dir.mkdirs();
    60             File passwd = new File(dir, "passwd");
    61             FileOutputStream os = new FileOutputStream(passwd);
    62             os.write("Jarda=heslo\nJirka=pesko\n".getBytes("UTF-8"));
    63             os.close();
    64         } catch (Exception ex) {
    65             throw new IllegalStateException(ex);
    66         }
    67         final Builder builder = new WebAppDescriptor.Builder("cz.xelfi.quoridor.webidor.resources");
    68         return builder.contextPath("context").build();
    69     }
    70 
    71     @Override
    72     public void tearDown() throws Exception {
    73         super.tearDown();
    74         deleteRec(dir);
    75     }
    76 
    77     static void deleteRec(File dir) throws IOException {
    78         if (dir == null) {
    79             return;
    80         }
    81         File[] arr = dir.listFiles();
    82         if (arr != null) {
    83             for (File f : arr) {
    84                 deleteRec(f);
    85             }
    86         }
    87         dir.delete();
    88     }
    89 
    90     @Test public void testCreateAGame() throws Exception {
    91         String logJarda = resource().path("login").
    92             queryParam("name", "Jarda").
    93             queryParam("password", "heslo").
    94             accept(MediaType.TEXT_PLAIN).
    95             put(String.class);
    96         String logJirka = resource().path("login").
    97             queryParam("name", "Jirka").
    98             queryParam("password", "pesko").
    99             accept(MediaType.TEXT_PLAIN).
   100             put(String.class);
   101     GameId s = resource().path("games").queryParam("loginID", logJarda).
   102                 queryParam("white", "Jarda")
   103                 .queryParam("black", "Jirka").post(GameId.class);
   104 
   105         Thread.sleep(100);
   106         final long now = System.currentTimeMillis();
   107         if (s.getModified() >= now) {
   108             fail("The game is supposed to be modified in past");
   109         }
   110         Thread.sleep(100);
   111 
   112         ResourceBundle b = ResourceBundle.getBundle("cz/xelfi/quoridor/webidor/TestBundle");
   113         String comment = b.getString("COMMENT");
   114 
   115         GameId s1 = resource().path("games/" + s.getId()).
   116             queryParam("loginID", logJarda).
   117             queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
   118 
   119         GameId comment1 = resource().path("games/" + s.getId()).
   120             queryParam("loginID", logJarda).
   121             queryParam("player", "Jarda").queryParam("comment", "I like this game!").put(GameId.class);
   122         assertEquals("One comment in the game", 1, comment1.getComments());
   123 
   124         GameId comment2 = resource().path("games/" + s.getId()).
   125             queryParam("loginID", logJirka).
   126             queryParam("player", "Jirka").queryParam("comment", comment).put(GameId.class);
   127         assertEquals("Snd comment in the game", 2, comment2.getComments());
   128 
   129         GameId s2 = resource().path("games/" + s.getId()).
   130             queryParam("loginID", logJirka).
   131             queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
   132         assertNotNull("Successful move", s2);
   133         assertEquals("Still two comments in the game", 2, s2.getComments());
   134 
   135         File game = new File(new File(dir, "games"), s1.getId());
   136         assertTrue("File for game exists", game.exists());
   137         String content = readFile(game);
   138 
   139         if (!content.contains("# white: Jarda")) {
   140             fail(content);
   141         }
   142         if (!content.contains("# black: Jirka")) {
   143             fail(content);
   144         }
   145         if (!content.contains("N")) {
   146             fail(content);
   147         }
   148         if (!content.contains("... S")) {
   149             fail(content);
   150         }
   151         if (!content.contains("I like")) {
   152             fail(content);
   153         }
   154         if (!content.contains(comment)) {
   155             fail(content);
   156         }
   157 
   158         Games read = new Games(new File(dir, "games"), new Quoridor());
   159         List<Game> readGames = read.getGames();
   160         assertEquals("One game read", 1, readGames.size());
   161         Board board = readGames.get(0).getBoard();
   162         assertEquals(1, board.getPlayers().get(0).getRow());
   163         assertEquals(7, board.getPlayers().get(1).getRow());
   164         assertEquals(Move.NORTH, readGames.get(0).getMoves().get(0).getMove());
   165         assertEquals(Move.SOUTH, readGames.get(0).getMoves().get(1).getMove());
   166 
   167         Game rg = readGames.get(0);
   168         assertNull("No comments on second move", rg.getMoves().get(1).getComments());
   169         List<Note> cmnts = rg.getMoves().get(0).getComments();
   170         assertNotNull("Some comments on first move", cmnts);
   171         assertEquals("Two comments: " + cmnts, 2, cmnts.size());
   172 
   173         if (!cmnts.get(0).getComment().contains("I like")) {
   174             fail();
   175         }
   176         if (!cmnts.get(1).getComment().contains(comment)) {
   177             fail();
   178         }
   179 
   180         Thread.sleep(1500);
   181 
   182         File tmp = File.createTempFile("test-board", "game");
   183         read.storeGame(rg, tmp);
   184 
   185         String sss1 = readFile(game).replaceAll("@.*:", "date");
   186         Thread.sleep(1500);
   187 
   188         assertEquals("Newly written file remains the same", sss1, readFile(tmp).replaceAll("@.*:", "date"));
   189 
   190         String sGame = resource().path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(String.class);
   191         Game readGame = resource().path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
   192         assertNotNull("Game really returned", readGame);
   193 
   194         assertEquals(Move.NORTH, readGame.getMoves().get(0).getMove());
   195         assertEquals(Move.SOUTH, readGame.getMoves().get(1).getMove());
   196 
   197         assertNull("No comments on second move", readGame.getMoves().get(1).getComments());
   198         cmnts = readGame.getMoves().get(0).getComments();
   199         assertNotNull("Some comments on first move", cmnts);
   200         assertEquals("Two comments: " + cmnts, 2, cmnts.size());
   201         if (!cmnts.get(0).getComment().contains("I like")) {
   202             fail();
   203         }
   204         if (!cmnts.get(1).getComment().contains(comment)) {
   205             fail();
   206         }
   207     }
   208 
   209     private String readFile(File game) throws IOException, FileNotFoundException {
   210         char[] arr = new char[4096];
   211         Reader gameContent = new InputStreamReader(new FileInputStream(game), "UTF-8");
   212         int len = gameContent.read(arr);
   213         String content = new String(arr, 0, len);
   214         return content;
   215     }
   216 
   217 }