5632 |
17 May 11 |
nicklas |
1 |
/* |
5632 |
17 May 11 |
nicklas |
$Id$ |
5632 |
17 May 11 |
nicklas |
3 |
|
5632 |
17 May 11 |
nicklas |
Copyright (C) 2005 Nicklas Nordborg |
5632 |
17 May 11 |
nicklas |
Copyright (C) 2006 Jari Häkkinen, Nicklas Nordborg |
5632 |
17 May 11 |
nicklas |
6 |
|
5632 |
17 May 11 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
5632 |
17 May 11 |
nicklas |
Available at http://base.thep.lu.se/ |
5632 |
17 May 11 |
nicklas |
9 |
|
5632 |
17 May 11 |
nicklas |
BASE is free software; you can redistribute it and/or |
5632 |
17 May 11 |
nicklas |
modify it under the terms of the GNU General Public License |
5632 |
17 May 11 |
nicklas |
as published by the Free Software Foundation; either version 3 |
5632 |
17 May 11 |
nicklas |
of the License, or (at your option) any later version. |
5632 |
17 May 11 |
nicklas |
14 |
|
5632 |
17 May 11 |
nicklas |
BASE is distributed in the hope that it will be useful, |
5632 |
17 May 11 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
5632 |
17 May 11 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
5632 |
17 May 11 |
nicklas |
GNU General Public License for more details. |
5632 |
17 May 11 |
nicklas |
19 |
|
5632 |
17 May 11 |
nicklas |
You should have received a copy of the GNU General Public License |
5632 |
17 May 11 |
nicklas |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
5632 |
17 May 11 |
nicklas |
22 |
*/ |
5632 |
17 May 11 |
nicklas |
23 |
import net.sf.basedb.core.*; |
5632 |
17 May 11 |
nicklas |
24 |
import net.sf.basedb.core.query.Expressions; |
5632 |
17 May 11 |
nicklas |
25 |
import net.sf.basedb.core.query.Hql; |
5632 |
17 May 11 |
nicklas |
26 |
import net.sf.basedb.core.query.Restrictions; |
5632 |
17 May 11 |
nicklas |
27 |
|
5632 |
17 May 11 |
nicklas |
28 |
import java.util.Date; |
5632 |
17 May 11 |
nicklas |
29 |
import java.util.Set; |
5632 |
17 May 11 |
nicklas |
30 |
|
5632 |
17 May 11 |
nicklas |
31 |
public class TestTag |
5632 |
17 May 11 |
nicklas |
32 |
{ |
5632 |
17 May 11 |
nicklas |
33 |
|
5632 |
17 May 11 |
nicklas |
34 |
static boolean ok = true; |
5632 |
17 May 11 |
nicklas |
35 |
public static void main(String[] args) |
5632 |
17 May 11 |
nicklas |
36 |
{ |
5632 |
17 May 11 |
nicklas |
37 |
TestUtil.checkArgs(args); |
5632 |
17 May 11 |
nicklas |
38 |
TestUtil.begin(); |
5632 |
17 May 11 |
nicklas |
39 |
ok = test_all(); |
5632 |
17 May 11 |
nicklas |
40 |
TestUtil.stop(); |
5632 |
17 May 11 |
nicklas |
41 |
} |
5632 |
17 May 11 |
nicklas |
42 |
|
5632 |
17 May 11 |
nicklas |
43 |
static boolean test_all() |
5632 |
17 May 11 |
nicklas |
44 |
{ |
5632 |
17 May 11 |
nicklas |
45 |
write("++Testing tags"); |
5632 |
17 May 11 |
nicklas |
46 |
write_header(); |
5632 |
17 May 11 |
nicklas |
// Standard tests: create, load, list |
5632 |
17 May 11 |
nicklas |
48 |
int id = test_create("Test label #1", 0, true); |
5632 |
17 May 11 |
nicklas |
49 |
int id2 = test_create("Test label #2", 0, false); |
5632 |
17 May 11 |
nicklas |
50 |
test_load(id); |
5632 |
17 May 11 |
nicklas |
51 |
test_list(-1); |
5632 |
17 May 11 |
nicklas |
52 |
|
5632 |
17 May 11 |
nicklas |
// Standard test: Delete |
5632 |
17 May 11 |
nicklas |
54 |
if (TestUtil.waitBeforeDelete()) TestUtil.waitForEnter(); |
5632 |
17 May 11 |
nicklas |
55 |
test_delete(id); |
5632 |
17 May 11 |
nicklas |
56 |
test_delete(id2); |
5632 |
17 May 11 |
nicklas |
57 |
write("++Testing tags "+(ok ? "OK" : "Failed")+"\n"); |
5632 |
17 May 11 |
nicklas |
58 |
return ok; |
5632 |
17 May 11 |
nicklas |
59 |
} |
5632 |
17 May 11 |
nicklas |
60 |
|
5632 |
17 May 11 |
nicklas |
61 |
static int test_create(String name, int subtypeId, boolean setAll) |
5632 |
17 May 11 |
nicklas |
62 |
{ |
5632 |
17 May 11 |
nicklas |
63 |
if (!TestUtil.hasPermission(Permission.CREATE, Item.TAG)) return 0; |
5632 |
17 May 11 |
nicklas |
64 |
int id = 0; |
5632 |
17 May 11 |
nicklas |
65 |
DbControl dc = null; |
5632 |
17 May 11 |
nicklas |
66 |
try |
5632 |
17 May 11 |
nicklas |
67 |
{ |
5632 |
17 May 11 |
nicklas |
68 |
dc = TestUtil.getDbControl(); |
5632 |
17 May 11 |
nicklas |
69 |
Tag t = Tag.getNew(dc); |
6082 |
14 Aug 12 |
nicklas |
70 |
t.setName(name); |
6082 |
14 Aug 12 |
nicklas |
71 |
if (subtypeId != 0) |
6082 |
14 Aug 12 |
nicklas |
72 |
{ |
6082 |
14 Aug 12 |
nicklas |
73 |
t.setItemSubtype(ItemSubtype.getById(dc, subtypeId)); |
6082 |
14 Aug 12 |
nicklas |
74 |
} |
5632 |
17 May 11 |
nicklas |
75 |
if (setAll) |
5632 |
17 May 11 |
nicklas |
76 |
{ |
5632 |
17 May 11 |
nicklas |
77 |
t.setDescription("Added at "+new Date()); |
5632 |
17 May 11 |
nicklas |
78 |
} |
5632 |
17 May 11 |
nicklas |
79 |
dc.saveItem(t); |
5632 |
17 May 11 |
nicklas |
80 |
dc.commit(); |
5632 |
17 May 11 |
nicklas |
81 |
id = t.getId(); |
5662 |
20 Jun 11 |
nicklas |
82 |
dc = TestUtil.getDbControl(); |
5662 |
20 Jun 11 |
nicklas |
83 |
dc.reattachItem(t, false); |
5632 |
17 May 11 |
nicklas |
84 |
write_item(0, t); |
5632 |
17 May 11 |
nicklas |
85 |
write("--Create tag OK"); |
5632 |
17 May 11 |
nicklas |
86 |
} |
5632 |
17 May 11 |
nicklas |
87 |
catch (Throwable ex) |
5632 |
17 May 11 |
nicklas |
88 |
{ |
5632 |
17 May 11 |
nicklas |
89 |
write("--Create tag FAILED"); |
5632 |
17 May 11 |
nicklas |
90 |
ex.printStackTrace(); |
5632 |
17 May 11 |
nicklas |
91 |
ok = false; |
5632 |
17 May 11 |
nicklas |
92 |
} |
5632 |
17 May 11 |
nicklas |
93 |
finally |
5632 |
17 May 11 |
nicklas |
94 |
{ |
5632 |
17 May 11 |
nicklas |
95 |
if (dc != null) dc.close(); |
5632 |
17 May 11 |
nicklas |
96 |
} |
5632 |
17 May 11 |
nicklas |
97 |
return id; |
5632 |
17 May 11 |
nicklas |
98 |
} |
5632 |
17 May 11 |
nicklas |
99 |
|
5632 |
17 May 11 |
nicklas |
100 |
static void test_load(int id) |
5632 |
17 May 11 |
nicklas |
101 |
{ |
5632 |
17 May 11 |
nicklas |
102 |
if (id == 0) return; |
5632 |
17 May 11 |
nicklas |
103 |
DbControl dc = null; |
5632 |
17 May 11 |
nicklas |
104 |
try |
5632 |
17 May 11 |
nicklas |
105 |
{ |
5632 |
17 May 11 |
nicklas |
106 |
dc = TestUtil.getDbControl(); |
5632 |
17 May 11 |
nicklas |
107 |
Tag t = Tag.getById(dc, id); |
5632 |
17 May 11 |
nicklas |
108 |
write_item(0, t); |
5632 |
17 May 11 |
nicklas |
109 |
write("--Load tag OK"); |
5632 |
17 May 11 |
nicklas |
110 |
} |
5632 |
17 May 11 |
nicklas |
111 |
catch (Throwable ex) |
5632 |
17 May 11 |
nicklas |
112 |
{ |
5632 |
17 May 11 |
nicklas |
113 |
write("--Load tag FAILED"); |
5632 |
17 May 11 |
nicklas |
114 |
ex.printStackTrace(); |
5632 |
17 May 11 |
nicklas |
115 |
ok = false; |
5632 |
17 May 11 |
nicklas |
116 |
} |
5632 |
17 May 11 |
nicklas |
117 |
finally |
5632 |
17 May 11 |
nicklas |
118 |
{ |
5632 |
17 May 11 |
nicklas |
119 |
if (dc != null) dc.close(); |
5632 |
17 May 11 |
nicklas |
120 |
} |
5632 |
17 May 11 |
nicklas |
121 |
} |
5632 |
17 May 11 |
nicklas |
122 |
|
5632 |
17 May 11 |
nicklas |
123 |
static int test_load(String name) |
5632 |
17 May 11 |
nicklas |
124 |
{ |
5632 |
17 May 11 |
nicklas |
125 |
if (name == null) return 0; |
5632 |
17 May 11 |
nicklas |
126 |
DbControl dc = null; |
5632 |
17 May 11 |
nicklas |
127 |
int id = 0; |
5632 |
17 May 11 |
nicklas |
128 |
try |
5632 |
17 May 11 |
nicklas |
129 |
{ |
5632 |
17 May 11 |
nicklas |
130 |
dc = TestUtil.getDbControl(); |
5632 |
17 May 11 |
nicklas |
131 |
ItemQuery<Tag> query = Tag.getQuery(); |
5632 |
17 May 11 |
nicklas |
132 |
query.restrict(Restrictions.eq(Hql.property("name"), Expressions.string(name))); |
5632 |
17 May 11 |
nicklas |
133 |
Tag t = query.list(dc).get(0); |
5632 |
17 May 11 |
nicklas |
134 |
write_item(0, t); |
5632 |
17 May 11 |
nicklas |
135 |
write("--Load tag by name OK"); |
5632 |
17 May 11 |
nicklas |
136 |
id = t.getId(); |
5632 |
17 May 11 |
nicklas |
137 |
} |
5632 |
17 May 11 |
nicklas |
138 |
catch (Throwable ex) |
5632 |
17 May 11 |
nicklas |
139 |
{ |
5632 |
17 May 11 |
nicklas |
140 |
write("--Load tag by name FAILED"); |
5632 |
17 May 11 |
nicklas |
141 |
ex.printStackTrace(); |
5632 |
17 May 11 |
nicklas |
142 |
ok = false; |
5632 |
17 May 11 |
nicklas |
143 |
} |
5632 |
17 May 11 |
nicklas |
144 |
finally |
5632 |
17 May 11 |
nicklas |
145 |
{ |
5632 |
17 May 11 |
nicklas |
146 |
if (dc != null) dc.close(); |
5632 |
17 May 11 |
nicklas |
147 |
} |
5632 |
17 May 11 |
nicklas |
148 |
return id; |
5632 |
17 May 11 |
nicklas |
149 |
} |
5632 |
17 May 11 |
nicklas |
150 |
|
5632 |
17 May 11 |
nicklas |
151 |
|
5632 |
17 May 11 |
nicklas |
152 |
static void test_list(int expectedResults) |
5632 |
17 May 11 |
nicklas |
153 |
{ |
5632 |
17 May 11 |
nicklas |
154 |
DbControl dc = null; |
5632 |
17 May 11 |
nicklas |
155 |
try |
5632 |
17 May 11 |
nicklas |
156 |
{ |
5632 |
17 May 11 |
nicklas |
157 |
dc = TestUtil.getDbControl(); |
5632 |
17 May 11 |
nicklas |
158 |
ItemResultList<Tag> l = Tag.getQuery().list(dc); |
5632 |
17 May 11 |
nicklas |
159 |
for (int i = 0; i<l.size(); i++) |
5632 |
17 May 11 |
nicklas |
160 |
{ |
5632 |
17 May 11 |
nicklas |
161 |
write_item(i, l.get(i)); |
5632 |
17 May 11 |
nicklas |
162 |
} |
5632 |
17 May 11 |
nicklas |
163 |
if (expectedResults >= 0 && expectedResults != l.size()) |
5632 |
17 May 11 |
nicklas |
164 |
{ |
5632 |
17 May 11 |
nicklas |
165 |
throw new BaseException("Expected "+expectedResults+" results, not "+l.size()); |
5632 |
17 May 11 |
nicklas |
166 |
} |
5632 |
17 May 11 |
nicklas |
167 |
write("--List tags OK ("+l.size()+")"); |
5632 |
17 May 11 |
nicklas |
168 |
} |
5632 |
17 May 11 |
nicklas |
169 |
catch (Throwable ex) |
5632 |
17 May 11 |
nicklas |
170 |
{ |
5632 |
17 May 11 |
nicklas |
171 |
write("--List tags FAILED"); |
5632 |
17 May 11 |
nicklas |
172 |
ex.printStackTrace(); |
5632 |
17 May 11 |
nicklas |
173 |
ok = false; |
5632 |
17 May 11 |
nicklas |
174 |
} |
5632 |
17 May 11 |
nicklas |
175 |
finally |
5632 |
17 May 11 |
nicklas |
176 |
{ |
5632 |
17 May 11 |
nicklas |
177 |
if (dc != null) dc.close(); |
5632 |
17 May 11 |
nicklas |
178 |
} |
5632 |
17 May 11 |
nicklas |
179 |
} |
5632 |
17 May 11 |
nicklas |
180 |
|
5632 |
17 May 11 |
nicklas |
181 |
static void test_delete(int id) |
5632 |
17 May 11 |
nicklas |
182 |
{ |
5632 |
17 May 11 |
nicklas |
183 |
if (id == 0) return; |
5632 |
17 May 11 |
nicklas |
184 |
DbControl dc = null; |
5632 |
17 May 11 |
nicklas |
185 |
try |
5632 |
17 May 11 |
nicklas |
186 |
{ |
5632 |
17 May 11 |
nicklas |
187 |
dc = TestUtil.getDbControl(); |
5632 |
17 May 11 |
nicklas |
188 |
Tag t = Tag.getById(dc, id); |
5632 |
17 May 11 |
nicklas |
189 |
dc.deleteItem(t); |
5632 |
17 May 11 |
nicklas |
190 |
Set<ItemProxy> using = t.getUsingItems(); |
5632 |
17 May 11 |
nicklas |
191 |
if (using.size() > 0) |
5632 |
17 May 11 |
nicklas |
192 |
{ |
5632 |
17 May 11 |
nicklas |
193 |
throw new BaseException(t + " is used by " + using); |
5632 |
17 May 11 |
nicklas |
194 |
} |
5632 |
17 May 11 |
nicklas |
195 |
dc.commit(); |
5632 |
17 May 11 |
nicklas |
196 |
write("--Delete tag OK"); |
5632 |
17 May 11 |
nicklas |
197 |
} |
5632 |
17 May 11 |
nicklas |
198 |
catch (Throwable ex) |
5632 |
17 May 11 |
nicklas |
199 |
{ |
5632 |
17 May 11 |
nicklas |
200 |
write("--Delete tag FAILED"); |
5632 |
17 May 11 |
nicklas |
201 |
ex.printStackTrace(); |
5632 |
17 May 11 |
nicklas |
202 |
ok = false; |
5632 |
17 May 11 |
nicklas |
203 |
} |
5632 |
17 May 11 |
nicklas |
204 |
finally |
5632 |
17 May 11 |
nicklas |
205 |
{ |
5632 |
17 May 11 |
nicklas |
206 |
if (dc != null) dc.close(); |
5632 |
17 May 11 |
nicklas |
207 |
} |
5632 |
17 May 11 |
nicklas |
208 |
} |
5632 |
17 May 11 |
nicklas |
209 |
|
5632 |
17 May 11 |
nicklas |
210 |
static void write_header() |
5632 |
17 May 11 |
nicklas |
211 |
{ |
5632 |
17 May 11 |
nicklas |
212 |
if (!TestUtil.getSilent()) |
5632 |
17 May 11 |
nicklas |
213 |
{ |
5632 |
17 May 11 |
nicklas |
214 |
write(" \tID \tName \tSubtype\tDescription"); |
5632 |
17 May 11 |
nicklas |
215 |
write("-- \t-- \t--------- \t-------\t-----------"); |
5632 |
17 May 11 |
nicklas |
216 |
} |
5632 |
17 May 11 |
nicklas |
217 |
} |
5632 |
17 May 11 |
nicklas |
218 |
static void write_item(int i, Tag t) |
5632 |
17 May 11 |
nicklas |
219 |
throws BaseException |
5632 |
17 May 11 |
nicklas |
220 |
{ |
5632 |
17 May 11 |
nicklas |
221 |
if (!TestUtil.getSilent()) System.out.println(i+":\t"+t.getId()+"\t"+t.getName()+"\t"+t.getItemSubtype()+"\t"+t.getDescription()); |
5632 |
17 May 11 |
nicklas |
222 |
} |
5632 |
17 May 11 |
nicklas |
223 |
static void write(String message) |
5632 |
17 May 11 |
nicklas |
224 |
{ |
5632 |
17 May 11 |
nicklas |
225 |
System.out.println(message); |
5632 |
17 May 11 |
nicklas |
226 |
} |
5632 |
17 May 11 |
nicklas |
227 |
} |