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