webidor/src/test/java/cz/xelfi/quoridor/webidor/resources/ChatTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 14 Sep 2010 08:56:13 +0200
changeset 264 d60370059c3c
parent 258 935118a5831a
child 285 bc4ddef89763
permissions -rw-r--r--
Changing headers to GPLv3
     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 cz.xelfi.quoridor.Board;
    25 import cz.xelfi.quoridor.Move;
    26 import cz.xelfi.quoridor.webidor.Game;
    27 import cz.xelfi.quoridor.webidor.GameId;
    28 import cz.xelfi.quoridor.webidor.Note;
    29 import java.io.File;
    30 import java.io.FileInputStream;
    31 import java.io.FileNotFoundException;
    32 import java.io.FileOutputStream;
    33 import java.io.IOException;
    34 import java.io.InputStreamReader;
    35 import java.io.Reader;
    36 import java.util.List;
    37 import java.util.ResourceBundle;
    38 import javax.ws.rs.core.MediaType;
    39 import org.junit.Test;
    40 import static org.junit.Assert.*;
    41 
    42 /**
    43  *
    44  * @author Jaroslav Tulach <jtulach@netbeans.org>
    45  */
    46 public class ChatTest extends JerseyTest {
    47     static {
    48         System.setProperty("JERSEY_HTTP_PORT", "33437");
    49     }
    50     private File dir;
    51 
    52     @Override
    53     protected AppDescriptor configure() {
    54         try {
    55             dir = File.createTempFile("quoridor", ".dir");
    56             dir.delete();
    57             System.setProperty("quoridor.dir", dir.getPath());
    58             dir.mkdirs();
    59             File passwd = new File(dir, "passwd");
    60             FileOutputStream os = new FileOutputStream(passwd);
    61             os.write("Jarda=heslo\nJirka=pesko\n".getBytes("UTF-8"));
    62             os.close();
    63         } catch (Exception ex) {
    64             throw new IllegalStateException(ex);
    65         }
    66         return new WebAppDescriptor.Builder("cz.xelfi.quoridor.webidor.resources").build();
    67     }
    68 
    69     @Override
    70     public void tearDown() throws Exception {
    71         super.tearDown();
    72         deleteRec(dir);
    73     }
    74 
    75     static void deleteRec(File dir) throws IOException {
    76         if (dir == null) {
    77             return;
    78         }
    79         File[] arr = dir.listFiles();
    80         if (arr != null) {
    81             for (File f : arr) {
    82                 deleteRec(f);
    83             }
    84         }
    85         dir.delete();
    86     }
    87 
    88     @Test public void testCreateAGame() throws Exception {
    89         String logJarda = resource().path("login").
    90             queryParam("name", "Jarda").
    91             queryParam("password", "heslo").
    92             accept(MediaType.TEXT_PLAIN).
    93             put(String.class);
    94         String logJirka = resource().path("login").
    95             queryParam("name", "Jirka").
    96             queryParam("password", "pesko").
    97             accept(MediaType.TEXT_PLAIN).
    98             put(String.class);
    99     GameId s = resource().path("games").queryParam("loginID", logJarda).
   100                 queryParam("white", "Jarda")
   101                 .queryParam("black", "Jirka").post(GameId.class);
   102 
   103         Thread.sleep(100);
   104         final long now = System.currentTimeMillis();
   105         if (s.getModified() >= now) {
   106             fail("The game is supposed to be modified in past");
   107         }
   108         Thread.sleep(100);
   109 
   110         ResourceBundle b = ResourceBundle.getBundle("cz/xelfi/quoridor/webidor/TestBundle");
   111         String comment = b.getString("COMMENT");
   112 
   113         GameId s1 = resource().path("games/" + s.getId()).
   114             queryParam("loginID", logJarda).
   115             queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
   116 
   117         GameId comment1 = resource().path("games/" + s.getId()).
   118             queryParam("loginID", logJarda).
   119             queryParam("player", "Jarda").queryParam("comment", "I like this game!").put(GameId.class);
   120         assertEquals("One comment in the game", 1, comment1.getComments());
   121 
   122         GameId comment2 = resource().path("games/" + s.getId()).
   123             queryParam("loginID", logJirka).
   124             queryParam("player", "Jirka").queryParam("comment", comment).put(GameId.class);
   125         assertEquals("Snd comment in the game", 2, comment2.getComments());
   126 
   127         GameId s2 = resource().path("games/" + s.getId()).
   128             queryParam("loginID", logJirka).
   129             queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
   130         assertNotNull("Successful move", s2);
   131         assertEquals("Still two comments in the game", 2, s2.getComments());
   132 
   133         File game = new File(new File(dir, "games"), s1.getId());
   134         assertTrue("File for game exists", game.exists());
   135         String content = readFile(game);
   136 
   137         if (!content.contains("# white: Jarda")) {
   138             fail(content);
   139         }
   140         if (!content.contains("# black: Jirka")) {
   141             fail(content);
   142         }
   143         if (!content.contains("N")) {
   144             fail(content);
   145         }
   146         if (!content.contains("... S")) {
   147             fail(content);
   148         }
   149         if (!content.contains("I like")) {
   150             fail(content);
   151         }
   152         if (!content.contains(comment)) {
   153             fail(content);
   154         }
   155 
   156         Games read = new Games(new File(dir, "games"), new Quoridor());
   157         List<Game> readGames = read.getGames();
   158         assertEquals("One game read", 1, readGames.size());
   159         Board board = readGames.get(0).getBoard();
   160         assertEquals(1, board.getPlayers().get(0).getRow());
   161         assertEquals(7, board.getPlayers().get(1).getRow());
   162         assertEquals(Move.NORTH, readGames.get(0).getMoves().get(0).getMove());
   163         assertEquals(Move.SOUTH, readGames.get(0).getMoves().get(1).getMove());
   164 
   165         Game rg = readGames.get(0);
   166         assertNull("No comments on second move", rg.getMoves().get(1).getComments());
   167         List<Note> cmnts = rg.getMoves().get(0).getComments();
   168         assertNotNull("Some comments on first move", cmnts);
   169         assertEquals("Two comments: " + cmnts, 2, cmnts.size());
   170 
   171         if (!cmnts.get(0).getComment().contains("I like")) {
   172             fail();
   173         }
   174         if (!cmnts.get(1).getComment().contains(comment)) {
   175             fail();
   176         }
   177 
   178         Thread.sleep(1500);
   179 
   180         File tmp = File.createTempFile("test-board", "game");
   181         read.storeGame(rg, tmp);
   182 
   183         String sss1 = readFile(game).replaceAll("@.*:", "date");
   184         Thread.sleep(1500);
   185 
   186         assertEquals("Newly written file remains the same", sss1, readFile(tmp).replaceAll("@.*:", "date"));
   187 
   188         String sGame = resource().path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(String.class);
   189         Game readGame = resource().path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
   190         assertNotNull("Game really returned", readGame);
   191 
   192         assertEquals(Move.NORTH, readGame.getMoves().get(0).getMove());
   193         assertEquals(Move.SOUTH, readGame.getMoves().get(1).getMove());
   194 
   195         assertNull("No comments on second move", readGame.getMoves().get(1).getComments());
   196         cmnts = readGame.getMoves().get(0).getComments();
   197         assertNotNull("Some comments on first move", cmnts);
   198         assertEquals("Two comments: " + cmnts, 2, cmnts.size());
   199         if (!cmnts.get(0).getComment().contains("I like")) {
   200             fail();
   201         }
   202         if (!cmnts.get(1).getComment().contains(comment)) {
   203             fail();
   204         }
   205     }
   206 
   207     private String readFile(File game) throws IOException, FileNotFoundException {
   208         char[] arr = new char[4096];
   209         Reader gameContent = new InputStreamReader(new FileInputStream(game), "UTF-8");
   210         int len = gameContent.read(arr);
   211         String content = new String(arr, 0, len);
   212         return content;
   213     }
   214 
   215 }