83 |
04 Mar 05 |
nicklas |
1 |
/* |
192 |
17 Mar 05 |
jari |
$Id$ |
83 |
04 Mar 05 |
nicklas |
3 |
|
4889 |
06 Apr 09 |
nicklas |
Copyright (C) 2005 Samuel Andersson, Jari Häkkinen, Nicklas Nordborg |
4889 |
06 Apr 09 |
nicklas |
Copyright (C) 2006 Jari Häkkinen, Nicklas Nordborg |
83 |
04 Mar 05 |
nicklas |
6 |
|
2304 |
22 May 06 |
jari |
This file is part of BASE - BioArray Software Environment. |
2304 |
22 May 06 |
jari |
Available at http://base.thep.lu.se/ |
83 |
04 Mar 05 |
nicklas |
9 |
|
83 |
04 Mar 05 |
nicklas |
BASE is free software; you can redistribute it and/or |
83 |
04 Mar 05 |
nicklas |
modify it under the terms of the GNU General Public License |
4480 |
05 Sep 08 |
jari |
as published by the Free Software Foundation; either version 3 |
83 |
04 Mar 05 |
nicklas |
of the License, or (at your option) any later version. |
83 |
04 Mar 05 |
nicklas |
14 |
|
83 |
04 Mar 05 |
nicklas |
BASE is distributed in the hope that it will be useful, |
83 |
04 Mar 05 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
83 |
04 Mar 05 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
83 |
04 Mar 05 |
nicklas |
GNU General Public License for more details. |
83 |
04 Mar 05 |
nicklas |
19 |
|
83 |
04 Mar 05 |
nicklas |
You should have received a copy of the GNU General Public License |
4514 |
11 Sep 08 |
jari |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
83 |
04 Mar 05 |
nicklas |
22 |
*/ |
83 |
04 Mar 05 |
nicklas |
23 |
import net.sf.basedb.core.*; |
83 |
04 Mar 05 |
nicklas |
24 |
|
83 |
04 Mar 05 |
nicklas |
25 |
import java.util.Date; |
5340 |
10 May 10 |
nicklas |
26 |
import java.util.Set; |
83 |
04 Mar 05 |
nicklas |
27 |
public class TestRole |
83 |
04 Mar 05 |
nicklas |
28 |
{ |
83 |
04 Mar 05 |
nicklas |
29 |
|
83 |
04 Mar 05 |
nicklas |
30 |
static boolean ok = true; |
83 |
04 Mar 05 |
nicklas |
31 |
public static void main(String[] args) |
83 |
04 Mar 05 |
nicklas |
32 |
{ |
83 |
04 Mar 05 |
nicklas |
33 |
TestUtil.checkArgs(args); |
83 |
04 Mar 05 |
nicklas |
34 |
TestUtil.begin(); |
83 |
04 Mar 05 |
nicklas |
35 |
ok = test_all(); |
83 |
04 Mar 05 |
nicklas |
36 |
TestUtil.stop(); |
83 |
04 Mar 05 |
nicklas |
37 |
} |
83 |
04 Mar 05 |
nicklas |
38 |
|
83 |
04 Mar 05 |
nicklas |
39 |
static boolean test_all() |
83 |
04 Mar 05 |
nicklas |
40 |
{ |
83 |
04 Mar 05 |
nicklas |
41 |
write("++Testing roles"); |
83 |
04 Mar 05 |
nicklas |
42 |
write_header(); |
83 |
04 Mar 05 |
nicklas |
// Standard tests: create, load, list |
221 |
23 Mar 05 |
nicklas |
44 |
int id = test_create(true); |
221 |
23 Mar 05 |
nicklas |
45 |
int id2 = test_create(false); |
84 |
07 Mar 05 |
nicklas |
46 |
test_load(id); |
292 |
31 Mar 05 |
nicklas |
47 |
test_list(-1); |
83 |
04 Mar 05 |
nicklas |
48 |
|
83 |
04 Mar 05 |
nicklas |
// Extra tests: add, list and remove users |
292 |
31 Mar 05 |
nicklas |
50 |
test_listusers(id, 0); |
130 |
10 Mar 05 |
nicklas |
51 |
test_adduser(id, TestUtil.getSessionControl().getLoggedInUserId()); |
292 |
31 Mar 05 |
nicklas |
52 |
test_listusers(id, 1); |
1529 |
27 Oct 05 |
nicklas |
53 |
|
1529 |
27 Oct 05 |
nicklas |
54 |
if (TestUtil.waitBeforeDelete()) TestUtil.waitForEnter(); |
130 |
10 Mar 05 |
nicklas |
55 |
test_removeuser(id, TestUtil.getSessionControl().getLoggedInUserId()); |
292 |
31 Mar 05 |
nicklas |
56 |
test_listusers(id, 0); |
83 |
04 Mar 05 |
nicklas |
57 |
|
83 |
04 Mar 05 |
nicklas |
// Standard test: Delete |
84 |
07 Mar 05 |
nicklas |
59 |
test_delete(id); |
221 |
23 Mar 05 |
nicklas |
60 |
test_delete(id2); |
83 |
04 Mar 05 |
nicklas |
61 |
write("++Testing roles "+(ok ? "OK" : "Failed")+"\n"); |
83 |
04 Mar 05 |
nicklas |
62 |
return ok; |
83 |
04 Mar 05 |
nicklas |
63 |
} |
83 |
04 Mar 05 |
nicklas |
64 |
|
221 |
23 Mar 05 |
nicklas |
65 |
static int test_create(boolean setAll) |
83 |
04 Mar 05 |
nicklas |
66 |
{ |
126 |
10 Mar 05 |
nicklas |
67 |
if (!TestUtil.hasPermission(Permission.CREATE, Item.ROLE)) return 0; |
83 |
04 Mar 05 |
nicklas |
68 |
int id = 0; |
83 |
04 Mar 05 |
nicklas |
69 |
DbControl dc = null; |
83 |
04 Mar 05 |
nicklas |
70 |
try |
83 |
04 Mar 05 |
nicklas |
71 |
{ |
83 |
04 Mar 05 |
nicklas |
72 |
dc = TestUtil.getDbControl(); |
83 |
04 Mar 05 |
nicklas |
73 |
Role r = Role.getNew(dc); |
221 |
23 Mar 05 |
nicklas |
74 |
if (setAll) |
221 |
23 Mar 05 |
nicklas |
75 |
{ |
221 |
23 Mar 05 |
nicklas |
76 |
r.setName("Test role"); |
221 |
23 Mar 05 |
nicklas |
77 |
r.setDescription("Added at "+new Date()); |
221 |
23 Mar 05 |
nicklas |
78 |
} |
83 |
04 Mar 05 |
nicklas |
79 |
dc.saveItem(r); |
83 |
04 Mar 05 |
nicklas |
80 |
dc.commit(); |
83 |
04 Mar 05 |
nicklas |
81 |
id = r.getId(); |
83 |
04 Mar 05 |
nicklas |
82 |
write_item(0, r); |
83 |
04 Mar 05 |
nicklas |
83 |
write("--Create role OK"); |
83 |
04 Mar 05 |
nicklas |
84 |
} |
358 |
04 Apr 05 |
nicklas |
85 |
catch (Throwable ex) |
83 |
04 Mar 05 |
nicklas |
86 |
{ |
83 |
04 Mar 05 |
nicklas |
87 |
write("--Create role FAILED"); |
83 |
04 Mar 05 |
nicklas |
88 |
ex.printStackTrace(); |
83 |
04 Mar 05 |
nicklas |
89 |
ok = false; |
83 |
04 Mar 05 |
nicklas |
90 |
} |
83 |
04 Mar 05 |
nicklas |
91 |
finally |
83 |
04 Mar 05 |
nicklas |
92 |
{ |
83 |
04 Mar 05 |
nicklas |
93 |
if (dc != null) dc.close(); |
83 |
04 Mar 05 |
nicklas |
94 |
} |
83 |
04 Mar 05 |
nicklas |
95 |
return id; |
83 |
04 Mar 05 |
nicklas |
96 |
} |
83 |
04 Mar 05 |
nicklas |
97 |
|
83 |
04 Mar 05 |
nicklas |
98 |
static void test_load(int id) |
83 |
04 Mar 05 |
nicklas |
99 |
{ |
83 |
04 Mar 05 |
nicklas |
100 |
if (id == 0) return; |
83 |
04 Mar 05 |
nicklas |
101 |
DbControl dc = null; |
83 |
04 Mar 05 |
nicklas |
102 |
try |
83 |
04 Mar 05 |
nicklas |
103 |
{ |
83 |
04 Mar 05 |
nicklas |
104 |
dc = TestUtil.getDbControl(); |
83 |
04 Mar 05 |
nicklas |
105 |
Role r = Role.getById(dc, id); |
83 |
04 Mar 05 |
nicklas |
106 |
write_item(0, r); |
83 |
04 Mar 05 |
nicklas |
107 |
write("--Load role OK"); |
83 |
04 Mar 05 |
nicklas |
108 |
} |
358 |
04 Apr 05 |
nicklas |
109 |
catch (Throwable ex) |
83 |
04 Mar 05 |
nicklas |
110 |
{ |
83 |
04 Mar 05 |
nicklas |
111 |
write("--Load role FAILED"); |
83 |
04 Mar 05 |
nicklas |
112 |
ex.printStackTrace(); |
83 |
04 Mar 05 |
nicklas |
113 |
ok = false; |
83 |
04 Mar 05 |
nicklas |
114 |
} |
83 |
04 Mar 05 |
nicklas |
115 |
finally |
83 |
04 Mar 05 |
nicklas |
116 |
{ |
83 |
04 Mar 05 |
nicklas |
117 |
if (dc != null) dc.close(); |
83 |
04 Mar 05 |
nicklas |
118 |
} |
83 |
04 Mar 05 |
nicklas |
119 |
} |
83 |
04 Mar 05 |
nicklas |
120 |
|
237 |
23 Mar 05 |
samuel |
121 |
|
292 |
31 Mar 05 |
nicklas |
122 |
static void test_list(int expectedResults) |
83 |
04 Mar 05 |
nicklas |
123 |
{ |
237 |
23 Mar 05 |
samuel |
124 |
DbControl dc = null; |
83 |
04 Mar 05 |
nicklas |
125 |
try |
83 |
04 Mar 05 |
nicklas |
126 |
{ |
237 |
23 Mar 05 |
samuel |
127 |
dc = TestUtil.getDbControl(); |
1418 |
07 Oct 05 |
nicklas |
128 |
ItemResultList<Role> l = Role.getQuery().list(dc); |
256 |
24 Mar 05 |
nicklas |
129 |
for (int i = 0; i<l.size(); i++) |
83 |
04 Mar 05 |
nicklas |
130 |
{ |
256 |
24 Mar 05 |
nicklas |
131 |
write_item(i, l.get(i)); |
83 |
04 Mar 05 |
nicklas |
132 |
} |
292 |
31 Mar 05 |
nicklas |
133 |
if (expectedResults >= 0 && expectedResults != l.size()) |
292 |
31 Mar 05 |
nicklas |
134 |
{ |
292 |
31 Mar 05 |
nicklas |
135 |
throw new BaseException("Expected "+expectedResults+" results, not "+l.size()); |
292 |
31 Mar 05 |
nicklas |
136 |
} |
256 |
24 Mar 05 |
nicklas |
137 |
write("--List roles OK ("+l.size()+")"); |
83 |
04 Mar 05 |
nicklas |
138 |
} |
358 |
04 Apr 05 |
nicklas |
139 |
catch (Throwable ex) |
83 |
04 Mar 05 |
nicklas |
140 |
{ |
83 |
04 Mar 05 |
nicklas |
141 |
write("--List roles FAILED"); |
83 |
04 Mar 05 |
nicklas |
142 |
ex.printStackTrace(); |
83 |
04 Mar 05 |
nicklas |
143 |
ok = false; |
83 |
04 Mar 05 |
nicklas |
144 |
} |
83 |
04 Mar 05 |
nicklas |
145 |
finally |
83 |
04 Mar 05 |
nicklas |
146 |
{ |
237 |
23 Mar 05 |
samuel |
147 |
if (dc != null) dc.close(); |
83 |
04 Mar 05 |
nicklas |
148 |
} |
83 |
04 Mar 05 |
nicklas |
149 |
} |
83 |
04 Mar 05 |
nicklas |
150 |
|
237 |
23 Mar 05 |
samuel |
151 |
|
83 |
04 Mar 05 |
nicklas |
152 |
static void test_delete(int id) |
83 |
04 Mar 05 |
nicklas |
153 |
{ |
83 |
04 Mar 05 |
nicklas |
154 |
if (id == 0) return; |
84 |
07 Mar 05 |
nicklas |
155 |
DbControl dc = null; |
83 |
04 Mar 05 |
nicklas |
156 |
try |
83 |
04 Mar 05 |
nicklas |
157 |
{ |
84 |
07 Mar 05 |
nicklas |
158 |
dc = TestUtil.getDbControl(); |
84 |
07 Mar 05 |
nicklas |
159 |
Role r = Role.getById(dc, id); |
84 |
07 Mar 05 |
nicklas |
160 |
dc.deleteItem(r); |
5340 |
10 May 10 |
nicklas |
161 |
Set<ItemProxy> using = r.getUsingItems(); |
5340 |
10 May 10 |
nicklas |
162 |
if (using.size() > 0) |
5340 |
10 May 10 |
nicklas |
163 |
{ |
5340 |
10 May 10 |
nicklas |
164 |
throw new BaseException(r + " is used by " + using); |
5340 |
10 May 10 |
nicklas |
165 |
} |
84 |
07 Mar 05 |
nicklas |
166 |
dc.commit(); |
83 |
04 Mar 05 |
nicklas |
167 |
write("--Delete role OK"); |
83 |
04 Mar 05 |
nicklas |
168 |
} |
358 |
04 Apr 05 |
nicklas |
169 |
catch (Throwable ex) |
83 |
04 Mar 05 |
nicklas |
170 |
{ |
83 |
04 Mar 05 |
nicklas |
171 |
write("--Delete role FAILED"); |
83 |
04 Mar 05 |
nicklas |
172 |
ex.printStackTrace(); |
83 |
04 Mar 05 |
nicklas |
173 |
ok = false; |
83 |
04 Mar 05 |
nicklas |
174 |
} |
83 |
04 Mar 05 |
nicklas |
175 |
finally |
83 |
04 Mar 05 |
nicklas |
176 |
{ |
84 |
07 Mar 05 |
nicklas |
177 |
if (dc != null) dc.close(); |
83 |
04 Mar 05 |
nicklas |
178 |
} |
83 |
04 Mar 05 |
nicklas |
179 |
} |
84 |
07 Mar 05 |
nicklas |
180 |
|
83 |
04 Mar 05 |
nicklas |
181 |
static void write_header() |
83 |
04 Mar 05 |
nicklas |
182 |
{ |
83 |
04 Mar 05 |
nicklas |
183 |
if (!TestUtil.getSilent()) |
83 |
04 Mar 05 |
nicklas |
184 |
{ |
83 |
04 Mar 05 |
nicklas |
185 |
System.out.println(" \tID \tName \tDescription"); |
83 |
04 Mar 05 |
nicklas |
186 |
System.out.println("-- \t-- \t--------- \t-----------"); |
83 |
04 Mar 05 |
nicklas |
187 |
} |
83 |
04 Mar 05 |
nicklas |
188 |
} |
83 |
04 Mar 05 |
nicklas |
189 |
static void write_item(int i, Role r) |
83 |
04 Mar 05 |
nicklas |
190 |
throws BaseException |
83 |
04 Mar 05 |
nicklas |
191 |
{ |
83 |
04 Mar 05 |
nicklas |
192 |
if (!TestUtil.getSilent()) System.out.println(i+":\t"+r.getId()+"\t"+r.getName()+"\t"+r.getDescription()); |
83 |
04 Mar 05 |
nicklas |
193 |
} |
83 |
04 Mar 05 |
nicklas |
194 |
static void write_item(int i, User u) |
83 |
04 Mar 05 |
nicklas |
195 |
throws BaseException |
83 |
04 Mar 05 |
nicklas |
196 |
{ |
83 |
04 Mar 05 |
nicklas |
197 |
if (!TestUtil.getSilent()) System.out.println(i+":\t"+u.getId()+"\t"+u.getName()+"\t"+u.getDescription()); |
83 |
04 Mar 05 |
nicklas |
198 |
} |
83 |
04 Mar 05 |
nicklas |
199 |
static void write(String message) |
83 |
04 Mar 05 |
nicklas |
200 |
{ |
83 |
04 Mar 05 |
nicklas |
201 |
System.out.println(message); |
83 |
04 Mar 05 |
nicklas |
202 |
} |
130 |
10 Mar 05 |
nicklas |
203 |
|
130 |
10 Mar 05 |
nicklas |
204 |
static void test_adduser(int id, int userId) |
83 |
04 Mar 05 |
nicklas |
205 |
{ |
131 |
10 Mar 05 |
nicklas |
206 |
if (id == 0 || userId == 0) return; |
130 |
10 Mar 05 |
nicklas |
207 |
DbControl dc = null; |
83 |
04 Mar 05 |
nicklas |
208 |
try |
83 |
04 Mar 05 |
nicklas |
209 |
{ |
130 |
10 Mar 05 |
nicklas |
210 |
dc = TestUtil.getDbControl(); |
130 |
10 Mar 05 |
nicklas |
211 |
Role r = Role.getById(dc, id); |
130 |
10 Mar 05 |
nicklas |
212 |
User u = User.getById(dc, userId); |
130 |
10 Mar 05 |
nicklas |
213 |
r.addUser(u); |
130 |
10 Mar 05 |
nicklas |
214 |
dc.commit(); |
83 |
04 Mar 05 |
nicklas |
215 |
write("--Add user to role OK"); |
83 |
04 Mar 05 |
nicklas |
216 |
} |
358 |
04 Apr 05 |
nicklas |
217 |
catch (Throwable ex) |
83 |
04 Mar 05 |
nicklas |
218 |
{ |
83 |
04 Mar 05 |
nicklas |
219 |
write("--Add user to role FAILED"); |
83 |
04 Mar 05 |
nicklas |
220 |
ex.printStackTrace(); |
83 |
04 Mar 05 |
nicklas |
221 |
ok = false; |
83 |
04 Mar 05 |
nicklas |
222 |
} |
83 |
04 Mar 05 |
nicklas |
223 |
finally |
83 |
04 Mar 05 |
nicklas |
224 |
{ |
130 |
10 Mar 05 |
nicklas |
225 |
if (dc != null) dc.close(); |
83 |
04 Mar 05 |
nicklas |
226 |
} |
83 |
04 Mar 05 |
nicklas |
227 |
} |
257 |
24 Mar 05 |
nicklas |
228 |
|
292 |
31 Mar 05 |
nicklas |
229 |
static void test_listusers(int id, int expectedResults) |
83 |
04 Mar 05 |
nicklas |
230 |
{ |
83 |
04 Mar 05 |
nicklas |
231 |
if (id == 0) return; |
257 |
24 Mar 05 |
nicklas |
232 |
DbControl dc = null; |
83 |
04 Mar 05 |
nicklas |
233 |
try |
83 |
04 Mar 05 |
nicklas |
234 |
{ |
257 |
24 Mar 05 |
nicklas |
235 |
dc = TestUtil.getDbControl(); |
257 |
24 Mar 05 |
nicklas |
236 |
Role r = Role.getById(dc, id); |
1418 |
07 Oct 05 |
nicklas |
237 |
ItemResultList<User> l = r.getUsers().list(dc); |
83 |
04 Mar 05 |
nicklas |
238 |
for (int i=0; i<l.size(); i++) |
83 |
04 Mar 05 |
nicklas |
239 |
{ |
257 |
24 Mar 05 |
nicklas |
240 |
write_item(i, l.get(i)); |
83 |
04 Mar 05 |
nicklas |
241 |
} |
292 |
31 Mar 05 |
nicklas |
242 |
if (expectedResults >= 0 && expectedResults != l.size()) |
292 |
31 Mar 05 |
nicklas |
243 |
{ |
292 |
31 Mar 05 |
nicklas |
244 |
throw new BaseException("Expected "+expectedResults+" results, not "+l.size()); |
292 |
31 Mar 05 |
nicklas |
245 |
} |
83 |
04 Mar 05 |
nicklas |
246 |
write("--List users in role OK ("+l.size()+")"); |
83 |
04 Mar 05 |
nicklas |
247 |
} |
358 |
04 Apr 05 |
nicklas |
248 |
catch (Throwable ex) |
83 |
04 Mar 05 |
nicklas |
249 |
{ |
83 |
04 Mar 05 |
nicklas |
250 |
write("--List users in role FAILED"); |
83 |
04 Mar 05 |
nicklas |
251 |
ex.printStackTrace(); |
83 |
04 Mar 05 |
nicklas |
252 |
ok = false; |
83 |
04 Mar 05 |
nicklas |
253 |
} |
83 |
04 Mar 05 |
nicklas |
254 |
finally |
83 |
04 Mar 05 |
nicklas |
255 |
{ |
257 |
24 Mar 05 |
nicklas |
256 |
if (dc != null) dc.close(); |
83 |
04 Mar 05 |
nicklas |
257 |
} |
83 |
04 Mar 05 |
nicklas |
258 |
} |
257 |
24 Mar 05 |
nicklas |
259 |
|
130 |
10 Mar 05 |
nicklas |
260 |
static void test_removeuser(int id, int userId) |
83 |
04 Mar 05 |
nicklas |
261 |
{ |
131 |
10 Mar 05 |
nicklas |
262 |
if (id == 0 || userId == 0) return; |
130 |
10 Mar 05 |
nicklas |
263 |
DbControl dc = null; |
83 |
04 Mar 05 |
nicklas |
264 |
try |
83 |
04 Mar 05 |
nicklas |
265 |
{ |
130 |
10 Mar 05 |
nicklas |
266 |
dc = TestUtil.getDbControl(); |
130 |
10 Mar 05 |
nicklas |
267 |
Role r = Role.getById(dc, id); |
130 |
10 Mar 05 |
nicklas |
268 |
User u = User.getById(dc, userId); |
130 |
10 Mar 05 |
nicklas |
269 |
r.removeUser(u); |
130 |
10 Mar 05 |
nicklas |
270 |
dc.commit(); |
83 |
04 Mar 05 |
nicklas |
271 |
write("--Remove user from role OK"); |
83 |
04 Mar 05 |
nicklas |
272 |
} |
358 |
04 Apr 05 |
nicklas |
273 |
catch (Throwable ex) |
83 |
04 Mar 05 |
nicklas |
274 |
{ |
83 |
04 Mar 05 |
nicklas |
275 |
write("--Remove user from role FAILED"); |
83 |
04 Mar 05 |
nicklas |
276 |
ex.printStackTrace(); |
83 |
04 Mar 05 |
nicklas |
277 |
ok = false; |
83 |
04 Mar 05 |
nicklas |
278 |
} |
83 |
04 Mar 05 |
nicklas |
279 |
finally |
83 |
04 Mar 05 |
nicklas |
280 |
{ |
130 |
10 Mar 05 |
nicklas |
281 |
if (dc != null) dc.close(); |
83 |
04 Mar 05 |
nicklas |
282 |
} |
83 |
04 Mar 05 |
nicklas |
283 |
} |
83 |
04 Mar 05 |
nicklas |
284 |
|
83 |
04 Mar 05 |
nicklas |
285 |
} |
83 |
04 Mar 05 |
nicklas |
286 |
|