jaroslav@50
|
1 |
/*
|
jaroslav@50
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
jaroslav@50
|
3 |
*
|
jaroslav@50
|
4 |
* The contents of this file are subject to the terms of either the GNU
|
jaroslav@50
|
5 |
* General Public License Version 2 only ("GPL") or the Common
|
jaroslav@50
|
6 |
* Development and Distribution License("CDDL") (collectively, the
|
jaroslav@50
|
7 |
* "License"). You may not use this file except in compliance with the
|
jaroslav@50
|
8 |
* License. You can obtain a copy of the License at
|
jaroslav@50
|
9 |
* http://www.netbeans.org/cddl-gplv2.html
|
jaroslav@50
|
10 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
|
jaroslav@50
|
11 |
* specific language governing permissions and limitations under the
|
jaroslav@50
|
12 |
* License. When distributing the software, include this License Header
|
jaroslav@50
|
13 |
* Notice in each file and include the License file at
|
jaroslav@50
|
14 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
|
jaroslav@50
|
15 |
* particular file as subject to the "Classpath" exception as provided
|
jaroslav@50
|
16 |
* by Sun in the GPL Version 2 section of the License file that
|
jaroslav@50
|
17 |
* accompanied this code. If applicable, add the following below the
|
jaroslav@50
|
18 |
* License Header, with the fields enclosed by brackets [] replaced by
|
jaroslav@50
|
19 |
* your own identifying information:
|
jaroslav@50
|
20 |
* "Portions Copyrighted [year] [name of copyright owner]"
|
jaroslav@50
|
21 |
*
|
jaroslav@50
|
22 |
* Contributor(s):
|
jaroslav@50
|
23 |
*
|
jaroslav@50
|
24 |
* Portions Copyrighted 2009 Jaroslav Tulach
|
jaroslav@50
|
25 |
*/
|
jaroslav@50
|
26 |
|
jaroslav@50
|
27 |
package cz.xelfi.quoridor.freemarkerdor;
|
jaroslav@50
|
28 |
|
jaroslav@50
|
29 |
import com.sun.jersey.api.client.Client;
|
jaroslav@73
|
30 |
import com.sun.jersey.api.client.ClientResponse;
|
jaroslav@50
|
31 |
import com.sun.jersey.api.client.WebResource;
|
jaroslav@50
|
32 |
import com.sun.jersey.core.header.MediaTypes;
|
jaroslav@73
|
33 |
import com.sun.jersey.core.util.MultivaluedMapImpl;
|
jaroslav@50
|
34 |
import java.io.File;
|
jaroslav@73
|
35 |
import java.io.FileOutputStream;
|
jaroslav@50
|
36 |
import java.io.IOException;
|
jaroslav@50
|
37 |
import java.net.URI;
|
jaroslav@62
|
38 |
import java.util.Locale;
|
jaroslav@50
|
39 |
import java.util.concurrent.Callable;
|
jaroslav@51
|
40 |
import javax.ws.rs.core.Cookie;
|
jaroslav@73
|
41 |
import javax.ws.rs.core.MultivaluedMap;
|
jaroslav@73
|
42 |
import org.junit.AfterClass;
|
jaroslav@50
|
43 |
import org.junit.Before;
|
jaroslav@62
|
44 |
import org.junit.BeforeClass;
|
jaroslav@50
|
45 |
import org.junit.Test;
|
jaroslav@50
|
46 |
import static org.junit.Assert.*;
|
jaroslav@50
|
47 |
|
jaroslav@50
|
48 |
/**
|
jaroslav@50
|
49 |
*
|
jaroslav@50
|
50 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@50
|
51 |
*/
|
jaroslav@50
|
52 |
public class UITest extends Object {
|
jaroslav@73
|
53 |
private static File dir;
|
jaroslav@73
|
54 |
private static Callable<Void> stop;
|
jaroslav@50
|
55 |
private WebResource webResource;
|
jaroslav@50
|
56 |
|
jaroslav@50
|
57 |
public UITest() throws Exception {
|
jaroslav@50
|
58 |
}
|
jaroslav@50
|
59 |
|
jaroslav@62
|
60 |
@BeforeClass
|
jaroslav@73
|
61 |
public static void localeEnglish() throws Exception {
|
jaroslav@62
|
62 |
Locale.setDefault(Locale.ENGLISH);
|
jaroslav@73
|
63 |
dir = File.createTempFile("quoridor", ".dir");
|
jaroslav@73
|
64 |
dir.delete();
|
jaroslav@73
|
65 |
dir.mkdirs();
|
jaroslav@73
|
66 |
System.setProperty("quoridor.dir", dir.getPath());
|
jaroslav@73
|
67 |
stop = UI.startServers(9991);
|
jaroslav@73
|
68 |
|
jaroslav@73
|
69 |
File passwd = new File(dir, "passwd");
|
jaroslav@73
|
70 |
FileOutputStream os = new FileOutputStream(passwd);
|
jaroslav@73
|
71 |
os.write("test=pes\n".getBytes("UTF-8"));
|
jaroslav@73
|
72 |
os.close();
|
jaroslav@62
|
73 |
}
|
jaroslav@62
|
74 |
|
jaroslav@50
|
75 |
@Before
|
jaroslav@50
|
76 |
public void setUp() throws Exception {
|
jaroslav@50
|
77 |
|
jaroslav@50
|
78 |
Client client = new Client();
|
jaroslav@68
|
79 |
webResource = client.resource(new URI("http://localhost:9991/"));
|
jaroslav@50
|
80 |
}
|
jaroslav@50
|
81 |
|
jaroslav@73
|
82 |
@AfterClass
|
jaroslav@73
|
83 |
public static void cleanUpAll() throws Exception {
|
jaroslav@50
|
84 |
deleteRec(dir);
|
jaroslav@50
|
85 |
if (stop != null) {
|
jaroslav@50
|
86 |
stop.call();
|
jaroslav@50
|
87 |
}
|
jaroslav@50
|
88 |
}
|
jaroslav@50
|
89 |
|
jaroslav@50
|
90 |
static void deleteRec(File dir) throws IOException {
|
jaroslav@50
|
91 |
if (dir == null) {
|
jaroslav@50
|
92 |
return;
|
jaroslav@50
|
93 |
}
|
jaroslav@50
|
94 |
File[] arr = dir.listFiles();
|
jaroslav@50
|
95 |
if (arr != null) {
|
jaroslav@50
|
96 |
for (File f : arr) {
|
jaroslav@50
|
97 |
deleteRec(f);
|
jaroslav@50
|
98 |
}
|
jaroslav@50
|
99 |
}
|
jaroslav@50
|
100 |
dir.delete();
|
jaroslav@50
|
101 |
}
|
jaroslav@50
|
102 |
|
jaroslav@50
|
103 |
/**
|
jaroslav@50
|
104 |
* Test if a WADL document is available at the relative path
|
jaroslav@50
|
105 |
* "application.wadl".
|
jaroslav@50
|
106 |
*/
|
jaroslav@50
|
107 |
@Test public void testApplicationWadl() {
|
jaroslav@50
|
108 |
String serviceWadl = webResource.path("application.wadl").
|
jaroslav@50
|
109 |
accept(MediaTypes.WADL).get(String.class);
|
jaroslav@50
|
110 |
assertTrue(serviceWadl.length() > 0);
|
jaroslav@50
|
111 |
}
|
jaroslav@50
|
112 |
|
jaroslav@50
|
113 |
@Test public void testGetIndexPage() throws Exception {
|
jaroslav@50
|
114 |
String res = webResource.accept("text/html").get(String.class);
|
jaroslav@50
|
115 |
if (res.indexOf("Quoridor") == -1) {
|
jaroslav@50
|
116 |
fail("Wrong index.html:\n" + res);
|
jaroslav@50
|
117 |
}
|
jaroslav@51
|
118 |
if (res.indexOf("Login") == -1) {
|
jaroslav@51
|
119 |
fail("Wrong index.html:\n" + res);
|
jaroslav@51
|
120 |
}
|
jaroslav@72
|
121 |
if (res.indexOf("action=\"/login\"") == -1) {
|
jaroslav@51
|
122 |
fail("Wrong index.html:\n" + res);
|
jaroslav@51
|
123 |
}
|
jtulach@54
|
124 |
if (res.toLowerCase().indexOf("error") != -1) {
|
jtulach@54
|
125 |
fail("There was an error:\n" + res);
|
jtulach@54
|
126 |
}
|
jaroslav@51
|
127 |
res = webResource.cookie(Cookie.valueOf("login=jarda")).accept("text/html").get(String.class);
|
jtulach@74
|
128 |
if (res.indexOf("action=\"/games/create\"") == -1) {
|
jaroslav@51
|
129 |
fail(res);
|
jaroslav@51
|
130 |
}
|
jaroslav@50
|
131 |
}
|
jaroslav@50
|
132 |
|
jaroslav@73
|
133 |
@Test public void testLogin() throws Exception {
|
jaroslav@73
|
134 |
MultivaluedMap formData = new MultivaluedMapImpl();
|
jaroslav@73
|
135 |
formData.add("name", "test");
|
jaroslav@73
|
136 |
formData.add("password", "pes");
|
jaroslav@73
|
137 |
ClientResponse response = webResource.path("login").
|
jaroslav@73
|
138 |
accept("text/html").type("application/x-www-form-urlencoded").
|
jaroslav@73
|
139 |
post(ClientResponse.class, formData);
|
jaroslav@73
|
140 |
final String res = response.getEntity(String.class);
|
jaroslav@73
|
141 |
assertEquals("OK", ClientResponse.Status.OK, response.getClientResponseStatus());
|
jtulach@81
|
142 |
if (res.indexOf("You are logged in as test") == -1) {
|
jaroslav@73
|
143 |
fail("res: " + res);
|
jaroslav@73
|
144 |
}
|
jaroslav@73
|
145 |
}
|
jaroslav@73
|
146 |
|
jaroslav@73
|
147 |
|
jaroslav@73
|
148 |
@Test public void testCreateGameWrongUsers() throws Exception {
|
jaroslav@73
|
149 |
ClientResponse res = webResource.path("games/create").
|
jaroslav@73
|
150 |
queryParam("white", "unknown1").
|
jaroslav@73
|
151 |
queryParam("black", "unknown2").
|
jaroslav@73
|
152 |
cookie(Cookie.valueOf("login=test")).
|
jaroslav@73
|
153 |
accept("text/html").
|
jaroslav@73
|
154 |
get(ClientResponse.class);
|
jaroslav@73
|
155 |
final String txt = res.getEntity(String.class);
|
jaroslav@73
|
156 |
assertEquals("Rejected, unknown user\n" + txt, ClientResponse.Status.NOT_FOUND, res.getClientResponseStatus());
|
jaroslav@73
|
157 |
if (txt.indexOf("You (test) must be") == -1) {
|
jaroslav@73
|
158 |
fail(txt);
|
jaroslav@73
|
159 |
}
|
jaroslav@73
|
160 |
}
|
jaroslav@73
|
161 |
|
jaroslav@73
|
162 |
@Test public void testCreateGameOK() throws Exception {
|
jaroslav@73
|
163 |
ClientResponse res = webResource.path("games/create").
|
jaroslav@73
|
164 |
queryParam("white", "unknown1").
|
jaroslav@73
|
165 |
queryParam("black", "test").
|
jaroslav@73
|
166 |
cookie(Cookie.valueOf("login=test")).
|
jaroslav@73
|
167 |
accept("text/html").
|
jaroslav@73
|
168 |
get(ClientResponse.class);
|
jaroslav@73
|
169 |
|
jaroslav@73
|
170 |
final String txt = res.getEntity(String.class);
|
jaroslav@73
|
171 |
assertEquals("OK\n" + txt, ClientResponse.Status.OK, res.getClientResponseStatus());
|
jaroslav@73
|
172 |
|
jaroslav@73
|
173 |
String[] games = new File(dir, "games").list();
|
jaroslav@73
|
174 |
assertEquals("One game exists", 1, games.length);
|
jaroslav@73
|
175 |
|
jaroslav@73
|
176 |
if (txt.indexOf(games[0]) == -1) {
|
jaroslav@73
|
177 |
fail(games[0] + " expected inside of:\n" + txt);
|
jaroslav@73
|
178 |
}
|
jaroslav@73
|
179 |
}
|
jaroslav@73
|
180 |
|
jaroslav@50
|
181 |
}
|