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