jaroslav@50
|
1 |
/*
|
jaroslav@264
|
2 |
* Quoridor server and related libraries
|
jaroslav@264
|
3 |
* Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@50
|
4 |
*
|
jaroslav@264
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@264
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@264
|
7 |
* the Free Software Foundation, either version 3 of the License.
|
jaroslav@50
|
8 |
*
|
jaroslav@264
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@264
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@264
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@264
|
12 |
* GNU General Public License for more details.
|
jaroslav@50
|
13 |
*
|
jaroslav@264
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@264
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@264
|
16 |
* If not, see http://www.gnu.org/licenses/.
|
jaroslav@50
|
17 |
*/
|
jaroslav@50
|
18 |
package cz.xelfi.quoridor.freemarkerdor;
|
jaroslav@50
|
19 |
|
jaroslav@50
|
20 |
import com.sun.jersey.api.client.Client;
|
jaroslav@73
|
21 |
import com.sun.jersey.api.client.ClientResponse;
|
jaroslav@50
|
22 |
import com.sun.jersey.api.client.WebResource;
|
jaroslav@50
|
23 |
import com.sun.jersey.core.header.MediaTypes;
|
jaroslav@73
|
24 |
import com.sun.jersey.core.util.MultivaluedMapImpl;
|
jaroslav@126
|
25 |
import com.sun.net.httpserver.HttpServer;
|
jaroslav@126
|
26 |
import cz.xelfi.quoridor.webidor.resources.Quoridor;
|
martin@180
|
27 |
import cz.xelfi.quoridor.statistics.resources.Statistics;
|
jaroslav@106
|
28 |
import java.awt.Image;
|
jaroslav@50
|
29 |
import java.io.File;
|
jaroslav@73
|
30 |
import java.io.FileOutputStream;
|
jaroslav@50
|
31 |
import java.io.IOException;
|
jaroslav@106
|
32 |
import java.io.InputStream;
|
jaroslav@50
|
33 |
import java.net.URI;
|
jaroslav@62
|
34 |
import java.util.Locale;
|
jaroslav@50
|
35 |
import java.util.concurrent.Callable;
|
jaroslav@106
|
36 |
import java.util.regex.Matcher;
|
jaroslav@106
|
37 |
import java.util.regex.Pattern;
|
jaroslav@51
|
38 |
import javax.ws.rs.core.Cookie;
|
jtulach@82
|
39 |
import javax.ws.rs.core.MediaType;
|
jaroslav@73
|
40 |
import javax.ws.rs.core.MultivaluedMap;
|
jaroslav@73
|
41 |
import org.junit.AfterClass;
|
jaroslav@50
|
42 |
import org.junit.Before;
|
jaroslav@62
|
43 |
import org.junit.BeforeClass;
|
jaroslav@50
|
44 |
import org.junit.Test;
|
jaroslav@50
|
45 |
import static org.junit.Assert.*;
|
jaroslav@50
|
46 |
|
jaroslav@50
|
47 |
/**
|
jaroslav@50
|
48 |
*
|
jaroslav@50
|
49 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@50
|
50 |
*/
|
jaroslav@50
|
51 |
public class UITest extends Object {
|
jaroslav@73
|
52 |
private static File dir;
|
jaroslav@126
|
53 |
private static HttpServer stopAPI;
|
martin@180
|
54 |
private static HttpServer stopStatistics;
|
jaroslav@73
|
55 |
private static Callable<Void> stop;
|
jaroslav@50
|
56 |
private WebResource webResource;
|
jaroslav@121
|
57 |
private WebResource apiResource;
|
martin@180
|
58 |
private WebResource statResource;
|
jaroslav@50
|
59 |
|
jaroslav@50
|
60 |
public UITest() throws Exception {
|
jaroslav@50
|
61 |
}
|
jaroslav@50
|
62 |
|
jaroslav@62
|
63 |
@BeforeClass
|
jaroslav@73
|
64 |
public static void localeEnglish() throws Exception {
|
jaroslav@62
|
65 |
Locale.setDefault(Locale.ENGLISH);
|
jaroslav@73
|
66 |
dir = File.createTempFile("quoridor", ".dir");
|
jaroslav@73
|
67 |
dir.delete();
|
jaroslav@73
|
68 |
dir.mkdirs();
|
jaroslav@73
|
69 |
System.setProperty("quoridor.dir", dir.getPath());
|
jaroslav@126
|
70 |
stopAPI = Quoridor.start(9990);
|
martin@180
|
71 |
stopStatistics = Quoridor.start(9992);
|
jaroslav@234
|
72 |
stop = UI.startServers(9991, "http://localhost:9990", "http://localhost:9992", null);
|
jaroslav@73
|
73 |
|
jaroslav@73
|
74 |
File passwd = new File(dir, "passwd");
|
jaroslav@73
|
75 |
FileOutputStream os = new FileOutputStream(passwd);
|
jtulach@82
|
76 |
os.write("test=pes\nJarda=darda\n".getBytes("UTF-8"));
|
jaroslav@73
|
77 |
os.close();
|
jaroslav@62
|
78 |
}
|
jaroslav@62
|
79 |
|
jaroslav@50
|
80 |
@Before
|
jaroslav@50
|
81 |
public void setUp() throws Exception {
|
jaroslav@50
|
82 |
|
jaroslav@50
|
83 |
Client client = new Client();
|
jaroslav@68
|
84 |
webResource = client.resource(new URI("http://localhost:9991/"));
|
jaroslav@124
|
85 |
apiResource = client.resource(new URI("http://localhost:9990/"));
|
martin@180
|
86 |
statResource = client.resource(new URI("http://localhost:9992/"));
|
jaroslav@50
|
87 |
}
|
jaroslav@50
|
88 |
|
jaroslav@73
|
89 |
@AfterClass
|
jaroslav@73
|
90 |
public static void cleanUpAll() throws Exception {
|
jaroslav@50
|
91 |
deleteRec(dir);
|
jaroslav@50
|
92 |
if (stop != null) {
|
jaroslav@50
|
93 |
stop.call();
|
jaroslav@50
|
94 |
}
|
jaroslav@126
|
95 |
if (stopAPI != null) {
|
jaroslav@126
|
96 |
stopAPI.stop(0);
|
jaroslav@126
|
97 |
}
|
jaroslav@50
|
98 |
}
|
jaroslav@50
|
99 |
|
jaroslav@50
|
100 |
static void deleteRec(File dir) throws IOException {
|
jaroslav@50
|
101 |
if (dir == null) {
|
jaroslav@50
|
102 |
return;
|
jaroslav@50
|
103 |
}
|
jaroslav@50
|
104 |
File[] arr = dir.listFiles();
|
jaroslav@50
|
105 |
if (arr != null) {
|
jaroslav@50
|
106 |
for (File f : arr) {
|
jaroslav@50
|
107 |
deleteRec(f);
|
jaroslav@50
|
108 |
}
|
jaroslav@50
|
109 |
}
|
jaroslav@50
|
110 |
dir.delete();
|
jaroslav@50
|
111 |
}
|
jaroslav@50
|
112 |
|
jaroslav@50
|
113 |
@Test public void testApplicationWadl() {
|
jaroslav@50
|
114 |
String serviceWadl = webResource.path("application.wadl").
|
jaroslav@50
|
115 |
accept(MediaTypes.WADL).get(String.class);
|
jaroslav@50
|
116 |
assertTrue(serviceWadl.length() > 0);
|
jaroslav@50
|
117 |
}
|
jaroslav@50
|
118 |
|
jaroslav@124
|
119 |
@Test public void testAPIWadl() {
|
jaroslav@124
|
120 |
String serviceWadl = apiResource.path("application.wadl").
|
jaroslav@124
|
121 |
accept(MediaTypes.WADL).get(String.class);
|
jaroslav@124
|
122 |
assertTrue(serviceWadl.length() > 0);
|
jaroslav@124
|
123 |
}
|
jaroslav@124
|
124 |
|
martin@180
|
125 |
@Test public void testStatWadl() {
|
martin@180
|
126 |
String serviceWadl = statResource.path("application.wadl").
|
martin@180
|
127 |
accept(MediaTypes.WADL).get(String.class);
|
martin@180
|
128 |
assertTrue(serviceWadl.length() > 0);
|
martin@180
|
129 |
}
|
martin@180
|
130 |
|
jaroslav@50
|
131 |
@Test public void testGetIndexPage() throws Exception {
|
jaroslav@121
|
132 |
String logJarda = apiResource.path("login").
|
jtulach@82
|
133 |
queryParam("name", "Jarda").
|
jtulach@82
|
134 |
queryParam("password", "darda").
|
jtulach@82
|
135 |
accept(MediaType.TEXT_PLAIN).
|
jtulach@82
|
136 |
put(String.class);
|
jtulach@82
|
137 |
assertNotNull("Logged in OK", logJarda);
|
jtulach@82
|
138 |
|
jaroslav@50
|
139 |
String res = webResource.accept("text/html").get(String.class);
|
jaroslav@50
|
140 |
if (res.indexOf("Quoridor") == -1) {
|
jaroslav@50
|
141 |
fail("Wrong index.html:\n" + res);
|
jaroslav@50
|
142 |
}
|
jaroslav@51
|
143 |
if (res.indexOf("Login") == -1) {
|
jaroslav@51
|
144 |
fail("Wrong index.html:\n" + res);
|
jaroslav@51
|
145 |
}
|
jaroslav@72
|
146 |
if (res.indexOf("action=\"/login\"") == -1) {
|
jaroslav@51
|
147 |
fail("Wrong index.html:\n" + res);
|
jaroslav@51
|
148 |
}
|
jtulach@54
|
149 |
if (res.toLowerCase().indexOf("error") != -1) {
|
jtulach@54
|
150 |
fail("There was an error:\n" + res);
|
jtulach@54
|
151 |
}
|
jtulach@82
|
152 |
res = webResource.cookie(Cookie.valueOf("login=" + logJarda)).accept("text/html").get(String.class);
|
jtulach@74
|
153 |
if (res.indexOf("action=\"/games/create\"") == -1) {
|
jaroslav@51
|
154 |
fail(res);
|
jaroslav@51
|
155 |
}
|
jaroslav@50
|
156 |
}
|
jaroslav@50
|
157 |
|
jaroslav@73
|
158 |
@Test public void testLogin() throws Exception {
|
jaroslav@73
|
159 |
MultivaluedMap formData = new MultivaluedMapImpl();
|
jaroslav@73
|
160 |
formData.add("name", "test");
|
jaroslav@73
|
161 |
formData.add("password", "pes");
|
jaroslav@73
|
162 |
ClientResponse response = webResource.path("login").
|
jaroslav@73
|
163 |
accept("text/html").type("application/x-www-form-urlencoded").
|
jaroslav@73
|
164 |
post(ClientResponse.class, formData);
|
jaroslav@73
|
165 |
final String res = response.getEntity(String.class);
|
jaroslav@73
|
166 |
assertEquals("OK", ClientResponse.Status.OK, response.getClientResponseStatus());
|
jtulach@81
|
167 |
if (res.indexOf("You are logged in as test") == -1) {
|
jaroslav@73
|
168 |
fail("res: " + res);
|
jaroslav@73
|
169 |
}
|
jaroslav@73
|
170 |
}
|
jaroslav@73
|
171 |
|
jaroslav@73
|
172 |
|
jaroslav@73
|
173 |
@Test public void testCreateGameWrongUsers() throws Exception {
|
jaroslav@121
|
174 |
String logTest = apiResource.path("login").
|
jtulach@82
|
175 |
queryParam("name", "test").
|
jtulach@82
|
176 |
queryParam("password", "pes").
|
jtulach@82
|
177 |
accept(MediaType.TEXT_PLAIN).
|
jtulach@82
|
178 |
put(String.class);
|
jaroslav@73
|
179 |
ClientResponse res = webResource.path("games/create").
|
jaroslav@73
|
180 |
queryParam("white", "unknown1").
|
jaroslav@73
|
181 |
queryParam("black", "unknown2").
|
jtulach@82
|
182 |
cookie(Cookie.valueOf("login=" + logTest)).
|
jaroslav@73
|
183 |
accept("text/html").
|
jaroslav@73
|
184 |
get(ClientResponse.class);
|
jaroslav@73
|
185 |
final String txt = res.getEntity(String.class);
|
jaroslav@73
|
186 |
assertEquals("Rejected, unknown user\n" + txt, ClientResponse.Status.NOT_FOUND, res.getClientResponseStatus());
|
jaroslav@73
|
187 |
if (txt.indexOf("You (test) must be") == -1) {
|
jaroslav@73
|
188 |
fail(txt);
|
jaroslav@73
|
189 |
}
|
jaroslav@73
|
190 |
}
|
jaroslav@73
|
191 |
|
jaroslav@73
|
192 |
@Test public void testCreateGameOK() throws Exception {
|
jaroslav@121
|
193 |
String logTest = apiResource.path("login").
|
jtulach@82
|
194 |
queryParam("name", "test").
|
jtulach@82
|
195 |
queryParam("password", "pes").
|
jtulach@82
|
196 |
accept(MediaType.TEXT_PLAIN).
|
jtulach@82
|
197 |
put(String.class);
|
jaroslav@121
|
198 |
String logJarda = apiResource.path("login").
|
jaroslav@106
|
199 |
queryParam("name", "Jarda").
|
jaroslav@106
|
200 |
queryParam("password", "darda").
|
jaroslav@106
|
201 |
accept(MediaType.TEXT_PLAIN).
|
jaroslav@106
|
202 |
put(String.class);
|
jaroslav@73
|
203 |
ClientResponse res = webResource.path("games/create").
|
jaroslav@106
|
204 |
queryParam("white", "Jarda").
|
jaroslav@73
|
205 |
queryParam("black", "test").
|
jtulach@82
|
206 |
cookie(Cookie.valueOf("login=" + logTest)).
|
jaroslav@73
|
207 |
accept("text/html").
|
jaroslav@73
|
208 |
get(ClientResponse.class);
|
jaroslav@73
|
209 |
|
jaroslav@73
|
210 |
final String txt = res.getEntity(String.class);
|
jaroslav@73
|
211 |
assertEquals("OK\n" + txt, ClientResponse.Status.OK, res.getClientResponseStatus());
|
jaroslav@73
|
212 |
|
jaroslav@73
|
213 |
String[] games = new File(dir, "games").list();
|
jaroslav@73
|
214 |
assertEquals("One game exists", 1, games.length);
|
jaroslav@73
|
215 |
|
jaroslav@73
|
216 |
if (txt.indexOf(games[0]) == -1) {
|
jaroslav@73
|
217 |
fail(games[0] + " expected inside of:\n" + txt);
|
jaroslav@73
|
218 |
}
|
jaroslav@106
|
219 |
|
jaroslav@106
|
220 |
ClientResponse page = webResource.path("games/" + games[0]).
|
jaroslav@106
|
221 |
cookie(Cookie.valueOf("login=" + logJarda)).
|
jaroslav@106
|
222 |
get(ClientResponse.class);
|
jaroslav@106
|
223 |
String ptxt = page.getEntity(String.class);
|
jaroslav@106
|
224 |
assertEquals("OK:\n" + ptxt, ClientResponse.Status.OK, page.getClientResponseStatus());
|
jaroslav@106
|
225 |
|
jaroslav@113
|
226 |
Pattern p = Pattern.compile(".*<img[^>]*src=\"([^\"]*)\"");
|
jaroslav@106
|
227 |
Matcher m = p.matcher(ptxt);
|
jaroslav@106
|
228 |
assertTrue("image found\n" + ptxt, m.find());
|
jaroslav@106
|
229 |
|
jaroslav@106
|
230 |
InputStream img1 = webResource.path(m.group(1)).get(InputStream.class);
|
jaroslav@106
|
231 |
assertNotNull("image found", img1);
|
jaroslav@106
|
232 |
|
jaroslav@121
|
233 |
ClientResponse move = apiResource.path("games/" + games[0]).
|
jaroslav@106
|
234 |
queryParam("loginID", logJarda).
|
jaroslav@106
|
235 |
queryParam("player", "Jarda").queryParam("move", "N").put(ClientResponse.class);
|
jaroslav@106
|
236 |
assertEquals("Move OK:\n" + move.getEntity(String.class), ClientResponse.Status.OK, move.getClientResponseStatus());
|
jaroslav@106
|
237 |
|
jaroslav@106
|
238 |
InputStream img2 = webResource.path(m.group(1)).get(InputStream.class);
|
jaroslav@106
|
239 |
assertNotNull("image found", img2);
|
jaroslav@106
|
240 |
|
jaroslav@106
|
241 |
ClientResponse page2 = webResource.path("games/" + games[0]).
|
jaroslav@106
|
242 |
cookie(Cookie.valueOf("login=" + logJarda)).
|
jaroslav@106
|
243 |
get(ClientResponse.class);
|
jaroslav@106
|
244 |
String ptxt2 = page2.getEntity(String.class);
|
jaroslav@106
|
245 |
assertEquals("OK:\n" + ptxt2, ClientResponse.Status.OK, page2.getClientResponseStatus());
|
jaroslav@106
|
246 |
|
jaroslav@106
|
247 |
Matcher m2 = p.matcher(ptxt2);
|
jaroslav@106
|
248 |
assertTrue("image found\n" + ptxt2, m2.find());
|
jaroslav@106
|
249 |
|
jaroslav@106
|
250 |
InputStream img3 = webResource.path(m2.group(1)).get(InputStream.class);
|
jaroslav@106
|
251 |
assertNotNull("image found", img3);
|
jaroslav@106
|
252 |
|
jaroslav@106
|
253 |
int diff = 0;
|
jaroslav@106
|
254 |
int cnt = 0;
|
jaroslav@106
|
255 |
for (;;) {
|
jaroslav@106
|
256 |
cnt++;
|
jaroslav@106
|
257 |
int b1 = img1.read();
|
jaroslav@106
|
258 |
int b2 = img2.read();
|
jaroslav@106
|
259 |
int b3 = img3.read();
|
jaroslav@106
|
260 |
assertEquals(b1, b2);
|
jaroslav@106
|
261 |
if (b3 != b1) {
|
jaroslav@106
|
262 |
diff++;
|
jaroslav@106
|
263 |
}
|
jaroslav@106
|
264 |
if (b1 == -1 || b3 == -1) break;
|
jaroslav@106
|
265 |
|
jaroslav@106
|
266 |
}
|
jaroslav@106
|
267 |
if (diff == 0) {
|
jaroslav@106
|
268 |
// fail("There shall be difference in the streams. Read bytes " + cnt);
|
jaroslav@106
|
269 |
}
|
jaroslav@73
|
270 |
}
|
jaroslav@73
|
271 |
|
jaroslav@50
|
272 |
}
|