1.1 --- a/freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java Tue Sep 08 07:54:12 2009 +0200
1.2 +++ b/freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java Tue Sep 08 15:29:15 2009 +0200
1.3 @@ -27,15 +27,19 @@
1.4 package cz.xelfi.quoridor.freemarkerdor;
1.5
1.6 import com.sun.jersey.api.client.Client;
1.7 +import com.sun.jersey.api.client.ClientResponse;
1.8 import com.sun.jersey.api.client.WebResource;
1.9 import com.sun.jersey.core.header.MediaTypes;
1.10 +import com.sun.jersey.core.util.MultivaluedMapImpl;
1.11 import java.io.File;
1.12 +import java.io.FileOutputStream;
1.13 import java.io.IOException;
1.14 import java.net.URI;
1.15 import java.util.Locale;
1.16 import java.util.concurrent.Callable;
1.17 import javax.ws.rs.core.Cookie;
1.18 -import org.junit.After;
1.19 +import javax.ws.rs.core.MultivaluedMap;
1.20 +import org.junit.AfterClass;
1.21 import org.junit.Before;
1.22 import org.junit.BeforeClass;
1.23 import org.junit.Test;
1.24 @@ -46,31 +50,37 @@
1.25 * @author Jaroslav Tulach <jtulach@netbeans.org>
1.26 */
1.27 public class UITest extends Object {
1.28 - private File dir;
1.29 - private Callable<Void> stop;
1.30 + private static File dir;
1.31 + private static Callable<Void> stop;
1.32 private WebResource webResource;
1.33
1.34 public UITest() throws Exception {
1.35 }
1.36
1.37 @BeforeClass
1.38 - public static void localeEnglish() {
1.39 + public static void localeEnglish() throws Exception {
1.40 Locale.setDefault(Locale.ENGLISH);
1.41 + dir = File.createTempFile("quoridor", ".dir");
1.42 + dir.delete();
1.43 + dir.mkdirs();
1.44 + System.setProperty("quoridor.dir", dir.getPath());
1.45 + stop = UI.startServers(9991);
1.46 +
1.47 + File passwd = new File(dir, "passwd");
1.48 + FileOutputStream os = new FileOutputStream(passwd);
1.49 + os.write("test=pes\n".getBytes("UTF-8"));
1.50 + os.close();
1.51 }
1.52
1.53 @Before
1.54 public void setUp() throws Exception {
1.55 - dir = File.createTempFile("quoridor", ".dir");
1.56 - dir.delete();
1.57 - System.setProperty("quoridor.dir", dir.getPath());
1.58 - stop = UI.startServers(9991);
1.59
1.60 Client client = new Client();
1.61 webResource = client.resource(new URI("http://localhost:9991/"));
1.62 }
1.63
1.64 - @After
1.65 - public void tearDown() throws Exception {
1.66 + @AfterClass
1.67 + public static void cleanUpAll() throws Exception {
1.68 deleteRec(dir);
1.69 if (stop != null) {
1.70 stop.call();
1.71 @@ -120,4 +130,52 @@
1.72 }
1.73 }
1.74
1.75 + @Test public void testLogin() throws Exception {
1.76 + MultivaluedMap formData = new MultivaluedMapImpl();
1.77 + formData.add("name", "test");
1.78 + formData.add("password", "pes");
1.79 + ClientResponse response = webResource.path("login").
1.80 + accept("text/html").type("application/x-www-form-urlencoded").
1.81 + post(ClientResponse.class, formData);
1.82 + final String res = response.getEntity(String.class);
1.83 + assertEquals("OK", ClientResponse.Status.OK, response.getClientResponseStatus());
1.84 + if (res.indexOf("You are logged as test") == -1) {
1.85 + fail("res: " + res);
1.86 + }
1.87 + }
1.88 +
1.89 +
1.90 + @Test public void testCreateGameWrongUsers() throws Exception {
1.91 + ClientResponse res = webResource.path("games/create").
1.92 + queryParam("white", "unknown1").
1.93 + queryParam("black", "unknown2").
1.94 + cookie(Cookie.valueOf("login=test")).
1.95 + accept("text/html").
1.96 + get(ClientResponse.class);
1.97 + final String txt = res.getEntity(String.class);
1.98 + assertEquals("Rejected, unknown user\n" + txt, ClientResponse.Status.NOT_FOUND, res.getClientResponseStatus());
1.99 + if (txt.indexOf("You (test) must be") == -1) {
1.100 + fail(txt);
1.101 + }
1.102 + }
1.103 +
1.104 + @Test public void testCreateGameOK() throws Exception {
1.105 + ClientResponse res = webResource.path("games/create").
1.106 + queryParam("white", "unknown1").
1.107 + queryParam("black", "test").
1.108 + cookie(Cookie.valueOf("login=test")).
1.109 + accept("text/html").
1.110 + get(ClientResponse.class);
1.111 +
1.112 + final String txt = res.getEntity(String.class);
1.113 + assertEquals("OK\n" + txt, ClientResponse.Status.OK, res.getClientResponseStatus());
1.114 +
1.115 + String[] games = new File(dir, "games").list();
1.116 + assertEquals("One game exists", 1, games.length);
1.117 +
1.118 + if (txt.indexOf(games[0]) == -1) {
1.119 + fail(games[0] + " expected inside of:\n" + txt);
1.120 + }
1.121 + }
1.122 +
1.123 }