135 |
10 Mar 05 |
nicklas |
1 |
/* |
135 |
10 Mar 05 |
nicklas |
$Id$ |
135 |
10 Mar 05 |
nicklas |
3 |
|
3675 |
16 Aug 07 |
jari |
Copyright (C) 2005 Johan Enell, Nicklas Nordborg |
4889 |
06 Apr 09 |
nicklas |
Copyright (C) 2006 Jari Häkkinen, Nicklas Nordborg, Martin Svensson |
3675 |
16 Aug 07 |
jari |
Copyright (C) 2007 Johan Enell, Nicklas Nordborg |
135 |
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/ |
135 |
10 Mar 05 |
nicklas |
10 |
|
135 |
10 Mar 05 |
nicklas |
BASE is free software; you can redistribute it and/or |
135 |
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 |
135 |
10 Mar 05 |
nicklas |
of the License, or (at your option) any later version. |
135 |
10 Mar 05 |
nicklas |
15 |
|
135 |
10 Mar 05 |
nicklas |
BASE is distributed in the hope that it will be useful, |
135 |
10 Mar 05 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
135 |
10 Mar 05 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
135 |
10 Mar 05 |
nicklas |
GNU General Public License for more details. |
135 |
10 Mar 05 |
nicklas |
20 |
|
135 |
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/>. |
135 |
10 Mar 05 |
nicklas |
23 |
*/ |
135 |
10 Mar 05 |
nicklas |
24 |
import net.sf.basedb.core.*; |
2218 |
05 May 06 |
nicklas |
25 |
|
135 |
10 Mar 05 |
nicklas |
26 |
import java.util.Date; |
135 |
10 Mar 05 |
nicklas |
27 |
import java.util.List; |
135 |
10 Mar 05 |
nicklas |
28 |
import java.util.EnumSet; |
4347 |
18 Jun 08 |
nicklas |
29 |
import java.util.Set; |
135 |
10 Mar 05 |
nicklas |
30 |
|
135 |
10 Mar 05 |
nicklas |
31 |
public class TestProject |
135 |
10 Mar 05 |
nicklas |
32 |
{ |
135 |
10 Mar 05 |
nicklas |
33 |
|
135 |
10 Mar 05 |
nicklas |
34 |
static boolean ok = true; |
135 |
10 Mar 05 |
nicklas |
35 |
public static void main(String[] args) |
135 |
10 Mar 05 |
nicklas |
36 |
{ |
135 |
10 Mar 05 |
nicklas |
37 |
TestUtil.checkArgs(args); |
135 |
10 Mar 05 |
nicklas |
38 |
TestUtil.begin(); |
135 |
10 Mar 05 |
nicklas |
39 |
ok = test_all(); |
135 |
10 Mar 05 |
nicklas |
40 |
TestUtil.stop(); |
135 |
10 Mar 05 |
nicklas |
41 |
} |
135 |
10 Mar 05 |
nicklas |
42 |
|
135 |
10 Mar 05 |
nicklas |
43 |
static boolean test_all() |
135 |
10 Mar 05 |
nicklas |
44 |
{ |
135 |
10 Mar 05 |
nicklas |
45 |
write("++Testing projects"); |
135 |
10 Mar 05 |
nicklas |
46 |
write_header(); |
135 |
10 Mar 05 |
nicklas |
// Standard tests: create, load, list |
5368 |
22 Jun 10 |
nicklas |
48 |
int id = test_create("Test project #1", true); |
5368 |
22 Jun 10 |
nicklas |
49 |
int id2 = test_create("Test project #2", false); |
135 |
10 Mar 05 |
nicklas |
50 |
test_load(id); |
292 |
31 Mar 05 |
nicklas |
51 |
test_list(-1); |
135 |
10 Mar 05 |
nicklas |
52 |
|
135 |
10 Mar 05 |
nicklas |
// Extra tests: add, list and remove users, use project |
221 |
23 Mar 05 |
nicklas |
54 |
int userId = TestUser.test_create(true); |
292 |
31 Mar 05 |
nicklas |
55 |
test_listusers(id, 0); |
266 |
29 Mar 05 |
nicklas |
56 |
test_adduser(id, userId, EnumSet.of(Permission.WRITE)); |
292 |
31 Mar 05 |
nicklas |
57 |
test_listusers(id, 1); |
1529 |
27 Oct 05 |
nicklas |
58 |
if (TestUtil.waitBeforeDelete()) TestUtil.waitForEnter(); |
135 |
10 Mar 05 |
nicklas |
59 |
test_removeuser(id, userId); |
292 |
31 Mar 05 |
nicklas |
60 |
test_listusers(id, 0); |
555 |
11 May 05 |
nicklas |
61 |
TestUser.test_delete(userId); |
135 |
10 Mar 05 |
nicklas |
62 |
|
135 |
10 Mar 05 |
nicklas |
// Extra tests: add, list and remove groups |
221 |
23 Mar 05 |
nicklas |
64 |
int groupId = TestGroup.test_create(true); |
292 |
31 Mar 05 |
nicklas |
65 |
test_listgroups(id, 0); |
266 |
29 Mar 05 |
nicklas |
66 |
test_addgroup(id, groupId, EnumSet.of(Permission.WRITE)); |
292 |
31 Mar 05 |
nicklas |
67 |
test_listgroups(id, 1); |
1529 |
27 Oct 05 |
nicklas |
68 |
if (TestUtil.waitBeforeDelete()) TestUtil.waitForEnter(); |
135 |
10 Mar 05 |
nicklas |
69 |
test_removegroup(id, groupId); |
292 |
31 Mar 05 |
nicklas |
70 |
test_listgroups(id, 0); |
135 |
10 Mar 05 |
nicklas |
71 |
TestGroup.test_delete(groupId); |
4527 |
16 Sep 08 |
nicklas |
72 |
|
4527 |
16 Sep 08 |
nicklas |
// Extra test: set default item. |
4527 |
16 Sep 08 |
nicklas |
74 |
int samplingId = TestProtocol.test_create( |
5630 |
13 May 11 |
nicklas |
75 |
SystemItems.getId(Protocol.SAMPLING), "Sampling protocol", false); |
5650 |
01 Jun 11 |
nicklas |
76 |
int protocolId = TestProtocol.test_create(0, "Generic", false); |
4527 |
16 Sep 08 |
nicklas |
77 |
int hybStationId = TestHardware.test_create( |
5630 |
13 May 11 |
nicklas |
78 |
SystemItems.getId(Hardware.HYBRIDIZATION_STATION), "Hyb station", false); |
5650 |
01 Jun 11 |
nicklas |
79 |
test_defaultItem(id, Item.PROTOCOL, protocolId); |
5650 |
01 Jun 11 |
nicklas |
80 |
test_defaultItem(id, Item.PROTOCOL, samplingId); |
5650 |
01 Jun 11 |
nicklas |
81 |
test_defaultItem(id, Item.HARDWARE, hybStationId); |
5650 |
01 Jun 11 |
nicklas |
82 |
test_find_default(id, Item.PROTOCOL, 0, protocolId); |
5650 |
01 Jun 11 |
nicklas |
83 |
test_find_default(id, Item.PROTOCOL, SystemItems.getId(Protocol.SAMPLING), samplingId); |
4527 |
16 Sep 08 |
nicklas |
84 |
|
2218 |
05 May 06 |
nicklas |
// Extra test: activate project, create shared item, and list all items |
4689 |
08 Dec 08 |
nicklas |
86 |
Set<Permission> pp = Permission.expand(EnumSet.of(Permission.WRITE)); |
4689 |
08 Dec 08 |
nicklas |
87 |
test_activate_project(id, pp); |
6100 |
04 Sep 12 |
nicklas |
88 |
int sampleId = TestSample.test_create(0, "Sample (shared)", 0, false); |
5653 |
13 Jun 11 |
nicklas |
89 |
int hybId = TestPhysicalBioAssay.test_create(0, "Test hyb", 0, 1); |
4689 |
08 Dec 08 |
nicklas |
90 |
test_list_items(id, pp, 2); |
2218 |
05 May 06 |
nicklas |
91 |
test_noactive_project(); |
4527 |
16 Sep 08 |
nicklas |
92 |
|
2218 |
05 May 06 |
nicklas |
93 |
if (TestUtil.waitBeforeDelete()) TestUtil.waitForEnter(); |
4527 |
16 Sep 08 |
nicklas |
94 |
TestSample.test_delete(sampleId); |
5642 |
26 May 11 |
nicklas |
95 |
TestPhysicalBioAssay.test_delete(hybId); |
2906 |
14 Nov 06 |
martin |
96 |
|
135 |
10 Mar 05 |
nicklas |
// Standard test: Delete |
135 |
10 Mar 05 |
nicklas |
98 |
test_delete(id); |
221 |
23 Mar 05 |
nicklas |
99 |
test_delete(id2); |
4669 |
26 Nov 08 |
nicklas |
100 |
TestProtocol.test_delete(samplingId); |
4527 |
16 Sep 08 |
nicklas |
101 |
TestHardware.test_delete(hybStationId); |
4527 |
16 Sep 08 |
nicklas |
102 |
|
135 |
10 Mar 05 |
nicklas |
103 |
write("++Testing projects "+(ok ? "OK" : "Failed")+"\n"); |
135 |
10 Mar 05 |
nicklas |
104 |
return ok; |
135 |
10 Mar 05 |
nicklas |
105 |
} |
135 |
10 Mar 05 |
nicklas |
106 |
|
5650 |
01 Jun 11 |
nicklas |
107 |
static void test_defaultItem(int project_id, Item defaultType, int item_id) |
2906 |
14 Nov 06 |
martin |
108 |
{ |
4527 |
16 Sep 08 |
nicklas |
109 |
if (project_id == 0 || item_id == 0) return; |
2906 |
14 Nov 06 |
martin |
110 |
DbControl dc = null; |
2906 |
14 Nov 06 |
martin |
111 |
try |
2906 |
14 Nov 06 |
martin |
112 |
{ |
4527 |
16 Sep 08 |
nicklas |
113 |
dc = TestUtil.getDbControl(); |
4527 |
16 Sep 08 |
nicklas |
114 |
Project p = Project.getById(dc, project_id); |
5650 |
01 Jun 11 |
nicklas |
115 |
BasicItem defaultItem = defaultType.getById(dc, item_id); |
5650 |
01 Jun 11 |
nicklas |
116 |
p.addDefaultItem(defaultItem); |
2906 |
14 Nov 06 |
martin |
117 |
dc.commit(); |
4527 |
16 Sep 08 |
nicklas |
118 |
write("--Set default item OK (" + defaultType + "; id=" + item_id + ")"); |
2906 |
14 Nov 06 |
martin |
119 |
} |
2906 |
14 Nov 06 |
martin |
120 |
catch(Throwable ex) |
2906 |
14 Nov 06 |
martin |
121 |
{ |
4527 |
16 Sep 08 |
nicklas |
122 |
write("--Set default item FAILED (" + defaultType + "; id=" + item_id + ")"); |
2906 |
14 Nov 06 |
martin |
123 |
ex.printStackTrace(); |
2906 |
14 Nov 06 |
martin |
124 |
ok = false; |
2906 |
14 Nov 06 |
martin |
125 |
} |
2906 |
14 Nov 06 |
martin |
126 |
finally |
2906 |
14 Nov 06 |
martin |
127 |
{ |
2906 |
14 Nov 06 |
martin |
128 |
if (dc != null) dc.close(); |
2906 |
14 Nov 06 |
martin |
129 |
} |
2906 |
14 Nov 06 |
martin |
130 |
} |
2906 |
14 Nov 06 |
martin |
131 |
|
5650 |
01 Jun 11 |
nicklas |
132 |
static void test_find_default(int project_id, Item defaultType, int subtypeId, int expectedId) |
5650 |
01 Jun 11 |
nicklas |
133 |
{ |
5650 |
01 Jun 11 |
nicklas |
134 |
if (project_id == 0 || expectedId == 0) return; |
5650 |
01 Jun 11 |
nicklas |
135 |
DbControl dc = null; |
5650 |
01 Jun 11 |
nicklas |
136 |
try |
5650 |
01 Jun 11 |
nicklas |
137 |
{ |
5650 |
01 Jun 11 |
nicklas |
138 |
dc = TestUtil.getDbControl(); |
5650 |
01 Jun 11 |
nicklas |
139 |
Project p = Project.getById(dc, project_id); |
5650 |
01 Jun 11 |
nicklas |
140 |
|
5687 |
09 Aug 11 |
nicklas |
141 |
List<? extends BasicItem> found = null; |
5650 |
01 Jun 11 |
nicklas |
142 |
if (subtypeId == 0) |
5650 |
01 Jun 11 |
nicklas |
143 |
{ |
5687 |
09 Aug 11 |
nicklas |
144 |
found = p.findDefaultItems(dc, defaultType, true); |
5650 |
01 Jun 11 |
nicklas |
145 |
} |
5650 |
01 Jun 11 |
nicklas |
146 |
else |
5650 |
01 Jun 11 |
nicklas |
147 |
{ |
5650 |
01 Jun 11 |
nicklas |
148 |
ItemSubtype subtype = ItemSubtype.getById(dc, subtypeId); |
5687 |
09 Aug 11 |
nicklas |
149 |
found = p.findDefaultItems(dc, subtype, true); |
5650 |
01 Jun 11 |
nicklas |
150 |
} |
5650 |
01 Jun 11 |
nicklas |
151 |
|
5687 |
09 Aug 11 |
nicklas |
152 |
if (found == null || found.size() == 0) |
5650 |
01 Jun 11 |
nicklas |
153 |
{ |
5650 |
01 Jun 11 |
nicklas |
154 |
throw new ItemNotFoundException("Default " + defaultType + " for project: " + p); |
5650 |
01 Jun 11 |
nicklas |
155 |
} |
5687 |
09 Aug 11 |
nicklas |
156 |
else if (found.get(0).getId() != expectedId) |
5650 |
01 Jun 11 |
nicklas |
157 |
{ |
5650 |
01 Jun 11 |
nicklas |
158 |
throw new RuntimeException("Found " + found); |
5650 |
01 Jun 11 |
nicklas |
159 |
} |
5650 |
01 Jun 11 |
nicklas |
160 |
|
5650 |
01 Jun 11 |
nicklas |
161 |
dc.commit(); |
5650 |
01 Jun 11 |
nicklas |
162 |
write("--Find default item OK (" + defaultType + "; id=" + expectedId + ")"); |
5650 |
01 Jun 11 |
nicklas |
163 |
} |
5650 |
01 Jun 11 |
nicklas |
164 |
catch(Throwable ex) |
5650 |
01 Jun 11 |
nicklas |
165 |
{ |
5650 |
01 Jun 11 |
nicklas |
166 |
write("--Find default item FAILED (" + defaultType + "; id=" + expectedId + ")"); |
5650 |
01 Jun 11 |
nicklas |
167 |
ex.printStackTrace(); |
5650 |
01 Jun 11 |
nicklas |
168 |
ok = false; |
5650 |
01 Jun 11 |
nicklas |
169 |
} |
5650 |
01 Jun 11 |
nicklas |
170 |
finally |
5650 |
01 Jun 11 |
nicklas |
171 |
{ |
5650 |
01 Jun 11 |
nicklas |
172 |
if (dc != null) dc.close(); |
5650 |
01 Jun 11 |
nicklas |
173 |
} |
5650 |
01 Jun 11 |
nicklas |
174 |
} |
5650 |
01 Jun 11 |
nicklas |
175 |
|
5650 |
01 Jun 11 |
nicklas |
176 |
|
5368 |
22 Jun 10 |
nicklas |
177 |
static int test_create(String name, boolean setAll) |
135 |
10 Mar 05 |
nicklas |
178 |
{ |
135 |
10 Mar 05 |
nicklas |
179 |
if (!TestUtil.hasPermission(Permission.CREATE, Item.PROJECT)) return 0; |
135 |
10 Mar 05 |
nicklas |
180 |
int id = 0; |
135 |
10 Mar 05 |
nicklas |
181 |
DbControl dc = null; |
135 |
10 Mar 05 |
nicklas |
182 |
try |
135 |
10 Mar 05 |
nicklas |
183 |
{ |
135 |
10 Mar 05 |
nicklas |
184 |
dc = TestUtil.getDbControl(); |
135 |
10 Mar 05 |
nicklas |
185 |
Project p = Project.getNew(dc); |
5368 |
22 Jun 10 |
nicklas |
186 |
p.setName(name); |
221 |
23 Mar 05 |
nicklas |
187 |
if (setAll) |
221 |
23 Mar 05 |
nicklas |
188 |
{ |
221 |
23 Mar 05 |
nicklas |
189 |
p.setDescription("Added at "+new Date()); |
221 |
23 Mar 05 |
nicklas |
190 |
} |
135 |
10 Mar 05 |
nicklas |
191 |
dc.saveItem(p); |
135 |
10 Mar 05 |
nicklas |
192 |
dc.commit(); |
135 |
10 Mar 05 |
nicklas |
193 |
id = p.getId(); |
135 |
10 Mar 05 |
nicklas |
194 |
write_item(0, p); |
135 |
10 Mar 05 |
nicklas |
195 |
write("--Create project OK"); |
135 |
10 Mar 05 |
nicklas |
196 |
} |
358 |
04 Apr 05 |
nicklas |
197 |
catch (Throwable ex) |
135 |
10 Mar 05 |
nicklas |
198 |
{ |
135 |
10 Mar 05 |
nicklas |
199 |
write("--Create project FAILED"); |
135 |
10 Mar 05 |
nicklas |
200 |
ex.printStackTrace(); |
135 |
10 Mar 05 |
nicklas |
201 |
ok = false; |
135 |
10 Mar 05 |
nicklas |
202 |
} |
135 |
10 Mar 05 |
nicklas |
203 |
finally |
135 |
10 Mar 05 |
nicklas |
204 |
{ |
135 |
10 Mar 05 |
nicklas |
205 |
if (dc != null) dc.close(); |
135 |
10 Mar 05 |
nicklas |
206 |
} |
135 |
10 Mar 05 |
nicklas |
207 |
return id; |
135 |
10 Mar 05 |
nicklas |
208 |
} |
135 |
10 Mar 05 |
nicklas |
209 |
|
135 |
10 Mar 05 |
nicklas |
210 |
static void test_load(int id) |
135 |
10 Mar 05 |
nicklas |
211 |
{ |
135 |
10 Mar 05 |
nicklas |
212 |
if (id == 0) return; |
135 |
10 Mar 05 |
nicklas |
213 |
DbControl dc = null; |
135 |
10 Mar 05 |
nicklas |
214 |
try |
135 |
10 Mar 05 |
nicklas |
215 |
{ |
135 |
10 Mar 05 |
nicklas |
216 |
dc = TestUtil.getDbControl(); |
135 |
10 Mar 05 |
nicklas |
217 |
Project p = Project.getById(dc, id); |
135 |
10 Mar 05 |
nicklas |
218 |
write_item(0, p); |
135 |
10 Mar 05 |
nicklas |
219 |
write("--Load project OK"); |
135 |
10 Mar 05 |
nicklas |
220 |
} |
358 |
04 Apr 05 |
nicklas |
221 |
catch (Throwable ex) |
135 |
10 Mar 05 |
nicklas |
222 |
{ |
135 |
10 Mar 05 |
nicklas |
223 |
write("--Load project FAILED"); |
135 |
10 Mar 05 |
nicklas |
224 |
ex.printStackTrace(); |
135 |
10 Mar 05 |
nicklas |
225 |
ok = false; |
135 |
10 Mar 05 |
nicklas |
226 |
} |
135 |
10 Mar 05 |
nicklas |
227 |
finally |
135 |
10 Mar 05 |
nicklas |
228 |
{ |
135 |
10 Mar 05 |
nicklas |
229 |
if (dc != null) dc.close(); |
135 |
10 Mar 05 |
nicklas |
230 |
} |
135 |
10 Mar 05 |
nicklas |
231 |
} |
135 |
10 Mar 05 |
nicklas |
232 |
|
292 |
31 Mar 05 |
nicklas |
233 |
static void test_list(int expectedResults) |
135 |
10 Mar 05 |
nicklas |
234 |
{ |
135 |
10 Mar 05 |
nicklas |
235 |
DbControl dc = null; |
135 |
10 Mar 05 |
nicklas |
236 |
try |
135 |
10 Mar 05 |
nicklas |
237 |
{ |
256 |
24 Mar 05 |
nicklas |
238 |
dc = TestUtil.getDbControl(); |
1418 |
07 Oct 05 |
nicklas |
239 |
ItemResultList<Project> l = Project.getQuery().list(dc); |
135 |
10 Mar 05 |
nicklas |
240 |
for (int i = 0; i<l.size(); i++) |
135 |
10 Mar 05 |
nicklas |
241 |
{ |
256 |
24 Mar 05 |
nicklas |
242 |
write_item(i, l.get(i)); |
135 |
10 Mar 05 |
nicklas |
243 |
} |
292 |
31 Mar 05 |
nicklas |
244 |
if (expectedResults >= 0 && expectedResults != l.size()) |
292 |
31 Mar 05 |
nicklas |
245 |
{ |
292 |
31 Mar 05 |
nicklas |
246 |
throw new BaseException("Expected "+expectedResults+" results, not "+l.size()); |
292 |
31 Mar 05 |
nicklas |
247 |
} |
135 |
10 Mar 05 |
nicklas |
248 |
write("--List projects OK ("+l.size()+")"); |
135 |
10 Mar 05 |
nicklas |
249 |
} |
358 |
04 Apr 05 |
nicklas |
250 |
catch (Throwable ex) |
135 |
10 Mar 05 |
nicklas |
251 |
{ |
135 |
10 Mar 05 |
nicklas |
252 |
write("--List projects FAILED"); |
135 |
10 Mar 05 |
nicklas |
253 |
ex.printStackTrace(); |
135 |
10 Mar 05 |
nicklas |
254 |
ok = false; |
135 |
10 Mar 05 |
nicklas |
255 |
} |
135 |
10 Mar 05 |
nicklas |
256 |
finally |
135 |
10 Mar 05 |
nicklas |
257 |
{ |
256 |
24 Mar 05 |
nicklas |
258 |
if (dc != null) dc.close(); |
135 |
10 Mar 05 |
nicklas |
259 |
} |
135 |
10 Mar 05 |
nicklas |
260 |
} |
135 |
10 Mar 05 |
nicklas |
261 |
|
256 |
24 Mar 05 |
nicklas |
262 |
|
135 |
10 Mar 05 |
nicklas |
263 |
static void test_delete(int id) |
135 |
10 Mar 05 |
nicklas |
264 |
{ |
135 |
10 Mar 05 |
nicklas |
265 |
if (id == 0) return; |
135 |
10 Mar 05 |
nicklas |
266 |
DbControl dc = null; |
135 |
10 Mar 05 |
nicklas |
267 |
try |
135 |
10 Mar 05 |
nicklas |
268 |
{ |
135 |
10 Mar 05 |
nicklas |
269 |
dc = TestUtil.getDbControl(); |
135 |
10 Mar 05 |
nicklas |
270 |
Project p = Project.getById(dc, id); |
135 |
10 Mar 05 |
nicklas |
271 |
dc.deleteItem(p); |
5340 |
10 May 10 |
nicklas |
272 |
Set<ItemProxy> using = p.getUsingItems(); |
5340 |
10 May 10 |
nicklas |
273 |
if (using.size() > 0) |
5340 |
10 May 10 |
nicklas |
274 |
{ |
5340 |
10 May 10 |
nicklas |
275 |
throw new BaseException(p + " is used by " + using); |
5340 |
10 May 10 |
nicklas |
276 |
} |
135 |
10 Mar 05 |
nicklas |
277 |
dc.commit(); |
135 |
10 Mar 05 |
nicklas |
278 |
write("--Delete project OK"); |
135 |
10 Mar 05 |
nicklas |
279 |
} |
358 |
04 Apr 05 |
nicklas |
280 |
catch (Throwable ex) |
135 |
10 Mar 05 |
nicklas |
281 |
{ |
135 |
10 Mar 05 |
nicklas |
282 |
write("--Delete project FAILED"); |
135 |
10 Mar 05 |
nicklas |
283 |
ex.printStackTrace(); |
135 |
10 Mar 05 |
nicklas |
284 |
ok = false; |
135 |
10 Mar 05 |
nicklas |
285 |
} |
135 |
10 Mar 05 |
nicklas |
286 |
finally |
135 |
10 Mar 05 |
nicklas |
287 |
{ |
135 |
10 Mar 05 |
nicklas |
288 |
if (dc != null) dc.close(); |
135 |
10 Mar 05 |
nicklas |
289 |
} |
135 |
10 Mar 05 |
nicklas |
290 |
} |
135 |
10 Mar 05 |
nicklas |
291 |
|
135 |
10 Mar 05 |
nicklas |
292 |
static void write_header() |
135 |
10 Mar 05 |
nicklas |
293 |
{ |
135 |
10 Mar 05 |
nicklas |
294 |
if (!TestUtil.getSilent()) |
135 |
10 Mar 05 |
nicklas |
295 |
{ |
135 |
10 Mar 05 |
nicklas |
296 |
write(" \tID \tName \tDescription"); |
135 |
10 Mar 05 |
nicklas |
297 |
write("-- \t-- \t--------- \t-----------"); |
135 |
10 Mar 05 |
nicklas |
298 |
} |
135 |
10 Mar 05 |
nicklas |
299 |
} |
135 |
10 Mar 05 |
nicklas |
300 |
static void write_item(int i, Nameable n) |
135 |
10 Mar 05 |
nicklas |
301 |
throws BaseException |
135 |
10 Mar 05 |
nicklas |
302 |
{ |
135 |
10 Mar 05 |
nicklas |
303 |
write_item(i, n, ""); |
135 |
10 Mar 05 |
nicklas |
304 |
} |
135 |
10 Mar 05 |
nicklas |
305 |
static void write_item(int i, Nameable n, String extra) |
135 |
10 Mar 05 |
nicklas |
306 |
throws BaseException |
135 |
10 Mar 05 |
nicklas |
307 |
{ |
135 |
10 Mar 05 |
nicklas |
308 |
if (!TestUtil.getSilent()) System.out.println(i+":\t"+n.getId()+"\t"+n.getName()+"\t"+n.getDescription()+"\t"+extra); |
135 |
10 Mar 05 |
nicklas |
309 |
} |
135 |
10 Mar 05 |
nicklas |
310 |
static void write(String message) |
135 |
10 Mar 05 |
nicklas |
311 |
{ |
135 |
10 Mar 05 |
nicklas |
312 |
System.out.println(message); |
135 |
10 Mar 05 |
nicklas |
313 |
} |
135 |
10 Mar 05 |
nicklas |
314 |
|
2218 |
05 May 06 |
nicklas |
315 |
static void write_item(int i, Shareable item, String extra) |
2218 |
05 May 06 |
nicklas |
316 |
{ |
2218 |
05 May 06 |
nicklas |
317 |
if (!TestUtil.getSilent()) |
2218 |
05 May 06 |
nicklas |
318 |
{ |
2218 |
05 May 06 |
nicklas |
319 |
System.out.print(i+":\t"+item.getId()+"\t"+item.getType()); |
2218 |
05 May 06 |
nicklas |
320 |
if (item instanceof Nameable) |
2218 |
05 May 06 |
nicklas |
321 |
{ |
2218 |
05 May 06 |
nicklas |
322 |
Nameable nameable = (Nameable)item; |
2218 |
05 May 06 |
nicklas |
323 |
String description = nameable.getDescription(); |
2218 |
05 May 06 |
nicklas |
324 |
if (description != null && description.length() > 20) |
2218 |
05 May 06 |
nicklas |
325 |
{ |
2218 |
05 May 06 |
nicklas |
326 |
description = description.substring(0, 20) + "..."; |
2218 |
05 May 06 |
nicklas |
327 |
} |
2218 |
05 May 06 |
nicklas |
328 |
System.out.println("\t" + nameable.getName() + "\t" + description+"\t"+extra); |
2218 |
05 May 06 |
nicklas |
329 |
} |
2218 |
05 May 06 |
nicklas |
330 |
else |
2218 |
05 May 06 |
nicklas |
331 |
{ |
2218 |
05 May 06 |
nicklas |
332 |
System.out.println("\t" + item.toString()+"\t"+extra); |
2218 |
05 May 06 |
nicklas |
333 |
} |
2218 |
05 May 06 |
nicklas |
334 |
} |
2218 |
05 May 06 |
nicklas |
335 |
} |
2218 |
05 May 06 |
nicklas |
336 |
|
2218 |
05 May 06 |
nicklas |
337 |
|
135 |
10 Mar 05 |
nicklas |
338 |
static void test_adduser(int id, int userId, EnumSet<Permission> permissions) |
135 |
10 Mar 05 |
nicklas |
339 |
{ |
135 |
10 Mar 05 |
nicklas |
340 |
if (id == 0 || userId == 0) return; |
135 |
10 Mar 05 |
nicklas |
341 |
DbControl dc = null; |
135 |
10 Mar 05 |
nicklas |
342 |
try |
135 |
10 Mar 05 |
nicklas |
343 |
{ |
135 |
10 Mar 05 |
nicklas |
344 |
dc = TestUtil.getDbControl(); |
135 |
10 Mar 05 |
nicklas |
345 |
Project p = Project.getById(dc, id); |
135 |
10 Mar 05 |
nicklas |
346 |
User u = User.getById(dc, userId); |
135 |
10 Mar 05 |
nicklas |
347 |
p.setPermissions(u, permissions); |
135 |
10 Mar 05 |
nicklas |
348 |
dc.commit(); |
135 |
10 Mar 05 |
nicklas |
349 |
write("--Add user to project OK"); |
135 |
10 Mar 05 |
nicklas |
350 |
} |
358 |
04 Apr 05 |
nicklas |
351 |
catch (Throwable ex) |
135 |
10 Mar 05 |
nicklas |
352 |
{ |
135 |
10 Mar 05 |
nicklas |
353 |
write("--Add user to project FAILED"); |
135 |
10 Mar 05 |
nicklas |
354 |
ex.printStackTrace(); |
135 |
10 Mar 05 |
nicklas |
355 |
ok = false; |
135 |
10 Mar 05 |
nicklas |
356 |
} |
135 |
10 Mar 05 |
nicklas |
357 |
finally |
135 |
10 Mar 05 |
nicklas |
358 |
{ |
135 |
10 Mar 05 |
nicklas |
359 |
if (dc != null) dc.close(); |
135 |
10 Mar 05 |
nicklas |
360 |
} |
135 |
10 Mar 05 |
nicklas |
361 |
} |
135 |
10 Mar 05 |
nicklas |
362 |
|
292 |
31 Mar 05 |
nicklas |
363 |
static void test_listusers(int id, int expectedResults) |
135 |
10 Mar 05 |
nicklas |
364 |
{ |
135 |
10 Mar 05 |
nicklas |
365 |
if (id == 0) return; |
261 |
24 Mar 05 |
nicklas |
366 |
DbControl dc = null; |
135 |
10 Mar 05 |
nicklas |
367 |
try |
135 |
10 Mar 05 |
nicklas |
368 |
{ |
261 |
24 Mar 05 |
nicklas |
369 |
dc = TestUtil.getDbControl(); |
261 |
24 Mar 05 |
nicklas |
370 |
Project p = Project.getById(dc, id); |
1418 |
07 Oct 05 |
nicklas |
371 |
ItemResultList<User> l = p.getUsers().list(dc); |
135 |
10 Mar 05 |
nicklas |
372 |
for (int i=0; i<l.size(); i++) |
135 |
10 Mar 05 |
nicklas |
373 |
{ |
261 |
24 Mar 05 |
nicklas |
374 |
User u = l.get(i); |
261 |
24 Mar 05 |
nicklas |
375 |
write_item(i, u, ":"+p.getPermissions(u)); |
135 |
10 Mar 05 |
nicklas |
376 |
} |
292 |
31 Mar 05 |
nicklas |
377 |
if (expectedResults >= 0 && expectedResults != l.size()) |
292 |
31 Mar 05 |
nicklas |
378 |
{ |
292 |
31 Mar 05 |
nicklas |
379 |
throw new BaseException("Expected "+expectedResults+" results, not "+l.size()); |
292 |
31 Mar 05 |
nicklas |
380 |
} |
135 |
10 Mar 05 |
nicklas |
381 |
write("--List users in project OK ("+l.size()+")"); |
135 |
10 Mar 05 |
nicklas |
382 |
} |
358 |
04 Apr 05 |
nicklas |
383 |
catch (Throwable ex) |
135 |
10 Mar 05 |
nicklas |
384 |
{ |
135 |
10 Mar 05 |
nicklas |
385 |
write("--List users in project FAILED"); |
135 |
10 Mar 05 |
nicklas |
386 |
ex.printStackTrace(); |
135 |
10 Mar 05 |
nicklas |
387 |
ok = false; |
135 |
10 Mar 05 |
nicklas |
388 |
} |
135 |
10 Mar 05 |
nicklas |
389 |
finally |
135 |
10 Mar 05 |
nicklas |
390 |
{ |
261 |
24 Mar 05 |
nicklas |
391 |
if (dc != null) dc.close(); |
135 |
10 Mar 05 |
nicklas |
392 |
} |
135 |
10 Mar 05 |
nicklas |
393 |
} |
135 |
10 Mar 05 |
nicklas |
394 |
|
135 |
10 Mar 05 |
nicklas |
395 |
static void test_removeuser(int id, int userId) |
135 |
10 Mar 05 |
nicklas |
396 |
{ |
135 |
10 Mar 05 |
nicklas |
397 |
if (id == 0 || userId == 0) return; |
135 |
10 Mar 05 |
nicklas |
398 |
DbControl dc = null; |
135 |
10 Mar 05 |
nicklas |
399 |
try |
135 |
10 Mar 05 |
nicklas |
400 |
{ |
135 |
10 Mar 05 |
nicklas |
401 |
dc = TestUtil.getDbControl(); |
135 |
10 Mar 05 |
nicklas |
402 |
Project p = Project.getById(dc, id); |
135 |
10 Mar 05 |
nicklas |
403 |
User u = User.getById(dc, userId); |
135 |
10 Mar 05 |
nicklas |
404 |
p.setPermissions(u, EnumSet.noneOf(Permission.class)); |
135 |
10 Mar 05 |
nicklas |
405 |
dc.commit(); |
135 |
10 Mar 05 |
nicklas |
406 |
write("--Remove user from project OK"); |
135 |
10 Mar 05 |
nicklas |
407 |
} |
358 |
04 Apr 05 |
nicklas |
408 |
catch (Throwable ex) |
135 |
10 Mar 05 |
nicklas |
409 |
{ |
135 |
10 Mar 05 |
nicklas |
410 |
write("--Remove user from project FAILED"); |
135 |
10 Mar 05 |
nicklas |
411 |
ex.printStackTrace(); |
135 |
10 Mar 05 |
nicklas |
412 |
ok = false; |
135 |
10 Mar 05 |
nicklas |
413 |
} |
135 |
10 Mar 05 |
nicklas |
414 |
finally |
135 |
10 Mar 05 |
nicklas |
415 |
{ |
135 |
10 Mar 05 |
nicklas |
416 |
if (dc != null) dc.close(); |
135 |
10 Mar 05 |
nicklas |
417 |
} |
135 |
10 Mar 05 |
nicklas |
418 |
} |
135 |
10 Mar 05 |
nicklas |
419 |
|
135 |
10 Mar 05 |
nicklas |
420 |
static void test_addgroup(int id, int groupId, EnumSet<Permission> permissions) |
135 |
10 Mar 05 |
nicklas |
421 |
{ |
135 |
10 Mar 05 |
nicklas |
422 |
if (id == 0 || groupId == 0) return; |
135 |
10 Mar 05 |
nicklas |
423 |
DbControl dc = null; |
135 |
10 Mar 05 |
nicklas |
424 |
try |
135 |
10 Mar 05 |
nicklas |
425 |
{ |
135 |
10 Mar 05 |
nicklas |
426 |
dc = TestUtil.getDbControl(); |
135 |
10 Mar 05 |
nicklas |
427 |
Project p = Project.getById(dc, id); |
135 |
10 Mar 05 |
nicklas |
428 |
Group g = Group.getById(dc, groupId); |
135 |
10 Mar 05 |
nicklas |
429 |
p.setPermissions(g, permissions); |
135 |
10 Mar 05 |
nicklas |
430 |
dc.commit(); |
135 |
10 Mar 05 |
nicklas |
431 |
write("--Add group to project OK"); |
135 |
10 Mar 05 |
nicklas |
432 |
} |
358 |
04 Apr 05 |
nicklas |
433 |
catch (Throwable ex) |
135 |
10 Mar 05 |
nicklas |
434 |
{ |
135 |
10 Mar 05 |
nicklas |
435 |
write("--Add group to project FAILED"); |
135 |
10 Mar 05 |
nicklas |
436 |
ex.printStackTrace(); |
135 |
10 Mar 05 |
nicklas |
437 |
ok = false; |
135 |
10 Mar 05 |
nicklas |
438 |
} |
135 |
10 Mar 05 |
nicklas |
439 |
finally |
135 |
10 Mar 05 |
nicklas |
440 |
{ |
135 |
10 Mar 05 |
nicklas |
441 |
if (dc != null) dc.close(); |
135 |
10 Mar 05 |
nicklas |
442 |
} |
135 |
10 Mar 05 |
nicklas |
443 |
} |
135 |
10 Mar 05 |
nicklas |
444 |
|
292 |
31 Mar 05 |
nicklas |
445 |
static void test_listgroups(int id, int expectedResults) |
135 |
10 Mar 05 |
nicklas |
446 |
{ |
135 |
10 Mar 05 |
nicklas |
447 |
if (id == 0) return; |
261 |
24 Mar 05 |
nicklas |
448 |
DbControl dc = null; |
135 |
10 Mar 05 |
nicklas |
449 |
try |
135 |
10 Mar 05 |
nicklas |
450 |
{ |
261 |
24 Mar 05 |
nicklas |
451 |
dc = TestUtil.getDbControl(); |
261 |
24 Mar 05 |
nicklas |
452 |
Project p = Project.getById(dc, id); |
1418 |
07 Oct 05 |
nicklas |
453 |
ItemResultList<Group> l = p.getGroups().list(dc); |
135 |
10 Mar 05 |
nicklas |
454 |
for (int i=0; i<l.size(); i++) |
135 |
10 Mar 05 |
nicklas |
455 |
{ |
261 |
24 Mar 05 |
nicklas |
456 |
Group g = l.get(i); |
261 |
24 Mar 05 |
nicklas |
457 |
write_item(i, g, ":"+p.getPermissions(g)); |
135 |
10 Mar 05 |
nicklas |
458 |
} |
292 |
31 Mar 05 |
nicklas |
459 |
if (expectedResults >= 0 && expectedResults != l.size()) |
292 |
31 Mar 05 |
nicklas |
460 |
{ |
292 |
31 Mar 05 |
nicklas |
461 |
throw new BaseException("Expected "+expectedResults+" results, not "+l.size()); |
292 |
31 Mar 05 |
nicklas |
462 |
} |
135 |
10 Mar 05 |
nicklas |
463 |
write("--List groups in project OK ("+l.size()+")"); |
135 |
10 Mar 05 |
nicklas |
464 |
} |
358 |
04 Apr 05 |
nicklas |
465 |
catch (Throwable ex) |
135 |
10 Mar 05 |
nicklas |
466 |
{ |
135 |
10 Mar 05 |
nicklas |
467 |
write("--List groups in project FAILED"); |
135 |
10 Mar 05 |
nicklas |
468 |
ex.printStackTrace(); |
135 |
10 Mar 05 |
nicklas |
469 |
ok = false; |
135 |
10 Mar 05 |
nicklas |
470 |
} |
135 |
10 Mar 05 |
nicklas |
471 |
finally |
135 |
10 Mar 05 |
nicklas |
472 |
{ |
261 |
24 Mar 05 |
nicklas |
473 |
if (dc != null) dc.close(); |
135 |
10 Mar 05 |
nicklas |
474 |
} |
135 |
10 Mar 05 |
nicklas |
475 |
} |
261 |
24 Mar 05 |
nicklas |
476 |
|
135 |
10 Mar 05 |
nicklas |
477 |
static void test_removegroup(int id, int groupId) |
135 |
10 Mar 05 |
nicklas |
478 |
{ |
135 |
10 Mar 05 |
nicklas |
479 |
if (id == 0 || groupId == 0) return; |
135 |
10 Mar 05 |
nicklas |
480 |
DbControl dc = null; |
135 |
10 Mar 05 |
nicklas |
481 |
try |
135 |
10 Mar 05 |
nicklas |
482 |
{ |
135 |
10 Mar 05 |
nicklas |
483 |
dc = TestUtil.getDbControl(); |
135 |
10 Mar 05 |
nicklas |
484 |
Project p = Project.getById(dc, id); |
135 |
10 Mar 05 |
nicklas |
485 |
Group g = Group.getById(dc, groupId); |
135 |
10 Mar 05 |
nicklas |
486 |
p.setPermissions(g, EnumSet.noneOf(Permission.class)); |
135 |
10 Mar 05 |
nicklas |
487 |
dc.commit(); |
135 |
10 Mar 05 |
nicklas |
488 |
write("--Remove group from project OK"); |
135 |
10 Mar 05 |
nicklas |
489 |
} |
358 |
04 Apr 05 |
nicklas |
490 |
catch (Throwable ex) |
135 |
10 Mar 05 |
nicklas |
491 |
{ |
135 |
10 Mar 05 |
nicklas |
492 |
write("--Remove group from project FAILED"); |
135 |
10 Mar 05 |
nicklas |
493 |
ex.printStackTrace(); |
135 |
10 Mar 05 |
nicklas |
494 |
ok = false; |
135 |
10 Mar 05 |
nicklas |
495 |
} |
135 |
10 Mar 05 |
nicklas |
496 |
finally |
135 |
10 Mar 05 |
nicklas |
497 |
{ |
135 |
10 Mar 05 |
nicklas |
498 |
if (dc != null) dc.close(); |
135 |
10 Mar 05 |
nicklas |
499 |
} |
135 |
10 Mar 05 |
nicklas |
500 |
} |
135 |
10 Mar 05 |
nicklas |
501 |
|
4689 |
08 Dec 08 |
nicklas |
502 |
static void test_activate_project(int id, Set<Permission> permissions) |
135 |
10 Mar 05 |
nicklas |
503 |
{ |
135 |
10 Mar 05 |
nicklas |
504 |
if (id == 0) return; |
141 |
10 Mar 05 |
nicklas |
505 |
DbControl dc = null; |
135 |
10 Mar 05 |
nicklas |
506 |
try |
135 |
10 Mar 05 |
nicklas |
507 |
{ |
141 |
10 Mar 05 |
nicklas |
508 |
dc = TestUtil.getDbControl(); |
141 |
10 Mar 05 |
nicklas |
509 |
Project p = Project.getById(dc, id); |
4689 |
08 Dec 08 |
nicklas |
510 |
if (permissions != null) p.setAutoPermission(permissions); |
4689 |
08 Dec 08 |
nicklas |
511 |
dc.commit(); |
141 |
10 Mar 05 |
nicklas |
512 |
TestUtil.getSessionControl().setActiveProject(p); |
7245 |
01 Dec 16 |
nicklas |
513 |
write("--Activate project OK (" + p.getName() + ")"); |
135 |
10 Mar 05 |
nicklas |
514 |
} |
358 |
04 Apr 05 |
nicklas |
515 |
catch (Throwable ex) |
135 |
10 Mar 05 |
nicklas |
516 |
{ |
2218 |
05 May 06 |
nicklas |
517 |
write("--Activate project FAILED"); |
135 |
10 Mar 05 |
nicklas |
518 |
ex.printStackTrace(); |
135 |
10 Mar 05 |
nicklas |
519 |
ok = false; |
135 |
10 Mar 05 |
nicklas |
520 |
} |
135 |
10 Mar 05 |
nicklas |
521 |
finally |
135 |
10 Mar 05 |
nicklas |
522 |
{ |
141 |
10 Mar 05 |
nicklas |
523 |
if (dc != null) dc.close(); |
135 |
10 Mar 05 |
nicklas |
524 |
} |
135 |
10 Mar 05 |
nicklas |
525 |
} |
2218 |
05 May 06 |
nicklas |
526 |
|
2218 |
05 May 06 |
nicklas |
527 |
static void test_noactive_project() |
2218 |
05 May 06 |
nicklas |
528 |
{ |
2218 |
05 May 06 |
nicklas |
529 |
try |
2218 |
05 May 06 |
nicklas |
530 |
{ |
2218 |
05 May 06 |
nicklas |
531 |
TestUtil.getSessionControl().setActiveProject(null); |
2218 |
05 May 06 |
nicklas |
532 |
write("--No active project OK"); |
2218 |
05 May 06 |
nicklas |
533 |
} |
2218 |
05 May 06 |
nicklas |
534 |
catch (Throwable ex) |
2218 |
05 May 06 |
nicklas |
535 |
{ |
2218 |
05 May 06 |
nicklas |
536 |
write("--No active project FAILED"); |
2218 |
05 May 06 |
nicklas |
537 |
ex.printStackTrace(); |
2218 |
05 May 06 |
nicklas |
538 |
ok = false; |
2218 |
05 May 06 |
nicklas |
539 |
} |
2218 |
05 May 06 |
nicklas |
540 |
} |
135 |
10 Mar 05 |
nicklas |
541 |
|
4689 |
08 Dec 08 |
nicklas |
542 |
static void test_list_items(int id, Set<Permission> expectedPermissions, int expectedResults) |
2218 |
05 May 06 |
nicklas |
543 |
{ |
2218 |
05 May 06 |
nicklas |
544 |
DbControl dc = null; |
2218 |
05 May 06 |
nicklas |
545 |
try |
2218 |
05 May 06 |
nicklas |
546 |
{ |
2218 |
05 May 06 |
nicklas |
547 |
TestUtil.getSessionControl().reloadPermissions(); |
2218 |
05 May 06 |
nicklas |
548 |
dc = TestUtil.getDbControl(); |
2218 |
05 May 06 |
nicklas |
549 |
Project p = Project.getById(dc, id); |
5167 |
28 Oct 09 |
nicklas |
550 |
List<Shareable> l = p.getItems((Set<Item>)null, 0, 0, null, null, null); |
2218 |
05 May 06 |
nicklas |
551 |
for (int i = 0; i<l.size(); i++) |
2218 |
05 May 06 |
nicklas |
552 |
{ |
2218 |
05 May 06 |
nicklas |
553 |
Shareable s = l.get(i); |
4689 |
08 Dec 08 |
nicklas |
554 |
Set<Permission> pp = s.getProjectKey().getPermissions(p); |
4689 |
08 Dec 08 |
nicklas |
555 |
write_item(i, s, ":" + pp); |
4689 |
08 Dec 08 |
nicklas |
556 |
if (expectedPermissions != null && !expectedPermissions.equals(pp)) |
4689 |
08 Dec 08 |
nicklas |
557 |
{ |
4689 |
08 Dec 08 |
nicklas |
558 |
throw new BaseException("Expected permission: " + expectedPermissions + "; not " + pp); |
4689 |
08 Dec 08 |
nicklas |
559 |
} |
2218 |
05 May 06 |
nicklas |
560 |
} |
2218 |
05 May 06 |
nicklas |
561 |
if (expectedResults >= 0 && expectedResults != l.size()) |
2218 |
05 May 06 |
nicklas |
562 |
{ |
2218 |
05 May 06 |
nicklas |
563 |
throw new BaseException("Expected "+expectedResults+" results, not "+l.size()); |
2218 |
05 May 06 |
nicklas |
564 |
} |
2218 |
05 May 06 |
nicklas |
565 |
write("--List items in project OK ("+l.size()+")"); |
2218 |
05 May 06 |
nicklas |
566 |
} |
2218 |
05 May 06 |
nicklas |
567 |
catch (Throwable ex) |
2218 |
05 May 06 |
nicklas |
568 |
{ |
2218 |
05 May 06 |
nicklas |
569 |
write("--List items in project FAILED"); |
2218 |
05 May 06 |
nicklas |
570 |
ex.printStackTrace(); |
2218 |
05 May 06 |
nicklas |
571 |
ok = false; |
2218 |
05 May 06 |
nicklas |
572 |
} |
3087 |
29 Jan 07 |
nicklas |
573 |
finally |
3087 |
29 Jan 07 |
nicklas |
574 |
{ |
3087 |
29 Jan 07 |
nicklas |
575 |
if (dc != null) dc.close(); |
3087 |
29 Jan 07 |
nicklas |
576 |
} |
2218 |
05 May 06 |
nicklas |
577 |
} |
135 |
10 Mar 05 |
nicklas |
578 |
} |
135 |
10 Mar 05 |
nicklas |
579 |
|