freemarkerdor/src/test/java/cz/xelfi/quoridor/freemarkerdor/UITest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 27 Nov 2010 07:52:19 +0100
changeset 272 215b417aac98
parent 264 d60370059c3c
child 276 f0a4e8c41419
permissions -rw-r--r--
Supress invalid XML characters
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@50
    20
import com.sun.jersey.api.client.Client;
jaroslav@73
    21
import com.sun.jersey.api.client.ClientResponse;
jaroslav@50
    22
import com.sun.jersey.api.client.WebResource;
jaroslav@50
    23
import com.sun.jersey.core.header.MediaTypes;
jaroslav@73
    24
import com.sun.jersey.core.util.MultivaluedMapImpl;
jaroslav@126
    25
import com.sun.net.httpserver.HttpServer;
jaroslav@126
    26
import cz.xelfi.quoridor.webidor.resources.Quoridor;
jaroslav@50
    27
import java.io.File;
jaroslav@73
    28
import java.io.FileOutputStream;
jaroslav@50
    29
import java.io.IOException;
jaroslav@106
    30
import java.io.InputStream;
jaroslav@50
    31
import java.net.URI;
jaroslav@62
    32
import java.util.Locale;
jaroslav@50
    33
import java.util.concurrent.Callable;
jaroslav@106
    34
import java.util.regex.Matcher;
jaroslav@106
    35
import java.util.regex.Pattern;
jaroslav@51
    36
import javax.ws.rs.core.Cookie;
jtulach@82
    37
import javax.ws.rs.core.MediaType;
jaroslav@73
    38
import javax.ws.rs.core.MultivaluedMap;
jaroslav@73
    39
import org.junit.AfterClass;
jaroslav@50
    40
import org.junit.Before;
jaroslav@62
    41
import org.junit.BeforeClass;
jaroslav@50
    42
import org.junit.Test;
jaroslav@50
    43
import static org.junit.Assert.*;
jaroslav@50
    44
jaroslav@50
    45
/**
jaroslav@50
    46
 *
jaroslav@50
    47
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@50
    48
 */
jaroslav@50
    49
public class UITest extends Object {
jaroslav@73
    50
    private static File dir;
jaroslav@126
    51
    private static HttpServer stopAPI;
martin@180
    52
    private static HttpServer stopStatistics;
jaroslav@73
    53
    private static Callable<Void> stop;
jaroslav@272
    54
jaroslav@50
    55
    private WebResource webResource;
jaroslav@121
    56
    private WebResource apiResource;
martin@180
    57
    private WebResource statResource;
jaroslav@50
    58
jaroslav@50
    59
    public UITest() throws Exception {
jaroslav@50
    60
    }
jaroslav@50
    61
jaroslav@62
    62
    @BeforeClass
jaroslav@73
    63
    public static void localeEnglish() throws Exception {
jaroslav@62
    64
        Locale.setDefault(Locale.ENGLISH);
jaroslav@73
    65
        dir = File.createTempFile("quoridor", ".dir");
jaroslav@73
    66
        dir.delete();
jaroslav@73
    67
        dir.mkdirs();
jaroslav@272
    68
        
jaroslav@272
    69
        File games = new File(dir, "games");
jaroslav@272
    70
        copyResource("wrong.encoding", new File(games, "fdcc275c-55ad-4b7e-a776-bde3876b2f2c"));
jaroslav@272
    71
        
jaroslav@272
    72
        
jaroslav@73
    73
        System.setProperty("quoridor.dir", dir.getPath());
jaroslav@126
    74
        stopAPI = Quoridor.start(9990);
martin@180
    75
        stopStatistics = Quoridor.start(9992);
jaroslav@234
    76
        stop = UI.startServers(9991, "http://localhost:9990", "http://localhost:9992", null);
jaroslav@73
    77
jaroslav@73
    78
        File passwd = new File(dir, "passwd");
jaroslav@73
    79
        FileOutputStream os = new FileOutputStream(passwd);
jtulach@82
    80
        os.write("test=pes\nJarda=darda\n".getBytes("UTF-8"));
jaroslav@73
    81
        os.close();
jaroslav@62
    82
    }
jaroslav@62
    83
jaroslav@50
    84
    @Before
jaroslav@50
    85
    public void setUp() throws Exception {
jaroslav@50
    86
jaroslav@50
    87
        Client client = new Client();
jaroslav@68
    88
        webResource = client.resource(new URI("http://localhost:9991/"));
jaroslav@124
    89
        apiResource = client.resource(new URI("http://localhost:9990/"));
martin@180
    90
        statResource = client.resource(new URI("http://localhost:9992/"));
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
martin@180
   129
    @Test public void testStatWadl() {
martin@180
   130
        String serviceWadl = statResource.path("application.wadl").
martin@180
   131
                accept(MediaTypes.WADL).get(String.class);
martin@180
   132
        assertTrue(serviceWadl.length() > 0);
martin@180
   133
    }
martin@180
   134
jaroslav@50
   135
    @Test public void testGetIndexPage() throws Exception {
jaroslav@121
   136
        String logJarda = apiResource.path("login").
jtulach@82
   137
            queryParam("name", "Jarda").
jtulach@82
   138
            queryParam("password", "darda").
jtulach@82
   139
            accept(MediaType.TEXT_PLAIN).
jtulach@82
   140
            put(String.class);
jtulach@82
   141
        assertNotNull("Logged in OK", logJarda);
jtulach@82
   142
jaroslav@50
   143
        String res = webResource.accept("text/html").get(String.class);
jaroslav@50
   144
        if (res.indexOf("Quoridor") == -1) {
jaroslav@50
   145
            fail("Wrong index.html:\n" + res);
jaroslav@50
   146
        }
jaroslav@51
   147
        if (res.indexOf("Login") == -1) {
jaroslav@51
   148
            fail("Wrong index.html:\n" + res);
jaroslav@51
   149
        }
jaroslav@72
   150
        if (res.indexOf("action=\"/login\"") == -1) {
jaroslav@51
   151
            fail("Wrong index.html:\n" + res);
jaroslav@51
   152
        }
jtulach@54
   153
        if (res.toLowerCase().indexOf("error") != -1) {
jtulach@54
   154
            fail("There was an error:\n" + res);
jtulach@54
   155
        }
jtulach@82
   156
        res = webResource.cookie(Cookie.valueOf("login=" + logJarda)).accept("text/html").get(String.class);
jtulach@74
   157
        if (res.indexOf("action=\"/games/create\"") == -1) {
jaroslav@51
   158
            fail(res);
jaroslav@51
   159
        }
jaroslav@50
   160
    }
jaroslav@50
   161
jaroslav@73
   162
    @Test public void testLogin() throws Exception {
jaroslav@73
   163
        MultivaluedMap formData = new MultivaluedMapImpl();
jaroslav@73
   164
        formData.add("name", "test");
jaroslav@73
   165
        formData.add("password", "pes");
jaroslav@73
   166
        ClientResponse response = webResource.path("login").
jaroslav@73
   167
            accept("text/html").type("application/x-www-form-urlencoded").
jaroslav@73
   168
            post(ClientResponse.class, formData);
jaroslav@73
   169
        final String res = response.getEntity(String.class);
jaroslav@73
   170
        assertEquals("OK", ClientResponse.Status.OK, response.getClientResponseStatus());
jtulach@81
   171
        if (res.indexOf("You are logged in as test") == -1) {
jaroslav@73
   172
            fail("res: " + res);
jaroslav@73
   173
        }
jaroslav@73
   174
    }
jaroslav@73
   175
jaroslav@73
   176
jaroslav@73
   177
    @Test public void testCreateGameWrongUsers() throws Exception {
jaroslav@121
   178
        String logTest = apiResource.path("login").
jtulach@82
   179
            queryParam("name", "test").
jtulach@82
   180
            queryParam("password", "pes").
jtulach@82
   181
            accept(MediaType.TEXT_PLAIN).
jtulach@82
   182
            put(String.class);
jaroslav@73
   183
        ClientResponse res = webResource.path("games/create").
jaroslav@73
   184
            queryParam("white", "unknown1").
jaroslav@73
   185
            queryParam("black", "unknown2").
jtulach@82
   186
            cookie(Cookie.valueOf("login=" + logTest)).
jaroslav@73
   187
            accept("text/html").
jaroslav@73
   188
            get(ClientResponse.class);
jaroslav@73
   189
        final String txt = res.getEntity(String.class);
jaroslav@73
   190
        assertEquals("Rejected, unknown user\n" + txt, ClientResponse.Status.NOT_FOUND, res.getClientResponseStatus());
jaroslav@73
   191
        if (txt.indexOf("You (test) must be") == -1) {
jaroslav@73
   192
            fail(txt);
jaroslav@73
   193
        }
jaroslav@73
   194
    }
jaroslav@73
   195
jaroslav@73
   196
    @Test public void testCreateGameOK() throws Exception {
jaroslav@272
   197
        int origGames = new File(dir, "games").list().length;
jaroslav@272
   198
        
jaroslav@272
   199
        
jaroslav@121
   200
        String logTest = apiResource.path("login").
jtulach@82
   201
            queryParam("name", "test").
jtulach@82
   202
            queryParam("password", "pes").
jtulach@82
   203
            accept(MediaType.TEXT_PLAIN).
jtulach@82
   204
            put(String.class);
jaroslav@121
   205
        String logJarda = apiResource.path("login").
jaroslav@106
   206
            queryParam("name", "Jarda").
jaroslav@106
   207
            queryParam("password", "darda").
jaroslav@106
   208
            accept(MediaType.TEXT_PLAIN).
jaroslav@106
   209
            put(String.class);
jaroslav@73
   210
        ClientResponse res = webResource.path("games/create").
jaroslav@106
   211
            queryParam("white", "Jarda").
jaroslav@73
   212
            queryParam("black", "test").
jtulach@82
   213
            cookie(Cookie.valueOf("login=" + logTest)).
jaroslav@73
   214
            accept("text/html").
jaroslav@73
   215
            get(ClientResponse.class);
jaroslav@73
   216
jaroslav@73
   217
        final String txt = res.getEntity(String.class);
jaroslav@73
   218
        assertEquals("OK\n" + txt, ClientResponse.Status.OK, res.getClientResponseStatus());
jaroslav@73
   219
jaroslav@73
   220
        String[] games = new File(dir, "games").list();
jaroslav@272
   221
        assertEquals("One new game", origGames + 1, games.length);
jaroslav@73
   222
jaroslav@73
   223
        if (txt.indexOf(games[0]) == -1) {
jaroslav@73
   224
            fail(games[0] + " expected inside of:\n" + txt);
jaroslav@73
   225
        }
jaroslav@106
   226
jaroslav@106
   227
        ClientResponse page = webResource.path("games/" + games[0]).
jaroslav@106
   228
            cookie(Cookie.valueOf("login=" + logJarda)).
jaroslav@106
   229
            get(ClientResponse.class);
jaroslav@106
   230
        String ptxt = page.getEntity(String.class);
jaroslav@106
   231
        assertEquals("OK:\n" + ptxt, ClientResponse.Status.OK, page.getClientResponseStatus());
jaroslav@106
   232
jaroslav@113
   233
        Pattern p = Pattern.compile(".*<img[^>]*src=\"([^\"]*)\"");
jaroslav@106
   234
        Matcher m = p.matcher(ptxt);
jaroslav@106
   235
        assertTrue("image found\n" + ptxt, m.find());
jaroslav@106
   236
jaroslav@106
   237
        InputStream img1 = webResource.path(m.group(1)).get(InputStream.class);
jaroslav@106
   238
        assertNotNull("image found", img1);
jaroslav@106
   239
jaroslav@121
   240
        ClientResponse move = apiResource.path("games/" + games[0]).
jaroslav@106
   241
            queryParam("loginID", logJarda).
jaroslav@106
   242
            queryParam("player", "Jarda").queryParam("move", "N").put(ClientResponse.class);
jaroslav@106
   243
        assertEquals("Move OK:\n" + move.getEntity(String.class), ClientResponse.Status.OK, move.getClientResponseStatus());
jaroslav@106
   244
jaroslav@106
   245
        InputStream img2 = webResource.path(m.group(1)).get(InputStream.class);
jaroslav@106
   246
        assertNotNull("image found", img2);
jaroslav@106
   247
jaroslav@106
   248
        ClientResponse page2 = webResource.path("games/" + games[0]).
jaroslav@106
   249
            cookie(Cookie.valueOf("login=" + logJarda)).
jaroslav@106
   250
            get(ClientResponse.class);
jaroslav@106
   251
        String ptxt2 = page2.getEntity(String.class);
jaroslav@106
   252
        assertEquals("OK:\n" + ptxt2, ClientResponse.Status.OK, page2.getClientResponseStatus());
jaroslav@106
   253
jaroslav@106
   254
        Matcher m2 = p.matcher(ptxt2);
jaroslav@106
   255
        assertTrue("image found\n" + ptxt2, m2.find());
jaroslav@106
   256
jaroslav@106
   257
        InputStream img3 = webResource.path(m2.group(1)).get(InputStream.class);
jaroslav@106
   258
        assertNotNull("image found", img3);
jaroslav@106
   259
jaroslav@106
   260
        int diff = 0;
jaroslav@106
   261
        int cnt = 0;
jaroslav@106
   262
        for (;;) {
jaroslav@106
   263
            cnt++;
jaroslav@106
   264
            int b1 = img1.read();
jaroslav@106
   265
            int b2 = img2.read();
jaroslav@106
   266
            int b3 = img3.read();
jaroslav@106
   267
            assertEquals(b1, b2);
jaroslav@106
   268
            if (b3 != b1) {
jaroslav@106
   269
                diff++;
jaroslav@106
   270
            }
jaroslav@106
   271
            if (b1 == -1 || b3 == -1) break;
jaroslav@106
   272
jaroslav@106
   273
        }
jaroslav@106
   274
        if (diff == 0) {
jaroslav@106
   275
//            fail("There shall be difference in the streams. Read bytes " + cnt);
jaroslav@106
   276
        }
jaroslav@73
   277
    }
jaroslav@272
   278
    
jaroslav@272
   279
    @Test public void testGetWrongGame() throws Exception {
jaroslav@272
   280
        String logTest = apiResource.path("login").
jaroslav@272
   281
            queryParam("name", "test").
jaroslav@272
   282
            queryParam("password", "pes").
jaroslav@272
   283
            accept(MediaType.TEXT_PLAIN).
jaroslav@272
   284
            put(String.class);
jaroslav@272
   285
        
jaroslav@272
   286
        String xml = apiResource.path("games/fdcc275c-55ad-4b7e-a776-bde3876b2f2c").
jaroslav@272
   287
            queryParam("name", "test").
jaroslav@272
   288
            queryParam("password", "pes").
jaroslav@272
   289
            accept(MediaType.TEXT_XML).
jaroslav@272
   290
            get(String.class);
jaroslav@272
   291
        
jaroslav@272
   292
        if (xml.indexOf((char)0x1b) >= 0) {
jaroslav@272
   293
            fail(xml.replace((char)0x1b, 'X'));
jaroslav@272
   294
        }
jaroslav@272
   295
        
jaroslav@272
   296
        ClientResponse res = webResource.path("games/fdcc275c-55ad-4b7e-a776-bde3876b2f2c").
jaroslav@272
   297
            queryParam("white", "Jarda").
jaroslav@272
   298
            queryParam("black", "test").
jaroslav@272
   299
            cookie(Cookie.valueOf("login=" + logTest)).
jaroslav@272
   300
            accept("text/html").
jaroslav@272
   301
            get(ClientResponse.class);
jaroslav@272
   302
        final String txt = res.getEntity(String.class);
jaroslav@272
   303
        assertEquals("OK\n" + txt, ClientResponse.Status.OK, res.getClientResponseStatus());
jaroslav@272
   304
    }
jaroslav@272
   305
    
jaroslav@73
   306
jaroslav@272
   307
    private static void copyResource(String res, File file) throws IOException {
jaroslav@272
   308
        InputStream is = UITest.class.getResourceAsStream(res);
jaroslav@272
   309
        file.getParentFile().mkdirs();
jaroslav@272
   310
        FileOutputStream os = new FileOutputStream(file);
jaroslav@272
   311
        byte[] arr = new byte[4092];
jaroslav@272
   312
        for (;;) {
jaroslav@272
   313
            int len = is.read(arr);
jaroslav@272
   314
            if (len == -1) {
jaroslav@272
   315
                break;
jaroslav@272
   316
            }
jaroslav@272
   317
            os.write(arr, 0, len);
jaroslav@272
   318
        }
jaroslav@272
   319
        is.close();
jaroslav@272
   320
        os.close();
jaroslav@272
   321
    }
jaroslav@272
   322
    
jaroslav@50
   323
}