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