freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 14 Sep 2010 08:56:13 +0200
changeset 264 d60370059c3c
parent 234 0a71b6bd786f
child 272 215b417aac98
permissions -rw-r--r--
Changing headers to GPLv3
     1 /*
     2  * Quoridor server and related libraries
     3  * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://www.gnu.org/licenses/.
    17  */
    18 package cz.xelfi.quoridor.freemarkerdor;
    19 
    20 import com.sun.jersey.api.client.Client;
    21 import com.sun.jersey.api.client.ClientResponse;
    22 import com.sun.jersey.api.client.WebResource;
    23 import com.sun.jersey.core.header.MediaTypes;
    24 import com.sun.jersey.core.util.MultivaluedMapImpl;
    25 import com.sun.net.httpserver.HttpServer;
    26 import cz.xelfi.quoridor.webidor.resources.Quoridor;
    27 import cz.xelfi.quoridor.statistics.resources.Statistics;
    28 import java.awt.Image;
    29 import java.io.File;
    30 import java.io.FileOutputStream;
    31 import java.io.IOException;
    32 import java.io.InputStream;
    33 import java.net.URI;
    34 import java.util.Locale;
    35 import java.util.concurrent.Callable;
    36 import java.util.regex.Matcher;
    37 import java.util.regex.Pattern;
    38 import javax.ws.rs.core.Cookie;
    39 import javax.ws.rs.core.MediaType;
    40 import javax.ws.rs.core.MultivaluedMap;
    41 import org.junit.AfterClass;
    42 import org.junit.Before;
    43 import org.junit.BeforeClass;
    44 import org.junit.Test;
    45 import static org.junit.Assert.*;
    46 
    47 /**
    48  *
    49  * @author Jaroslav Tulach <jtulach@netbeans.org>
    50  */
    51 public class UITest extends Object {
    52     private static File dir;
    53     private static HttpServer stopAPI;
    54     private static HttpServer stopStatistics;
    55     private static Callable<Void> stop;
    56     private WebResource webResource;
    57     private WebResource apiResource;
    58     private WebResource statResource;
    59 
    60     public UITest() throws Exception {
    61     }
    62 
    63     @BeforeClass
    64     public static void localeEnglish() throws Exception {
    65         Locale.setDefault(Locale.ENGLISH);
    66         dir = File.createTempFile("quoridor", ".dir");
    67         dir.delete();
    68         dir.mkdirs();
    69         System.setProperty("quoridor.dir", dir.getPath());
    70         stopAPI = Quoridor.start(9990);
    71         stopStatistics = Quoridor.start(9992);
    72         stop = UI.startServers(9991, "http://localhost:9990", "http://localhost:9992", null);
    73 
    74         File passwd = new File(dir, "passwd");
    75         FileOutputStream os = new FileOutputStream(passwd);
    76         os.write("test=pes\nJarda=darda\n".getBytes("UTF-8"));
    77         os.close();
    78     }
    79 
    80     @Before
    81     public void setUp() throws Exception {
    82 
    83         Client client = new Client();
    84         webResource = client.resource(new URI("http://localhost:9991/"));
    85         apiResource = client.resource(new URI("http://localhost:9990/"));
    86         statResource = client.resource(new URI("http://localhost:9992/"));
    87     }
    88 
    89     @AfterClass
    90     public static void cleanUpAll() throws Exception {
    91         deleteRec(dir);
    92         if (stop != null) {
    93             stop.call();
    94         }
    95         if (stopAPI != null) {
    96             stopAPI.stop(0);
    97         }
    98     }
    99 
   100     static void deleteRec(File dir) throws IOException {
   101         if (dir == null) {
   102             return;
   103         }
   104         File[] arr = dir.listFiles();
   105         if (arr != null) {
   106             for (File f : arr) {
   107                 deleteRec(f);
   108             }
   109         }
   110         dir.delete();
   111     }
   112 
   113     @Test public void testApplicationWadl() {
   114         String serviceWadl = webResource.path("application.wadl").
   115                 accept(MediaTypes.WADL).get(String.class);
   116         assertTrue(serviceWadl.length() > 0);
   117     }
   118 
   119     @Test public void testAPIWadl() {
   120         String serviceWadl = apiResource.path("application.wadl").
   121                 accept(MediaTypes.WADL).get(String.class);
   122         assertTrue(serviceWadl.length() > 0);
   123     }
   124 
   125     @Test public void testStatWadl() {
   126         String serviceWadl = statResource.path("application.wadl").
   127                 accept(MediaTypes.WADL).get(String.class);
   128         assertTrue(serviceWadl.length() > 0);
   129     }
   130 
   131     @Test public void testGetIndexPage() throws Exception {
   132         String logJarda = apiResource.path("login").
   133             queryParam("name", "Jarda").
   134             queryParam("password", "darda").
   135             accept(MediaType.TEXT_PLAIN).
   136             put(String.class);
   137         assertNotNull("Logged in OK", logJarda);
   138 
   139         String res = webResource.accept("text/html").get(String.class);
   140         if (res.indexOf("Quoridor") == -1) {
   141             fail("Wrong index.html:\n" + res);
   142         }
   143         if (res.indexOf("Login") == -1) {
   144             fail("Wrong index.html:\n" + res);
   145         }
   146         if (res.indexOf("action=\"/login\"") == -1) {
   147             fail("Wrong index.html:\n" + res);
   148         }
   149         if (res.toLowerCase().indexOf("error") != -1) {
   150             fail("There was an error:\n" + res);
   151         }
   152         res = webResource.cookie(Cookie.valueOf("login=" + logJarda)).accept("text/html").get(String.class);
   153         if (res.indexOf("action=\"/games/create\"") == -1) {
   154             fail(res);
   155         }
   156     }
   157 
   158     @Test public void testLogin() throws Exception {
   159         MultivaluedMap formData = new MultivaluedMapImpl();
   160         formData.add("name", "test");
   161         formData.add("password", "pes");
   162         ClientResponse response = webResource.path("login").
   163             accept("text/html").type("application/x-www-form-urlencoded").
   164             post(ClientResponse.class, formData);
   165         final String res = response.getEntity(String.class);
   166         assertEquals("OK", ClientResponse.Status.OK, response.getClientResponseStatus());
   167         if (res.indexOf("You are logged in as test") == -1) {
   168             fail("res: " + res);
   169         }
   170     }
   171 
   172 
   173     @Test public void testCreateGameWrongUsers() throws Exception {
   174         String logTest = apiResource.path("login").
   175             queryParam("name", "test").
   176             queryParam("password", "pes").
   177             accept(MediaType.TEXT_PLAIN).
   178             put(String.class);
   179         ClientResponse res = webResource.path("games/create").
   180             queryParam("white", "unknown1").
   181             queryParam("black", "unknown2").
   182             cookie(Cookie.valueOf("login=" + logTest)).
   183             accept("text/html").
   184             get(ClientResponse.class);
   185         final String txt = res.getEntity(String.class);
   186         assertEquals("Rejected, unknown user\n" + txt, ClientResponse.Status.NOT_FOUND, res.getClientResponseStatus());
   187         if (txt.indexOf("You (test) must be") == -1) {
   188             fail(txt);
   189         }
   190     }
   191 
   192     @Test public void testCreateGameOK() throws Exception {
   193         String logTest = apiResource.path("login").
   194             queryParam("name", "test").
   195             queryParam("password", "pes").
   196             accept(MediaType.TEXT_PLAIN).
   197             put(String.class);
   198         String logJarda = apiResource.path("login").
   199             queryParam("name", "Jarda").
   200             queryParam("password", "darda").
   201             accept(MediaType.TEXT_PLAIN).
   202             put(String.class);
   203         ClientResponse res = webResource.path("games/create").
   204             queryParam("white", "Jarda").
   205             queryParam("black", "test").
   206             cookie(Cookie.valueOf("login=" + logTest)).
   207             accept("text/html").
   208             get(ClientResponse.class);
   209 
   210         final String txt = res.getEntity(String.class);
   211         assertEquals("OK\n" + txt, ClientResponse.Status.OK, res.getClientResponseStatus());
   212 
   213         String[] games = new File(dir, "games").list();
   214         assertEquals("One game exists", 1, games.length);
   215 
   216         if (txt.indexOf(games[0]) == -1) {
   217             fail(games[0] + " expected inside of:\n" + txt);
   218         }
   219 
   220         ClientResponse page = webResource.path("games/" + games[0]).
   221             cookie(Cookie.valueOf("login=" + logJarda)).
   222             get(ClientResponse.class);
   223         String ptxt = page.getEntity(String.class);
   224         assertEquals("OK:\n" + ptxt, ClientResponse.Status.OK, page.getClientResponseStatus());
   225 
   226         Pattern p = Pattern.compile(".*<img[^>]*src=\"([^\"]*)\"");
   227         Matcher m = p.matcher(ptxt);
   228         assertTrue("image found\n" + ptxt, m.find());
   229 
   230         InputStream img1 = webResource.path(m.group(1)).get(InputStream.class);
   231         assertNotNull("image found", img1);
   232 
   233         ClientResponse move = apiResource.path("games/" + games[0]).
   234             queryParam("loginID", logJarda).
   235             queryParam("player", "Jarda").queryParam("move", "N").put(ClientResponse.class);
   236         assertEquals("Move OK:\n" + move.getEntity(String.class), ClientResponse.Status.OK, move.getClientResponseStatus());
   237 
   238         InputStream img2 = webResource.path(m.group(1)).get(InputStream.class);
   239         assertNotNull("image found", img2);
   240 
   241         ClientResponse page2 = webResource.path("games/" + games[0]).
   242             cookie(Cookie.valueOf("login=" + logJarda)).
   243             get(ClientResponse.class);
   244         String ptxt2 = page2.getEntity(String.class);
   245         assertEquals("OK:\n" + ptxt2, ClientResponse.Status.OK, page2.getClientResponseStatus());
   246 
   247         Matcher m2 = p.matcher(ptxt2);
   248         assertTrue("image found\n" + ptxt2, m2.find());
   249 
   250         InputStream img3 = webResource.path(m2.group(1)).get(InputStream.class);
   251         assertNotNull("image found", img3);
   252 
   253         int diff = 0;
   254         int cnt = 0;
   255         for (;;) {
   256             cnt++;
   257             int b1 = img1.read();
   258             int b2 = img2.read();
   259             int b3 = img3.read();
   260             assertEquals(b1, b2);
   261             if (b3 != b1) {
   262                 diff++;
   263             }
   264             if (b1 == -1 || b3 == -1) break;
   265 
   266         }
   267         if (diff == 0) {
   268 //            fail("There shall be difference in the streams. Read bytes " + cnt);
   269         }
   270     }
   271 
   272 }