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