239 |
23 Mar 05 |
nicklas |
1 |
/* |
239 |
23 Mar 05 |
nicklas |
$Id$ |
239 |
23 Mar 05 |
nicklas |
3 |
|
3675 |
16 Aug 07 |
jari |
Copyright (C) 2005 Nicklas Nordborg |
4889 |
06 Apr 09 |
nicklas |
Copyright (C) 2006 Jari Häkkinen, Nicklas Nordborg |
239 |
23 Mar 05 |
nicklas |
6 |
|
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/ |
239 |
23 Mar 05 |
nicklas |
9 |
|
239 |
23 Mar 05 |
nicklas |
BASE is free software; you can redistribute it and/or |
239 |
23 Mar 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 |
239 |
23 Mar 05 |
nicklas |
of the License, or (at your option) any later version. |
239 |
23 Mar 05 |
nicklas |
14 |
|
239 |
23 Mar 05 |
nicklas |
BASE is distributed in the hope that it will be useful, |
239 |
23 Mar 05 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
239 |
23 Mar 05 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
239 |
23 Mar 05 |
nicklas |
GNU General Public License for more details. |
239 |
23 Mar 05 |
nicklas |
19 |
|
239 |
23 Mar 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/>. |
239 |
23 Mar 05 |
nicklas |
22 |
*/ |
239 |
23 Mar 05 |
nicklas |
23 |
import net.sf.basedb.core.*; |
6424 |
25 Feb 14 |
nicklas |
24 |
import net.sf.basedb.core.authentication.LoginRequest; |
239 |
23 Mar 05 |
nicklas |
25 |
|
239 |
23 Mar 05 |
nicklas |
26 |
import java.util.Date; |
5340 |
10 May 10 |
nicklas |
27 |
import java.util.Set; |
239 |
23 Mar 05 |
nicklas |
28 |
|
239 |
23 Mar 05 |
nicklas |
29 |
public class TestClient |
239 |
23 Mar 05 |
nicklas |
30 |
{ |
239 |
23 Mar 05 |
nicklas |
31 |
|
239 |
23 Mar 05 |
nicklas |
32 |
static boolean ok = true; |
239 |
23 Mar 05 |
nicklas |
33 |
public static void main(String[] args) |
239 |
23 Mar 05 |
nicklas |
34 |
{ |
239 |
23 Mar 05 |
nicklas |
35 |
TestUtil.checkArgs(args); |
239 |
23 Mar 05 |
nicklas |
36 |
TestUtil.begin(); |
239 |
23 Mar 05 |
nicklas |
37 |
ok = test_all(); |
239 |
23 Mar 05 |
nicklas |
38 |
TestUtil.stop(); |
239 |
23 Mar 05 |
nicklas |
39 |
} |
239 |
23 Mar 05 |
nicklas |
40 |
|
239 |
23 Mar 05 |
nicklas |
41 |
static boolean test_all() |
239 |
23 Mar 05 |
nicklas |
42 |
{ |
239 |
23 Mar 05 |
nicklas |
43 |
write("++Testing client"); |
239 |
23 Mar 05 |
nicklas |
44 |
write_header(); |
239 |
23 Mar 05 |
nicklas |
// Standard tests: create, load, list |
239 |
23 Mar 05 |
nicklas |
46 |
int id = test_create("net.sf.basedb.clients.testcreate", true); |
239 |
23 Mar 05 |
nicklas |
47 |
int id2 = test_create("net.sf.basedb.clients.testcreate2", false); |
239 |
23 Mar 05 |
nicklas |
48 |
test_load(id); |
292 |
31 Mar 05 |
nicklas |
49 |
test_list(-1); |
239 |
23 Mar 05 |
nicklas |
50 |
|
239 |
23 Mar 05 |
nicklas |
// Extra tests: log in and log out with the new client |
239 |
23 Mar 05 |
nicklas |
52 |
test_login_logout(id); |
239 |
23 Mar 05 |
nicklas |
53 |
|
1529 |
27 Oct 05 |
nicklas |
54 |
if (TestUtil.waitBeforeDelete()) TestUtil.waitForEnter(); |
239 |
23 Mar 05 |
nicklas |
// Standard test: Delete |
239 |
23 Mar 05 |
nicklas |
56 |
test_delete(id); |
239 |
23 Mar 05 |
nicklas |
57 |
test_delete(id2); |
239 |
23 Mar 05 |
nicklas |
58 |
write("++Testing client "+(ok ? "OK" : "Failed")+"\n"); |
239 |
23 Mar 05 |
nicklas |
59 |
return ok; |
239 |
23 Mar 05 |
nicklas |
60 |
} |
239 |
23 Mar 05 |
nicklas |
61 |
|
239 |
23 Mar 05 |
nicklas |
62 |
static int test_create(String externalId, boolean setAll) |
239 |
23 Mar 05 |
nicklas |
63 |
{ |
239 |
23 Mar 05 |
nicklas |
64 |
if (!TestUtil.hasPermission(Permission.CREATE, Item.CLIENT)) return 0; |
239 |
23 Mar 05 |
nicklas |
65 |
int id = 0; |
239 |
23 Mar 05 |
nicklas |
66 |
DbControl dc = null; |
239 |
23 Mar 05 |
nicklas |
67 |
try |
239 |
23 Mar 05 |
nicklas |
68 |
{ |
239 |
23 Mar 05 |
nicklas |
69 |
dc = TestUtil.getDbControl(); |
239 |
23 Mar 05 |
nicklas |
70 |
Client c = Client.getNew(dc, externalId); |
239 |
23 Mar 05 |
nicklas |
71 |
if (setAll) |
239 |
23 Mar 05 |
nicklas |
72 |
{ |
239 |
23 Mar 05 |
nicklas |
73 |
c.setName("Test client"); |
239 |
23 Mar 05 |
nicklas |
74 |
c.setDescription("Added at "+new Date()); |
239 |
23 Mar 05 |
nicklas |
75 |
} |
239 |
23 Mar 05 |
nicklas |
76 |
dc.saveItem(c); |
239 |
23 Mar 05 |
nicklas |
77 |
dc.commit(); |
239 |
23 Mar 05 |
nicklas |
78 |
id = c.getId(); |
239 |
23 Mar 05 |
nicklas |
79 |
write_item(0, c); |
239 |
23 Mar 05 |
nicklas |
80 |
write("--Create client OK"); |
239 |
23 Mar 05 |
nicklas |
81 |
} |
358 |
04 Apr 05 |
nicklas |
82 |
catch (Throwable ex) |
239 |
23 Mar 05 |
nicklas |
83 |
{ |
239 |
23 Mar 05 |
nicklas |
84 |
write("--Create client FAILED"); |
239 |
23 Mar 05 |
nicklas |
85 |
ex.printStackTrace(); |
239 |
23 Mar 05 |
nicklas |
86 |
ok = false; |
239 |
23 Mar 05 |
nicklas |
87 |
} |
239 |
23 Mar 05 |
nicklas |
88 |
finally |
239 |
23 Mar 05 |
nicklas |
89 |
{ |
239 |
23 Mar 05 |
nicklas |
90 |
if (dc != null) dc.close(); |
239 |
23 Mar 05 |
nicklas |
91 |
} |
239 |
23 Mar 05 |
nicklas |
92 |
return id; |
239 |
23 Mar 05 |
nicklas |
93 |
} |
239 |
23 Mar 05 |
nicklas |
94 |
|
239 |
23 Mar 05 |
nicklas |
95 |
static void test_load(int id) |
239 |
23 Mar 05 |
nicklas |
96 |
{ |
239 |
23 Mar 05 |
nicklas |
97 |
if (id == 0) return; |
239 |
23 Mar 05 |
nicklas |
98 |
DbControl dc = null; |
239 |
23 Mar 05 |
nicklas |
99 |
try |
239 |
23 Mar 05 |
nicklas |
100 |
{ |
239 |
23 Mar 05 |
nicklas |
101 |
dc = TestUtil.getDbControl(); |
239 |
23 Mar 05 |
nicklas |
102 |
Client c = Client.getById(dc, id); |
239 |
23 Mar 05 |
nicklas |
103 |
write_item(0, c); |
239 |
23 Mar 05 |
nicklas |
104 |
write("--Load client OK"); |
239 |
23 Mar 05 |
nicklas |
105 |
} |
358 |
04 Apr 05 |
nicklas |
106 |
catch (Throwable ex) |
239 |
23 Mar 05 |
nicklas |
107 |
{ |
239 |
23 Mar 05 |
nicklas |
108 |
write("--Load client FAILED"); |
239 |
23 Mar 05 |
nicklas |
109 |
ex.printStackTrace(); |
239 |
23 Mar 05 |
nicklas |
110 |
ok = false; |
239 |
23 Mar 05 |
nicklas |
111 |
} |
239 |
23 Mar 05 |
nicklas |
112 |
finally |
239 |
23 Mar 05 |
nicklas |
113 |
{ |
239 |
23 Mar 05 |
nicklas |
114 |
if (dc != null) dc.close(); |
239 |
23 Mar 05 |
nicklas |
115 |
} |
239 |
23 Mar 05 |
nicklas |
116 |
} |
239 |
23 Mar 05 |
nicklas |
117 |
|
239 |
23 Mar 05 |
nicklas |
118 |
|
292 |
31 Mar 05 |
nicklas |
119 |
static void test_list(int expectedResults) |
239 |
23 Mar 05 |
nicklas |
120 |
{ |
239 |
23 Mar 05 |
nicklas |
121 |
DbControl dc = null; |
239 |
23 Mar 05 |
nicklas |
122 |
try |
239 |
23 Mar 05 |
nicklas |
123 |
{ |
239 |
23 Mar 05 |
nicklas |
124 |
dc = TestUtil.getDbControl(); |
1418 |
07 Oct 05 |
nicklas |
125 |
ItemQuery<Client> q = Client.getQuery(); |
1418 |
07 Oct 05 |
nicklas |
126 |
ItemResultList<Client> l = q.list(dc); |
292 |
31 Mar 05 |
nicklas |
127 |
for (int i = 0; i<l.size(); i++) |
239 |
23 Mar 05 |
nicklas |
128 |
{ |
292 |
31 Mar 05 |
nicklas |
129 |
write_item(i, l.get(i)); |
239 |
23 Mar 05 |
nicklas |
130 |
} |
292 |
31 Mar 05 |
nicklas |
131 |
if (expectedResults >= 0 && expectedResults != l.size()) |
292 |
31 Mar 05 |
nicklas |
132 |
{ |
292 |
31 Mar 05 |
nicklas |
133 |
throw new BaseException("Expected "+expectedResults+" results, not "+l.size()); |
292 |
31 Mar 05 |
nicklas |
134 |
} |
292 |
31 Mar 05 |
nicklas |
135 |
write("--List clients OK ("+l.size()+")"); |
239 |
23 Mar 05 |
nicklas |
136 |
} |
358 |
04 Apr 05 |
nicklas |
137 |
catch (Throwable ex) |
239 |
23 Mar 05 |
nicklas |
138 |
{ |
239 |
23 Mar 05 |
nicklas |
139 |
write("--List clients FAILED"); |
239 |
23 Mar 05 |
nicklas |
140 |
ex.printStackTrace(); |
239 |
23 Mar 05 |
nicklas |
141 |
ok = false; |
239 |
23 Mar 05 |
nicklas |
142 |
} |
239 |
23 Mar 05 |
nicklas |
143 |
finally |
239 |
23 Mar 05 |
nicklas |
144 |
{ |
239 |
23 Mar 05 |
nicklas |
145 |
if (dc != null) dc.close(); |
239 |
23 Mar 05 |
nicklas |
146 |
} |
239 |
23 Mar 05 |
nicklas |
147 |
} |
239 |
23 Mar 05 |
nicklas |
148 |
|
239 |
23 Mar 05 |
nicklas |
149 |
|
239 |
23 Mar 05 |
nicklas |
150 |
static void test_delete(int id) |
239 |
23 Mar 05 |
nicklas |
151 |
{ |
239 |
23 Mar 05 |
nicklas |
152 |
if (id == 0) return; |
239 |
23 Mar 05 |
nicklas |
153 |
DbControl dc = null; |
239 |
23 Mar 05 |
nicklas |
154 |
try |
239 |
23 Mar 05 |
nicklas |
155 |
{ |
239 |
23 Mar 05 |
nicklas |
156 |
dc = TestUtil.getDbControl(); |
239 |
23 Mar 05 |
nicklas |
157 |
Client c = Client.getById(dc, id); |
239 |
23 Mar 05 |
nicklas |
158 |
dc.deleteItem(c); |
5340 |
10 May 10 |
nicklas |
159 |
Set<ItemProxy> using = c.getUsingItems(); |
5340 |
10 May 10 |
nicklas |
160 |
if (using.size() > 0) |
5340 |
10 May 10 |
nicklas |
161 |
{ |
5340 |
10 May 10 |
nicklas |
162 |
throw new BaseException(c + " is used by " + using); |
5340 |
10 May 10 |
nicklas |
163 |
} |
239 |
23 Mar 05 |
nicklas |
164 |
dc.commit(); |
239 |
23 Mar 05 |
nicklas |
165 |
write("--Delete client OK"); |
239 |
23 Mar 05 |
nicklas |
166 |
} |
358 |
04 Apr 05 |
nicklas |
167 |
catch (Throwable ex) |
239 |
23 Mar 05 |
nicklas |
168 |
{ |
239 |
23 Mar 05 |
nicklas |
169 |
write("--Delete client FAILED"); |
239 |
23 Mar 05 |
nicklas |
170 |
ex.printStackTrace(); |
239 |
23 Mar 05 |
nicklas |
171 |
ok = false; |
239 |
23 Mar 05 |
nicklas |
172 |
} |
239 |
23 Mar 05 |
nicklas |
173 |
finally |
239 |
23 Mar 05 |
nicklas |
174 |
{ |
239 |
23 Mar 05 |
nicklas |
175 |
if (dc != null) dc.close(); |
239 |
23 Mar 05 |
nicklas |
176 |
} |
239 |
23 Mar 05 |
nicklas |
177 |
} |
239 |
23 Mar 05 |
nicklas |
178 |
|
239 |
23 Mar 05 |
nicklas |
179 |
static void write_header() |
239 |
23 Mar 05 |
nicklas |
180 |
{ |
239 |
23 Mar 05 |
nicklas |
181 |
if (!TestUtil.getSilent()) |
239 |
23 Mar 05 |
nicklas |
182 |
{ |
239 |
23 Mar 05 |
nicklas |
183 |
write(" \tID \tName \tDescription\tExternal ID"); |
239 |
23 Mar 05 |
nicklas |
184 |
write("-- \t-- \t--------- \t-----------\t-----------"); |
239 |
23 Mar 05 |
nicklas |
185 |
} |
239 |
23 Mar 05 |
nicklas |
186 |
} |
239 |
23 Mar 05 |
nicklas |
187 |
static void write_item(int i, Client c) |
239 |
23 Mar 05 |
nicklas |
188 |
throws BaseException |
239 |
23 Mar 05 |
nicklas |
189 |
{ |
239 |
23 Mar 05 |
nicklas |
190 |
if (!TestUtil.getSilent()) System.out.println(i+":\t"+c.getId()+"\t"+c.getName()+"\t"+ |
239 |
23 Mar 05 |
nicklas |
191 |
c.getDescription()+"\t"+c.getExternalId()); |
239 |
23 Mar 05 |
nicklas |
192 |
} |
239 |
23 Mar 05 |
nicklas |
193 |
static void write(String message) |
239 |
23 Mar 05 |
nicklas |
194 |
{ |
239 |
23 Mar 05 |
nicklas |
195 |
System.out.println(message); |
239 |
23 Mar 05 |
nicklas |
196 |
} |
239 |
23 Mar 05 |
nicklas |
197 |
|
239 |
23 Mar 05 |
nicklas |
198 |
static void test_login_logout(int id) |
239 |
23 Mar 05 |
nicklas |
199 |
{ |
239 |
23 Mar 05 |
nicklas |
200 |
if (id == 0) return; |
239 |
23 Mar 05 |
nicklas |
201 |
DbControl dc = null; |
239 |
23 Mar 05 |
nicklas |
202 |
try |
239 |
23 Mar 05 |
nicklas |
203 |
{ |
239 |
23 Mar 05 |
nicklas |
204 |
dc = TestUtil.getDbControl(); |
239 |
23 Mar 05 |
nicklas |
205 |
Client c = Client.getById(dc, id); |
239 |
23 Mar 05 |
nicklas |
206 |
SessionControl sc = Application.newSessionControl(c.getExternalId(), null, null); |
6424 |
25 Feb 14 |
nicklas |
207 |
LoginRequest loginRequest = new LoginRequest(TestUtil.getLogin(), TestUtil.getPassword()); |
6424 |
25 Feb 14 |
nicklas |
208 |
loginRequest.setComment("Running test program"); |
6424 |
25 Feb 14 |
nicklas |
209 |
sc.login(loginRequest); |
239 |
23 Mar 05 |
nicklas |
210 |
sc.logout(); |
239 |
23 Mar 05 |
nicklas |
211 |
write("--Login/logout OK"); |
239 |
23 Mar 05 |
nicklas |
212 |
} |
358 |
04 Apr 05 |
nicklas |
213 |
catch (Throwable ex) |
239 |
23 Mar 05 |
nicklas |
214 |
{ |
239 |
23 Mar 05 |
nicklas |
215 |
write("--Login/logout FAILED"); |
239 |
23 Mar 05 |
nicklas |
216 |
ex.printStackTrace(); |
239 |
23 Mar 05 |
nicklas |
217 |
ok = false; |
239 |
23 Mar 05 |
nicklas |
218 |
} |
239 |
23 Mar 05 |
nicklas |
219 |
finally |
239 |
23 Mar 05 |
nicklas |
220 |
{ |
239 |
23 Mar 05 |
nicklas |
221 |
if (dc != null) dc.close(); |
239 |
23 Mar 05 |
nicklas |
222 |
} |
239 |
23 Mar 05 |
nicklas |
223 |
} |
239 |
23 Mar 05 |
nicklas |
224 |
|
239 |
23 Mar 05 |
nicklas |
225 |
|
239 |
23 Mar 05 |
nicklas |
226 |
|
239 |
23 Mar 05 |
nicklas |
227 |
} |
239 |
23 Mar 05 |
nicklas |
228 |
|