freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 15 Apr 2011 21:18:29 +0200
changeset 282 40fc213a7f43
parent 276 f0a4e8c41419
permissions -rw-r--r--
Using Grizzly
jaroslav@50
     1
/*
jaroslav@264
     2
 * Quoridor server and related libraries
jaroslav@264
     3
 * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@50
     4
 *
jaroslav@264
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@264
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@264
     7
 * the Free Software Foundation, either version 3 of the License.
jaroslav@50
     8
 *
jaroslav@264
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@264
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@264
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@264
    12
 * GNU General Public License for more details.
jaroslav@50
    13
 *
jaroslav@264
    14
 * You should have received a copy of the GNU General Public License
jaroslav@264
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@264
    16
 * If not, see http://www.gnu.org/licenses/.
jaroslav@50
    17
 */
jaroslav@50
    18
package cz.xelfi.quoridor.freemarkerdor;
jaroslav@50
    19
jaroslav@282
    20
import org.glassfish.grizzly.http.server.HttpServer;
jaroslav@276
    21
import java.util.Collection;
jaroslav@276
    22
import java.util.Arrays;
jaroslav@50
    23
import com.sun.jersey.api.client.Client;
jaroslav@73
    24
import com.sun.jersey.api.client.ClientResponse;
jaroslav@50
    25
import com.sun.jersey.api.client.WebResource;
jaroslav@50
    26
import com.sun.jersey.core.header.MediaTypes;
jaroslav@73
    27
import com.sun.jersey.core.util.MultivaluedMapImpl;
jaroslav@126
    28
import cz.xelfi.quoridor.webidor.resources.Quoridor;
jaroslav@50
    29
import java.io.File;
jaroslav@73
    30
import java.io.FileOutputStream;
jaroslav@50
    31
import java.io.IOException;
jaroslav@106
    32
import java.io.InputStream;
jaroslav@50
    33
import java.net.URI;
jaroslav@276
    34
import java.util.HashSet;
jaroslav@62
    35
import java.util.Locale;
jaroslav@50
    36
import java.util.concurrent.Callable;
jaroslav@106
    37
import java.util.regex.Matcher;
jaroslav@106
    38
import java.util.regex.Pattern;
jaroslav@51
    39
import javax.ws.rs.core.Cookie;
jtulach@82
    40
import javax.ws.rs.core.MediaType;
jaroslav@73
    41
import javax.ws.rs.core.MultivaluedMap;
jaroslav@73
    42
import org.junit.AfterClass;
jaroslav@50
    43
import org.junit.Before;
jaroslav@62
    44
import org.junit.BeforeClass;
jaroslav@50
    45
import org.junit.Test;
jaroslav@50
    46
import static org.junit.Assert.*;
jaroslav@50
    47
jaroslav@50
    48
/**
jaroslav@50
    49
 *
jaroslav@50
    50
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@50
    51
 */
jaroslav@50
    52
public class UITest extends Object {
jaroslav@73
    53
    private static File dir;
jaroslav@126
    54
    private static HttpServer stopAPI;
martin@180
    55
    private static HttpServer stopStatistics;
jaroslav@73
    56
    private static Callable<Void> stop;
jaroslav@272
    57
jaroslav@50
    58
    private WebResource webResource;
jaroslav@121
    59
    private WebResource apiResource;
martin@180
    60
    private WebResource statResource;
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@272
    71
        
jaroslav@272
    72
        File games = new File(dir, "games");
jaroslav@272
    73
        copyResource("wrong.encoding", new File(games, "fdcc275c-55ad-4b7e-a776-bde3876b2f2c"));
jaroslav@272
    74
        
jaroslav@272
    75
        
jaroslav@73
    76
        System.setProperty("quoridor.dir", dir.getPath());
jaroslav@126
    77
        stopAPI = Quoridor.start(9990);
martin@180
    78
        stopStatistics = Quoridor.start(9992);
jaroslav@234
    79
        stop = UI.startServers(9991, "http://localhost:9990", "http://localhost:9992", null);
jaroslav@73
    80
jaroslav@73
    81
        File passwd = new File(dir, "passwd");
jaroslav@73
    82
        FileOutputStream os = new FileOutputStream(passwd);
jtulach@82
    83
        os.write("test=pes\nJarda=darda\n".getBytes("UTF-8"));
jaroslav@73
    84
        os.close();
jaroslav@62
    85
    }
jaroslav@62
    86
jaroslav@50
    87
    @Before
jaroslav@50
    88
    public void setUp() throws Exception {
jaroslav@50
    89
jaroslav@50
    90
        Client client = new Client();
jaroslav@68
    91
        webResource = client.resource(new URI("http://localhost:9991/"));
jaroslav@124
    92
        apiResource = client.resource(new URI("http://localhost:9990/"));
martin@180
    93
        statResource = client.resource(new URI("http://localhost:9992/"));
jaroslav@50
    94
    }
jaroslav@50
    95
jaroslav@73
    96
    @AfterClass
jaroslav@73
    97
    public static void cleanUpAll() throws Exception {
jaroslav@50
    98
        deleteRec(dir);
jaroslav@50
    99
        if (stop != null) {
jaroslav@50
   100
            stop.call();
jaroslav@50
   101
        }
jaroslav@126
   102
        if (stopAPI != null) {
jaroslav@282
   103
            stopAPI.stop();
jaroslav@126
   104
        }
jaroslav@50
   105
    }
jaroslav@50
   106
jaroslav@50
   107
    static void deleteRec(File dir) throws IOException {
jaroslav@50
   108
        if (dir == null) {
jaroslav@50
   109
            return;
jaroslav@50
   110
        }
jaroslav@50
   111
        File[] arr = dir.listFiles();
jaroslav@50
   112
        if (arr != null) {
jaroslav@50
   113
            for (File f : arr) {
jaroslav@50
   114
                deleteRec(f);
jaroslav@50
   115
            }
jaroslav@50
   116
        }
jaroslav@50
   117
        dir.delete();
jaroslav@50
   118
    }
jaroslav@50
   119
jaroslav@50
   120
    @Test public void testApplicationWadl() {
jaroslav@50
   121
        String serviceWadl = webResource.path("application.wadl").
jaroslav@50
   122
                accept(MediaTypes.WADL).get(String.class);
jaroslav@50
   123
        assertTrue(serviceWadl.length() > 0);
jaroslav@50
   124
    }
jaroslav@50
   125
jaroslav@124
   126
    @Test public void testAPIWadl() {
jaroslav@124
   127
        String serviceWadl = apiResource.path("application.wadl").
jaroslav@124
   128
                accept(MediaTypes.WADL).get(String.class);
jaroslav@124
   129
        assertTrue(serviceWadl.length() > 0);
jaroslav@124
   130
    }
jaroslav@124
   131
martin@180
   132
    @Test public void testStatWadl() {
martin@180
   133
        String serviceWadl = statResource.path("application.wadl").
martin@180
   134
                accept(MediaTypes.WADL).get(String.class);
martin@180
   135
        assertTrue(serviceWadl.length() > 0);
martin@180
   136
    }
martin@180
   137
jaroslav@50
   138
    @Test public void testGetIndexPage() throws Exception {
jaroslav@121
   139
        String logJarda = apiResource.path("login").
jtulach@82
   140
            queryParam("name", "Jarda").
jtulach@82
   141
            queryParam("password", "darda").
jtulach@82
   142
            accept(MediaType.TEXT_PLAIN).
jtulach@82
   143
            put(String.class);
jtulach@82
   144
        assertNotNull("Logged in OK", logJarda);
jtulach@82
   145
jaroslav@50
   146
        String res = webResource.accept("text/html").get(String.class);
jaroslav@50
   147
        if (res.indexOf("Quoridor") == -1) {
jaroslav@50
   148
            fail("Wrong index.html:\n" + res);
jaroslav@50
   149
        }
jaroslav@51
   150
        if (res.indexOf("Login") == -1) {
jaroslav@51
   151
            fail("Wrong index.html:\n" + res);
jaroslav@51
   152
        }
jaroslav@72
   153
        if (res.indexOf("action=\"/login\"") == -1) {
jaroslav@51
   154
            fail("Wrong index.html:\n" + res);
jaroslav@51
   155
        }
jtulach@54
   156
        if (res.toLowerCase().indexOf("error") != -1) {
jtulach@54
   157
            fail("There was an error:\n" + res);
jtulach@54
   158
        }
jtulach@82
   159
        res = webResource.cookie(Cookie.valueOf("login=" + logJarda)).accept("text/html").get(String.class);
jtulach@74
   160
        if (res.indexOf("action=\"/games/create\"") == -1) {
jaroslav@51
   161
            fail(res);
jaroslav@51
   162
        }
jaroslav@50
   163
    }
jaroslav@50
   164
jaroslav@73
   165
    @Test public void testLogin() throws Exception {
jaroslav@73
   166
        MultivaluedMap formData = new MultivaluedMapImpl();
jaroslav@73
   167
        formData.add("name", "test");
jaroslav@73
   168
        formData.add("password", "pes");
jaroslav@73
   169
        ClientResponse response = webResource.path("login").
jaroslav@73
   170
            accept("text/html").type("application/x-www-form-urlencoded").
jaroslav@73
   171
            post(ClientResponse.class, formData);
jaroslav@73
   172
        final String res = response.getEntity(String.class);
jaroslav@73
   173
        assertEquals("OK", ClientResponse.Status.OK, response.getClientResponseStatus());
jtulach@81
   174
        if (res.indexOf("You are logged in as test") == -1) {
jaroslav@73
   175
            fail("res: " + res);
jaroslav@73
   176
        }
jaroslav@73
   177
    }
jaroslav@73
   178
jaroslav@73
   179
jaroslav@73
   180
    @Test public void testCreateGameWrongUsers() throws Exception {
jaroslav@121
   181
        String logTest = apiResource.path("login").
jtulach@82
   182
            queryParam("name", "test").
jtulach@82
   183
            queryParam("password", "pes").
jtulach@82
   184
            accept(MediaType.TEXT_PLAIN).
jtulach@82
   185
            put(String.class);
jaroslav@73
   186
        ClientResponse res = webResource.path("games/create").
jaroslav@73
   187
            queryParam("white", "unknown1").
jaroslav@73
   188
            queryParam("black", "unknown2").
jtulach@82
   189
            cookie(Cookie.valueOf("login=" + logTest)).
jaroslav@73
   190
            accept("text/html").
jaroslav@73
   191
            get(ClientResponse.class);
jaroslav@73
   192
        final String txt = res.getEntity(String.class);
jaroslav@73
   193
        assertEquals("Rejected, unknown user\n" + txt, ClientResponse.Status.NOT_FOUND, res.getClientResponseStatus());
jaroslav@73
   194
        if (txt.indexOf("You (test) must be") == -1) {
jaroslav@73
   195
            fail(txt);
jaroslav@73
   196
        }
jaroslav@73
   197
    }
jaroslav@73
   198
jaroslav@73
   199
    @Test public void testCreateGameOK() throws Exception {
jaroslav@276
   200
        Collection<String> origGamesNames= Arrays.asList(new File(dir, "games").list());
jaroslav@276
   201
        int origGames = origGamesNames.size();
jaroslav@272
   202
        
jaroslav@121
   203
        String logTest = apiResource.path("login").
jtulach@82
   204
            queryParam("name", "test").
jtulach@82
   205
            queryParam("password", "pes").
jtulach@82
   206
            accept(MediaType.TEXT_PLAIN).
jtulach@82
   207
            put(String.class);
jaroslav@121
   208
        String logJarda = apiResource.path("login").
jaroslav@106
   209
            queryParam("name", "Jarda").
jaroslav@106
   210
            queryParam("password", "darda").
jaroslav@106
   211
            accept(MediaType.TEXT_PLAIN).
jaroslav@106
   212
            put(String.class);
jaroslav@73
   213
        ClientResponse res = webResource.path("games/create").
jaroslav@106
   214
            queryParam("white", "Jarda").
jaroslav@73
   215
            queryParam("black", "test").
jtulach@82
   216
            cookie(Cookie.valueOf("login=" + logTest)).
jaroslav@73
   217
            accept("text/html").
jaroslav@73
   218
            get(ClientResponse.class);
jaroslav@73
   219
jaroslav@73
   220
        final String txt = res.getEntity(String.class);
jaroslav@73
   221
        assertEquals("OK\n" + txt, ClientResponse.Status.OK, res.getClientResponseStatus());
jaroslav@73
   222
jaroslav@276
   223
        
jaroslav@276
   224
        String myGame;
jaroslav@276
   225
        {
jaroslav@276
   226
            String[] games = new File(dir, "games").list();
jaroslav@276
   227
            assertEquals("One new game", origGames + 1, games.length);
jaroslav@276
   228
            
jaroslav@276
   229
            HashSet<String> newNames = new HashSet<String>();
jaroslav@276
   230
            newNames.addAll(Arrays.asList(games));
jaroslav@276
   231
            newNames.removeAll(origGamesNames);
jaroslav@276
   232
            assertEquals("Just one name left: " + newNames, 1, newNames.size());
jaroslav@276
   233
            myGame = newNames.iterator().next();
jaroslav@73
   234
        }
jaroslav@106
   235
jaroslav@276
   236
        if (txt.indexOf(myGame) == -1) {
jaroslav@276
   237
            fail(myGame + " expected inside of:\n" + txt);
jaroslav@276
   238
        }
jaroslav@276
   239
jaroslav@276
   240
        ClientResponse page = webResource.path("games/" + myGame).
jaroslav@106
   241
            cookie(Cookie.valueOf("login=" + logJarda)).
jaroslav@106
   242
            get(ClientResponse.class);
jaroslav@106
   243
        String ptxt = page.getEntity(String.class);
jaroslav@106
   244
        assertEquals("OK:\n" + ptxt, ClientResponse.Status.OK, page.getClientResponseStatus());
jaroslav@106
   245
jaroslav@113
   246
        Pattern p = Pattern.compile(".*<img[^>]*src=\"([^\"]*)\"");
jaroslav@106
   247
        Matcher m = p.matcher(ptxt);
jaroslav@106
   248
        assertTrue("image found\n" + ptxt, m.find());
jaroslav@106
   249
jaroslav@106
   250
        InputStream img1 = webResource.path(m.group(1)).get(InputStream.class);
jaroslav@106
   251
        assertNotNull("image found", img1);
jaroslav@106
   252
jaroslav@276
   253
        ClientResponse move = apiResource.path("games/" + myGame).
jaroslav@106
   254
            queryParam("loginID", logJarda).
jaroslav@106
   255
            queryParam("player", "Jarda").queryParam("move", "N").put(ClientResponse.class);
jaroslav@106
   256
        assertEquals("Move OK:\n" + move.getEntity(String.class), ClientResponse.Status.OK, move.getClientResponseStatus());
jaroslav@106
   257
jaroslav@106
   258
        InputStream img2 = webResource.path(m.group(1)).get(InputStream.class);
jaroslav@106
   259
        assertNotNull("image found", img2);
jaroslav@106
   260
jaroslav@276
   261
        ClientResponse page2 = webResource.path("games/" + myGame).
jaroslav@106
   262
            cookie(Cookie.valueOf("login=" + logJarda)).
jaroslav@106
   263
            get(ClientResponse.class);
jaroslav@106
   264
        String ptxt2 = page2.getEntity(String.class);
jaroslav@106
   265
        assertEquals("OK:\n" + ptxt2, ClientResponse.Status.OK, page2.getClientResponseStatus());
jaroslav@106
   266
jaroslav@106
   267
        Matcher m2 = p.matcher(ptxt2);
jaroslav@106
   268
        assertTrue("image found\n" + ptxt2, m2.find());
jaroslav@106
   269
jaroslav@106
   270
        InputStream img3 = webResource.path(m2.group(1)).get(InputStream.class);
jaroslav@106
   271
        assertNotNull("image found", img3);
jaroslav@106
   272
jaroslav@106
   273
        int diff = 0;
jaroslav@106
   274
        int cnt = 0;
jaroslav@106
   275
        for (;;) {
jaroslav@106
   276
            cnt++;
jaroslav@106
   277
            int b1 = img1.read();
jaroslav@106
   278
            int b2 = img2.read();
jaroslav@106
   279
            int b3 = img3.read();
jaroslav@106
   280
            assertEquals(b1, b2);
jaroslav@106
   281
            if (b3 != b1) {
jaroslav@106
   282
                diff++;
jaroslav@106
   283
            }
jaroslav@106
   284
            if (b1 == -1 || b3 == -1) break;
jaroslav@106
   285
jaroslav@106
   286
        }
jaroslav@106
   287
        if (diff == 0) {
jaroslav@106
   288
//            fail("There shall be difference in the streams. Read bytes " + cnt);
jaroslav@106
   289
        }
jaroslav@73
   290
    }
jaroslav@272
   291
    
jaroslav@272
   292
    @Test public void testGetWrongGame() throws Exception {
jaroslav@272
   293
        String logTest = apiResource.path("login").
jaroslav@272
   294
            queryParam("name", "test").
jaroslav@272
   295
            queryParam("password", "pes").
jaroslav@272
   296
            accept(MediaType.TEXT_PLAIN).
jaroslav@272
   297
            put(String.class);
jaroslav@272
   298
        
jaroslav@272
   299
        String xml = apiResource.path("games/fdcc275c-55ad-4b7e-a776-bde3876b2f2c").
jaroslav@272
   300
            queryParam("name", "test").
jaroslav@272
   301
            queryParam("password", "pes").
jaroslav@272
   302
            accept(MediaType.TEXT_XML).
jaroslav@272
   303
            get(String.class);
jaroslav@272
   304
        
jaroslav@272
   305
        if (xml.indexOf((char)0x1b) >= 0) {
jaroslav@272
   306
            fail(xml.replace((char)0x1b, 'X'));
jaroslav@272
   307
        }
jaroslav@272
   308
        
jaroslav@272
   309
        ClientResponse res = webResource.path("games/fdcc275c-55ad-4b7e-a776-bde3876b2f2c").
jaroslav@272
   310
            queryParam("white", "Jarda").
jaroslav@272
   311
            queryParam("black", "test").
jaroslav@272
   312
            cookie(Cookie.valueOf("login=" + logTest)).
jaroslav@272
   313
            accept("text/html").
jaroslav@272
   314
            get(ClientResponse.class);
jaroslav@272
   315
        final String txt = res.getEntity(String.class);
jaroslav@272
   316
        assertEquals("OK\n" + txt, ClientResponse.Status.OK, res.getClientResponseStatus());
jaroslav@272
   317
    }
jaroslav@272
   318
    
jaroslav@73
   319
jaroslav@272
   320
    private static void copyResource(String res, File file) throws IOException {
jaroslav@272
   321
        InputStream is = UITest.class.getResourceAsStream(res);
jaroslav@272
   322
        file.getParentFile().mkdirs();
jaroslav@272
   323
        FileOutputStream os = new FileOutputStream(file);
jaroslav@272
   324
        byte[] arr = new byte[4092];
jaroslav@272
   325
        for (;;) {
jaroslav@272
   326
            int len = is.read(arr);
jaroslav@272
   327
            if (len == -1) {
jaroslav@272
   328
                break;
jaroslav@272
   329
            }
jaroslav@272
   330
            os.write(arr, 0, len);
jaroslav@272
   331
        }
jaroslav@272
   332
        is.close();
jaroslav@272
   333
        os.close();
jaroslav@272
   334
    }
jaroslav@272
   335
    
jaroslav@50
   336
}