1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package de.saly.javamail.mock2;
27
28 import java.util.Date;
29
30 import javax.activation.DataHandler;
31 import javax.mail.Address;
32 import javax.mail.Flags;
33 import javax.mail.Folder;
34 import javax.mail.IllegalWriteException;
35 import javax.mail.Message;
36 import javax.mail.MessagingException;
37 import javax.mail.Multipart;
38 import javax.mail.internet.MimeMessage;
39
40 public class MockMessage extends MimeMessage implements Comparable<MockMessage> {
41
42 public static interface FlagChangeListener {
43 void onFlagChange(MockMessage msg, Flags flags, boolean set);
44 }
45
46 private final FlagChangeListener flagChangeListener;
47 private Folder folder;
48 protected final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(this.getClass());
49 private final MailboxFolder mbf;
50
51 private final long mockid;
52
53 protected MockMessage(final Message source, final Folder folder) throws MessagingException {
54 this((MimeMessage) source, ((MockMessage) source).mockid, ((MockMessage) source).mbf, ((MockMessage) source).flagChangeListener);
55 this.folder = folder;
56 this.setMessageNumber(source.getMessageNumber());
57 }
58
59 protected MockMessage(final MimeMessage source, final long mockid, final MailboxFolder mbf, final FlagChangeListener flagChangeListener)
60 throws MessagingException {
61 super(source);
62 this.mockid = mockid;
63 this.flagChangeListener = flagChangeListener;
64 this.mbf = mbf;
65
66 }
67
68
69
70
71 @Override
72 public void addFrom(final Address[] addresses) throws MessagingException {
73
74 throw new IllegalWriteException("Mock messages are read-only");
75 }
76
77
78
79
80 @Override
81 public void addHeader(final String name, final String value) throws MessagingException {
82 throw new IllegalWriteException("Mock messages are read-only");
83 }
84
85
86
87
88 @Override
89 public void addHeaderLine(final String line) throws MessagingException {
90 throw new IllegalWriteException("Mock messages are read-only");
91 }
92
93
94
95
96 @Override
97 public void addRecipient(final javax.mail.Message.RecipientType type, final Address address) throws MessagingException {
98 throw new IllegalWriteException("Mock messages are read-only");
99 }
100
101
102
103
104 @Override
105 public void addRecipients(final javax.mail.Message.RecipientType type, final Address[] addresses) throws MessagingException {
106 throw new IllegalWriteException("Mock messages are read-only");
107 }
108
109
110
111
112 @Override
113 public void addRecipients(final javax.mail.Message.RecipientType type, final String addresses) throws MessagingException {
114 throw new IllegalWriteException("Mock messages are read-only");
115 }
116
117 @Override
118 public int compareTo(final MockMessage o) {
119
120 return new Long(this.getMockid()).compareTo(new Long(o.getMockid()));
121 }
122
123 @Override
124 public synchronized Folder getFolder() {
125 if (folder == null) {
126 throw new RuntimeException("wrong/unshaded mock message");
127 } else {
128 return folder;
129 }
130 }
131
132
133
134
135
136
137 public long getMockid() {
138 return mockid;
139 }
140
141
142
143
144 @Override
145 public void removeHeader(final String name) throws MessagingException {
146 throw new IllegalWriteException("Mock messages are read-only");
147 }
148
149
150
151
152 @Override
153 public void saveChanges() throws MessagingException {
154 throw new IllegalWriteException("Mock messages are read-only");
155 }
156
157
158
159
160 @Override
161 public void setContent(final Multipart mp) throws MessagingException {
162 throw new IllegalWriteException("Mock messages are read-only");
163 }
164
165
166
167
168 @Override
169 public void setContent(final Object o, final String type) throws MessagingException {
170 throw new IllegalWriteException("Mock messages are read-only");
171 }
172
173
174
175
176 @Override
177 public void setContentID(final String cid) throws MessagingException {
178 throw new IllegalWriteException("Mock messages are read-only");
179 }
180
181
182
183
184 @Override
185 public void setContentLanguage(final String[] languages) throws MessagingException {
186 throw new IllegalWriteException("Mock messages are read-only");
187 }
188
189
190
191
192 @Override
193 public void setContentMD5(final String md5) throws MessagingException {
194 throw new IllegalWriteException("Mock messages are read-only");
195 }
196
197 @Override
198 public void setDataHandler(final DataHandler content) throws MessagingException {
199 throw new IllegalWriteException("Mock messages are read-only");
200 }
201
202
203
204
205 @Override
206 public void setDescription(final String description) throws MessagingException {
207 throw new IllegalWriteException("Mock messages are read-only");
208 }
209
210
211
212
213 @Override
214 public void setDescription(final String description, final String charset) throws MessagingException {
215 throw new IllegalWriteException("Mock messages are read-only");
216 }
217
218
219
220
221 @Override
222 public void setDisposition(final String disposition) throws MessagingException {
223 throw new IllegalWriteException("Mock messages are read-only");
224 }
225
226 @Override
227 protected void setExpunged(final boolean expunged) {
228
229 super.setExpunged(expunged);
230 }
231
232
233
234
235 @Override
236 public void setFileName(final String filename) throws MessagingException {
237 throw new IllegalWriteException("Mock messages are read-only");
238 }
239
240
241
242
243 @Override
244 public synchronized void setFlags(final Flags flag, final boolean set) throws MessagingException {
245
246 super.setFlags(flag, set);
247
248 if (flagChangeListener != null) {
249 flagChangeListener.onFlagChange(this, flag, set);
250 }
251
252 }
253
254
255
256
257 @Override
258 public void setFrom() throws MessagingException {
259 throw new IllegalWriteException("Mock messages are read-only");
260 }
261
262
263
264
265 @Override
266 public void setFrom(final Address address) throws MessagingException {
267 throw new IllegalWriteException("Mock messages are read-only");
268 }
269
270
271
272
273 @Override
274 public void setFrom(final String address) throws MessagingException {
275 throw new IllegalWriteException("Mock messages are read-only");
276 }
277
278
279
280
281 @Override
282 public void setHeader(final String name, final String value) throws MessagingException {
283 throw new IllegalWriteException("Mock messages are read-only");
284 }
285
286 @Override
287 protected void setMessageNumber(final int msgnum) {
288
289 super.setMessageNumber(msgnum);
290 }
291
292
293
294
295 @Override
296 public void setRecipient(final javax.mail.Message.RecipientType type, final Address address) throws MessagingException {
297 throw new IllegalWriteException("Mock messages are read-only");
298 }
299
300
301
302
303 @Override
304 public void setRecipients(final javax.mail.Message.RecipientType type, final Address[] addresses) throws MessagingException {
305 throw new IllegalWriteException("Mock messages are read-only");
306 }
307
308
309
310
311 @Override
312 public void setRecipients(final javax.mail.Message.RecipientType type, final String addresses) throws MessagingException {
313 throw new IllegalWriteException("Mock messages are read-only");
314 }
315
316
317
318
319 @Override
320 public void setReplyTo(final Address[] addresses) throws MessagingException {
321 throw new IllegalWriteException("Mock messages are read-only");
322 }
323
324
325
326
327 @Override
328 public void setSender(final Address address) throws MessagingException {
329 throw new IllegalWriteException("Mock messages are read-only");
330 }
331
332
333
334
335 @Override
336 public void setSentDate(final Date d) throws MessagingException {
337 throw new IllegalWriteException("Mock messages are read-only");
338 }
339
340 void setSpecialHeader(final String name, final String value) throws MessagingException {
341 super.addHeader(name, value);
342 }
343
344
345
346
347 @Override
348 public void setSubject(final String subject) throws MessagingException {
349 throw new IllegalWriteException("Mock messages are read-only");
350 }
351
352
353
354
355 @Override
356 public void setSubject(final String subject, final String charset) throws MessagingException {
357 throw new IllegalWriteException("Mock messages are read-only");
358 }
359
360
361
362
363 @Override
364 public void setText(final String text) throws MessagingException {
365 throw new IllegalWriteException("Mock messages are read-only");
366 }
367
368
369
370
371 @Override
372 public void setText(final String text, final String charset) throws MessagingException {
373 throw new IllegalWriteException("Mock messages are read-only");
374 }
375
376
377
378
379 @Override
380 public void setText(final String text, final String charset, final String subtype) throws MessagingException {
381 throw new IllegalWriteException("Mock messages are read-only");
382 }
383
384 }