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;
35 import java.io.FileOutputStream;
36 import java.io.IOException;
38 import java.util.Locale;
39 import java.util.concurrent.Callable;
40 import javax.ws.rs.core.Cookie;
41 import javax.ws.rs.core.MediaType;
42 import javax.ws.rs.core.MultivaluedMap;
43 import org.junit.AfterClass;
44 import org.junit.Before;
45 import org.junit.BeforeClass;
46 import org.junit.Test;
47 import static org.junit.Assert.*;
51 * @author Jaroslav Tulach <jtulach@netbeans.org>
53 public class UITest extends Object {
54 private static File dir;
55 private static Callable<Void> stop;
56 private WebResource webResource;
58 public UITest() throws Exception {
62 public static void localeEnglish() throws Exception {
63 Locale.setDefault(Locale.ENGLISH);
64 dir = File.createTempFile("quoridor", ".dir");
67 System.setProperty("quoridor.dir", dir.getPath());
68 stop = UI.startServers(9991);
70 File passwd = new File(dir, "passwd");
71 FileOutputStream os = new FileOutputStream(passwd);
72 os.write("test=pes\nJarda=darda\n".getBytes("UTF-8"));
77 public void setUp() throws Exception {
79 Client client = new Client();
80 webResource = client.resource(new URI("http://localhost:9991/"));
84 public static void cleanUpAll() throws Exception {
91 static void deleteRec(File dir) throws IOException {
95 File[] arr = dir.listFiles();
105 * Test if a WADL document is available at the relative path
106 * "application.wadl".
108 @Test public void testApplicationWadl() {
109 String serviceWadl = webResource.path("application.wadl").
110 accept(MediaTypes.WADL).get(String.class);
111 assertTrue(serviceWadl.length() > 0);
114 @Test public void testGetIndexPage() throws Exception {
115 String logJarda = webResource.path("api/login").
116 queryParam("name", "Jarda").
117 queryParam("password", "darda").
118 accept(MediaType.TEXT_PLAIN).
120 assertNotNull("Logged in OK", logJarda);
122 String res = webResource.accept("text/html").get(String.class);
123 if (res.indexOf("Quoridor") == -1) {
124 fail("Wrong index.html:\n" + res);
126 if (res.indexOf("Login") == -1) {
127 fail("Wrong index.html:\n" + res);
129 if (res.indexOf("action=\"/login\"") == -1) {
130 fail("Wrong index.html:\n" + res);
132 if (res.toLowerCase().indexOf("error") != -1) {
133 fail("There was an error:\n" + res);
135 res = webResource.cookie(Cookie.valueOf("login=" + logJarda)).accept("text/html").get(String.class);
136 if (res.indexOf("action=\"/games/create\"") == -1) {
141 @Test public void testLogin() throws Exception {
142 MultivaluedMap formData = new MultivaluedMapImpl();
143 formData.add("name", "test");
144 formData.add("password", "pes");
145 ClientResponse response = webResource.path("login").
146 accept("text/html").type("application/x-www-form-urlencoded").
147 post(ClientResponse.class, formData);
148 final String res = response.getEntity(String.class);
149 assertEquals("OK", ClientResponse.Status.OK, response.getClientResponseStatus());
150 if (res.indexOf("You are logged in as test") == -1) {
156 @Test public void testCreateGameWrongUsers() throws Exception {
157 String logTest = webResource.path("api/login").
158 queryParam("name", "test").
159 queryParam("password", "pes").
160 accept(MediaType.TEXT_PLAIN).
162 ClientResponse res = webResource.path("games/create").
163 queryParam("white", "unknown1").
164 queryParam("black", "unknown2").
165 cookie(Cookie.valueOf("login=" + logTest)).
167 get(ClientResponse.class);
168 final String txt = res.getEntity(String.class);
169 assertEquals("Rejected, unknown user\n" + txt, ClientResponse.Status.NOT_FOUND, res.getClientResponseStatus());
170 if (txt.indexOf("You (test) must be") == -1) {
175 @Test public void testCreateGameOK() throws Exception {
176 String logTest = webResource.path("api/login").
177 queryParam("name", "test").
178 queryParam("password", "pes").
179 accept(MediaType.TEXT_PLAIN).
181 ClientResponse res = webResource.path("games/create").
182 queryParam("white", "unknown1").
183 queryParam("black", "test").
184 cookie(Cookie.valueOf("login=" + logTest)).
186 get(ClientResponse.class);
188 final String txt = res.getEntity(String.class);
189 assertEquals("OK\n" + txt, ClientResponse.Status.OK, res.getClientResponseStatus());
191 String[] games = new File(dir, "games").list();
192 assertEquals("One game exists", 1, games.length);
194 if (txt.indexOf(games[0]) == -1) {
195 fail(games[0] + " expected inside of:\n" + txt);