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