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