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 org.glassfish.grizzly.http.server.HttpServer;
21 import java.util.Collection;
22 import java.util.Arrays;
23 import com.sun.jersey.api.client.Client;
24 import com.sun.jersey.api.client.ClientResponse;
25 import com.sun.jersey.api.client.WebResource;
26 import com.sun.jersey.core.header.MediaTypes;
27 import com.sun.jersey.core.util.MultivaluedMapImpl;
28 import cz.xelfi.quoridor.webidor.resources.Quoridor;
30 import java.io.FileOutputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
34 import java.util.HashSet;
35 import java.util.Locale;
36 import java.util.concurrent.Callable;
37 import java.util.regex.Matcher;
38 import java.util.regex.Pattern;
39 import javax.ws.rs.core.Cookie;
40 import javax.ws.rs.core.MediaType;
41 import javax.ws.rs.core.MultivaluedMap;
42 import org.junit.AfterClass;
43 import org.junit.Before;
44 import org.junit.BeforeClass;
45 import org.junit.Test;
46 import static org.junit.Assert.*;
50 * @author Jaroslav Tulach <jtulach@netbeans.org>
52 public class UITest extends Object {
53 private static File dir;
54 private static HttpServer stopAPI;
55 private static HttpServer stopStatistics;
56 private static Callable<Void> stop;
58 private WebResource webResource;
59 private WebResource apiResource;
60 private WebResource statResource;
62 public UITest() throws Exception {
66 public static void localeEnglish() throws Exception {
67 Locale.setDefault(Locale.ENGLISH);
68 dir = File.createTempFile("quoridor", ".dir");
72 File games = new File(dir, "games");
73 copyResource("wrong.encoding", new File(games, "fdcc275c-55ad-4b7e-a776-bde3876b2f2c"));
76 System.setProperty("quoridor.dir", dir.getPath());
77 stopAPI = Quoridor.start(9990);
78 stopStatistics = Quoridor.start(9992);
79 stop = UI.startServers(9991, "http://localhost:9990", "http://localhost:9992", null);
81 File passwd = new File(dir, "passwd");
82 FileOutputStream os = new FileOutputStream(passwd);
83 os.write("test=pes\nJarda=darda\n".getBytes("UTF-8"));
88 public void setUp() throws Exception {
90 Client client = new Client();
91 webResource = client.resource(new URI("http://localhost:9991/"));
92 apiResource = client.resource(new URI("http://localhost:9990/"));
93 statResource = client.resource(new URI("http://localhost:9992/"));
97 public static void cleanUpAll() throws Exception {
102 if (stopAPI != null) {
107 static void deleteRec(File dir) throws IOException {
111 File[] arr = dir.listFiles();
120 @Test public void testApplicationWadl() {
121 String serviceWadl = webResource.path("application.wadl").
122 accept(MediaTypes.WADL).get(String.class);
123 assertTrue(serviceWadl.length() > 0);
126 @Test public void testAPIWadl() {
127 String serviceWadl = apiResource.path("application.wadl").
128 accept(MediaTypes.WADL).get(String.class);
129 assertTrue(serviceWadl.length() > 0);
132 @Test public void testStatWadl() {
133 String serviceWadl = statResource.path("application.wadl").
134 accept(MediaTypes.WADL).get(String.class);
135 assertTrue(serviceWadl.length() > 0);
138 @Test public void testGetIndexPage() throws Exception {
139 String logJarda = apiResource.path("login").
140 queryParam("name", "Jarda").
141 queryParam("password", "darda").
142 accept(MediaType.TEXT_PLAIN).
144 assertNotNull("Logged in OK", logJarda);
146 String res = webResource.accept("text/html").get(String.class);
147 if (res.indexOf("Quoridor") == -1) {
148 fail("Wrong index.html:\n" + res);
150 if (res.indexOf("Login") == -1) {
151 fail("Wrong index.html:\n" + res);
153 if (res.indexOf("action=\"/login\"") == -1) {
154 fail("Wrong index.html:\n" + res);
156 if (res.toLowerCase().indexOf("error") != -1) {
157 fail("There was an error:\n" + res);
159 res = webResource.cookie(Cookie.valueOf("login=" + logJarda)).accept("text/html").get(String.class);
160 if (res.indexOf("action=\"/games/create\"") == -1) {
165 @Test public void testLogin() throws Exception {
166 MultivaluedMap formData = new MultivaluedMapImpl();
167 formData.add("name", "test");
168 formData.add("password", "pes");
169 ClientResponse response = webResource.path("login").
170 accept("text/html").type("application/x-www-form-urlencoded").
171 post(ClientResponse.class, formData);
172 final String res = response.getEntity(String.class);
173 assertEquals("OK", ClientResponse.Status.OK, response.getClientResponseStatus());
174 if (res.indexOf("You are logged in as test") == -1) {
180 @Test public void testCreateGameWrongUsers() throws Exception {
181 String logTest = apiResource.path("login").
182 queryParam("name", "test").
183 queryParam("password", "pes").
184 accept(MediaType.TEXT_PLAIN).
186 ClientResponse res = webResource.path("games/create").
187 queryParam("white", "unknown1").
188 queryParam("black", "unknown2").
189 cookie(Cookie.valueOf("login=" + logTest)).
191 get(ClientResponse.class);
192 final String txt = res.getEntity(String.class);
193 assertEquals("Rejected, unknown user\n" + txt, ClientResponse.Status.NOT_FOUND, res.getClientResponseStatus());
194 if (txt.indexOf("You (test) must be") == -1) {
199 @Test public void testCreateGameOK() throws Exception {
200 Collection<String> origGamesNames= Arrays.asList(new File(dir, "games").list());
201 int origGames = origGamesNames.size();
203 String logTest = apiResource.path("login").
204 queryParam("name", "test").
205 queryParam("password", "pes").
206 accept(MediaType.TEXT_PLAIN).
208 String logJarda = apiResource.path("login").
209 queryParam("name", "Jarda").
210 queryParam("password", "darda").
211 accept(MediaType.TEXT_PLAIN).
213 ClientResponse res = webResource.path("games/create").
214 queryParam("white", "Jarda").
215 queryParam("black", "test").
216 cookie(Cookie.valueOf("login=" + logTest)).
218 get(ClientResponse.class);
220 final String txt = res.getEntity(String.class);
221 assertEquals("OK\n" + txt, ClientResponse.Status.OK, res.getClientResponseStatus());
226 String[] games = new File(dir, "games").list();
227 assertEquals("One new game", origGames + 1, games.length);
229 HashSet<String> newNames = new HashSet<String>();
230 newNames.addAll(Arrays.asList(games));
231 newNames.removeAll(origGamesNames);
232 assertEquals("Just one name left: " + newNames, 1, newNames.size());
233 myGame = newNames.iterator().next();
236 if (txt.indexOf(myGame) == -1) {
237 fail(myGame + " expected inside of:\n" + txt);
240 ClientResponse page = webResource.path("games/" + myGame).
241 cookie(Cookie.valueOf("login=" + logJarda)).
242 get(ClientResponse.class);
243 String ptxt = page.getEntity(String.class);
244 assertEquals("OK:\n" + ptxt, ClientResponse.Status.OK, page.getClientResponseStatus());
246 Pattern p = Pattern.compile(".*<img[^>]*src=\"([^\"]*)\"");
247 Matcher m = p.matcher(ptxt);
248 assertTrue("image found\n" + ptxt, m.find());
250 InputStream img1 = webResource.path(m.group(1)).get(InputStream.class);
251 assertNotNull("image found", img1);
253 ClientResponse move = apiResource.path("games/" + myGame).
254 queryParam("loginID", logJarda).
255 queryParam("player", "Jarda").queryParam("move", "N").put(ClientResponse.class);
256 assertEquals("Move OK:\n" + move.getEntity(String.class), ClientResponse.Status.OK, move.getClientResponseStatus());
258 InputStream img2 = webResource.path(m.group(1)).get(InputStream.class);
259 assertNotNull("image found", img2);
261 ClientResponse page2 = webResource.path("games/" + myGame).
262 cookie(Cookie.valueOf("login=" + logJarda)).
263 get(ClientResponse.class);
264 String ptxt2 = page2.getEntity(String.class);
265 assertEquals("OK:\n" + ptxt2, ClientResponse.Status.OK, page2.getClientResponseStatus());
267 Matcher m2 = p.matcher(ptxt2);
268 assertTrue("image found\n" + ptxt2, m2.find());
270 InputStream img3 = webResource.path(m2.group(1)).get(InputStream.class);
271 assertNotNull("image found", img3);
277 int b1 = img1.read();
278 int b2 = img2.read();
279 int b3 = img3.read();
280 assertEquals(b1, b2);
284 if (b1 == -1 || b3 == -1) break;
288 // fail("There shall be difference in the streams. Read bytes " + cnt);
292 @Test public void testGetWrongGame() throws Exception {
293 String logTest = apiResource.path("login").
294 queryParam("name", "test").
295 queryParam("password", "pes").
296 accept(MediaType.TEXT_PLAIN).
299 String xml = apiResource.path("games/fdcc275c-55ad-4b7e-a776-bde3876b2f2c").
300 queryParam("name", "test").
301 queryParam("password", "pes").
302 accept(MediaType.TEXT_XML).
305 if (xml.indexOf((char)0x1b) >= 0) {
306 fail(xml.replace((char)0x1b, 'X'));
309 ClientResponse res = webResource.path("games/fdcc275c-55ad-4b7e-a776-bde3876b2f2c").
310 queryParam("white", "Jarda").
311 queryParam("black", "test").
312 cookie(Cookie.valueOf("login=" + logTest)).
314 get(ClientResponse.class);
315 final String txt = res.getEntity(String.class);
316 assertEquals("OK\n" + txt, ClientResponse.Status.OK, res.getClientResponseStatus());
320 private static void copyResource(String res, File file) throws IOException {
321 InputStream is = UITest.class.getResourceAsStream(res);
322 file.getParentFile().mkdirs();
323 FileOutputStream os = new FileOutputStream(file);
324 byte[] arr = new byte[4092];
326 int len = is.read(arr);
330 os.write(arr, 0, len);