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