freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 15 Apr 2011 21:18:29 +0200
changeset 282 40fc213a7f43
parent 276 f0a4e8c41419
permissions -rw-r--r--
Using Grizzly
     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 org.glassfish.grizzly.http.server.HttpServer;
    21 import java.util.Collection;
    22 import java.util.Arrays;
    23 import com.sun.jersey.api.client.Client;
    24 import com.sun.jersey.api.client.ClientResponse;
    25 import com.sun.jersey.api.client.WebResource;
    26 import com.sun.jersey.core.header.MediaTypes;
    27 import com.sun.jersey.core.util.MultivaluedMapImpl;
    28 import cz.xelfi.quoridor.webidor.resources.Quoridor;
    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.HashSet;
    35 import java.util.Locale;
    36 import java.util.concurrent.Callable;
    37 import java.util.regex.Matcher;
    38 import java.util.regex.Pattern;
    39 import javax.ws.rs.core.Cookie;
    40 import javax.ws.rs.core.MediaType;
    41 import javax.ws.rs.core.MultivaluedMap;
    42 import org.junit.AfterClass;
    43 import org.junit.Before;
    44 import org.junit.BeforeClass;
    45 import org.junit.Test;
    46 import static org.junit.Assert.*;
    47 
    48 /**
    49  *
    50  * @author Jaroslav Tulach <jtulach@netbeans.org>
    51  */
    52 public class UITest extends Object {
    53     private static File dir;
    54     private static HttpServer stopAPI;
    55     private static HttpServer stopStatistics;
    56     private static Callable<Void> stop;
    57 
    58     private WebResource webResource;
    59     private WebResource apiResource;
    60     private WebResource statResource;
    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         
    72         File games = new File(dir, "games");
    73         copyResource("wrong.encoding", new File(games, "fdcc275c-55ad-4b7e-a776-bde3876b2f2c"));
    74         
    75         
    76         System.setProperty("quoridor.dir", dir.getPath());
    77         stopAPI = Quoridor.start(9990);
    78         stopStatistics = Quoridor.start(9992);
    79         stop = UI.startServers(9991, "http://localhost:9990", "http://localhost:9992", null);
    80 
    81         File passwd = new File(dir, "passwd");
    82         FileOutputStream os = new FileOutputStream(passwd);
    83         os.write("test=pes\nJarda=darda\n".getBytes("UTF-8"));
    84         os.close();
    85     }
    86 
    87     @Before
    88     public void setUp() throws Exception {
    89 
    90         Client client = new Client();
    91         webResource = client.resource(new URI("http://localhost:9991/"));
    92         apiResource = client.resource(new URI("http://localhost:9990/"));
    93         statResource = client.resource(new URI("http://localhost:9992/"));
    94     }
    95 
    96     @AfterClass
    97     public static void cleanUpAll() throws Exception {
    98         deleteRec(dir);
    99         if (stop != null) {
   100             stop.call();
   101         }
   102         if (stopAPI != null) {
   103             stopAPI.stop();
   104         }
   105     }
   106 
   107     static void deleteRec(File dir) throws IOException {
   108         if (dir == null) {
   109             return;
   110         }
   111         File[] arr = dir.listFiles();
   112         if (arr != null) {
   113             for (File f : arr) {
   114                 deleteRec(f);
   115             }
   116         }
   117         dir.delete();
   118     }
   119 
   120     @Test public void testApplicationWadl() {
   121         String serviceWadl = webResource.path("application.wadl").
   122                 accept(MediaTypes.WADL).get(String.class);
   123         assertTrue(serviceWadl.length() > 0);
   124     }
   125 
   126     @Test public void testAPIWadl() {
   127         String serviceWadl = apiResource.path("application.wadl").
   128                 accept(MediaTypes.WADL).get(String.class);
   129         assertTrue(serviceWadl.length() > 0);
   130     }
   131 
   132     @Test public void testStatWadl() {
   133         String serviceWadl = statResource.path("application.wadl").
   134                 accept(MediaTypes.WADL).get(String.class);
   135         assertTrue(serviceWadl.length() > 0);
   136     }
   137 
   138     @Test public void testGetIndexPage() throws Exception {
   139         String logJarda = apiResource.path("login").
   140             queryParam("name", "Jarda").
   141             queryParam("password", "darda").
   142             accept(MediaType.TEXT_PLAIN).
   143             put(String.class);
   144         assertNotNull("Logged in OK", logJarda);
   145 
   146         String res = webResource.accept("text/html").get(String.class);
   147         if (res.indexOf("Quoridor") == -1) {
   148             fail("Wrong index.html:\n" + res);
   149         }
   150         if (res.indexOf("Login") == -1) {
   151             fail("Wrong index.html:\n" + res);
   152         }
   153         if (res.indexOf("action=\"/login\"") == -1) {
   154             fail("Wrong index.html:\n" + res);
   155         }
   156         if (res.toLowerCase().indexOf("error") != -1) {
   157             fail("There was an error:\n" + res);
   158         }
   159         res = webResource.cookie(Cookie.valueOf("login=" + logJarda)).accept("text/html").get(String.class);
   160         if (res.indexOf("action=\"/games/create\"") == -1) {
   161             fail(res);
   162         }
   163     }
   164 
   165     @Test public void testLogin() throws Exception {
   166         MultivaluedMap formData = new MultivaluedMapImpl();
   167         formData.add("name", "test");
   168         formData.add("password", "pes");
   169         ClientResponse response = webResource.path("login").
   170             accept("text/html").type("application/x-www-form-urlencoded").
   171             post(ClientResponse.class, formData);
   172         final String res = response.getEntity(String.class);
   173         assertEquals("OK", ClientResponse.Status.OK, response.getClientResponseStatus());
   174         if (res.indexOf("You are logged in as test") == -1) {
   175             fail("res: " + res);
   176         }
   177     }
   178 
   179 
   180     @Test public void testCreateGameWrongUsers() throws Exception {
   181         String logTest = apiResource.path("login").
   182             queryParam("name", "test").
   183             queryParam("password", "pes").
   184             accept(MediaType.TEXT_PLAIN).
   185             put(String.class);
   186         ClientResponse res = webResource.path("games/create").
   187             queryParam("white", "unknown1").
   188             queryParam("black", "unknown2").
   189             cookie(Cookie.valueOf("login=" + logTest)).
   190             accept("text/html").
   191             get(ClientResponse.class);
   192         final String txt = res.getEntity(String.class);
   193         assertEquals("Rejected, unknown user\n" + txt, ClientResponse.Status.NOT_FOUND, res.getClientResponseStatus());
   194         if (txt.indexOf("You (test) must be") == -1) {
   195             fail(txt);
   196         }
   197     }
   198 
   199     @Test public void testCreateGameOK() throws Exception {
   200         Collection<String> origGamesNames= Arrays.asList(new File(dir, "games").list());
   201         int origGames = origGamesNames.size();
   202         
   203         String logTest = apiResource.path("login").
   204             queryParam("name", "test").
   205             queryParam("password", "pes").
   206             accept(MediaType.TEXT_PLAIN).
   207             put(String.class);
   208         String logJarda = apiResource.path("login").
   209             queryParam("name", "Jarda").
   210             queryParam("password", "darda").
   211             accept(MediaType.TEXT_PLAIN).
   212             put(String.class);
   213         ClientResponse res = webResource.path("games/create").
   214             queryParam("white", "Jarda").
   215             queryParam("black", "test").
   216             cookie(Cookie.valueOf("login=" + logTest)).
   217             accept("text/html").
   218             get(ClientResponse.class);
   219 
   220         final String txt = res.getEntity(String.class);
   221         assertEquals("OK\n" + txt, ClientResponse.Status.OK, res.getClientResponseStatus());
   222 
   223         
   224         String myGame;
   225         {
   226             String[] games = new File(dir, "games").list();
   227             assertEquals("One new game", origGames + 1, games.length);
   228             
   229             HashSet<String> newNames = new HashSet<String>();
   230             newNames.addAll(Arrays.asList(games));
   231             newNames.removeAll(origGamesNames);
   232             assertEquals("Just one name left: " + newNames, 1, newNames.size());
   233             myGame = newNames.iterator().next();
   234         }
   235 
   236         if (txt.indexOf(myGame) == -1) {
   237             fail(myGame + " expected inside of:\n" + txt);
   238         }
   239 
   240         ClientResponse page = webResource.path("games/" + myGame).
   241             cookie(Cookie.valueOf("login=" + logJarda)).
   242             get(ClientResponse.class);
   243         String ptxt = page.getEntity(String.class);
   244         assertEquals("OK:\n" + ptxt, ClientResponse.Status.OK, page.getClientResponseStatus());
   245 
   246         Pattern p = Pattern.compile(".*<img[^>]*src=\"([^\"]*)\"");
   247         Matcher m = p.matcher(ptxt);
   248         assertTrue("image found\n" + ptxt, m.find());
   249 
   250         InputStream img1 = webResource.path(m.group(1)).get(InputStream.class);
   251         assertNotNull("image found", img1);
   252 
   253         ClientResponse move = apiResource.path("games/" + myGame).
   254             queryParam("loginID", logJarda).
   255             queryParam("player", "Jarda").queryParam("move", "N").put(ClientResponse.class);
   256         assertEquals("Move OK:\n" + move.getEntity(String.class), ClientResponse.Status.OK, move.getClientResponseStatus());
   257 
   258         InputStream img2 = webResource.path(m.group(1)).get(InputStream.class);
   259         assertNotNull("image found", img2);
   260 
   261         ClientResponse page2 = webResource.path("games/" + myGame).
   262             cookie(Cookie.valueOf("login=" + logJarda)).
   263             get(ClientResponse.class);
   264         String ptxt2 = page2.getEntity(String.class);
   265         assertEquals("OK:\n" + ptxt2, ClientResponse.Status.OK, page2.getClientResponseStatus());
   266 
   267         Matcher m2 = p.matcher(ptxt2);
   268         assertTrue("image found\n" + ptxt2, m2.find());
   269 
   270         InputStream img3 = webResource.path(m2.group(1)).get(InputStream.class);
   271         assertNotNull("image found", img3);
   272 
   273         int diff = 0;
   274         int cnt = 0;
   275         for (;;) {
   276             cnt++;
   277             int b1 = img1.read();
   278             int b2 = img2.read();
   279             int b3 = img3.read();
   280             assertEquals(b1, b2);
   281             if (b3 != b1) {
   282                 diff++;
   283             }
   284             if (b1 == -1 || b3 == -1) break;
   285 
   286         }
   287         if (diff == 0) {
   288 //            fail("There shall be difference in the streams. Read bytes " + cnt);
   289         }
   290     }
   291     
   292     @Test public void testGetWrongGame() throws Exception {
   293         String logTest = apiResource.path("login").
   294             queryParam("name", "test").
   295             queryParam("password", "pes").
   296             accept(MediaType.TEXT_PLAIN).
   297             put(String.class);
   298         
   299         String xml = apiResource.path("games/fdcc275c-55ad-4b7e-a776-bde3876b2f2c").
   300             queryParam("name", "test").
   301             queryParam("password", "pes").
   302             accept(MediaType.TEXT_XML).
   303             get(String.class);
   304         
   305         if (xml.indexOf((char)0x1b) >= 0) {
   306             fail(xml.replace((char)0x1b, 'X'));
   307         }
   308         
   309         ClientResponse res = webResource.path("games/fdcc275c-55ad-4b7e-a776-bde3876b2f2c").
   310             queryParam("white", "Jarda").
   311             queryParam("black", "test").
   312             cookie(Cookie.valueOf("login=" + logTest)).
   313             accept("text/html").
   314             get(ClientResponse.class);
   315         final String txt = res.getEntity(String.class);
   316         assertEquals("OK\n" + txt, ClientResponse.Status.OK, res.getClientResponseStatus());
   317     }
   318     
   319 
   320     private static void copyResource(String res, File file) throws IOException {
   321         InputStream is = UITest.class.getResourceAsStream(res);
   322         file.getParentFile().mkdirs();
   323         FileOutputStream os = new FileOutputStream(file);
   324         byte[] arr = new byte[4092];
   325         for (;;) {
   326             int len = is.read(arr);
   327             if (len == -1) {
   328                 break;
   329             }
   330             os.write(arr, 0, len);
   331         }
   332         is.close();
   333         os.close();
   334     }
   335     
   336 }