freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 26 Sep 2009 00:06:58 +0200
changeset 113 25b00d888853
parent 106 7d090f2c5b91
child 121 95dfb04fcee1
permissions -rw-r--r--
No test failures, new version
     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 java.awt.Image;
    35 import java.io.File;
    36 import java.io.FileOutputStream;
    37 import java.io.IOException;
    38 import java.io.InputStream;
    39 import java.net.URI;
    40 import java.util.Locale;
    41 import java.util.concurrent.Callable;
    42 import java.util.regex.Matcher;
    43 import java.util.regex.Pattern;
    44 import javax.ws.rs.core.Cookie;
    45 import javax.ws.rs.core.MediaType;
    46 import javax.ws.rs.core.MultivaluedMap;
    47 import org.junit.AfterClass;
    48 import org.junit.Before;
    49 import org.junit.BeforeClass;
    50 import org.junit.Test;
    51 import static org.junit.Assert.*;
    52 
    53 /**
    54  *
    55  * @author Jaroslav Tulach <jtulach@netbeans.org>
    56  */
    57 public class UITest extends Object {
    58     private static File dir;
    59     private static Callable<Void> stop;
    60     private WebResource webResource;
    61 
    62     public UITest() throws Exception {
    63     }
    64 
    65     @BeforeClass
    66     public static void localeEnglish() throws Exception {
    67         Locale.setDefault(Locale.ENGLISH);
    68         dir = File.createTempFile("quoridor", ".dir");
    69         dir.delete();
    70         dir.mkdirs();
    71         System.setProperty("quoridor.dir", dir.getPath());
    72         stop = UI.startServers(9991);
    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     }
    86 
    87     @AfterClass
    88     public static void cleanUpAll() throws Exception {
    89         deleteRec(dir);
    90         if (stop != null) {
    91             stop.call();
    92         }
    93     }
    94 
    95     static void deleteRec(File dir) throws IOException {
    96         if (dir == null) {
    97             return;
    98         }
    99         File[] arr = dir.listFiles();
   100         if (arr != null) {
   101             for (File f : arr) {
   102                 deleteRec(f);
   103             }
   104         }
   105         dir.delete();
   106     }
   107 
   108     /**
   109      * Test if a WADL document is available at the relative path
   110      * "application.wadl".
   111      */
   112     @Test public void testApplicationWadl() {
   113         String serviceWadl = webResource.path("application.wadl").
   114                 accept(MediaTypes.WADL).get(String.class);
   115         assertTrue(serviceWadl.length() > 0);
   116     }
   117 
   118     @Test public void testGetIndexPage() throws Exception {
   119         String logJarda = webResource.path("api/login").
   120             queryParam("name", "Jarda").
   121             queryParam("password", "darda").
   122             accept(MediaType.TEXT_PLAIN).
   123             put(String.class);
   124         assertNotNull("Logged in OK", logJarda);
   125 
   126         String res = webResource.accept("text/html").get(String.class);
   127         if (res.indexOf("Quoridor") == -1) {
   128             fail("Wrong index.html:\n" + res);
   129         }
   130         if (res.indexOf("Login") == -1) {
   131             fail("Wrong index.html:\n" + res);
   132         }
   133         if (res.indexOf("action=\"/login\"") == -1) {
   134             fail("Wrong index.html:\n" + res);
   135         }
   136         if (res.toLowerCase().indexOf("error") != -1) {
   137             fail("There was an error:\n" + res);
   138         }
   139         res = webResource.cookie(Cookie.valueOf("login=" + logJarda)).accept("text/html").get(String.class);
   140         if (res.indexOf("action=\"/games/create\"") == -1) {
   141             fail(res);
   142         }
   143     }
   144 
   145     @Test public void testLogin() throws Exception {
   146         MultivaluedMap formData = new MultivaluedMapImpl();
   147         formData.add("name", "test");
   148         formData.add("password", "pes");
   149         ClientResponse response = webResource.path("login").
   150             accept("text/html").type("application/x-www-form-urlencoded").
   151             post(ClientResponse.class, formData);
   152         final String res = response.getEntity(String.class);
   153         assertEquals("OK", ClientResponse.Status.OK, response.getClientResponseStatus());
   154         if (res.indexOf("You are logged in as test") == -1) {
   155             fail("res: " + res);
   156         }
   157     }
   158 
   159 
   160     @Test public void testCreateGameWrongUsers() throws Exception {
   161         String logTest = webResource.path("api/login").
   162             queryParam("name", "test").
   163             queryParam("password", "pes").
   164             accept(MediaType.TEXT_PLAIN).
   165             put(String.class);
   166         ClientResponse res = webResource.path("games/create").
   167             queryParam("white", "unknown1").
   168             queryParam("black", "unknown2").
   169             cookie(Cookie.valueOf("login=" + logTest)).
   170             accept("text/html").
   171             get(ClientResponse.class);
   172         final String txt = res.getEntity(String.class);
   173         assertEquals("Rejected, unknown user\n" + txt, ClientResponse.Status.NOT_FOUND, res.getClientResponseStatus());
   174         if (txt.indexOf("You (test) must be") == -1) {
   175             fail(txt);
   176         }
   177     }
   178 
   179     @Test public void testCreateGameOK() throws Exception {
   180         String logTest = webResource.path("api/login").
   181             queryParam("name", "test").
   182             queryParam("password", "pes").
   183             accept(MediaType.TEXT_PLAIN).
   184             put(String.class);
   185         String logJarda = webResource.path("api/login").
   186             queryParam("name", "Jarda").
   187             queryParam("password", "darda").
   188             accept(MediaType.TEXT_PLAIN).
   189             put(String.class);
   190         ClientResponse res = webResource.path("games/create").
   191             queryParam("white", "Jarda").
   192             queryParam("black", "test").
   193             cookie(Cookie.valueOf("login=" + logTest)).
   194             accept("text/html").
   195             get(ClientResponse.class);
   196 
   197         final String txt = res.getEntity(String.class);
   198         assertEquals("OK\n" + txt, ClientResponse.Status.OK, res.getClientResponseStatus());
   199 
   200         String[] games = new File(dir, "games").list();
   201         assertEquals("One game exists", 1, games.length);
   202 
   203         if (txt.indexOf(games[0]) == -1) {
   204             fail(games[0] + " expected inside of:\n" + txt);
   205         }
   206 
   207         ClientResponse page = webResource.path("games/" + games[0]).
   208             cookie(Cookie.valueOf("login=" + logJarda)).
   209             get(ClientResponse.class);
   210         String ptxt = page.getEntity(String.class);
   211         assertEquals("OK:\n" + ptxt, ClientResponse.Status.OK, page.getClientResponseStatus());
   212 
   213         Pattern p = Pattern.compile(".*<img[^>]*src=\"([^\"]*)\"");
   214         Matcher m = p.matcher(ptxt);
   215         assertTrue("image found\n" + ptxt, m.find());
   216 
   217         InputStream img1 = webResource.path(m.group(1)).get(InputStream.class);
   218         assertNotNull("image found", img1);
   219 
   220         ClientResponse move = webResource.path("api/games/" + games[0]).
   221             queryParam("loginID", logJarda).
   222             queryParam("player", "Jarda").queryParam("move", "N").put(ClientResponse.class);
   223         assertEquals("Move OK:\n" + move.getEntity(String.class), ClientResponse.Status.OK, move.getClientResponseStatus());
   224 
   225         InputStream img2 = webResource.path(m.group(1)).get(InputStream.class);
   226         assertNotNull("image found", img2);
   227 
   228         ClientResponse page2 = webResource.path("games/" + games[0]).
   229             cookie(Cookie.valueOf("login=" + logJarda)).
   230             get(ClientResponse.class);
   231         String ptxt2 = page2.getEntity(String.class);
   232         assertEquals("OK:\n" + ptxt2, ClientResponse.Status.OK, page2.getClientResponseStatus());
   233 
   234         Matcher m2 = p.matcher(ptxt2);
   235         assertTrue("image found\n" + ptxt2, m2.find());
   236 
   237         InputStream img3 = webResource.path(m2.group(1)).get(InputStream.class);
   238         assertNotNull("image found", img3);
   239 
   240         int diff = 0;
   241         int cnt = 0;
   242         for (;;) {
   243             cnt++;
   244             int b1 = img1.read();
   245             int b2 = img2.read();
   246             int b3 = img3.read();
   247             assertEquals(b1, b2);
   248             if (b3 != b1) {
   249                 diff++;
   250             }
   251             if (b1 == -1 || b3 == -1) break;
   252 
   253         }
   254         if (diff == 0) {
   255 //            fail("There shall be difference in the streams. Read bytes " + cnt);
   256         }
   257     }
   258 
   259 }