Initial version of statistics and ELO rating. Donated by Martin Rexa
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
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]"
24 * Portions Copyrighted 2009 Jaroslav Tulach
27 package cz.xelfi.quoridor.webidor;
29 import com.sun.jersey.api.client.GenericType;
30 import com.sun.jersey.api.client.UniformInterfaceException;
31 import com.sun.jersey.core.header.MediaTypes;
32 import com.sun.jersey.test.framework.JerseyTest;
33 import cz.xelfi.quoridor.Board;
34 import cz.xelfi.quoridor.Move;
35 import cz.xelfi.quoridor.webidor.resources.Games;
36 import cz.xelfi.quoridor.webidor.resources.Quoridor;
38 import java.io.FileOutputStream;
39 import java.io.FileReader;
40 import java.io.IOException;
41 import java.util.List;
43 import javax.ws.rs.core.MediaType;
44 import org.junit.Test;
45 import static org.junit.Assert.*;
49 * @author Jaroslav Tulach <jtulach@netbeans.org>
51 public class QuoridorTest extends JerseyTest {
53 System.setProperty("JERSEY_HTTP_PORT", "33434");
57 public QuoridorTest() throws Exception {
58 super("cz.xelfi.quoridor.webidor.resources");
62 public void setUp() throws Exception {
63 dir = File.createTempFile("quoridor", ".dir");
65 System.setProperty("quoridor.dir", dir.getPath());
67 File passwd = new File(dir, "passwd");
68 FileOutputStream os = new FileOutputStream(passwd);
69 os.write("Jarda=heslo\nJirka=pesko\n".getBytes("UTF-8"));
75 public void tearDown() throws Exception {
80 static void deleteRec(File dir) throws IOException {
84 File[] arr = dir.listFiles();
93 @Test public void testApplicationWadl() {
94 String serviceWadl = webResource.path("application.wadl").
95 accept(MediaTypes.WADL).get(String.class);
96 assertTrue(serviceWadl.length() > 0);
99 @Test public void testCreateAGame() throws Exception {
100 String logJarda = webResource.path("login").
101 queryParam("name", "Jarda").
102 queryParam("password", "heslo").
103 accept(MediaType.TEXT_PLAIN).
105 String logJirka = webResource.path("login").
106 queryParam("name", "Jirka").
107 queryParam("password", "pesko").
108 accept(MediaType.TEXT_PLAIN).
111 User uJirka = webResource.path("users").
112 queryParam("loginID", logJirka).
113 accept(MediaType.TEXT_XML).
116 assertEquals("Jirka", uJirka.getId());
119 GameId s = webResource.path("games").queryParam("loginID", logJarda).
120 queryParam("white", "Jarda")
121 .queryParam("black", "Jirka").post(GameId.class);
124 final long now = System.currentTimeMillis();
125 if (s.getModified() >= now) {
126 fail("The game is supposed to be modified in past");
130 String msg = webResource.path("games").get(String.class);
131 //List<Game> games = webResource.path("games").get(new GenericType<List<Game>>() {});
133 GenericType<List<GameId>> gType = new GenericType<List<GameId>>() {};
135 List<GameId> games = webResource.path("games").accept("application/json").get(gType);
136 assertEquals("One game", 1, games.size());
137 assertEquals("Same white", "Jarda", games.get(0).getWhite());
138 assertEquals("Same black", "Jirka", games.get(0).getBlack());
140 GameId s1 = webResource.path("games/" + s.getId()).
141 queryParam("loginID", logJarda).
142 queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
144 GameId s2 = webResource.path("games/" + s.getId()).
145 queryParam("loginID", logJarda).
146 queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
147 fail("Not Jarda's turn, previous call shall fail");
148 } catch (UniformInterfaceException ex) {
152 GameId s2 = webResource.path("games/" + s.getId()).
153 queryParam("loginID", logJirka).
154 queryParam("player", "Jirka").queryParam("move", "NONSENCE").put(GameId.class);
155 fail("Invalid move");
156 } catch (UniformInterfaceException ex) {
159 GameId s2 = webResource.path("games/" + s.getId()).
160 queryParam("loginID", logJirka).
161 queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
162 assertNotNull("Successful move", s2);
163 if (s2.getModified() <= now) {
164 fail("The game is newly modified");
166 Game snapshot = webResource.path("games/" + s.getId()).queryParam("move", "0").accept(MediaType.TEXT_XML).get(Game.class);
167 String ssnapshot = webResource.path("games/" + s.getId()).queryParam("move", "0").accept(MediaType.TEXT_XML).get(String.class);
168 assertEquals("All moves listed:\n" + ssnapshot, 2, snapshot.getMoves().size());
169 assertEquals("Current move", 0, snapshot.getCurrentMove());
170 assertEquals("Position 0", 0, snapshot.getBoard().getPlayers().get(0).getRow());
171 assertEquals("Position 8", 8, snapshot.getBoard().getPlayers().get(1).getRow());
172 assertEquals("Moves numbered from 1", 1, snapshot.getMoves().get(0).getIndex());
173 assertEquals("Moves numbered from 1, 2", 2, snapshot.getMoves().get(1).getIndex());
175 File game = new File(new File(dir, "games"), s2.getId());
176 assertTrue("File for game exists", game.exists());
178 char[] arr = new char[4096];
179 FileReader gameContent = new FileReader(game);
180 int len = gameContent.read(arr);
181 String content = new String(arr, 0, len);
183 if (!content.contains("# white: Jarda")) {
186 if (!content.contains("# black: Jirka")) {
189 if (!content.contains("N S")) {
193 Games read = new Games(new File(dir, "games"), new Quoridor());
194 List<Game> readGames = read.getGames();
195 assertEquals("One game read", 1, readGames.size());
196 Board board = readGames.get(0).getBoard();
197 assertEquals(1, board.getPlayers().get(0).getRow());
198 assertEquals(7, board.getPlayers().get(1).getRow());
199 assertEquals(Move.NORTH, readGames.get(0).getMoves().get(0).getMove());
200 assertEquals(Move.SOUTH, readGames.get(0).getMoves().get(1).getMove());
202 class GMap extends GenericType<Map<String,Object>>{}
203 String text = webResource.path("games").path(s.getId()).accept(MediaType.TEXT_PLAIN).get(String.class);
204 text = (new Board(text)).boardToPicture();
205 if (text.indexOf("-----") == -1) {
206 fail("Expecting board:\n" + text);
208 Game readGame = webResource.path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
209 String sGame = webResource.path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(String.class);
210 assertNotNull("Game really returned", readGame);
211 // assertEquals("Same game as in text representation", readGame.getBoard(), Board.valueOf(text));
212 assertEquals("Same game as in text representation", readGame.getBoard(), Board.picture2board(text));
213 // assertEquals("It is same as text of our game", readGame.getBoard().toString(), text);
214 assertEquals("It is same as text of our game", readGame.getBoard().boardToPicture(), text);
216 assertEquals(Move.NORTH, readGame.getMoves().get(0).getMove());
217 assertEquals(Move.SOUTH, readGame.getMoves().get(1).getMove());