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/.
18 package cz.xelfi.quoridor.webidor;
20 import com.sun.jersey.test.framework.AppDescriptor;
21 import cz.xelfi.quoridor.IllegalPositionException;
22 import java.io.StringWriter;
23 import com.sun.jersey.api.client.GenericType;
24 import com.sun.jersey.api.client.UniformInterfaceException;
25 import com.sun.jersey.core.header.MediaTypes;
26 import com.sun.jersey.test.framework.JerseyTest;
27 import com.sun.jersey.test.framework.WebAppDescriptor;
28 import cz.xelfi.quoridor.Board;
29 import cz.xelfi.quoridor.Move;
30 import cz.xelfi.quoridor.webidor.resources.Games;
31 import cz.xelfi.quoridor.webidor.resources.Quoridor;
33 import java.io.FileOutputStream;
34 import java.io.FileReader;
35 import java.io.IOException;
36 import java.io.StringReader;
37 import java.util.List;
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 QuoridorTest extends JerseyTest {
49 System.setProperty("JERSEY_HTTP_PORT", "33434");
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 return new WebAppDescriptor.Builder("cz.xelfi.quoridor.webidor.resources").contextPath("quoTest").build();
73 public void tearDown() throws Exception {
78 static void deleteRec(File dir) throws IOException {
82 File[] arr = dir.listFiles();
91 @Test public void testApplicationWadl() {
92 String serviceWadl = resource().path("application.wadl").
93 accept(MediaTypes.WADL).get(String.class);
94 assertTrue(serviceWadl.length() > 0);
97 @Test public void testCreateAGame() throws Exception {
98 String logJarda = resource().path("login").
99 queryParam("name", "Jarda").
100 queryParam("password", "heslo").
101 accept(MediaType.TEXT_PLAIN).
103 String logJirka = resource().path("login").
104 queryParam("name", "Jirka").
105 queryParam("password", "pesko").
106 accept(MediaType.TEXT_PLAIN).
109 User uJirka = resource().path("users").
110 queryParam("loginID", logJirka).
111 accept(MediaType.TEXT_XML).
114 assertEquals("Jirka", uJirka.getId());
117 GameId s = resource().path("games").queryParam("loginID", logJarda).
118 queryParam("white", "Jarda")
119 .queryParam("black", "Jirka").post(GameId.class);
122 final long now = System.currentTimeMillis();
123 if (s.getModified() >= now) {
124 fail("The game is supposed to be modified in past");
128 String msg = resource().path("games").get(String.class);
129 //List<Game> games = resource().path("games").get(new GenericType<List<Game>>() {});
131 GenericType<List<GameId>> gType = new GenericType<List<GameId>>() {};
133 List<GameId> games = resource().path("games").accept("application/json").get(gType);
134 assertEquals("One game", 1, games.size());
135 assertEquals("Same white", "Jarda", games.get(0).getWhite());
136 assertEquals("Same black", "Jirka", games.get(0).getBlack());
138 GameId s1 = resource().path("games/" + s.getId()).
139 queryParam("loginID", logJarda).
140 queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
142 GameId s2 = resource().path("games/" + s.getId()).
143 queryParam("loginID", logJarda).
144 queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
145 fail("Not Jarda's turn, previous call shall fail");
146 } catch (UniformInterfaceException ex) {
150 GameId s2 = resource().path("games/" + s.getId()).
151 queryParam("loginID", logJirka).
152 queryParam("player", "Jirka").queryParam("move", "NONSENCE").put(GameId.class);
153 fail("Invalid move");
154 } catch (UniformInterfaceException ex) {
157 GameId s2 = resource().path("games/" + s.getId()).
158 queryParam("loginID", logJirka).
159 queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
160 assertNotNull("Successful move", s2);
161 if (s2.getModified() <= now) {
162 fail("The game is newly modified");
164 Game snapshot = resource().path("games/" + s.getId()).queryParam("move", "0").accept(MediaType.TEXT_XML).get(Game.class);
165 String ssnapshot = resource().path("games/" + s.getId()).queryParam("move", "0").accept(MediaType.TEXT_XML).get(String.class);
166 assertEquals("All moves listed:\n" + ssnapshot, 2, snapshot.getMoves().size());
167 assertEquals("Current move", 0, snapshot.getCurrentMove());
168 assertEquals("Position 0", 0, snapshot.getBoard().getPlayers().get(0).getRow());
169 assertEquals("Position 8", 8, snapshot.getBoard().getPlayers().get(1).getRow());
170 assertEquals("Moves numbered from 1", 1, snapshot.getMoves().get(0).getIndex());
171 assertEquals("Moves numbered from 1, 2", 2, snapshot.getMoves().get(1).getIndex());
173 File game = new File(new File(dir, "games"), s2.getId());
174 assertTrue("File for game exists", game.exists());
176 char[] arr = new char[4096];
177 FileReader gameContent = new FileReader(game);
178 int len = gameContent.read(arr);
179 String content = new String(arr, 0, len);
181 if (!content.contains("# white: Jarda")) {
184 if (!content.contains("# black: Jirka")) {
187 if (!content.contains("N S")) {
191 Games read = new Games(new File(dir, "games"), new Quoridor());
192 List<Game> readGames = read.getGames();
193 assertEquals("One game read", 1, readGames.size());
194 Board board = readGames.get(0).getBoard();
195 assertEquals(1, board.getPlayers().get(0).getRow());
196 assertEquals(7, board.getPlayers().get(1).getRow());
197 assertEquals(Move.NORTH, readGames.get(0).getMoves().get(0).getMove());
198 assertEquals(Move.SOUTH, readGames.get(0).getMoves().get(1).getMove());
200 class GMap extends GenericType<Map<String,Object>>{}
201 String text = resource().path("games").path(s.getId()).accept(MediaType.TEXT_PLAIN).get(String.class);
202 text = (boardToPicture(Board.valueOf(text)));
203 if (text.indexOf("-----") == -1) {
204 fail("Expecting board:\n" + text);
206 Game readGame = resource().path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
207 String sGame = resource().path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(String.class);
208 assertNotNull("Game really returned", readGame);
209 // assertEquals("Same game as in text representation", readGame.getBoard(), Board.valueOf(text));
210 assertEquals("Same game as in text representation", readGame.getBoard(), picture2board(text));
211 // assertEquals("It is same as text of our game", readGame.getBoard().toString(), text);
212 assertEquals("It is same as text of our game", boardToPicture(readGame.getBoard()), text);
214 assertEquals(Move.NORTH, readGame.getMoves().get(0).getMove());
215 assertEquals(Move.SOUTH, readGame.getMoves().get(1).getMove());
217 private static String boardToPicture(Board b) {
218 StringWriter w = new StringWriter();
221 } catch (IOException ex) {
222 return ex.toString();
227 private static Board picture2board(String text) throws IOException, IllegalPositionException {
228 StringReader sr = new StringReader(text);
229 return Board.read(sr);