jaroslav@52
|
1 |
/*
|
jaroslav@264
|
2 |
* Quoridor server and related libraries
|
jaroslav@264
|
3 |
* Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@52
|
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@52
|
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@52
|
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@52
|
17 |
*/
|
jaroslav@52
|
18 |
|
jaroslav@52
|
19 |
package cz.xelfi.quoridor.webidor;
|
jaroslav@52
|
20 |
|
jaroslav@52
|
21 |
import cz.xelfi.quoridor.webidor.resources.Games;
|
jtulach@82
|
22 |
import cz.xelfi.quoridor.webidor.resources.Quoridor;
|
jaroslav@52
|
23 |
import java.io.File;
|
jaroslav@52
|
24 |
import java.io.FileOutputStream;
|
jaroslav@52
|
25 |
import java.io.IOException;
|
jtulach@117
|
26 |
import java.util.ResourceBundle;
|
jaroslav@52
|
27 |
import org.junit.After;
|
jaroslav@52
|
28 |
import org.junit.Before;
|
jaroslav@52
|
29 |
import org.junit.Test;
|
jaroslav@52
|
30 |
import static org.junit.Assert.*;
|
jaroslav@52
|
31 |
|
jaroslav@52
|
32 |
/**
|
jaroslav@52
|
33 |
*
|
jaroslav@52
|
34 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@52
|
35 |
*/
|
jaroslav@52
|
36 |
public class GamesTest extends Object {
|
jaroslav@149
|
37 |
static {
|
jaroslav@149
|
38 |
System.setProperty("JERSEY_HTTP_PORT", "33436");
|
jaroslav@149
|
39 |
}
|
jaroslav@52
|
40 |
private File dir;
|
jaroslav@52
|
41 |
|
jaroslav@52
|
42 |
@Before
|
jaroslav@52
|
43 |
public void setUp() throws Exception {
|
jaroslav@52
|
44 |
dir = File.createTempFile("quoridor", ".dir");
|
jaroslav@52
|
45 |
dir.delete();
|
jaroslav@52
|
46 |
System.setProperty("quoridor.dir", dir.getPath());
|
jaroslav@52
|
47 |
}
|
jaroslav@52
|
48 |
|
jaroslav@52
|
49 |
@After
|
jaroslav@52
|
50 |
public void tearDown() throws Exception {
|
jaroslav@52
|
51 |
deleteRec(dir);
|
jaroslav@52
|
52 |
}
|
jaroslav@52
|
53 |
|
jaroslav@52
|
54 |
static void deleteRec(File dir) throws IOException {
|
jaroslav@52
|
55 |
if (dir == null) {
|
jaroslav@52
|
56 |
return;
|
jaroslav@52
|
57 |
}
|
jaroslav@52
|
58 |
File[] arr = dir.listFiles();
|
jaroslav@52
|
59 |
if (arr != null) {
|
jaroslav@52
|
60 |
for (File f : arr) {
|
jaroslav@52
|
61 |
deleteRec(f);
|
jaroslav@52
|
62 |
}
|
jaroslav@52
|
63 |
}
|
jaroslav@52
|
64 |
dir.delete();
|
jaroslav@52
|
65 |
}
|
jaroslav@52
|
66 |
|
jaroslav@52
|
67 |
@Test public void testCreateAGame() throws Exception {
|
jaroslav@52
|
68 |
File f = new File(dir, "x");
|
jaroslav@52
|
69 |
f.getParentFile().mkdirs();
|
jaroslav@52
|
70 |
FileOutputStream os = new FileOutputStream(f);
|
jtulach@79
|
71 |
os.write("# white: W\n# black: B\n# status: IN_PROGRESS\nN S\n\n".getBytes("UTF-8"));
|
jaroslav@52
|
72 |
os.close();
|
jaroslav@52
|
73 |
|
jtulach@79
|
74 |
Thread.sleep(1000);
|
jtulach@79
|
75 |
|
jtulach@79
|
76 |
long middle = f.lastModified();
|
jtulach@79
|
77 |
|
jtulach@79
|
78 |
Thread.sleep(1000);
|
jtulach@79
|
79 |
|
jtulach@82
|
80 |
Games games = new Games(dir, new Quoridor());
|
jaroslav@162
|
81 |
Game g = games.getBoardInfo("", "x", -1);
|
jaroslav@52
|
82 |
assertNotNull("Game found", g);
|
jaroslav@52
|
83 |
assertNotNull("Board found", g.getBoard());
|
jtulach@79
|
84 |
assertEquals("List of moves has two", 2, g.getMoves().size());
|
jtulach@79
|
85 |
|
jtulach@79
|
86 |
assertEquals("Last move is last touch of the file", middle, g.getId().getModified());
|
jaroslav@52
|
87 |
}
|
jaroslav@52
|
88 |
|
jaroslav@114
|
89 |
@Test public void testLoadGameWithComments() throws Exception {
|
jaroslav@114
|
90 |
File f = new File(dir, "x");
|
jaroslav@114
|
91 |
f.getParentFile().mkdirs();
|
jaroslav@114
|
92 |
FileOutputStream os = new FileOutputStream(f);
|
jaroslav@114
|
93 |
os.write("# white: W\n# black: B\n# status: IN_PROGRESS\nN #good move\n... S # ok move\n\n".getBytes("UTF-8"));
|
jaroslav@114
|
94 |
os.close();
|
jaroslav@114
|
95 |
|
jaroslav@114
|
96 |
Thread.sleep(1000);
|
jaroslav@114
|
97 |
|
jaroslav@114
|
98 |
long middle = f.lastModified();
|
jaroslav@114
|
99 |
|
jaroslav@114
|
100 |
Thread.sleep(1000);
|
jaroslav@114
|
101 |
|
jaroslav@114
|
102 |
Games games = new Games(dir, new Quoridor());
|
jaroslav@162
|
103 |
Game g = games.getBoardInfo("", "x", -1);
|
jaroslav@114
|
104 |
assertNotNull("Game found", g);
|
jaroslav@114
|
105 |
assertNotNull("Board found", g.getBoard());
|
jaroslav@114
|
106 |
assertEquals("List of moves has two", 2, g.getMoves().size());
|
jaroslav@114
|
107 |
|
jaroslav@114
|
108 |
assertEquals("Last move is last touch of the file", middle, g.getId().getModified());
|
jaroslav@114
|
109 |
}
|
jaroslav@114
|
110 |
|
jtulach@117
|
111 |
@Test public void testLoadGameWithInternationalComments() throws Exception {
|
jtulach@117
|
112 |
File f = new File(dir, "x");
|
jtulach@117
|
113 |
f.getParentFile().mkdirs();
|
jtulach@117
|
114 |
FileOutputStream os = new FileOutputStream(f);
|
jtulach@117
|
115 |
ResourceBundle b = ResourceBundle.getBundle("cz/xelfi/quoridor/webidor/TestBundle");
|
jtulach@117
|
116 |
String comment = b.getString("COMMENT");
|
jtulach@117
|
117 |
os.write(("# white: W\n# black: B\n# status: IN_PROGRESS\nN\n#" +
|
jtulach@117
|
118 |
comment + "\n... S # ok move\n\n").getBytes("UTF-8"));
|
jtulach@117
|
119 |
os.close();
|
jtulach@117
|
120 |
|
jtulach@117
|
121 |
Thread.sleep(1000);
|
jtulach@117
|
122 |
|
jtulach@117
|
123 |
long middle = f.lastModified();
|
jtulach@117
|
124 |
|
jtulach@117
|
125 |
Thread.sleep(1000);
|
jtulach@117
|
126 |
|
jtulach@117
|
127 |
Games games = new Games(dir, new Quoridor());
|
jaroslav@162
|
128 |
Game g = games.getBoardInfo("", "x", -1);
|
jtulach@117
|
129 |
assertNotNull("Game found", g);
|
jtulach@117
|
130 |
assertNotNull("Board found", g.getBoard());
|
jtulach@117
|
131 |
assertEquals("List of moves has two", 2, g.getMoves().size());
|
jtulach@117
|
132 |
String commentRead = g.getMoves().get(0).getComments().get(0).getComment();
|
jtulach@117
|
133 |
assertEquals(comment, commentRead);
|
jtulach@117
|
134 |
|
jtulach@117
|
135 |
assertEquals("Last move is last touch of the file", middle, g.getId().getModified());
|
jtulach@117
|
136 |
}
|
jtulach@117
|
137 |
|
jaroslav@52
|
138 |
}
|