webidor/src/test/java/cz/xelfi/quoridor/webidor/resources/ChatTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 12 Sep 2010 00:06:44 +0200
changeset 258 935118a5831a
parent 149 a441b02a638a
child 264 d60370059c3c
permissions -rw-r--r--
Upgrading to jersey 1.3
jaroslav@115
     1
/*
jaroslav@115
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@115
     3
 *
jaroslav@115
     4
 * The contents of this file are subject to the terms of either the GNU
jaroslav@115
     5
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@115
     6
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@115
     7
 * "License"). You may not use this file except in compliance with the
jaroslav@115
     8
 * License. You can obtain a copy of the License at
jaroslav@115
     9
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@115
    10
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@115
    11
 * specific language governing permissions and limitations under the
jaroslav@115
    12
 * License.  When distributing the software, include this License Header
jaroslav@115
    13
 * Notice in each file and include the License file at
jaroslav@115
    14
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
jaroslav@115
    15
 * particular file as subject to the "Classpath" exception as provided
jaroslav@115
    16
 * by Sun in the GPL Version 2 section of the License file that
jaroslav@115
    17
 * accompanied this code. If applicable, add the following below the
jaroslav@115
    18
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@115
    19
 * your own identifying information:
jaroslav@115
    20
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@115
    21
 *
jaroslav@115
    22
 * Contributor(s):
jaroslav@115
    23
 *
jaroslav@115
    24
 * Portions Copyrighted 2009 Jaroslav Tulach
jaroslav@115
    25
 */
jaroslav@115
    26
jaroslav@115
    27
package cz.xelfi.quoridor.webidor.resources;
jaroslav@115
    28
jaroslav@258
    29
import com.sun.jersey.test.framework.WebAppDescriptor;
jaroslav@258
    30
import com.sun.jersey.test.framework.AppDescriptor;
jaroslav@115
    31
import com.sun.jersey.test.framework.JerseyTest;
jaroslav@115
    32
import cz.xelfi.quoridor.Board;
jaroslav@115
    33
import cz.xelfi.quoridor.Move;
jaroslav@115
    34
import cz.xelfi.quoridor.webidor.Game;
jaroslav@115
    35
import cz.xelfi.quoridor.webidor.GameId;
jaroslav@115
    36
import cz.xelfi.quoridor.webidor.Note;
jaroslav@115
    37
import java.io.File;
jaroslav@118
    38
import java.io.FileInputStream;
jaroslav@115
    39
import java.io.FileNotFoundException;
jaroslav@115
    40
import java.io.FileOutputStream;
jaroslav@115
    41
import java.io.IOException;
jaroslav@118
    42
import java.io.InputStreamReader;
jaroslav@118
    43
import java.io.Reader;
jaroslav@115
    44
import java.util.List;
jtulach@117
    45
import java.util.ResourceBundle;
jaroslav@115
    46
import javax.ws.rs.core.MediaType;
jaroslav@115
    47
import org.junit.Test;
jaroslav@115
    48
import static org.junit.Assert.*;
jaroslav@115
    49
jaroslav@115
    50
/**
jaroslav@115
    51
 *
jaroslav@115
    52
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@115
    53
 */
jaroslav@115
    54
public class ChatTest extends JerseyTest {
jaroslav@149
    55
    static {
jaroslav@149
    56
        System.setProperty("JERSEY_HTTP_PORT", "33437");
jaroslav@149
    57
    }
jaroslav@115
    58
    private File dir;
jaroslav@115
    59
jaroslav@115
    60
    @Override
jaroslav@258
    61
    protected AppDescriptor configure() {
jaroslav@258
    62
        try {
jaroslav@258
    63
            dir = File.createTempFile("quoridor", ".dir");
jaroslav@258
    64
            dir.delete();
jaroslav@258
    65
            System.setProperty("quoridor.dir", dir.getPath());
jaroslav@258
    66
            dir.mkdirs();
jaroslav@258
    67
            File passwd = new File(dir, "passwd");
jaroslav@258
    68
            FileOutputStream os = new FileOutputStream(passwd);
jaroslav@258
    69
            os.write("Jarda=heslo\nJirka=pesko\n".getBytes("UTF-8"));
jaroslav@258
    70
            os.close();
jaroslav@258
    71
        } catch (Exception ex) {
jaroslav@258
    72
            throw new IllegalStateException(ex);
jaroslav@258
    73
        }
jaroslav@258
    74
        return new WebAppDescriptor.Builder("cz.xelfi.quoridor.webidor.resources").build();
jaroslav@115
    75
    }
jaroslav@115
    76
jaroslav@115
    77
    @Override
jaroslav@115
    78
    public void tearDown() throws Exception {
jaroslav@115
    79
        super.tearDown();
jaroslav@115
    80
        deleteRec(dir);
jaroslav@115
    81
    }
jaroslav@115
    82
jaroslav@115
    83
    static void deleteRec(File dir) throws IOException {
jaroslav@115
    84
        if (dir == null) {
jaroslav@115
    85
            return;
jaroslav@115
    86
        }
jaroslav@115
    87
        File[] arr = dir.listFiles();
jaroslav@115
    88
        if (arr != null) {
jaroslav@115
    89
            for (File f : arr) {
jaroslav@115
    90
                deleteRec(f);
jaroslav@115
    91
            }
jaroslav@115
    92
        }
jaroslav@115
    93
        dir.delete();
jaroslav@115
    94
    }
jaroslav@115
    95
jaroslav@115
    96
    @Test public void testCreateAGame() throws Exception {
jaroslav@258
    97
        String logJarda = resource().path("login").
jaroslav@115
    98
            queryParam("name", "Jarda").
jaroslav@115
    99
            queryParam("password", "heslo").
jaroslav@115
   100
            accept(MediaType.TEXT_PLAIN).
jaroslav@115
   101
            put(String.class);
jaroslav@258
   102
        String logJirka = resource().path("login").
jaroslav@115
   103
            queryParam("name", "Jirka").
jaroslav@115
   104
            queryParam("password", "pesko").
jaroslav@115
   105
            accept(MediaType.TEXT_PLAIN).
jaroslav@115
   106
            put(String.class);
jaroslav@258
   107
    GameId s = resource().path("games").queryParam("loginID", logJarda).
jaroslav@115
   108
                queryParam("white", "Jarda")
jaroslav@115
   109
                .queryParam("black", "Jirka").post(GameId.class);
jaroslav@115
   110
jaroslav@115
   111
        Thread.sleep(100);
jaroslav@115
   112
        final long now = System.currentTimeMillis();
jaroslav@115
   113
        if (s.getModified() >= now) {
jaroslav@115
   114
            fail("The game is supposed to be modified in past");
jaroslav@115
   115
        }
jaroslav@115
   116
        Thread.sleep(100);
jaroslav@115
   117
jtulach@117
   118
        ResourceBundle b = ResourceBundle.getBundle("cz/xelfi/quoridor/webidor/TestBundle");
jtulach@117
   119
        String comment = b.getString("COMMENT");
jtulach@117
   120
jaroslav@258
   121
        GameId s1 = resource().path("games/" + s.getId()).
jaroslav@115
   122
            queryParam("loginID", logJarda).
jaroslav@115
   123
            queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
jaroslav@115
   124
jaroslav@258
   125
        GameId comment1 = resource().path("games/" + s.getId()).
jaroslav@115
   126
            queryParam("loginID", logJarda).
jaroslav@115
   127
            queryParam("player", "Jarda").queryParam("comment", "I like this game!").put(GameId.class);
jaroslav@130
   128
        assertEquals("One comment in the game", 1, comment1.getComments());
jaroslav@115
   129
jaroslav@258
   130
        GameId comment2 = resource().path("games/" + s.getId()).
jaroslav@115
   131
            queryParam("loginID", logJirka).
jtulach@117
   132
            queryParam("player", "Jirka").queryParam("comment", comment).put(GameId.class);
jaroslav@130
   133
        assertEquals("Snd comment in the game", 2, comment2.getComments());
jaroslav@130
   134
jaroslav@258
   135
        GameId s2 = resource().path("games/" + s.getId()).
jaroslav@115
   136
            queryParam("loginID", logJirka).
jaroslav@115
   137
            queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
jaroslav@115
   138
        assertNotNull("Successful move", s2);
jaroslav@130
   139
        assertEquals("Still two comments in the game", 2, s2.getComments());
jaroslav@115
   140
jaroslav@115
   141
        File game = new File(new File(dir, "games"), s1.getId());
jaroslav@115
   142
        assertTrue("File for game exists", game.exists());
jaroslav@115
   143
        String content = readFile(game);
jaroslav@115
   144
jaroslav@115
   145
        if (!content.contains("# white: Jarda")) {
jaroslav@115
   146
            fail(content);
jaroslav@115
   147
        }
jaroslav@115
   148
        if (!content.contains("# black: Jirka")) {
jaroslav@115
   149
            fail(content);
jaroslav@115
   150
        }
jaroslav@115
   151
        if (!content.contains("N")) {
jaroslav@115
   152
            fail(content);
jaroslav@115
   153
        }
jaroslav@115
   154
        if (!content.contains("... S")) {
jaroslav@115
   155
            fail(content);
jaroslav@115
   156
        }
jaroslav@115
   157
        if (!content.contains("I like")) {
jaroslav@115
   158
            fail(content);
jaroslav@115
   159
        }
jtulach@117
   160
        if (!content.contains(comment)) {
jaroslav@115
   161
            fail(content);
jaroslav@115
   162
        }
jaroslav@115
   163
jaroslav@115
   164
        Games read = new Games(new File(dir, "games"), new Quoridor());
jaroslav@115
   165
        List<Game> readGames = read.getGames();
jaroslav@115
   166
        assertEquals("One game read", 1, readGames.size());
jaroslav@115
   167
        Board board = readGames.get(0).getBoard();
jaroslav@115
   168
        assertEquals(1, board.getPlayers().get(0).getRow());
jaroslav@115
   169
        assertEquals(7, board.getPlayers().get(1).getRow());
jaroslav@115
   170
        assertEquals(Move.NORTH, readGames.get(0).getMoves().get(0).getMove());
jaroslav@115
   171
        assertEquals(Move.SOUTH, readGames.get(0).getMoves().get(1).getMove());
jaroslav@115
   172
jaroslav@115
   173
        Game rg = readGames.get(0);
jaroslav@115
   174
        assertNull("No comments on second move", rg.getMoves().get(1).getComments());
jaroslav@115
   175
        List<Note> cmnts = rg.getMoves().get(0).getComments();
jaroslav@115
   176
        assertNotNull("Some comments on first move", cmnts);
jaroslav@115
   177
        assertEquals("Two comments: " + cmnts, 2, cmnts.size());
jaroslav@115
   178
jaroslav@115
   179
        if (!cmnts.get(0).getComment().contains("I like")) {
jaroslav@115
   180
            fail();
jaroslav@115
   181
        }
jtulach@117
   182
        if (!cmnts.get(1).getComment().contains(comment)) {
jaroslav@115
   183
            fail();
jaroslav@115
   184
        }
jaroslav@115
   185
jaroslav@121
   186
        Thread.sleep(1500);
jaroslav@121
   187
jaroslav@115
   188
        File tmp = File.createTempFile("test-board", "game");
jaroslav@115
   189
        read.storeGame(rg, tmp);
jaroslav@115
   190
jaroslav@130
   191
        String sss1 = readFile(game).replaceAll("@.*:", "date");
jaroslav@121
   192
        Thread.sleep(1500);
jaroslav@121
   193
jaroslav@130
   194
        assertEquals("Newly written file remains the same", sss1, readFile(tmp).replaceAll("@.*:", "date"));
jaroslav@115
   195
jaroslav@258
   196
        String sGame = resource().path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(String.class);
jaroslav@258
   197
        Game readGame = resource().path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
jaroslav@115
   198
        assertNotNull("Game really returned", readGame);
jaroslav@115
   199
jaroslav@115
   200
        assertEquals(Move.NORTH, readGame.getMoves().get(0).getMove());
jaroslav@115
   201
        assertEquals(Move.SOUTH, readGame.getMoves().get(1).getMove());
jaroslav@115
   202
jaroslav@115
   203
        assertNull("No comments on second move", readGame.getMoves().get(1).getComments());
jaroslav@115
   204
        cmnts = readGame.getMoves().get(0).getComments();
jaroslav@115
   205
        assertNotNull("Some comments on first move", cmnts);
jaroslav@115
   206
        assertEquals("Two comments: " + cmnts, 2, cmnts.size());
jaroslav@115
   207
        if (!cmnts.get(0).getComment().contains("I like")) {
jaroslav@115
   208
            fail();
jaroslav@115
   209
        }
jtulach@117
   210
        if (!cmnts.get(1).getComment().contains(comment)) {
jaroslav@115
   211
            fail();
jaroslav@115
   212
        }
jaroslav@115
   213
    }
jaroslav@115
   214
jaroslav@115
   215
    private String readFile(File game) throws IOException, FileNotFoundException {
jaroslav@115
   216
        char[] arr = new char[4096];
jaroslav@118
   217
        Reader gameContent = new InputStreamReader(new FileInputStream(game), "UTF-8");
jaroslav@115
   218
        int len = gameContent.read(arr);
jaroslav@115
   219
        String content = new String(arr, 0, len);
jaroslav@115
   220
        return content;
jaroslav@115
   221
    }
jaroslav@115
   222
jaroslav@115
   223
}