2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * The contents of this file are subject to the terms of either the GNU
5 * General Public License Version 2 only ("GPL") or the Common
6 * Development and Distribution License("CDDL") (collectively, the
7 * "License"). You may not use this file except in compliance with the
8 * License. You can obtain a copy of the License at
9 * http://www.netbeans.org/cddl-gplv2.html
10 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
11 * specific language governing permissions and limitations under the
12 * License. When distributing the software, include this License Header
13 * Notice in each file and include the License file at
14 * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
15 * particular file as subject to the "Classpath" exception as provided
16 * by Sun in the GPL Version 2 section of the License file that
17 * accompanied this code. If applicable, add the following below the
18 * License Header, with the fields enclosed by brackets [] replaced by
19 * your own identifying information:
20 * "Portions Copyrighted [year] [name of copyright owner]"
24 * Portions Copyrighted 2009 Jaroslav Tulach
27 package cz.xelfi.quoridor.webidor;
29 import com.sun.jersey.test.framework.WebAppDescriptor;
30 import com.sun.jersey.test.framework.AppDescriptor;
31 import com.sun.jersey.api.client.GenericType;
32 import java.util.List;
33 import com.sun.jersey.test.framework.JerseyTest;
35 import java.io.FileOutputStream;
36 import java.io.IOException;
37 import java.util.Properties;
38 import javax.ws.rs.core.MediaType;
39 import org.junit.Test;
40 import static org.junit.Assert.*;
44 * @author Jaroslav Tulach <jtulach@netbeans.org>
46 public class AllGamesTest extends JerseyTest {
48 System.setProperty("JERSEY_HTTP_PORT", "45420");
54 protected AppDescriptor configure() {
56 dir = File.createTempFile("quoridor", ".dir");
58 System.setProperty("quoridor.dir", dir.getPath());
60 File passwd = new File(dir, "passwd");
61 FileOutputStream os = new FileOutputStream(passwd);
62 os.write("Jarda=heslo\nJirka=pesko\nJan=pan\n".getBytes("UTF-8"));
64 } catch (Exception ex) {
65 throw new IllegalStateException(ex);
67 return new WebAppDescriptor.Builder("cz.xelfi.quoridor.webidor.resources").build();
71 public void tearDown() throws Exception {
75 static void deleteRec(File dir) throws IOException {
79 File[] arr = dir.listFiles();
87 @Test public void testListGames() throws Exception {
88 File usersDir = new File(dir, "users");
90 File fJarda = new File(usersDir, "Jarda");
92 Properties p = new Properties();
93 p.setProperty("email", "jar@da.cz");
94 p.setProperty("permission.games", "true");
95 p.store(new FileOutputStream(fJarda), "");
97 File fJirka = new File(usersDir, "Jirka");
99 Properties p = new Properties();
100 p.setProperty("email", "jir@ka.cz");
101 p.store(new FileOutputStream(fJirka), "");
103 File fJan = new File(usersDir, "Jan");
105 Properties p = new Properties();
106 p.setProperty("email", "j@an.cz");
107 p.store(new FileOutputStream(fJan), "");
110 String logRoot = resource().path("login").
111 queryParam("name", "Jarda").
112 queryParam("password", "heslo").
113 accept(MediaType.TEXT_PLAIN).
115 String logJirka = resource().path("login").
116 queryParam("name", "Jirka").
117 queryParam("password", "pesko").
118 accept(MediaType.TEXT_PLAIN).
120 String logJan = resource().path("login").
121 queryParam("name", "Jan").
122 queryParam("password", "pan").
123 accept(MediaType.TEXT_PLAIN).
126 GameId s = resource().path("games").queryParam("white", "Jan")
127 .queryParam("loginID", logJan)
128 .queryParam("black", "Jirka").post(GameId.class);
129 resource().path("games/" + s.getId())
130 .queryParam("loginID", logJan)
131 .queryParam("player", "Jan").queryParam("move", "RESIGN").put(GameId.class);
133 GenericType<List<GameId>> gType = new GenericType<List<GameId>>() {};
134 List<GameId> all = resource().path("games/").queryParam("loginID", logRoot).accept(MediaType.TEXT_XML).get(gType);
135 boolean found = false;
136 for (GameId id : all) {
137 if (id.getId().equals(s.getId())) {
142 assertTrue("List of games shall contai all games: " + all, found);
144 Game end = resource().path("games/" + s.getId()).queryParam("loginID", logRoot).accept(MediaType.TEXT_XML).get(Game.class);
145 assertEquals("One can see status of games with priviledges", GameStatus.blackWon, end.getId().getStatus());