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.test.framework.JerseyTest;
33 import java.io.FileOutputStream;
34 import java.io.IOException;
35 import java.util.List;
36 import javax.ws.rs.core.MediaType;
37 import org.junit.Test;
38 import static org.junit.Assert.*;
42 * @author Jaroslav Tulach <jtulach@netbeans.org>
44 public class FinishedGameTest extends JerseyTest {
46 System.setProperty("JERSEY_HTTP_PORT", "33435");
50 public FinishedGameTest() throws Exception {
51 super("cz.xelfi.quoridor.webidor.resources");
55 public void setUp() throws Exception {
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"));
68 public void tearDown() throws Exception {
73 static void deleteRec(File dir) throws IOException {
77 File[] arr = dir.listFiles();
85 @Test public void testNotLoggedIn() {
86 String status = webResource.path("login").queryParam("id", "not-logged-in").
87 accept(MediaType.TEXT_PLAIN).get(String.class);
88 assertEquals("Nobody is logged in", "", status);
92 @Test public void testCreateAGame() throws Exception {
93 String logJarda = webResource.path("login").
94 queryParam("name", "Jarda").
95 queryParam("password", "heslo").
96 accept(MediaType.TEXT_PLAIN).
98 String logJirka = webResource.path("login").
99 queryParam("name", "Jirka").
100 queryParam("password", "pesko").
101 accept(MediaType.TEXT_PLAIN).
103 assertNotNull("Logged in ok", logJarda);
104 GameId s = webResource.path("games").queryParam("white", "Jarda")
105 .queryParam("loginID", logJarda)
106 .queryParam("black", "Jirka").post(GameId.class);
108 for (int i = 0; i < 3; i++) {
109 webResource.path("games/" + s.getId())
110 .queryParam("loginID", logJarda)
111 .queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
112 webResource.path("games/" + s.getId())
113 .queryParam("loginID", logJirka)
114 .queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
117 webResource.path("games/" + s.getId())
118 .queryParam("loginID", logJarda)
119 .queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
120 webResource.path("games/" + s.getId())
121 .queryParam("loginID", logJirka)
122 .queryParam("player", "Jirka").queryParam("move", "SS").put(GameId.class);
125 GenericType<List<GameId>> gType = new GenericType<List<GameId>>() {};
126 List<GameId> nothing = webResource.path("games/").queryParam("status", "blackWon").accept(MediaType.TEXT_XML).get(gType);
127 assertTrue("Nothing has been finished yet: " + nothing, nothing.isEmpty());
129 for (int i = 0; i < 3; i++) {
130 webResource.path("games/" + s.getId())
131 .queryParam("loginID", logJarda)
132 .queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
133 webResource.path("games/" + s.getId())
134 .queryParam("loginID", logJirka)
135 .queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
140 Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
141 fail("If the game is finished, one cannot get its status without login");
142 } catch (UniformInterfaceException ex) {
145 Game end = webResource.path("games/" + s.getId()).queryParam("loginID", logJirka).accept(MediaType.TEXT_XML).get(Game.class);
146 assertEquals("BlackWins", GameStatus.blackWon, end.getId().getStatus());
148 assertEquals("Jirka wins", "Jirka", end.getCurrentPlayer());
150 List<GameId> none = webResource.path("games/").queryParam("status", "blackWon").accept(MediaType.TEXT_XML).get(gType);
151 assertEquals("No games, for not logged in users: " + none, 0, none.size());
153 List<GameId> something = webResource.path("games/").queryParam("loginID", logJirka).queryParam("status", "blackWon").accept(MediaType.TEXT_XML).get(gType);
154 assertEquals("One game finished: " + something, 1, something.size());
155 assertEquals("Id is OK", end.getId().getId(), something.get(0).getId());
158 @Test public void testResignAGame() throws Exception {
159 String logJarda = webResource.path("login").
160 queryParam("name", "Jarda").
161 queryParam("password", "heslo").
162 accept(MediaType.TEXT_PLAIN).
164 GameId s = webResource.path("games").queryParam("white", "Jarda")
165 .queryParam("loginID", logJarda)
166 .queryParam("black", "Jirka").post(GameId.class);
168 assertTrue("In progress", s.getStatus().isInProgress());
170 webResource.path("games/" + s.getId()).
171 queryParam("loginID", logJarda).
172 queryParam("player", "Jarda").
173 queryParam("move", "RESIGN").put(GameId.class);
175 Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
176 fail("Should not be able to get game when finished");
177 } catch (UniformInterfaceException ex) {
180 Game end = webResource.path("games/" + s.getId()).queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(Game.class);
181 assertEquals("BlackWins", GameStatus.blackWon, end.getId().getStatus());
182 assertEquals("Jirka wins", "Jirka", end.getCurrentPlayer());
184 assertFalse("is finished", end.getId().getStatus().isInProgress());
187 @Test public void testResignBGame() throws Exception {
188 String logJarda = webResource.path("login").
189 queryParam("name", "Jarda").
190 queryParam("password", "heslo").
191 accept(MediaType.TEXT_PLAIN).
193 String logJirka = webResource.path("login").
194 queryParam("name", "Jirka").
195 queryParam("password", "pesko").
196 accept(MediaType.TEXT_PLAIN).
199 GameId s = webResource.path("games").queryParam("white", "Jarda")
200 .queryParam("loginID", logJarda)
201 .queryParam("black", "Jirka").post(GameId.class);
203 assertTrue("In progress", s.getStatus().isInProgress());
205 webResource.path("games/" + s.getId()).
206 queryParam("loginID", logJarda).
207 queryParam("player", "Jarda").
208 queryParam("move", "N").put(GameId.class);
209 webResource.path("games/" + s.getId()).
210 queryParam("loginID", logJirka).
211 queryParam("player", "Jirka").
212 queryParam("move", "RESIGN").put(GameId.class);
216 Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
217 fail("Should not be able to get game when finished");
218 } catch (UniformInterfaceException ex) {
221 Game end = webResource.path("games/" + s.getId()).queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(Game.class);
222 assertEquals("WhiteWins", GameStatus.whiteWon, end.getId().getStatus());
223 assertEquals("Jarda wins", "Jarda", end.getCurrentPlayer());
225 assertFalse("is finished", end.getId().getStatus().isInProgress());