2 * Quoridor server and related libraries
3 * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://www.gnu.org/licenses/.
19 package cz.xelfi.quoridor.webidor.resources;
21 import com.sun.jersey.test.framework.WebAppDescriptor;
22 import com.sun.jersey.test.framework.AppDescriptor;
23 import com.sun.jersey.test.framework.JerseyTest;
24 import cz.xelfi.quoridor.Board;
25 import cz.xelfi.quoridor.Move;
26 import cz.xelfi.quoridor.webidor.Game;
27 import cz.xelfi.quoridor.webidor.GameId;
28 import cz.xelfi.quoridor.webidor.Note;
30 import java.io.FileInputStream;
31 import java.io.FileNotFoundException;
32 import java.io.FileOutputStream;
33 import java.io.IOException;
34 import java.io.InputStreamReader;
35 import java.io.Reader;
36 import java.util.List;
37 import java.util.ResourceBundle;
38 import javax.ws.rs.core.MediaType;
39 import org.junit.Test;
40 import static org.junit.Assert.*;
44 * @author Jaroslav Tulach <jtulach@netbeans.org>
46 public class ChatTest extends JerseyTest {
48 System.setProperty("JERSEY_HTTP_PORT", "33437");
53 protected AppDescriptor configure() {
55 dir = File.createTempFile("quoridor", ".dir");
57 System.setProperty("quoridor.dir", dir.getPath());
59 File passwd = new File(dir, "passwd");
60 FileOutputStream os = new FileOutputStream(passwd);
61 os.write("Jarda=heslo\nJirka=pesko\n".getBytes("UTF-8"));
63 } catch (Exception ex) {
64 throw new IllegalStateException(ex);
66 return new WebAppDescriptor.Builder("cz.xelfi.quoridor.webidor.resources").build();
70 public void tearDown() throws Exception {
75 static void deleteRec(File dir) throws IOException {
79 File[] arr = dir.listFiles();
88 @Test public void testCreateAGame() throws Exception {
89 String logJarda = resource().path("login").
90 queryParam("name", "Jarda").
91 queryParam("password", "heslo").
92 accept(MediaType.TEXT_PLAIN).
94 String logJirka = resource().path("login").
95 queryParam("name", "Jirka").
96 queryParam("password", "pesko").
97 accept(MediaType.TEXT_PLAIN).
99 GameId s = resource().path("games").queryParam("loginID", logJarda).
100 queryParam("white", "Jarda")
101 .queryParam("black", "Jirka").post(GameId.class);
104 final long now = System.currentTimeMillis();
105 if (s.getModified() >= now) {
106 fail("The game is supposed to be modified in past");
110 ResourceBundle b = ResourceBundle.getBundle("cz/xelfi/quoridor/webidor/TestBundle");
111 String comment = b.getString("COMMENT");
113 GameId s1 = resource().path("games/" + s.getId()).
114 queryParam("loginID", logJarda).
115 queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
117 GameId comment1 = resource().path("games/" + s.getId()).
118 queryParam("loginID", logJarda).
119 queryParam("player", "Jarda").queryParam("comment", "I like this game!").put(GameId.class);
120 assertEquals("One comment in the game", 1, comment1.getComments());
122 GameId comment2 = resource().path("games/" + s.getId()).
123 queryParam("loginID", logJirka).
124 queryParam("player", "Jirka").queryParam("comment", comment).put(GameId.class);
125 assertEquals("Snd comment in the game", 2, comment2.getComments());
127 GameId s2 = resource().path("games/" + s.getId()).
128 queryParam("loginID", logJirka).
129 queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
130 assertNotNull("Successful move", s2);
131 assertEquals("Still two comments in the game", 2, s2.getComments());
133 File game = new File(new File(dir, "games"), s1.getId());
134 assertTrue("File for game exists", game.exists());
135 String content = readFile(game);
137 if (!content.contains("# white: Jarda")) {
140 if (!content.contains("# black: Jirka")) {
143 if (!content.contains("N")) {
146 if (!content.contains("... S")) {
149 if (!content.contains("I like")) {
152 if (!content.contains(comment)) {
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());
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());
171 if (!cmnts.get(0).getComment().contains("I like")) {
174 if (!cmnts.get(1).getComment().contains(comment)) {
180 File tmp = File.createTempFile("test-board", "game");
181 read.storeGame(rg, tmp);
183 String sss1 = readFile(game).replaceAll("@.*:", "date");
186 assertEquals("Newly written file remains the same", sss1, readFile(tmp).replaceAll("@.*:", "date"));
188 String sGame = resource().path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(String.class);
189 Game readGame = resource().path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
190 assertNotNull("Game really returned", readGame);
192 assertEquals(Move.NORTH, readGame.getMoves().get(0).getMove());
193 assertEquals(Move.SOUTH, readGame.getMoves().get(1).getMove());
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")) {
202 if (!cmnts.get(1).getComment().contains(comment)) {
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);