Have to disable compile on save for tests, otherwise they don't execute under debugger
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 com.sun.jersey.test.framework.WebAppDescriptor.Builder;
25 import cz.xelfi.quoridor.Board;
26 import cz.xelfi.quoridor.Move;
27 import cz.xelfi.quoridor.webidor.Game;
28 import cz.xelfi.quoridor.webidor.GameId;
29 import cz.xelfi.quoridor.webidor.Note;
31 import java.io.FileInputStream;
32 import java.io.FileNotFoundException;
33 import java.io.FileOutputStream;
34 import java.io.IOException;
35 import java.io.InputStreamReader;
36 import java.io.Reader;
37 import java.util.List;
38 import java.util.ResourceBundle;
39 import javax.ws.rs.core.MediaType;
40 import org.junit.Test;
41 import static org.junit.Assert.*;
45 * @author Jaroslav Tulach <jtulach@netbeans.org>
47 public class ChatTest extends JerseyTest {
49 System.setProperty("JERSEY_HTTP_PORT", "33437");
54 protected AppDescriptor configure() {
56 dir = File.createTempFile("quoridor", ".dir");
58 System.setProperty("quoridor.dir", dir.getPath());
60 File passwd = new File(dir, "passwd");
61 FileOutputStream os = new FileOutputStream(passwd);
62 os.write("Jarda=heslo\nJirka=pesko\n".getBytes("UTF-8"));
64 } catch (Exception ex) {
65 throw new IllegalStateException(ex);
67 final Builder builder = new WebAppDescriptor.Builder("cz.xelfi.quoridor.webidor.resources");
68 return builder.contextPath("context").build();
72 public void tearDown() throws Exception {
77 static void deleteRec(File dir) throws IOException {
81 File[] arr = dir.listFiles();
90 @Test public void testCreateAGame() throws Exception {
91 String logJarda = resource().path("login").
92 queryParam("name", "Jarda").
93 queryParam("password", "heslo").
94 accept(MediaType.TEXT_PLAIN).
96 String logJirka = resource().path("login").
97 queryParam("name", "Jirka").
98 queryParam("password", "pesko").
99 accept(MediaType.TEXT_PLAIN).
101 GameId s = resource().path("games").queryParam("loginID", logJarda).
102 queryParam("white", "Jarda")
103 .queryParam("black", "Jirka").post(GameId.class);
106 final long now = System.currentTimeMillis();
107 if (s.getModified() >= now) {
108 fail("The game is supposed to be modified in past");
112 ResourceBundle b = ResourceBundle.getBundle("cz/xelfi/quoridor/webidor/TestBundle");
113 String comment = b.getString("COMMENT");
115 GameId s1 = resource().path("games/" + s.getId()).
116 queryParam("loginID", logJarda).
117 queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
119 GameId comment1 = resource().path("games/" + s.getId()).
120 queryParam("loginID", logJarda).
121 queryParam("player", "Jarda").queryParam("comment", "I like this game!").put(GameId.class);
122 assertEquals("One comment in the game", 1, comment1.getComments());
124 GameId comment2 = resource().path("games/" + s.getId()).
125 queryParam("loginID", logJirka).
126 queryParam("player", "Jirka").queryParam("comment", comment).put(GameId.class);
127 assertEquals("Snd comment in the game", 2, comment2.getComments());
129 GameId s2 = resource().path("games/" + s.getId()).
130 queryParam("loginID", logJirka).
131 queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
132 assertNotNull("Successful move", s2);
133 assertEquals("Still two comments in the game", 2, s2.getComments());
135 File game = new File(new File(dir, "games"), s1.getId());
136 assertTrue("File for game exists", game.exists());
137 String content = readFile(game);
139 if (!content.contains("# white: Jarda")) {
142 if (!content.contains("# black: Jirka")) {
145 if (!content.contains("N")) {
148 if (!content.contains("... S")) {
151 if (!content.contains("I like")) {
154 if (!content.contains(comment)) {
158 Games read = new Games(new File(dir, "games"), new Quoridor());
159 List<Game> readGames = read.getGames();
160 assertEquals("One game read", 1, readGames.size());
161 Board board = readGames.get(0).getBoard();
162 assertEquals(1, board.getPlayers().get(0).getRow());
163 assertEquals(7, board.getPlayers().get(1).getRow());
164 assertEquals(Move.NORTH, readGames.get(0).getMoves().get(0).getMove());
165 assertEquals(Move.SOUTH, readGames.get(0).getMoves().get(1).getMove());
167 Game rg = readGames.get(0);
168 assertNull("No comments on second move", rg.getMoves().get(1).getComments());
169 List<Note> cmnts = rg.getMoves().get(0).getComments();
170 assertNotNull("Some comments on first move", cmnts);
171 assertEquals("Two comments: " + cmnts, 2, cmnts.size());
173 if (!cmnts.get(0).getComment().contains("I like")) {
176 if (!cmnts.get(1).getComment().contains(comment)) {
182 File tmp = File.createTempFile("test-board", "game");
183 read.storeGame(rg, tmp);
185 String sss1 = readFile(game).replaceAll("@.*:", "date");
188 assertEquals("Newly written file remains the same", sss1, readFile(tmp).replaceAll("@.*:", "date"));
190 String sGame = resource().path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(String.class);
191 Game readGame = resource().path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
192 assertNotNull("Game really returned", readGame);
194 assertEquals(Move.NORTH, readGame.getMoves().get(0).getMove());
195 assertEquals(Move.SOUTH, readGame.getMoves().get(1).getMove());
197 assertNull("No comments on second move", readGame.getMoves().get(1).getComments());
198 cmnts = readGame.getMoves().get(0).getComments();
199 assertNotNull("Some comments on first move", cmnts);
200 assertEquals("Two comments: " + cmnts, 2, cmnts.size());
201 if (!cmnts.get(0).getComment().contains("I like")) {
204 if (!cmnts.get(1).getComment().contains(comment)) {
209 private String readFile(File game) throws IOException, FileNotFoundException {
210 char[] arr = new char[4096];
211 Reader gameContent = new InputStreamReader(new FileInputStream(game), "UTF-8");
212 int len = gameContent.read(arr);
213 String content = new String(arr, 0, len);