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