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