Removing compile time dependency on webidor, its functionality is accessed only via REST
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * The contents of this file are subject to the terms of either the GNU
5 * General Public License Version 2 only ("GPL") or the Common
6 * Development and Distribution License("CDDL") (collectively, the
7 * "License"). You may not use this file except in compliance with the
8 * License. You can obtain a copy of the License at
9 * http://www.netbeans.org/cddl-gplv2.html
10 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
11 * specific language governing permissions and limitations under the
12 * License. When distributing the software, include this License Header
13 * Notice in each file and include the License file at
14 * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
15 * particular file as subject to the "Classpath" exception as provided
16 * by Sun in the GPL Version 2 section of the License file that
17 * accompanied this code. If applicable, add the following below the
18 * License Header, with the fields enclosed by brackets [] replaced by
19 * your own identifying information:
20 * "Portions Copyrighted [year] [name of copyright owner]"
24 * Portions Copyrighted 2009 Jaroslav Tulach
27 package cz.xelfi.quoridor.freemarkerdor;
29 import com.sun.jersey.api.client.Client;
30 import com.sun.jersey.api.client.ClientResponse;
31 import com.sun.jersey.api.client.WebResource;
32 import com.sun.jersey.core.header.MediaTypes;
33 import com.sun.jersey.core.util.MultivaluedMapImpl;
34 import com.sun.net.httpserver.HttpServer;
35 import cz.xelfi.quoridor.webidor.resources.Quoridor;
36 import java.awt.Image;
38 import java.io.FileOutputStream;
39 import java.io.IOException;
40 import java.io.InputStream;
42 import java.util.Locale;
43 import java.util.concurrent.Callable;
44 import java.util.regex.Matcher;
45 import java.util.regex.Pattern;
46 import javax.ws.rs.core.Cookie;
47 import javax.ws.rs.core.MediaType;
48 import javax.ws.rs.core.MultivaluedMap;
49 import org.junit.AfterClass;
50 import org.junit.Before;
51 import org.junit.BeforeClass;
52 import org.junit.Test;
53 import static org.junit.Assert.*;
57 * @author Jaroslav Tulach <jtulach@netbeans.org>
59 public class UITest extends Object {
60 private static File dir;
61 private static HttpServer stopAPI;
62 private static Callable<Void> stop;
63 private WebResource webResource;
64 private WebResource apiResource;
66 public UITest() throws Exception {
70 public static void localeEnglish() throws Exception {
71 Locale.setDefault(Locale.ENGLISH);
72 dir = File.createTempFile("quoridor", ".dir");
75 System.setProperty("quoridor.dir", dir.getPath());
76 stopAPI = Quoridor.start(9990);
77 stop = UI.startServers(9991, "http://localhost:9990");
79 File passwd = new File(dir, "passwd");
80 FileOutputStream os = new FileOutputStream(passwd);
81 os.write("test=pes\nJarda=darda\n".getBytes("UTF-8"));
86 public void setUp() throws Exception {
88 Client client = new Client();
89 webResource = client.resource(new URI("http://localhost:9991/"));
90 apiResource = client.resource(new URI("http://localhost:9990/"));
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 testGetIndexPage() throws Exception {
130 String logJarda = apiResource.path("login").
131 queryParam("name", "Jarda").
132 queryParam("password", "darda").
133 accept(MediaType.TEXT_PLAIN).
135 assertNotNull("Logged in OK", logJarda);
137 String res = webResource.accept("text/html").get(String.class);
138 if (res.indexOf("Quoridor") == -1) {
139 fail("Wrong index.html:\n" + res);
141 if (res.indexOf("Login") == -1) {
142 fail("Wrong index.html:\n" + res);
144 if (res.indexOf("action=\"/login\"") == -1) {
145 fail("Wrong index.html:\n" + res);
147 if (res.toLowerCase().indexOf("error") != -1) {
148 fail("There was an error:\n" + res);
150 res = webResource.cookie(Cookie.valueOf("login=" + logJarda)).accept("text/html").get(String.class);
151 if (res.indexOf("action=\"/games/create\"") == -1) {
156 @Test public void testLogin() throws Exception {
157 MultivaluedMap formData = new MultivaluedMapImpl();
158 formData.add("name", "test");
159 formData.add("password", "pes");
160 ClientResponse response = webResource.path("login").
161 accept("text/html").type("application/x-www-form-urlencoded").
162 post(ClientResponse.class, formData);
163 final String res = response.getEntity(String.class);
164 assertEquals("OK", ClientResponse.Status.OK, response.getClientResponseStatus());
165 if (res.indexOf("You are logged in as test") == -1) {
171 @Test public void testCreateGameWrongUsers() throws Exception {
172 String logTest = apiResource.path("login").
173 queryParam("name", "test").
174 queryParam("password", "pes").
175 accept(MediaType.TEXT_PLAIN).
177 ClientResponse res = webResource.path("games/create").
178 queryParam("white", "unknown1").
179 queryParam("black", "unknown2").
180 cookie(Cookie.valueOf("login=" + logTest)).
182 get(ClientResponse.class);
183 final String txt = res.getEntity(String.class);
184 assertEquals("Rejected, unknown user\n" + txt, ClientResponse.Status.NOT_FOUND, res.getClientResponseStatus());
185 if (txt.indexOf("You (test) must be") == -1) {
190 @Test public void testCreateGameOK() throws Exception {
191 String logTest = apiResource.path("login").
192 queryParam("name", "test").
193 queryParam("password", "pes").
194 accept(MediaType.TEXT_PLAIN).
196 String logJarda = apiResource.path("login").
197 queryParam("name", "Jarda").
198 queryParam("password", "darda").
199 accept(MediaType.TEXT_PLAIN).
201 ClientResponse res = webResource.path("games/create").
202 queryParam("white", "Jarda").
203 queryParam("black", "test").
204 cookie(Cookie.valueOf("login=" + logTest)).
206 get(ClientResponse.class);
208 final String txt = res.getEntity(String.class);
209 assertEquals("OK\n" + txt, ClientResponse.Status.OK, res.getClientResponseStatus());
211 String[] games = new File(dir, "games").list();
212 assertEquals("One game exists", 1, games.length);
214 if (txt.indexOf(games[0]) == -1) {
215 fail(games[0] + " expected inside of:\n" + txt);
218 ClientResponse page = webResource.path("games/" + games[0]).
219 cookie(Cookie.valueOf("login=" + logJarda)).
220 get(ClientResponse.class);
221 String ptxt = page.getEntity(String.class);
222 assertEquals("OK:\n" + ptxt, ClientResponse.Status.OK, page.getClientResponseStatus());
224 Pattern p = Pattern.compile(".*<img[^>]*src=\"([^\"]*)\"");
225 Matcher m = p.matcher(ptxt);
226 assertTrue("image found\n" + ptxt, m.find());
228 InputStream img1 = webResource.path(m.group(1)).get(InputStream.class);
229 assertNotNull("image found", img1);
231 ClientResponse move = apiResource.path("games/" + games[0]).
232 queryParam("loginID", logJarda).
233 queryParam("player", "Jarda").queryParam("move", "N").put(ClientResponse.class);
234 assertEquals("Move OK:\n" + move.getEntity(String.class), ClientResponse.Status.OK, move.getClientResponseStatus());
236 InputStream img2 = webResource.path(m.group(1)).get(InputStream.class);
237 assertNotNull("image found", img2);
239 ClientResponse page2 = webResource.path("games/" + games[0]).
240 cookie(Cookie.valueOf("login=" + logJarda)).
241 get(ClientResponse.class);
242 String ptxt2 = page2.getEntity(String.class);
243 assertEquals("OK:\n" + ptxt2, ClientResponse.Status.OK, page2.getClientResponseStatus());
245 Matcher m2 = p.matcher(ptxt2);
246 assertTrue("image found\n" + ptxt2, m2.find());
248 InputStream img3 = webResource.path(m2.group(1)).get(InputStream.class);
249 assertNotNull("image found", img3);
255 int b1 = img1.read();
256 int b2 = img2.read();
257 int b3 = img3.read();
258 assertEquals(b1, b2);
262 if (b1 == -1 || b3 == -1) break;
266 // fail("There shall be difference in the streams. Read bytes " + cnt);