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