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