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; |
4555 |
02 Oct 08 |
nicklas |
11 |
import net.sf.basedb.core.StringUtil; |
4541 |
22 Sep 08 |
nicklas |
12 |
import net.sf.basedb.core.SystemItems; |
4541 |
22 Sep 08 |
nicklas |
13 |
import net.sf.basedb.core.Unit; |
4555 |
02 Oct 08 |
nicklas |
14 |
import net.sf.basedb.util.units.UnitUtil; |
4541 |
22 Sep 08 |
nicklas |
15 |
|
4541 |
22 Sep 08 |
nicklas |
16 |
/* |
4555 |
02 Oct 08 |
nicklas |
$Id$ |
4541 |
22 Sep 08 |
nicklas |
18 |
|
4541 |
22 Sep 08 |
nicklas |
Copyright (C) 2008 Nicklas Nordborg |
4541 |
22 Sep 08 |
nicklas |
20 |
|
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 |
23 |
|
4555 |
02 Oct 08 |
nicklas |
BASE is free software; you can redistribute it and/or |
4555 |
02 Oct 08 |
nicklas |
modify it under the terms of the GNU General Public License |
4555 |
02 Oct 08 |
nicklas |
as published by the Free Software Foundation; either version 3 |
4555 |
02 Oct 08 |
nicklas |
of the License, or (at your option) any later version. |
4541 |
22 Sep 08 |
nicklas |
28 |
|
4555 |
02 Oct 08 |
nicklas |
BASE is distributed in the hope that it will be useful, |
4555 |
02 Oct 08 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
4555 |
02 Oct 08 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
4555 |
02 Oct 08 |
nicklas |
GNU General Public License for more details. |
4541 |
22 Sep 08 |
nicklas |
33 |
|
4555 |
02 Oct 08 |
nicklas |
You should have received a copy of the GNU General Public License |
4555 |
02 Oct 08 |
nicklas |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
4555 |
02 Oct 08 |
nicklas |
36 |
*/ |
4541 |
22 Sep 08 |
nicklas |
37 |
|
4541 |
22 Sep 08 |
nicklas |
38 |
public class TestUnit |
4541 |
22 Sep 08 |
nicklas |
39 |
{ |
4541 |
22 Sep 08 |
nicklas |
40 |
static boolean ok = true; |
4541 |
22 Sep 08 |
nicklas |
41 |
|
4541 |
22 Sep 08 |
nicklas |
42 |
public static void main(String[] args) |
4541 |
22 Sep 08 |
nicklas |
43 |
{ |
4541 |
22 Sep 08 |
nicklas |
44 |
TestUtil.checkArgs(args); |
4541 |
22 Sep 08 |
nicklas |
45 |
TestUtil.begin(); |
4541 |
22 Sep 08 |
nicklas |
46 |
ok = test_all(); |
4541 |
22 Sep 08 |
nicklas |
47 |
TestUtil.stop(); |
4541 |
22 Sep 08 |
nicklas |
48 |
} |
4541 |
22 Sep 08 |
nicklas |
49 |
|
4541 |
22 Sep 08 |
nicklas |
50 |
static boolean test_all() |
4541 |
22 Sep 08 |
nicklas |
51 |
{ |
4541 |
22 Sep 08 |
nicklas |
52 |
write("++Testing units"); |
4555 |
02 Oct 08 |
nicklas |
53 |
|
4555 |
02 Oct 08 |
nicklas |
54 |
test_split_value("123 mm", "123", "mm"); |
4555 |
02 Oct 08 |
nicklas |
55 |
test_split_value("123.6 mm", "123.6", "mm"); |
4555 |
02 Oct 08 |
nicklas |
56 |
test_split_value("123.cm", "123", "cm"); |
4555 |
02 Oct 08 |
nicklas |
57 |
test_split_value("-123mm", "-123", "mm"); |
4555 |
02 Oct 08 |
nicklas |
58 |
test_split_value("-0.44", "-0.44", null); |
4555 |
02 Oct 08 |
nicklas |
59 |
test_split_value("-0.33cm", "-0.33", "cm"); |
4555 |
02 Oct 08 |
nicklas |
60 |
test_split_value("1.2345E10cm", "1.2345E10", "cm"); |
4555 |
02 Oct 08 |
nicklas |
61 |
test_split_value("1.2345E-10 mm", "1.2345E-10", "mm"); |
4555 |
02 Oct 08 |
nicklas |
62 |
test_split_value("1.2345e+10", "1.2345e+10", null); |
4555 |
02 Oct 08 |
nicklas |
63 |
test_split_value("1.2345eV", "1.2345", "eV"); |
4555 |
02 Oct 08 |
nicklas |
64 |
|
4541 |
22 Sep 08 |
nicklas |
65 |
write_header(); |
4541 |
22 Sep 08 |
nicklas |
// Standard tests: create, load, list |
4541 |
22 Sep 08 |
nicklas |
67 |
int lengthId = SystemItems.getId(Quantity.LENGTH); |
4541 |
22 Sep 08 |
nicklas |
68 |
int id1 = test_create(lengthId, "Astronimical unit", "ua", 1.49598E11, 0.0, true); |
4541 |
22 Sep 08 |
nicklas |
69 |
test_load(id1); |
4541 |
22 Sep 08 |
nicklas |
70 |
test_add_symbol(id1, "ae"); |
4541 |
22 Sep 08 |
nicklas |
71 |
test_load_by_symbol(lengthId, "ae"); |
4541 |
22 Sep 08 |
nicklas |
72 |
test_list(); |
4541 |
22 Sep 08 |
nicklas |
73 |
|
4541 |
22 Sep 08 |
nicklas |
// Extra test, load by symbol, convert between units |
4541 |
22 Sep 08 |
nicklas |
75 |
int meterId = test_load_by_symbol(lengthId, "m"); |
4541 |
22 Sep 08 |
nicklas |
76 |
test_convert_units(meterId, 55, id1, 3.67651973957E-10, 1E-6); |
4541 |
22 Sep 08 |
nicklas |
77 |
|
4541 |
22 Sep 08 |
nicklas |
78 |
if (TestUtil.waitBeforeDelete()) TestUtil.waitForEnter(); |
4541 |
22 Sep 08 |
nicklas |
79 |
|
4541 |
22 Sep 08 |
nicklas |
// Standard test: Delete |
4541 |
22 Sep 08 |
nicklas |
81 |
test_remove_symbol(id1, "ae"); |
4541 |
22 Sep 08 |
nicklas |
82 |
test_delete(id1); |
4541 |
22 Sep 08 |
nicklas |
83 |
write("++Testing units "+(ok ? "OK" : "Failed")+"\n"); |
4541 |
22 Sep 08 |
nicklas |
84 |
return ok; |
4541 |
22 Sep 08 |
nicklas |
85 |
} |
4541 |
22 Sep 08 |
nicklas |
86 |
|
4541 |
22 Sep 08 |
nicklas |
87 |
static int test_create(int quantityId, String name, String symbol, double factor, double offset, boolean setAll) |
4541 |
22 Sep 08 |
nicklas |
88 |
{ |
4541 |
22 Sep 08 |
nicklas |
89 |
if (quantityId == 0 || !TestUtil.hasPermission(Permission.CREATE, Item.UNIT)) return 0; |
4541 |
22 Sep 08 |
nicklas |
90 |
int id = 0; |
4541 |
22 Sep 08 |
nicklas |
91 |
DbControl dc = null; |
4541 |
22 Sep 08 |
nicklas |
92 |
try |
4541 |
22 Sep 08 |
nicklas |
93 |
{ |
4541 |
22 Sep 08 |
nicklas |
94 |
dc = TestUtil.getDbControl(); |
4541 |
22 Sep 08 |
nicklas |
95 |
Quantity q = Quantity.getById(dc, quantityId); |
4541 |
22 Sep 08 |
nicklas |
96 |
Unit u = Unit.getNew(dc, q, symbol); |
4541 |
22 Sep 08 |
nicklas |
97 |
if(setAll) |
4541 |
22 Sep 08 |
nicklas |
98 |
{ |
4541 |
22 Sep 08 |
nicklas |
99 |
u.setName("Test unit"); |
4541 |
22 Sep 08 |
nicklas |
100 |
u.setDescription("Added at "+new Date()); |
4541 |
22 Sep 08 |
nicklas |
101 |
} |
4541 |
22 Sep 08 |
nicklas |
102 |
if (name != null) u.setName(name); |
4541 |
22 Sep 08 |
nicklas |
103 |
u.changeReferenceFactorAndOffset(factor, offset); |
4541 |
22 Sep 08 |
nicklas |
104 |
dc.saveItem(u); |
4541 |
22 Sep 08 |
nicklas |
105 |
dc.commit(); |
4541 |
22 Sep 08 |
nicklas |
106 |
id = u.getId(); |
4541 |
22 Sep 08 |
nicklas |
107 |
dc = TestUtil.getDbControl(); |
5060 |
19 Aug 09 |
nicklas |
108 |
dc.reattachItem(u, false); |
4541 |
22 Sep 08 |
nicklas |
109 |
write_item(0, u); |
4541 |
22 Sep 08 |
nicklas |
110 |
write("--Create unit OK"); |
4541 |
22 Sep 08 |
nicklas |
111 |
} |
4541 |
22 Sep 08 |
nicklas |
112 |
catch (Throwable ex) |
4541 |
22 Sep 08 |
nicklas |
113 |
{ |
4541 |
22 Sep 08 |
nicklas |
114 |
write("--Create unit FAILED"); |
4541 |
22 Sep 08 |
nicklas |
115 |
ex.printStackTrace(); |
4541 |
22 Sep 08 |
nicklas |
116 |
ok = false; |
4541 |
22 Sep 08 |
nicklas |
117 |
} |
4541 |
22 Sep 08 |
nicklas |
118 |
finally |
4541 |
22 Sep 08 |
nicklas |
119 |
{ |
4541 |
22 Sep 08 |
nicklas |
120 |
if (dc != null) dc.close(); |
4541 |
22 Sep 08 |
nicklas |
121 |
} |
4541 |
22 Sep 08 |
nicklas |
122 |
return id; |
4541 |
22 Sep 08 |
nicklas |
123 |
} |
4541 |
22 Sep 08 |
nicklas |
124 |
|
4541 |
22 Sep 08 |
nicklas |
125 |
static void test_delete(int id) |
4541 |
22 Sep 08 |
nicklas |
126 |
{ |
4541 |
22 Sep 08 |
nicklas |
127 |
if (id == 0) return; |
4541 |
22 Sep 08 |
nicklas |
128 |
DbControl dc = null; |
4541 |
22 Sep 08 |
nicklas |
129 |
try |
4541 |
22 Sep 08 |
nicklas |
130 |
{ |
4541 |
22 Sep 08 |
nicklas |
131 |
dc = TestUtil.getDbControl(); |
4541 |
22 Sep 08 |
nicklas |
132 |
Unit u = Unit.getById(dc, id); |
4541 |
22 Sep 08 |
nicklas |
133 |
dc.deleteItem(u); |
5340 |
10 May 10 |
nicklas |
134 |
Set<ItemProxy> using = u.getUsingItems(); |
5340 |
10 May 10 |
nicklas |
135 |
if (using.size() > 0) |
5340 |
10 May 10 |
nicklas |
136 |
{ |
5340 |
10 May 10 |
nicklas |
137 |
throw new BaseException(u + " is used by " + using); |
5340 |
10 May 10 |
nicklas |
138 |
} |
4541 |
22 Sep 08 |
nicklas |
139 |
dc.commit(); |
4541 |
22 Sep 08 |
nicklas |
140 |
write("--Delete unit OK"); |
4541 |
22 Sep 08 |
nicklas |
141 |
} |
4541 |
22 Sep 08 |
nicklas |
142 |
catch (Throwable ex) |
4541 |
22 Sep 08 |
nicklas |
143 |
{ |
4541 |
22 Sep 08 |
nicklas |
144 |
write("--Delete unit FAILED"); |
4541 |
22 Sep 08 |
nicklas |
145 |
ex.printStackTrace(); |
4541 |
22 Sep 08 |
nicklas |
146 |
ok = false; |
4541 |
22 Sep 08 |
nicklas |
147 |
} |
4541 |
22 Sep 08 |
nicklas |
148 |
finally |
4541 |
22 Sep 08 |
nicklas |
149 |
{ |
4541 |
22 Sep 08 |
nicklas |
150 |
if (dc != null) dc.close(); |
4541 |
22 Sep 08 |
nicklas |
151 |
} |
4541 |
22 Sep 08 |
nicklas |
152 |
} |
4541 |
22 Sep 08 |
nicklas |
153 |
|
4541 |
22 Sep 08 |
nicklas |
154 |
static void test_load(int id) |
4541 |
22 Sep 08 |
nicklas |
155 |
{ |
4541 |
22 Sep 08 |
nicklas |
156 |
if (id == 0) return; |
4541 |
22 Sep 08 |
nicklas |
157 |
DbControl dc = null; |
4541 |
22 Sep 08 |
nicklas |
158 |
try |
4541 |
22 Sep 08 |
nicklas |
159 |
{ |
4541 |
22 Sep 08 |
nicklas |
160 |
dc = TestUtil.getDbControl(); |
4541 |
22 Sep 08 |
nicklas |
161 |
Unit u = Unit.getById(dc, id); |
4541 |
22 Sep 08 |
nicklas |
162 |
write_item(0, u); |
4541 |
22 Sep 08 |
nicklas |
163 |
write("--Load unit OK"); |
4541 |
22 Sep 08 |
nicklas |
164 |
} |
4541 |
22 Sep 08 |
nicklas |
165 |
catch (Throwable ex) |
4541 |
22 Sep 08 |
nicklas |
166 |
{ |
4541 |
22 Sep 08 |
nicklas |
167 |
write("--Load unit FAILED"); |
4541 |
22 Sep 08 |
nicklas |
168 |
ex.printStackTrace(); |
4541 |
22 Sep 08 |
nicklas |
169 |
ok = false; |
4541 |
22 Sep 08 |
nicklas |
170 |
} |
4541 |
22 Sep 08 |
nicklas |
171 |
finally |
4541 |
22 Sep 08 |
nicklas |
172 |
{ |
4541 |
22 Sep 08 |
nicklas |
173 |
if (dc != null) dc.close(); |
4541 |
22 Sep 08 |
nicklas |
174 |
} |
4541 |
22 Sep 08 |
nicklas |
175 |
} |
4541 |
22 Sep 08 |
nicklas |
176 |
|
4541 |
22 Sep 08 |
nicklas |
177 |
static int test_load_by_symbol(int quantityId, String symbol) |
4541 |
22 Sep 08 |
nicklas |
178 |
{ |
4541 |
22 Sep 08 |
nicklas |
179 |
if (quantityId == 0) return 0; |
4541 |
22 Sep 08 |
nicklas |
180 |
int id = 0; |
4541 |
22 Sep 08 |
nicklas |
181 |
DbControl dc = null; |
4541 |
22 Sep 08 |
nicklas |
182 |
try |
4541 |
22 Sep 08 |
nicklas |
183 |
{ |
4541 |
22 Sep 08 |
nicklas |
184 |
dc = TestUtil.getDbControl(); |
4541 |
22 Sep 08 |
nicklas |
185 |
Quantity q = Quantity.getById(dc, quantityId); |
4541 |
22 Sep 08 |
nicklas |
186 |
Unit u = q.getUnit(symbol); |
4541 |
22 Sep 08 |
nicklas |
187 |
id = u.getId(); |
4541 |
22 Sep 08 |
nicklas |
188 |
write_item(0, u); |
4541 |
22 Sep 08 |
nicklas |
189 |
write("--Load unit by symbol OK"); |
4541 |
22 Sep 08 |
nicklas |
190 |
} |
4541 |
22 Sep 08 |
nicklas |
191 |
catch (Throwable ex) |
4541 |
22 Sep 08 |
nicklas |
192 |
{ |
4541 |
22 Sep 08 |
nicklas |
193 |
write("--Load unit by symbol FAILED"); |
4541 |
22 Sep 08 |
nicklas |
194 |
ex.printStackTrace(); |
4541 |
22 Sep 08 |
nicklas |
195 |
ok = false; |
4541 |
22 Sep 08 |
nicklas |
196 |
} |
4541 |
22 Sep 08 |
nicklas |
197 |
finally |
4541 |
22 Sep 08 |
nicklas |
198 |
{ |
4541 |
22 Sep 08 |
nicklas |
199 |
if (dc != null) dc.close(); |
4541 |
22 Sep 08 |
nicklas |
200 |
} |
4541 |
22 Sep 08 |
nicklas |
201 |
return id; |
4541 |
22 Sep 08 |
nicklas |
202 |
} |
4541 |
22 Sep 08 |
nicklas |
203 |
|
4541 |
22 Sep 08 |
nicklas |
204 |
static void test_list() |
4541 |
22 Sep 08 |
nicklas |
205 |
{ |
4541 |
22 Sep 08 |
nicklas |
206 |
DbControl dc = null; |
4541 |
22 Sep 08 |
nicklas |
207 |
try |
4541 |
22 Sep 08 |
nicklas |
208 |
{ |
4541 |
22 Sep 08 |
nicklas |
209 |
dc = TestUtil.getDbControl(); |
4541 |
22 Sep 08 |
nicklas |
210 |
ItemResultList<Unit> l = Unit.getQuery().list(dc); |
4541 |
22 Sep 08 |
nicklas |
211 |
for (int i = 0; i<l.size(); i++) |
4541 |
22 Sep 08 |
nicklas |
212 |
{ |
4541 |
22 Sep 08 |
nicklas |
213 |
write_item(i, l.get(i)); |
4541 |
22 Sep 08 |
nicklas |
214 |
} |
4541 |
22 Sep 08 |
nicklas |
215 |
write("--List units OK ("+l.size()+")"); |
4541 |
22 Sep 08 |
nicklas |
216 |
} |
4541 |
22 Sep 08 |
nicklas |
217 |
catch (Throwable ex) |
4541 |
22 Sep 08 |
nicklas |
218 |
{ |
4541 |
22 Sep 08 |
nicklas |
219 |
write("--List units FAILED"); |
4541 |
22 Sep 08 |
nicklas |
220 |
ex.printStackTrace(); |
4541 |
22 Sep 08 |
nicklas |
221 |
ok = false; |
4541 |
22 Sep 08 |
nicklas |
222 |
} |
4541 |
22 Sep 08 |
nicklas |
223 |
finally |
4541 |
22 Sep 08 |
nicklas |
224 |
{ |
4541 |
22 Sep 08 |
nicklas |
225 |
if (dc != null) dc.close(); |
4541 |
22 Sep 08 |
nicklas |
226 |
} |
4541 |
22 Sep 08 |
nicklas |
227 |
} |
4541 |
22 Sep 08 |
nicklas |
228 |
|
4541 |
22 Sep 08 |
nicklas |
229 |
static void write_header() |
4541 |
22 Sep 08 |
nicklas |
230 |
{ |
4541 |
22 Sep 08 |
nicklas |
231 |
if (!TestUtil.getSilent()) |
4541 |
22 Sep 08 |
nicklas |
232 |
{ |
4541 |
22 Sep 08 |
nicklas |
233 |
write(" \tID \tName \tDescription\tQuantity\tSymbol\tFactor\tOffset\tAliases"); |
4541 |
22 Sep 08 |
nicklas |
234 |
write("-- \t-- \t--------- \t-----------\t--------\t------\t------\t------\t-------"); |
4541 |
22 Sep 08 |
nicklas |
235 |
} |
4541 |
22 Sep 08 |
nicklas |
236 |
} |
4541 |
22 Sep 08 |
nicklas |
237 |
static void write_item(int i, Unit u) |
4541 |
22 Sep 08 |
nicklas |
238 |
throws BaseException |
4541 |
22 Sep 08 |
nicklas |
239 |
{ |
4541 |
22 Sep 08 |
nicklas |
240 |
if (!TestUtil.getSilent()) |
4541 |
22 Sep 08 |
nicklas |
241 |
{ |
4541 |
22 Sep 08 |
nicklas |
242 |
write(i+":\t"+u.getId()+"\t"+u.getName()+"\t"+u.getDescription()+"\t"+ |
4541 |
22 Sep 08 |
nicklas |
243 |
u.getQuantity().getName() + "\t" + |
4541 |
22 Sep 08 |
nicklas |
244 |
u.getDisplaySymbol()+"\t"+u.getReferenceFactor() + "\t" + |
4541 |
22 Sep 08 |
nicklas |
245 |
u.getReferenceOffset() + "\t" + u.getSymbols()); |
4541 |
22 Sep 08 |
nicklas |
246 |
} |
4541 |
22 Sep 08 |
nicklas |
247 |
} |
4541 |
22 Sep 08 |
nicklas |
248 |
static void write(String message) |
4541 |
22 Sep 08 |
nicklas |
249 |
{ |
4541 |
22 Sep 08 |
nicklas |
250 |
System.out.println(message); |
4541 |
22 Sep 08 |
nicklas |
251 |
} |
4541 |
22 Sep 08 |
nicklas |
252 |
|
4541 |
22 Sep 08 |
nicklas |
253 |
static void test_convert_units(int unit1Id, double v1, int unit2Id, double v2, double delta) |
4541 |
22 Sep 08 |
nicklas |
254 |
{ |
4541 |
22 Sep 08 |
nicklas |
255 |
if (unit1Id == 0 || unit2Id == 0) return; |
4541 |
22 Sep 08 |
nicklas |
256 |
DbControl dc = null; |
4541 |
22 Sep 08 |
nicklas |
257 |
try |
4541 |
22 Sep 08 |
nicklas |
258 |
{ |
4541 |
22 Sep 08 |
nicklas |
259 |
dc = TestUtil.getDbControl(); |
4541 |
22 Sep 08 |
nicklas |
260 |
Unit u1 = Unit.getById(dc, unit1Id); |
4541 |
22 Sep 08 |
nicklas |
261 |
Unit u2 = Unit.getById(dc, unit2Id); |
4541 |
22 Sep 08 |
nicklas |
262 |
|
4541 |
22 Sep 08 |
nicklas |
263 |
double v1Tov2 = u1.toUnit(v1, u2); |
4541 |
22 Sep 08 |
nicklas |
264 |
double v2Tov1 = u1.fromOther(v2, u2); |
4541 |
22 Sep 08 |
nicklas |
265 |
|
4541 |
22 Sep 08 |
nicklas |
266 |
if (Math.abs(v2 - v1Tov2) > delta) |
4541 |
22 Sep 08 |
nicklas |
267 |
{ |
4541 |
22 Sep 08 |
nicklas |
268 |
throw new BaseException("Conversion of " + |
4541 |
22 Sep 08 |
nicklas |
269 |
v1 + u1.getDisplaySymbol() + " --> " + u2.getDisplaySymbol() + " failed. " + |
4541 |
22 Sep 08 |
nicklas |
270 |
"Got " + v1Tov2 +"; expected " + v2); |
4541 |
22 Sep 08 |
nicklas |
271 |
} |
4541 |
22 Sep 08 |
nicklas |
272 |
if (Math.abs(v1 - v2Tov1) > delta) |
4541 |
22 Sep 08 |
nicklas |
273 |
{ |
4541 |
22 Sep 08 |
nicklas |
274 |
throw new BaseException("Conversion of " + |
4541 |
22 Sep 08 |
nicklas |
275 |
v2 + u2.getDisplaySymbol() + " --> " + u1.getDisplaySymbol() + " failed. " + |
4541 |
22 Sep 08 |
nicklas |
276 |
"Got " + v2Tov1 +"; expected " + v1); |
4541 |
22 Sep 08 |
nicklas |
277 |
} |
4541 |
22 Sep 08 |
nicklas |
278 |
if (!TestUtil.getSilent()) |
4541 |
22 Sep 08 |
nicklas |
279 |
{ |
4541 |
22 Sep 08 |
nicklas |
280 |
write("--" + v1 + u1.getDisplaySymbol() + " ~= " + v1Tov2 + u2.getDisplaySymbol()); |
4541 |
22 Sep 08 |
nicklas |
281 |
write("--" + v2 + u2.getDisplaySymbol() + " ~= " + v2Tov1 + u1.getDisplaySymbol()); |
4541 |
22 Sep 08 |
nicklas |
282 |
} |
4541 |
22 Sep 08 |
nicklas |
283 |
|
4541 |
22 Sep 08 |
nicklas |
284 |
write("--Convert units OK"); |
4541 |
22 Sep 08 |
nicklas |
285 |
} |
4541 |
22 Sep 08 |
nicklas |
286 |
catch (Throwable ex) |
4541 |
22 Sep 08 |
nicklas |
287 |
{ |
4541 |
22 Sep 08 |
nicklas |
288 |
write("--Convert units FAILED"); |
4541 |
22 Sep 08 |
nicklas |
289 |
ex.printStackTrace(); |
4541 |
22 Sep 08 |
nicklas |
290 |
ok = false; |
4541 |
22 Sep 08 |
nicklas |
291 |
} |
4541 |
22 Sep 08 |
nicklas |
292 |
finally |
4541 |
22 Sep 08 |
nicklas |
293 |
{ |
4541 |
22 Sep 08 |
nicklas |
294 |
if (dc != null) dc.close(); |
4541 |
22 Sep 08 |
nicklas |
295 |
} |
4541 |
22 Sep 08 |
nicklas |
296 |
} |
4541 |
22 Sep 08 |
nicklas |
297 |
|
4541 |
22 Sep 08 |
nicklas |
298 |
static void test_add_symbol(int id, String symbol) |
4541 |
22 Sep 08 |
nicklas |
299 |
{ |
4541 |
22 Sep 08 |
nicklas |
300 |
if (id == 0) return; |
4541 |
22 Sep 08 |
nicklas |
301 |
DbControl dc = null; |
4541 |
22 Sep 08 |
nicklas |
302 |
try |
4541 |
22 Sep 08 |
nicklas |
303 |
{ |
4541 |
22 Sep 08 |
nicklas |
304 |
dc = TestUtil.getDbControl(); |
4541 |
22 Sep 08 |
nicklas |
305 |
Unit u = Unit.getById(dc, id); |
4541 |
22 Sep 08 |
nicklas |
306 |
u.addSymbol(symbol); |
4541 |
22 Sep 08 |
nicklas |
307 |
dc.commit(); |
4541 |
22 Sep 08 |
nicklas |
308 |
dc = TestUtil.getDbControl(); |
4543 |
22 Sep 08 |
nicklas |
309 |
u = Unit.getById(dc, id); |
4541 |
22 Sep 08 |
nicklas |
310 |
write_item(0, u); |
4543 |
22 Sep 08 |
nicklas |
311 |
if (!u.getSymbols().contains(symbol)) |
4543 |
22 Sep 08 |
nicklas |
312 |
{ |
4543 |
22 Sep 08 |
nicklas |
313 |
throw new BaseException("Add symbol '" + symbol + "' to unit '" + u.getName() + "' failed."); |
4543 |
22 Sep 08 |
nicklas |
314 |
} |
4541 |
22 Sep 08 |
nicklas |
315 |
write("--Add symbol to unit OK"); |
4541 |
22 Sep 08 |
nicklas |
316 |
} |
4541 |
22 Sep 08 |
nicklas |
317 |
catch (Throwable ex) |
4541 |
22 Sep 08 |
nicklas |
318 |
{ |
4541 |
22 Sep 08 |
nicklas |
319 |
write("--Add symbol to unit FAILED"); |
4541 |
22 Sep 08 |
nicklas |
320 |
ex.printStackTrace(); |
4541 |
22 Sep 08 |
nicklas |
321 |
ok = false; |
4541 |
22 Sep 08 |
nicklas |
322 |
} |
4541 |
22 Sep 08 |
nicklas |
323 |
finally |
4541 |
22 Sep 08 |
nicklas |
324 |
{ |
4541 |
22 Sep 08 |
nicklas |
325 |
if (dc != null) dc.close(); |
4541 |
22 Sep 08 |
nicklas |
326 |
} |
4541 |
22 Sep 08 |
nicklas |
327 |
} |
4541 |
22 Sep 08 |
nicklas |
328 |
|
4541 |
22 Sep 08 |
nicklas |
329 |
static void test_remove_symbol(int id, String symbol) |
4541 |
22 Sep 08 |
nicklas |
330 |
{ |
4541 |
22 Sep 08 |
nicklas |
331 |
if (id == 0) return; |
4541 |
22 Sep 08 |
nicklas |
332 |
DbControl dc = null; |
4541 |
22 Sep 08 |
nicklas |
333 |
try |
4541 |
22 Sep 08 |
nicklas |
334 |
{ |
4541 |
22 Sep 08 |
nicklas |
335 |
dc = TestUtil.getDbControl(); |
4541 |
22 Sep 08 |
nicklas |
336 |
Unit u = Unit.getById(dc, id); |
4541 |
22 Sep 08 |
nicklas |
337 |
u.removeSymbol(symbol); |
4541 |
22 Sep 08 |
nicklas |
338 |
dc.commit(); |
4541 |
22 Sep 08 |
nicklas |
339 |
dc = TestUtil.getDbControl(); |
4543 |
22 Sep 08 |
nicklas |
340 |
u = Unit.getById(dc, id); |
4541 |
22 Sep 08 |
nicklas |
341 |
write_item(0, u); |
4543 |
22 Sep 08 |
nicklas |
342 |
if (u.getSymbols().contains(symbol)) |
4543 |
22 Sep 08 |
nicklas |
343 |
{ |
4543 |
22 Sep 08 |
nicklas |
344 |
throw new BaseException("Remove symbol '" + symbol + "' from unit '" + u.getName() + "' failed."); |
4543 |
22 Sep 08 |
nicklas |
345 |
} |
4541 |
22 Sep 08 |
nicklas |
346 |
write("--Remove symbol from unit OK"); |
4541 |
22 Sep 08 |
nicklas |
347 |
} |
4541 |
22 Sep 08 |
nicklas |
348 |
catch (Throwable ex) |
4541 |
22 Sep 08 |
nicklas |
349 |
{ |
4541 |
22 Sep 08 |
nicklas |
350 |
write("--Remove symbol from unit FAILED"); |
4541 |
22 Sep 08 |
nicklas |
351 |
ex.printStackTrace(); |
4541 |
22 Sep 08 |
nicklas |
352 |
ok = false; |
4541 |
22 Sep 08 |
nicklas |
353 |
} |
4541 |
22 Sep 08 |
nicklas |
354 |
finally |
4541 |
22 Sep 08 |
nicklas |
355 |
{ |
4541 |
22 Sep 08 |
nicklas |
356 |
if (dc != null) dc.close(); |
4541 |
22 Sep 08 |
nicklas |
357 |
} |
4541 |
22 Sep 08 |
nicklas |
358 |
} |
4555 |
02 Oct 08 |
nicklas |
359 |
|
4555 |
02 Oct 08 |
nicklas |
360 |
static void test_split_value(String value, String number, String unit) |
4555 |
02 Oct 08 |
nicklas |
361 |
{ |
4555 |
02 Oct 08 |
nicklas |
362 |
String[] valueWithUnit = UnitUtil.splitValueWithUnit(value); |
4555 |
02 Oct 08 |
nicklas |
363 |
boolean splitOk = false; |
4555 |
02 Oct 08 |
nicklas |
364 |
if (number == null) |
4555 |
02 Oct 08 |
nicklas |
365 |
{ |
4555 |
02 Oct 08 |
nicklas |
366 |
splitOk = valueWithUnit == null; |
4555 |
02 Oct 08 |
nicklas |
367 |
} |
4555 |
02 Oct 08 |
nicklas |
368 |
else if (valueWithUnit != null) |
4555 |
02 Oct 08 |
nicklas |
369 |
{ |
4555 |
02 Oct 08 |
nicklas |
370 |
splitOk = number.equals(valueWithUnit[0]) && StringUtil.isEqualOrNull(unit, valueWithUnit[1]); |
4555 |
02 Oct 08 |
nicklas |
371 |
} |
4555 |
02 Oct 08 |
nicklas |
372 |
|
4555 |
02 Oct 08 |
nicklas |
373 |
if (splitOk) |
4555 |
02 Oct 08 |
nicklas |
374 |
{ |
4555 |
02 Oct 08 |
nicklas |
375 |
write("--" + value + " --> " + number + "[" + unit + "] OK"); |
4555 |
02 Oct 08 |
nicklas |
376 |
} |
4555 |
02 Oct 08 |
nicklas |
377 |
else |
4555 |
02 Oct 08 |
nicklas |
378 |
{ |
4555 |
02 Oct 08 |
nicklas |
379 |
String msg = "--" + value + " --> " + number + "[" + unit + "] FAILED: "; |
4555 |
02 Oct 08 |
nicklas |
380 |
if (valueWithUnit == null) |
4555 |
02 Oct 08 |
nicklas |
381 |
{ |
4555 |
02 Oct 08 |
nicklas |
382 |
msg += "null"; |
4555 |
02 Oct 08 |
nicklas |
383 |
} |
4555 |
02 Oct 08 |
nicklas |
384 |
else |
4555 |
02 Oct 08 |
nicklas |
385 |
{ |
4555 |
02 Oct 08 |
nicklas |
386 |
msg += valueWithUnit[0] + "[" + valueWithUnit[1] + "]"; |
4555 |
02 Oct 08 |
nicklas |
387 |
} |
4555 |
02 Oct 08 |
nicklas |
388 |
write(msg); |
4555 |
02 Oct 08 |
nicklas |
389 |
ok = false; |
4555 |
02 Oct 08 |
nicklas |
390 |
} |
4555 |
02 Oct 08 |
nicklas |
391 |
} |
4541 |
22 Sep 08 |
nicklas |
392 |
} |