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 java.util.Properties;
30 import com.sun.jersey.api.client.GenericType;
31 import com.sun.jersey.api.client.UniformInterfaceException;
32 import com.sun.jersey.test.framework.JerseyTest;
34 import java.io.FileOutputStream;
35 import java.io.IOException;
36 import java.util.List;
37 import javax.ws.rs.core.MediaType;
38 import org.junit.Test;
39 import static org.junit.Assert.*;
43 * @author Jaroslav Tulach <jtulach@netbeans.org>
45 public class FinishedGameTest extends JerseyTest {
47 System.setProperty("JERSEY_HTTP_PORT", "33435");
51 public FinishedGameTest() throws Exception {
52 super("cz.xelfi.quoridor.webidor.resources");
56 public void setUp() throws Exception {
57 dir = File.createTempFile("quoridor", ".dir");
59 System.setProperty("quoridor.dir", dir.getPath());
61 File passwd = new File(dir, "passwd");
62 FileOutputStream os = new FileOutputStream(passwd);
63 os.write("Jarda=heslo\nJirka=pesko\nMaster=mr\n".getBytes("UTF-8"));
69 public void tearDown() throws Exception {
74 static void deleteRec(File dir) throws IOException {
78 File[] arr = dir.listFiles();
86 @Test public void testNotLoggedIn() {
87 String status = webResource.path("login").queryParam("id", "not-logged-in").
88 accept(MediaType.TEXT_PLAIN).get(String.class);
89 assertEquals("Nobody is logged in", "", status);
93 @Test public void testCreateAGame() throws Exception {
94 String logJarda = webResource.path("login").
95 queryParam("name", "Jarda").
96 queryParam("password", "heslo").
97 accept(MediaType.TEXT_PLAIN).
99 String logJirka = webResource.path("login").
100 queryParam("name", "Jirka").
101 queryParam("password", "pesko").
102 accept(MediaType.TEXT_PLAIN).
104 assertNotNull("Logged in ok", logJarda);
105 GameId s = webResource.path("games").queryParam("white", "Jarda")
106 .queryParam("loginID", logJarda)
107 .queryParam("black", "Jirka").post(GameId.class);
109 for (int i = 0; i < 3; i++) {
110 webResource.path("games/" + s.getId())
111 .queryParam("loginID", logJarda)
112 .queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
113 webResource.path("games/" + s.getId())
114 .queryParam("loginID", logJirka)
115 .queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
118 webResource.path("games/" + s.getId())
119 .queryParam("loginID", logJarda)
120 .queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
121 webResource.path("games/" + s.getId())
122 .queryParam("loginID", logJirka)
123 .queryParam("player", "Jirka").queryParam("move", "SS").put(GameId.class);
126 GenericType<List<GameId>> gType = new GenericType<List<GameId>>() {};
127 List<GameId> nothing = webResource.path("games/").queryParam("status", "blackWon").accept(MediaType.TEXT_XML).get(gType);
128 assertTrue("Nothing has been finished yet: " + nothing, nothing.isEmpty());
130 for (int i = 0; i < 3; i++) {
131 webResource.path("games/" + s.getId())
132 .queryParam("loginID", logJarda)
133 .queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
134 webResource.path("games/" + s.getId())
135 .queryParam("loginID", logJirka)
136 .queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
141 Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
142 fail("If the game is finished, one cannot get its status without login");
143 } catch (UniformInterfaceException ex) {
146 Game end = webResource.path("games/" + s.getId()).queryParam("loginID", logJirka).accept(MediaType.TEXT_XML).get(Game.class);
147 assertEquals("BlackWins", GameStatus.blackWon, end.getId().getStatus());
149 assertEquals("Jirka wins", "Jirka", end.getCurrentPlayer());
151 List<GameId> none = webResource.path("games/").queryParam("status", "blackWon").accept(MediaType.TEXT_XML).get(gType);
152 assertEquals("No games, for not logged in users: " + none, 0, none.size());
154 List<GameId> something = webResource.path("games/").queryParam("loginID", logJirka).queryParam("status", "blackWon").accept(MediaType.TEXT_XML).get(gType);
155 assertEquals("One game finished: " + something, 1, something.size());
156 assertEquals("Id is OK", end.getId().getId(), something.get(0).getId());
159 @Test public void testResignAGame() throws Exception {
160 String logJarda = webResource.path("login").
161 queryParam("name", "Jarda").
162 queryParam("password", "heslo").
163 accept(MediaType.TEXT_PLAIN).
165 GameId s = webResource.path("games").queryParam("white", "Jarda")
166 .queryParam("loginID", logJarda)
167 .queryParam("black", "Jirka").post(GameId.class);
169 assertTrue("In progress", s.getStatus().isInProgress());
171 webResource.path("games/" + s.getId()).
172 queryParam("loginID", logJarda).
173 queryParam("player", "Jarda").
174 queryParam("move", "RESIGN").put(GameId.class);
176 Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
177 fail("Should not be able to get game when finished");
178 } catch (UniformInterfaceException ex) {
181 Game end = webResource.path("games/" + s.getId()).queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(Game.class);
182 assertEquals("BlackWins", GameStatus.blackWon, end.getId().getStatus());
183 assertEquals("Jirka wins", "Jirka", end.getCurrentPlayer());
185 assertFalse("is finished", end.getId().getStatus().isInProgress());
188 @Test public void testResignBGame() throws Exception {
189 String logJarda = webResource.path("login").
190 queryParam("name", "Jarda").
191 queryParam("password", "heslo").
192 accept(MediaType.TEXT_PLAIN).
194 String logJirka = webResource.path("login").
195 queryParam("name", "Jirka").
196 queryParam("password", "pesko").
197 accept(MediaType.TEXT_PLAIN).
200 GameId s = webResource.path("games").queryParam("white", "Jarda")
201 .queryParam("loginID", logJarda)
202 .queryParam("black", "Jirka").post(GameId.class);
204 assertTrue("In progress", s.getStatus().isInProgress());
206 webResource.path("games/" + s.getId()).
207 queryParam("loginID", logJarda).
208 queryParam("player", "Jarda").
209 queryParam("move", "N").put(GameId.class);
210 webResource.path("games/" + s.getId()).
211 queryParam("loginID", logJirka).
212 queryParam("player", "Jirka").
213 queryParam("move", "RESIGN").put(GameId.class);
217 Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
218 fail("Should not be able to get game when finished");
219 } catch (UniformInterfaceException ex) {
222 Game end = webResource.path("games/" + s.getId()).queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(Game.class);
223 assertEquals("WhiteWins", GameStatus.whiteWon, end.getId().getStatus());
224 assertEquals("Jarda wins", "Jarda", end.getCurrentPlayer());
226 assertFalse("is finished", end.getId().getStatus().isInProgress());
229 @Test public void testResignForeignGame() throws Exception {
230 String logJarda = webResource.path("login").
231 queryParam("name", "Jarda").
232 queryParam("password", "heslo").
233 accept(MediaType.TEXT_PLAIN).
235 GameId s = webResource.path("games").queryParam("white", "Jarda")
236 .queryParam("loginID", logJarda)
237 .queryParam("black", "Jirka").post(GameId.class);
238 File usersDir = new File(dir, "users");
240 File Master = new File(usersDir, "Master");
242 Properties p = new Properties();
243 p.setProperty("email", "mas@ter.cz");
244 p.setProperty("permission.resign", "true");
245 p.store(new FileOutputStream(Master), "");
248 assertTrue("In progress", s.getStatus().isInProgress());
249 String logMaster = webResource.path("login").
250 queryParam("name", "Master").
251 queryParam("password", "mr").
252 accept(MediaType.TEXT_PLAIN).
255 webResource.path("games/" + s.getId()).
256 queryParam("loginID", logMaster).
257 queryParam("player", "Jarda").
258 queryParam("move", "RESIGN").put(GameId.class);
260 Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
261 fail("Should not be able to get game when finished");
262 } catch (UniformInterfaceException ex) {
265 Game end = webResource.path("games/" + s.getId()).queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(Game.class);
266 assertEquals("BlackWins", GameStatus.blackWon, end.getId().getStatus());
267 assertEquals("Jirka wins", "Jirka", end.getCurrentPlayer());
269 assertFalse("is finished", end.getId().getStatus().isInProgress());