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