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