557 |
11 May 05 |
enell |
1 |
import java.util.Date; |
5340 |
10 May 10 |
nicklas |
2 |
import java.util.Set; |
557 |
11 May 05 |
enell |
3 |
|
4740 |
05 Feb 09 |
nicklas |
4 |
import net.sf.basedb.core.AnnotationType; |
557 |
11 May 05 |
enell |
5 |
import net.sf.basedb.core.BaseException; |
557 |
11 May 05 |
enell |
6 |
import net.sf.basedb.core.DbControl; |
557 |
11 May 05 |
enell |
7 |
import net.sf.basedb.core.File; |
5340 |
10 May 10 |
nicklas |
8 |
import net.sf.basedb.core.ItemProxy; |
5630 |
13 May 11 |
nicklas |
9 |
import net.sf.basedb.core.ItemSubtype; |
557 |
11 May 05 |
enell |
10 |
import net.sf.basedb.core.Protocol; |
1418 |
07 Oct 05 |
nicklas |
11 |
import net.sf.basedb.core.ItemResultList; |
557 |
11 May 05 |
enell |
12 |
import net.sf.basedb.core.SystemItems; |
557 |
11 May 05 |
enell |
13 |
|
557 |
11 May 05 |
enell |
14 |
/* |
557 |
11 May 05 |
enell |
$Id$ |
557 |
11 May 05 |
enell |
16 |
|
3675 |
16 Aug 07 |
jari |
Copyright (C) 2005 Johan Enell, Nicklas Nordborg |
4889 |
06 Apr 09 |
nicklas |
Copyright (C) 2006 Jari Häkkinen |
557 |
11 May 05 |
enell |
19 |
|
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/ |
557 |
11 May 05 |
enell |
22 |
|
557 |
11 May 05 |
enell |
BASE is free software; you can redistribute it and/or |
557 |
11 May 05 |
enell |
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 |
557 |
11 May 05 |
enell |
of the License, or (at your option) any later version. |
557 |
11 May 05 |
enell |
27 |
|
557 |
11 May 05 |
enell |
BASE is distributed in the hope that it will be useful, |
557 |
11 May 05 |
enell |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
557 |
11 May 05 |
enell |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
557 |
11 May 05 |
enell |
GNU General Public License for more details. |
557 |
11 May 05 |
enell |
32 |
|
557 |
11 May 05 |
enell |
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/>. |
557 |
11 May 05 |
enell |
35 |
*/ |
557 |
11 May 05 |
enell |
36 |
|
557 |
11 May 05 |
enell |
37 |
public class TestProtocol |
557 |
11 May 05 |
enell |
38 |
{ |
557 |
11 May 05 |
enell |
39 |
static boolean ok = true; |
557 |
11 May 05 |
enell |
40 |
|
557 |
11 May 05 |
enell |
41 |
public static void main(String[] args) |
557 |
11 May 05 |
enell |
42 |
{ |
557 |
11 May 05 |
enell |
43 |
TestUtil.checkArgs(args); |
557 |
11 May 05 |
enell |
44 |
TestUtil.begin(); |
557 |
11 May 05 |
enell |
45 |
ok = test_all(); |
557 |
11 May 05 |
enell |
46 |
TestUtil.stop(); |
557 |
11 May 05 |
enell |
47 |
} |
557 |
11 May 05 |
enell |
48 |
|
557 |
11 May 05 |
enell |
49 |
static boolean test_all() |
557 |
11 May 05 |
enell |
50 |
{ |
557 |
11 May 05 |
enell |
51 |
write("++Testing protocol"); |
557 |
11 May 05 |
enell |
52 |
write_header(); |
557 |
11 May 05 |
enell |
// Standard tests: create, load, list |
5630 |
13 May 11 |
nicklas |
54 |
int id1 = test_create(SystemItems.getId(Protocol.SAMPLING), null, true); |
5630 |
13 May 11 |
nicklas |
55 |
int id2 = test_create(SystemItems.getId(Protocol.LABELING), null, false); |
664 |
27 May 05 |
enell |
56 |
test_load(id1); |
557 |
11 May 05 |
enell |
57 |
test_list(); |
557 |
11 May 05 |
enell |
58 |
|
557 |
11 May 05 |
enell |
// Extra tests: attach file to protocol |
3719 |
12 Sep 07 |
nicklas |
60 |
int fileId = TestFile.test_create("test.upload.txt", false, false); |
664 |
27 May 05 |
enell |
61 |
test_attachfile(id1, fileId); |
1529 |
27 Oct 05 |
nicklas |
62 |
|
1529 |
27 Oct 05 |
nicklas |
63 |
if (TestUtil.waitBeforeDelete()) TestUtil.waitForEnter(); |
664 |
27 May 05 |
enell |
64 |
test_deattachfile(id1); |
557 |
11 May 05 |
enell |
65 |
TestFile.test_delete(fileId); |
557 |
11 May 05 |
enell |
66 |
|
557 |
11 May 05 |
enell |
// Standard test: Delete |
664 |
27 May 05 |
enell |
68 |
test_delete(id1); |
664 |
27 May 05 |
enell |
69 |
test_delete(id2); |
557 |
11 May 05 |
enell |
70 |
write("++Testing protocol "+(ok ? "OK" : "Failed")+"\n"); |
557 |
11 May 05 |
enell |
71 |
return ok; |
557 |
11 May 05 |
enell |
72 |
} |
557 |
11 May 05 |
enell |
73 |
|
4366 |
02 Jul 08 |
nicklas |
74 |
static int test_create(int protocoltypeId, String name, boolean setAll) |
557 |
11 May 05 |
enell |
75 |
{ |
557 |
11 May 05 |
enell |
76 |
int id = 0; |
557 |
11 May 05 |
enell |
77 |
DbControl dc = null; |
557 |
11 May 05 |
enell |
78 |
try |
557 |
11 May 05 |
enell |
79 |
{ |
557 |
11 May 05 |
enell |
80 |
dc = TestUtil.getDbControl(); |
5630 |
13 May 11 |
nicklas |
81 |
Protocol p = Protocol.getNew(dc); |
5630 |
13 May 11 |
nicklas |
82 |
if (protocoltypeId > 0) |
5630 |
13 May 11 |
nicklas |
83 |
{ |
5630 |
13 May 11 |
nicklas |
84 |
p.setItemSubtype(ItemSubtype.getById(dc, protocoltypeId)); |
5630 |
13 May 11 |
nicklas |
85 |
} |
664 |
27 May 05 |
enell |
86 |
if(setAll) |
664 |
27 May 05 |
enell |
87 |
{ |
664 |
27 May 05 |
enell |
88 |
p.setName("Test protocol"); |
664 |
27 May 05 |
enell |
89 |
p.setDescription("Added at "+new Date()); |
5248 |
17 Feb 10 |
nicklas |
90 |
p.setExternalId("External protocol id"); |
664 |
27 May 05 |
enell |
91 |
} |
5249 |
17 Feb 10 |
nicklas |
92 |
if (name != null) |
5249 |
17 Feb 10 |
nicklas |
93 |
{ |
5249 |
17 Feb 10 |
nicklas |
94 |
p.setName(name); |
5249 |
17 Feb 10 |
nicklas |
95 |
p.setExternalId(name + ".id"); |
5249 |
17 Feb 10 |
nicklas |
96 |
} |
557 |
11 May 05 |
enell |
97 |
dc.saveItem(p); |
557 |
11 May 05 |
enell |
98 |
dc.commit(); |
557 |
11 May 05 |
enell |
99 |
id = p.getId(); |
806 |
22 Jun 05 |
nicklas |
100 |
dc = TestUtil.getDbControl(); |
5060 |
19 Aug 09 |
nicklas |
101 |
dc.reattachItem(p, false); |
557 |
11 May 05 |
enell |
102 |
write_item(0, p); |
557 |
11 May 05 |
enell |
103 |
write("--Create protocol OK"); |
557 |
11 May 05 |
enell |
104 |
} |
816 |
23 Jun 05 |
nicklas |
105 |
catch (Throwable ex) |
557 |
11 May 05 |
enell |
106 |
{ |
557 |
11 May 05 |
enell |
107 |
write("--Create protocol FAILED"); |
557 |
11 May 05 |
enell |
108 |
ex.printStackTrace(); |
557 |
11 May 05 |
enell |
109 |
ok = false; |
557 |
11 May 05 |
enell |
110 |
} |
557 |
11 May 05 |
enell |
111 |
finally |
557 |
11 May 05 |
enell |
112 |
{ |
557 |
11 May 05 |
enell |
113 |
if (dc != null) dc.close(); |
557 |
11 May 05 |
enell |
114 |
} |
557 |
11 May 05 |
enell |
115 |
return id; |
557 |
11 May 05 |
enell |
116 |
} |
557 |
11 May 05 |
enell |
117 |
|
557 |
11 May 05 |
enell |
118 |
static void test_delete(int id) |
557 |
11 May 05 |
enell |
119 |
{ |
557 |
11 May 05 |
enell |
120 |
if (id == 0) return; |
557 |
11 May 05 |
enell |
121 |
DbControl dc = null; |
557 |
11 May 05 |
enell |
122 |
try |
557 |
11 May 05 |
enell |
123 |
{ |
557 |
11 May 05 |
enell |
124 |
dc = TestUtil.getDbControl(); |
557 |
11 May 05 |
enell |
125 |
Protocol p = Protocol.getById(dc, id); |
557 |
11 May 05 |
enell |
126 |
dc.deleteItem(p); |
5340 |
10 May 10 |
nicklas |
127 |
Set<ItemProxy> using = p.getUsingItems(); |
5340 |
10 May 10 |
nicklas |
128 |
if (using.size() > 0) |
5340 |
10 May 10 |
nicklas |
129 |
{ |
5340 |
10 May 10 |
nicklas |
130 |
throw new BaseException(p + " is used by " + using); |
5340 |
10 May 10 |
nicklas |
131 |
} |
557 |
11 May 05 |
enell |
132 |
dc.commit(); |
557 |
11 May 05 |
enell |
133 |
write("--Delete protocol OK"); |
557 |
11 May 05 |
enell |
134 |
} |
816 |
23 Jun 05 |
nicklas |
135 |
catch (Throwable ex) |
557 |
11 May 05 |
enell |
136 |
{ |
557 |
11 May 05 |
enell |
137 |
write("--Delete protocol FAILED"); |
557 |
11 May 05 |
enell |
138 |
ex.printStackTrace(); |
557 |
11 May 05 |
enell |
139 |
ok = false; |
557 |
11 May 05 |
enell |
140 |
} |
557 |
11 May 05 |
enell |
141 |
finally |
557 |
11 May 05 |
enell |
142 |
{ |
557 |
11 May 05 |
enell |
143 |
if (dc != null) dc.close(); |
557 |
11 May 05 |
enell |
144 |
} |
557 |
11 May 05 |
enell |
145 |
} |
557 |
11 May 05 |
enell |
146 |
|
557 |
11 May 05 |
enell |
147 |
static void test_load(int id) |
557 |
11 May 05 |
enell |
148 |
{ |
557 |
11 May 05 |
enell |
149 |
if (id == 0) return; |
557 |
11 May 05 |
enell |
150 |
DbControl dc = null; |
557 |
11 May 05 |
enell |
151 |
try |
557 |
11 May 05 |
enell |
152 |
{ |
557 |
11 May 05 |
enell |
153 |
dc = TestUtil.getDbControl(); |
557 |
11 May 05 |
enell |
154 |
Protocol p = Protocol.getById(dc, id); |
557 |
11 May 05 |
enell |
155 |
write_item(0, p); |
557 |
11 May 05 |
enell |
156 |
write("--Load protocol OK"); |
557 |
11 May 05 |
enell |
157 |
} |
816 |
23 Jun 05 |
nicklas |
158 |
catch (Throwable ex) |
557 |
11 May 05 |
enell |
159 |
{ |
557 |
11 May 05 |
enell |
160 |
write("--Load protocol FAILED"); |
557 |
11 May 05 |
enell |
161 |
ex.printStackTrace(); |
557 |
11 May 05 |
enell |
162 |
ok = false; |
557 |
11 May 05 |
enell |
163 |
} |
557 |
11 May 05 |
enell |
164 |
finally |
557 |
11 May 05 |
enell |
165 |
{ |
557 |
11 May 05 |
enell |
166 |
if (dc != null) dc.close(); |
557 |
11 May 05 |
enell |
167 |
} |
557 |
11 May 05 |
enell |
168 |
} |
557 |
11 May 05 |
enell |
169 |
|
557 |
11 May 05 |
enell |
170 |
static void test_list() |
557 |
11 May 05 |
enell |
171 |
{ |
557 |
11 May 05 |
enell |
172 |
DbControl dc = null; |
557 |
11 May 05 |
enell |
173 |
try |
557 |
11 May 05 |
enell |
174 |
{ |
557 |
11 May 05 |
enell |
175 |
dc = TestUtil.getDbControl(); |
1418 |
07 Oct 05 |
nicklas |
176 |
ItemResultList<Protocol> l = Protocol.getQuery().list(dc); |
557 |
11 May 05 |
enell |
177 |
for (int i = 0; i<l.size(); i++) |
557 |
11 May 05 |
enell |
178 |
{ |
557 |
11 May 05 |
enell |
179 |
write_item(i, l.get(i)); |
557 |
11 May 05 |
enell |
180 |
} |
557 |
11 May 05 |
enell |
181 |
write("--List protocols OK ("+l.size()+")"); |
557 |
11 May 05 |
enell |
182 |
} |
816 |
23 Jun 05 |
nicklas |
183 |
catch (Throwable ex) |
557 |
11 May 05 |
enell |
184 |
{ |
557 |
11 May 05 |
enell |
185 |
write("--List protocols FAILED"); |
557 |
11 May 05 |
enell |
186 |
ex.printStackTrace(); |
557 |
11 May 05 |
enell |
187 |
ok = false; |
557 |
11 May 05 |
enell |
188 |
} |
557 |
11 May 05 |
enell |
189 |
finally |
557 |
11 May 05 |
enell |
190 |
{ |
557 |
11 May 05 |
enell |
191 |
if (dc != null) dc.close(); |
557 |
11 May 05 |
enell |
192 |
} |
557 |
11 May 05 |
enell |
193 |
} |
557 |
11 May 05 |
enell |
194 |
|
557 |
11 May 05 |
enell |
195 |
static void write_header() |
557 |
11 May 05 |
enell |
196 |
{ |
557 |
11 May 05 |
enell |
197 |
if (!TestUtil.getSilent()) |
557 |
11 May 05 |
enell |
198 |
{ |
5248 |
17 Feb 10 |
nicklas |
199 |
write(" \tID \tName \tDescription\tType\tExternal ID\tFile"); |
5248 |
17 Feb 10 |
nicklas |
200 |
write("-- \t-- \t--------- \t-----------\t----\t-----------\t----"); |
557 |
11 May 05 |
enell |
201 |
} |
557 |
11 May 05 |
enell |
202 |
} |
557 |
11 May 05 |
enell |
203 |
static void write_item(int i, Protocol p) |
557 |
11 May 05 |
enell |
204 |
throws BaseException |
557 |
11 May 05 |
enell |
205 |
{ |
557 |
11 May 05 |
enell |
206 |
if (!TestUtil.getSilent()) |
5630 |
13 May 11 |
nicklas |
207 |
write(i+":\t"+p.getId()+"\t"+p.getName()+"\t"+p.getDescription()+"\t"+p.getItemSubtype()+ |
5248 |
17 Feb 10 |
nicklas |
208 |
"\t" + p.getExternalId() + "\t"+p.getFile()); |
557 |
11 May 05 |
enell |
209 |
} |
557 |
11 May 05 |
enell |
210 |
static void write(String message) |
557 |
11 May 05 |
enell |
211 |
{ |
557 |
11 May 05 |
enell |
212 |
System.out.println(message); |
557 |
11 May 05 |
enell |
213 |
} |
806 |
22 Jun 05 |
nicklas |
214 |
|
806 |
22 Jun 05 |
nicklas |
215 |
private static void test_deattachfile(int protocolId) |
806 |
22 Jun 05 |
nicklas |
216 |
{ |
806 |
22 Jun 05 |
nicklas |
217 |
if (protocolId == 0) return; |
806 |
22 Jun 05 |
nicklas |
218 |
DbControl dc = null; |
806 |
22 Jun 05 |
nicklas |
219 |
try |
806 |
22 Jun 05 |
nicklas |
220 |
{ |
806 |
22 Jun 05 |
nicklas |
221 |
dc = TestUtil.getDbControl(); |
806 |
22 Jun 05 |
nicklas |
222 |
Protocol p = Protocol.getById(dc, protocolId); |
806 |
22 Jun 05 |
nicklas |
223 |
p.setFile(null); |
806 |
22 Jun 05 |
nicklas |
224 |
dc.commit(); |
806 |
22 Jun 05 |
nicklas |
225 |
dc = TestUtil.getDbControl(); |
5060 |
19 Aug 09 |
nicklas |
226 |
dc.reattachItem(p, false); |
806 |
22 Jun 05 |
nicklas |
227 |
write_item(0, p); |
806 |
22 Jun 05 |
nicklas |
228 |
write("--Deattach file OK"); |
806 |
22 Jun 05 |
nicklas |
229 |
} |
816 |
23 Jun 05 |
nicklas |
230 |
catch (Throwable ex) |
806 |
22 Jun 05 |
nicklas |
231 |
{ |
806 |
22 Jun 05 |
nicklas |
232 |
write("--Deattach file FAILED"); |
806 |
22 Jun 05 |
nicklas |
233 |
ex.printStackTrace(); |
806 |
22 Jun 05 |
nicklas |
234 |
ok = false; |
806 |
22 Jun 05 |
nicklas |
235 |
} |
806 |
22 Jun 05 |
nicklas |
236 |
finally |
806 |
22 Jun 05 |
nicklas |
237 |
{ |
806 |
22 Jun 05 |
nicklas |
238 |
if (dc != null) dc.close(); |
806 |
22 Jun 05 |
nicklas |
239 |
} |
806 |
22 Jun 05 |
nicklas |
240 |
} |
806 |
22 Jun 05 |
nicklas |
241 |
|
806 |
22 Jun 05 |
nicklas |
242 |
private static void test_attachfile(int protocolId, int fileId) |
806 |
22 Jun 05 |
nicklas |
243 |
{ |
806 |
22 Jun 05 |
nicklas |
244 |
if (fileId == 0 || protocolId == 0) return; |
806 |
22 Jun 05 |
nicklas |
245 |
DbControl dc = null; |
806 |
22 Jun 05 |
nicklas |
246 |
try |
806 |
22 Jun 05 |
nicklas |
247 |
{ |
806 |
22 Jun 05 |
nicklas |
248 |
dc = TestUtil.getDbControl(); |
806 |
22 Jun 05 |
nicklas |
249 |
Protocol p = Protocol.getById(dc, protocolId); |
806 |
22 Jun 05 |
nicklas |
250 |
File f = File.getById(dc, fileId); |
806 |
22 Jun 05 |
nicklas |
251 |
f.setName("attachfile"); |
806 |
22 Jun 05 |
nicklas |
252 |
p.setFile(f); |
806 |
22 Jun 05 |
nicklas |
253 |
dc.commit(); |
806 |
22 Jun 05 |
nicklas |
254 |
dc = TestUtil.getDbControl(); |
5060 |
19 Aug 09 |
nicklas |
255 |
dc.reattachItem(p, false); |
806 |
22 Jun 05 |
nicklas |
256 |
write_item(0, p); |
806 |
22 Jun 05 |
nicklas |
257 |
write("--Attach file OK"); |
806 |
22 Jun 05 |
nicklas |
258 |
} |
816 |
23 Jun 05 |
nicklas |
259 |
catch (Throwable ex) |
806 |
22 Jun 05 |
nicklas |
260 |
{ |
806 |
22 Jun 05 |
nicklas |
261 |
write("--Attach file FAILED"); |
806 |
22 Jun 05 |
nicklas |
262 |
ex.printStackTrace(); |
806 |
22 Jun 05 |
nicklas |
263 |
ok = false; |
806 |
22 Jun 05 |
nicklas |
264 |
} |
806 |
22 Jun 05 |
nicklas |
265 |
finally |
806 |
22 Jun 05 |
nicklas |
266 |
{ |
806 |
22 Jun 05 |
nicklas |
267 |
if (dc != null) dc.close(); |
806 |
22 Jun 05 |
nicklas |
268 |
} |
806 |
22 Jun 05 |
nicklas |
269 |
} |
4740 |
05 Feb 09 |
nicklas |
270 |
|
4740 |
05 Feb 09 |
nicklas |
271 |
static void test_set_protocol_parameter(int protocolId, int annotationTypeId) |
4740 |
05 Feb 09 |
nicklas |
272 |
{ |
4740 |
05 Feb 09 |
nicklas |
273 |
if (annotationTypeId == 0 || protocolId == 0) return; |
4740 |
05 Feb 09 |
nicklas |
274 |
DbControl dc = null; |
4740 |
05 Feb 09 |
nicklas |
275 |
try |
4740 |
05 Feb 09 |
nicklas |
276 |
{ |
4740 |
05 Feb 09 |
nicklas |
277 |
dc = TestUtil.getDbControl(); |
4740 |
05 Feb 09 |
nicklas |
278 |
Protocol p = Protocol.getById(dc, protocolId); |
4740 |
05 Feb 09 |
nicklas |
279 |
AnnotationType at = AnnotationType.getById(dc, annotationTypeId); |
4740 |
05 Feb 09 |
nicklas |
280 |
at.setProtocolParameter(true); |
4740 |
05 Feb 09 |
nicklas |
281 |
p.addParameter(at); |
4740 |
05 Feb 09 |
nicklas |
282 |
dc.commit(); |
4740 |
05 Feb 09 |
nicklas |
283 |
write("--Set protocol parameter OK"); |
4740 |
05 Feb 09 |
nicklas |
284 |
} |
4740 |
05 Feb 09 |
nicklas |
285 |
catch (Throwable ex) |
4740 |
05 Feb 09 |
nicklas |
286 |
{ |
4740 |
05 Feb 09 |
nicklas |
287 |
write("--Set protocol parameter FAILED"); |
4740 |
05 Feb 09 |
nicklas |
288 |
ex.printStackTrace(); |
4740 |
05 Feb 09 |
nicklas |
289 |
ok = false; |
4740 |
05 Feb 09 |
nicklas |
290 |
} |
4740 |
05 Feb 09 |
nicklas |
291 |
finally |
4740 |
05 Feb 09 |
nicklas |
292 |
{ |
4740 |
05 Feb 09 |
nicklas |
293 |
if (dc != null) dc.close(); |
4740 |
05 Feb 09 |
nicklas |
294 |
} |
4740 |
05 Feb 09 |
nicklas |
295 |
} |
806 |
22 Jun 05 |
nicklas |
296 |
|
557 |
11 May 05 |
enell |
297 |
} |