357 |
04 Apr 05 |
nicklas |
1 |
/* |
357 |
04 Apr 05 |
nicklas |
$Id$ |
357 |
04 Apr 05 |
nicklas |
3 |
|
3675 |
16 Aug 07 |
jari |
Copyright (C) 2005 Samuel Andersson, Nicklas Nordborg |
4889 |
06 Apr 09 |
nicklas |
Copyright (C) 2006 Jari Häkkinen |
3675 |
16 Aug 07 |
jari |
Copyright (C) 2007 Nicklas Nordborg |
357 |
04 Apr 05 |
nicklas |
7 |
|
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/ |
357 |
04 Apr 05 |
nicklas |
10 |
|
357 |
04 Apr 05 |
nicklas |
BASE is free software; you can redistribute it and/or |
357 |
04 Apr 05 |
nicklas |
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 |
357 |
04 Apr 05 |
nicklas |
of the License, or (at your option) any later version. |
357 |
04 Apr 05 |
nicklas |
15 |
|
357 |
04 Apr 05 |
nicklas |
BASE is distributed in the hope that it will be useful, |
357 |
04 Apr 05 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
357 |
04 Apr 05 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
357 |
04 Apr 05 |
nicklas |
GNU General Public License for more details. |
357 |
04 Apr 05 |
nicklas |
20 |
|
357 |
04 Apr 05 |
nicklas |
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/>. |
357 |
04 Apr 05 |
nicklas |
23 |
*/ |
357 |
04 Apr 05 |
nicklas |
24 |
import net.sf.basedb.core.*; |
3272 |
26 Apr 07 |
nicklas |
25 |
import net.sf.basedb.plugins.HelpImporter; |
3272 |
26 Apr 07 |
nicklas |
26 |
import net.sf.basedb.util.FileUtil; |
3272 |
26 Apr 07 |
nicklas |
27 |
|
3272 |
26 Apr 07 |
nicklas |
28 |
import java.io.InputStream; |
357 |
04 Apr 05 |
nicklas |
29 |
import java.util.Date; |
5340 |
10 May 10 |
nicklas |
30 |
import java.util.Set; |
357 |
04 Apr 05 |
nicklas |
31 |
|
357 |
04 Apr 05 |
nicklas |
32 |
public class TestHelp |
357 |
04 Apr 05 |
nicklas |
33 |
{ |
357 |
04 Apr 05 |
nicklas |
34 |
|
357 |
04 Apr 05 |
nicklas |
35 |
static boolean ok = true; |
357 |
04 Apr 05 |
nicklas |
36 |
|
357 |
04 Apr 05 |
nicklas |
37 |
public static void main(String[] args) |
357 |
04 Apr 05 |
nicklas |
38 |
{ |
357 |
04 Apr 05 |
nicklas |
39 |
TestUtil.checkArgs(args); |
357 |
04 Apr 05 |
nicklas |
40 |
TestUtil.begin(); |
357 |
04 Apr 05 |
nicklas |
41 |
ok = test_all(); |
357 |
04 Apr 05 |
nicklas |
42 |
TestUtil.stop(); |
357 |
04 Apr 05 |
nicklas |
43 |
} |
357 |
04 Apr 05 |
nicklas |
44 |
|
357 |
04 Apr 05 |
nicklas |
45 |
static boolean test_all() |
357 |
04 Apr 05 |
nicklas |
46 |
{ |
357 |
04 Apr 05 |
nicklas |
47 |
write("++Testing Help"); |
357 |
04 Apr 05 |
nicklas |
48 |
write_header(); |
357 |
04 Apr 05 |
nicklas |
// Standard tests: create, load, list |
357 |
04 Apr 05 |
nicklas |
50 |
int clientId = TestClient.test_create("net.sf.basedb.clients.testcreate", true); |
357 |
04 Apr 05 |
nicklas |
51 |
int id = test_create(clientId, "help.text", true); |
357 |
04 Apr 05 |
nicklas |
52 |
int id2 = test_create(clientId, "help.text2", false); |
357 |
04 Apr 05 |
nicklas |
53 |
test_load(id); |
357 |
04 Apr 05 |
nicklas |
54 |
test_list(clientId, -1); |
357 |
04 Apr 05 |
nicklas |
55 |
|
357 |
04 Apr 05 |
nicklas |
// Extra tests: |
5689 |
11 Aug 11 |
nicklas |
57 |
if (id != 0) test_load_external_id(clientId, "help.text"); |
357 |
04 Apr 05 |
nicklas |
58 |
|
3272 |
26 Apr 07 |
nicklas |
59 |
test_import("net.sf.basedb.clients.web", "../../data/helptexts.xml"); |
3272 |
26 Apr 07 |
nicklas |
60 |
|
357 |
04 Apr 05 |
nicklas |
// Standard test: Delete |
1529 |
27 Oct 05 |
nicklas |
62 |
if (TestUtil.waitBeforeDelete()) TestUtil.waitForEnter(); |
357 |
04 Apr 05 |
nicklas |
63 |
test_delete(id); |
357 |
04 Apr 05 |
nicklas |
64 |
test_delete(id2); |
357 |
04 Apr 05 |
nicklas |
65 |
TestClient.test_delete(clientId); |
357 |
04 Apr 05 |
nicklas |
66 |
write("++Testing help "+(ok ? "OK" : "Failed")+"\n"); |
357 |
04 Apr 05 |
nicklas |
67 |
return ok; |
357 |
04 Apr 05 |
nicklas |
68 |
} |
357 |
04 Apr 05 |
nicklas |
69 |
|
357 |
04 Apr 05 |
nicklas |
70 |
static int test_create(int clientId, String externalId, boolean setAll) |
357 |
04 Apr 05 |
nicklas |
71 |
{ |
357 |
04 Apr 05 |
nicklas |
72 |
if (clientId == 0) return 0; |
357 |
04 Apr 05 |
nicklas |
73 |
int id = 0; |
357 |
04 Apr 05 |
nicklas |
74 |
DbControl dc = null; |
357 |
04 Apr 05 |
nicklas |
75 |
try |
357 |
04 Apr 05 |
nicklas |
76 |
{ |
357 |
04 Apr 05 |
nicklas |
77 |
dc = TestUtil.getDbControl(); |
357 |
04 Apr 05 |
nicklas |
78 |
Client client = Client.getById(dc, clientId); |
357 |
04 Apr 05 |
nicklas |
79 |
Help h = Help.getNew(dc, client, externalId); |
357 |
04 Apr 05 |
nicklas |
80 |
if (setAll) |
357 |
04 Apr 05 |
nicklas |
81 |
{ |
357 |
04 Apr 05 |
nicklas |
82 |
h.setName("Test help"); |
357 |
04 Apr 05 |
nicklas |
83 |
h.setDescription("Added at "+new Date()); |
357 |
04 Apr 05 |
nicklas |
84 |
} |
357 |
04 Apr 05 |
nicklas |
85 |
dc.saveItem(h); |
357 |
04 Apr 05 |
nicklas |
86 |
dc.commit(); |
357 |
04 Apr 05 |
nicklas |
87 |
id = h.getId(); |
806 |
22 Jun 05 |
nicklas |
88 |
dc = TestUtil.getDbControl(); |
5060 |
19 Aug 09 |
nicklas |
89 |
dc.reattachItem(h, false); |
357 |
04 Apr 05 |
nicklas |
90 |
write_item(0, h); |
357 |
04 Apr 05 |
nicklas |
91 |
write("--Create help OK"); |
357 |
04 Apr 05 |
nicklas |
92 |
} |
358 |
04 Apr 05 |
nicklas |
93 |
catch (Throwable ex) |
357 |
04 Apr 05 |
nicklas |
94 |
{ |
357 |
04 Apr 05 |
nicklas |
95 |
write("--Create help FAILED"); |
357 |
04 Apr 05 |
nicklas |
96 |
ex.printStackTrace(); |
357 |
04 Apr 05 |
nicklas |
97 |
ok = false; |
357 |
04 Apr 05 |
nicklas |
98 |
} |
357 |
04 Apr 05 |
nicklas |
99 |
finally |
357 |
04 Apr 05 |
nicklas |
100 |
{ |
357 |
04 Apr 05 |
nicklas |
101 |
if (dc != null) dc.close(); |
357 |
04 Apr 05 |
nicklas |
102 |
} |
357 |
04 Apr 05 |
nicklas |
103 |
return id; |
357 |
04 Apr 05 |
nicklas |
104 |
} |
357 |
04 Apr 05 |
nicklas |
105 |
|
357 |
04 Apr 05 |
nicklas |
106 |
static void test_load(int id) |
357 |
04 Apr 05 |
nicklas |
107 |
{ |
357 |
04 Apr 05 |
nicklas |
108 |
if (id == 0) return; |
357 |
04 Apr 05 |
nicklas |
109 |
DbControl dc = null; |
357 |
04 Apr 05 |
nicklas |
110 |
try |
357 |
04 Apr 05 |
nicklas |
111 |
{ |
357 |
04 Apr 05 |
nicklas |
112 |
dc = TestUtil.getDbControl(); |
357 |
04 Apr 05 |
nicklas |
113 |
Help h = Help.getById(dc, id); |
357 |
04 Apr 05 |
nicklas |
114 |
write_item(0, h); |
357 |
04 Apr 05 |
nicklas |
115 |
write("--Load help OK"); |
357 |
04 Apr 05 |
nicklas |
116 |
} |
358 |
04 Apr 05 |
nicklas |
117 |
catch (Throwable ex) |
357 |
04 Apr 05 |
nicklas |
118 |
{ |
357 |
04 Apr 05 |
nicklas |
119 |
write("--Load help FAILED"); |
357 |
04 Apr 05 |
nicklas |
120 |
ex.printStackTrace(); |
357 |
04 Apr 05 |
nicklas |
121 |
ok = false; |
357 |
04 Apr 05 |
nicklas |
122 |
} |
357 |
04 Apr 05 |
nicklas |
123 |
finally |
357 |
04 Apr 05 |
nicklas |
124 |
{ |
357 |
04 Apr 05 |
nicklas |
125 |
if (dc != null) dc.close(); |
357 |
04 Apr 05 |
nicklas |
126 |
} |
357 |
04 Apr 05 |
nicklas |
127 |
} |
357 |
04 Apr 05 |
nicklas |
128 |
|
357 |
04 Apr 05 |
nicklas |
129 |
static void test_list(int clientId, int expectedResults) |
357 |
04 Apr 05 |
nicklas |
130 |
{ |
357 |
04 Apr 05 |
nicklas |
131 |
DbControl dc = null; |
357 |
04 Apr 05 |
nicklas |
132 |
try |
357 |
04 Apr 05 |
nicklas |
133 |
{ |
357 |
04 Apr 05 |
nicklas |
134 |
dc = TestUtil.getDbControl(); |
357 |
04 Apr 05 |
nicklas |
135 |
Client client = Client.getById(dc, clientId); |
1418 |
07 Oct 05 |
nicklas |
136 |
ItemResultList<Help> l = Help.getQuery(client).list(dc); |
357 |
04 Apr 05 |
nicklas |
137 |
for (int i = 0; i<l.size(); i++) |
357 |
04 Apr 05 |
nicklas |
138 |
{ |
357 |
04 Apr 05 |
nicklas |
139 |
write_item(i, l.get(i)); |
357 |
04 Apr 05 |
nicklas |
140 |
} |
357 |
04 Apr 05 |
nicklas |
141 |
if (expectedResults >= 0 && expectedResults != l.size()) |
357 |
04 Apr 05 |
nicklas |
142 |
{ |
357 |
04 Apr 05 |
nicklas |
143 |
throw new BaseException("Expected "+expectedResults+" results, not "+l.size()); |
357 |
04 Apr 05 |
nicklas |
144 |
} |
357 |
04 Apr 05 |
nicklas |
145 |
write("--List help OK ("+l.size()+")"); |
357 |
04 Apr 05 |
nicklas |
146 |
} |
358 |
04 Apr 05 |
nicklas |
147 |
catch (Throwable ex) |
357 |
04 Apr 05 |
nicklas |
148 |
{ |
357 |
04 Apr 05 |
nicklas |
149 |
write("--List help FAILED"); |
357 |
04 Apr 05 |
nicklas |
150 |
ex.printStackTrace(); |
357 |
04 Apr 05 |
nicklas |
151 |
ok = false; |
357 |
04 Apr 05 |
nicklas |
152 |
} |
357 |
04 Apr 05 |
nicklas |
153 |
finally |
357 |
04 Apr 05 |
nicklas |
154 |
{ |
357 |
04 Apr 05 |
nicklas |
155 |
if (dc != null) dc.close(); |
357 |
04 Apr 05 |
nicklas |
156 |
} |
357 |
04 Apr 05 |
nicklas |
157 |
} |
357 |
04 Apr 05 |
nicklas |
158 |
|
357 |
04 Apr 05 |
nicklas |
159 |
static void test_delete(int id) |
357 |
04 Apr 05 |
nicklas |
160 |
{ |
357 |
04 Apr 05 |
nicklas |
161 |
if (id == 0) return; |
357 |
04 Apr 05 |
nicklas |
162 |
DbControl dc = null; |
357 |
04 Apr 05 |
nicklas |
163 |
try |
357 |
04 Apr 05 |
nicklas |
164 |
{ |
357 |
04 Apr 05 |
nicklas |
165 |
dc = TestUtil.getDbControl(); |
357 |
04 Apr 05 |
nicklas |
166 |
Help h = Help.getById(dc, id); |
357 |
04 Apr 05 |
nicklas |
167 |
dc.deleteItem(h); |
5340 |
10 May 10 |
nicklas |
168 |
Set<ItemProxy> using = h.getUsingItems(); |
5340 |
10 May 10 |
nicklas |
169 |
if (using.size() > 0) |
5340 |
10 May 10 |
nicklas |
170 |
{ |
5340 |
10 May 10 |
nicklas |
171 |
throw new BaseException(h + " is used by " + using); |
5340 |
10 May 10 |
nicklas |
172 |
} |
357 |
04 Apr 05 |
nicklas |
173 |
dc.commit(); |
357 |
04 Apr 05 |
nicklas |
174 |
write("--Delete help OK"); |
357 |
04 Apr 05 |
nicklas |
175 |
} |
358 |
04 Apr 05 |
nicklas |
176 |
catch (Throwable ex) |
357 |
04 Apr 05 |
nicklas |
177 |
{ |
357 |
04 Apr 05 |
nicklas |
178 |
write("--Delete help FAILED"); |
357 |
04 Apr 05 |
nicklas |
179 |
ex.printStackTrace(); |
357 |
04 Apr 05 |
nicklas |
180 |
ok = false; |
357 |
04 Apr 05 |
nicklas |
181 |
} |
357 |
04 Apr 05 |
nicklas |
182 |
finally |
357 |
04 Apr 05 |
nicklas |
183 |
{ |
357 |
04 Apr 05 |
nicklas |
184 |
if (dc != null) dc.close(); |
357 |
04 Apr 05 |
nicklas |
185 |
} |
357 |
04 Apr 05 |
nicklas |
186 |
} |
357 |
04 Apr 05 |
nicklas |
187 |
|
357 |
04 Apr 05 |
nicklas |
188 |
static void write_header() |
357 |
04 Apr 05 |
nicklas |
189 |
{ |
357 |
04 Apr 05 |
nicklas |
190 |
if (!TestUtil.getSilent()) |
357 |
04 Apr 05 |
nicklas |
191 |
{ |
357 |
04 Apr 05 |
nicklas |
192 |
write(" \tID \tName \tDescription\tExternalID\tClient"); |
357 |
04 Apr 05 |
nicklas |
193 |
write("-- \t-- \t--------- \t-----------\t----------\t------"); |
357 |
04 Apr 05 |
nicklas |
194 |
} |
357 |
04 Apr 05 |
nicklas |
195 |
} |
357 |
04 Apr 05 |
nicklas |
196 |
static void write_item(int i, Help h) |
357 |
04 Apr 05 |
nicklas |
197 |
throws BaseException |
357 |
04 Apr 05 |
nicklas |
198 |
{ |
357 |
04 Apr 05 |
nicklas |
199 |
if (!TestUtil.getSilent()) |
357 |
04 Apr 05 |
nicklas |
200 |
{ |
357 |
04 Apr 05 |
nicklas |
201 |
System.out.println(i+":\t"+h.getId()+"\t"+h.getName()+"\t"+h.getDescription()+ |
357 |
04 Apr 05 |
nicklas |
202 |
"\t"+h.getExternalId()+"\t"+h.getClient()); |
357 |
04 Apr 05 |
nicklas |
203 |
} |
357 |
04 Apr 05 |
nicklas |
204 |
} |
357 |
04 Apr 05 |
nicklas |
205 |
static void write(String message) |
357 |
04 Apr 05 |
nicklas |
206 |
{ |
357 |
04 Apr 05 |
nicklas |
207 |
System.out.println(message); |
357 |
04 Apr 05 |
nicklas |
208 |
} |
357 |
04 Apr 05 |
nicklas |
209 |
|
357 |
04 Apr 05 |
nicklas |
210 |
static void test_load_external_id(int clientId, String externalId) |
357 |
04 Apr 05 |
nicklas |
211 |
{ |
357 |
04 Apr 05 |
nicklas |
212 |
DbControl dc = null; |
357 |
04 Apr 05 |
nicklas |
213 |
try |
357 |
04 Apr 05 |
nicklas |
214 |
{ |
357 |
04 Apr 05 |
nicklas |
215 |
dc = TestUtil.getDbControl(); |
357 |
04 Apr 05 |
nicklas |
216 |
Client client = Client.getById(dc, clientId); |
5689 |
11 Aug 11 |
nicklas |
217 |
Help h = client.getHelpByExternalId(dc, externalId, false); |
357 |
04 Apr 05 |
nicklas |
218 |
write_item(0, h); |
357 |
04 Apr 05 |
nicklas |
219 |
write("--Load help by external ID OK"); |
357 |
04 Apr 05 |
nicklas |
220 |
} |
358 |
04 Apr 05 |
nicklas |
221 |
catch (Throwable ex) |
357 |
04 Apr 05 |
nicklas |
222 |
{ |
357 |
04 Apr 05 |
nicklas |
223 |
write("--Load help by external ID FAILED"); |
357 |
04 Apr 05 |
nicklas |
224 |
ex.printStackTrace(); |
357 |
04 Apr 05 |
nicklas |
225 |
ok = false; |
357 |
04 Apr 05 |
nicklas |
226 |
} |
357 |
04 Apr 05 |
nicklas |
227 |
finally |
357 |
04 Apr 05 |
nicklas |
228 |
{ |
357 |
04 Apr 05 |
nicklas |
229 |
if (dc != null) dc.close(); |
357 |
04 Apr 05 |
nicklas |
230 |
} |
357 |
04 Apr 05 |
nicklas |
231 |
} |
3272 |
26 Apr 07 |
nicklas |
232 |
|
3272 |
26 Apr 07 |
nicklas |
233 |
static void test_import(String clientId, String filename) |
3272 |
26 Apr 07 |
nicklas |
234 |
{ |
3272 |
26 Apr 07 |
nicklas |
235 |
if (!TestUtil.hasPermission(Permission.WRITE, Item.CLIENT)) return; |
3272 |
26 Apr 07 |
nicklas |
236 |
DbControl dc = null; |
3272 |
26 Apr 07 |
nicklas |
237 |
try |
3272 |
26 Apr 07 |
nicklas |
238 |
{ |
3272 |
26 Apr 07 |
nicklas |
239 |
dc = TestUtil.getDbControl(); |
3272 |
26 Apr 07 |
nicklas |
240 |
Client client = Client.getByExternalId(dc, clientId); |
3272 |
26 Apr 07 |
nicklas |
241 |
dc.close(); |
3272 |
26 Apr 07 |
nicklas |
242 |
java.io.File file = new java.io.File(filename); |
3272 |
26 Apr 07 |
nicklas |
243 |
if (file.exists()) |
3272 |
26 Apr 07 |
nicklas |
244 |
{ |
3272 |
26 Apr 07 |
nicklas |
245 |
InputStream is = FileUtil.getInputStream(file); |
3272 |
26 Apr 07 |
nicklas |
246 |
HelpImporter helpImp = new HelpImporter(); |
3272 |
26 Apr 07 |
nicklas |
247 |
helpImp.init(TestUtil.getSessionControl(), null, null); |
3617 |
01 Aug 07 |
nicklas |
248 |
helpImp.installHelpTexts(client, true, is, file.toString()); |
3272 |
26 Apr 07 |
nicklas |
249 |
helpImp.done(); |
3272 |
26 Apr 07 |
nicklas |
250 |
write("--Import help texts OK"); |
3272 |
26 Apr 07 |
nicklas |
251 |
} |
3272 |
26 Apr 07 |
nicklas |
252 |
else |
3272 |
26 Apr 07 |
nicklas |
253 |
{ |
3272 |
26 Apr 07 |
nicklas |
254 |
write("--Import help texts SKIPPED (help text file doesn't exists"); |
3272 |
26 Apr 07 |
nicklas |
255 |
} |
3272 |
26 Apr 07 |
nicklas |
256 |
} |
3272 |
26 Apr 07 |
nicklas |
257 |
catch (Throwable ex) |
3272 |
26 Apr 07 |
nicklas |
258 |
{ |
3272 |
26 Apr 07 |
nicklas |
259 |
write("--Import help texts FAILED"); |
3272 |
26 Apr 07 |
nicklas |
260 |
ex.printStackTrace(); |
3272 |
26 Apr 07 |
nicklas |
261 |
ok = false; |
3272 |
26 Apr 07 |
nicklas |
262 |
} |
3272 |
26 Apr 07 |
nicklas |
263 |
finally |
3272 |
26 Apr 07 |
nicklas |
264 |
{ |
3272 |
26 Apr 07 |
nicklas |
265 |
if (dc != null) dc.close(); |
3272 |
26 Apr 07 |
nicklas |
266 |
} |
3272 |
26 Apr 07 |
nicklas |
267 |
|
3272 |
26 Apr 07 |
nicklas |
268 |
|
3272 |
26 Apr 07 |
nicklas |
269 |
|
3272 |
26 Apr 07 |
nicklas |
270 |
} |
357 |
04 Apr 05 |
nicklas |
271 |
|
357 |
04 Apr 05 |
nicklas |
272 |
} |
357 |
04 Apr 05 |
nicklas |
273 |
|