6990 |
02 Nov 15 |
nicklas |
1 |
import java.util.Date; |
6990 |
02 Nov 15 |
nicklas |
2 |
import java.util.Set; |
6990 |
02 Nov 15 |
nicklas |
3 |
|
6990 |
02 Nov 15 |
nicklas |
4 |
import net.sf.basedb.core.BaseException; |
6990 |
02 Nov 15 |
nicklas |
5 |
import net.sf.basedb.core.DbControl; |
6990 |
02 Nov 15 |
nicklas |
6 |
import net.sf.basedb.core.Extract; |
6990 |
02 Nov 15 |
nicklas |
7 |
import net.sf.basedb.core.ItemProxy; |
6990 |
02 Nov 15 |
nicklas |
8 |
import net.sf.basedb.core.ItemSubtype; |
6990 |
02 Nov 15 |
nicklas |
9 |
import net.sf.basedb.core.Kit; |
6998 |
04 Nov 15 |
nicklas |
10 |
import net.sf.basedb.core.Trashcan; |
6990 |
02 Nov 15 |
nicklas |
11 |
import net.sf.basedb.core.ItemResultList; |
6990 |
02 Nov 15 |
nicklas |
12 |
|
6990 |
02 Nov 15 |
nicklas |
13 |
/* |
6990 |
02 Nov 15 |
nicklas |
$Id$ |
6990 |
02 Nov 15 |
nicklas |
15 |
|
6990 |
02 Nov 15 |
nicklas |
Copyright (C) 2015 Nicklas Nordborg |
6990 |
02 Nov 15 |
nicklas |
17 |
|
6990 |
02 Nov 15 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
6990 |
02 Nov 15 |
nicklas |
Available at http://base.thep.lu.se/ |
6990 |
02 Nov 15 |
nicklas |
20 |
|
6990 |
02 Nov 15 |
nicklas |
BASE is free software; you can redistribute it and/or |
6990 |
02 Nov 15 |
nicklas |
modify it under the terms of the GNU General Public License |
6990 |
02 Nov 15 |
nicklas |
as published by the Free Software Foundation; either version 3 |
6990 |
02 Nov 15 |
nicklas |
of the License, or (at your option) any later version. |
6990 |
02 Nov 15 |
nicklas |
25 |
|
6990 |
02 Nov 15 |
nicklas |
BASE is distributed in the hope that it will be useful, |
6990 |
02 Nov 15 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
6990 |
02 Nov 15 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
6990 |
02 Nov 15 |
nicklas |
GNU General Public License for more details. |
6990 |
02 Nov 15 |
nicklas |
30 |
|
6990 |
02 Nov 15 |
nicklas |
You should have received a copy of the GNU General Public License |
6990 |
02 Nov 15 |
nicklas |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
6990 |
02 Nov 15 |
nicklas |
33 |
*/ |
6990 |
02 Nov 15 |
nicklas |
34 |
|
6990 |
02 Nov 15 |
nicklas |
35 |
public class TestKit |
6990 |
02 Nov 15 |
nicklas |
36 |
{ |
6990 |
02 Nov 15 |
nicklas |
37 |
static boolean ok = true; |
6990 |
02 Nov 15 |
nicklas |
38 |
|
6990 |
02 Nov 15 |
nicklas |
39 |
public static void main(String[] args) |
6990 |
02 Nov 15 |
nicklas |
40 |
{ |
6990 |
02 Nov 15 |
nicklas |
41 |
TestUtil.checkArgs(args); |
6990 |
02 Nov 15 |
nicklas |
42 |
TestUtil.begin(); |
6990 |
02 Nov 15 |
nicklas |
43 |
ok = test_all(); |
6990 |
02 Nov 15 |
nicklas |
44 |
TestUtil.stop(); |
6990 |
02 Nov 15 |
nicklas |
45 |
} |
6990 |
02 Nov 15 |
nicklas |
46 |
|
6990 |
02 Nov 15 |
nicklas |
47 |
static boolean test_all() |
6990 |
02 Nov 15 |
nicklas |
48 |
{ |
6990 |
02 Nov 15 |
nicklas |
49 |
write("++Testing kits"); |
6990 |
02 Nov 15 |
nicklas |
50 |
write_header(); |
6990 |
02 Nov 15 |
nicklas |
// Standard tests: create, load, list |
6990 |
02 Nov 15 |
nicklas |
52 |
int id1 = test_create(0, null, true); |
6990 |
02 Nov 15 |
nicklas |
53 |
int id2 = test_create(0, null, false); |
6990 |
02 Nov 15 |
nicklas |
54 |
test_load(id1); |
6998 |
04 Nov 15 |
nicklas |
55 |
test_list(-1); |
6990 |
02 Nov 15 |
nicklas |
56 |
|
6990 |
02 Nov 15 |
nicklas |
57 |
int extractId = TestExtract.test_create(0, "Extract with kit", 0, 0, false); |
6990 |
02 Nov 15 |
nicklas |
58 |
test_use_kit(id1, extractId); |
6990 |
02 Nov 15 |
nicklas |
59 |
|
6990 |
02 Nov 15 |
nicklas |
60 |
if (TestUtil.waitBeforeDelete()) TestUtil.waitForEnter(); |
6990 |
02 Nov 15 |
nicklas |
61 |
|
6990 |
02 Nov 15 |
nicklas |
62 |
TestExtract.test_delete(extractId); |
6990 |
02 Nov 15 |
nicklas |
63 |
|
6990 |
02 Nov 15 |
nicklas |
// Standard test: Delete |
6990 |
02 Nov 15 |
nicklas |
65 |
test_delete(id1); |
6990 |
02 Nov 15 |
nicklas |
66 |
test_delete(id2); |
6990 |
02 Nov 15 |
nicklas |
67 |
write("++Testing kits "+(ok ? "OK" : "Failed")+"\n"); |
6990 |
02 Nov 15 |
nicklas |
68 |
return ok; |
6990 |
02 Nov 15 |
nicklas |
69 |
} |
6990 |
02 Nov 15 |
nicklas |
70 |
|
6990 |
02 Nov 15 |
nicklas |
71 |
static int test_create(int kitTypeId, String name, boolean setAll) |
6990 |
02 Nov 15 |
nicklas |
72 |
{ |
6990 |
02 Nov 15 |
nicklas |
73 |
int id = 0; |
6990 |
02 Nov 15 |
nicklas |
74 |
DbControl dc = null; |
6990 |
02 Nov 15 |
nicklas |
75 |
try |
6990 |
02 Nov 15 |
nicklas |
76 |
{ |
6990 |
02 Nov 15 |
nicklas |
77 |
dc = TestUtil.getDbControl(); |
6990 |
02 Nov 15 |
nicklas |
78 |
Kit k = Kit.getNew(dc); |
6990 |
02 Nov 15 |
nicklas |
79 |
if (kitTypeId > 0) |
6990 |
02 Nov 15 |
nicklas |
80 |
{ |
6990 |
02 Nov 15 |
nicklas |
81 |
k.setItemSubtype(ItemSubtype.getById(dc, kitTypeId)); |
6990 |
02 Nov 15 |
nicklas |
82 |
} |
6990 |
02 Nov 15 |
nicklas |
83 |
if(setAll) |
6990 |
02 Nov 15 |
nicklas |
84 |
{ |
6990 |
02 Nov 15 |
nicklas |
85 |
k.setName("Test kit"); |
6990 |
02 Nov 15 |
nicklas |
86 |
k.setDescription("Added at "+new Date()); |
6990 |
02 Nov 15 |
nicklas |
87 |
k.setInactive(true); |
6990 |
02 Nov 15 |
nicklas |
88 |
k.setExpirationDate(new Date(System.currentTimeMillis()+48*3600*1000)); // Expire in two days |
6990 |
02 Nov 15 |
nicklas |
89 |
} |
6990 |
02 Nov 15 |
nicklas |
90 |
if (name != null) |
6990 |
02 Nov 15 |
nicklas |
91 |
{ |
6990 |
02 Nov 15 |
nicklas |
92 |
k.setName(name); |
6990 |
02 Nov 15 |
nicklas |
93 |
} |
6990 |
02 Nov 15 |
nicklas |
94 |
dc.saveItem(k); |
6990 |
02 Nov 15 |
nicklas |
95 |
dc.commit(); |
6990 |
02 Nov 15 |
nicklas |
96 |
id = k.getId(); |
6990 |
02 Nov 15 |
nicklas |
97 |
dc = TestUtil.getDbControl(); |
6990 |
02 Nov 15 |
nicklas |
98 |
dc.reattachItem(k, false); |
6990 |
02 Nov 15 |
nicklas |
99 |
write_item(0, k); |
6990 |
02 Nov 15 |
nicklas |
100 |
write("--Create kit OK"); |
6990 |
02 Nov 15 |
nicklas |
101 |
} |
6990 |
02 Nov 15 |
nicklas |
102 |
catch (Throwable ex) |
6990 |
02 Nov 15 |
nicklas |
103 |
{ |
6990 |
02 Nov 15 |
nicklas |
104 |
write("--Create kit FAILED"); |
6990 |
02 Nov 15 |
nicklas |
105 |
ex.printStackTrace(); |
6990 |
02 Nov 15 |
nicklas |
106 |
ok = false; |
6990 |
02 Nov 15 |
nicklas |
107 |
} |
6990 |
02 Nov 15 |
nicklas |
108 |
finally |
6990 |
02 Nov 15 |
nicklas |
109 |
{ |
6990 |
02 Nov 15 |
nicklas |
110 |
if (dc != null) dc.close(); |
6990 |
02 Nov 15 |
nicklas |
111 |
} |
6990 |
02 Nov 15 |
nicklas |
112 |
return id; |
6990 |
02 Nov 15 |
nicklas |
113 |
} |
6990 |
02 Nov 15 |
nicklas |
114 |
|
6990 |
02 Nov 15 |
nicklas |
115 |
static void test_delete(int id) |
6990 |
02 Nov 15 |
nicklas |
116 |
{ |
6990 |
02 Nov 15 |
nicklas |
117 |
if (id == 0) return; |
6990 |
02 Nov 15 |
nicklas |
118 |
DbControl dc = null; |
6990 |
02 Nov 15 |
nicklas |
119 |
try |
6990 |
02 Nov 15 |
nicklas |
120 |
{ |
6990 |
02 Nov 15 |
nicklas |
121 |
dc = TestUtil.getDbControl(); |
6990 |
02 Nov 15 |
nicklas |
122 |
Kit k = Kit.getById(dc, id); |
6990 |
02 Nov 15 |
nicklas |
123 |
dc.deleteItem(k); |
6990 |
02 Nov 15 |
nicklas |
124 |
Set<ItemProxy> using = k.getUsingItems(); |
6990 |
02 Nov 15 |
nicklas |
125 |
if (using.size() > 0) |
6990 |
02 Nov 15 |
nicklas |
126 |
{ |
6990 |
02 Nov 15 |
nicklas |
127 |
throw new BaseException(k + " is used by " + using); |
6990 |
02 Nov 15 |
nicklas |
128 |
} |
6990 |
02 Nov 15 |
nicklas |
129 |
dc.commit(); |
6990 |
02 Nov 15 |
nicklas |
130 |
write("--Delete kit OK"); |
6990 |
02 Nov 15 |
nicklas |
131 |
} |
6990 |
02 Nov 15 |
nicklas |
132 |
catch (Throwable ex) |
6990 |
02 Nov 15 |
nicklas |
133 |
{ |
6990 |
02 Nov 15 |
nicklas |
134 |
write("--Delete kit FAILED"); |
6990 |
02 Nov 15 |
nicklas |
135 |
ex.printStackTrace(); |
6990 |
02 Nov 15 |
nicklas |
136 |
ok = false; |
6990 |
02 Nov 15 |
nicklas |
137 |
} |
6990 |
02 Nov 15 |
nicklas |
138 |
finally |
6990 |
02 Nov 15 |
nicklas |
139 |
{ |
6990 |
02 Nov 15 |
nicklas |
140 |
if (dc != null) dc.close(); |
6990 |
02 Nov 15 |
nicklas |
141 |
} |
6990 |
02 Nov 15 |
nicklas |
142 |
} |
6990 |
02 Nov 15 |
nicklas |
143 |
|
6990 |
02 Nov 15 |
nicklas |
144 |
static void test_load(int id) |
6990 |
02 Nov 15 |
nicklas |
145 |
{ |
6990 |
02 Nov 15 |
nicklas |
146 |
if (id == 0) return; |
6990 |
02 Nov 15 |
nicklas |
147 |
DbControl dc = null; |
6990 |
02 Nov 15 |
nicklas |
148 |
try |
6990 |
02 Nov 15 |
nicklas |
149 |
{ |
6990 |
02 Nov 15 |
nicklas |
150 |
dc = TestUtil.getDbControl(); |
6990 |
02 Nov 15 |
nicklas |
151 |
Kit k = Kit.getById(dc, id); |
6990 |
02 Nov 15 |
nicklas |
152 |
write_item(0, k); |
6990 |
02 Nov 15 |
nicklas |
153 |
write("--Load kit OK"); |
6990 |
02 Nov 15 |
nicklas |
154 |
} |
6990 |
02 Nov 15 |
nicklas |
155 |
catch (Throwable ex) |
6990 |
02 Nov 15 |
nicklas |
156 |
{ |
6990 |
02 Nov 15 |
nicklas |
157 |
write("--Load kit FAILED"); |
6990 |
02 Nov 15 |
nicklas |
158 |
ex.printStackTrace(); |
6990 |
02 Nov 15 |
nicklas |
159 |
ok = false; |
6990 |
02 Nov 15 |
nicklas |
160 |
} |
6990 |
02 Nov 15 |
nicklas |
161 |
finally |
6990 |
02 Nov 15 |
nicklas |
162 |
{ |
6990 |
02 Nov 15 |
nicklas |
163 |
if (dc != null) dc.close(); |
6990 |
02 Nov 15 |
nicklas |
164 |
} |
6990 |
02 Nov 15 |
nicklas |
165 |
} |
6990 |
02 Nov 15 |
nicklas |
166 |
|
6998 |
04 Nov 15 |
nicklas |
167 |
static boolean test_list(int expectedResults) |
6990 |
02 Nov 15 |
nicklas |
168 |
{ |
6990 |
02 Nov 15 |
nicklas |
169 |
DbControl dc = null; |
6990 |
02 Nov 15 |
nicklas |
170 |
try |
6990 |
02 Nov 15 |
nicklas |
171 |
{ |
6990 |
02 Nov 15 |
nicklas |
172 |
dc = TestUtil.getDbControl(); |
6990 |
02 Nov 15 |
nicklas |
173 |
ItemResultList<Kit> l = Kit.getQuery().list(dc); |
6990 |
02 Nov 15 |
nicklas |
174 |
for (int i = 0; i<l.size(); i++) |
6990 |
02 Nov 15 |
nicklas |
175 |
{ |
6990 |
02 Nov 15 |
nicklas |
176 |
write_item(i, l.get(i)); |
6990 |
02 Nov 15 |
nicklas |
177 |
} |
6998 |
04 Nov 15 |
nicklas |
178 |
if (expectedResults >=0 && expectedResults != l.size()) |
6998 |
04 Nov 15 |
nicklas |
179 |
{ |
6998 |
04 Nov 15 |
nicklas |
180 |
throw new BaseException("Expected " + expectedResults + " results, " + |
6998 |
04 Nov 15 |
nicklas |
181 |
"not " + l.size()); |
6998 |
04 Nov 15 |
nicklas |
182 |
} |
6990 |
02 Nov 15 |
nicklas |
183 |
write("--List kits OK ("+l.size()+")"); |
6990 |
02 Nov 15 |
nicklas |
184 |
} |
6990 |
02 Nov 15 |
nicklas |
185 |
catch (Throwable ex) |
6990 |
02 Nov 15 |
nicklas |
186 |
{ |
6990 |
02 Nov 15 |
nicklas |
187 |
write("--List kits FAILED"); |
6990 |
02 Nov 15 |
nicklas |
188 |
ex.printStackTrace(); |
6990 |
02 Nov 15 |
nicklas |
189 |
ok = false; |
6990 |
02 Nov 15 |
nicklas |
190 |
} |
6990 |
02 Nov 15 |
nicklas |
191 |
finally |
6990 |
02 Nov 15 |
nicklas |
192 |
{ |
6990 |
02 Nov 15 |
nicklas |
193 |
if (dc != null) dc.close(); |
6990 |
02 Nov 15 |
nicklas |
194 |
} |
6998 |
04 Nov 15 |
nicklas |
195 |
return ok; |
6990 |
02 Nov 15 |
nicklas |
196 |
} |
6990 |
02 Nov 15 |
nicklas |
197 |
|
6990 |
02 Nov 15 |
nicklas |
198 |
static void write_header() |
6990 |
02 Nov 15 |
nicklas |
199 |
{ |
6990 |
02 Nov 15 |
nicklas |
200 |
if (!TestUtil.getSilent()) |
6990 |
02 Nov 15 |
nicklas |
201 |
{ |
6990 |
02 Nov 15 |
nicklas |
202 |
write(" \tID \tName \tDescription\tType\tInactive\tExpiration date"); |
6990 |
02 Nov 15 |
nicklas |
203 |
write("-- \t-- \t--------- \t-----------\t----\t--------\t---------------"); |
6990 |
02 Nov 15 |
nicklas |
204 |
} |
6990 |
02 Nov 15 |
nicklas |
205 |
} |
6990 |
02 Nov 15 |
nicklas |
206 |
static void write_item(int i, Kit k) |
6990 |
02 Nov 15 |
nicklas |
207 |
throws BaseException |
6990 |
02 Nov 15 |
nicklas |
208 |
{ |
6990 |
02 Nov 15 |
nicklas |
209 |
if (!TestUtil.getSilent()) |
6990 |
02 Nov 15 |
nicklas |
210 |
write(i+":\t"+k.getId()+"\t"+k.getName()+"\t"+k.getDescription()+"\t"+k.getItemSubtype()+ |
6990 |
02 Nov 15 |
nicklas |
211 |
"\t"+k.isInactive()+"\t"+k.getExpirationDate() |
6990 |
02 Nov 15 |
nicklas |
212 |
); |
6990 |
02 Nov 15 |
nicklas |
213 |
} |
6990 |
02 Nov 15 |
nicklas |
214 |
static void write_item(int i, Extract e) |
6990 |
02 Nov 15 |
nicklas |
215 |
throws BaseException |
6990 |
02 Nov 15 |
nicklas |
216 |
{ |
6990 |
02 Nov 15 |
nicklas |
217 |
if (!TestUtil.getSilent()) |
6990 |
02 Nov 15 |
nicklas |
218 |
write(i+":\t"+e.getId()+"\t"+e.getName()+"\t"+e.getCreationEvent().getKit()); |
6990 |
02 Nov 15 |
nicklas |
219 |
} |
6990 |
02 Nov 15 |
nicklas |
220 |
static void write(String message) |
6990 |
02 Nov 15 |
nicklas |
221 |
{ |
6990 |
02 Nov 15 |
nicklas |
222 |
System.out.println(message); |
6990 |
02 Nov 15 |
nicklas |
223 |
} |
6990 |
02 Nov 15 |
nicklas |
224 |
|
6990 |
02 Nov 15 |
nicklas |
225 |
static void test_use_kit(int kitId, int extractId) |
6990 |
02 Nov 15 |
nicklas |
226 |
{ |
6990 |
02 Nov 15 |
nicklas |
227 |
if (extractId == 0 || kitId == 0) return; |
6990 |
02 Nov 15 |
nicklas |
228 |
DbControl dc = null; |
6990 |
02 Nov 15 |
nicklas |
229 |
try |
6990 |
02 Nov 15 |
nicklas |
230 |
{ |
6990 |
02 Nov 15 |
nicklas |
231 |
dc = TestUtil.getDbControl(); |
6990 |
02 Nov 15 |
nicklas |
232 |
Kit k = Kit.getById(dc, kitId); |
6990 |
02 Nov 15 |
nicklas |
233 |
Extract e = Extract.getById(dc, extractId); |
6990 |
02 Nov 15 |
nicklas |
234 |
e.getCreationEvent().setKit(k); |
6990 |
02 Nov 15 |
nicklas |
235 |
dc.commit(); |
6990 |
02 Nov 15 |
nicklas |
236 |
dc = TestUtil.getDbControl(); |
6990 |
02 Nov 15 |
nicklas |
237 |
e = Extract.getById(dc, extractId); |
6990 |
02 Nov 15 |
nicklas |
238 |
write_item(0, e); |
6990 |
02 Nov 15 |
nicklas |
239 |
write("--Use kit OK"); |
6990 |
02 Nov 15 |
nicklas |
240 |
} |
6990 |
02 Nov 15 |
nicklas |
241 |
catch (Throwable ex) |
6990 |
02 Nov 15 |
nicklas |
242 |
{ |
6990 |
02 Nov 15 |
nicklas |
243 |
write("--Use kit FAILED"); |
6990 |
02 Nov 15 |
nicklas |
244 |
ex.printStackTrace(); |
6990 |
02 Nov 15 |
nicklas |
245 |
ok = false; |
6990 |
02 Nov 15 |
nicklas |
246 |
} |
6990 |
02 Nov 15 |
nicklas |
247 |
finally |
6990 |
02 Nov 15 |
nicklas |
248 |
{ |
6990 |
02 Nov 15 |
nicklas |
249 |
if (dc != null) dc.close(); |
6990 |
02 Nov 15 |
nicklas |
250 |
} |
6990 |
02 Nov 15 |
nicklas |
251 |
} |
6998 |
04 Nov 15 |
nicklas |
252 |
|
6998 |
04 Nov 15 |
nicklas |
253 |
static void test_delete_all() |
6998 |
04 Nov 15 |
nicklas |
254 |
{ |
6998 |
04 Nov 15 |
nicklas |
255 |
DbControl dc = null; |
6998 |
04 Nov 15 |
nicklas |
256 |
try |
6998 |
04 Nov 15 |
nicklas |
257 |
{ |
6998 |
04 Nov 15 |
nicklas |
258 |
dc = TestUtil.getDbControl(); |
6998 |
04 Nov 15 |
nicklas |
259 |
ItemResultList<Kit> l = Kit.getQuery().list(dc); |
6998 |
04 Nov 15 |
nicklas |
260 |
int numDeleted = Trashcan.delete(dc.getSessionControl(), l, true, null); |
6998 |
04 Nov 15 |
nicklas |
261 |
if (numDeleted != l.size()) throw new BaseException("Could not delete all items: " + numDeleted + " of " + l.size()); |
6998 |
04 Nov 15 |
nicklas |
262 |
dc.commit(); |
6998 |
04 Nov 15 |
nicklas |
263 |
write("--Delete all kits OK ("+l.size()+")"); |
6998 |
04 Nov 15 |
nicklas |
264 |
} |
6998 |
04 Nov 15 |
nicklas |
265 |
catch (Throwable ex) |
6998 |
04 Nov 15 |
nicklas |
266 |
{ |
6998 |
04 Nov 15 |
nicklas |
267 |
write("--Delete all kits FAILED"); |
6998 |
04 Nov 15 |
nicklas |
268 |
ex.printStackTrace(); |
6998 |
04 Nov 15 |
nicklas |
269 |
ok = false; |
6998 |
04 Nov 15 |
nicklas |
270 |
} |
6998 |
04 Nov 15 |
nicklas |
271 |
finally |
6998 |
04 Nov 15 |
nicklas |
272 |
{ |
6998 |
04 Nov 15 |
nicklas |
273 |
if (dc != null) dc.close(); |
6998 |
04 Nov 15 |
nicklas |
274 |
} |
6998 |
04 Nov 15 |
nicklas |
275 |
} |
6990 |
02 Nov 15 |
nicklas |
276 |
|
6990 |
02 Nov 15 |
nicklas |
277 |
} |