2 * Quoridor server and related libraries
3 * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://www.gnu.org/licenses/.
19 package cz.xelfi.quoridor.webidor;
21 import com.sun.jersey.test.framework.WebAppDescriptor;
22 import com.sun.jersey.test.framework.AppDescriptor;
23 import com.sun.jersey.api.client.GenericType;
24 import java.util.List;
25 import com.sun.jersey.test.framework.JerseyTest;
27 import java.io.FileOutputStream;
28 import java.io.IOException;
29 import java.util.Properties;
30 import javax.ws.rs.core.MediaType;
31 import org.junit.Test;
32 import static org.junit.Assert.*;
36 * @author Jaroslav Tulach <jtulach@netbeans.org>
38 public class AllGamesTest extends JerseyTest {
40 System.setProperty("JERSEY_HTTP_PORT", "45420");
46 protected AppDescriptor configure() {
48 dir = File.createTempFile("quoridor", ".dir");
50 System.setProperty("quoridor.dir", dir.getPath());
52 File passwd = new File(dir, "passwd");
53 FileOutputStream os = new FileOutputStream(passwd);
54 os.write("Jarda=heslo\nJirka=pesko\nJan=pan\n".getBytes("UTF-8"));
56 } catch (Exception ex) {
57 throw new IllegalStateException(ex);
59 return new WebAppDescriptor.Builder("cz.xelfi.quoridor.webidor.resources").contextPath("allgamess").build();
63 public void tearDown() throws Exception {
67 static void deleteRec(File dir) throws IOException {
71 File[] arr = dir.listFiles();
79 @Test public void testListGames() throws Exception {
80 File usersDir = new File(dir, "users");
82 File fJarda = new File(usersDir, "Jarda");
84 Properties p = new Properties();
85 p.setProperty("email", "jar@da.cz");
86 p.setProperty("permission.games", "true");
87 p.store(new FileOutputStream(fJarda), "");
89 File fJirka = new File(usersDir, "Jirka");
91 Properties p = new Properties();
92 p.setProperty("email", "jir@ka.cz");
93 p.store(new FileOutputStream(fJirka), "");
95 File fJan = new File(usersDir, "Jan");
97 Properties p = new Properties();
98 p.setProperty("email", "j@an.cz");
99 p.store(new FileOutputStream(fJan), "");
102 String logRoot = resource().path("login").
103 queryParam("name", "Jarda").
104 queryParam("password", "heslo").
105 accept(MediaType.TEXT_PLAIN).
107 String logJirka = resource().path("login").
108 queryParam("name", "Jirka").
109 queryParam("password", "pesko").
110 accept(MediaType.TEXT_PLAIN).
112 String logJan = resource().path("login").
113 queryParam("name", "Jan").
114 queryParam("password", "pan").
115 accept(MediaType.TEXT_PLAIN).
118 GameId s = resource().path("games").queryParam("white", "Jan")
119 .queryParam("loginID", logJan)
120 .queryParam("black", "Jirka").post(GameId.class);
121 resource().path("games/" + s.getId())
122 .queryParam("loginID", logJan)
123 .queryParam("player", "Jan").queryParam("move", "RESIGN").put(GameId.class);
125 GenericType<List<GameId>> gType = new GenericType<List<GameId>>() {};
126 List<GameId> all = resource().path("games/").queryParam("loginID", logRoot).accept(MediaType.TEXT_XML).get(gType);
127 boolean found = false;
128 for (GameId id : all) {
129 if (id.getId().equals(s.getId())) {
134 assertTrue("List of games shall contai all games: " + all, found);
136 Game end = resource().path("games/" + s.getId()).queryParam("loginID", logRoot).accept(MediaType.TEXT_XML).get(Game.class);
137 assertEquals("One can see status of games with priviledges", GameStatus.blackWon, end.getId().getStatus());