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;
27 import cz.xelfi.quoridor.statistics.resources.Statistics;
28 import java.awt.Image;
30 import java.io.FileOutputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
34 import java.util.Locale;
35 import java.util.concurrent.Callable;
36 import java.util.regex.Matcher;
37 import java.util.regex.Pattern;
38 import javax.ws.rs.core.Cookie;
39 import javax.ws.rs.core.MediaType;
40 import javax.ws.rs.core.MultivaluedMap;
41 import org.junit.AfterClass;
42 import org.junit.Before;
43 import org.junit.BeforeClass;
44 import org.junit.Test;
45 import static org.junit.Assert.*;
49 * @author Jaroslav Tulach <jtulach@netbeans.org>
51 public class UITest extends Object {
52 private static File dir;
53 private static HttpServer stopAPI;
54 private static HttpServer stopStatistics;
55 private static Callable<Void> stop;
56 private WebResource webResource;
57 private WebResource apiResource;
58 private WebResource statResource;
60 public UITest() throws Exception {
64 public static void localeEnglish() throws Exception {
65 Locale.setDefault(Locale.ENGLISH);
66 dir = File.createTempFile("quoridor", ".dir");
69 System.setProperty("quoridor.dir", dir.getPath());
70 stopAPI = Quoridor.start(9990);
71 stopStatistics = Quoridor.start(9992);
72 stop = UI.startServers(9991, "http://localhost:9990", "http://localhost:9992", null);
74 File passwd = new File(dir, "passwd");
75 FileOutputStream os = new FileOutputStream(passwd);
76 os.write("test=pes\nJarda=darda\n".getBytes("UTF-8"));
81 public void setUp() throws Exception {
83 Client client = new Client();
84 webResource = client.resource(new URI("http://localhost:9991/"));
85 apiResource = client.resource(new URI("http://localhost:9990/"));
86 statResource = client.resource(new URI("http://localhost:9992/"));
90 public static void cleanUpAll() throws Exception {
95 if (stopAPI != null) {
100 static void deleteRec(File dir) throws IOException {
104 File[] arr = dir.listFiles();
113 @Test public void testApplicationWadl() {
114 String serviceWadl = webResource.path("application.wadl").
115 accept(MediaTypes.WADL).get(String.class);
116 assertTrue(serviceWadl.length() > 0);
119 @Test public void testAPIWadl() {
120 String serviceWadl = apiResource.path("application.wadl").
121 accept(MediaTypes.WADL).get(String.class);
122 assertTrue(serviceWadl.length() > 0);
125 @Test public void testStatWadl() {
126 String serviceWadl = statResource.path("application.wadl").
127 accept(MediaTypes.WADL).get(String.class);
128 assertTrue(serviceWadl.length() > 0);
131 @Test public void testGetIndexPage() throws Exception {
132 String logJarda = apiResource.path("login").
133 queryParam("name", "Jarda").
134 queryParam("password", "darda").
135 accept(MediaType.TEXT_PLAIN).
137 assertNotNull("Logged in OK", logJarda);
139 String res = webResource.accept("text/html").get(String.class);
140 if (res.indexOf("Quoridor") == -1) {
141 fail("Wrong index.html:\n" + res);
143 if (res.indexOf("Login") == -1) {
144 fail("Wrong index.html:\n" + res);
146 if (res.indexOf("action=\"/login\"") == -1) {
147 fail("Wrong index.html:\n" + res);
149 if (res.toLowerCase().indexOf("error") != -1) {
150 fail("There was an error:\n" + res);
152 res = webResource.cookie(Cookie.valueOf("login=" + logJarda)).accept("text/html").get(String.class);
153 if (res.indexOf("action=\"/games/create\"") == -1) {
158 @Test public void testLogin() throws Exception {
159 MultivaluedMap formData = new MultivaluedMapImpl();
160 formData.add("name", "test");
161 formData.add("password", "pes");
162 ClientResponse response = webResource.path("login").
163 accept("text/html").type("application/x-www-form-urlencoded").
164 post(ClientResponse.class, formData);
165 final String res = response.getEntity(String.class);
166 assertEquals("OK", ClientResponse.Status.OK, response.getClientResponseStatus());
167 if (res.indexOf("You are logged in as test") == -1) {
173 @Test public void testCreateGameWrongUsers() throws Exception {
174 String logTest = apiResource.path("login").
175 queryParam("name", "test").
176 queryParam("password", "pes").
177 accept(MediaType.TEXT_PLAIN).
179 ClientResponse res = webResource.path("games/create").
180 queryParam("white", "unknown1").
181 queryParam("black", "unknown2").
182 cookie(Cookie.valueOf("login=" + logTest)).
184 get(ClientResponse.class);
185 final String txt = res.getEntity(String.class);
186 assertEquals("Rejected, unknown user\n" + txt, ClientResponse.Status.NOT_FOUND, res.getClientResponseStatus());
187 if (txt.indexOf("You (test) must be") == -1) {
192 @Test public void testCreateGameOK() throws Exception {
193 String logTest = apiResource.path("login").
194 queryParam("name", "test").
195 queryParam("password", "pes").
196 accept(MediaType.TEXT_PLAIN).
198 String logJarda = apiResource.path("login").
199 queryParam("name", "Jarda").
200 queryParam("password", "darda").
201 accept(MediaType.TEXT_PLAIN).
203 ClientResponse res = webResource.path("games/create").
204 queryParam("white", "Jarda").
205 queryParam("black", "test").
206 cookie(Cookie.valueOf("login=" + logTest)).
208 get(ClientResponse.class);
210 final String txt = res.getEntity(String.class);
211 assertEquals("OK\n" + txt, ClientResponse.Status.OK, res.getClientResponseStatus());
213 String[] games = new File(dir, "games").list();
214 assertEquals("One game exists", 1, games.length);
216 if (txt.indexOf(games[0]) == -1) {
217 fail(games[0] + " expected inside of:\n" + txt);
220 ClientResponse page = webResource.path("games/" + games[0]).
221 cookie(Cookie.valueOf("login=" + logJarda)).
222 get(ClientResponse.class);
223 String ptxt = page.getEntity(String.class);
224 assertEquals("OK:\n" + ptxt, ClientResponse.Status.OK, page.getClientResponseStatus());
226 Pattern p = Pattern.compile(".*<img[^>]*src=\"([^\"]*)\"");
227 Matcher m = p.matcher(ptxt);
228 assertTrue("image found\n" + ptxt, m.find());
230 InputStream img1 = webResource.path(m.group(1)).get(InputStream.class);
231 assertNotNull("image found", img1);
233 ClientResponse move = apiResource.path("games/" + games[0]).
234 queryParam("loginID", logJarda).
235 queryParam("player", "Jarda").queryParam("move", "N").put(ClientResponse.class);
236 assertEquals("Move OK:\n" + move.getEntity(String.class), ClientResponse.Status.OK, move.getClientResponseStatus());
238 InputStream img2 = webResource.path(m.group(1)).get(InputStream.class);
239 assertNotNull("image found", img2);
241 ClientResponse page2 = webResource.path("games/" + games[0]).
242 cookie(Cookie.valueOf("login=" + logJarda)).
243 get(ClientResponse.class);
244 String ptxt2 = page2.getEntity(String.class);
245 assertEquals("OK:\n" + ptxt2, ClientResponse.Status.OK, page2.getClientResponseStatus());
247 Matcher m2 = p.matcher(ptxt2);
248 assertTrue("image found\n" + ptxt2, m2.find());
250 InputStream img3 = webResource.path(m2.group(1)).get(InputStream.class);
251 assertNotNull("image found", img3);
257 int b1 = img1.read();
258 int b2 = img2.read();
259 int b3 = img3.read();
260 assertEquals(b1, b2);
264 if (b1 == -1 || b3 == -1) break;
268 // fail("There shall be difference in the streams. Read bytes " + cnt);