webidor/src/test/java/cz/xelfi/quoridor/webidor/resources/ChatTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 11 Oct 2009 14:20:19 +0200
changeset 121 95dfb04fcee1
parent 118 bbe71fd2f13b
child 124 90371f3eb106
permissions -rw-r--r--
Getting ready for separate execution of API as well as UI
     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     private File dir;
    54 
    55     public ChatTest() throws Exception {
    56         super("cz.xelfi.quoridor.webidor.resources");
    57     }
    58 
    59     @Override
    60     public void setUp() throws Exception {
    61         dir = File.createTempFile("quoridor", ".dir");
    62         dir.delete();
    63         System.setProperty("quoridor.dir", dir.getPath());
    64         dir.mkdirs();
    65         File passwd = new File(dir, "passwd");
    66         FileOutputStream os = new FileOutputStream(passwd);
    67         os.write("Jarda=heslo\nJirka=pesko\n".getBytes("UTF-8"));
    68         os.close();
    69         super.setUp();
    70     }
    71 
    72     @Override
    73     public void tearDown() throws Exception {
    74         super.tearDown();
    75         deleteRec(dir);
    76     }
    77 
    78     static void deleteRec(File dir) throws IOException {
    79         if (dir == null) {
    80             return;
    81         }
    82         File[] arr = dir.listFiles();
    83         if (arr != null) {
    84             for (File f : arr) {
    85                 deleteRec(f);
    86             }
    87         }
    88         dir.delete();
    89     }
    90 
    91     @Test public void testCreateAGame() throws Exception {
    92         webResource = webResource.path("api");
    93         String logJarda = webResource.path("login").
    94             queryParam("name", "Jarda").
    95             queryParam("password", "heslo").
    96             accept(MediaType.TEXT_PLAIN).
    97             put(String.class);
    98         String logJirka = webResource.path("login").
    99             queryParam("name", "Jirka").
   100             queryParam("password", "pesko").
   101             accept(MediaType.TEXT_PLAIN).
   102             put(String.class);
   103     GameId s = webResource.path("games").queryParam("loginID", logJarda).
   104                 queryParam("white", "Jarda")
   105                 .queryParam("black", "Jirka").post(GameId.class);
   106 
   107         Thread.sleep(100);
   108         final long now = System.currentTimeMillis();
   109         if (s.getModified() >= now) {
   110             fail("The game is supposed to be modified in past");
   111         }
   112         Thread.sleep(100);
   113 
   114         ResourceBundle b = ResourceBundle.getBundle("cz/xelfi/quoridor/webidor/TestBundle");
   115         String comment = b.getString("COMMENT");
   116 
   117         GameId s1 = webResource.path("games/" + s.getId()).
   118             queryParam("loginID", logJarda).
   119             queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
   120 
   121         GameId comment1 = webResource.path("games/" + s.getId()).
   122             queryParam("loginID", logJarda).
   123             queryParam("player", "Jarda").queryParam("comment", "I like this game!").put(GameId.class);
   124 
   125         GameId comment2 = webResource.path("games/" + s.getId()).
   126             queryParam("loginID", logJirka).
   127             queryParam("player", "Jirka").queryParam("comment", comment).put(GameId.class);
   128         GameId s2 = webResource.path("games/" + s.getId()).
   129             queryParam("loginID", logJirka).
   130             queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
   131         assertNotNull("Successful move", s2);
   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);
   184         Thread.sleep(1500);
   185 
   186         assertEquals("Newly written file remains the same", sss1, readFile(tmp));
   187 
   188         String sGame = webResource.path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(String.class);
   189         Game readGame = webResource.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 }