webidor/src/test/java/cz/xelfi/quoridor/webidor/resources/ChatTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 28 Sep 2009 14:42:19 +0200
changeset 115 6a80463a74c0
child 117 c1057591a344
permissions -rw-r--r--
Support for comments among the moves
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * The contents of this file are subject to the terms of either the GNU
     5  * General Public License Version 2 only ("GPL") or the Common
     6  * Development and Distribution License("CDDL") (collectively, the
     7  * "License"). You may not use this file except in compliance with the
     8  * License. You can obtain a copy of the License at
     9  * http://www.netbeans.org/cddl-gplv2.html
    10  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    11  * specific language governing permissions and limitations under the
    12  * License.  When distributing the software, include this License Header
    13  * Notice in each file and include the License file at
    14  * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    15  * particular file as subject to the "Classpath" exception as provided
    16  * by Sun in the GPL Version 2 section of the License file that
    17  * accompanied this code. If applicable, add the following below the
    18  * License Header, with the fields enclosed by brackets [] replaced by
    19  * your own identifying information:
    20  * "Portions Copyrighted [year] [name of copyright owner]"
    21  *
    22  * Contributor(s):
    23  *
    24  * Portions Copyrighted 2009 Jaroslav Tulach
    25  */
    26 
    27 package cz.xelfi.quoridor.webidor.resources;
    28 
    29 import com.sun.jersey.test.framework.JerseyTest;
    30 import cz.xelfi.quoridor.Board;
    31 import cz.xelfi.quoridor.Move;
    32 import cz.xelfi.quoridor.webidor.Game;
    33 import cz.xelfi.quoridor.webidor.GameId;
    34 import cz.xelfi.quoridor.webidor.Note;
    35 import cz.xelfi.quoridor.webidor.QuoridorTest;
    36 import java.io.File;
    37 import java.io.FileNotFoundException;
    38 import java.io.FileOutputStream;
    39 import java.io.FileReader;
    40 import java.io.IOException;
    41 import java.util.List;
    42 import javax.ws.rs.core.MediaType;
    43 import org.junit.Test;
    44 import static org.junit.Assert.*;
    45 
    46 /**
    47  *
    48  * @author Jaroslav Tulach <jtulach@netbeans.org>
    49  */
    50 public class ChatTest extends JerseyTest {
    51     private File dir;
    52 
    53     public ChatTest() throws Exception {
    54         super("cz.xelfi.quoridor.webidor.resources");
    55     }
    56 
    57     @Override
    58     public void setUp() throws Exception {
    59         dir = File.createTempFile("quoridor", ".dir");
    60         dir.delete();
    61         System.setProperty("quoridor.dir", dir.getPath());
    62         dir.mkdirs();
    63         File passwd = new File(dir, "passwd");
    64         FileOutputStream os = new FileOutputStream(passwd);
    65         os.write("Jarda=heslo\nJirka=pesko\n".getBytes("UTF-8"));
    66         os.close();
    67         super.setUp();
    68     }
    69 
    70     @Override
    71     public void tearDown() throws Exception {
    72         super.tearDown();
    73         deleteRec(dir);
    74     }
    75 
    76     static void deleteRec(File dir) throws IOException {
    77         if (dir == null) {
    78             return;
    79         }
    80         File[] arr = dir.listFiles();
    81         if (arr != null) {
    82             for (File f : arr) {
    83                 deleteRec(f);
    84             }
    85         }
    86         dir.delete();
    87     }
    88 
    89     @Test public void testCreateAGame() throws Exception {
    90         webResource = webResource.path("api");
    91         String logJarda = webResource.path("login").
    92             queryParam("name", "Jarda").
    93             queryParam("password", "heslo").
    94             accept(MediaType.TEXT_PLAIN).
    95             put(String.class);
    96         String logJirka = webResource.path("login").
    97             queryParam("name", "Jirka").
    98             queryParam("password", "pesko").
    99             accept(MediaType.TEXT_PLAIN).
   100             put(String.class);
   101     GameId s = webResource.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         GameId s1 = webResource.path("games/" + s.getId()).
   113             queryParam("loginID", logJarda).
   114             queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
   115 
   116         GameId comment1 = webResource.path("games/" + s.getId()).
   117             queryParam("loginID", logJarda).
   118             queryParam("player", "Jarda").queryParam("comment", "I like this game!").put(GameId.class);
   119 
   120         GameId comment2 = webResource.path("games/" + s.getId()).
   121             queryParam("loginID", logJirka).
   122             queryParam("player", "Jirka").queryParam("comment", "I love it too!").put(GameId.class);
   123         GameId s2 = webResource.path("games/" + s.getId()).
   124             queryParam("loginID", logJirka).
   125             queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
   126         assertNotNull("Successful move", s2);
   127 
   128         File game = new File(new File(dir, "games"), s1.getId());
   129         assertTrue("File for game exists", game.exists());
   130         String content = readFile(game);
   131 
   132         if (!content.contains("# white: Jarda")) {
   133             fail(content);
   134         }
   135         if (!content.contains("# black: Jirka")) {
   136             fail(content);
   137         }
   138         if (!content.contains("N")) {
   139             fail(content);
   140         }
   141         if (!content.contains("... S")) {
   142             fail(content);
   143         }
   144         if (!content.contains("I like")) {
   145             fail(content);
   146         }
   147         if (!content.contains("I love")) {
   148             fail(content);
   149         }
   150 
   151         Games read = new Games(new File(dir, "games"), new Quoridor());
   152         List<Game> readGames = read.getGames();
   153         assertEquals("One game read", 1, readGames.size());
   154         Board board = readGames.get(0).getBoard();
   155         assertEquals(1, board.getPlayers().get(0).getRow());
   156         assertEquals(7, board.getPlayers().get(1).getRow());
   157         assertEquals(Move.NORTH, readGames.get(0).getMoves().get(0).getMove());
   158         assertEquals(Move.SOUTH, readGames.get(0).getMoves().get(1).getMove());
   159 
   160         Game rg = readGames.get(0);
   161         assertNull("No comments on second move", rg.getMoves().get(1).getComments());
   162         List<Note> cmnts = rg.getMoves().get(0).getComments();
   163         assertNotNull("Some comments on first move", cmnts);
   164         assertEquals("Two comments: " + cmnts, 2, cmnts.size());
   165 
   166         if (!cmnts.get(0).getComment().contains("I like")) {
   167             fail();
   168         }
   169         if (!cmnts.get(1).getComment().contains("I love")) {
   170             fail();
   171         }
   172 
   173         File tmp = File.createTempFile("test-board", "game");
   174         read.storeGame(rg, tmp);
   175 
   176         assertEquals("Newly written file remains the same", readFile(game), readFile(tmp));
   177 
   178         String sGame = webResource.path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(String.class);
   179         Game readGame = webResource.path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
   180         assertNotNull("Game really returned", readGame);
   181 
   182         assertEquals(Move.NORTH, readGame.getMoves().get(0).getMove());
   183         assertEquals(Move.SOUTH, readGame.getMoves().get(1).getMove());
   184 
   185         assertNull("No comments on second move", readGame.getMoves().get(1).getComments());
   186         cmnts = readGame.getMoves().get(0).getComments();
   187         assertNotNull("Some comments on first move", cmnts);
   188         assertEquals("Two comments: " + cmnts, 2, cmnts.size());
   189         if (!cmnts.get(0).getComment().contains("I like")) {
   190             fail();
   191         }
   192         if (!cmnts.get(1).getComment().contains("I love")) {
   193             fail();
   194         }
   195     }
   196 
   197     private String readFile(File game) throws IOException, FileNotFoundException {
   198         char[] arr = new char[4096];
   199         FileReader gameContent = new FileReader(game);
   200         int len = gameContent.read(arr);
   201         String content = new String(arr, 0, len);
   202         return content;
   203     }
   204 
   205 }