freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 08 Sep 2009 15:29:15 +0200
changeset 73 b3165f3a9ad7
parent 72 5f081edc8502
child 74 b2af4da1cbbe
permissions -rw-r--r--
More tests to check behaviour of "UI", querying for text/xml in create new game page
     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.io.File;
    35 import java.io.FileOutputStream;
    36 import java.io.IOException;
    37 import java.net.URI;
    38 import java.util.Locale;
    39 import java.util.concurrent.Callable;
    40 import javax.ws.rs.core.Cookie;
    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 Callable<Void> stop;
    55     private WebResource webResource;
    56 
    57     public UITest() throws Exception {
    58     }
    59 
    60     @BeforeClass
    61     public static void localeEnglish() throws Exception {
    62         Locale.setDefault(Locale.ENGLISH);
    63         dir = File.createTempFile("quoridor", ".dir");
    64         dir.delete();
    65         dir.mkdirs();
    66         System.setProperty("quoridor.dir", dir.getPath());
    67         stop = UI.startServers(9991);
    68 
    69         File passwd = new File(dir, "passwd");
    70         FileOutputStream os = new FileOutputStream(passwd);
    71         os.write("test=pes\n".getBytes("UTF-8"));
    72         os.close();
    73     }
    74 
    75     @Before
    76     public void setUp() throws Exception {
    77 
    78         Client client = new Client();
    79         webResource = client.resource(new URI("http://localhost:9991/"));
    80     }
    81 
    82     @AfterClass
    83     public static void cleanUpAll() throws Exception {
    84         deleteRec(dir);
    85         if (stop != null) {
    86             stop.call();
    87         }
    88     }
    89 
    90     static void deleteRec(File dir) throws IOException {
    91         if (dir == null) {
    92             return;
    93         }
    94         File[] arr = dir.listFiles();
    95         if (arr != null) {
    96             for (File f : arr) {
    97                 deleteRec(f);
    98             }
    99         }
   100         dir.delete();
   101     }
   102 
   103     /**
   104      * Test if a WADL document is available at the relative path
   105      * "application.wadl".
   106      */
   107     @Test public void testApplicationWadl() {
   108         String serviceWadl = webResource.path("application.wadl").
   109                 accept(MediaTypes.WADL).get(String.class);
   110         assertTrue(serviceWadl.length() > 0);
   111     }
   112 
   113     @Test public void testGetIndexPage() throws Exception {
   114         String res = webResource.accept("text/html").get(String.class);
   115         if (res.indexOf("Quoridor") == -1) {
   116             fail("Wrong index.html:\n" + res);
   117         }
   118         if (res.indexOf("Login") == -1) {
   119             fail("Wrong index.html:\n" + res);
   120         }
   121         if (res.indexOf("action=\"/login\"") == -1) {
   122             fail("Wrong index.html:\n" + res);
   123         }
   124         if (res.toLowerCase().indexOf("error") != -1) {
   125             fail("There was an error:\n" + res);
   126         }
   127         res = webResource.cookie(Cookie.valueOf("login=jarda")).accept("text/html").get(String.class);
   128         if (res.indexOf("action=\"games/create\"") == -1) {
   129             fail(res);
   130         }
   131     }
   132 
   133     @Test public void testLogin() throws Exception {
   134         MultivaluedMap formData = new MultivaluedMapImpl();
   135         formData.add("name", "test");
   136         formData.add("password", "pes");
   137         ClientResponse response = webResource.path("login").
   138             accept("text/html").type("application/x-www-form-urlencoded").
   139             post(ClientResponse.class, formData);
   140         final String res = response.getEntity(String.class);
   141         assertEquals("OK", ClientResponse.Status.OK, response.getClientResponseStatus());
   142         if (res.indexOf("You are logged as test") == -1) {
   143             fail("res: " + res);
   144         }
   145     }
   146 
   147 
   148     @Test public void testCreateGameWrongUsers() throws Exception {
   149         ClientResponse res = webResource.path("games/create").
   150             queryParam("white", "unknown1").
   151             queryParam("black", "unknown2").
   152             cookie(Cookie.valueOf("login=test")).
   153             accept("text/html").
   154             get(ClientResponse.class);
   155         final String txt = res.getEntity(String.class);
   156         assertEquals("Rejected, unknown user\n" + txt, ClientResponse.Status.NOT_FOUND, res.getClientResponseStatus());
   157         if (txt.indexOf("You (test) must be") == -1) {
   158             fail(txt);
   159         }
   160     }
   161 
   162     @Test public void testCreateGameOK() throws Exception {
   163         ClientResponse res = webResource.path("games/create").
   164             queryParam("white", "unknown1").
   165             queryParam("black", "test").
   166             cookie(Cookie.valueOf("login=test")).
   167             accept("text/html").
   168             get(ClientResponse.class);
   169 
   170         final String txt = res.getEntity(String.class);
   171         assertEquals("OK\n" + txt, ClientResponse.Status.OK, res.getClientResponseStatus());
   172 
   173         String[] games = new File(dir, "games").list();
   174         assertEquals("One game exists", 1, games.length);
   175 
   176         if (txt.indexOf(games[0]) == -1) {
   177             fail(games[0] + " expected inside of:\n" + txt);
   178         }
   179     }
   180 
   181 }