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