freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 07 Sep 2009 17:43:43 +0200
changeset 68 33cf89760fab
parent 62 5d36990642b7
child 72 5f081edc8502
permissions -rw-r--r--
Pluggable argument with reference to port
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@62
    35
import java.util.Locale;
jaroslav@50
    36
import java.util.concurrent.Callable;
jaroslav@51
    37
import javax.ws.rs.core.Cookie;
jaroslav@50
    38
import org.junit.After;
jaroslav@50
    39
import org.junit.Before;
jaroslav@62
    40
import org.junit.BeforeClass;
jaroslav@50
    41
import org.junit.Test;
jaroslav@50
    42
import static org.junit.Assert.*;
jaroslav@50
    43
jaroslav@50
    44
/**
jaroslav@50
    45
 *
jaroslav@50
    46
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@50
    47
 */
jaroslav@50
    48
public class UITest extends Object {
jaroslav@50
    49
    private File dir;
jaroslav@50
    50
    private Callable<Void> stop;
jaroslav@50
    51
    private WebResource webResource;
jaroslav@50
    52
jaroslav@50
    53
    public UITest() throws Exception {
jaroslav@50
    54
    }
jaroslav@50
    55
jaroslav@62
    56
    @BeforeClass
jaroslav@62
    57
    public static void localeEnglish() {
jaroslav@62
    58
        Locale.setDefault(Locale.ENGLISH);
jaroslav@62
    59
    }
jaroslav@62
    60
jaroslav@50
    61
    @Before
jaroslav@50
    62
    public void setUp() throws Exception {
jaroslav@50
    63
        dir = File.createTempFile("quoridor", ".dir");
jaroslav@50
    64
        dir.delete();
jaroslav@50
    65
        System.setProperty("quoridor.dir", dir.getPath());
jaroslav@68
    66
        stop = UI.startServers(9991);
jaroslav@50
    67
jaroslav@50
    68
        Client client = new Client();
jaroslav@68
    69
        webResource = client.resource(new URI("http://localhost:9991/"));
jaroslav@50
    70
    }
jaroslav@50
    71
jaroslav@50
    72
    @After
jaroslav@50
    73
    public void tearDown() throws Exception {
jaroslav@50
    74
        deleteRec(dir);
jaroslav@50
    75
        if (stop != null) {
jaroslav@50
    76
            stop.call();
jaroslav@50
    77
        }
jaroslav@50
    78
    }
jaroslav@50
    79
jaroslav@50
    80
    static void deleteRec(File dir) throws IOException {
jaroslav@50
    81
        if (dir == null) {
jaroslav@50
    82
            return;
jaroslav@50
    83
        }
jaroslav@50
    84
        File[] arr = dir.listFiles();
jaroslav@50
    85
        if (arr != null) {
jaroslav@50
    86
            for (File f : arr) {
jaroslav@50
    87
                deleteRec(f);
jaroslav@50
    88
            }
jaroslav@50
    89
        }
jaroslav@50
    90
        dir.delete();
jaroslav@50
    91
    }
jaroslav@50
    92
jaroslav@50
    93
    /**
jaroslav@50
    94
     * Test if a WADL document is available at the relative path
jaroslav@50
    95
     * "application.wadl".
jaroslav@50
    96
     */
jaroslav@50
    97
    @Test public void testApplicationWadl() {
jaroslav@50
    98
        String serviceWadl = webResource.path("application.wadl").
jaroslav@50
    99
                accept(MediaTypes.WADL).get(String.class);
jaroslav@50
   100
        assertTrue(serviceWadl.length() > 0);
jaroslav@50
   101
    }
jaroslav@50
   102
jaroslav@50
   103
    @Test public void testGetIndexPage() throws Exception {
jaroslav@50
   104
        String res = webResource.accept("text/html").get(String.class);
jaroslav@50
   105
        if (res.indexOf("Quoridor") == -1) {
jaroslav@50
   106
            fail("Wrong index.html:\n" + res);
jaroslav@50
   107
        }
jaroslav@51
   108
        if (res.indexOf("Login") == -1) {
jaroslav@51
   109
            fail("Wrong index.html:\n" + res);
jaroslav@51
   110
        }
jaroslav@51
   111
        if (res.indexOf("action=\"login\"") == -1) {
jaroslav@51
   112
            fail("Wrong index.html:\n" + res);
jaroslav@51
   113
        }
jtulach@54
   114
        if (res.toLowerCase().indexOf("error") != -1) {
jtulach@54
   115
            fail("There was an error:\n" + res);
jtulach@54
   116
        }
jaroslav@51
   117
        res = webResource.cookie(Cookie.valueOf("login=jarda")).accept("text/html").get(String.class);
jaroslav@51
   118
        if (res.indexOf("action=\"games/create\"") == -1) {
jaroslav@51
   119
            fail(res);
jaroslav@51
   120
        }
jaroslav@50
   121
    }
jaroslav@50
   122
jaroslav@50
   123
}