freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 09 Mar 2010 19:33:46 +0100
changeset 234 0a71b6bd786f
parent 180 c92831d4812c
child 264 d60370059c3c
permissions -rw-r--r--
Allowing the server to specify real public URL
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@73
    30
import com.sun.jersey.api.client.ClientResponse;
jaroslav@50
    31
import com.sun.jersey.api.client.WebResource;
jaroslav@50
    32
import com.sun.jersey.core.header.MediaTypes;
jaroslav@73
    33
import com.sun.jersey.core.util.MultivaluedMapImpl;
jaroslav@126
    34
import com.sun.net.httpserver.HttpServer;
jaroslav@126
    35
import cz.xelfi.quoridor.webidor.resources.Quoridor;
martin@180
    36
import cz.xelfi.quoridor.statistics.resources.Statistics;
jaroslav@106
    37
import java.awt.Image;
jaroslav@50
    38
import java.io.File;
jaroslav@73
    39
import java.io.FileOutputStream;
jaroslav@50
    40
import java.io.IOException;
jaroslav@106
    41
import java.io.InputStream;
jaroslav@50
    42
import java.net.URI;
jaroslav@62
    43
import java.util.Locale;
jaroslav@50
    44
import java.util.concurrent.Callable;
jaroslav@106
    45
import java.util.regex.Matcher;
jaroslav@106
    46
import java.util.regex.Pattern;
jaroslav@51
    47
import javax.ws.rs.core.Cookie;
jtulach@82
    48
import javax.ws.rs.core.MediaType;
jaroslav@73
    49
import javax.ws.rs.core.MultivaluedMap;
jaroslav@73
    50
import org.junit.AfterClass;
jaroslav@50
    51
import org.junit.Before;
jaroslav@62
    52
import org.junit.BeforeClass;
jaroslav@50
    53
import org.junit.Test;
jaroslav@50
    54
import static org.junit.Assert.*;
jaroslav@50
    55
jaroslav@50
    56
/**
jaroslav@50
    57
 *
jaroslav@50
    58
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@50
    59
 */
jaroslav@50
    60
public class UITest extends Object {
jaroslav@73
    61
    private static File dir;
jaroslav@126
    62
    private static HttpServer stopAPI;
martin@180
    63
    private static HttpServer stopStatistics;
jaroslav@73
    64
    private static Callable<Void> stop;
jaroslav@50
    65
    private WebResource webResource;
jaroslav@121
    66
    private WebResource apiResource;
martin@180
    67
    private WebResource statResource;
jaroslav@50
    68
jaroslav@50
    69
    public UITest() throws Exception {
jaroslav@50
    70
    }
jaroslav@50
    71
jaroslav@62
    72
    @BeforeClass
jaroslav@73
    73
    public static void localeEnglish() throws Exception {
jaroslav@62
    74
        Locale.setDefault(Locale.ENGLISH);
jaroslav@73
    75
        dir = File.createTempFile("quoridor", ".dir");
jaroslav@73
    76
        dir.delete();
jaroslav@73
    77
        dir.mkdirs();
jaroslav@73
    78
        System.setProperty("quoridor.dir", dir.getPath());
jaroslav@126
    79
        stopAPI = Quoridor.start(9990);
martin@180
    80
        stopStatistics = Quoridor.start(9992);
jaroslav@234
    81
        stop = UI.startServers(9991, "http://localhost:9990", "http://localhost:9992", null);
jaroslav@73
    82
jaroslav@73
    83
        File passwd = new File(dir, "passwd");
jaroslav@73
    84
        FileOutputStream os = new FileOutputStream(passwd);
jtulach@82
    85
        os.write("test=pes\nJarda=darda\n".getBytes("UTF-8"));
jaroslav@73
    86
        os.close();
jaroslav@62
    87
    }
jaroslav@62
    88
jaroslav@50
    89
    @Before
jaroslav@50
    90
    public void setUp() throws Exception {
jaroslav@50
    91
jaroslav@50
    92
        Client client = new Client();
jaroslav@68
    93
        webResource = client.resource(new URI("http://localhost:9991/"));
jaroslav@124
    94
        apiResource = client.resource(new URI("http://localhost:9990/"));
martin@180
    95
        statResource = client.resource(new URI("http://localhost:9992/"));
jaroslav@50
    96
    }
jaroslav@50
    97
jaroslav@73
    98
    @AfterClass
jaroslav@73
    99
    public static void cleanUpAll() throws Exception {
jaroslav@50
   100
        deleteRec(dir);
jaroslav@50
   101
        if (stop != null) {
jaroslav@50
   102
            stop.call();
jaroslav@50
   103
        }
jaroslav@126
   104
        if (stopAPI != null) {
jaroslav@126
   105
            stopAPI.stop(0);
jaroslav@126
   106
        }
jaroslav@50
   107
    }
jaroslav@50
   108
jaroslav@50
   109
    static void deleteRec(File dir) throws IOException {
jaroslav@50
   110
        if (dir == null) {
jaroslav@50
   111
            return;
jaroslav@50
   112
        }
jaroslav@50
   113
        File[] arr = dir.listFiles();
jaroslav@50
   114
        if (arr != null) {
jaroslav@50
   115
            for (File f : arr) {
jaroslav@50
   116
                deleteRec(f);
jaroslav@50
   117
            }
jaroslav@50
   118
        }
jaroslav@50
   119
        dir.delete();
jaroslav@50
   120
    }
jaroslav@50
   121
jaroslav@50
   122
    @Test public void testApplicationWadl() {
jaroslav@50
   123
        String serviceWadl = webResource.path("application.wadl").
jaroslav@50
   124
                accept(MediaTypes.WADL).get(String.class);
jaroslav@50
   125
        assertTrue(serviceWadl.length() > 0);
jaroslav@50
   126
    }
jaroslav@50
   127
jaroslav@124
   128
    @Test public void testAPIWadl() {
jaroslav@124
   129
        String serviceWadl = apiResource.path("application.wadl").
jaroslav@124
   130
                accept(MediaTypes.WADL).get(String.class);
jaroslav@124
   131
        assertTrue(serviceWadl.length() > 0);
jaroslav@124
   132
    }
jaroslav@124
   133
martin@180
   134
    @Test public void testStatWadl() {
martin@180
   135
        String serviceWadl = statResource.path("application.wadl").
martin@180
   136
                accept(MediaTypes.WADL).get(String.class);
martin@180
   137
        assertTrue(serviceWadl.length() > 0);
martin@180
   138
    }
martin@180
   139
jaroslav@50
   140
    @Test public void testGetIndexPage() throws Exception {
jaroslav@121
   141
        String logJarda = apiResource.path("login").
jtulach@82
   142
            queryParam("name", "Jarda").
jtulach@82
   143
            queryParam("password", "darda").
jtulach@82
   144
            accept(MediaType.TEXT_PLAIN).
jtulach@82
   145
            put(String.class);
jtulach@82
   146
        assertNotNull("Logged in OK", logJarda);
jtulach@82
   147
jaroslav@50
   148
        String res = webResource.accept("text/html").get(String.class);
jaroslav@50
   149
        if (res.indexOf("Quoridor") == -1) {
jaroslav@50
   150
            fail("Wrong index.html:\n" + res);
jaroslav@50
   151
        }
jaroslav@51
   152
        if (res.indexOf("Login") == -1) {
jaroslav@51
   153
            fail("Wrong index.html:\n" + res);
jaroslav@51
   154
        }
jaroslav@72
   155
        if (res.indexOf("action=\"/login\"") == -1) {
jaroslav@51
   156
            fail("Wrong index.html:\n" + res);
jaroslav@51
   157
        }
jtulach@54
   158
        if (res.toLowerCase().indexOf("error") != -1) {
jtulach@54
   159
            fail("There was an error:\n" + res);
jtulach@54
   160
        }
jtulach@82
   161
        res = webResource.cookie(Cookie.valueOf("login=" + logJarda)).accept("text/html").get(String.class);
jtulach@74
   162
        if (res.indexOf("action=\"/games/create\"") == -1) {
jaroslav@51
   163
            fail(res);
jaroslav@51
   164
        }
jaroslav@50
   165
    }
jaroslav@50
   166
jaroslav@73
   167
    @Test public void testLogin() throws Exception {
jaroslav@73
   168
        MultivaluedMap formData = new MultivaluedMapImpl();
jaroslav@73
   169
        formData.add("name", "test");
jaroslav@73
   170
        formData.add("password", "pes");
jaroslav@73
   171
        ClientResponse response = webResource.path("login").
jaroslav@73
   172
            accept("text/html").type("application/x-www-form-urlencoded").
jaroslav@73
   173
            post(ClientResponse.class, formData);
jaroslav@73
   174
        final String res = response.getEntity(String.class);
jaroslav@73
   175
        assertEquals("OK", ClientResponse.Status.OK, response.getClientResponseStatus());
jtulach@81
   176
        if (res.indexOf("You are logged in as test") == -1) {
jaroslav@73
   177
            fail("res: " + res);
jaroslav@73
   178
        }
jaroslav@73
   179
    }
jaroslav@73
   180
jaroslav@73
   181
jaroslav@73
   182
    @Test public void testCreateGameWrongUsers() throws Exception {
jaroslav@121
   183
        String logTest = apiResource.path("login").
jtulach@82
   184
            queryParam("name", "test").
jtulach@82
   185
            queryParam("password", "pes").
jtulach@82
   186
            accept(MediaType.TEXT_PLAIN).
jtulach@82
   187
            put(String.class);
jaroslav@73
   188
        ClientResponse res = webResource.path("games/create").
jaroslav@73
   189
            queryParam("white", "unknown1").
jaroslav@73
   190
            queryParam("black", "unknown2").
jtulach@82
   191
            cookie(Cookie.valueOf("login=" + logTest)).
jaroslav@73
   192
            accept("text/html").
jaroslav@73
   193
            get(ClientResponse.class);
jaroslav@73
   194
        final String txt = res.getEntity(String.class);
jaroslav@73
   195
        assertEquals("Rejected, unknown user\n" + txt, ClientResponse.Status.NOT_FOUND, res.getClientResponseStatus());
jaroslav@73
   196
        if (txt.indexOf("You (test) must be") == -1) {
jaroslav@73
   197
            fail(txt);
jaroslav@73
   198
        }
jaroslav@73
   199
    }
jaroslav@73
   200
jaroslav@73
   201
    @Test public void testCreateGameOK() throws Exception {
jaroslav@121
   202
        String logTest = apiResource.path("login").
jtulach@82
   203
            queryParam("name", "test").
jtulach@82
   204
            queryParam("password", "pes").
jtulach@82
   205
            accept(MediaType.TEXT_PLAIN).
jtulach@82
   206
            put(String.class);
jaroslav@121
   207
        String logJarda = apiResource.path("login").
jaroslav@106
   208
            queryParam("name", "Jarda").
jaroslav@106
   209
            queryParam("password", "darda").
jaroslav@106
   210
            accept(MediaType.TEXT_PLAIN).
jaroslav@106
   211
            put(String.class);
jaroslav@73
   212
        ClientResponse res = webResource.path("games/create").
jaroslav@106
   213
            queryParam("white", "Jarda").
jaroslav@73
   214
            queryParam("black", "test").
jtulach@82
   215
            cookie(Cookie.valueOf("login=" + logTest)).
jaroslav@73
   216
            accept("text/html").
jaroslav@73
   217
            get(ClientResponse.class);
jaroslav@73
   218
jaroslav@73
   219
        final String txt = res.getEntity(String.class);
jaroslav@73
   220
        assertEquals("OK\n" + txt, ClientResponse.Status.OK, res.getClientResponseStatus());
jaroslav@73
   221
jaroslav@73
   222
        String[] games = new File(dir, "games").list();
jaroslav@73
   223
        assertEquals("One game exists", 1, games.length);
jaroslav@73
   224
jaroslav@73
   225
        if (txt.indexOf(games[0]) == -1) {
jaroslav@73
   226
            fail(games[0] + " expected inside of:\n" + txt);
jaroslav@73
   227
        }
jaroslav@106
   228
jaroslav@106
   229
        ClientResponse page = webResource.path("games/" + games[0]).
jaroslav@106
   230
            cookie(Cookie.valueOf("login=" + logJarda)).
jaroslav@106
   231
            get(ClientResponse.class);
jaroslav@106
   232
        String ptxt = page.getEntity(String.class);
jaroslav@106
   233
        assertEquals("OK:\n" + ptxt, ClientResponse.Status.OK, page.getClientResponseStatus());
jaroslav@106
   234
jaroslav@113
   235
        Pattern p = Pattern.compile(".*<img[^>]*src=\"([^\"]*)\"");
jaroslav@106
   236
        Matcher m = p.matcher(ptxt);
jaroslav@106
   237
        assertTrue("image found\n" + ptxt, m.find());
jaroslav@106
   238
jaroslav@106
   239
        InputStream img1 = webResource.path(m.group(1)).get(InputStream.class);
jaroslav@106
   240
        assertNotNull("image found", img1);
jaroslav@106
   241
jaroslav@121
   242
        ClientResponse move = apiResource.path("games/" + games[0]).
jaroslav@106
   243
            queryParam("loginID", logJarda).
jaroslav@106
   244
            queryParam("player", "Jarda").queryParam("move", "N").put(ClientResponse.class);
jaroslav@106
   245
        assertEquals("Move OK:\n" + move.getEntity(String.class), ClientResponse.Status.OK, move.getClientResponseStatus());
jaroslav@106
   246
jaroslav@106
   247
        InputStream img2 = webResource.path(m.group(1)).get(InputStream.class);
jaroslav@106
   248
        assertNotNull("image found", img2);
jaroslav@106
   249
jaroslav@106
   250
        ClientResponse page2 = webResource.path("games/" + games[0]).
jaroslav@106
   251
            cookie(Cookie.valueOf("login=" + logJarda)).
jaroslav@106
   252
            get(ClientResponse.class);
jaroslav@106
   253
        String ptxt2 = page2.getEntity(String.class);
jaroslav@106
   254
        assertEquals("OK:\n" + ptxt2, ClientResponse.Status.OK, page2.getClientResponseStatus());
jaroslav@106
   255
jaroslav@106
   256
        Matcher m2 = p.matcher(ptxt2);
jaroslav@106
   257
        assertTrue("image found\n" + ptxt2, m2.find());
jaroslav@106
   258
jaroslav@106
   259
        InputStream img3 = webResource.path(m2.group(1)).get(InputStream.class);
jaroslav@106
   260
        assertNotNull("image found", img3);
jaroslav@106
   261
jaroslav@106
   262
        int diff = 0;
jaroslav@106
   263
        int cnt = 0;
jaroslav@106
   264
        for (;;) {
jaroslav@106
   265
            cnt++;
jaroslav@106
   266
            int b1 = img1.read();
jaroslav@106
   267
            int b2 = img2.read();
jaroslav@106
   268
            int b3 = img3.read();
jaroslav@106
   269
            assertEquals(b1, b2);
jaroslav@106
   270
            if (b3 != b1) {
jaroslav@106
   271
                diff++;
jaroslav@106
   272
            }
jaroslav@106
   273
            if (b1 == -1 || b3 == -1) break;
jaroslav@106
   274
jaroslav@106
   275
        }
jaroslav@106
   276
        if (diff == 0) {
jaroslav@106
   277
//            fail("There shall be difference in the streams. Read bytes " + cnt);
jaroslav@106
   278
        }
jaroslav@73
   279
    }
jaroslav@73
   280
jaroslav@50
   281
}