jaroslav@57
|
1 |
/*
|
jaroslav@57
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
jaroslav@57
|
3 |
*
|
jaroslav@57
|
4 |
* The contents of this file are subject to the terms of either the GNU
|
jaroslav@57
|
5 |
* General Public License Version 2 only ("GPL") or the Common
|
jaroslav@57
|
6 |
* Development and Distribution License("CDDL") (collectively, the
|
jaroslav@57
|
7 |
* "License"). You may not use this file except in compliance with the
|
jaroslav@57
|
8 |
* License. You can obtain a copy of the License at
|
jaroslav@57
|
9 |
* http://www.netbeans.org/cddl-gplv2.html
|
jaroslav@57
|
10 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
|
jaroslav@57
|
11 |
* specific language governing permissions and limitations under the
|
jaroslav@57
|
12 |
* License. When distributing the software, include this License Header
|
jaroslav@57
|
13 |
* Notice in each file and include the License file at
|
jaroslav@57
|
14 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
|
jaroslav@57
|
15 |
* particular file as subject to the "Classpath" exception as provided
|
jaroslav@57
|
16 |
* by Sun in the GPL Version 2 section of the License file that
|
jaroslav@57
|
17 |
* accompanied this code. If applicable, add the following below the
|
jaroslav@57
|
18 |
* License Header, with the fields enclosed by brackets [] replaced by
|
jaroslav@57
|
19 |
* your own identifying information:
|
jaroslav@57
|
20 |
* "Portions Copyrighted [year] [name of copyright owner]"
|
jaroslav@57
|
21 |
*
|
jaroslav@57
|
22 |
* Contributor(s):
|
jaroslav@57
|
23 |
*
|
jaroslav@57
|
24 |
* Portions Copyrighted 2009 Jaroslav Tulach
|
jaroslav@57
|
25 |
*/
|
jaroslav@57
|
26 |
|
jaroslav@57
|
27 |
package cz.xelfi.quoridor.webidor;
|
jaroslav@57
|
28 |
|
jaroslav@128
|
29 |
import com.sun.jersey.api.client.GenericType;
|
jaroslav@162
|
30 |
import com.sun.jersey.api.client.UniformInterfaceException;
|
jaroslav@57
|
31 |
import com.sun.jersey.test.framework.JerseyTest;
|
jaroslav@57
|
32 |
import java.io.File;
|
jtulach@82
|
33 |
import java.io.FileOutputStream;
|
jaroslav@57
|
34 |
import java.io.IOException;
|
jaroslav@128
|
35 |
import java.util.List;
|
jaroslav@57
|
36 |
import javax.ws.rs.core.MediaType;
|
jaroslav@57
|
37 |
import org.junit.Test;
|
jaroslav@57
|
38 |
import static org.junit.Assert.*;
|
jaroslav@57
|
39 |
|
jaroslav@57
|
40 |
/**
|
jaroslav@57
|
41 |
*
|
jaroslav@57
|
42 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@57
|
43 |
*/
|
jaroslav@57
|
44 |
public class FinishedGameTest extends JerseyTest {
|
jaroslav@149
|
45 |
static {
|
jaroslav@149
|
46 |
System.setProperty("JERSEY_HTTP_PORT", "33435");
|
jaroslav@149
|
47 |
}
|
jaroslav@57
|
48 |
private File dir;
|
jaroslav@57
|
49 |
|
jaroslav@57
|
50 |
public FinishedGameTest() throws Exception {
|
jaroslav@57
|
51 |
super("cz.xelfi.quoridor.webidor.resources");
|
jaroslav@57
|
52 |
}
|
jaroslav@57
|
53 |
|
jaroslav@57
|
54 |
@Override
|
jaroslav@57
|
55 |
public void setUp() throws Exception {
|
jaroslav@57
|
56 |
dir = File.createTempFile("quoridor", ".dir");
|
jaroslav@57
|
57 |
dir.delete();
|
jaroslav@57
|
58 |
System.setProperty("quoridor.dir", dir.getPath());
|
jtulach@82
|
59 |
dir.mkdirs();
|
jtulach@82
|
60 |
File passwd = new File(dir, "passwd");
|
jtulach@82
|
61 |
FileOutputStream os = new FileOutputStream(passwd);
|
jtulach@82
|
62 |
os.write("Jarda=heslo\nJirka=pesko\n".getBytes("UTF-8"));
|
jtulach@82
|
63 |
os.close();
|
jaroslav@57
|
64 |
super.setUp();
|
jaroslav@57
|
65 |
}
|
jaroslav@57
|
66 |
|
jaroslav@57
|
67 |
@Override
|
jaroslav@57
|
68 |
public void tearDown() throws Exception {
|
jaroslav@57
|
69 |
super.tearDown();
|
jaroslav@57
|
70 |
deleteRec(dir);
|
jaroslav@57
|
71 |
}
|
jaroslav@57
|
72 |
|
jaroslav@57
|
73 |
static void deleteRec(File dir) throws IOException {
|
jaroslav@57
|
74 |
if (dir == null) {
|
jaroslav@57
|
75 |
return;
|
jaroslav@57
|
76 |
}
|
jaroslav@57
|
77 |
File[] arr = dir.listFiles();
|
jaroslav@57
|
78 |
if (arr != null) {
|
jaroslav@57
|
79 |
for (File f : arr) {
|
jaroslav@57
|
80 |
deleteRec(f);
|
jaroslav@57
|
81 |
}
|
jaroslav@57
|
82 |
}
|
jaroslav@57
|
83 |
dir.delete();
|
jaroslav@57
|
84 |
}
|
jtulach@83
|
85 |
@Test public void testNotLoggedIn() {
|
jtulach@83
|
86 |
String status = webResource.path("login").queryParam("id", "not-logged-in").
|
jtulach@83
|
87 |
accept(MediaType.TEXT_PLAIN).get(String.class);
|
jtulach@83
|
88 |
assertEquals("Nobody is logged in", "", status);
|
jtulach@83
|
89 |
}
|
jtulach@83
|
90 |
|
jaroslav@57
|
91 |
|
jaroslav@57
|
92 |
@Test public void testCreateAGame() throws Exception {
|
jtulach@82
|
93 |
String logJarda = webResource.path("login").
|
jtulach@82
|
94 |
queryParam("name", "Jarda").
|
jtulach@82
|
95 |
queryParam("password", "heslo").
|
jtulach@82
|
96 |
accept(MediaType.TEXT_PLAIN).
|
jtulach@82
|
97 |
put(String.class);
|
jtulach@82
|
98 |
String logJirka = webResource.path("login").
|
jtulach@82
|
99 |
queryParam("name", "Jirka").
|
jtulach@82
|
100 |
queryParam("password", "pesko").
|
jtulach@82
|
101 |
accept(MediaType.TEXT_PLAIN).
|
jtulach@82
|
102 |
put(String.class);
|
jtulach@82
|
103 |
assertNotNull("Logged in ok", logJarda);
|
jaroslav@57
|
104 |
GameId s = webResource.path("games").queryParam("white", "Jarda")
|
jtulach@82
|
105 |
.queryParam("loginID", logJarda)
|
jaroslav@57
|
106 |
.queryParam("black", "Jirka").post(GameId.class);
|
jaroslav@57
|
107 |
|
jaroslav@57
|
108 |
for (int i = 0; i < 3; i++) {
|
jtulach@82
|
109 |
webResource.path("games/" + s.getId())
|
jtulach@82
|
110 |
.queryParam("loginID", logJarda)
|
jtulach@82
|
111 |
.queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
|
jtulach@82
|
112 |
webResource.path("games/" + s.getId())
|
jtulach@82
|
113 |
.queryParam("loginID", logJirka)
|
jtulach@82
|
114 |
.queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
|
jaroslav@57
|
115 |
}
|
jaroslav@57
|
116 |
|
jtulach@82
|
117 |
webResource.path("games/" + s.getId())
|
jtulach@82
|
118 |
.queryParam("loginID", logJarda)
|
jtulach@82
|
119 |
.queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
|
jtulach@82
|
120 |
webResource.path("games/" + s.getId())
|
jtulach@82
|
121 |
.queryParam("loginID", logJirka)
|
jtulach@82
|
122 |
.queryParam("player", "Jirka").queryParam("move", "SS").put(GameId.class);
|
jaroslav@57
|
123 |
|
jaroslav@128
|
124 |
|
jaroslav@128
|
125 |
GenericType<List<GameId>> gType = new GenericType<List<GameId>>() {};
|
jaroslav@128
|
126 |
List<GameId> nothing = webResource.path("games/").queryParam("status", "blackWon").accept(MediaType.TEXT_XML).get(gType);
|
jaroslav@128
|
127 |
assertTrue("Nothing has been finished yet: " + nothing, nothing.isEmpty());
|
jaroslav@128
|
128 |
|
jaroslav@57
|
129 |
for (int i = 0; i < 3; i++) {
|
jtulach@82
|
130 |
webResource.path("games/" + s.getId())
|
jtulach@82
|
131 |
.queryParam("loginID", logJarda)
|
jtulach@82
|
132 |
.queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
|
jtulach@82
|
133 |
webResource.path("games/" + s.getId())
|
jtulach@82
|
134 |
.queryParam("loginID", logJirka)
|
jtulach@82
|
135 |
.queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
|
jaroslav@57
|
136 |
}
|
jaroslav@57
|
137 |
|
jaroslav@128
|
138 |
|
jaroslav@162
|
139 |
try {
|
jaroslav@162
|
140 |
Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
|
jaroslav@162
|
141 |
fail("If the game is finished, one cannot get its status without login");
|
jaroslav@162
|
142 |
} catch (UniformInterfaceException ex) {
|
jaroslav@162
|
143 |
// OK
|
jaroslav@162
|
144 |
}
|
jaroslav@162
|
145 |
Game end = webResource.path("games/" + s.getId()).queryParam("loginID", logJirka).accept(MediaType.TEXT_XML).get(Game.class);
|
jtulach@77
|
146 |
assertEquals("BlackWins", GameStatus.blackWon, end.getId().getStatus());
|
jaroslav@128
|
147 |
|
jaroslav@140
|
148 |
assertEquals("Jirka wins", "Jirka", end.getCurrentPlayer());
|
jaroslav@140
|
149 |
|
jtulach@166
|
150 |
List<GameId> none = webResource.path("games/").queryParam("status", "blackWon").accept(MediaType.TEXT_XML).get(gType);
|
jtulach@166
|
151 |
assertEquals("No games, for not logged in users: " + none, 0, none.size());
|
jaroslav@128
|
152 |
|
jtulach@166
|
153 |
List<GameId> something = webResource.path("games/").queryParam("loginID", logJirka).queryParam("status", "blackWon").accept(MediaType.TEXT_XML).get(gType);
|
jaroslav@128
|
154 |
assertEquals("One game finished: " + something, 1, something.size());
|
jaroslav@128
|
155 |
assertEquals("Id is OK", end.getId().getId(), something.get(0).getId());
|
jaroslav@57
|
156 |
}
|
jaroslav@57
|
157 |
|
jtulach@75
|
158 |
@Test public void testResignAGame() throws Exception {
|
jtulach@82
|
159 |
String logJarda = webResource.path("login").
|
jtulach@82
|
160 |
queryParam("name", "Jarda").
|
jtulach@82
|
161 |
queryParam("password", "heslo").
|
jtulach@82
|
162 |
accept(MediaType.TEXT_PLAIN).
|
jtulach@82
|
163 |
put(String.class);
|
jtulach@75
|
164 |
GameId s = webResource.path("games").queryParam("white", "Jarda")
|
jtulach@82
|
165 |
.queryParam("loginID", logJarda)
|
jtulach@75
|
166 |
.queryParam("black", "Jirka").post(GameId.class);
|
jtulach@75
|
167 |
|
jaroslav@162
|
168 |
assertTrue("In progress", s.getStatus().isInProgress());
|
jaroslav@162
|
169 |
|
jtulach@82
|
170 |
webResource.path("games/" + s.getId()).
|
jtulach@82
|
171 |
queryParam("loginID", logJarda).
|
jtulach@82
|
172 |
queryParam("player", "Jarda").
|
jtulach@82
|
173 |
queryParam("move", "RESIGN").put(GameId.class);
|
jaroslav@162
|
174 |
try {
|
jaroslav@162
|
175 |
Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
|
jaroslav@162
|
176 |
fail("Should not be able to get game when finished");
|
jaroslav@162
|
177 |
} catch (UniformInterfaceException ex) {
|
jaroslav@162
|
178 |
// OK
|
jaroslav@162
|
179 |
}
|
jaroslav@162
|
180 |
Game end = webResource.path("games/" + s.getId()).queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(Game.class);
|
jtulach@77
|
181 |
assertEquals("BlackWins", GameStatus.blackWon, end.getId().getStatus());
|
jaroslav@140
|
182 |
assertEquals("Jirka wins", "Jirka", end.getCurrentPlayer());
|
jaroslav@162
|
183 |
|
jaroslav@162
|
184 |
assertFalse("is finished", end.getId().getStatus().isInProgress());
|
jtulach@75
|
185 |
}
|
jtulach@75
|
186 |
|
jaroslav@57
|
187 |
}
|