2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * The contents of this file are subject to the terms of either the GNU
5 * General Public License Version 2 only ("GPL") or the Common
6 * Development and Distribution License("CDDL") (collectively, the
7 * "License"). You may not use this file except in compliance with the
8 * License. You can obtain a copy of the License at
9 * http://www.netbeans.org/cddl-gplv2.html
10 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
11 * specific language governing permissions and limitations under the
12 * License. When distributing the software, include this License Header
13 * Notice in each file and include the License file at
14 * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
15 * particular file as subject to the "Classpath" exception as provided
16 * by Sun in the GPL Version 2 section of the License file that
17 * accompanied this code. If applicable, add the following below the
18 * License Header, with the fields enclosed by brackets [] replaced by
19 * your own identifying information:
20 * "Portions Copyrighted [year] [name of copyright owner]"
24 * Portions Copyrighted 2009 Jaroslav Tulach
27 package cz.xelfi.quoridor.webidor.resources;
29 import com.sun.jersey.test.framework.WebAppDescriptor;
30 import com.sun.jersey.test.framework.AppDescriptor;
31 import com.sun.jersey.test.framework.JerseyTest;
32 import cz.xelfi.quoridor.Board;
33 import cz.xelfi.quoridor.Move;
34 import cz.xelfi.quoridor.webidor.Game;
35 import cz.xelfi.quoridor.webidor.GameId;
36 import cz.xelfi.quoridor.webidor.Note;
38 import java.io.FileInputStream;
39 import java.io.FileNotFoundException;
40 import java.io.FileOutputStream;
41 import java.io.IOException;
42 import java.io.InputStreamReader;
43 import java.io.Reader;
44 import java.util.List;
45 import java.util.ResourceBundle;
46 import javax.ws.rs.core.MediaType;
47 import org.junit.Test;
48 import static org.junit.Assert.*;
52 * @author Jaroslav Tulach <jtulach@netbeans.org>
54 public class ChatTest extends JerseyTest {
56 System.setProperty("JERSEY_HTTP_PORT", "33437");
61 protected AppDescriptor configure() {
63 dir = File.createTempFile("quoridor", ".dir");
65 System.setProperty("quoridor.dir", dir.getPath());
67 File passwd = new File(dir, "passwd");
68 FileOutputStream os = new FileOutputStream(passwd);
69 os.write("Jarda=heslo\nJirka=pesko\n".getBytes("UTF-8"));
71 } catch (Exception ex) {
72 throw new IllegalStateException(ex);
74 return new WebAppDescriptor.Builder("cz.xelfi.quoridor.webidor.resources").build();
78 public void tearDown() throws Exception {
83 static void deleteRec(File dir) throws IOException {
87 File[] arr = dir.listFiles();
96 @Test public void testCreateAGame() throws Exception {
97 String logJarda = resource().path("login").
98 queryParam("name", "Jarda").
99 queryParam("password", "heslo").
100 accept(MediaType.TEXT_PLAIN).
102 String logJirka = resource().path("login").
103 queryParam("name", "Jirka").
104 queryParam("password", "pesko").
105 accept(MediaType.TEXT_PLAIN).
107 GameId s = resource().path("games").queryParam("loginID", logJarda).
108 queryParam("white", "Jarda")
109 .queryParam("black", "Jirka").post(GameId.class);
112 final long now = System.currentTimeMillis();
113 if (s.getModified() >= now) {
114 fail("The game is supposed to be modified in past");
118 ResourceBundle b = ResourceBundle.getBundle("cz/xelfi/quoridor/webidor/TestBundle");
119 String comment = b.getString("COMMENT");
121 GameId s1 = resource().path("games/" + s.getId()).
122 queryParam("loginID", logJarda).
123 queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
125 GameId comment1 = resource().path("games/" + s.getId()).
126 queryParam("loginID", logJarda).
127 queryParam("player", "Jarda").queryParam("comment", "I like this game!").put(GameId.class);
128 assertEquals("One comment in the game", 1, comment1.getComments());
130 GameId comment2 = resource().path("games/" + s.getId()).
131 queryParam("loginID", logJirka).
132 queryParam("player", "Jirka").queryParam("comment", comment).put(GameId.class);
133 assertEquals("Snd comment in the game", 2, comment2.getComments());
135 GameId s2 = resource().path("games/" + s.getId()).
136 queryParam("loginID", logJirka).
137 queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
138 assertNotNull("Successful move", s2);
139 assertEquals("Still two comments in the game", 2, s2.getComments());
141 File game = new File(new File(dir, "games"), s1.getId());
142 assertTrue("File for game exists", game.exists());
143 String content = readFile(game);
145 if (!content.contains("# white: Jarda")) {
148 if (!content.contains("# black: Jirka")) {
151 if (!content.contains("N")) {
154 if (!content.contains("... S")) {
157 if (!content.contains("I like")) {
160 if (!content.contains(comment)) {
164 Games read = new Games(new File(dir, "games"), new Quoridor());
165 List<Game> readGames = read.getGames();
166 assertEquals("One game read", 1, readGames.size());
167 Board board = readGames.get(0).getBoard();
168 assertEquals(1, board.getPlayers().get(0).getRow());
169 assertEquals(7, board.getPlayers().get(1).getRow());
170 assertEquals(Move.NORTH, readGames.get(0).getMoves().get(0).getMove());
171 assertEquals(Move.SOUTH, readGames.get(0).getMoves().get(1).getMove());
173 Game rg = readGames.get(0);
174 assertNull("No comments on second move", rg.getMoves().get(1).getComments());
175 List<Note> cmnts = rg.getMoves().get(0).getComments();
176 assertNotNull("Some comments on first move", cmnts);
177 assertEquals("Two comments: " + cmnts, 2, cmnts.size());
179 if (!cmnts.get(0).getComment().contains("I like")) {
182 if (!cmnts.get(1).getComment().contains(comment)) {
188 File tmp = File.createTempFile("test-board", "game");
189 read.storeGame(rg, tmp);
191 String sss1 = readFile(game).replaceAll("@.*:", "date");
194 assertEquals("Newly written file remains the same", sss1, readFile(tmp).replaceAll("@.*:", "date"));
196 String sGame = resource().path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(String.class);
197 Game readGame = resource().path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
198 assertNotNull("Game really returned", readGame);
200 assertEquals(Move.NORTH, readGame.getMoves().get(0).getMove());
201 assertEquals(Move.SOUTH, readGame.getMoves().get(1).getMove());
203 assertNull("No comments on second move", readGame.getMoves().get(1).getComments());
204 cmnts = readGame.getMoves().get(0).getComments();
205 assertNotNull("Some comments on first move", cmnts);
206 assertEquals("Two comments: " + cmnts, 2, cmnts.size());
207 if (!cmnts.get(0).getComment().contains("I like")) {
210 if (!cmnts.get(1).getComment().contains(comment)) {
215 private String readFile(File game) throws IOException, FileNotFoundException {
216 char[] arr = new char[4096];
217 Reader gameContent = new InputStreamReader(new FileInputStream(game), "UTF-8");
218 int len = gameContent.read(arr);
219 String content = new String(arr, 0, len);