1.1 --- a/webidor/src/test/java/cz/xelfi/quoridor/webidor/FinishedGameTest.java Sun Apr 25 21:20:09 2010 +0200
1.2 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/FinishedGameTest.java Sun Sep 12 00:06:44 2010 +0200
1.3 @@ -26,6 +26,8 @@
1.4
1.5 package cz.xelfi.quoridor.webidor;
1.6
1.7 +import com.sun.jersey.test.framework.WebAppDescriptor;
1.8 +import com.sun.jersey.test.framework.AppDescriptor;
1.9 import java.util.Properties;
1.10 import com.sun.jersey.api.client.GenericType;
1.11 import com.sun.jersey.api.client.UniformInterfaceException;
1.12 @@ -47,22 +49,22 @@
1.13 System.setProperty("JERSEY_HTTP_PORT", "33435");
1.14 }
1.15 private File dir;
1.16 -
1.17 - public FinishedGameTest() throws Exception {
1.18 - super("cz.xelfi.quoridor.webidor.resources");
1.19 - }
1.20 -
1.21 +
1.22 @Override
1.23 - public void setUp() throws Exception {
1.24 - dir = File.createTempFile("quoridor", ".dir");
1.25 - dir.delete();
1.26 - System.setProperty("quoridor.dir", dir.getPath());
1.27 - dir.mkdirs();
1.28 - File passwd = new File(dir, "passwd");
1.29 - FileOutputStream os = new FileOutputStream(passwd);
1.30 - os.write("Jarda=heslo\nJirka=pesko\nMaster=mr\n".getBytes("UTF-8"));
1.31 - os.close();
1.32 - super.setUp();
1.33 + protected AppDescriptor configure() {
1.34 + try {
1.35 + dir = File.createTempFile("quoridor", ".dir");
1.36 + dir.delete();
1.37 + System.setProperty("quoridor.dir", dir.getPath());
1.38 + dir.mkdirs();
1.39 + File passwd = new File(dir, "passwd");
1.40 + FileOutputStream os = new FileOutputStream(passwd);
1.41 + os.write("Jarda=heslo\nJirka=pesko\nMaster=mr\n".getBytes("UTF-8"));
1.42 + os.close();
1.43 + } catch (Exception ex) {
1.44 + throw new IllegalStateException(ex);
1.45 + }
1.46 + return new WebAppDescriptor.Builder("cz.xelfi.quoridor.webidor.resources").build();
1.47 }
1.48
1.49 @Override
1.50 @@ -84,101 +86,101 @@
1.51 dir.delete();
1.52 }
1.53 @Test public void testNotLoggedIn() {
1.54 - String status = webResource.path("login").queryParam("id", "not-logged-in").
1.55 + String status = resource().path("login").queryParam("id", "not-logged-in").
1.56 accept(MediaType.TEXT_PLAIN).get(String.class);
1.57 assertEquals("Nobody is logged in", "", status);
1.58 }
1.59
1.60
1.61 @Test public void testCreateAGame() throws Exception {
1.62 - String logJarda = webResource.path("login").
1.63 + String logJarda = resource().path("login").
1.64 queryParam("name", "Jarda").
1.65 queryParam("password", "heslo").
1.66 accept(MediaType.TEXT_PLAIN).
1.67 put(String.class);
1.68 - String logJirka = webResource.path("login").
1.69 + String logJirka = resource().path("login").
1.70 queryParam("name", "Jirka").
1.71 queryParam("password", "pesko").
1.72 accept(MediaType.TEXT_PLAIN).
1.73 put(String.class);
1.74 assertNotNull("Logged in ok", logJarda);
1.75 - GameId s = webResource.path("games").queryParam("white", "Jarda")
1.76 + GameId s = resource().path("games").queryParam("white", "Jarda")
1.77 .queryParam("loginID", logJarda)
1.78 .queryParam("black", "Jirka").post(GameId.class);
1.79
1.80 for (int i = 0; i < 3; i++) {
1.81 - webResource.path("games/" + s.getId())
1.82 + resource().path("games/" + s.getId())
1.83 .queryParam("loginID", logJarda)
1.84 .queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
1.85 - webResource.path("games/" + s.getId())
1.86 + resource().path("games/" + s.getId())
1.87 .queryParam("loginID", logJirka)
1.88 .queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
1.89 }
1.90
1.91 - webResource.path("games/" + s.getId())
1.92 + resource().path("games/" + s.getId())
1.93 .queryParam("loginID", logJarda)
1.94 .queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
1.95 - webResource.path("games/" + s.getId())
1.96 + resource().path("games/" + s.getId())
1.97 .queryParam("loginID", logJirka)
1.98 .queryParam("player", "Jirka").queryParam("move", "SS").put(GameId.class);
1.99
1.100
1.101 GenericType<List<GameId>> gType = new GenericType<List<GameId>>() {};
1.102 - List<GameId> nothing = webResource.path("games/").queryParam("status", "blackWon").accept(MediaType.TEXT_XML).get(gType);
1.103 + List<GameId> nothing = resource().path("games/").queryParam("status", "blackWon").accept(MediaType.TEXT_XML).get(gType);
1.104 assertTrue("Nothing has been finished yet: " + nothing, nothing.isEmpty());
1.105
1.106 for (int i = 0; i < 3; i++) {
1.107 - webResource.path("games/" + s.getId())
1.108 + resource().path("games/" + s.getId())
1.109 .queryParam("loginID", logJarda)
1.110 .queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
1.111 - webResource.path("games/" + s.getId())
1.112 + resource().path("games/" + s.getId())
1.113 .queryParam("loginID", logJirka)
1.114 .queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
1.115 }
1.116
1.117
1.118 try {
1.119 - Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
1.120 + Game end = resource().path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
1.121 fail("If the game is finished, one cannot get its status without login");
1.122 } catch (UniformInterfaceException ex) {
1.123 // OK
1.124 }
1.125 - Game end = webResource.path("games/" + s.getId()).queryParam("loginID", logJirka).accept(MediaType.TEXT_XML).get(Game.class);
1.126 + Game end = resource().path("games/" + s.getId()).queryParam("loginID", logJirka).accept(MediaType.TEXT_XML).get(Game.class);
1.127 assertEquals("BlackWins", GameStatus.blackWon, end.getId().getStatus());
1.128
1.129 assertEquals("Jirka wins", "Jirka", end.getCurrentPlayer());
1.130
1.131 - List<GameId> none = webResource.path("games/").queryParam("status", "blackWon").accept(MediaType.TEXT_XML).get(gType);
1.132 + List<GameId> none = resource().path("games/").queryParam("status", "blackWon").accept(MediaType.TEXT_XML).get(gType);
1.133 assertEquals("No games, for not logged in users: " + none, 0, none.size());
1.134
1.135 - List<GameId> something = webResource.path("games/").queryParam("loginID", logJirka).queryParam("status", "blackWon").accept(MediaType.TEXT_XML).get(gType);
1.136 + List<GameId> something = resource().path("games/").queryParam("loginID", logJirka).queryParam("status", "blackWon").accept(MediaType.TEXT_XML).get(gType);
1.137 assertEquals("One game finished: " + something, 1, something.size());
1.138 assertEquals("Id is OK", end.getId().getId(), something.get(0).getId());
1.139 }
1.140
1.141 @Test public void testResignAGame() throws Exception {
1.142 - String logJarda = webResource.path("login").
1.143 + String logJarda = resource().path("login").
1.144 queryParam("name", "Jarda").
1.145 queryParam("password", "heslo").
1.146 accept(MediaType.TEXT_PLAIN).
1.147 put(String.class);
1.148 - GameId s = webResource.path("games").queryParam("white", "Jarda")
1.149 + GameId s = resource().path("games").queryParam("white", "Jarda")
1.150 .queryParam("loginID", logJarda)
1.151 .queryParam("black", "Jirka").post(GameId.class);
1.152
1.153 assertTrue("In progress", s.getStatus().isInProgress());
1.154
1.155 - webResource.path("games/" + s.getId()).
1.156 + resource().path("games/" + s.getId()).
1.157 queryParam("loginID", logJarda).
1.158 queryParam("player", "Jarda").
1.159 queryParam("move", "RESIGN").put(GameId.class);
1.160 try {
1.161 - Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
1.162 + Game end = resource().path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
1.163 fail("Should not be able to get game when finished");
1.164 } catch (UniformInterfaceException ex) {
1.165 // OK
1.166 }
1.167 - Game end = webResource.path("games/" + s.getId()).queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(Game.class);
1.168 + Game end = resource().path("games/" + s.getId()).queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(Game.class);
1.169 assertEquals("BlackWins", GameStatus.blackWon, end.getId().getStatus());
1.170 assertEquals("Jirka wins", "Jirka", end.getCurrentPlayer());
1.171
1.172 @@ -186,40 +188,40 @@
1.173 }
1.174
1.175 @Test public void testResignBGame() throws Exception {
1.176 - String logJarda = webResource.path("login").
1.177 + String logJarda = resource().path("login").
1.178 queryParam("name", "Jarda").
1.179 queryParam("password", "heslo").
1.180 accept(MediaType.TEXT_PLAIN).
1.181 put(String.class);
1.182 - String logJirka = webResource.path("login").
1.183 + String logJirka = resource().path("login").
1.184 queryParam("name", "Jirka").
1.185 queryParam("password", "pesko").
1.186 accept(MediaType.TEXT_PLAIN).
1.187 put(String.class);
1.188
1.189 - GameId s = webResource.path("games").queryParam("white", "Jarda")
1.190 + GameId s = resource().path("games").queryParam("white", "Jarda")
1.191 .queryParam("loginID", logJarda)
1.192 .queryParam("black", "Jirka").post(GameId.class);
1.193
1.194 assertTrue("In progress", s.getStatus().isInProgress());
1.195
1.196 - webResource.path("games/" + s.getId()).
1.197 + resource().path("games/" + s.getId()).
1.198 queryParam("loginID", logJarda).
1.199 queryParam("player", "Jarda").
1.200 queryParam("move", "N").put(GameId.class);
1.201 - webResource.path("games/" + s.getId()).
1.202 + resource().path("games/" + s.getId()).
1.203 queryParam("loginID", logJirka).
1.204 queryParam("player", "Jirka").
1.205 queryParam("move", "RESIGN").put(GameId.class);
1.206
1.207
1.208 try {
1.209 - Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
1.210 + Game end = resource().path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
1.211 fail("Should not be able to get game when finished");
1.212 } catch (UniformInterfaceException ex) {
1.213 // OK
1.214 }
1.215 - Game end = webResource.path("games/" + s.getId()).queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(Game.class);
1.216 + Game end = resource().path("games/" + s.getId()).queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(Game.class);
1.217 assertEquals("WhiteWins", GameStatus.whiteWon, end.getId().getStatus());
1.218 assertEquals("Jarda wins", "Jarda", end.getCurrentPlayer());
1.219
1.220 @@ -227,12 +229,12 @@
1.221 }
1.222
1.223 @Test public void testResignForeignGame() throws Exception {
1.224 - String logJarda = webResource.path("login").
1.225 + String logJarda = resource().path("login").
1.226 queryParam("name", "Jarda").
1.227 queryParam("password", "heslo").
1.228 accept(MediaType.TEXT_PLAIN).
1.229 put(String.class);
1.230 - GameId s = webResource.path("games").queryParam("white", "Jarda")
1.231 + GameId s = resource().path("games").queryParam("white", "Jarda")
1.232 .queryParam("loginID", logJarda)
1.233 .queryParam("black", "Jirka").post(GameId.class);
1.234 File usersDir = new File(dir, "users");
1.235 @@ -246,23 +248,23 @@
1.236 }
1.237
1.238 assertTrue("In progress", s.getStatus().isInProgress());
1.239 - String logMaster = webResource.path("login").
1.240 + String logMaster = resource().path("login").
1.241 queryParam("name", "Master").
1.242 queryParam("password", "mr").
1.243 accept(MediaType.TEXT_PLAIN).
1.244 put(String.class);
1.245
1.246 - webResource.path("games/" + s.getId()).
1.247 + resource().path("games/" + s.getId()).
1.248 queryParam("loginID", logMaster).
1.249 queryParam("player", "Jarda").
1.250 queryParam("move", "RESIGN").put(GameId.class);
1.251 try {
1.252 - Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
1.253 + Game end = resource().path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
1.254 fail("Should not be able to get game when finished");
1.255 } catch (UniformInterfaceException ex) {
1.256 // OK
1.257 }
1.258 - Game end = webResource.path("games/" + s.getId()).queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(Game.class);
1.259 + Game end = resource().path("games/" + s.getId()).queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(Game.class);
1.260 assertEquals("BlackWins", GameStatus.blackWon, end.getId().getStatus());
1.261 assertEquals("Jirka wins", "Jirka", end.getCurrentPlayer());
1.262