332 |
01 Apr 05 |
nicklas |
1 |
/* |
332 |
01 Apr 05 |
nicklas |
$Id$ |
332 |
01 Apr 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 |
332 |
01 Apr 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/ |
332 |
01 Apr 05 |
nicklas |
9 |
|
332 |
01 Apr 05 |
nicklas |
BASE is free software; you can redistribute it and/or |
332 |
01 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 |
332 |
01 Apr 05 |
nicklas |
of the License, or (at your option) any later version. |
332 |
01 Apr 05 |
nicklas |
14 |
|
332 |
01 Apr 05 |
nicklas |
BASE is distributed in the hope that it will be useful, |
332 |
01 Apr 05 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
332 |
01 Apr 05 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
332 |
01 Apr 05 |
nicklas |
GNU General Public License for more details. |
332 |
01 Apr 05 |
nicklas |
19 |
|
332 |
01 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/>. |
332 |
01 Apr 05 |
nicklas |
22 |
*/ |
5340 |
10 May 10 |
nicklas |
23 |
import java.util.Set; |
5340 |
10 May 10 |
nicklas |
24 |
|
332 |
01 Apr 05 |
nicklas |
25 |
import net.sf.basedb.core.*; |
332 |
01 Apr 05 |
nicklas |
26 |
|
332 |
01 Apr 05 |
nicklas |
27 |
public class TestUserClientSetting |
332 |
01 Apr 05 |
nicklas |
28 |
{ |
332 |
01 Apr 05 |
nicklas |
29 |
|
332 |
01 Apr 05 |
nicklas |
30 |
static boolean ok = true; |
332 |
01 Apr 05 |
nicklas |
31 |
|
332 |
01 Apr 05 |
nicklas |
32 |
public static void main(String[] args) |
332 |
01 Apr 05 |
nicklas |
33 |
{ |
332 |
01 Apr 05 |
nicklas |
34 |
TestUtil.checkArgs(args); |
332 |
01 Apr 05 |
nicklas |
35 |
TestUtil.begin(); |
332 |
01 Apr 05 |
nicklas |
36 |
test_all(); |
332 |
01 Apr 05 |
nicklas |
37 |
TestUtil.stop(); |
332 |
01 Apr 05 |
nicklas |
38 |
} |
332 |
01 Apr 05 |
nicklas |
39 |
|
342 |
01 Apr 05 |
nicklas |
40 |
static boolean test_all() |
332 |
01 Apr 05 |
nicklas |
41 |
{ |
332 |
01 Apr 05 |
nicklas |
42 |
write("++Testing user client setting"); |
332 |
01 Apr 05 |
nicklas |
43 |
write_header(); |
332 |
01 Apr 05 |
nicklas |
// Standard tests: create, load, list |
352 |
04 Apr 05 |
nicklas |
45 |
int userId = TestUser.test_create(false); |
352 |
04 Apr 05 |
nicklas |
46 |
int clientId = TestClient.test_create("net.sf.basedb.clients.testcreate", false); |
352 |
04 Apr 05 |
nicklas |
47 |
int id = test_create(userId, clientId, "fontsize", "9"); |
332 |
01 Apr 05 |
nicklas |
48 |
test_duplicate(id); |
332 |
01 Apr 05 |
nicklas |
49 |
test_load(id); |
354 |
04 Apr 05 |
nicklas |
50 |
test_list(userId, clientId, -1); |
332 |
01 Apr 05 |
nicklas |
51 |
|
332 |
01 Apr 05 |
nicklas |
// Extra tests: |
332 |
01 Apr 05 |
nicklas |
53 |
|
332 |
01 Apr 05 |
nicklas |
// Standard test: Delete |
1529 |
27 Oct 05 |
nicklas |
55 |
if (TestUtil.waitBeforeDelete()) TestUtil.waitForEnter(); |
332 |
01 Apr 05 |
nicklas |
56 |
test_delete(id); |
352 |
04 Apr 05 |
nicklas |
57 |
TestClient.test_delete(clientId); |
352 |
04 Apr 05 |
nicklas |
58 |
TestUser.test_delete(userId); |
332 |
01 Apr 05 |
nicklas |
59 |
write("++Testing user client setting "+(ok ? "OK" : "Failed")+"\n"); |
342 |
01 Apr 05 |
nicklas |
60 |
return ok; |
332 |
01 Apr 05 |
nicklas |
61 |
} |
332 |
01 Apr 05 |
nicklas |
62 |
|
352 |
04 Apr 05 |
nicklas |
63 |
static int test_create(int userId, int clientId, String name, String value) |
332 |
01 Apr 05 |
nicklas |
64 |
{ |
352 |
04 Apr 05 |
nicklas |
65 |
if (userId == 0 || clientId == 0) return 0; |
332 |
01 Apr 05 |
nicklas |
66 |
int id = 0; |
332 |
01 Apr 05 |
nicklas |
67 |
DbControl dc = null; |
332 |
01 Apr 05 |
nicklas |
68 |
try |
332 |
01 Apr 05 |
nicklas |
69 |
{ |
332 |
01 Apr 05 |
nicklas |
70 |
dc = TestUtil.getDbControl(); |
352 |
04 Apr 05 |
nicklas |
71 |
User user = User.getById(dc, userId); |
352 |
04 Apr 05 |
nicklas |
72 |
Client client = Client.getById(dc, clientId); |
352 |
04 Apr 05 |
nicklas |
73 |
UserClientSetting ucs = UserClientSetting.getNew(dc, user, client, name, value); |
332 |
01 Apr 05 |
nicklas |
74 |
dc.saveItem(ucs); |
332 |
01 Apr 05 |
nicklas |
75 |
dc.commit(); |
332 |
01 Apr 05 |
nicklas |
76 |
id = ucs.getId(); |
806 |
22 Jun 05 |
nicklas |
77 |
dc = TestUtil.getDbControl(); |
5060 |
19 Aug 09 |
nicklas |
78 |
dc.reattachItem(ucs, false); |
332 |
01 Apr 05 |
nicklas |
79 |
write_item(0, ucs); |
332 |
01 Apr 05 |
nicklas |
80 |
write("--Create user client setting OK"); |
332 |
01 Apr 05 |
nicklas |
81 |
} |
358 |
04 Apr 05 |
nicklas |
82 |
catch (Throwable ex) |
332 |
01 Apr 05 |
nicklas |
83 |
{ |
332 |
01 Apr 05 |
nicklas |
84 |
write("--Create user client setting FAILED"); |
332 |
01 Apr 05 |
nicklas |
85 |
ex.printStackTrace(); |
332 |
01 Apr 05 |
nicklas |
86 |
ok = false; |
332 |
01 Apr 05 |
nicklas |
87 |
} |
332 |
01 Apr 05 |
nicklas |
88 |
finally |
332 |
01 Apr 05 |
nicklas |
89 |
{ |
332 |
01 Apr 05 |
nicklas |
90 |
if (dc != null) dc.close(); |
332 |
01 Apr 05 |
nicklas |
91 |
} |
332 |
01 Apr 05 |
nicklas |
92 |
return id; |
332 |
01 Apr 05 |
nicklas |
93 |
} |
332 |
01 Apr 05 |
nicklas |
94 |
|
332 |
01 Apr 05 |
nicklas |
95 |
static void test_load(int id) |
332 |
01 Apr 05 |
nicklas |
96 |
{ |
332 |
01 Apr 05 |
nicklas |
97 |
if (id == 0) return; |
332 |
01 Apr 05 |
nicklas |
98 |
DbControl dc = null; |
332 |
01 Apr 05 |
nicklas |
99 |
try |
332 |
01 Apr 05 |
nicklas |
100 |
{ |
332 |
01 Apr 05 |
nicklas |
101 |
dc = TestUtil.getDbControl(); |
332 |
01 Apr 05 |
nicklas |
102 |
UserClientSetting ucs = UserClientSetting.getById(dc, id); |
332 |
01 Apr 05 |
nicklas |
103 |
write_item(0, ucs); |
332 |
01 Apr 05 |
nicklas |
104 |
write("--Load user client setting OK"); |
332 |
01 Apr 05 |
nicklas |
105 |
} |
358 |
04 Apr 05 |
nicklas |
106 |
catch (Throwable ex) |
332 |
01 Apr 05 |
nicklas |
107 |
{ |
332 |
01 Apr 05 |
nicklas |
108 |
write("--Load user client setting FAILED"); |
332 |
01 Apr 05 |
nicklas |
109 |
ex.printStackTrace(); |
332 |
01 Apr 05 |
nicklas |
110 |
ok = false; |
332 |
01 Apr 05 |
nicklas |
111 |
} |
332 |
01 Apr 05 |
nicklas |
112 |
finally |
332 |
01 Apr 05 |
nicklas |
113 |
{ |
332 |
01 Apr 05 |
nicklas |
114 |
if (dc != null) dc.close(); |
332 |
01 Apr 05 |
nicklas |
115 |
} |
332 |
01 Apr 05 |
nicklas |
116 |
} |
332 |
01 Apr 05 |
nicklas |
117 |
|
354 |
04 Apr 05 |
nicklas |
118 |
static void test_list(int userId, int clientId, int expectedResults) |
332 |
01 Apr 05 |
nicklas |
119 |
{ |
354 |
04 Apr 05 |
nicklas |
120 |
if (userId == 0 || clientId == 0) return; |
332 |
01 Apr 05 |
nicklas |
121 |
DbControl dc = null; |
332 |
01 Apr 05 |
nicklas |
122 |
try |
332 |
01 Apr 05 |
nicklas |
123 |
{ |
332 |
01 Apr 05 |
nicklas |
124 |
dc = TestUtil.getDbControl(); |
354 |
04 Apr 05 |
nicklas |
125 |
User user = User.getById(dc, userId); |
354 |
04 Apr 05 |
nicklas |
126 |
Client client = Client.getById(dc, clientId); |
1418 |
07 Oct 05 |
nicklas |
127 |
ItemResultList<UserClientSetting> l = UserClientSetting.getQuery(user, client).list(dc); |
332 |
01 Apr 05 |
nicklas |
128 |
for (int i = 0; i<l.size(); i++) |
332 |
01 Apr 05 |
nicklas |
129 |
{ |
332 |
01 Apr 05 |
nicklas |
130 |
write_item(i, l.get(i)); |
332 |
01 Apr 05 |
nicklas |
131 |
} |
332 |
01 Apr 05 |
nicklas |
132 |
if (expectedResults >= 0 && expectedResults != l.size()) |
332 |
01 Apr 05 |
nicklas |
133 |
{ |
332 |
01 Apr 05 |
nicklas |
134 |
throw new BaseException("Expected "+expectedResults+" results, not "+l.size()); |
332 |
01 Apr 05 |
nicklas |
135 |
} |
332 |
01 Apr 05 |
nicklas |
136 |
write("--List user client settings OK ("+l.size()+")"); |
332 |
01 Apr 05 |
nicklas |
137 |
} |
358 |
04 Apr 05 |
nicklas |
138 |
catch (Throwable ex) |
332 |
01 Apr 05 |
nicklas |
139 |
{ |
332 |
01 Apr 05 |
nicklas |
140 |
write("--List user client settings FAILED"); |
332 |
01 Apr 05 |
nicklas |
141 |
ex.printStackTrace(); |
332 |
01 Apr 05 |
nicklas |
142 |
ok = false; |
332 |
01 Apr 05 |
nicklas |
143 |
} |
332 |
01 Apr 05 |
nicklas |
144 |
finally |
332 |
01 Apr 05 |
nicklas |
145 |
{ |
332 |
01 Apr 05 |
nicklas |
146 |
if (dc != null) dc.close(); |
332 |
01 Apr 05 |
nicklas |
147 |
} |
332 |
01 Apr 05 |
nicklas |
148 |
} |
332 |
01 Apr 05 |
nicklas |
149 |
|
332 |
01 Apr 05 |
nicklas |
150 |
static void test_delete(int id) |
332 |
01 Apr 05 |
nicklas |
151 |
{ |
332 |
01 Apr 05 |
nicklas |
152 |
if (id == 0) return; |
332 |
01 Apr 05 |
nicklas |
153 |
DbControl dc = null; |
332 |
01 Apr 05 |
nicklas |
154 |
try |
332 |
01 Apr 05 |
nicklas |
155 |
{ |
332 |
01 Apr 05 |
nicklas |
156 |
dc = TestUtil.getDbControl(); |
332 |
01 Apr 05 |
nicklas |
157 |
UserClientSetting ucs = UserClientSetting.getById(dc, id); |
332 |
01 Apr 05 |
nicklas |
158 |
dc.deleteItem(ucs); |
5340 |
10 May 10 |
nicklas |
159 |
Set<ItemProxy> using = ucs.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(ucs + " is used by " + using); |
5340 |
10 May 10 |
nicklas |
163 |
} |
332 |
01 Apr 05 |
nicklas |
164 |
dc.commit(); |
332 |
01 Apr 05 |
nicklas |
165 |
write("--Delete user client setting OK"); |
332 |
01 Apr 05 |
nicklas |
166 |
} |
358 |
04 Apr 05 |
nicklas |
167 |
catch (Throwable ex) |
332 |
01 Apr 05 |
nicklas |
168 |
{ |
332 |
01 Apr 05 |
nicklas |
169 |
write("--Delete user client setting FAILED"); |
332 |
01 Apr 05 |
nicklas |
170 |
ex.printStackTrace(); |
332 |
01 Apr 05 |
nicklas |
171 |
ok = false; |
332 |
01 Apr 05 |
nicklas |
172 |
} |
332 |
01 Apr 05 |
nicklas |
173 |
finally |
332 |
01 Apr 05 |
nicklas |
174 |
{ |
332 |
01 Apr 05 |
nicklas |
175 |
if (dc != null) dc.close(); |
332 |
01 Apr 05 |
nicklas |
176 |
} |
332 |
01 Apr 05 |
nicklas |
177 |
} |
332 |
01 Apr 05 |
nicklas |
178 |
|
332 |
01 Apr 05 |
nicklas |
179 |
static void write_header() |
332 |
01 Apr 05 |
nicklas |
180 |
{ |
332 |
01 Apr 05 |
nicklas |
181 |
if (!TestUtil.getSilent()) |
332 |
01 Apr 05 |
nicklas |
182 |
{ |
332 |
01 Apr 05 |
nicklas |
183 |
write(" \tID \tName\tValue\tUser\tClient"); |
332 |
01 Apr 05 |
nicklas |
184 |
write("-- \t-- \t----\t-----\t----\t------"); |
332 |
01 Apr 05 |
nicklas |
185 |
} |
332 |
01 Apr 05 |
nicklas |
186 |
} |
332 |
01 Apr 05 |
nicklas |
187 |
static void write_item(int i, UserClientSetting ucs) |
332 |
01 Apr 05 |
nicklas |
188 |
throws BaseException |
332 |
01 Apr 05 |
nicklas |
189 |
{ |
332 |
01 Apr 05 |
nicklas |
190 |
if (!TestUtil.getSilent()) System.out.println(i+":\t"+ucs.getId()+"\t"+ucs.getName()+"\t"+ |
332 |
01 Apr 05 |
nicklas |
191 |
ucs.getValue()+"\t"+ucs.getUser()+"\t"+ucs.getClient()); |
332 |
01 Apr 05 |
nicklas |
192 |
} |
332 |
01 Apr 05 |
nicklas |
193 |
static void write(String message) |
332 |
01 Apr 05 |
nicklas |
194 |
{ |
332 |
01 Apr 05 |
nicklas |
195 |
System.out.println(message); |
332 |
01 Apr 05 |
nicklas |
196 |
} |
332 |
01 Apr 05 |
nicklas |
197 |
|
332 |
01 Apr 05 |
nicklas |
198 |
static void test_duplicate(int id) |
332 |
01 Apr 05 |
nicklas |
199 |
{ |
332 |
01 Apr 05 |
nicklas |
200 |
if (id == 0) return; |
332 |
01 Apr 05 |
nicklas |
201 |
DbControl dc = null; |
332 |
01 Apr 05 |
nicklas |
202 |
try |
332 |
01 Apr 05 |
nicklas |
203 |
{ |
332 |
01 Apr 05 |
nicklas |
204 |
dc = TestUtil.getDbControl(); |
332 |
01 Apr 05 |
nicklas |
205 |
UserClientSetting ucs = UserClientSetting.getById(dc, id); |
352 |
04 Apr 05 |
nicklas |
206 |
UserClientSetting duplicate = UserClientSetting.getNew(dc, ucs.getUser(), ucs.getClient(), ucs.getName(), ucs.getValue()); |
332 |
01 Apr 05 |
nicklas |
207 |
dc.saveItem(duplicate); |
332 |
01 Apr 05 |
nicklas |
208 |
dc.commit(); |
332 |
01 Apr 05 |
nicklas |
209 |
dc.deleteItem(duplicate); |
332 |
01 Apr 05 |
nicklas |
210 |
dc.commit(); |
1500 |
21 Oct 05 |
nicklas |
211 |
throw new BaseException("Expected DatabaseException"); |
332 |
01 Apr 05 |
nicklas |
212 |
} |
1500 |
21 Oct 05 |
nicklas |
213 |
catch (DatabaseException ex) |
332 |
01 Apr 05 |
nicklas |
214 |
{ |
332 |
01 Apr 05 |
nicklas |
215 |
write("--Duplicate user client setting OK"); |
332 |
01 Apr 05 |
nicklas |
216 |
} |
358 |
04 Apr 05 |
nicklas |
217 |
catch (Throwable ex) |
332 |
01 Apr 05 |
nicklas |
218 |
{ |
332 |
01 Apr 05 |
nicklas |
219 |
write("--Duplicate user client setting FAILED"); |
332 |
01 Apr 05 |
nicklas |
220 |
ex.printStackTrace(); |
332 |
01 Apr 05 |
nicklas |
221 |
ok = false; |
332 |
01 Apr 05 |
nicklas |
222 |
} |
332 |
01 Apr 05 |
nicklas |
223 |
finally |
332 |
01 Apr 05 |
nicklas |
224 |
{ |
332 |
01 Apr 05 |
nicklas |
225 |
if (dc != null) dc.close(); |
332 |
01 Apr 05 |
nicklas |
226 |
} |
332 |
01 Apr 05 |
nicklas |
227 |
} |
332 |
01 Apr 05 |
nicklas |
228 |
|
332 |
01 Apr 05 |
nicklas |
229 |
} |
332 |
01 Apr 05 |
nicklas |
230 |
|
332 |
01 Apr 05 |
nicklas |
231 |
|