4541 |
22 Sep 08 |
nicklas |
1 |
import java.util.Date; |
5340 |
10 May 10 |
nicklas |
2 |
import java.util.Set; |
4541 |
22 Sep 08 |
nicklas |
3 |
|
4541 |
22 Sep 08 |
nicklas |
4 |
import net.sf.basedb.core.BaseException; |
4541 |
22 Sep 08 |
nicklas |
5 |
import net.sf.basedb.core.DbControl; |
4541 |
22 Sep 08 |
nicklas |
6 |
import net.sf.basedb.core.Item; |
5340 |
10 May 10 |
nicklas |
7 |
import net.sf.basedb.core.ItemProxy; |
4541 |
22 Sep 08 |
nicklas |
8 |
import net.sf.basedb.core.Permission; |
4541 |
22 Sep 08 |
nicklas |
9 |
import net.sf.basedb.core.ItemResultList; |
4541 |
22 Sep 08 |
nicklas |
10 |
import net.sf.basedb.core.Quantity; |
4541 |
22 Sep 08 |
nicklas |
11 |
|
4541 |
22 Sep 08 |
nicklas |
12 |
/* |
4541 |
22 Sep 08 |
nicklas |
$Id $ |
4541 |
22 Sep 08 |
nicklas |
14 |
|
4541 |
22 Sep 08 |
nicklas |
Copyright (C) 2008 Nicklas Nordborg |
4541 |
22 Sep 08 |
nicklas |
16 |
|
4541 |
22 Sep 08 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
4541 |
22 Sep 08 |
nicklas |
Available at http://base.thep.lu.se/ |
4541 |
22 Sep 08 |
nicklas |
19 |
|
4541 |
22 Sep 08 |
nicklas |
BASE is free software; you can redistribute it and/or |
4541 |
22 Sep 08 |
nicklas |
modify it under the terms of the GNU General Public License |
4541 |
22 Sep 08 |
nicklas |
as published by the Free Software Foundation; either version 3 |
4541 |
22 Sep 08 |
nicklas |
of the License, or (at your option) any later version. |
4541 |
22 Sep 08 |
nicklas |
24 |
|
4541 |
22 Sep 08 |
nicklas |
BASE is distributed in the hope that it will be useful, |
4541 |
22 Sep 08 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
4541 |
22 Sep 08 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
4541 |
22 Sep 08 |
nicklas |
GNU General Public License for more details. |
4541 |
22 Sep 08 |
nicklas |
29 |
|
4541 |
22 Sep 08 |
nicklas |
You should have received a copy of the GNU General Public License |
4541 |
22 Sep 08 |
nicklas |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
4541 |
22 Sep 08 |
nicklas |
32 |
*/ |
4541 |
22 Sep 08 |
nicklas |
33 |
|
4541 |
22 Sep 08 |
nicklas |
34 |
public class TestQuantity |
4541 |
22 Sep 08 |
nicklas |
35 |
{ |
4541 |
22 Sep 08 |
nicklas |
36 |
static boolean ok = true; |
4541 |
22 Sep 08 |
nicklas |
37 |
|
4541 |
22 Sep 08 |
nicklas |
38 |
public static void main(String[] args) |
4541 |
22 Sep 08 |
nicklas |
39 |
{ |
4541 |
22 Sep 08 |
nicklas |
40 |
TestUtil.checkArgs(args); |
4541 |
22 Sep 08 |
nicklas |
41 |
TestUtil.begin(); |
4541 |
22 Sep 08 |
nicklas |
42 |
ok = test_all(); |
4541 |
22 Sep 08 |
nicklas |
43 |
TestUtil.stop(); |
4541 |
22 Sep 08 |
nicklas |
44 |
} |
4541 |
22 Sep 08 |
nicklas |
45 |
|
4541 |
22 Sep 08 |
nicklas |
46 |
static boolean test_all() |
4541 |
22 Sep 08 |
nicklas |
47 |
{ |
4541 |
22 Sep 08 |
nicklas |
48 |
write("++Testing quantities"); |
4541 |
22 Sep 08 |
nicklas |
49 |
write_header(); |
4541 |
22 Sep 08 |
nicklas |
// Standard tests: create, load, list |
4541 |
22 Sep 08 |
nicklas |
51 |
int id1 = test_create(true); |
4541 |
22 Sep 08 |
nicklas |
52 |
int id2 = test_create(false); |
4541 |
22 Sep 08 |
nicklas |
53 |
test_load(id1); |
4541 |
22 Sep 08 |
nicklas |
54 |
test_list(); |
4541 |
22 Sep 08 |
nicklas |
55 |
|
4541 |
22 Sep 08 |
nicklas |
// Standard test: Delete |
4541 |
22 Sep 08 |
nicklas |
57 |
if (TestUtil.waitBeforeDelete()) TestUtil.waitForEnter(); |
4541 |
22 Sep 08 |
nicklas |
58 |
test_delete(id1); |
4541 |
22 Sep 08 |
nicklas |
59 |
test_delete(id2); |
4541 |
22 Sep 08 |
nicklas |
60 |
write("++Testing quantities "+(ok ? "OK" : "Failed")+"\n"); |
4541 |
22 Sep 08 |
nicklas |
61 |
return ok; |
4541 |
22 Sep 08 |
nicklas |
62 |
} |
4541 |
22 Sep 08 |
nicklas |
63 |
|
4541 |
22 Sep 08 |
nicklas |
64 |
static int test_create(boolean setAll) |
4541 |
22 Sep 08 |
nicklas |
65 |
{ |
4541 |
22 Sep 08 |
nicklas |
66 |
if (!TestUtil.hasPermission(Permission.CREATE, Item.QUANTITY)) return 0; |
4541 |
22 Sep 08 |
nicklas |
67 |
int id = 0; |
4541 |
22 Sep 08 |
nicklas |
68 |
DbControl dc = null; |
4541 |
22 Sep 08 |
nicklas |
69 |
try |
4541 |
22 Sep 08 |
nicklas |
70 |
{ |
4541 |
22 Sep 08 |
nicklas |
71 |
dc = TestUtil.getDbControl(); |
4541 |
22 Sep 08 |
nicklas |
72 |
Quantity q = Quantity.getNew(dc, "1 test unit"); |
4541 |
22 Sep 08 |
nicklas |
73 |
if (setAll) |
4541 |
22 Sep 08 |
nicklas |
74 |
{ |
4541 |
22 Sep 08 |
nicklas |
75 |
q.setName("Test quantity"); |
4541 |
22 Sep 08 |
nicklas |
76 |
q.setDescription("Added at "+new Date()); |
4541 |
22 Sep 08 |
nicklas |
77 |
} |
4541 |
22 Sep 08 |
nicklas |
78 |
dc.saveItem(q); |
4541 |
22 Sep 08 |
nicklas |
79 |
dc.commit(); |
4541 |
22 Sep 08 |
nicklas |
80 |
id = q.getId(); |
4541 |
22 Sep 08 |
nicklas |
81 |
write_item(0, q); |
4541 |
22 Sep 08 |
nicklas |
82 |
write("--Create quantity OK"); |
4541 |
22 Sep 08 |
nicklas |
83 |
} |
4541 |
22 Sep 08 |
nicklas |
84 |
catch (Throwable ex) |
4541 |
22 Sep 08 |
nicklas |
85 |
{ |
4541 |
22 Sep 08 |
nicklas |
86 |
write("--Create quantity FAILED"); |
4541 |
22 Sep 08 |
nicklas |
87 |
ex.printStackTrace(); |
4541 |
22 Sep 08 |
nicklas |
88 |
ok = false; |
4541 |
22 Sep 08 |
nicklas |
89 |
} |
4541 |
22 Sep 08 |
nicklas |
90 |
finally |
4541 |
22 Sep 08 |
nicklas |
91 |
{ |
4541 |
22 Sep 08 |
nicklas |
92 |
if (dc != null) dc.close(); |
4541 |
22 Sep 08 |
nicklas |
93 |
} |
4541 |
22 Sep 08 |
nicklas |
94 |
return id; |
4541 |
22 Sep 08 |
nicklas |
95 |
} |
4541 |
22 Sep 08 |
nicklas |
96 |
|
4541 |
22 Sep 08 |
nicklas |
97 |
static void test_load(int id) |
4541 |
22 Sep 08 |
nicklas |
98 |
{ |
4541 |
22 Sep 08 |
nicklas |
99 |
if (id == 0) return; |
4541 |
22 Sep 08 |
nicklas |
100 |
DbControl dc = null; |
4541 |
22 Sep 08 |
nicklas |
101 |
try |
4541 |
22 Sep 08 |
nicklas |
102 |
{ |
4541 |
22 Sep 08 |
nicklas |
103 |
dc = TestUtil.getDbControl(); |
4541 |
22 Sep 08 |
nicklas |
104 |
Quantity q = Quantity.getById(dc, id); |
4541 |
22 Sep 08 |
nicklas |
105 |
write_item(0, q); |
4541 |
22 Sep 08 |
nicklas |
106 |
write("--Load quantity OK"); |
4541 |
22 Sep 08 |
nicklas |
107 |
} |
4541 |
22 Sep 08 |
nicklas |
108 |
catch (Throwable ex) |
4541 |
22 Sep 08 |
nicklas |
109 |
{ |
4541 |
22 Sep 08 |
nicklas |
110 |
write("--Load quantity FAILED"); |
4541 |
22 Sep 08 |
nicklas |
111 |
ex.printStackTrace(); |
4541 |
22 Sep 08 |
nicklas |
112 |
ok = false; |
4541 |
22 Sep 08 |
nicklas |
113 |
} |
4541 |
22 Sep 08 |
nicklas |
114 |
finally |
4541 |
22 Sep 08 |
nicklas |
115 |
{ |
4541 |
22 Sep 08 |
nicklas |
116 |
if (dc != null) dc.close(); |
4541 |
22 Sep 08 |
nicklas |
117 |
} |
4541 |
22 Sep 08 |
nicklas |
118 |
} |
4541 |
22 Sep 08 |
nicklas |
119 |
|
4541 |
22 Sep 08 |
nicklas |
120 |
static void test_list() |
4541 |
22 Sep 08 |
nicklas |
121 |
{ |
4541 |
22 Sep 08 |
nicklas |
122 |
DbControl dc = null; |
4541 |
22 Sep 08 |
nicklas |
123 |
try |
4541 |
22 Sep 08 |
nicklas |
124 |
{ |
4541 |
22 Sep 08 |
nicklas |
125 |
dc = TestUtil.getDbControl(); |
4541 |
22 Sep 08 |
nicklas |
126 |
ItemResultList<Quantity> l = Quantity.getQuery().list(dc); |
4541 |
22 Sep 08 |
nicklas |
127 |
for (int i = 0; i<l.size(); i++) |
4541 |
22 Sep 08 |
nicklas |
128 |
{ |
4541 |
22 Sep 08 |
nicklas |
129 |
write_item(i, l.get(i)); |
4541 |
22 Sep 08 |
nicklas |
130 |
} |
4541 |
22 Sep 08 |
nicklas |
131 |
write("--List quantities OK ("+l.size()+")"); |
4541 |
22 Sep 08 |
nicklas |
132 |
} |
4541 |
22 Sep 08 |
nicklas |
133 |
catch (Throwable ex) |
4541 |
22 Sep 08 |
nicklas |
134 |
{ |
4541 |
22 Sep 08 |
nicklas |
135 |
write("--List quantities FAILED"); |
4541 |
22 Sep 08 |
nicklas |
136 |
ex.printStackTrace(); |
4541 |
22 Sep 08 |
nicklas |
137 |
ok = false; |
4541 |
22 Sep 08 |
nicklas |
138 |
} |
4541 |
22 Sep 08 |
nicklas |
139 |
finally |
4541 |
22 Sep 08 |
nicklas |
140 |
{ |
4541 |
22 Sep 08 |
nicklas |
141 |
if (dc != null) dc.close(); |
4541 |
22 Sep 08 |
nicklas |
142 |
} |
4541 |
22 Sep 08 |
nicklas |
143 |
} |
4541 |
22 Sep 08 |
nicklas |
144 |
|
4541 |
22 Sep 08 |
nicklas |
145 |
static void test_delete(int id) |
4541 |
22 Sep 08 |
nicklas |
146 |
{ |
4541 |
22 Sep 08 |
nicklas |
147 |
if (id == 0) return; |
4541 |
22 Sep 08 |
nicklas |
148 |
DbControl dc = null; |
4541 |
22 Sep 08 |
nicklas |
149 |
try |
4541 |
22 Sep 08 |
nicklas |
150 |
{ |
4541 |
22 Sep 08 |
nicklas |
151 |
dc = TestUtil.getDbControl(); |
4541 |
22 Sep 08 |
nicklas |
152 |
Quantity q = Quantity.getById(dc, id); |
4541 |
22 Sep 08 |
nicklas |
153 |
dc.deleteItem(q); |
5340 |
10 May 10 |
nicklas |
154 |
Set<ItemProxy> using = q.getUsingItems(); |
5340 |
10 May 10 |
nicklas |
155 |
if (using.size() > 0) |
5340 |
10 May 10 |
nicklas |
156 |
{ |
5340 |
10 May 10 |
nicklas |
157 |
throw new BaseException(q + " is used by " + using); |
5340 |
10 May 10 |
nicklas |
158 |
} |
4541 |
22 Sep 08 |
nicklas |
159 |
dc.commit(); |
4541 |
22 Sep 08 |
nicklas |
160 |
write("--Delete quantity OK"); |
4541 |
22 Sep 08 |
nicklas |
161 |
} |
4541 |
22 Sep 08 |
nicklas |
162 |
catch (Throwable ex) |
4541 |
22 Sep 08 |
nicklas |
163 |
{ |
4541 |
22 Sep 08 |
nicklas |
164 |
write("--Delete quantity FAILED"); |
4541 |
22 Sep 08 |
nicklas |
165 |
ex.printStackTrace(); |
4541 |
22 Sep 08 |
nicklas |
166 |
ok = false; |
4541 |
22 Sep 08 |
nicklas |
167 |
} |
4541 |
22 Sep 08 |
nicklas |
168 |
finally |
4541 |
22 Sep 08 |
nicklas |
169 |
{ |
4541 |
22 Sep 08 |
nicklas |
170 |
if (dc != null) dc.close(); |
4541 |
22 Sep 08 |
nicklas |
171 |
} |
4541 |
22 Sep 08 |
nicklas |
172 |
} |
4541 |
22 Sep 08 |
nicklas |
173 |
|
4541 |
22 Sep 08 |
nicklas |
174 |
static void write_header() |
4541 |
22 Sep 08 |
nicklas |
175 |
{ |
4541 |
22 Sep 08 |
nicklas |
176 |
if (!TestUtil.getSilent()) |
4541 |
22 Sep 08 |
nicklas |
177 |
{ |
4541 |
22 Sep 08 |
nicklas |
178 |
write(" \tID \tName \tDescription\tReference unit\tSystem"); |
4541 |
22 Sep 08 |
nicklas |
179 |
write("-- \t-- \t--------- \t-----------\t--------------\t------"); |
4541 |
22 Sep 08 |
nicklas |
180 |
} |
4541 |
22 Sep 08 |
nicklas |
181 |
} |
4541 |
22 Sep 08 |
nicklas |
182 |
|
4541 |
22 Sep 08 |
nicklas |
183 |
static void write_item(int i, Quantity q) |
4541 |
22 Sep 08 |
nicklas |
184 |
throws BaseException |
4541 |
22 Sep 08 |
nicklas |
185 |
{ |
4541 |
22 Sep 08 |
nicklas |
186 |
if (!TestUtil.getSilent()) |
4541 |
22 Sep 08 |
nicklas |
187 |
{ |
4541 |
22 Sep 08 |
nicklas |
188 |
System.out.println(i+":\t"+q.getId()+"\t"+q.getName()+"\t"+q.getDescription()+"\t"+ |
4541 |
22 Sep 08 |
nicklas |
189 |
q.getReferenceUnit() + "\t" + q.isSystemItem()); |
4541 |
22 Sep 08 |
nicklas |
190 |
} |
4541 |
22 Sep 08 |
nicklas |
191 |
} |
4541 |
22 Sep 08 |
nicklas |
192 |
|
4541 |
22 Sep 08 |
nicklas |
193 |
static void write(String message) |
4541 |
22 Sep 08 |
nicklas |
194 |
{ |
4541 |
22 Sep 08 |
nicklas |
195 |
System.out.println(message); |
4541 |
22 Sep 08 |
nicklas |
196 |
} |
4541 |
22 Sep 08 |
nicklas |
197 |
} |