freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 30 Aug 2009 15:19:24 +0200
changeset 51 8358ef0d29d7
parent 50 1cce50d16bb5
child 54 f041b6570ff9
permissions -rw-r--r--
Getting the page after login as well
jaroslav@50
     1
/*
jaroslav@50
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@50
     3
 *
jaroslav@50
     4
 * The contents of this file are subject to the terms of either the GNU
jaroslav@50
     5
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@50
     6
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@50
     7
 * "License"). You may not use this file except in compliance with the
jaroslav@50
     8
 * License. You can obtain a copy of the License at
jaroslav@50
     9
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@50
    10
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@50
    11
 * specific language governing permissions and limitations under the
jaroslav@50
    12
 * License.  When distributing the software, include this License Header
jaroslav@50
    13
 * Notice in each file and include the License file at
jaroslav@50
    14
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
jaroslav@50
    15
 * particular file as subject to the "Classpath" exception as provided
jaroslav@50
    16
 * by Sun in the GPL Version 2 section of the License file that
jaroslav@50
    17
 * accompanied this code. If applicable, add the following below the
jaroslav@50
    18
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@50
    19
 * your own identifying information:
jaroslav@50
    20
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@50
    21
 *
jaroslav@50
    22
 * Contributor(s):
jaroslav@50
    23
 *
jaroslav@50
    24
 * Portions Copyrighted 2009 Jaroslav Tulach
jaroslav@50
    25
 */
jaroslav@50
    26
jaroslav@50
    27
package cz.xelfi.quoridor.freemarkerdor;
jaroslav@50
    28
jaroslav@50
    29
import com.sun.jersey.api.client.Client;
jaroslav@50
    30
import com.sun.jersey.api.client.WebResource;
jaroslav@50
    31
import com.sun.jersey.core.header.MediaTypes;
jaroslav@50
    32
import java.io.File;
jaroslav@50
    33
import java.io.IOException;
jaroslav@50
    34
import java.net.URI;
jaroslav@50
    35
import java.util.concurrent.Callable;
jaroslav@51
    36
import javax.ws.rs.core.Cookie;
jaroslav@50
    37
import org.junit.After;
jaroslav@50
    38
import org.junit.Before;
jaroslav@50
    39
import org.junit.Test;
jaroslav@50
    40
import static org.junit.Assert.*;
jaroslav@50
    41
jaroslav@50
    42
/**
jaroslav@50
    43
 *
jaroslav@50
    44
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@50
    45
 */
jaroslav@50
    46
public class UITest extends Object {
jaroslav@50
    47
    private File dir;
jaroslav@50
    48
    private Callable<Void> stop;
jaroslav@50
    49
    private WebResource webResource;
jaroslav@50
    50
jaroslav@50
    51
    public UITest() throws Exception {
jaroslav@50
    52
    }
jaroslav@50
    53
jaroslav@50
    54
    @Before
jaroslav@50
    55
    public void setUp() throws Exception {
jaroslav@50
    56
        dir = File.createTempFile("quoridor", ".dir");
jaroslav@50
    57
        dir.delete();
jaroslav@50
    58
        System.setProperty("quoridor.dir", dir.getPath());
jaroslav@50
    59
        stop = UI.startServers();
jaroslav@50
    60
jaroslav@50
    61
        Client client = new Client();
jaroslav@50
    62
        webResource = client.resource(new URI("http://localhost:9997/"));
jaroslav@50
    63
    }
jaroslav@50
    64
jaroslav@50
    65
    @After
jaroslav@50
    66
    public void tearDown() throws Exception {
jaroslav@50
    67
        deleteRec(dir);
jaroslav@50
    68
        if (stop != null) {
jaroslav@50
    69
            stop.call();
jaroslav@50
    70
        }
jaroslav@50
    71
    }
jaroslav@50
    72
jaroslav@50
    73
    static void deleteRec(File dir) throws IOException {
jaroslav@50
    74
        if (dir == null) {
jaroslav@50
    75
            return;
jaroslav@50
    76
        }
jaroslav@50
    77
        File[] arr = dir.listFiles();
jaroslav@50
    78
        if (arr != null) {
jaroslav@50
    79
            for (File f : arr) {
jaroslav@50
    80
                deleteRec(f);
jaroslav@50
    81
            }
jaroslav@50
    82
        }
jaroslav@50
    83
        dir.delete();
jaroslav@50
    84
    }
jaroslav@50
    85
jaroslav@50
    86
    /**
jaroslav@50
    87
     * Test if a WADL document is available at the relative path
jaroslav@50
    88
     * "application.wadl".
jaroslav@50
    89
     */
jaroslav@50
    90
    @Test public void testApplicationWadl() {
jaroslav@50
    91
        String serviceWadl = webResource.path("application.wadl").
jaroslav@50
    92
                accept(MediaTypes.WADL).get(String.class);
jaroslav@50
    93
        assertTrue(serviceWadl.length() > 0);
jaroslav@50
    94
    }
jaroslav@50
    95
jaroslav@50
    96
    @Test public void testGetIndexPage() throws Exception {
jaroslav@50
    97
        String res = webResource.accept("text/html").get(String.class);
jaroslav@50
    98
        if (res.indexOf("Quoridor") == -1) {
jaroslav@50
    99
            fail("Wrong index.html:\n" + res);
jaroslav@50
   100
        }
jaroslav@51
   101
        if (res.indexOf("Login") == -1) {
jaroslav@51
   102
            fail("Wrong index.html:\n" + res);
jaroslav@51
   103
        }
jaroslav@51
   104
        if (res.indexOf("action=\"login\"") == -1) {
jaroslav@51
   105
            fail("Wrong index.html:\n" + res);
jaroslav@51
   106
        }
jaroslav@51
   107
        res = webResource.cookie(Cookie.valueOf("login=jarda")).accept("text/html").get(String.class);
jaroslav@51
   108
        if (res.indexOf("action=\"games/create\"") == -1) {
jaroslav@51
   109
            fail(res);
jaroslav@51
   110
        }
jaroslav@50
   111
    }
jaroslav@50
   112
jaroslav@50
   113
}