591 |
13 May 05 |
enell |
1 |
import java.util.Date; |
5340 |
10 May 10 |
nicklas |
2 |
import java.util.Set; |
591 |
13 May 05 |
enell |
3 |
|
591 |
13 May 05 |
enell |
4 |
import net.sf.basedb.core.BaseException; |
591 |
13 May 05 |
enell |
5 |
import net.sf.basedb.core.DbControl; |
591 |
13 May 05 |
enell |
6 |
import net.sf.basedb.core.Item; |
5340 |
10 May 10 |
nicklas |
7 |
import net.sf.basedb.core.ItemProxy; |
5630 |
13 May 11 |
nicklas |
8 |
import net.sf.basedb.core.ItemSubtype; |
591 |
13 May 05 |
enell |
9 |
import net.sf.basedb.core.Permission; |
1418 |
07 Oct 05 |
nicklas |
10 |
import net.sf.basedb.core.ItemResultList; |
591 |
13 May 05 |
enell |
11 |
import net.sf.basedb.core.Software; |
591 |
13 May 05 |
enell |
12 |
import net.sf.basedb.core.SystemItems; |
591 |
13 May 05 |
enell |
13 |
|
591 |
13 May 05 |
enell |
14 |
/* |
591 |
13 May 05 |
enell |
$Id$ |
591 |
13 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 |
591 |
13 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/ |
591 |
13 May 05 |
enell |
22 |
|
591 |
13 May 05 |
enell |
BASE is free software; you can redistribute it and/or |
591 |
13 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 |
591 |
13 May 05 |
enell |
of the License, or (at your option) any later version. |
591 |
13 May 05 |
enell |
27 |
|
591 |
13 May 05 |
enell |
BASE is distributed in the hope that it will be useful, |
591 |
13 May 05 |
enell |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
591 |
13 May 05 |
enell |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
591 |
13 May 05 |
enell |
GNU General Public License for more details. |
591 |
13 May 05 |
enell |
32 |
|
591 |
13 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/>. |
591 |
13 May 05 |
enell |
35 |
*/ |
591 |
13 May 05 |
enell |
36 |
|
591 |
13 May 05 |
enell |
37 |
public class TestSoftware |
591 |
13 May 05 |
enell |
38 |
{ |
591 |
13 May 05 |
enell |
39 |
static boolean ok = true; |
591 |
13 May 05 |
enell |
40 |
public static void main(String[] args) |
591 |
13 May 05 |
enell |
41 |
{ |
591 |
13 May 05 |
enell |
42 |
TestUtil.checkArgs(args); |
591 |
13 May 05 |
enell |
43 |
TestUtil.begin(); |
591 |
13 May 05 |
enell |
44 |
ok = test_all(); |
591 |
13 May 05 |
enell |
45 |
TestUtil.stop(); |
591 |
13 May 05 |
enell |
46 |
} |
591 |
13 May 05 |
enell |
47 |
|
591 |
13 May 05 |
enell |
48 |
static boolean test_all() |
591 |
13 May 05 |
enell |
49 |
{ |
591 |
13 May 05 |
enell |
50 |
write("++Testing software"); |
591 |
13 May 05 |
enell |
51 |
write_header(); |
591 |
13 May 05 |
enell |
// Standard tests: create, load, list |
5630 |
13 May 11 |
nicklas |
53 |
int id1 = test_create(SystemItems.getId(Software.FEATURE_EXTRACTION),null, true); |
5630 |
13 May 11 |
nicklas |
54 |
int id2 = test_create(SystemItems.getId(Software.FEATURE_EXTRACTION), null, false); |
668 |
27 May 05 |
enell |
55 |
test_load(id1); |
591 |
13 May 05 |
enell |
56 |
test_list(); |
591 |
13 May 05 |
enell |
57 |
|
591 |
13 May 05 |
enell |
// Extra tests: |
591 |
13 May 05 |
enell |
59 |
|
591 |
13 May 05 |
enell |
// Standard test: Delete |
1529 |
27 Oct 05 |
nicklas |
61 |
if (TestUtil.waitBeforeDelete()) TestUtil.waitForEnter(); |
668 |
27 May 05 |
enell |
62 |
test_delete(id1); |
668 |
27 May 05 |
enell |
63 |
test_delete(id2); |
591 |
13 May 05 |
enell |
64 |
write("++Testing software "+(ok ? "OK" : "Failed")+"\n"); |
591 |
13 May 05 |
enell |
65 |
return ok; |
591 |
13 May 05 |
enell |
66 |
} |
591 |
13 May 05 |
enell |
67 |
|
4426 |
29 Aug 08 |
nicklas |
68 |
static int test_create(int softwareTypeId, String name, boolean setAll) |
591 |
13 May 05 |
enell |
69 |
{ |
591 |
13 May 05 |
enell |
70 |
if (!TestUtil.hasPermission(Permission.CREATE, Item.SOFTWARE)) return 0; |
591 |
13 May 05 |
enell |
71 |
int id = 0; |
591 |
13 May 05 |
enell |
72 |
DbControl dc = null; |
591 |
13 May 05 |
enell |
73 |
try |
591 |
13 May 05 |
enell |
74 |
{ |
591 |
13 May 05 |
enell |
75 |
dc = TestUtil.getDbControl(); |
5630 |
13 May 11 |
nicklas |
76 |
Software sw = Software.getNew(dc); |
5630 |
13 May 11 |
nicklas |
77 |
if (softwareTypeId > 0) |
5630 |
13 May 11 |
nicklas |
78 |
{ |
5630 |
13 May 11 |
nicklas |
79 |
sw.setItemSubtype(ItemSubtype.getById(dc, softwareTypeId)); |
5630 |
13 May 11 |
nicklas |
80 |
} |
668 |
27 May 05 |
enell |
81 |
if (setAll) |
668 |
27 May 05 |
enell |
82 |
{ |
668 |
27 May 05 |
enell |
83 |
sw.setName("Test software"); |
668 |
27 May 05 |
enell |
84 |
sw.setDescription("Added at "+new Date()); |
668 |
27 May 05 |
enell |
85 |
sw.setVersionString("0.9 beta"); |
668 |
27 May 05 |
enell |
86 |
} |
4426 |
29 Aug 08 |
nicklas |
87 |
if (name != null) sw.setName(name); |
591 |
13 May 05 |
enell |
88 |
dc.saveItem(sw); |
591 |
13 May 05 |
enell |
89 |
dc.commit(); |
591 |
13 May 05 |
enell |
90 |
id = sw.getId(); |
806 |
22 Jun 05 |
nicklas |
91 |
dc = TestUtil.getDbControl(); |
5060 |
19 Aug 09 |
nicklas |
92 |
dc.reattachItem(sw, false); |
591 |
13 May 05 |
enell |
93 |
write_item(0, sw); |
591 |
13 May 05 |
enell |
94 |
write("--Create software OK"); |
591 |
13 May 05 |
enell |
95 |
} |
816 |
23 Jun 05 |
nicklas |
96 |
catch (Throwable ex) |
591 |
13 May 05 |
enell |
97 |
{ |
591 |
13 May 05 |
enell |
98 |
write("--Create software FAILED"); |
591 |
13 May 05 |
enell |
99 |
ex.printStackTrace(); |
591 |
13 May 05 |
enell |
100 |
ok = false; |
591 |
13 May 05 |
enell |
101 |
} |
591 |
13 May 05 |
enell |
102 |
finally |
591 |
13 May 05 |
enell |
103 |
{ |
591 |
13 May 05 |
enell |
104 |
if (dc != null) dc.close(); |
591 |
13 May 05 |
enell |
105 |
} |
591 |
13 May 05 |
enell |
106 |
return id; |
591 |
13 May 05 |
enell |
107 |
} |
591 |
13 May 05 |
enell |
108 |
|
591 |
13 May 05 |
enell |
109 |
static void test_load(int id) |
591 |
13 May 05 |
enell |
110 |
{ |
591 |
13 May 05 |
enell |
111 |
if (id == 0) return; |
591 |
13 May 05 |
enell |
112 |
DbControl dc = null; |
591 |
13 May 05 |
enell |
113 |
try |
591 |
13 May 05 |
enell |
114 |
{ |
591 |
13 May 05 |
enell |
115 |
dc = TestUtil.getDbControl(); |
591 |
13 May 05 |
enell |
116 |
Software sw = Software.getById(dc, id); |
591 |
13 May 05 |
enell |
117 |
write_item(0, sw); |
591 |
13 May 05 |
enell |
118 |
write("--Load software OK"); |
591 |
13 May 05 |
enell |
119 |
} |
816 |
23 Jun 05 |
nicklas |
120 |
catch (Throwable ex) |
591 |
13 May 05 |
enell |
121 |
{ |
591 |
13 May 05 |
enell |
122 |
write("--Load software FAILED"); |
591 |
13 May 05 |
enell |
123 |
ex.printStackTrace(); |
591 |
13 May 05 |
enell |
124 |
ok = false; |
591 |
13 May 05 |
enell |
125 |
} |
591 |
13 May 05 |
enell |
126 |
finally |
591 |
13 May 05 |
enell |
127 |
{ |
591 |
13 May 05 |
enell |
128 |
if (dc != null) dc.close(); |
591 |
13 May 05 |
enell |
129 |
} |
591 |
13 May 05 |
enell |
130 |
} |
591 |
13 May 05 |
enell |
131 |
|
591 |
13 May 05 |
enell |
132 |
static void test_list() |
591 |
13 May 05 |
enell |
133 |
{ |
591 |
13 May 05 |
enell |
134 |
DbControl dc = null; |
591 |
13 May 05 |
enell |
135 |
try |
591 |
13 May 05 |
enell |
136 |
{ |
591 |
13 May 05 |
enell |
137 |
dc = TestUtil.getDbControl(); |
1418 |
07 Oct 05 |
nicklas |
138 |
ItemResultList<Software> l = Software.getQuery().list(dc); |
591 |
13 May 05 |
enell |
139 |
for (int i = 0; i<l.size(); i++) |
591 |
13 May 05 |
enell |
140 |
{ |
6870 |
16 Apr 15 |
nicklas |
141 |
write_item(i, l.get(i)); |
591 |
13 May 05 |
enell |
142 |
} |
591 |
13 May 05 |
enell |
143 |
write("--List software OK ("+l.size()+")"); |
591 |
13 May 05 |
enell |
144 |
} |
816 |
23 Jun 05 |
nicklas |
145 |
catch (Throwable ex) |
591 |
13 May 05 |
enell |
146 |
{ |
591 |
13 May 05 |
enell |
147 |
write("--List software FAILED"); |
591 |
13 May 05 |
enell |
148 |
ex.printStackTrace(); |
591 |
13 May 05 |
enell |
149 |
ok = false; |
591 |
13 May 05 |
enell |
150 |
} |
591 |
13 May 05 |
enell |
151 |
finally |
591 |
13 May 05 |
enell |
152 |
{ |
591 |
13 May 05 |
enell |
153 |
if (dc != null) dc.close(); |
591 |
13 May 05 |
enell |
154 |
} |
591 |
13 May 05 |
enell |
155 |
} |
591 |
13 May 05 |
enell |
156 |
|
591 |
13 May 05 |
enell |
157 |
static void test_delete(int id) |
591 |
13 May 05 |
enell |
158 |
{ |
591 |
13 May 05 |
enell |
159 |
if (id == 0) return; |
591 |
13 May 05 |
enell |
160 |
DbControl dc = null; |
591 |
13 May 05 |
enell |
161 |
try |
591 |
13 May 05 |
enell |
162 |
{ |
591 |
13 May 05 |
enell |
163 |
dc = TestUtil.getDbControl(); |
591 |
13 May 05 |
enell |
164 |
Software sw = Software.getById(dc, id); |
591 |
13 May 05 |
enell |
165 |
dc.deleteItem(sw); |
5340 |
10 May 10 |
nicklas |
166 |
Set<ItemProxy> using = sw.getUsingItems(); |
5340 |
10 May 10 |
nicklas |
167 |
if (using.size() > 0) |
5340 |
10 May 10 |
nicklas |
168 |
{ |
5340 |
10 May 10 |
nicklas |
169 |
throw new BaseException(sw + " is used by " + using); |
5340 |
10 May 10 |
nicklas |
170 |
} |
591 |
13 May 05 |
enell |
171 |
dc.commit(); |
591 |
13 May 05 |
enell |
172 |
write("--Delete software OK"); |
591 |
13 May 05 |
enell |
173 |
} |
816 |
23 Jun 05 |
nicklas |
174 |
catch (Throwable ex) |
591 |
13 May 05 |
enell |
175 |
{ |
591 |
13 May 05 |
enell |
176 |
write("--Delete software FAILED"); |
591 |
13 May 05 |
enell |
177 |
ex.printStackTrace(); |
591 |
13 May 05 |
enell |
178 |
ok = false; |
591 |
13 May 05 |
enell |
179 |
} |
591 |
13 May 05 |
enell |
180 |
finally |
591 |
13 May 05 |
enell |
181 |
{ |
591 |
13 May 05 |
enell |
182 |
if (dc != null) dc.close(); |
591 |
13 May 05 |
enell |
183 |
} |
591 |
13 May 05 |
enell |
184 |
} |
591 |
13 May 05 |
enell |
185 |
|
591 |
13 May 05 |
enell |
186 |
static void write_header() |
591 |
13 May 05 |
enell |
187 |
{ |
591 |
13 May 05 |
enell |
188 |
if (!TestUtil.getSilent()) |
591 |
13 May 05 |
enell |
189 |
{ |
591 |
13 May 05 |
enell |
190 |
write(" \tID \tName \tDescription\tVersion\tType"); |
591 |
13 May 05 |
enell |
191 |
write("-- \t-- \t--------- \t-----------\t-------\t----"); |
591 |
13 May 05 |
enell |
192 |
} |
591 |
13 May 05 |
enell |
193 |
} |
591 |
13 May 05 |
enell |
194 |
static void write_item(int i, Software sw) |
591 |
13 May 05 |
enell |
195 |
throws BaseException |
591 |
13 May 05 |
enell |
196 |
{ |
5630 |
13 May 11 |
nicklas |
197 |
if (!TestUtil.getSilent()) |
5630 |
13 May 11 |
nicklas |
198 |
{ |
5630 |
13 May 11 |
nicklas |
199 |
System.out.println(i+":\t"+sw.getId()+"\t"+sw.getName()+"\t"+sw.getDescription()+"\t"+ |
5630 |
13 May 11 |
nicklas |
200 |
sw.getVersionString()+"\t"+sw.getItemSubtype()); |
5630 |
13 May 11 |
nicklas |
201 |
} |
591 |
13 May 05 |
enell |
202 |
} |
591 |
13 May 05 |
enell |
203 |
static void write(String message) |
591 |
13 May 05 |
enell |
204 |
{ |
591 |
13 May 05 |
enell |
205 |
System.out.println(message); |
591 |
13 May 05 |
enell |
206 |
} |
591 |
13 May 05 |
enell |
207 |
} |