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/.
18 package cz.xelfi.quoridor.freemarkerdor;
20 import com.sun.jersey.api.client.Client;
21 import com.sun.jersey.api.client.ClientResponse;
22 import com.sun.jersey.api.client.WebResource;
23 import com.sun.jersey.core.header.MediaTypes;
24 import com.sun.jersey.core.util.MultivaluedMapImpl;
25 import com.sun.net.httpserver.HttpServer;
26 import cz.xelfi.quoridor.webidor.resources.Quoridor;
28 import java.io.FileOutputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
32 import java.util.Locale;
33 import java.util.concurrent.Callable;
34 import java.util.regex.Matcher;
35 import java.util.regex.Pattern;
36 import javax.ws.rs.core.Cookie;
37 import javax.ws.rs.core.MediaType;
38 import javax.ws.rs.core.MultivaluedMap;
39 import org.junit.AfterClass;
40 import org.junit.Before;
41 import org.junit.BeforeClass;
42 import org.junit.Test;
43 import static org.junit.Assert.*;
47 * @author Jaroslav Tulach <jtulach@netbeans.org>
49 public class UITest extends Object {
50 private static File dir;
51 private static HttpServer stopAPI;
52 private static HttpServer stopStatistics;
53 private static Callable<Void> stop;
55 private WebResource webResource;
56 private WebResource apiResource;
57 private WebResource statResource;
59 public UITest() throws Exception {
63 public static void localeEnglish() throws Exception {
64 Locale.setDefault(Locale.ENGLISH);
65 dir = File.createTempFile("quoridor", ".dir");
69 File games = new File(dir, "games");
70 copyResource("wrong.encoding", new File(games, "fdcc275c-55ad-4b7e-a776-bde3876b2f2c"));
73 System.setProperty("quoridor.dir", dir.getPath());
74 stopAPI = Quoridor.start(9990);
75 stopStatistics = Quoridor.start(9992);
76 stop = UI.startServers(9991, "http://localhost:9990", "http://localhost:9992", null);
78 File passwd = new File(dir, "passwd");
79 FileOutputStream os = new FileOutputStream(passwd);
80 os.write("test=pes\nJarda=darda\n".getBytes("UTF-8"));
85 public void setUp() throws Exception {
87 Client client = new Client();
88 webResource = client.resource(new URI("http://localhost:9991/"));
89 apiResource = client.resource(new URI("http://localhost:9990/"));
90 statResource = client.resource(new URI("http://localhost:9992/"));
94 public static void cleanUpAll() throws Exception {
99 if (stopAPI != null) {
104 static void deleteRec(File dir) throws IOException {
108 File[] arr = dir.listFiles();
117 @Test public void testApplicationWadl() {
118 String serviceWadl = webResource.path("application.wadl").
119 accept(MediaTypes.WADL).get(String.class);
120 assertTrue(serviceWadl.length() > 0);
123 @Test public void testAPIWadl() {
124 String serviceWadl = apiResource.path("application.wadl").
125 accept(MediaTypes.WADL).get(String.class);
126 assertTrue(serviceWadl.length() > 0);
129 @Test public void testStatWadl() {
130 String serviceWadl = statResource.path("application.wadl").
131 accept(MediaTypes.WADL).get(String.class);
132 assertTrue(serviceWadl.length() > 0);
135 @Test public void testGetIndexPage() throws Exception {
136 String logJarda = apiResource.path("login").
137 queryParam("name", "Jarda").
138 queryParam("password", "darda").
139 accept(MediaType.TEXT_PLAIN).
141 assertNotNull("Logged in OK", logJarda);
143 String res = webResource.accept("text/html").get(String.class);
144 if (res.indexOf("Quoridor") == -1) {
145 fail("Wrong index.html:\n" + res);
147 if (res.indexOf("Login") == -1) {
148 fail("Wrong index.html:\n" + res);
150 if (res.indexOf("action=\"/login\"") == -1) {
151 fail("Wrong index.html:\n" + res);
153 if (res.toLowerCase().indexOf("error") != -1) {
154 fail("There was an error:\n" + res);
156 res = webResource.cookie(Cookie.valueOf("login=" + logJarda)).accept("text/html").get(String.class);
157 if (res.indexOf("action=\"/games/create\"") == -1) {
162 @Test public void testLogin() throws Exception {
163 MultivaluedMap formData = new MultivaluedMapImpl();
164 formData.add("name", "test");
165 formData.add("password", "pes");
166 ClientResponse response = webResource.path("login").
167 accept("text/html").type("application/x-www-form-urlencoded").
168 post(ClientResponse.class, formData);
169 final String res = response.getEntity(String.class);
170 assertEquals("OK", ClientResponse.Status.OK, response.getClientResponseStatus());
171 if (res.indexOf("You are logged in as test") == -1) {
177 @Test public void testCreateGameWrongUsers() throws Exception {
178 String logTest = apiResource.path("login").
179 queryParam("name", "test").
180 queryParam("password", "pes").
181 accept(MediaType.TEXT_PLAIN).
183 ClientResponse res = webResource.path("games/create").
184 queryParam("white", "unknown1").
185 queryParam("black", "unknown2").
186 cookie(Cookie.valueOf("login=" + logTest)).
188 get(ClientResponse.class);
189 final String txt = res.getEntity(String.class);
190 assertEquals("Rejected, unknown user\n" + txt, ClientResponse.Status.NOT_FOUND, res.getClientResponseStatus());
191 if (txt.indexOf("You (test) must be") == -1) {
196 @Test public void testCreateGameOK() throws Exception {
197 int origGames = new File(dir, "games").list().length;
200 String logTest = apiResource.path("login").
201 queryParam("name", "test").
202 queryParam("password", "pes").
203 accept(MediaType.TEXT_PLAIN).
205 String logJarda = apiResource.path("login").
206 queryParam("name", "Jarda").
207 queryParam("password", "darda").
208 accept(MediaType.TEXT_PLAIN).
210 ClientResponse res = webResource.path("games/create").
211 queryParam("white", "Jarda").
212 queryParam("black", "test").
213 cookie(Cookie.valueOf("login=" + logTest)).
215 get(ClientResponse.class);
217 final String txt = res.getEntity(String.class);
218 assertEquals("OK\n" + txt, ClientResponse.Status.OK, res.getClientResponseStatus());
220 String[] games = new File(dir, "games").list();
221 assertEquals("One new game", origGames + 1, games.length);
223 if (txt.indexOf(games[0]) == -1) {
224 fail(games[0] + " expected inside of:\n" + txt);
227 ClientResponse page = webResource.path("games/" + games[0]).
228 cookie(Cookie.valueOf("login=" + logJarda)).
229 get(ClientResponse.class);
230 String ptxt = page.getEntity(String.class);
231 assertEquals("OK:\n" + ptxt, ClientResponse.Status.OK, page.getClientResponseStatus());
233 Pattern p = Pattern.compile(".*<img[^>]*src=\"([^\"]*)\"");
234 Matcher m = p.matcher(ptxt);
235 assertTrue("image found\n" + ptxt, m.find());
237 InputStream img1 = webResource.path(m.group(1)).get(InputStream.class);
238 assertNotNull("image found", img1);
240 ClientResponse move = apiResource.path("games/" + games[0]).
241 queryParam("loginID", logJarda).
242 queryParam("player", "Jarda").queryParam("move", "N").put(ClientResponse.class);
243 assertEquals("Move OK:\n" + move.getEntity(String.class), ClientResponse.Status.OK, move.getClientResponseStatus());
245 InputStream img2 = webResource.path(m.group(1)).get(InputStream.class);
246 assertNotNull("image found", img2);
248 ClientResponse page2 = webResource.path("games/" + games[0]).
249 cookie(Cookie.valueOf("login=" + logJarda)).
250 get(ClientResponse.class);
251 String ptxt2 = page2.getEntity(String.class);
252 assertEquals("OK:\n" + ptxt2, ClientResponse.Status.OK, page2.getClientResponseStatus());
254 Matcher m2 = p.matcher(ptxt2);
255 assertTrue("image found\n" + ptxt2, m2.find());
257 InputStream img3 = webResource.path(m2.group(1)).get(InputStream.class);
258 assertNotNull("image found", img3);
264 int b1 = img1.read();
265 int b2 = img2.read();
266 int b3 = img3.read();
267 assertEquals(b1, b2);
271 if (b1 == -1 || b3 == -1) break;
275 // fail("There shall be difference in the streams. Read bytes " + cnt);
279 @Test public void testGetWrongGame() throws Exception {
280 String logTest = apiResource.path("login").
281 queryParam("name", "test").
282 queryParam("password", "pes").
283 accept(MediaType.TEXT_PLAIN).
286 String xml = apiResource.path("games/fdcc275c-55ad-4b7e-a776-bde3876b2f2c").
287 queryParam("name", "test").
288 queryParam("password", "pes").
289 accept(MediaType.TEXT_XML).
292 if (xml.indexOf((char)0x1b) >= 0) {
293 fail(xml.replace((char)0x1b, 'X'));
296 ClientResponse res = webResource.path("games/fdcc275c-55ad-4b7e-a776-bde3876b2f2c").
297 queryParam("white", "Jarda").
298 queryParam("black", "test").
299 cookie(Cookie.valueOf("login=" + logTest)).
301 get(ClientResponse.class);
302 final String txt = res.getEntity(String.class);
303 assertEquals("OK\n" + txt, ClientResponse.Status.OK, res.getClientResponseStatus());
307 private static void copyResource(String res, File file) throws IOException {
308 InputStream is = UITest.class.getResourceAsStream(res);
309 file.getParentFile().mkdirs();
310 FileOutputStream os = new FileOutputStream(file);
311 byte[] arr = new byte[4092];
313 int len = is.read(arr);
317 os.write(arr, 0, len);