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