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 |
String logJarda = webResource.path("login").
|
jaroslav@115
|
93 |
queryParam("name", "Jarda").
|
jaroslav@115
|
94 |
queryParam("password", "heslo").
|
jaroslav@115
|
95 |
accept(MediaType.TEXT_PLAIN).
|
jaroslav@115
|
96 |
put(String.class);
|
jaroslav@115
|
97 |
String logJirka = webResource.path("login").
|
jaroslav@115
|
98 |
queryParam("name", "Jirka").
|
jaroslav@115
|
99 |
queryParam("password", "pesko").
|
jaroslav@115
|
100 |
accept(MediaType.TEXT_PLAIN).
|
jaroslav@115
|
101 |
put(String.class);
|
jaroslav@115
|
102 |
GameId s = webResource.path("games").queryParam("loginID", logJarda).
|
jaroslav@115
|
103 |
queryParam("white", "Jarda")
|
jaroslav@115
|
104 |
.queryParam("black", "Jirka").post(GameId.class);
|
jaroslav@115
|
105 |
|
jaroslav@115
|
106 |
Thread.sleep(100);
|
jaroslav@115
|
107 |
final long now = System.currentTimeMillis();
|
jaroslav@115
|
108 |
if (s.getModified() >= now) {
|
jaroslav@115
|
109 |
fail("The game is supposed to be modified in past");
|
jaroslav@115
|
110 |
}
|
jaroslav@115
|
111 |
Thread.sleep(100);
|
jaroslav@115
|
112 |
|
jtulach@117
|
113 |
ResourceBundle b = ResourceBundle.getBundle("cz/xelfi/quoridor/webidor/TestBundle");
|
jtulach@117
|
114 |
String comment = b.getString("COMMENT");
|
jtulach@117
|
115 |
|
jaroslav@115
|
116 |
GameId s1 = webResource.path("games/" + s.getId()).
|
jaroslav@115
|
117 |
queryParam("loginID", logJarda).
|
jaroslav@115
|
118 |
queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
|
jaroslav@115
|
119 |
|
jaroslav@115
|
120 |
GameId comment1 = webResource.path("games/" + s.getId()).
|
jaroslav@115
|
121 |
queryParam("loginID", logJarda).
|
jaroslav@115
|
122 |
queryParam("player", "Jarda").queryParam("comment", "I like this game!").put(GameId.class);
|
jaroslav@130
|
123 |
assertEquals("One comment in the game", 1, comment1.getComments());
|
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@130
|
128 |
assertEquals("Snd comment in the game", 2, comment2.getComments());
|
jaroslav@130
|
129 |
|
jaroslav@115
|
130 |
GameId s2 = webResource.path("games/" + s.getId()).
|
jaroslav@115
|
131 |
queryParam("loginID", logJirka).
|
jaroslav@115
|
132 |
queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
|
jaroslav@115
|
133 |
assertNotNull("Successful move", s2);
|
jaroslav@130
|
134 |
assertEquals("Still two comments in the game", 2, s2.getComments());
|
jaroslav@115
|
135 |
|
jaroslav@115
|
136 |
File game = new File(new File(dir, "games"), s1.getId());
|
jaroslav@115
|
137 |
assertTrue("File for game exists", game.exists());
|
jaroslav@115
|
138 |
String content = readFile(game);
|
jaroslav@115
|
139 |
|
jaroslav@115
|
140 |
if (!content.contains("# white: Jarda")) {
|
jaroslav@115
|
141 |
fail(content);
|
jaroslav@115
|
142 |
}
|
jaroslav@115
|
143 |
if (!content.contains("# black: Jirka")) {
|
jaroslav@115
|
144 |
fail(content);
|
jaroslav@115
|
145 |
}
|
jaroslav@115
|
146 |
if (!content.contains("N")) {
|
jaroslav@115
|
147 |
fail(content);
|
jaroslav@115
|
148 |
}
|
jaroslav@115
|
149 |
if (!content.contains("... S")) {
|
jaroslav@115
|
150 |
fail(content);
|
jaroslav@115
|
151 |
}
|
jaroslav@115
|
152 |
if (!content.contains("I like")) {
|
jaroslav@115
|
153 |
fail(content);
|
jaroslav@115
|
154 |
}
|
jtulach@117
|
155 |
if (!content.contains(comment)) {
|
jaroslav@115
|
156 |
fail(content);
|
jaroslav@115
|
157 |
}
|
jaroslav@115
|
158 |
|
jaroslav@115
|
159 |
Games read = new Games(new File(dir, "games"), new Quoridor());
|
jaroslav@115
|
160 |
List<Game> readGames = read.getGames();
|
jaroslav@115
|
161 |
assertEquals("One game read", 1, readGames.size());
|
jaroslav@115
|
162 |
Board board = readGames.get(0).getBoard();
|
jaroslav@115
|
163 |
assertEquals(1, board.getPlayers().get(0).getRow());
|
jaroslav@115
|
164 |
assertEquals(7, board.getPlayers().get(1).getRow());
|
jaroslav@115
|
165 |
assertEquals(Move.NORTH, readGames.get(0).getMoves().get(0).getMove());
|
jaroslav@115
|
166 |
assertEquals(Move.SOUTH, readGames.get(0).getMoves().get(1).getMove());
|
jaroslav@115
|
167 |
|
jaroslav@115
|
168 |
Game rg = readGames.get(0);
|
jaroslav@115
|
169 |
assertNull("No comments on second move", rg.getMoves().get(1).getComments());
|
jaroslav@115
|
170 |
List<Note> cmnts = rg.getMoves().get(0).getComments();
|
jaroslav@115
|
171 |
assertNotNull("Some comments on first move", cmnts);
|
jaroslav@115
|
172 |
assertEquals("Two comments: " + cmnts, 2, cmnts.size());
|
jaroslav@115
|
173 |
|
jaroslav@115
|
174 |
if (!cmnts.get(0).getComment().contains("I like")) {
|
jaroslav@115
|
175 |
fail();
|
jaroslav@115
|
176 |
}
|
jtulach@117
|
177 |
if (!cmnts.get(1).getComment().contains(comment)) {
|
jaroslav@115
|
178 |
fail();
|
jaroslav@115
|
179 |
}
|
jaroslav@115
|
180 |
|
jaroslav@121
|
181 |
Thread.sleep(1500);
|
jaroslav@121
|
182 |
|
jaroslav@115
|
183 |
File tmp = File.createTempFile("test-board", "game");
|
jaroslav@115
|
184 |
read.storeGame(rg, tmp);
|
jaroslav@115
|
185 |
|
jaroslav@130
|
186 |
String sss1 = readFile(game).replaceAll("@.*:", "date");
|
jaroslav@121
|
187 |
Thread.sleep(1500);
|
jaroslav@121
|
188 |
|
jaroslav@130
|
189 |
assertEquals("Newly written file remains the same", sss1, readFile(tmp).replaceAll("@.*:", "date"));
|
jaroslav@115
|
190 |
|
jaroslav@115
|
191 |
String sGame = webResource.path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(String.class);
|
jaroslav@115
|
192 |
Game readGame = webResource.path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
|
jaroslav@115
|
193 |
assertNotNull("Game really returned", readGame);
|
jaroslav@115
|
194 |
|
jaroslav@115
|
195 |
assertEquals(Move.NORTH, readGame.getMoves().get(0).getMove());
|
jaroslav@115
|
196 |
assertEquals(Move.SOUTH, readGame.getMoves().get(1).getMove());
|
jaroslav@115
|
197 |
|
jaroslav@115
|
198 |
assertNull("No comments on second move", readGame.getMoves().get(1).getComments());
|
jaroslav@115
|
199 |
cmnts = readGame.getMoves().get(0).getComments();
|
jaroslav@115
|
200 |
assertNotNull("Some comments on first move", cmnts);
|
jaroslav@115
|
201 |
assertEquals("Two comments: " + cmnts, 2, cmnts.size());
|
jaroslav@115
|
202 |
if (!cmnts.get(0).getComment().contains("I like")) {
|
jaroslav@115
|
203 |
fail();
|
jaroslav@115
|
204 |
}
|
jtulach@117
|
205 |
if (!cmnts.get(1).getComment().contains(comment)) {
|
jaroslav@115
|
206 |
fail();
|
jaroslav@115
|
207 |
}
|
jaroslav@115
|
208 |
}
|
jaroslav@115
|
209 |
|
jaroslav@115
|
210 |
private String readFile(File game) throws IOException, FileNotFoundException {
|
jaroslav@115
|
211 |
char[] arr = new char[4096];
|
jaroslav@118
|
212 |
Reader gameContent = new InputStreamReader(new FileInputStream(game), "UTF-8");
|
jaroslav@115
|
213 |
int len = gameContent.read(arr);
|
jaroslav@115
|
214 |
String content = new String(arr, 0, len);
|
jaroslav@115
|
215 |
return content;
|
jaroslav@115
|
216 |
}
|
jaroslav@115
|
217 |
|
jaroslav@115
|
218 |
}
|