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.test.framework.WebAppDescriptor;
30 import com.sun.jersey.test.framework.AppDescriptor;
31 import java.util.Properties;
32 import com.sun.jersey.api.client.GenericType;
33 import com.sun.jersey.api.client.UniformInterfaceException;
34 import com.sun.jersey.test.framework.JerseyTest;
36 import java.io.FileOutputStream;
37 import java.io.IOException;
38 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 FinishedGameTest extends JerseyTest {
49 System.setProperty("JERSEY_HTTP_PORT", "33435");
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\nMaster=mr\n".getBytes("UTF-8"));
64 } catch (Exception ex) {
65 throw new IllegalStateException(ex);
67 return new WebAppDescriptor.Builder("cz.xelfi.quoridor.webidor.resources").build();
71 public void tearDown() throws Exception {
76 static void deleteRec(File dir) throws IOException {
80 File[] arr = dir.listFiles();
88 @Test public void testNotLoggedIn() {
89 String status = resource().path("login").queryParam("id", "not-logged-in").
90 accept(MediaType.TEXT_PLAIN).get(String.class);
91 assertEquals("Nobody is logged in", "", status);
95 @Test public void testCreateAGame() throws Exception {
96 String logJarda = resource().path("login").
97 queryParam("name", "Jarda").
98 queryParam("password", "heslo").
99 accept(MediaType.TEXT_PLAIN).
101 String logJirka = resource().path("login").
102 queryParam("name", "Jirka").
103 queryParam("password", "pesko").
104 accept(MediaType.TEXT_PLAIN).
106 assertNotNull("Logged in ok", logJarda);
107 GameId s = resource().path("games").queryParam("white", "Jarda")
108 .queryParam("loginID", logJarda)
109 .queryParam("black", "Jirka").post(GameId.class);
111 for (int i = 0; i < 3; i++) {
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", "S").put(GameId.class);
120 resource().path("games/" + s.getId())
121 .queryParam("loginID", logJarda)
122 .queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
123 resource().path("games/" + s.getId())
124 .queryParam("loginID", logJirka)
125 .queryParam("player", "Jirka").queryParam("move", "SS").put(GameId.class);
128 GenericType<List<GameId>> gType = new GenericType<List<GameId>>() {};
129 List<GameId> nothing = resource().path("games/").queryParam("status", "blackWon").accept(MediaType.TEXT_XML).get(gType);
130 assertTrue("Nothing has been finished yet: " + nothing, nothing.isEmpty());
132 for (int i = 0; i < 3; i++) {
133 resource().path("games/" + s.getId())
134 .queryParam("loginID", logJarda)
135 .queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
136 resource().path("games/" + s.getId())
137 .queryParam("loginID", logJirka)
138 .queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
143 Game end = resource().path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
144 fail("If the game is finished, one cannot get its status without login");
145 } catch (UniformInterfaceException ex) {
148 Game end = resource().path("games/" + s.getId()).queryParam("loginID", logJirka).accept(MediaType.TEXT_XML).get(Game.class);
149 assertEquals("BlackWins", GameStatus.blackWon, end.getId().getStatus());
151 assertEquals("Jirka wins", "Jirka", end.getCurrentPlayer());
153 List<GameId> none = resource().path("games/").queryParam("status", "blackWon").accept(MediaType.TEXT_XML).get(gType);
154 assertEquals("No games, for not logged in users: " + none, 0, none.size());
156 List<GameId> something = resource().path("games/").queryParam("loginID", logJirka).queryParam("status", "blackWon").accept(MediaType.TEXT_XML).get(gType);
157 assertEquals("One game finished: " + something, 1, something.size());
158 assertEquals("Id is OK", end.getId().getId(), something.get(0).getId());
161 @Test public void testResignAGame() throws Exception {
162 String logJarda = resource().path("login").
163 queryParam("name", "Jarda").
164 queryParam("password", "heslo").
165 accept(MediaType.TEXT_PLAIN).
167 GameId s = resource().path("games").queryParam("white", "Jarda")
168 .queryParam("loginID", logJarda)
169 .queryParam("black", "Jirka").post(GameId.class);
171 assertTrue("In progress", s.getStatus().isInProgress());
173 resource().path("games/" + s.getId()).
174 queryParam("loginID", logJarda).
175 queryParam("player", "Jarda").
176 queryParam("move", "RESIGN").put(GameId.class);
178 Game end = resource().path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
179 fail("Should not be able to get game when finished");
180 } catch (UniformInterfaceException ex) {
183 Game end = resource().path("games/" + s.getId()).queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(Game.class);
184 assertEquals("BlackWins", GameStatus.blackWon, end.getId().getStatus());
185 assertEquals("Jirka wins", "Jirka", end.getCurrentPlayer());
187 assertFalse("is finished", end.getId().getStatus().isInProgress());
190 @Test public void testResignBGame() throws Exception {
191 String logJarda = resource().path("login").
192 queryParam("name", "Jarda").
193 queryParam("password", "heslo").
194 accept(MediaType.TEXT_PLAIN).
196 String logJirka = resource().path("login").
197 queryParam("name", "Jirka").
198 queryParam("password", "pesko").
199 accept(MediaType.TEXT_PLAIN).
202 GameId s = resource().path("games").queryParam("white", "Jarda")
203 .queryParam("loginID", logJarda)
204 .queryParam("black", "Jirka").post(GameId.class);
206 assertTrue("In progress", s.getStatus().isInProgress());
208 resource().path("games/" + s.getId()).
209 queryParam("loginID", logJarda).
210 queryParam("player", "Jarda").
211 queryParam("move", "N").put(GameId.class);
212 resource().path("games/" + s.getId()).
213 queryParam("loginID", logJirka).
214 queryParam("player", "Jirka").
215 queryParam("move", "RESIGN").put(GameId.class);
219 Game end = resource().path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
220 fail("Should not be able to get game when finished");
221 } catch (UniformInterfaceException ex) {
224 Game end = resource().path("games/" + s.getId()).queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(Game.class);
225 assertEquals("WhiteWins", GameStatus.whiteWon, end.getId().getStatus());
226 assertEquals("Jarda wins", "Jarda", end.getCurrentPlayer());
228 assertFalse("is finished", end.getId().getStatus().isInProgress());
231 @Test public void testResignForeignGame() throws Exception {
232 String logJarda = resource().path("login").
233 queryParam("name", "Jarda").
234 queryParam("password", "heslo").
235 accept(MediaType.TEXT_PLAIN).
237 GameId s = resource().path("games").queryParam("white", "Jarda")
238 .queryParam("loginID", logJarda)
239 .queryParam("black", "Jirka").post(GameId.class);
240 File usersDir = new File(dir, "users");
242 File Master = new File(usersDir, "Master");
244 Properties p = new Properties();
245 p.setProperty("email", "mas@ter.cz");
246 p.setProperty("permission.resign", "true");
247 p.store(new FileOutputStream(Master), "");
250 assertTrue("In progress", s.getStatus().isInProgress());
251 String logMaster = resource().path("login").
252 queryParam("name", "Master").
253 queryParam("password", "mr").
254 accept(MediaType.TEXT_PLAIN).
257 resource().path("games/" + s.getId()).
258 queryParam("loginID", logMaster).
259 queryParam("player", "Jarda").
260 queryParam("move", "RESIGN").put(GameId.class);
262 Game end = resource().path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
263 fail("Should not be able to get game when finished");
264 } catch (UniformInterfaceException ex) {
267 Game end = resource().path("games/" + s.getId()).queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(Game.class);
268 assertEquals("BlackWins", GameStatus.blackWon, end.getId().getStatus());
269 assertEquals("Jirka wins", "Jirka", end.getCurrentPlayer());
271 assertFalse("is finished", end.getId().getStatus().isInProgress());