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.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 Callable<Void> stop;
55 private WebResource webResource;
57 public UITest() throws Exception {
61 public static void localeEnglish() throws Exception {
62 Locale.setDefault(Locale.ENGLISH);
63 dir = File.createTempFile("quoridor", ".dir");
66 System.setProperty("quoridor.dir", dir.getPath());
67 stop = UI.startServers(9991);
69 File passwd = new File(dir, "passwd");
70 FileOutputStream os = new FileOutputStream(passwd);
71 os.write("test=pes\n".getBytes("UTF-8"));
76 public void setUp() throws Exception {
78 Client client = new Client();
79 webResource = client.resource(new URI("http://localhost:9991/"));
83 public static void cleanUpAll() throws Exception {
90 static void deleteRec(File dir) throws IOException {
94 File[] arr = dir.listFiles();
104 * Test if a WADL document is available at the relative path
105 * "application.wadl".
107 @Test public void testApplicationWadl() {
108 String serviceWadl = webResource.path("application.wadl").
109 accept(MediaTypes.WADL).get(String.class);
110 assertTrue(serviceWadl.length() > 0);
113 @Test public void testGetIndexPage() throws Exception {
114 String res = webResource.accept("text/html").get(String.class);
115 if (res.indexOf("Quoridor") == -1) {
116 fail("Wrong index.html:\n" + res);
118 if (res.indexOf("Login") == -1) {
119 fail("Wrong index.html:\n" + res);
121 if (res.indexOf("action=\"/login\"") == -1) {
122 fail("Wrong index.html:\n" + res);
124 if (res.toLowerCase().indexOf("error") != -1) {
125 fail("There was an error:\n" + res);
127 res = webResource.cookie(Cookie.valueOf("login=jarda")).accept("text/html").get(String.class);
128 if (res.indexOf("action=\"/games/create\"") == -1) {
133 @Test public void testLogin() throws Exception {
134 MultivaluedMap formData = new MultivaluedMapImpl();
135 formData.add("name", "test");
136 formData.add("password", "pes");
137 ClientResponse response = webResource.path("login").
138 accept("text/html").type("application/x-www-form-urlencoded").
139 post(ClientResponse.class, formData);
140 final String res = response.getEntity(String.class);
141 assertEquals("OK", ClientResponse.Status.OK, response.getClientResponseStatus());
142 if (res.indexOf("You are logged in as test") == -1) {
148 @Test public void testCreateGameWrongUsers() throws Exception {
149 ClientResponse res = webResource.path("games/create").
150 queryParam("white", "unknown1").
151 queryParam("black", "unknown2").
152 cookie(Cookie.valueOf("login=test")).
154 get(ClientResponse.class);
155 final String txt = res.getEntity(String.class);
156 assertEquals("Rejected, unknown user\n" + txt, ClientResponse.Status.NOT_FOUND, res.getClientResponseStatus());
157 if (txt.indexOf("You (test) must be") == -1) {
162 @Test public void testCreateGameOK() throws Exception {
163 ClientResponse res = webResource.path("games/create").
164 queryParam("white", "unknown1").
165 queryParam("black", "test").
166 cookie(Cookie.valueOf("login=test")).
168 get(ClientResponse.class);
170 final String txt = res.getEntity(String.class);
171 assertEquals("OK\n" + txt, ClientResponse.Status.OK, res.getClientResponseStatus());
173 String[] games = new File(dir, "games").list();
174 assertEquals("One game exists", 1, games.length);
176 if (txt.indexOf(games[0]) == -1) {
177 fail(games[0] + " expected inside of:\n" + txt);