webidor/src/test/java/cz/xelfi/quoridor/webidor/resources/ChatTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 20 Aug 2011 17:14:23 +0200
branchglassfish
changeset 285 bc4ddef89763
parent 264 d60370059c3c
permissions -rw-r--r--
Adjusting tests to use different path
jaroslav@115
     1
/*
jaroslav@264
     2
 * Quoridor server and related libraries
jaroslav@264
     3
 * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@115
     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@115
     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@115
    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@115
    17
 */
jaroslav@115
    18
jaroslav@115
    19
package cz.xelfi.quoridor.webidor.resources;
jaroslav@115
    20
jaroslav@258
    21
import com.sun.jersey.test.framework.WebAppDescriptor;
jaroslav@258
    22
import com.sun.jersey.test.framework.AppDescriptor;
jaroslav@115
    23
import com.sun.jersey.test.framework.JerseyTest;
jaroslav@285
    24
import com.sun.jersey.test.framework.WebAppDescriptor.Builder;
jaroslav@115
    25
import cz.xelfi.quoridor.Board;
jaroslav@115
    26
import cz.xelfi.quoridor.Move;
jaroslav@115
    27
import cz.xelfi.quoridor.webidor.Game;
jaroslav@115
    28
import cz.xelfi.quoridor.webidor.GameId;
jaroslav@115
    29
import cz.xelfi.quoridor.webidor.Note;
jaroslav@115
    30
import java.io.File;
jaroslav@118
    31
import java.io.FileInputStream;
jaroslav@115
    32
import java.io.FileNotFoundException;
jaroslav@115
    33
import java.io.FileOutputStream;
jaroslav@115
    34
import java.io.IOException;
jaroslav@118
    35
import java.io.InputStreamReader;
jaroslav@118
    36
import java.io.Reader;
jaroslav@115
    37
import java.util.List;
jtulach@117
    38
import java.util.ResourceBundle;
jaroslav@115
    39
import javax.ws.rs.core.MediaType;
jaroslav@115
    40
import org.junit.Test;
jaroslav@115
    41
import static org.junit.Assert.*;
jaroslav@115
    42
jaroslav@115
    43
/**
jaroslav@115
    44
 *
jaroslav@115
    45
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@115
    46
 */
jaroslav@115
    47
public class ChatTest extends JerseyTest {
jaroslav@149
    48
    static {
jaroslav@149
    49
        System.setProperty("JERSEY_HTTP_PORT", "33437");
jaroslav@149
    50
    }
jaroslav@115
    51
    private File dir;
jaroslav@115
    52
jaroslav@115
    53
    @Override
jaroslav@258
    54
    protected AppDescriptor configure() {
jaroslav@258
    55
        try {
jaroslav@258
    56
            dir = File.createTempFile("quoridor", ".dir");
jaroslav@258
    57
            dir.delete();
jaroslav@258
    58
            System.setProperty("quoridor.dir", dir.getPath());
jaroslav@258
    59
            dir.mkdirs();
jaroslav@258
    60
            File passwd = new File(dir, "passwd");
jaroslav@258
    61
            FileOutputStream os = new FileOutputStream(passwd);
jaroslav@258
    62
            os.write("Jarda=heslo\nJirka=pesko\n".getBytes("UTF-8"));
jaroslav@258
    63
            os.close();
jaroslav@258
    64
        } catch (Exception ex) {
jaroslav@258
    65
            throw new IllegalStateException(ex);
jaroslav@258
    66
        }
jaroslav@285
    67
        final Builder builder = new WebAppDescriptor.Builder("cz.xelfi.quoridor.webidor.resources");
jaroslav@285
    68
        return builder.contextPath("context").build();
jaroslav@115
    69
    }
jaroslav@115
    70
jaroslav@115
    71
    @Override
jaroslav@115
    72
    public void tearDown() throws Exception {
jaroslav@115
    73
        super.tearDown();
jaroslav@115
    74
        deleteRec(dir);
jaroslav@115
    75
    }
jaroslav@115
    76
jaroslav@115
    77
    static void deleteRec(File dir) throws IOException {
jaroslav@115
    78
        if (dir == null) {
jaroslav@115
    79
            return;
jaroslav@115
    80
        }
jaroslav@115
    81
        File[] arr = dir.listFiles();
jaroslav@115
    82
        if (arr != null) {
jaroslav@115
    83
            for (File f : arr) {
jaroslav@115
    84
                deleteRec(f);
jaroslav@115
    85
            }
jaroslav@115
    86
        }
jaroslav@115
    87
        dir.delete();
jaroslav@115
    88
    }
jaroslav@115
    89
jaroslav@115
    90
    @Test public void testCreateAGame() throws Exception {
jaroslav@258
    91
        String logJarda = resource().path("login").
jaroslav@115
    92
            queryParam("name", "Jarda").
jaroslav@115
    93
            queryParam("password", "heslo").
jaroslav@115
    94
            accept(MediaType.TEXT_PLAIN).
jaroslav@115
    95
            put(String.class);
jaroslav@258
    96
        String logJirka = resource().path("login").
jaroslav@115
    97
            queryParam("name", "Jirka").
jaroslav@115
    98
            queryParam("password", "pesko").
jaroslav@115
    99
            accept(MediaType.TEXT_PLAIN).
jaroslav@115
   100
            put(String.class);
jaroslav@258
   101
    GameId s = resource().path("games").queryParam("loginID", logJarda).
jaroslav@115
   102
                queryParam("white", "Jarda")
jaroslav@115
   103
                .queryParam("black", "Jirka").post(GameId.class);
jaroslav@115
   104
jaroslav@115
   105
        Thread.sleep(100);
jaroslav@115
   106
        final long now = System.currentTimeMillis();
jaroslav@115
   107
        if (s.getModified() >= now) {
jaroslav@115
   108
            fail("The game is supposed to be modified in past");
jaroslav@115
   109
        }
jaroslav@115
   110
        Thread.sleep(100);
jaroslav@115
   111
jtulach@117
   112
        ResourceBundle b = ResourceBundle.getBundle("cz/xelfi/quoridor/webidor/TestBundle");
jtulach@117
   113
        String comment = b.getString("COMMENT");
jtulach@117
   114
jaroslav@258
   115
        GameId s1 = resource().path("games/" + s.getId()).
jaroslav@115
   116
            queryParam("loginID", logJarda).
jaroslav@115
   117
            queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
jaroslav@115
   118
jaroslav@258
   119
        GameId comment1 = resource().path("games/" + s.getId()).
jaroslav@115
   120
            queryParam("loginID", logJarda).
jaroslav@115
   121
            queryParam("player", "Jarda").queryParam("comment", "I like this game!").put(GameId.class);
jaroslav@130
   122
        assertEquals("One comment in the game", 1, comment1.getComments());
jaroslav@115
   123
jaroslav@258
   124
        GameId comment2 = resource().path("games/" + s.getId()).
jaroslav@115
   125
            queryParam("loginID", logJirka).
jtulach@117
   126
            queryParam("player", "Jirka").queryParam("comment", comment).put(GameId.class);
jaroslav@130
   127
        assertEquals("Snd comment in the game", 2, comment2.getComments());
jaroslav@130
   128
jaroslav@258
   129
        GameId s2 = resource().path("games/" + s.getId()).
jaroslav@115
   130
            queryParam("loginID", logJirka).
jaroslav@115
   131
            queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
jaroslav@115
   132
        assertNotNull("Successful move", s2);
jaroslav@130
   133
        assertEquals("Still two comments in the game", 2, s2.getComments());
jaroslav@115
   134
jaroslav@115
   135
        File game = new File(new File(dir, "games"), s1.getId());
jaroslav@115
   136
        assertTrue("File for game exists", game.exists());
jaroslav@115
   137
        String content = readFile(game);
jaroslav@115
   138
jaroslav@115
   139
        if (!content.contains("# white: Jarda")) {
jaroslav@115
   140
            fail(content);
jaroslav@115
   141
        }
jaroslav@115
   142
        if (!content.contains("# black: Jirka")) {
jaroslav@115
   143
            fail(content);
jaroslav@115
   144
        }
jaroslav@115
   145
        if (!content.contains("N")) {
jaroslav@115
   146
            fail(content);
jaroslav@115
   147
        }
jaroslav@115
   148
        if (!content.contains("... S")) {
jaroslav@115
   149
            fail(content);
jaroslav@115
   150
        }
jaroslav@115
   151
        if (!content.contains("I like")) {
jaroslav@115
   152
            fail(content);
jaroslav@115
   153
        }
jtulach@117
   154
        if (!content.contains(comment)) {
jaroslav@115
   155
            fail(content);
jaroslav@115
   156
        }
jaroslav@115
   157
jaroslav@115
   158
        Games read = new Games(new File(dir, "games"), new Quoridor());
jaroslav@115
   159
        List<Game> readGames = read.getGames();
jaroslav@115
   160
        assertEquals("One game read", 1, readGames.size());
jaroslav@115
   161
        Board board = readGames.get(0).getBoard();
jaroslav@115
   162
        assertEquals(1, board.getPlayers().get(0).getRow());
jaroslav@115
   163
        assertEquals(7, board.getPlayers().get(1).getRow());
jaroslav@115
   164
        assertEquals(Move.NORTH, readGames.get(0).getMoves().get(0).getMove());
jaroslav@115
   165
        assertEquals(Move.SOUTH, readGames.get(0).getMoves().get(1).getMove());
jaroslav@115
   166
jaroslav@115
   167
        Game rg = readGames.get(0);
jaroslav@115
   168
        assertNull("No comments on second move", rg.getMoves().get(1).getComments());
jaroslav@115
   169
        List<Note> cmnts = rg.getMoves().get(0).getComments();
jaroslav@115
   170
        assertNotNull("Some comments on first move", cmnts);
jaroslav@115
   171
        assertEquals("Two comments: " + cmnts, 2, cmnts.size());
jaroslav@115
   172
jaroslav@115
   173
        if (!cmnts.get(0).getComment().contains("I like")) {
jaroslav@115
   174
            fail();
jaroslav@115
   175
        }
jtulach@117
   176
        if (!cmnts.get(1).getComment().contains(comment)) {
jaroslav@115
   177
            fail();
jaroslav@115
   178
        }
jaroslav@115
   179
jaroslav@121
   180
        Thread.sleep(1500);
jaroslav@121
   181
jaroslav@115
   182
        File tmp = File.createTempFile("test-board", "game");
jaroslav@115
   183
        read.storeGame(rg, tmp);
jaroslav@115
   184
jaroslav@130
   185
        String sss1 = readFile(game).replaceAll("@.*:", "date");
jaroslav@121
   186
        Thread.sleep(1500);
jaroslav@121
   187
jaroslav@130
   188
        assertEquals("Newly written file remains the same", sss1, readFile(tmp).replaceAll("@.*:", "date"));
jaroslav@115
   189
jaroslav@258
   190
        String sGame = resource().path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(String.class);
jaroslav@258
   191
        Game readGame = resource().path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
jaroslav@115
   192
        assertNotNull("Game really returned", readGame);
jaroslav@115
   193
jaroslav@115
   194
        assertEquals(Move.NORTH, readGame.getMoves().get(0).getMove());
jaroslav@115
   195
        assertEquals(Move.SOUTH, readGame.getMoves().get(1).getMove());
jaroslav@115
   196
jaroslav@115
   197
        assertNull("No comments on second move", readGame.getMoves().get(1).getComments());
jaroslav@115
   198
        cmnts = readGame.getMoves().get(0).getComments();
jaroslav@115
   199
        assertNotNull("Some comments on first move", cmnts);
jaroslav@115
   200
        assertEquals("Two comments: " + cmnts, 2, cmnts.size());
jaroslav@115
   201
        if (!cmnts.get(0).getComment().contains("I like")) {
jaroslav@115
   202
            fail();
jaroslav@115
   203
        }
jtulach@117
   204
        if (!cmnts.get(1).getComment().contains(comment)) {
jaroslav@115
   205
            fail();
jaroslav@115
   206
        }
jaroslav@115
   207
    }
jaroslav@115
   208
jaroslav@115
   209
    private String readFile(File game) throws IOException, FileNotFoundException {
jaroslav@115
   210
        char[] arr = new char[4096];
jaroslav@118
   211
        Reader gameContent = new InputStreamReader(new FileInputStream(game), "UTF-8");
jaroslav@115
   212
        int len = gameContent.read(arr);
jaroslav@115
   213
        String content = new String(arr, 0, len);
jaroslav@115
   214
        return content;
jaroslav@115
   215
    }
jaroslav@115
   216
jaroslav@115
   217
}