597 |
16 May 05 |
samuel |
1 |
/* |
597 |
16 May 05 |
samuel |
$Id$ |
597 |
16 May 05 |
samuel |
3 |
|
3675 |
16 Aug 07 |
jari |
Copyright (C) 2005 Samuel Andersson, Johan Enell, Nicklas Nordborg |
4889 |
06 Apr 09 |
nicklas |
Copyright (C) 2006 Johan Enell, Jari Häkkinen, Nicklas Nordborg |
3675 |
16 Aug 07 |
jari |
Copyright (C) 2007 Nicklas Nordborg |
597 |
16 May 05 |
samuel |
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/ |
597 |
16 May 05 |
samuel |
10 |
|
597 |
16 May 05 |
samuel |
BASE is free software; you can redistribute it and/or |
597 |
16 May 05 |
samuel |
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 |
597 |
16 May 05 |
samuel |
of the License, or (at your option) any later version. |
597 |
16 May 05 |
samuel |
15 |
|
597 |
16 May 05 |
samuel |
BASE is distributed in the hope that it will be useful, |
597 |
16 May 05 |
samuel |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
597 |
16 May 05 |
samuel |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
597 |
16 May 05 |
samuel |
GNU General Public License for more details. |
597 |
16 May 05 |
samuel |
20 |
|
597 |
16 May 05 |
samuel |
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/>. |
597 |
16 May 05 |
samuel |
23 |
*/ |
597 |
16 May 05 |
samuel |
24 |
import net.sf.basedb.core.*; |
597 |
16 May 05 |
samuel |
25 |
import net.sf.basedb.core.data.ReporterData; |
1135 |
25 Aug 05 |
nicklas |
26 |
import net.sf.basedb.util.FileUtil; |
2203 |
28 Apr 06 |
nicklas |
27 |
import net.sf.basedb.util.parser.FlatFileParser; |
597 |
16 May 05 |
samuel |
28 |
|
1135 |
25 Aug 05 |
nicklas |
29 |
import java.util.regex.Pattern; |
597 |
16 May 05 |
samuel |
30 |
import java.util.Date; |
597 |
16 May 05 |
samuel |
31 |
|
597 |
16 May 05 |
samuel |
32 |
public class TestReporter |
597 |
16 May 05 |
samuel |
33 |
{ |
597 |
16 May 05 |
samuel |
34 |
static boolean ok = true; |
597 |
16 May 05 |
samuel |
35 |
|
597 |
16 May 05 |
samuel |
36 |
public static void main(String[] args) |
597 |
16 May 05 |
samuel |
37 |
{ |
597 |
16 May 05 |
samuel |
38 |
TestUtil.checkArgs(args); |
597 |
16 May 05 |
samuel |
39 |
TestUtil.begin(); |
597 |
16 May 05 |
samuel |
40 |
ok = test_all(); |
597 |
16 May 05 |
samuel |
41 |
TestUtil.stop(); |
597 |
16 May 05 |
samuel |
42 |
} |
597 |
16 May 05 |
samuel |
43 |
|
597 |
16 May 05 |
samuel |
44 |
static boolean test_all() |
597 |
16 May 05 |
samuel |
45 |
{ |
597 |
16 May 05 |
samuel |
46 |
write("++Testing reporter batching"); |
630 |
20 May 05 |
samuel |
47 |
int rtId = TestReporterType.test_create(); |
639 |
23 May 05 |
samuel |
48 |
test_create(50, rtId, 0, true); |
639 |
23 May 05 |
samuel |
49 |
test_create(50, rtId, 50, false); |
671 |
27 May 05 |
samuel |
50 |
test_update("Batchtest0"); |
597 |
16 May 05 |
samuel |
51 |
write_header(); |
639 |
23 May 05 |
samuel |
52 |
test_list(100); |
640 |
24 May 05 |
samuel |
53 |
test_externalId("Batchtest0"); |
1135 |
25 Aug 05 |
nicklas |
54 |
|
3626 |
03 Aug 07 |
nicklas |
55 |
test_import_from_file("data/test.rawdata.import.txt", "\"Block\"\\t\"Column\"\\t\"Row\"\\t\"Name\"\\t\"ID\".*", "\\t", 4, 3); |
1135 |
25 Aug 05 |
nicklas |
56 |
|
1529 |
27 Oct 05 |
nicklas |
57 |
if (TestUtil.waitBeforeDelete()) TestUtil.waitForEnter(); |
597 |
16 May 05 |
samuel |
58 |
test_delete(); |
630 |
20 May 05 |
samuel |
59 |
TestReporterType.test_delete(rtId); |
597 |
16 May 05 |
samuel |
60 |
write("++Testing reporter batching "+(ok ? "OK" : "Failed")+"\n"); |
597 |
16 May 05 |
samuel |
61 |
return ok; |
597 |
16 May 05 |
samuel |
62 |
} |
597 |
16 May 05 |
samuel |
63 |
|
639 |
23 May 05 |
samuel |
64 |
static void test_create(int nbr, int rtId, int startNbr, boolean setAll) |
597 |
16 May 05 |
samuel |
65 |
{ |
1135 |
25 Aug 05 |
nicklas |
66 |
if (!TestUtil.hasPermission(Permission.CREATE, Item.REPORTER)) return; |
597 |
16 May 05 |
samuel |
67 |
write("--Start reporter batching(" + nbr + ")"); |
597 |
16 May 05 |
samuel |
68 |
DbControl dc = null; |
1122 |
24 Aug 05 |
nicklas |
69 |
long time; |
597 |
16 May 05 |
samuel |
70 |
try |
597 |
16 May 05 |
samuel |
71 |
{ |
597 |
16 May 05 |
samuel |
72 |
dc = TestUtil.getDbControl(); |
630 |
20 May 05 |
samuel |
73 |
ReporterType rt = ReporterType.getById(dc, rtId); |
597 |
16 May 05 |
samuel |
74 |
ReporterBatcher rb = ReporterBatcher.getNew(dc); |
630 |
20 May 05 |
samuel |
75 |
|
597 |
16 May 05 |
samuel |
76 |
rb.setBatchSize(500); |
639 |
23 May 05 |
samuel |
77 |
int toNbr = nbr + startNbr; |
1122 |
24 Aug 05 |
nicklas |
78 |
time = System.currentTimeMillis(); |
639 |
23 May 05 |
samuel |
79 |
for (int i = startNbr; i < toNbr; ++i) |
597 |
16 May 05 |
samuel |
80 |
{ |
1122 |
24 Aug 05 |
nicklas |
81 |
ReporterData rd = Reporter.getNew("Batchtest" + i); |
1122 |
24 Aug 05 |
nicklas |
82 |
Reporter.setReporterType(rd, rt); |
639 |
23 May 05 |
samuel |
83 |
if (setAll) |
639 |
23 May 05 |
samuel |
84 |
{ |
639 |
23 May 05 |
samuel |
85 |
rd.setName("Test reporter"); |
639 |
23 May 05 |
samuel |
86 |
rd.setDescription("Added at "+new Date()); |
639 |
23 May 05 |
samuel |
87 |
rd.setSymbol("Some_symbol"); |
639 |
23 May 05 |
samuel |
88 |
rd.setExtended("species", "Homo Sapiens"); |
639 |
23 May 05 |
samuel |
89 |
rd.setExtended("sequence", "atcatcgagaggaatgagacgtgacgtagg"); |
7516 |
02 Nov 18 |
nicklas |
90 |
rd.setExtended("length", Integer.valueOf(30)); |
639 |
23 May 05 |
samuel |
91 |
} |
597 |
16 May 05 |
samuel |
92 |
rb.insert(rd); |
597 |
16 May 05 |
samuel |
93 |
} |
597 |
16 May 05 |
samuel |
94 |
dc.commit(); |
1122 |
24 Aug 05 |
nicklas |
95 |
time = System.currentTimeMillis()-time; |
1122 |
24 Aug 05 |
nicklas |
96 |
write("--Batching reporters OK (time="+time+" ms)"); |
597 |
16 May 05 |
samuel |
97 |
} |
597 |
16 May 05 |
samuel |
98 |
catch (Throwable ex) |
597 |
16 May 05 |
samuel |
99 |
{ |
597 |
16 May 05 |
samuel |
100 |
write("--Batching reporters FAILED"); |
597 |
16 May 05 |
samuel |
101 |
ex.printStackTrace(); |
597 |
16 May 05 |
samuel |
102 |
ok = false; |
597 |
16 May 05 |
samuel |
103 |
} |
597 |
16 May 05 |
samuel |
104 |
finally |
597 |
16 May 05 |
samuel |
105 |
{ |
597 |
16 May 05 |
samuel |
106 |
if (dc != null) dc.close(); |
597 |
16 May 05 |
samuel |
107 |
} |
597 |
16 May 05 |
samuel |
108 |
} |
597 |
16 May 05 |
samuel |
109 |
|
3633 |
06 Aug 07 |
nicklas |
110 |
static boolean test_list(int expectedResults) |
597 |
16 May 05 |
samuel |
111 |
{ |
597 |
16 May 05 |
samuel |
112 |
DbControl dc = null; |
597 |
16 May 05 |
samuel |
113 |
try |
597 |
16 May 05 |
samuel |
114 |
{ |
597 |
16 May 05 |
samuel |
115 |
dc = TestUtil.getDbControl(); |
1418 |
07 Oct 05 |
nicklas |
116 |
DataQuery<ReporterData> query = Reporter.getQuery(); |
1418 |
07 Oct 05 |
nicklas |
117 |
DataResultIterator<ReporterData> it = query.iterate(dc); |
597 |
16 May 05 |
samuel |
118 |
int counter = 0; |
597 |
16 May 05 |
samuel |
119 |
while (it.hasNext()) |
597 |
16 May 05 |
samuel |
120 |
{ |
630 |
20 May 05 |
samuel |
121 |
write_item(dc, it.next()); |
597 |
16 May 05 |
samuel |
122 |
++counter; |
597 |
16 May 05 |
samuel |
123 |
} |
597 |
16 May 05 |
samuel |
124 |
if (expectedResults >= 0 && expectedResults != counter) |
597 |
16 May 05 |
samuel |
125 |
{ |
597 |
16 May 05 |
samuel |
126 |
throw new BaseException("Expected "+expectedResults+" results, not "+counter); |
597 |
16 May 05 |
samuel |
127 |
} |
597 |
16 May 05 |
samuel |
128 |
write("--List reporters OK (" + counter + ")"); |
597 |
16 May 05 |
samuel |
129 |
} |
597 |
16 May 05 |
samuel |
130 |
catch (Throwable ex) |
597 |
16 May 05 |
samuel |
131 |
{ |
597 |
16 May 05 |
samuel |
132 |
write("--List reporters FAILED"); |
597 |
16 May 05 |
samuel |
133 |
ex.printStackTrace(); |
597 |
16 May 05 |
samuel |
134 |
ok = false; |
3633 |
06 Aug 07 |
nicklas |
135 |
return false; |
597 |
16 May 05 |
samuel |
136 |
} |
597 |
16 May 05 |
samuel |
137 |
finally |
597 |
16 May 05 |
samuel |
138 |
{ |
597 |
16 May 05 |
samuel |
139 |
if (dc != null) dc.close(); |
597 |
16 May 05 |
samuel |
140 |
} |
3633 |
06 Aug 07 |
nicklas |
141 |
return true; |
597 |
16 May 05 |
samuel |
142 |
} |
640 |
24 May 05 |
samuel |
143 |
|
671 |
27 May 05 |
samuel |
144 |
static void test_update(String externalId) |
671 |
27 May 05 |
samuel |
145 |
{ |
671 |
27 May 05 |
samuel |
146 |
DbControl dc = null; |
671 |
27 May 05 |
samuel |
147 |
try |
671 |
27 May 05 |
samuel |
148 |
{ |
671 |
27 May 05 |
samuel |
149 |
dc = TestUtil.getDbControl(); |
671 |
27 May 05 |
samuel |
150 |
ReporterBatcher rb = ReporterBatcher.getNew(dc); |
671 |
27 May 05 |
samuel |
151 |
ReporterData rd = Reporter.getByExternalId(dc, externalId); |
671 |
27 May 05 |
samuel |
152 |
rd.setExtended("species", "Mus Musculus"); |
671 |
27 May 05 |
samuel |
153 |
rb.update(rd); |
671 |
27 May 05 |
samuel |
154 |
dc.commit(); |
671 |
27 May 05 |
samuel |
155 |
write("--Update by external id OK"); |
671 |
27 May 05 |
samuel |
156 |
} |
671 |
27 May 05 |
samuel |
157 |
catch (Throwable ex) |
671 |
27 May 05 |
samuel |
158 |
{ |
671 |
27 May 05 |
samuel |
159 |
write("--Update by external id FAILED"); |
671 |
27 May 05 |
samuel |
160 |
ex.printStackTrace(); |
671 |
27 May 05 |
samuel |
161 |
ok = false; |
671 |
27 May 05 |
samuel |
162 |
} |
671 |
27 May 05 |
samuel |
163 |
finally |
671 |
27 May 05 |
samuel |
164 |
{ |
671 |
27 May 05 |
samuel |
165 |
if (dc != null) dc.close(); |
671 |
27 May 05 |
samuel |
166 |
} |
671 |
27 May 05 |
samuel |
167 |
} |
671 |
27 May 05 |
samuel |
168 |
|
3468 |
08 Jun 07 |
nicklas |
169 |
static int test_externalId(String externalId) |
640 |
24 May 05 |
samuel |
170 |
{ |
640 |
24 May 05 |
samuel |
171 |
DbControl dc = null; |
3468 |
08 Jun 07 |
nicklas |
172 |
int id = 0; |
640 |
24 May 05 |
samuel |
173 |
try |
640 |
24 May 05 |
samuel |
174 |
{ |
640 |
24 May 05 |
samuel |
175 |
dc = TestUtil.getDbControl(); |
640 |
24 May 05 |
samuel |
176 |
ReporterData rd = Reporter.getByExternalId(dc, externalId); |
3468 |
08 Jun 07 |
nicklas |
177 |
id = rd.getId(); |
640 |
24 May 05 |
samuel |
178 |
write_item(dc, rd); |
640 |
24 May 05 |
samuel |
179 |
write("--Get by external id OK"); |
640 |
24 May 05 |
samuel |
180 |
} |
640 |
24 May 05 |
samuel |
181 |
catch (Throwable ex) |
640 |
24 May 05 |
samuel |
182 |
{ |
640 |
24 May 05 |
samuel |
183 |
write("--Get by external id FAILED"); |
640 |
24 May 05 |
samuel |
184 |
ex.printStackTrace(); |
640 |
24 May 05 |
samuel |
185 |
ok = false; |
640 |
24 May 05 |
samuel |
186 |
} |
640 |
24 May 05 |
samuel |
187 |
finally |
640 |
24 May 05 |
samuel |
188 |
{ |
640 |
24 May 05 |
samuel |
189 |
if (dc != null) dc.close(); |
640 |
24 May 05 |
samuel |
190 |
} |
3468 |
08 Jun 07 |
nicklas |
191 |
return id; |
640 |
24 May 05 |
samuel |
192 |
} |
597 |
16 May 05 |
samuel |
193 |
|
597 |
16 May 05 |
samuel |
194 |
static void test_delete() |
597 |
16 May 05 |
samuel |
195 |
{ |
597 |
16 May 05 |
samuel |
196 |
DbControl dc = null; |
597 |
16 May 05 |
samuel |
197 |
try |
597 |
16 May 05 |
samuel |
198 |
{ |
597 |
16 May 05 |
samuel |
199 |
dc = TestUtil.getDbControl(); |
597 |
16 May 05 |
samuel |
200 |
ReporterBatcher rb = ReporterBatcher.getNew(dc); |
1418 |
07 Oct 05 |
nicklas |
201 |
DataQuery<ReporterData> query = Reporter.getQuery(); |
1418 |
07 Oct 05 |
nicklas |
202 |
DataResultIterator<ReporterData> it = query.iterate(dc); |
597 |
16 May 05 |
samuel |
203 |
while (it.hasNext()) |
597 |
16 May 05 |
samuel |
204 |
{ |
597 |
16 May 05 |
samuel |
205 |
rb.delete(it.next()); |
597 |
16 May 05 |
samuel |
206 |
} |
597 |
16 May 05 |
samuel |
207 |
dc.commit(); |
597 |
16 May 05 |
samuel |
208 |
write("--Delete reporters OK"); |
597 |
16 May 05 |
samuel |
209 |
} |
597 |
16 May 05 |
samuel |
210 |
catch (Throwable ex) |
597 |
16 May 05 |
samuel |
211 |
{ |
597 |
16 May 05 |
samuel |
212 |
write("--Delete reporters FAILED"); |
597 |
16 May 05 |
samuel |
213 |
ex.printStackTrace(); |
597 |
16 May 05 |
samuel |
214 |
ok = false; |
597 |
16 May 05 |
samuel |
215 |
} |
597 |
16 May 05 |
samuel |
216 |
finally |
597 |
16 May 05 |
samuel |
217 |
{ |
597 |
16 May 05 |
samuel |
218 |
if (dc != null) dc.close(); |
597 |
16 May 05 |
samuel |
219 |
} |
597 |
16 May 05 |
samuel |
220 |
} |
597 |
16 May 05 |
samuel |
221 |
|
597 |
16 May 05 |
samuel |
222 |
static void write_header() |
597 |
16 May 05 |
samuel |
223 |
{ |
597 |
16 May 05 |
samuel |
224 |
if (!TestUtil.getSilent()) |
597 |
16 May 05 |
samuel |
225 |
{ |
1293 |
08 Sep 05 |
nicklas |
226 |
write("ID \tExternal ID\tName \tDescription \tSymbol \tLast update \tCluster ID \tReporter type"); |
640 |
24 May 05 |
samuel |
227 |
write("-- \t-----------\t--------- \t-------------\t----------\t--------------\t---------\t-------------"); |
597 |
16 May 05 |
samuel |
228 |
} |
597 |
16 May 05 |
samuel |
229 |
} |
630 |
20 May 05 |
samuel |
230 |
static void write_item(DbControl dc, ReporterData rd) |
597 |
16 May 05 |
samuel |
231 |
throws BaseException |
597 |
16 May 05 |
samuel |
232 |
{ |
640 |
24 May 05 |
samuel |
233 |
if (!TestUtil.getSilent()) System.out.println(rd.getId()+"\t"+rd.getExternalId()+"\t"+rd.getName()+"\t"+rd.getDescription()+ |
1293 |
08 Sep 05 |
nicklas |
234 |
"\t"+rd.getSymbol()+"\t"+rd.getLastUpdate()+"\t"+rd.getExtended("clusterId")+"\t"+Reporter.getReporterType(dc, rd)); |
597 |
16 May 05 |
samuel |
235 |
} |
597 |
16 May 05 |
samuel |
236 |
static void write(String message) |
597 |
16 May 05 |
samuel |
237 |
{ |
597 |
16 May 05 |
samuel |
238 |
System.out.println(message); |
597 |
16 May 05 |
samuel |
239 |
} |
1135 |
25 Aug 05 |
nicklas |
240 |
|
3626 |
03 Aug 07 |
nicklas |
241 |
static void test_import_from_file(String filename, String headerRegexp, String splitterRegexp, int idCol, int nameCol) |
1135 |
25 Aug 05 |
nicklas |
242 |
{ |
1135 |
25 Aug 05 |
nicklas |
243 |
if (!TestUtil.hasPermission(Permission.CREATE, Item.REPORTER)) return; |
1135 |
25 Aug 05 |
nicklas |
244 |
write("--Start import reporters from file (" + filename + ")"); |
1135 |
25 Aug 05 |
nicklas |
245 |
DbControl dc = null; |
1135 |
25 Aug 05 |
nicklas |
246 |
long time; |
1135 |
25 Aug 05 |
nicklas |
247 |
int i = 0; |
1135 |
25 Aug 05 |
nicklas |
248 |
try |
1135 |
25 Aug 05 |
nicklas |
249 |
{ |
1135 |
25 Aug 05 |
nicklas |
250 |
dc = TestUtil.getDbControl(); |
1135 |
25 Aug 05 |
nicklas |
251 |
|
1135 |
25 Aug 05 |
nicklas |
252 |
FlatFileParser ffp = new FlatFileParser(); |
1143 |
29 Aug 05 |
nicklas |
253 |
ffp.setDataHeaderRegexp(Pattern.compile(headerRegexp)); |
3626 |
03 Aug 07 |
nicklas |
254 |
ffp.setDataSplitterRegexp(Pattern.compile(splitterRegexp)); |
2992 |
01 Dec 06 |
enell |
255 |
ffp.setInputStream(FileUtil.getInputStream(new java.io.File(filename)), "ISO-8859-1"); |
1135 |
25 Aug 05 |
nicklas |
256 |
ffp.parseHeaders(); |
1135 |
25 Aug 05 |
nicklas |
257 |
ReporterBatcher rb = ReporterBatcher.getNew(dc); |
1135 |
25 Aug 05 |
nicklas |
258 |
time = System.currentTimeMillis(); |
1135 |
25 Aug 05 |
nicklas |
259 |
while (ffp.hasMoreData()) |
1135 |
25 Aug 05 |
nicklas |
260 |
{ |
1135 |
25 Aug 05 |
nicklas |
261 |
FlatFileParser.Data data = ffp.nextData(); |
7665 |
20 Mar 19 |
nicklas |
262 |
String externalId = data.getString(idCol); |
7665 |
20 Mar 19 |
nicklas |
263 |
String name = data.getString(nameCol); |
1594 |
14 Nov 05 |
nicklas |
264 |
if (name == null) name = externalId; |
1594 |
14 Nov 05 |
nicklas |
265 |
if (externalId != null && !rb.exists(externalId, true, true)) |
1135 |
25 Aug 05 |
nicklas |
266 |
{ |
1143 |
29 Aug 05 |
nicklas |
267 |
ReporterData r = Reporter.getNew(externalId); |
1594 |
14 Nov 05 |
nicklas |
268 |
r.setName(name); |
1135 |
25 Aug 05 |
nicklas |
269 |
rb.insert(r); |
1135 |
25 Aug 05 |
nicklas |
270 |
i++; |
1135 |
25 Aug 05 |
nicklas |
271 |
} |
1135 |
25 Aug 05 |
nicklas |
272 |
} |
1135 |
25 Aug 05 |
nicklas |
273 |
dc.commit(); |
1135 |
25 Aug 05 |
nicklas |
274 |
time = System.currentTimeMillis()-time; |
1135 |
25 Aug 05 |
nicklas |
275 |
write("--Import reporters from file OK ("+i+", time="+time+" ms)"); |
1135 |
25 Aug 05 |
nicklas |
276 |
} |
1135 |
25 Aug 05 |
nicklas |
277 |
catch (Throwable ex) |
1135 |
25 Aug 05 |
nicklas |
278 |
{ |
1135 |
25 Aug 05 |
nicklas |
279 |
write("--Import reporters from file FAILED ("+i+")"); |
1135 |
25 Aug 05 |
nicklas |
280 |
ex.printStackTrace(); |
1135 |
25 Aug 05 |
nicklas |
281 |
ok = false; |
1135 |
25 Aug 05 |
nicklas |
282 |
} |
1135 |
25 Aug 05 |
nicklas |
283 |
finally |
1135 |
25 Aug 05 |
nicklas |
284 |
{ |
1135 |
25 Aug 05 |
nicklas |
285 |
if (dc != null) dc.close(); |
1135 |
25 Aug 05 |
nicklas |
286 |
} |
1135 |
25 Aug 05 |
nicklas |
287 |
|
1135 |
25 Aug 05 |
nicklas |
288 |
} |
1135 |
25 Aug 05 |
nicklas |
289 |
|
597 |
16 May 05 |
samuel |
290 |
} |
597 |
16 May 05 |
samuel |
291 |
|