jaroslav@57
|
1 |
/*
|
jaroslav@57
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
jaroslav@57
|
3 |
*
|
jaroslav@57
|
4 |
* The contents of this file are subject to the terms of either the GNU
|
jaroslav@57
|
5 |
* General Public License Version 2 only ("GPL") or the Common
|
jaroslav@57
|
6 |
* Development and Distribution License("CDDL") (collectively, the
|
jaroslav@57
|
7 |
* "License"). You may not use this file except in compliance with the
|
jaroslav@57
|
8 |
* License. You can obtain a copy of the License at
|
jaroslav@57
|
9 |
* http://www.netbeans.org/cddl-gplv2.html
|
jaroslav@57
|
10 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
|
jaroslav@57
|
11 |
* specific language governing permissions and limitations under the
|
jaroslav@57
|
12 |
* License. When distributing the software, include this License Header
|
jaroslav@57
|
13 |
* Notice in each file and include the License file at
|
jaroslav@57
|
14 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
|
jaroslav@57
|
15 |
* particular file as subject to the "Classpath" exception as provided
|
jaroslav@57
|
16 |
* by Sun in the GPL Version 2 section of the License file that
|
jaroslav@57
|
17 |
* accompanied this code. If applicable, add the following below the
|
jaroslav@57
|
18 |
* License Header, with the fields enclosed by brackets [] replaced by
|
jaroslav@57
|
19 |
* your own identifying information:
|
jaroslav@57
|
20 |
* "Portions Copyrighted [year] [name of copyright owner]"
|
jaroslav@57
|
21 |
*
|
jaroslav@57
|
22 |
* Contributor(s):
|
jaroslav@57
|
23 |
*
|
jaroslav@57
|
24 |
* Portions Copyrighted 2009 Jaroslav Tulach
|
jaroslav@57
|
25 |
*/
|
jaroslav@57
|
26 |
|
jaroslav@57
|
27 |
package cz.xelfi.quoridor.webidor;
|
jaroslav@57
|
28 |
|
jaroslav@57
|
29 |
import com.sun.jersey.test.framework.JerseyTest;
|
jaroslav@57
|
30 |
import java.io.File;
|
jtulach@82
|
31 |
import java.io.FileOutputStream;
|
jaroslav@57
|
32 |
import java.io.IOException;
|
jaroslav@57
|
33 |
import javax.ws.rs.core.MediaType;
|
jaroslav@57
|
34 |
import org.junit.Test;
|
jaroslav@57
|
35 |
import static org.junit.Assert.*;
|
jaroslav@57
|
36 |
|
jaroslav@57
|
37 |
/**
|
jaroslav@57
|
38 |
*
|
jaroslav@57
|
39 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@57
|
40 |
*/
|
jaroslav@57
|
41 |
public class FinishedGameTest extends JerseyTest {
|
jaroslav@57
|
42 |
private File dir;
|
jaroslav@57
|
43 |
|
jaroslav@57
|
44 |
public FinishedGameTest() throws Exception {
|
jaroslav@57
|
45 |
super("cz.xelfi.quoridor.webidor.resources");
|
jaroslav@57
|
46 |
}
|
jaroslav@57
|
47 |
|
jaroslav@57
|
48 |
@Override
|
jaroslav@57
|
49 |
public void setUp() throws Exception {
|
jaroslav@57
|
50 |
dir = File.createTempFile("quoridor", ".dir");
|
jaroslav@57
|
51 |
dir.delete();
|
jaroslav@57
|
52 |
System.setProperty("quoridor.dir", dir.getPath());
|
jtulach@82
|
53 |
dir.mkdirs();
|
jtulach@82
|
54 |
File passwd = new File(dir, "passwd");
|
jtulach@82
|
55 |
FileOutputStream os = new FileOutputStream(passwd);
|
jtulach@82
|
56 |
os.write("Jarda=heslo\nJirka=pesko\n".getBytes("UTF-8"));
|
jtulach@82
|
57 |
os.close();
|
jaroslav@57
|
58 |
super.setUp();
|
jaroslav@57
|
59 |
}
|
jaroslav@57
|
60 |
|
jaroslav@57
|
61 |
@Override
|
jaroslav@57
|
62 |
public void tearDown() throws Exception {
|
jaroslav@57
|
63 |
super.tearDown();
|
jaroslav@57
|
64 |
deleteRec(dir);
|
jaroslav@57
|
65 |
}
|
jaroslav@57
|
66 |
|
jaroslav@57
|
67 |
static void deleteRec(File dir) throws IOException {
|
jaroslav@57
|
68 |
if (dir == null) {
|
jaroslav@57
|
69 |
return;
|
jaroslav@57
|
70 |
}
|
jaroslav@57
|
71 |
File[] arr = dir.listFiles();
|
jaroslav@57
|
72 |
if (arr != null) {
|
jaroslav@57
|
73 |
for (File f : arr) {
|
jaroslav@57
|
74 |
deleteRec(f);
|
jaroslav@57
|
75 |
}
|
jaroslav@57
|
76 |
}
|
jaroslav@57
|
77 |
dir.delete();
|
jaroslav@57
|
78 |
}
|
jtulach@83
|
79 |
@Test public void testNotLoggedIn() {
|
jtulach@83
|
80 |
webResource = webResource.path("api");
|
jtulach@83
|
81 |
String status = webResource.path("login").queryParam("id", "not-logged-in").
|
jtulach@83
|
82 |
accept(MediaType.TEXT_PLAIN).get(String.class);
|
jtulach@83
|
83 |
assertEquals("Nobody is logged in", "", status);
|
jtulach@83
|
84 |
}
|
jtulach@83
|
85 |
|
jaroslav@57
|
86 |
|
jaroslav@57
|
87 |
@Test public void testCreateAGame() throws Exception {
|
jaroslav@57
|
88 |
webResource = webResource.path("api");
|
jtulach@82
|
89 |
String logJarda = webResource.path("login").
|
jtulach@82
|
90 |
queryParam("name", "Jarda").
|
jtulach@82
|
91 |
queryParam("password", "heslo").
|
jtulach@82
|
92 |
accept(MediaType.TEXT_PLAIN).
|
jtulach@82
|
93 |
put(String.class);
|
jtulach@82
|
94 |
String logJirka = webResource.path("login").
|
jtulach@82
|
95 |
queryParam("name", "Jirka").
|
jtulach@82
|
96 |
queryParam("password", "pesko").
|
jtulach@82
|
97 |
accept(MediaType.TEXT_PLAIN).
|
jtulach@82
|
98 |
put(String.class);
|
jtulach@82
|
99 |
assertNotNull("Logged in ok", logJarda);
|
jaroslav@57
|
100 |
GameId s = webResource.path("games").queryParam("white", "Jarda")
|
jtulach@82
|
101 |
.queryParam("loginID", logJarda)
|
jaroslav@57
|
102 |
.queryParam("black", "Jirka").post(GameId.class);
|
jaroslav@57
|
103 |
|
jaroslav@57
|
104 |
for (int i = 0; i < 3; i++) {
|
jtulach@82
|
105 |
webResource.path("games/" + s.getId())
|
jtulach@82
|
106 |
.queryParam("loginID", logJarda)
|
jtulach@82
|
107 |
.queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
|
jtulach@82
|
108 |
webResource.path("games/" + s.getId())
|
jtulach@82
|
109 |
.queryParam("loginID", logJirka)
|
jtulach@82
|
110 |
.queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
|
jaroslav@57
|
111 |
}
|
jaroslav@57
|
112 |
|
jtulach@82
|
113 |
webResource.path("games/" + s.getId())
|
jtulach@82
|
114 |
.queryParam("loginID", logJarda)
|
jtulach@82
|
115 |
.queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
|
jtulach@82
|
116 |
webResource.path("games/" + s.getId())
|
jtulach@82
|
117 |
.queryParam("loginID", logJirka)
|
jtulach@82
|
118 |
.queryParam("player", "Jirka").queryParam("move", "SS").put(GameId.class);
|
jaroslav@57
|
119 |
|
jaroslav@57
|
120 |
for (int i = 0; i < 3; i++) {
|
jtulach@82
|
121 |
webResource.path("games/" + s.getId())
|
jtulach@82
|
122 |
.queryParam("loginID", logJarda)
|
jtulach@82
|
123 |
.queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
|
jtulach@82
|
124 |
webResource.path("games/" + s.getId())
|
jtulach@82
|
125 |
.queryParam("loginID", logJirka)
|
jtulach@82
|
126 |
.queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
|
jaroslav@57
|
127 |
}
|
jaroslav@57
|
128 |
|
jaroslav@57
|
129 |
Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
|
jtulach@77
|
130 |
assertEquals("BlackWins", GameStatus.blackWon, end.getId().getStatus());
|
jaroslav@57
|
131 |
}
|
jaroslav@57
|
132 |
|
jtulach@75
|
133 |
@Test public void testResignAGame() throws Exception {
|
jtulach@75
|
134 |
webResource = webResource.path("api");
|
jtulach@82
|
135 |
String logJarda = webResource.path("login").
|
jtulach@82
|
136 |
queryParam("name", "Jarda").
|
jtulach@82
|
137 |
queryParam("password", "heslo").
|
jtulach@82
|
138 |
accept(MediaType.TEXT_PLAIN).
|
jtulach@82
|
139 |
put(String.class);
|
jtulach@75
|
140 |
GameId s = webResource.path("games").queryParam("white", "Jarda")
|
jtulach@82
|
141 |
.queryParam("loginID", logJarda)
|
jtulach@75
|
142 |
.queryParam("black", "Jirka").post(GameId.class);
|
jtulach@75
|
143 |
|
jtulach@82
|
144 |
webResource.path("games/" + s.getId()).
|
jtulach@82
|
145 |
queryParam("loginID", logJarda).
|
jtulach@82
|
146 |
queryParam("player", "Jarda").
|
jtulach@82
|
147 |
queryParam("move", "RESIGN").put(GameId.class);
|
jtulach@75
|
148 |
Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
|
jtulach@77
|
149 |
assertEquals("BlackWins", GameStatus.blackWon, end.getId().getStatus());
|
jtulach@75
|
150 |
}
|
jtulach@75
|
151 |
|
jaroslav@57
|
152 |
}
|