982 |
21 Jul 05 |
enell |
1 |
/* |
982 |
21 Jul 05 |
enell |
$Id$ |
982 |
21 Jul 05 |
enell |
3 |
|
3675 |
16 Aug 07 |
jari |
Copyright (C) 2005 Johan Enell, Nicklas Nordborg |
4889 |
06 Apr 09 |
nicklas |
Copyright (C) 2006 Jari Häkkinen |
982 |
21 Jul 05 |
enell |
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/ |
982 |
21 Jul 05 |
enell |
9 |
|
982 |
21 Jul 05 |
enell |
BASE is free software; you can redistribute it and/or |
982 |
21 Jul 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 |
982 |
21 Jul 05 |
enell |
of the License, or (at your option) any later version. |
982 |
21 Jul 05 |
enell |
14 |
|
982 |
21 Jul 05 |
enell |
BASE is distributed in the hope that it will be useful, |
982 |
21 Jul 05 |
enell |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
982 |
21 Jul 05 |
enell |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
982 |
21 Jul 05 |
enell |
GNU General Public License for more details. |
982 |
21 Jul 05 |
enell |
19 |
|
982 |
21 Jul 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/>. |
982 |
21 Jul 05 |
enell |
22 |
*/ |
982 |
21 Jul 05 |
enell |
23 |
|
974 |
20 Jul 05 |
enell |
24 |
import java.util.Date; |
5340 |
10 May 10 |
nicklas |
25 |
import java.util.Set; |
974 |
20 Jul 05 |
enell |
26 |
|
974 |
20 Jul 05 |
enell |
27 |
import net.sf.basedb.core.ArrayBatch; |
974 |
20 Jul 05 |
enell |
28 |
import net.sf.basedb.core.ArrayDesign; |
974 |
20 Jul 05 |
enell |
29 |
import net.sf.basedb.core.BaseException; |
974 |
20 Jul 05 |
enell |
30 |
import net.sf.basedb.core.DbControl; |
974 |
20 Jul 05 |
enell |
31 |
import net.sf.basedb.core.Item; |
5340 |
10 May 10 |
nicklas |
32 |
import net.sf.basedb.core.ItemProxy; |
974 |
20 Jul 05 |
enell |
33 |
import net.sf.basedb.core.Permission; |
1418 |
07 Oct 05 |
nicklas |
34 |
import net.sf.basedb.core.ItemQuery; |
1418 |
07 Oct 05 |
nicklas |
35 |
import net.sf.basedb.core.ItemResultList; |
3820 |
12 Oct 07 |
nicklas |
36 |
import net.sf.basedb.core.Platform; |
4434 |
02 Sep 08 |
nicklas |
37 |
import net.sf.basedb.core.Trashcan; |
974 |
20 Jul 05 |
enell |
38 |
|
974 |
20 Jul 05 |
enell |
39 |
public class TestArrayBatch |
974 |
20 Jul 05 |
enell |
40 |
{ |
974 |
20 Jul 05 |
enell |
41 |
|
974 |
20 Jul 05 |
enell |
42 |
static boolean ok = true; |
974 |
20 Jul 05 |
enell |
43 |
public static void main(String[] args) |
974 |
20 Jul 05 |
enell |
44 |
{ |
974 |
20 Jul 05 |
enell |
45 |
TestUtil.checkArgs(args); |
974 |
20 Jul 05 |
enell |
46 |
TestUtil.begin(); |
974 |
20 Jul 05 |
enell |
47 |
ok = test_all(); |
974 |
20 Jul 05 |
enell |
48 |
TestUtil.stop(); |
974 |
20 Jul 05 |
enell |
49 |
} |
974 |
20 Jul 05 |
enell |
50 |
|
974 |
20 Jul 05 |
enell |
51 |
public static boolean test_all() |
974 |
20 Jul 05 |
enell |
52 |
{ |
974 |
20 Jul 05 |
enell |
53 |
write("++Testing array batch"); |
974 |
20 Jul 05 |
enell |
54 |
write_header(); |
974 |
20 Jul 05 |
enell |
// Standard tests: create, load, list |
3820 |
12 Oct 07 |
nicklas |
56 |
int arraydesign_id = TestArrayDesign.test_create(Platform.GENERIC, false); |
1116 |
23 Aug 05 |
nicklas |
57 |
int id = test_create(arraydesign_id, true); |
1116 |
23 Aug 05 |
nicklas |
58 |
int id2 = test_create(arraydesign_id, false); |
974 |
20 Jul 05 |
enell |
59 |
test_load(id); |
974 |
20 Jul 05 |
enell |
60 |
test_list(-1); |
974 |
20 Jul 05 |
enell |
61 |
|
974 |
20 Jul 05 |
enell |
// Extra tests: |
974 |
20 Jul 05 |
enell |
63 |
|
1529 |
27 Oct 05 |
nicklas |
64 |
if (TestUtil.waitBeforeDelete()) TestUtil.waitForEnter(); |
1529 |
27 Oct 05 |
nicklas |
65 |
|
974 |
20 Jul 05 |
enell |
// Standard test: Delete |
974 |
20 Jul 05 |
enell |
67 |
test_delete(id); |
974 |
20 Jul 05 |
enell |
68 |
test_delete(id2); |
974 |
20 Jul 05 |
enell |
69 |
TestArrayDesign.test_delete(arraydesign_id); |
974 |
20 Jul 05 |
enell |
70 |
write("++Testing array batch "+(ok ? "OK" : "Failed")+"\n"); |
974 |
20 Jul 05 |
enell |
71 |
return ok; |
974 |
20 Jul 05 |
enell |
72 |
} |
974 |
20 Jul 05 |
enell |
73 |
|
5696 |
12 Aug 11 |
nicklas |
74 |
static boolean test_list(int expectedResults) |
974 |
20 Jul 05 |
enell |
75 |
{ |
974 |
20 Jul 05 |
enell |
76 |
DbControl dc = null; |
974 |
20 Jul 05 |
enell |
77 |
try |
974 |
20 Jul 05 |
enell |
78 |
{ |
974 |
20 Jul 05 |
enell |
79 |
dc = TestUtil.getDbControl(); |
1418 |
07 Oct 05 |
nicklas |
80 |
ItemQuery<ArrayBatch> q = ArrayBatch.getQuery(); |
1418 |
07 Oct 05 |
nicklas |
81 |
ItemResultList<ArrayBatch> l = q.list(dc); |
974 |
20 Jul 05 |
enell |
82 |
for (int i = 0; i<l.size(); i++) |
974 |
20 Jul 05 |
enell |
83 |
{ |
1418 |
07 Oct 05 |
nicklas |
84 |
write_item(i, l.get(i)); |
974 |
20 Jul 05 |
enell |
85 |
} |
974 |
20 Jul 05 |
enell |
86 |
|
974 |
20 Jul 05 |
enell |
87 |
if (expectedResults >= 0 && expectedResults != l.size()) |
974 |
20 Jul 05 |
enell |
88 |
{ |
974 |
20 Jul 05 |
enell |
89 |
throw new BaseException("Expected "+expectedResults+" results, not "+l.size()); |
974 |
20 Jul 05 |
enell |
90 |
} |
974 |
20 Jul 05 |
enell |
91 |
write("--List array batches OK ("+l.size()+")"); |
974 |
20 Jul 05 |
enell |
92 |
} |
974 |
20 Jul 05 |
enell |
93 |
catch (Exception ex) |
974 |
20 Jul 05 |
enell |
94 |
{ |
974 |
20 Jul 05 |
enell |
95 |
write("--List array batches FAILED"); |
974 |
20 Jul 05 |
enell |
96 |
ex.printStackTrace(); |
974 |
20 Jul 05 |
enell |
97 |
ok = false; |
5696 |
12 Aug 11 |
nicklas |
98 |
return false; |
974 |
20 Jul 05 |
enell |
99 |
} |
974 |
20 Jul 05 |
enell |
100 |
finally |
974 |
20 Jul 05 |
enell |
101 |
{ |
974 |
20 Jul 05 |
enell |
102 |
if (dc != null) dc.close(); |
974 |
20 Jul 05 |
enell |
103 |
} |
5696 |
12 Aug 11 |
nicklas |
104 |
return true; |
974 |
20 Jul 05 |
enell |
105 |
} |
974 |
20 Jul 05 |
enell |
106 |
|
982 |
21 Jul 05 |
enell |
107 |
static void test_delete(int id) |
974 |
20 Jul 05 |
enell |
108 |
{ |
974 |
20 Jul 05 |
enell |
109 |
if (id == 0) return; |
974 |
20 Jul 05 |
enell |
110 |
DbControl dc = null; |
974 |
20 Jul 05 |
enell |
111 |
try |
974 |
20 Jul 05 |
enell |
112 |
{ |
974 |
20 Jul 05 |
enell |
113 |
dc = TestUtil.getDbControl(); |
974 |
20 Jul 05 |
enell |
114 |
ArrayBatch ab = ArrayBatch.getById(dc, id); |
974 |
20 Jul 05 |
enell |
115 |
dc.deleteItem(ab); |
5340 |
10 May 10 |
nicklas |
116 |
Set<ItemProxy> using = ab.getUsingItems(); |
5340 |
10 May 10 |
nicklas |
117 |
if (using.size() > 0) |
5340 |
10 May 10 |
nicklas |
118 |
{ |
5340 |
10 May 10 |
nicklas |
119 |
throw new BaseException(ab + " is used by " + using); |
5340 |
10 May 10 |
nicklas |
120 |
} |
974 |
20 Jul 05 |
enell |
121 |
dc.commit(); |
974 |
20 Jul 05 |
enell |
122 |
write("--Delete array batch OK"); |
974 |
20 Jul 05 |
enell |
123 |
} |
974 |
20 Jul 05 |
enell |
124 |
catch (Exception ex) |
974 |
20 Jul 05 |
enell |
125 |
{ |
974 |
20 Jul 05 |
enell |
126 |
write("--Delete array batch FAILED"); |
974 |
20 Jul 05 |
enell |
127 |
ex.printStackTrace(); |
974 |
20 Jul 05 |
enell |
128 |
ok = false; |
974 |
20 Jul 05 |
enell |
129 |
} |
974 |
20 Jul 05 |
enell |
130 |
finally |
974 |
20 Jul 05 |
enell |
131 |
{ |
974 |
20 Jul 05 |
enell |
132 |
if (dc != null) dc.close(); |
974 |
20 Jul 05 |
enell |
133 |
} |
974 |
20 Jul 05 |
enell |
134 |
|
974 |
20 Jul 05 |
enell |
135 |
} |
974 |
20 Jul 05 |
enell |
136 |
|
4434 |
02 Sep 08 |
nicklas |
137 |
static void test_load(int id) |
974 |
20 Jul 05 |
enell |
138 |
{ |
974 |
20 Jul 05 |
enell |
139 |
if (id == 0) return; |
974 |
20 Jul 05 |
enell |
140 |
DbControl dc = null; |
974 |
20 Jul 05 |
enell |
141 |
try |
974 |
20 Jul 05 |
enell |
142 |
{ |
974 |
20 Jul 05 |
enell |
143 |
dc = TestUtil.getDbControl(); |
974 |
20 Jul 05 |
enell |
144 |
ArrayBatch ab = ArrayBatch.getById(dc, id); |
974 |
20 Jul 05 |
enell |
145 |
write_item(0, ab); |
974 |
20 Jul 05 |
enell |
146 |
write("--Load array batch OK"); |
974 |
20 Jul 05 |
enell |
147 |
} |
974 |
20 Jul 05 |
enell |
148 |
catch (Exception ex) |
974 |
20 Jul 05 |
enell |
149 |
{ |
974 |
20 Jul 05 |
enell |
150 |
write("--Load array batch FAILED"); |
974 |
20 Jul 05 |
enell |
151 |
ex.printStackTrace(); |
974 |
20 Jul 05 |
enell |
152 |
ok = false; |
974 |
20 Jul 05 |
enell |
153 |
} |
974 |
20 Jul 05 |
enell |
154 |
finally |
974 |
20 Jul 05 |
enell |
155 |
{ |
974 |
20 Jul 05 |
enell |
156 |
if (dc != null) dc.close(); |
974 |
20 Jul 05 |
enell |
157 |
} |
974 |
20 Jul 05 |
enell |
158 |
} |
974 |
20 Jul 05 |
enell |
159 |
|
1116 |
23 Aug 05 |
nicklas |
160 |
public static int test_create(int arraydesign_id, boolean setAll) |
974 |
20 Jul 05 |
enell |
161 |
{ |
974 |
20 Jul 05 |
enell |
162 |
if (!TestUtil.hasPermission(Permission.CREATE, Item.ARRAYBATCH)) return 0; |
974 |
20 Jul 05 |
enell |
163 |
int id = 0; |
974 |
20 Jul 05 |
enell |
164 |
DbControl dc = null; |
974 |
20 Jul 05 |
enell |
165 |
try |
974 |
20 Jul 05 |
enell |
166 |
{ |
974 |
20 Jul 05 |
enell |
167 |
dc = TestUtil.getDbControl(); |
974 |
20 Jul 05 |
enell |
168 |
ArrayDesign ad = ArrayDesign.getById(dc, arraydesign_id); |
974 |
20 Jul 05 |
enell |
169 |
ArrayBatch ab = ad.newArrayBatch(); |
1116 |
23 Aug 05 |
nicklas |
170 |
if (setAll) |
974 |
20 Jul 05 |
enell |
171 |
{ |
974 |
20 Jul 05 |
enell |
172 |
ab.setName("Test array batch"); |
974 |
20 Jul 05 |
enell |
173 |
ab.setDescription("Added at "+new Date()); |
974 |
20 Jul 05 |
enell |
174 |
} |
974 |
20 Jul 05 |
enell |
175 |
dc.saveItem(ab); |
974 |
20 Jul 05 |
enell |
176 |
dc.commit(); |
974 |
20 Jul 05 |
enell |
177 |
id = ab.getId(); |
974 |
20 Jul 05 |
enell |
178 |
dc = TestUtil.getDbControl(); |
5060 |
19 Aug 09 |
nicklas |
179 |
dc.reattachItem(ab, false); |
974 |
20 Jul 05 |
enell |
180 |
write_item(0, ab); |
974 |
20 Jul 05 |
enell |
181 |
write("--Create array batch OK"); |
974 |
20 Jul 05 |
enell |
182 |
} |
974 |
20 Jul 05 |
enell |
183 |
catch (Throwable ex) |
974 |
20 Jul 05 |
enell |
184 |
{ |
974 |
20 Jul 05 |
enell |
185 |
write("--Create array batch FAILED"); |
974 |
20 Jul 05 |
enell |
186 |
ex.printStackTrace(); |
974 |
20 Jul 05 |
enell |
187 |
ok = false; |
974 |
20 Jul 05 |
enell |
188 |
} |
974 |
20 Jul 05 |
enell |
189 |
finally |
974 |
20 Jul 05 |
enell |
190 |
{ |
974 |
20 Jul 05 |
enell |
191 |
if (dc != null) dc.close(); |
974 |
20 Jul 05 |
enell |
192 |
} |
974 |
20 Jul 05 |
enell |
193 |
return id; |
974 |
20 Jul 05 |
enell |
194 |
} |
974 |
20 Jul 05 |
enell |
195 |
|
4434 |
02 Sep 08 |
nicklas |
196 |
static void test_delete_all() |
4434 |
02 Sep 08 |
nicklas |
197 |
{ |
4434 |
02 Sep 08 |
nicklas |
198 |
DbControl dc = null; |
4434 |
02 Sep 08 |
nicklas |
199 |
try |
4434 |
02 Sep 08 |
nicklas |
200 |
{ |
4434 |
02 Sep 08 |
nicklas |
201 |
dc = TestUtil.getDbControl(); |
4434 |
02 Sep 08 |
nicklas |
202 |
ItemResultList<ArrayBatch> l = ArrayBatch.getQuery().list(dc); |
5688 |
10 Aug 11 |
nicklas |
203 |
int numDeleted = Trashcan.delete(dc.getSessionControl(), l, true, null); |
5688 |
10 Aug 11 |
nicklas |
204 |
if (numDeleted != l.size()) throw new BaseException("Could not delete all items: " + numDeleted + " of " + l.size()); |
4434 |
02 Sep 08 |
nicklas |
205 |
dc.commit(); |
4434 |
02 Sep 08 |
nicklas |
206 |
write("--Delete all array batches OK ("+l.size()+")"); |
4434 |
02 Sep 08 |
nicklas |
207 |
} |
4434 |
02 Sep 08 |
nicklas |
208 |
catch (Throwable ex) |
4434 |
02 Sep 08 |
nicklas |
209 |
{ |
4434 |
02 Sep 08 |
nicklas |
210 |
write("--Delete all array batches FAILED"); |
4434 |
02 Sep 08 |
nicklas |
211 |
ex.printStackTrace(); |
4434 |
02 Sep 08 |
nicklas |
212 |
ok = false; |
4434 |
02 Sep 08 |
nicklas |
213 |
} |
4434 |
02 Sep 08 |
nicklas |
214 |
finally |
4434 |
02 Sep 08 |
nicklas |
215 |
{ |
4434 |
02 Sep 08 |
nicklas |
216 |
if (dc != null) dc.close(); |
4434 |
02 Sep 08 |
nicklas |
217 |
} |
4434 |
02 Sep 08 |
nicklas |
218 |
} |
4434 |
02 Sep 08 |
nicklas |
219 |
|
974 |
20 Jul 05 |
enell |
220 |
static void write_header() |
974 |
20 Jul 05 |
enell |
221 |
{ |
974 |
20 Jul 05 |
enell |
222 |
if (!TestUtil.getSilent()) |
974 |
20 Jul 05 |
enell |
223 |
{ |
974 |
20 Jul 05 |
enell |
224 |
write(" \tID \tName \tDescription\tArray design"); |
974 |
20 Jul 05 |
enell |
225 |
write("-- \t-- \t--------- \t-----------\t------------"); |
974 |
20 Jul 05 |
enell |
226 |
} |
974 |
20 Jul 05 |
enell |
227 |
} |
974 |
20 Jul 05 |
enell |
228 |
|
974 |
20 Jul 05 |
enell |
229 |
static void write_item(int i, ArrayBatch ab) |
974 |
20 Jul 05 |
enell |
230 |
throws BaseException |
974 |
20 Jul 05 |
enell |
231 |
{ |
974 |
20 Jul 05 |
enell |
232 |
if (!TestUtil.getSilent()) System.out.println(i+":\t"+ab.getId()+"\t"+ab.getName()+"\t"+ |
974 |
20 Jul 05 |
enell |
233 |
ab.getDescription()+"\t"+ab.getArrayDesign()); |
974 |
20 Jul 05 |
enell |
234 |
} |
974 |
20 Jul 05 |
enell |
235 |
|
974 |
20 Jul 05 |
enell |
236 |
static void write(String message) |
974 |
20 Jul 05 |
enell |
237 |
{ |
974 |
20 Jul 05 |
enell |
238 |
System.out.println(message); |
974 |
20 Jul 05 |
enell |
239 |
} |
974 |
20 Jul 05 |
enell |
240 |
} |