134 |
10 Mar 05 |
nicklas |
1 |
/* |
134 |
10 Mar 05 |
nicklas |
$Id$ |
134 |
10 Mar 05 |
nicklas |
3 |
|
3675 |
16 Aug 07 |
jari |
Copyright (C) 2005 Nicklas Nordborg |
4889 |
06 Apr 09 |
nicklas |
Copyright (C) 2006 Jari Häkkinen, Nicklas Nordborg |
3675 |
16 Aug 07 |
jari |
Copyright (C) 2007 Nicklas Nordborg |
134 |
10 Mar 05 |
nicklas |
7 |
|
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/ |
134 |
10 Mar 05 |
nicklas |
10 |
|
134 |
10 Mar 05 |
nicklas |
BASE is free software; you can redistribute it and/or |
134 |
10 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 |
134 |
10 Mar 05 |
nicklas |
of the License, or (at your option) any later version. |
134 |
10 Mar 05 |
nicklas |
15 |
|
134 |
10 Mar 05 |
nicklas |
BASE is distributed in the hope that it will be useful, |
134 |
10 Mar 05 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
134 |
10 Mar 05 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
134 |
10 Mar 05 |
nicklas |
GNU General Public License for more details. |
134 |
10 Mar 05 |
nicklas |
20 |
|
134 |
10 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/>. |
134 |
10 Mar 05 |
nicklas |
23 |
*/ |
134 |
10 Mar 05 |
nicklas |
24 |
import net.sf.basedb.core.*; |
5340 |
10 May 10 |
nicklas |
25 |
|
134 |
10 Mar 05 |
nicklas |
26 |
import java.util.Date; |
259 |
24 Mar 05 |
nicklas |
27 |
import java.util.EnumSet; |
5340 |
10 May 10 |
nicklas |
28 |
import java.util.Set; |
134 |
10 Mar 05 |
nicklas |
29 |
|
134 |
10 Mar 05 |
nicklas |
30 |
public class TestUser |
134 |
10 Mar 05 |
nicklas |
31 |
{ |
134 |
10 Mar 05 |
nicklas |
32 |
|
134 |
10 Mar 05 |
nicklas |
33 |
static boolean ok = true; |
134 |
10 Mar 05 |
nicklas |
34 |
public static void main(String[] args) |
134 |
10 Mar 05 |
nicklas |
35 |
{ |
134 |
10 Mar 05 |
nicklas |
36 |
TestUtil.checkArgs(args); |
134 |
10 Mar 05 |
nicklas |
37 |
TestUtil.begin(); |
134 |
10 Mar 05 |
nicklas |
38 |
ok = test_all(); |
134 |
10 Mar 05 |
nicklas |
39 |
TestUtil.stop(); |
134 |
10 Mar 05 |
nicklas |
40 |
} |
134 |
10 Mar 05 |
nicklas |
41 |
|
134 |
10 Mar 05 |
nicklas |
42 |
static boolean test_all() |
134 |
10 Mar 05 |
nicklas |
43 |
{ |
134 |
10 Mar 05 |
nicklas |
44 |
write("++Testing users"); |
134 |
10 Mar 05 |
nicklas |
45 |
write_header(); |
134 |
10 Mar 05 |
nicklas |
// Standard tests: create, load, list |
3467 |
08 Jun 07 |
nicklas |
47 |
int id2 = test_create(true); |
3467 |
08 Jun 07 |
nicklas |
48 |
int id = test_create(false); |
134 |
10 Mar 05 |
nicklas |
49 |
test_load(id); |
292 |
31 Mar 05 |
nicklas |
50 |
test_list(-1); |
1529 |
27 Oct 05 |
nicklas |
51 |
|
1529 |
27 Oct 05 |
nicklas |
// Extra test: modify password |
1529 |
27 Oct 05 |
nicklas |
53 |
test_change_password(id); |
1529 |
27 Oct 05 |
nicklas |
54 |
test_duplicate(id); |
134 |
10 Mar 05 |
nicklas |
55 |
|
5690 |
11 Aug 11 |
nicklas |
// Extra test: create an item and change the owner |
5690 |
11 Aug 11 |
nicklas |
57 |
int tag_id = TestTag.test_create("Test tag for setting owner", 0, false); |
5690 |
11 Aug 11 |
nicklas |
58 |
test_set_owner(id, Item.TAG, tag_id); |
5690 |
11 Aug 11 |
nicklas |
59 |
|
259 |
24 Mar 05 |
nicklas |
// Extra tests: add, list and remove groups |
259 |
24 Mar 05 |
nicklas |
61 |
int groupId = TestGroup.test_create(true); |
292 |
31 Mar 05 |
nicklas |
62 |
test_listgroups(id, 0); |
259 |
24 Mar 05 |
nicklas |
63 |
TestGroup.test_adduser(groupId, id); |
292 |
31 Mar 05 |
nicklas |
64 |
test_listgroups(id, 1); |
1529 |
27 Oct 05 |
nicklas |
65 |
if (TestUtil.waitBeforeDelete()) TestUtil.waitForEnter(); |
259 |
24 Mar 05 |
nicklas |
66 |
TestGroup.test_removeuser(groupId, id); |
292 |
31 Mar 05 |
nicklas |
67 |
test_listgroups(id, 0); |
259 |
24 Mar 05 |
nicklas |
68 |
TestGroup.test_delete(groupId); |
259 |
24 Mar 05 |
nicklas |
69 |
|
259 |
24 Mar 05 |
nicklas |
// Extra tests: add, list and remove roles |
259 |
24 Mar 05 |
nicklas |
71 |
int roleId = TestRole.test_create(true); |
292 |
31 Mar 05 |
nicklas |
72 |
test_listroles(id, 0); |
259 |
24 Mar 05 |
nicklas |
73 |
TestRole.test_adduser(roleId, id); |
292 |
31 Mar 05 |
nicklas |
74 |
test_listroles(id, 1); |
1529 |
27 Oct 05 |
nicklas |
75 |
if (TestUtil.waitBeforeDelete()) TestUtil.waitForEnter(); |
259 |
24 Mar 05 |
nicklas |
76 |
TestRole.test_removeuser(roleId, id); |
292 |
31 Mar 05 |
nicklas |
77 |
test_listroles(id, 0); |
259 |
24 Mar 05 |
nicklas |
78 |
TestRole.test_delete(roleId); |
259 |
24 Mar 05 |
nicklas |
79 |
|
259 |
24 Mar 05 |
nicklas |
// Extra tests: add, list and remove projects |
5368 |
22 Jun 10 |
nicklas |
81 |
int projectId = TestProject.test_create("User project", true); |
292 |
31 Mar 05 |
nicklas |
82 |
test_listprojects(id, 0); |
266 |
29 Mar 05 |
nicklas |
83 |
TestProject.test_adduser(projectId, id, EnumSet.of(Permission.WRITE)); |
292 |
31 Mar 05 |
nicklas |
84 |
test_listprojects(id, 1); |
1529 |
27 Oct 05 |
nicklas |
85 |
if (TestUtil.waitBeforeDelete()) TestUtil.waitForEnter(); |
259 |
24 Mar 05 |
nicklas |
86 |
TestProject.test_removeuser(projectId, id); |
292 |
31 Mar 05 |
nicklas |
87 |
test_listprojects(id, 0); |
259 |
24 Mar 05 |
nicklas |
88 |
TestProject.test_delete(projectId); |
259 |
24 Mar 05 |
nicklas |
89 |
|
134 |
10 Mar 05 |
nicklas |
// Standard test: Delete |
5690 |
11 Aug 11 |
nicklas |
91 |
TestTag.test_delete(tag_id); |
134 |
10 Mar 05 |
nicklas |
92 |
test_delete(id); |
221 |
23 Mar 05 |
nicklas |
93 |
test_delete(id2); |
134 |
10 Mar 05 |
nicklas |
94 |
|
134 |
10 Mar 05 |
nicklas |
95 |
write("++Testing users "+(ok ? "OK" : "Failed")+"\n"); |
134 |
10 Mar 05 |
nicklas |
96 |
return ok; |
134 |
10 Mar 05 |
nicklas |
97 |
} |
134 |
10 Mar 05 |
nicklas |
98 |
|
221 |
23 Mar 05 |
nicklas |
99 |
static int test_create(boolean setAll) |
134 |
10 Mar 05 |
nicklas |
100 |
{ |
134 |
10 Mar 05 |
nicklas |
101 |
if (!TestUtil.hasPermission(Permission.CREATE, Item.USER)) return 0; |
134 |
10 Mar 05 |
nicklas |
102 |
int id = 0; |
134 |
10 Mar 05 |
nicklas |
103 |
DbControl dc = null; |
134 |
10 Mar 05 |
nicklas |
104 |
try |
134 |
10 Mar 05 |
nicklas |
105 |
{ |
134 |
10 Mar 05 |
nicklas |
106 |
dc = TestUtil.getDbControl(); |
1595 |
14 Nov 05 |
nicklas |
107 |
String login = "user"+Application.generateRandomId(4); |
134 |
10 Mar 05 |
nicklas |
108 |
User u = User.getNew(dc, login, "password"); |
221 |
23 Mar 05 |
nicklas |
109 |
if (setAll) |
221 |
23 Mar 05 |
nicklas |
110 |
{ |
221 |
23 Mar 05 |
nicklas |
111 |
u.setName("Test user"); |
221 |
23 Mar 05 |
nicklas |
112 |
u.setOrganisation("Lund university"); |
4888 |
06 Apr 09 |
nicklas |
113 |
u.setAddress("Sölvegaten 14A"); |
221 |
23 Mar 05 |
nicklas |
114 |
u.setPhone("046-1111111"); |
221 |
23 Mar 05 |
nicklas |
115 |
u.setFax("046-2222222"); |
221 |
23 Mar 05 |
nicklas |
116 |
u.setEmail("user@thep.lu.se"); |
221 |
23 Mar 05 |
nicklas |
117 |
u.setUrl("http://www.thep.lu.se"); |
221 |
23 Mar 05 |
nicklas |
118 |
u.setDescription("Added at "+new Date()); |
3467 |
08 Jun 07 |
nicklas |
119 |
u.addToDefaultRolesAndGroups(); |
221 |
23 Mar 05 |
nicklas |
120 |
} |
134 |
10 Mar 05 |
nicklas |
121 |
dc.saveItem(u); |
134 |
10 Mar 05 |
nicklas |
122 |
dc.commit(); |
134 |
10 Mar 05 |
nicklas |
123 |
id = u.getId(); |
134 |
10 Mar 05 |
nicklas |
124 |
write_item(0, u); |
134 |
10 Mar 05 |
nicklas |
125 |
write("--Create user OK"); |
134 |
10 Mar 05 |
nicklas |
126 |
} |
358 |
04 Apr 05 |
nicklas |
127 |
catch (Throwable ex) |
134 |
10 Mar 05 |
nicklas |
128 |
{ |
134 |
10 Mar 05 |
nicklas |
129 |
write("--Create user FAILED"); |
134 |
10 Mar 05 |
nicklas |
130 |
ex.printStackTrace(); |
134 |
10 Mar 05 |
nicklas |
131 |
ok = false; |
134 |
10 Mar 05 |
nicklas |
132 |
} |
134 |
10 Mar 05 |
nicklas |
133 |
finally |
134 |
10 Mar 05 |
nicklas |
134 |
{ |
134 |
10 Mar 05 |
nicklas |
135 |
if (dc != null) dc.close(); |
134 |
10 Mar 05 |
nicklas |
136 |
} |
134 |
10 Mar 05 |
nicklas |
137 |
return id; |
134 |
10 Mar 05 |
nicklas |
138 |
} |
134 |
10 Mar 05 |
nicklas |
139 |
|
134 |
10 Mar 05 |
nicklas |
140 |
static void test_load(int id) |
134 |
10 Mar 05 |
nicklas |
141 |
{ |
134 |
10 Mar 05 |
nicklas |
142 |
if (id == 0) return; |
134 |
10 Mar 05 |
nicklas |
143 |
DbControl dc = null; |
134 |
10 Mar 05 |
nicklas |
144 |
try |
134 |
10 Mar 05 |
nicklas |
145 |
{ |
134 |
10 Mar 05 |
nicklas |
146 |
dc = TestUtil.getDbControl(); |
134 |
10 Mar 05 |
nicklas |
147 |
User u = User.getById(dc, id); |
134 |
10 Mar 05 |
nicklas |
148 |
write_item(0, u); |
134 |
10 Mar 05 |
nicklas |
149 |
write("--Load user OK"); |
134 |
10 Mar 05 |
nicklas |
150 |
} |
358 |
04 Apr 05 |
nicklas |
151 |
catch (Throwable ex) |
134 |
10 Mar 05 |
nicklas |
152 |
{ |
134 |
10 Mar 05 |
nicklas |
153 |
write("--Load user FAILED"); |
134 |
10 Mar 05 |
nicklas |
154 |
ex.printStackTrace(); |
134 |
10 Mar 05 |
nicklas |
155 |
ok = false; |
134 |
10 Mar 05 |
nicklas |
156 |
} |
134 |
10 Mar 05 |
nicklas |
157 |
finally |
134 |
10 Mar 05 |
nicklas |
158 |
{ |
134 |
10 Mar 05 |
nicklas |
159 |
if (dc != null) dc.close(); |
134 |
10 Mar 05 |
nicklas |
160 |
} |
134 |
10 Mar 05 |
nicklas |
161 |
} |
134 |
10 Mar 05 |
nicklas |
162 |
|
292 |
31 Mar 05 |
nicklas |
163 |
static void test_list(int expectedResults) |
134 |
10 Mar 05 |
nicklas |
164 |
{ |
256 |
24 Mar 05 |
nicklas |
165 |
DbControl dc = null; |
134 |
10 Mar 05 |
nicklas |
166 |
try |
134 |
10 Mar 05 |
nicklas |
167 |
{ |
256 |
24 Mar 05 |
nicklas |
168 |
dc = TestUtil.getDbControl(); |
1418 |
07 Oct 05 |
nicklas |
169 |
ItemResultList<User> l = User.getQuery().list(dc); |
134 |
10 Mar 05 |
nicklas |
170 |
for (int i = 0; i<l.size(); i++) |
134 |
10 Mar 05 |
nicklas |
171 |
{ |
256 |
24 Mar 05 |
nicklas |
172 |
write_item(i, l.get(i)); |
134 |
10 Mar 05 |
nicklas |
173 |
} |
292 |
31 Mar 05 |
nicklas |
174 |
if (expectedResults >= 0 && expectedResults != l.size()) |
292 |
31 Mar 05 |
nicklas |
175 |
{ |
292 |
31 Mar 05 |
nicklas |
176 |
throw new BaseException("Expected "+expectedResults+" results, not "+l.size()); |
292 |
31 Mar 05 |
nicklas |
177 |
} |
134 |
10 Mar 05 |
nicklas |
178 |
write("--List users OK ("+l.size()+")"); |
134 |
10 Mar 05 |
nicklas |
179 |
} |
358 |
04 Apr 05 |
nicklas |
180 |
catch (Throwable ex) |
134 |
10 Mar 05 |
nicklas |
181 |
{ |
134 |
10 Mar 05 |
nicklas |
182 |
write("--List users FAILED"); |
134 |
10 Mar 05 |
nicklas |
183 |
ex.printStackTrace(); |
134 |
10 Mar 05 |
nicklas |
184 |
ok = false; |
134 |
10 Mar 05 |
nicklas |
185 |
} |
134 |
10 Mar 05 |
nicklas |
186 |
finally |
134 |
10 Mar 05 |
nicklas |
187 |
{ |
256 |
24 Mar 05 |
nicklas |
188 |
if (dc != null) dc.close(); |
134 |
10 Mar 05 |
nicklas |
189 |
} |
134 |
10 Mar 05 |
nicklas |
190 |
} |
134 |
10 Mar 05 |
nicklas |
191 |
|
134 |
10 Mar 05 |
nicklas |
192 |
static void test_delete(int id) |
134 |
10 Mar 05 |
nicklas |
193 |
{ |
134 |
10 Mar 05 |
nicklas |
194 |
if (id == 0) return; |
134 |
10 Mar 05 |
nicklas |
195 |
DbControl dc = null; |
134 |
10 Mar 05 |
nicklas |
196 |
try |
134 |
10 Mar 05 |
nicklas |
197 |
{ |
134 |
10 Mar 05 |
nicklas |
198 |
dc = TestUtil.getDbControl(); |
134 |
10 Mar 05 |
nicklas |
199 |
User u = User.getById(dc, id); |
134 |
10 Mar 05 |
nicklas |
200 |
dc.deleteItem(u); |
5340 |
10 May 10 |
nicklas |
201 |
Set<ItemProxy> using = u.getUsingItems(); |
5340 |
10 May 10 |
nicklas |
202 |
if (using.size() > 0) |
5340 |
10 May 10 |
nicklas |
203 |
{ |
5340 |
10 May 10 |
nicklas |
204 |
throw new BaseException(u + " is used by " + using); |
5340 |
10 May 10 |
nicklas |
205 |
} |
134 |
10 Mar 05 |
nicklas |
206 |
dc.commit(); |
134 |
10 Mar 05 |
nicklas |
207 |
write("--Delete user OK"); |
134 |
10 Mar 05 |
nicklas |
208 |
} |
358 |
04 Apr 05 |
nicklas |
209 |
catch (Throwable ex) |
134 |
10 Mar 05 |
nicklas |
210 |
{ |
134 |
10 Mar 05 |
nicklas |
211 |
write("--Delete user FAILED"); |
134 |
10 Mar 05 |
nicklas |
212 |
ex.printStackTrace(); |
134 |
10 Mar 05 |
nicklas |
213 |
ok = false; |
134 |
10 Mar 05 |
nicklas |
214 |
} |
134 |
10 Mar 05 |
nicklas |
215 |
finally |
134 |
10 Mar 05 |
nicklas |
216 |
{ |
134 |
10 Mar 05 |
nicklas |
217 |
if (dc != null) dc.close(); |
134 |
10 Mar 05 |
nicklas |
218 |
} |
134 |
10 Mar 05 |
nicklas |
219 |
} |
134 |
10 Mar 05 |
nicklas |
220 |
|
134 |
10 Mar 05 |
nicklas |
221 |
static void write_header() |
134 |
10 Mar 05 |
nicklas |
222 |
{ |
134 |
10 Mar 05 |
nicklas |
223 |
if (!TestUtil.getSilent()) |
134 |
10 Mar 05 |
nicklas |
224 |
{ |
1466 |
14 Oct 05 |
nicklas |
225 |
write(" \tID \tName \tLogin\tDescription\tOrganisation\tAddress\tPhone\tFax\tEmail\tUrl"); |
1466 |
14 Oct 05 |
nicklas |
226 |
write("-- \t-- \t--------- \t-----\t-----------\t------------\t-------\t-----\t---\t-----\t---"); |
134 |
10 Mar 05 |
nicklas |
227 |
} |
134 |
10 Mar 05 |
nicklas |
228 |
} |
134 |
10 Mar 05 |
nicklas |
229 |
static void write_item(int i, Group g) |
134 |
10 Mar 05 |
nicklas |
230 |
throws BaseException |
134 |
10 Mar 05 |
nicklas |
231 |
{ |
134 |
10 Mar 05 |
nicklas |
232 |
if (!TestUtil.getSilent()) System.out.println(i+":\t"+g.getId()+"\t"+g.getName()+"\t"+g.getDescription()); |
134 |
10 Mar 05 |
nicklas |
233 |
} |
134 |
10 Mar 05 |
nicklas |
234 |
static void write_item(int i, Role r) |
134 |
10 Mar 05 |
nicklas |
235 |
throws BaseException |
134 |
10 Mar 05 |
nicklas |
236 |
{ |
134 |
10 Mar 05 |
nicklas |
237 |
if (!TestUtil.getSilent()) System.out.println(i+":\t"+r.getId()+"\t"+r.getName()+"\t"+r.getDescription()); |
134 |
10 Mar 05 |
nicklas |
238 |
} |
259 |
24 Mar 05 |
nicklas |
239 |
static void write_item(int i, Project p) |
259 |
24 Mar 05 |
nicklas |
240 |
throws BaseException |
259 |
24 Mar 05 |
nicklas |
241 |
{ |
259 |
24 Mar 05 |
nicklas |
242 |
if (!TestUtil.getSilent()) System.out.println(i+":\t"+p.getId()+"\t"+p.getName()+"\t"+p.getDescription()); |
259 |
24 Mar 05 |
nicklas |
243 |
} |
134 |
10 Mar 05 |
nicklas |
244 |
static void write_item(int i, User u) |
134 |
10 Mar 05 |
nicklas |
245 |
throws BaseException |
134 |
10 Mar 05 |
nicklas |
246 |
{ |
134 |
10 Mar 05 |
nicklas |
247 |
if (!TestUtil.getSilent()) |
134 |
10 Mar 05 |
nicklas |
248 |
{ |
1466 |
14 Oct 05 |
nicklas |
249 |
System.out.println(i+":\t"+u.getId()+"\t"+u.getName()+"\t"+u.getLogin()+"\t"+u.getDescription()+ |
134 |
10 Mar 05 |
nicklas |
250 |
"\t"+u.getOrganisation()+","+u.getAddress()+","+u.getPhone()+","+u.getFax()+","+ |
134 |
10 Mar 05 |
nicklas |
251 |
u.getEmail()+","+u.getUrl() |
134 |
10 Mar 05 |
nicklas |
252 |
); |
134 |
10 Mar 05 |
nicklas |
253 |
} |
134 |
10 Mar 05 |
nicklas |
254 |
|
134 |
10 Mar 05 |
nicklas |
255 |
} |
134 |
10 Mar 05 |
nicklas |
256 |
static void write(String message) |
134 |
10 Mar 05 |
nicklas |
257 |
{ |
134 |
10 Mar 05 |
nicklas |
258 |
System.out.println(message); |
134 |
10 Mar 05 |
nicklas |
259 |
} |
134 |
10 Mar 05 |
nicklas |
260 |
|
134 |
10 Mar 05 |
nicklas |
261 |
static void test_change_password(int id) |
134 |
10 Mar 05 |
nicklas |
262 |
{ |
134 |
10 Mar 05 |
nicklas |
263 |
if (id == 0) return; |
134 |
10 Mar 05 |
nicklas |
264 |
DbControl dc = null; |
134 |
10 Mar 05 |
nicklas |
265 |
try |
134 |
10 Mar 05 |
nicklas |
266 |
{ |
134 |
10 Mar 05 |
nicklas |
267 |
dc = TestUtil.getDbControl(); |
134 |
10 Mar 05 |
nicklas |
268 |
User u = User.getById(dc, id); |
134 |
10 Mar 05 |
nicklas |
269 |
u.setPassword("new password"); |
134 |
10 Mar 05 |
nicklas |
270 |
dc.commit(); |
134 |
10 Mar 05 |
nicklas |
271 |
write_item(0, u); |
134 |
10 Mar 05 |
nicklas |
272 |
write("--Change password OK"); |
134 |
10 Mar 05 |
nicklas |
273 |
} |
358 |
04 Apr 05 |
nicklas |
274 |
catch (Throwable ex) |
134 |
10 Mar 05 |
nicklas |
275 |
{ |
134 |
10 Mar 05 |
nicklas |
276 |
write("--Change password FAILED"); |
134 |
10 Mar 05 |
nicklas |
277 |
ex.printStackTrace(); |
134 |
10 Mar 05 |
nicklas |
278 |
ok = false; |
134 |
10 Mar 05 |
nicklas |
279 |
} |
134 |
10 Mar 05 |
nicklas |
280 |
finally |
134 |
10 Mar 05 |
nicklas |
281 |
{ |
134 |
10 Mar 05 |
nicklas |
282 |
if (dc != null) dc.close(); |
134 |
10 Mar 05 |
nicklas |
283 |
} |
134 |
10 Mar 05 |
nicklas |
284 |
} |
134 |
10 Mar 05 |
nicklas |
285 |
|
177 |
16 Mar 05 |
nicklas |
286 |
static void test_duplicate(int id) |
177 |
16 Mar 05 |
nicklas |
287 |
{ |
177 |
16 Mar 05 |
nicklas |
288 |
if (id == 0) return; |
177 |
16 Mar 05 |
nicklas |
289 |
DbControl dc = null; |
177 |
16 Mar 05 |
nicklas |
290 |
try |
177 |
16 Mar 05 |
nicklas |
291 |
{ |
177 |
16 Mar 05 |
nicklas |
292 |
dc = TestUtil.getDbControl(); |
177 |
16 Mar 05 |
nicklas |
293 |
User u = User.getById(dc, id); |
177 |
16 Mar 05 |
nicklas |
294 |
User duplicate = User.getNew(dc, u.getLogin(), "password"); |
177 |
16 Mar 05 |
nicklas |
295 |
dc.saveItem(duplicate); |
177 |
16 Mar 05 |
nicklas |
296 |
dc.commit(); |
1595 |
14 Nov 05 |
nicklas |
297 |
dc = TestUtil.getDbControl(); |
331 |
01 Apr 05 |
nicklas |
298 |
dc.deleteItem(duplicate); |
331 |
01 Apr 05 |
nicklas |
299 |
dc.commit(); |
1500 |
21 Oct 05 |
nicklas |
300 |
throw new BaseException("Expected DatabaseException"); |
177 |
16 Mar 05 |
nicklas |
301 |
} |
1500 |
21 Oct 05 |
nicklas |
302 |
catch (DatabaseException ex) |
177 |
16 Mar 05 |
nicklas |
303 |
{ |
177 |
16 Mar 05 |
nicklas |
304 |
write("--Duplicate user OK"); |
177 |
16 Mar 05 |
nicklas |
305 |
} |
358 |
04 Apr 05 |
nicklas |
306 |
catch (Throwable ex) |
177 |
16 Mar 05 |
nicklas |
307 |
{ |
177 |
16 Mar 05 |
nicklas |
308 |
write("--Duplicate user FAILED"); |
177 |
16 Mar 05 |
nicklas |
309 |
ex.printStackTrace(); |
177 |
16 Mar 05 |
nicklas |
310 |
ok = false; |
177 |
16 Mar 05 |
nicklas |
311 |
} |
177 |
16 Mar 05 |
nicklas |
312 |
finally |
177 |
16 Mar 05 |
nicklas |
313 |
{ |
177 |
16 Mar 05 |
nicklas |
314 |
if (dc != null) dc.close(); |
177 |
16 Mar 05 |
nicklas |
315 |
} |
177 |
16 Mar 05 |
nicklas |
316 |
} |
134 |
10 Mar 05 |
nicklas |
317 |
|
292 |
31 Mar 05 |
nicklas |
318 |
static void test_listroles(int id, int expectedResults) |
134 |
10 Mar 05 |
nicklas |
319 |
{ |
134 |
10 Mar 05 |
nicklas |
320 |
if (id == 0) return; |
259 |
24 Mar 05 |
nicklas |
321 |
DbControl dc = null; |
134 |
10 Mar 05 |
nicklas |
322 |
try |
134 |
10 Mar 05 |
nicklas |
323 |
{ |
259 |
24 Mar 05 |
nicklas |
324 |
dc = TestUtil.getDbControl(); |
259 |
24 Mar 05 |
nicklas |
325 |
User u = User.getById(dc, id); |
1418 |
07 Oct 05 |
nicklas |
326 |
ItemResultList<Role> l = u.getRoles().list(dc); |
134 |
10 Mar 05 |
nicklas |
327 |
for (int i=0; i<l.size(); i++) |
134 |
10 Mar 05 |
nicklas |
328 |
{ |
259 |
24 Mar 05 |
nicklas |
329 |
write_item(i, l.get(i)); |
134 |
10 Mar 05 |
nicklas |
330 |
} |
292 |
31 Mar 05 |
nicklas |
331 |
if (expectedResults >= 0 && expectedResults != l.size()) |
292 |
31 Mar 05 |
nicklas |
332 |
{ |
292 |
31 Mar 05 |
nicklas |
333 |
throw new BaseException("Expected "+expectedResults+" results, not "+l.size()); |
292 |
31 Mar 05 |
nicklas |
334 |
} |
134 |
10 Mar 05 |
nicklas |
335 |
write("--List roles for user OK ("+l.size()+")"); |
134 |
10 Mar 05 |
nicklas |
336 |
} |
358 |
04 Apr 05 |
nicklas |
337 |
catch (Throwable ex) |
134 |
10 Mar 05 |
nicklas |
338 |
{ |
134 |
10 Mar 05 |
nicklas |
339 |
write("--List roles for user FAILED"); |
134 |
10 Mar 05 |
nicklas |
340 |
ex.printStackTrace(); |
134 |
10 Mar 05 |
nicklas |
341 |
ok = false; |
134 |
10 Mar 05 |
nicklas |
342 |
} |
134 |
10 Mar 05 |
nicklas |
343 |
finally |
134 |
10 Mar 05 |
nicklas |
344 |
{ |
259 |
24 Mar 05 |
nicklas |
345 |
if (dc != null) dc.close(); |
134 |
10 Mar 05 |
nicklas |
346 |
} |
134 |
10 Mar 05 |
nicklas |
347 |
} |
134 |
10 Mar 05 |
nicklas |
348 |
|
292 |
31 Mar 05 |
nicklas |
349 |
static void test_listgroups(int id, int expectedResults) |
134 |
10 Mar 05 |
nicklas |
350 |
{ |
134 |
10 Mar 05 |
nicklas |
351 |
if (id == 0) return; |
259 |
24 Mar 05 |
nicklas |
352 |
DbControl dc = null; |
134 |
10 Mar 05 |
nicklas |
353 |
try |
134 |
10 Mar 05 |
nicklas |
354 |
{ |
259 |
24 Mar 05 |
nicklas |
355 |
dc = TestUtil.getDbControl(); |
259 |
24 Mar 05 |
nicklas |
356 |
User u = User.getById(dc, id); |
1418 |
07 Oct 05 |
nicklas |
357 |
ItemResultList<Group> l = u.getGroups().list(dc); |
134 |
10 Mar 05 |
nicklas |
358 |
for (int i=0; i<l.size(); i++) |
134 |
10 Mar 05 |
nicklas |
359 |
{ |
259 |
24 Mar 05 |
nicklas |
360 |
write_item(i, l.get(i)); |
134 |
10 Mar 05 |
nicklas |
361 |
} |
292 |
31 Mar 05 |
nicklas |
362 |
if (expectedResults >= 0 && expectedResults != l.size()) |
292 |
31 Mar 05 |
nicklas |
363 |
{ |
292 |
31 Mar 05 |
nicklas |
364 |
throw new BaseException("Expected "+expectedResults+" results, not "+l.size()); |
292 |
31 Mar 05 |
nicklas |
365 |
} |
134 |
10 Mar 05 |
nicklas |
366 |
write("--List groups for user OK ("+l.size()+")"); |
134 |
10 Mar 05 |
nicklas |
367 |
} |
358 |
04 Apr 05 |
nicklas |
368 |
catch (Throwable ex) |
134 |
10 Mar 05 |
nicklas |
369 |
{ |
134 |
10 Mar 05 |
nicklas |
370 |
write("--List groups for user FAILED"); |
134 |
10 Mar 05 |
nicklas |
371 |
ex.printStackTrace(); |
134 |
10 Mar 05 |
nicklas |
372 |
ok = false; |
134 |
10 Mar 05 |
nicklas |
373 |
} |
134 |
10 Mar 05 |
nicklas |
374 |
finally |
134 |
10 Mar 05 |
nicklas |
375 |
{ |
259 |
24 Mar 05 |
nicklas |
376 |
if (dc != null) dc.close(); |
134 |
10 Mar 05 |
nicklas |
377 |
} |
134 |
10 Mar 05 |
nicklas |
378 |
} |
134 |
10 Mar 05 |
nicklas |
379 |
|
292 |
31 Mar 05 |
nicklas |
380 |
static void test_listprojects(int id, int expectedResults) |
259 |
24 Mar 05 |
nicklas |
381 |
{ |
259 |
24 Mar 05 |
nicklas |
382 |
if (id == 0) return; |
259 |
24 Mar 05 |
nicklas |
383 |
DbControl dc = null; |
259 |
24 Mar 05 |
nicklas |
384 |
try |
259 |
24 Mar 05 |
nicklas |
385 |
{ |
259 |
24 Mar 05 |
nicklas |
386 |
dc = TestUtil.getDbControl(); |
259 |
24 Mar 05 |
nicklas |
387 |
User u = User.getById(dc, id); |
1418 |
07 Oct 05 |
nicklas |
388 |
ItemResultList<Project> l = u.getProjects().list(dc); |
259 |
24 Mar 05 |
nicklas |
389 |
for (int i=0; i<l.size(); i++) |
259 |
24 Mar 05 |
nicklas |
390 |
{ |
259 |
24 Mar 05 |
nicklas |
391 |
write_item(i, l.get(i)); |
259 |
24 Mar 05 |
nicklas |
392 |
} |
292 |
31 Mar 05 |
nicklas |
393 |
if (expectedResults >= 0 && expectedResults != l.size()) |
292 |
31 Mar 05 |
nicklas |
394 |
{ |
292 |
31 Mar 05 |
nicklas |
395 |
throw new BaseException("Expected "+expectedResults+" results, not "+l.size()); |
292 |
31 Mar 05 |
nicklas |
396 |
} |
259 |
24 Mar 05 |
nicklas |
397 |
write("--List projects for user OK ("+l.size()+")"); |
259 |
24 Mar 05 |
nicklas |
398 |
} |
358 |
04 Apr 05 |
nicklas |
399 |
catch (Throwable ex) |
259 |
24 Mar 05 |
nicklas |
400 |
{ |
259 |
24 Mar 05 |
nicklas |
401 |
write("--List projects for user FAILED"); |
259 |
24 Mar 05 |
nicklas |
402 |
ex.printStackTrace(); |
259 |
24 Mar 05 |
nicklas |
403 |
ok = false; |
259 |
24 Mar 05 |
nicklas |
404 |
} |
259 |
24 Mar 05 |
nicklas |
405 |
finally |
259 |
24 Mar 05 |
nicklas |
406 |
{ |
259 |
24 Mar 05 |
nicklas |
407 |
if (dc != null) dc.close(); |
259 |
24 Mar 05 |
nicklas |
408 |
} |
259 |
24 Mar 05 |
nicklas |
409 |
} |
259 |
24 Mar 05 |
nicklas |
410 |
|
5690 |
11 Aug 11 |
nicklas |
411 |
static void test_set_owner(int id, Item itemType, int item_id) |
134 |
10 Mar 05 |
nicklas |
412 |
{ |
5690 |
11 Aug 11 |
nicklas |
413 |
if (id == 0 || item_id == 0) return; |
5690 |
11 Aug 11 |
nicklas |
414 |
DbControl dc = null; |
134 |
10 Mar 05 |
nicklas |
415 |
try |
134 |
10 Mar 05 |
nicklas |
416 |
{ |
5690 |
11 Aug 11 |
nicklas |
417 |
dc = TestUtil.getDbControl(); |
5690 |
11 Aug 11 |
nicklas |
418 |
User u = User.getById(dc, id); |
5690 |
11 Aug 11 |
nicklas |
419 |
Ownable o = (Ownable)itemType.getById(dc, item_id); |
5690 |
11 Aug 11 |
nicklas |
420 |
o.setOwner(u); |
5690 |
11 Aug 11 |
nicklas |
421 |
dc.commit(); |
134 |
10 Mar 05 |
nicklas |
422 |
write("--Set owner OK"); |
134 |
10 Mar 05 |
nicklas |
423 |
} |
358 |
04 Apr 05 |
nicklas |
424 |
catch (Throwable ex) |
134 |
10 Mar 05 |
nicklas |
425 |
{ |
134 |
10 Mar 05 |
nicklas |
426 |
write("--Set owner FAILED"); |
134 |
10 Mar 05 |
nicklas |
427 |
ex.printStackTrace(); |
134 |
10 Mar 05 |
nicklas |
428 |
ok = false; |
134 |
10 Mar 05 |
nicklas |
429 |
} |
134 |
10 Mar 05 |
nicklas |
430 |
finally |
134 |
10 Mar 05 |
nicklas |
431 |
{ |
5690 |
11 Aug 11 |
nicklas |
432 |
if (dc != null) dc.close(); |
134 |
10 Mar 05 |
nicklas |
433 |
} |
134 |
10 Mar 05 |
nicklas |
434 |
} |
134 |
10 Mar 05 |
nicklas |
435 |
|
134 |
10 Mar 05 |
nicklas |
436 |
} |
134 |
10 Mar 05 |
nicklas |
437 |
|