1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/resources/ChatTest.java Mon Sep 28 14:42:19 2009 +0200
1.3 @@ -0,0 +1,205 @@
1.4 +/*
1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 + *
1.7 + * The contents of this file are subject to the terms of either the GNU
1.8 + * General Public License Version 2 only ("GPL") or the Common
1.9 + * Development and Distribution License("CDDL") (collectively, the
1.10 + * "License"). You may not use this file except in compliance with the
1.11 + * License. You can obtain a copy of the License at
1.12 + * http://www.netbeans.org/cddl-gplv2.html
1.13 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.14 + * specific language governing permissions and limitations under the
1.15 + * License. When distributing the software, include this License Header
1.16 + * Notice in each file and include the License file at
1.17 + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
1.18 + * particular file as subject to the "Classpath" exception as provided
1.19 + * by Sun in the GPL Version 2 section of the License file that
1.20 + * accompanied this code. If applicable, add the following below the
1.21 + * License Header, with the fields enclosed by brackets [] replaced by
1.22 + * your own identifying information:
1.23 + * "Portions Copyrighted [year] [name of copyright owner]"
1.24 + *
1.25 + * Contributor(s):
1.26 + *
1.27 + * Portions Copyrighted 2009 Jaroslav Tulach
1.28 + */
1.29 +
1.30 +package cz.xelfi.quoridor.webidor.resources;
1.31 +
1.32 +import com.sun.jersey.test.framework.JerseyTest;
1.33 +import cz.xelfi.quoridor.Board;
1.34 +import cz.xelfi.quoridor.Move;
1.35 +import cz.xelfi.quoridor.webidor.Game;
1.36 +import cz.xelfi.quoridor.webidor.GameId;
1.37 +import cz.xelfi.quoridor.webidor.Note;
1.38 +import cz.xelfi.quoridor.webidor.QuoridorTest;
1.39 +import java.io.File;
1.40 +import java.io.FileNotFoundException;
1.41 +import java.io.FileOutputStream;
1.42 +import java.io.FileReader;
1.43 +import java.io.IOException;
1.44 +import java.util.List;
1.45 +import javax.ws.rs.core.MediaType;
1.46 +import org.junit.Test;
1.47 +import static org.junit.Assert.*;
1.48 +
1.49 +/**
1.50 + *
1.51 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.52 + */
1.53 +public class ChatTest extends JerseyTest {
1.54 + private File dir;
1.55 +
1.56 + public ChatTest() throws Exception {
1.57 + super("cz.xelfi.quoridor.webidor.resources");
1.58 + }
1.59 +
1.60 + @Override
1.61 + public void setUp() throws Exception {
1.62 + dir = File.createTempFile("quoridor", ".dir");
1.63 + dir.delete();
1.64 + System.setProperty("quoridor.dir", dir.getPath());
1.65 + dir.mkdirs();
1.66 + File passwd = new File(dir, "passwd");
1.67 + FileOutputStream os = new FileOutputStream(passwd);
1.68 + os.write("Jarda=heslo\nJirka=pesko\n".getBytes("UTF-8"));
1.69 + os.close();
1.70 + super.setUp();
1.71 + }
1.72 +
1.73 + @Override
1.74 + public void tearDown() throws Exception {
1.75 + super.tearDown();
1.76 + deleteRec(dir);
1.77 + }
1.78 +
1.79 + static void deleteRec(File dir) throws IOException {
1.80 + if (dir == null) {
1.81 + return;
1.82 + }
1.83 + File[] arr = dir.listFiles();
1.84 + if (arr != null) {
1.85 + for (File f : arr) {
1.86 + deleteRec(f);
1.87 + }
1.88 + }
1.89 + dir.delete();
1.90 + }
1.91 +
1.92 + @Test public void testCreateAGame() throws Exception {
1.93 + webResource = webResource.path("api");
1.94 + String logJarda = webResource.path("login").
1.95 + queryParam("name", "Jarda").
1.96 + queryParam("password", "heslo").
1.97 + accept(MediaType.TEXT_PLAIN).
1.98 + put(String.class);
1.99 + String logJirka = webResource.path("login").
1.100 + queryParam("name", "Jirka").
1.101 + queryParam("password", "pesko").
1.102 + accept(MediaType.TEXT_PLAIN).
1.103 + put(String.class);
1.104 + GameId s = webResource.path("games").queryParam("loginID", logJarda).
1.105 + queryParam("white", "Jarda")
1.106 + .queryParam("black", "Jirka").post(GameId.class);
1.107 +
1.108 + Thread.sleep(100);
1.109 + final long now = System.currentTimeMillis();
1.110 + if (s.getModified() >= now) {
1.111 + fail("The game is supposed to be modified in past");
1.112 + }
1.113 + Thread.sleep(100);
1.114 +
1.115 + GameId s1 = webResource.path("games/" + s.getId()).
1.116 + queryParam("loginID", logJarda).
1.117 + queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
1.118 +
1.119 + GameId comment1 = webResource.path("games/" + s.getId()).
1.120 + queryParam("loginID", logJarda).
1.121 + queryParam("player", "Jarda").queryParam("comment", "I like this game!").put(GameId.class);
1.122 +
1.123 + GameId comment2 = webResource.path("games/" + s.getId()).
1.124 + queryParam("loginID", logJirka).
1.125 + queryParam("player", "Jirka").queryParam("comment", "I love it too!").put(GameId.class);
1.126 + GameId s2 = webResource.path("games/" + s.getId()).
1.127 + queryParam("loginID", logJirka).
1.128 + queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
1.129 + assertNotNull("Successful move", s2);
1.130 +
1.131 + File game = new File(new File(dir, "games"), s1.getId());
1.132 + assertTrue("File for game exists", game.exists());
1.133 + String content = readFile(game);
1.134 +
1.135 + if (!content.contains("# white: Jarda")) {
1.136 + fail(content);
1.137 + }
1.138 + if (!content.contains("# black: Jirka")) {
1.139 + fail(content);
1.140 + }
1.141 + if (!content.contains("N")) {
1.142 + fail(content);
1.143 + }
1.144 + if (!content.contains("... S")) {
1.145 + fail(content);
1.146 + }
1.147 + if (!content.contains("I like")) {
1.148 + fail(content);
1.149 + }
1.150 + if (!content.contains("I love")) {
1.151 + fail(content);
1.152 + }
1.153 +
1.154 + Games read = new Games(new File(dir, "games"), new Quoridor());
1.155 + List<Game> readGames = read.getGames();
1.156 + assertEquals("One game read", 1, readGames.size());
1.157 + Board board = readGames.get(0).getBoard();
1.158 + assertEquals(1, board.getPlayers().get(0).getRow());
1.159 + assertEquals(7, board.getPlayers().get(1).getRow());
1.160 + assertEquals(Move.NORTH, readGames.get(0).getMoves().get(0).getMove());
1.161 + assertEquals(Move.SOUTH, readGames.get(0).getMoves().get(1).getMove());
1.162 +
1.163 + Game rg = readGames.get(0);
1.164 + assertNull("No comments on second move", rg.getMoves().get(1).getComments());
1.165 + List<Note> cmnts = rg.getMoves().get(0).getComments();
1.166 + assertNotNull("Some comments on first move", cmnts);
1.167 + assertEquals("Two comments: " + cmnts, 2, cmnts.size());
1.168 +
1.169 + if (!cmnts.get(0).getComment().contains("I like")) {
1.170 + fail();
1.171 + }
1.172 + if (!cmnts.get(1).getComment().contains("I love")) {
1.173 + fail();
1.174 + }
1.175 +
1.176 + File tmp = File.createTempFile("test-board", "game");
1.177 + read.storeGame(rg, tmp);
1.178 +
1.179 + assertEquals("Newly written file remains the same", readFile(game), readFile(tmp));
1.180 +
1.181 + String sGame = webResource.path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(String.class);
1.182 + Game readGame = webResource.path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
1.183 + assertNotNull("Game really returned", readGame);
1.184 +
1.185 + assertEquals(Move.NORTH, readGame.getMoves().get(0).getMove());
1.186 + assertEquals(Move.SOUTH, readGame.getMoves().get(1).getMove());
1.187 +
1.188 + assertNull("No comments on second move", readGame.getMoves().get(1).getComments());
1.189 + cmnts = readGame.getMoves().get(0).getComments();
1.190 + assertNotNull("Some comments on first move", cmnts);
1.191 + assertEquals("Two comments: " + cmnts, 2, cmnts.size());
1.192 + if (!cmnts.get(0).getComment().contains("I like")) {
1.193 + fail();
1.194 + }
1.195 + if (!cmnts.get(1).getComment().contains("I love")) {
1.196 + fail();
1.197 + }
1.198 + }
1.199 +
1.200 + private String readFile(File game) throws IOException, FileNotFoundException {
1.201 + char[] arr = new char[4096];
1.202 + FileReader gameContent = new FileReader(game);
1.203 + int len = gameContent.read(arr);
1.204 + String content = new String(arr, 0, len);
1.205 + return content;
1.206 + }
1.207 +
1.208 +}