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