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;
21 import com.sun.jersey.test.framework.WebAppDescriptor;
22 import com.sun.jersey.test.framework.AppDescriptor;
23 import java.util.Properties;
24 import com.sun.jersey.api.client.GenericType;
25 import com.sun.jersey.api.client.UniformInterfaceException;
26 import com.sun.jersey.test.framework.JerseyTest;
28 import java.io.FileOutputStream;
29 import java.io.IOException;
30 import java.util.List;
31 import javax.ws.rs.core.MediaType;
32 import org.junit.Test;
33 import static org.junit.Assert.*;
37 * @author Jaroslav Tulach <jtulach@netbeans.org>
39 public class FinishedGameTest extends JerseyTest {
41 System.setProperty("JERSEY_HTTP_PORT", "33435");
46 protected AppDescriptor configure() {
48 dir = File.createTempFile("quoridor", ".dir");
50 System.setProperty("quoridor.dir", dir.getPath());
52 File passwd = new File(dir, "passwd");
53 FileOutputStream os = new FileOutputStream(passwd);
54 os.write("Jarda=heslo\nJirka=pesko\nMaster=mr\n".getBytes("UTF-8"));
56 } catch (Exception ex) {
57 throw new IllegalStateException(ex);
59 return new WebAppDescriptor.Builder("cz.xelfi.quoridor.webidor.resources").contextPath("finishedGame").build();
63 public void tearDown() throws Exception {
68 static void deleteRec(File dir) throws IOException {
72 File[] arr = dir.listFiles();
80 @Test public void testNotLoggedIn() {
81 String status = resource().path("login").queryParam("id", "not-logged-in").
82 accept(MediaType.TEXT_PLAIN).get(String.class);
83 assertEquals("Nobody is logged in", "", status);
87 @Test public void testCreateAGame() throws Exception {
88 String logJarda = resource().path("login").
89 queryParam("name", "Jarda").
90 queryParam("password", "heslo").
91 accept(MediaType.TEXT_PLAIN).
93 String logJirka = resource().path("login").
94 queryParam("name", "Jirka").
95 queryParam("password", "pesko").
96 accept(MediaType.TEXT_PLAIN).
98 assertNotNull("Logged in ok", logJarda);
99 GameId s = resource().path("games").queryParam("white", "Jarda")
100 .queryParam("loginID", logJarda)
101 .queryParam("black", "Jirka").post(GameId.class);
103 for (int i = 0; i < 3; i++) {
104 resource().path("games/" + s.getId())
105 .queryParam("loginID", logJarda)
106 .queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
107 resource().path("games/" + s.getId())
108 .queryParam("loginID", logJirka)
109 .queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
112 resource().path("games/" + s.getId())
113 .queryParam("loginID", logJarda)
114 .queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
115 resource().path("games/" + s.getId())
116 .queryParam("loginID", logJirka)
117 .queryParam("player", "Jirka").queryParam("move", "SS").put(GameId.class);
120 GenericType<List<GameId>> gType = new GenericType<List<GameId>>() {};
121 List<GameId> nothing = resource().path("games/").queryParam("status", "blackWon").accept(MediaType.TEXT_XML).get(gType);
122 assertTrue("Nothing has been finished yet: " + nothing, nothing.isEmpty());
124 for (int i = 0; i < 3; i++) {
125 resource().path("games/" + s.getId())
126 .queryParam("loginID", logJarda)
127 .queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
128 resource().path("games/" + s.getId())
129 .queryParam("loginID", logJirka)
130 .queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
135 Game end = resource().path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
136 fail("If the game is finished, one cannot get its status without login");
137 } catch (UniformInterfaceException ex) {
140 Game end = resource().path("games/" + s.getId()).queryParam("loginID", logJirka).accept(MediaType.TEXT_XML).get(Game.class);
141 assertEquals("BlackWins", GameStatus.blackWon, end.getId().getStatus());
143 assertEquals("Jirka wins", "Jirka", end.getCurrentPlayer());
145 List<GameId> none = resource().path("games/").queryParam("status", "blackWon").accept(MediaType.TEXT_XML).get(gType);
146 assertEquals("No games, for not logged in users: " + none, 0, none.size());
148 List<GameId> something = resource().path("games/").queryParam("loginID", logJirka).queryParam("status", "blackWon").accept(MediaType.TEXT_XML).get(gType);
149 assertEquals("One game finished: " + something, 1, something.size());
150 assertEquals("Id is OK", end.getId().getId(), something.get(0).getId());
153 @Test public void testResignAGame() throws Exception {
154 String logJarda = resource().path("login").
155 queryParam("name", "Jarda").
156 queryParam("password", "heslo").
157 accept(MediaType.TEXT_PLAIN).
159 GameId s = resource().path("games").queryParam("white", "Jarda")
160 .queryParam("loginID", logJarda)
161 .queryParam("black", "Jirka").post(GameId.class);
163 assertTrue("In progress", s.getStatus().isInProgress());
165 resource().path("games/" + s.getId()).
166 queryParam("loginID", logJarda).
167 queryParam("player", "Jarda").
168 queryParam("move", "RESIGN").put(GameId.class);
170 Game end = resource().path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
171 fail("Should not be able to get game when finished");
172 } catch (UniformInterfaceException ex) {
175 Game end = resource().path("games/" + s.getId()).queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(Game.class);
176 assertEquals("BlackWins", GameStatus.blackWon, end.getId().getStatus());
177 assertEquals("Jirka wins", "Jirka", end.getCurrentPlayer());
179 assertFalse("is finished", end.getId().getStatus().isInProgress());
182 @Test public void testResignBGame() throws Exception {
183 String logJarda = resource().path("login").
184 queryParam("name", "Jarda").
185 queryParam("password", "heslo").
186 accept(MediaType.TEXT_PLAIN).
188 String logJirka = resource().path("login").
189 queryParam("name", "Jirka").
190 queryParam("password", "pesko").
191 accept(MediaType.TEXT_PLAIN).
194 GameId s = resource().path("games").queryParam("white", "Jarda")
195 .queryParam("loginID", logJarda)
196 .queryParam("black", "Jirka").post(GameId.class);
198 assertTrue("In progress", s.getStatus().isInProgress());
200 resource().path("games/" + s.getId()).
201 queryParam("loginID", logJarda).
202 queryParam("player", "Jarda").
203 queryParam("move", "N").put(GameId.class);
204 resource().path("games/" + s.getId()).
205 queryParam("loginID", logJirka).
206 queryParam("player", "Jirka").
207 queryParam("move", "RESIGN").put(GameId.class);
211 Game end = resource().path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
212 fail("Should not be able to get game when finished");
213 } catch (UniformInterfaceException ex) {
216 Game end = resource().path("games/" + s.getId()).queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(Game.class);
217 assertEquals("WhiteWins", GameStatus.whiteWon, end.getId().getStatus());
218 assertEquals("Jarda wins", "Jarda", end.getCurrentPlayer());
220 assertFalse("is finished", end.getId().getStatus().isInProgress());
223 @Test public void testResignForeignGame() throws Exception {
224 String logJarda = resource().path("login").
225 queryParam("name", "Jarda").
226 queryParam("password", "heslo").
227 accept(MediaType.TEXT_PLAIN).
229 GameId s = resource().path("games").queryParam("white", "Jarda")
230 .queryParam("loginID", logJarda)
231 .queryParam("black", "Jirka").post(GameId.class);
232 File usersDir = new File(dir, "users");
234 File Master = new File(usersDir, "Master");
236 Properties p = new Properties();
237 p.setProperty("email", "mas@ter.cz");
238 p.setProperty("permission.resign", "true");
239 p.store(new FileOutputStream(Master), "");
242 assertTrue("In progress", s.getStatus().isInProgress());
243 String logMaster = resource().path("login").
244 queryParam("name", "Master").
245 queryParam("password", "mr").
246 accept(MediaType.TEXT_PLAIN).
249 resource().path("games/" + s.getId()).
250 queryParam("loginID", logMaster).
251 queryParam("player", "Jarda").
252 queryParam("move", "RESIGN").put(GameId.class);
254 Game end = resource().path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
255 fail("Should not be able to get game when finished");
256 } catch (UniformInterfaceException ex) {
259 Game end = resource().path("games/" + s.getId()).queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(Game.class);
260 assertEquals("BlackWins", GameStatus.blackWon, end.getId().getStatus());
261 assertEquals("Jirka wins", "Jirka", end.getCurrentPlayer());
263 assertFalse("is finished", end.getId().getStatus().isInProgress());