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