Subversion Repositories javautils

Rev

Rev 3 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 daniel-mar 1
package de.viathinksoft.utils.mail.sender;
2
 
3
import static org.junit.Assert.fail;
4
 
5
import java.net.ConnectException;
6
 
7
import javax.mail.AuthenticationFailedException;
8
import javax.mail.MessagingException;
9
import javax.mail.internet.AddressException;
10
 
11
import org.junit.Test;
12
 
13
import de.viathinksoft.utils.mail.EMailAddress;
14
import de.viathinksoft.utils.mail.sender.PlainTextMailSender;
9 daniel-mar 15
import eMailTests.TestConfiguration;
3 daniel-mar 16
 
17
public class RawMailSenderPlainTextTest {
18
        @Test
19
        public void testPostMailNullPointer() throws MessagingException,
9 daniel-mar 20
                        AuthentificateDataIncompleteException {
3 daniel-mar 21
                PlainTextMailSender mailsender = new PlainTextMailSender();
22
 
23
                try {
24
                        mailsender.sendMail();
25
                        fail();
26
                } catch (AddressException e) {
27
                } catch (MessagingException e) {
28
                        // if ((e.getCause() != null) && (e.getCause().getNextException() ==
29
                        // ConnectException)) {
30
                        if ((e.getCause() != null)
31
                                        && (e.getCause().getClass() == ConnectException.class)) {
32
                                // Wir erwarten eine Exception, da wir davon ausgehen, dass auf
33
                                // Localhost kein SMTP-Server läuft!
34
                        } else {
35
                                e.printStackTrace();
36
                                fail();
37
                        }
38
                }
39
 
40
                // -----------------------------------------------------
41
 
42
                try {
43
                        mailsender.setRecipient((EMailAddress)null);
44
                        fail();
9 daniel-mar 45
                } catch (NullPointerException e1) {
3 daniel-mar 46
                }
47
 
48
                try {
49
                        mailsender.sendMail();
50
                        fail();
51
                } catch (AddressException e) {
52
                } catch (MessagingException e) {
53
                        // if ((e.getCause() != null) && (e.getCause().getNextException() ==
54
                        // ConnectException)) {
55
                        if ((e.getCause() != null)
56
                                        && (e.getCause().getClass() == ConnectException.class)) {
57
                                // Wir erwarten eine Exception, da wir davon ausgehen, dass auf
58
                                // Localhost kein SMTP-Server läuft!
59
                        } else {
60
                                e.printStackTrace();
61
                                fail();
62
                        }
63
                }
64
 
65
                // -----------------------------------------------------
66
 
67
                try {
68
                        mailsender.setRecipient((String)null);
69
                        fail();
9 daniel-mar 70
                } catch (NullPointerException e1) {
3 daniel-mar 71
                }
72
 
73
                try {
74
                        mailsender.sendMail();
75
                        fail();
76
                } catch (AddressException e) {
77
                } catch (MessagingException e) {
78
                        // if ((e.getCause() != null) && (e.getCause().getNextException() ==
79
                        // ConnectException)) {
80
                        if ((e.getCause() != null)
81
                                        && (e.getCause().getClass() == ConnectException.class)) {
82
                                // Wir erwarten eine Exception, da wir davon ausgehen, dass auf
83
                                // Localhost kein SMTP-Server läuft!
84
                        } else {
85
                                e.printStackTrace();
86
                                fail();
87
                        }
88
                }
89
 
90
                // -----------------------------------------------------
91
 
92
                mailsender.setRecipient(TestConfiguration.getSpamAddress());
93
 
94
                mailsender.setSmtpHost(null);
95
                mailsender.setSmtpUsername(null);
96
                mailsender.setSmtpPassword(null);
97
                mailsender.setSmtpPort(-1);
98
 
99
                try {
100
                        mailsender.sendMail();
101
                        fail();
102
                } catch (MessagingException e) {
103
                        // if ((e.getCause() != null) && (e.getCause().getNextException() ==
104
                        // ConnectException)) {
105
                        if ((e.getCause() != null)
106
                                        && (e.getCause().getClass() == ConnectException.class)) {
107
                                // Wir erwarten eine Exception, da wir davon ausgehen, dass auf
108
                                // Localhost kein SMTP-Server läuft!
109
                        } else {
110
                                e.printStackTrace();
111
                                fail();
112
                        }
113
                }
114
 
115
                // -----------------------------------------------------
116
 
117
                mailsender.setSmtpHost(TestConfiguration.getSmtpHost());
118
                mailsender.setSmtpUsername(null);
119
                mailsender.setSmtpPassword(null);
120
                mailsender.setSmtpPort(TestConfiguration.getSmtpPort());
121
 
122
                mailsender.setSmtpAuth(true);
123
                // mailsender.setSmtpAuthUser(null);
124
                // mailsender.setSmtpAuthPass(null);
125
 
126
                try {
127
                        mailsender.sendMail();
128
                        fail();
129
                } catch (AuthentificateDataIncompleteException e) {
130
                }
131
 
132
                // -----------------------------------------------------
133
 
134
                mailsender.setSmtpAuth(false);
135
 
136
                try {
137
                        mailsender.setMailFrom((EMailAddress)null);
138
                        fail();
9 daniel-mar 139
                } catch (NullPointerException e1) {
3 daniel-mar 140
                }
141
                mailsender.setSubject(null);
142
                mailsender.setMessage(null);
143
 
144
                try {
145
                        mailsender.sendMail();
146
                } catch (AuthenticationFailedException e) {
147
                        // Diese Fehlermeldung KANN vom SMTP geworfen werden. MUSS aber
148
                        // nicht.
149
                }
150
 
151
                // -----------------------------------------------------
152
 
153
                try {
154
                        mailsender.setMailFrom((String)null);
155
                        fail();
9 daniel-mar 156
                } catch (NullPointerException e1) {
3 daniel-mar 157
                }
158
 
159
                try {
160
                        mailsender.sendMail();
161
                } catch (AuthenticationFailedException e) {
162
                        // Diese Fehlermeldung KANN vom SMTP geworfen werden. MUSS aber
163
                        // nicht.
164
                }
165
 
166
                // -----------------------------------------------------
167
 
168
                mailsender.setSmtpAuth(true);
169
                mailsender.setSmtpUsername(TestConfiguration.getSmtpUsername());
170
                mailsender.setSmtpPassword(TestConfiguration.getSmtpPassword());
171
 
172
                mailsender.sendMail();
173
        }
174
 
175
        @Test
176
        public void testPostMailBlank()
9 daniel-mar 177
                        throws AuthentificateDataIncompleteException {
3 daniel-mar 178
                PlainTextMailSender mailsender = new PlainTextMailSender();
179
 
180
                mailsender.setRecipient(TestConfiguration.getSpamAddress());
181
                mailsender.setSubject("JUnit-Test 1");
182
                mailsender.setMessage("JUnit-Test 1 von Compuglobal");
183
 
184
                try {
185
                        mailsender.sendMail();
186
                        fail();
187
                } catch (MessagingException e) {
188
                        // if ((e.getCause() != null) && (e.getCause().getNextException() ==
189
                        // ConnectException)) {
190
                        if ((e.getCause() != null)
191
                                        && (e.getCause().getClass() == ConnectException.class)) {
192
                                // Wir erwarten eine Exception, da wir davon ausgehen, dass auf
193
                                // Localhost kein SMTP-Server läuft!
194
                        } else {
195
                                e.printStackTrace();
196
                                fail();
197
                        }
198
                }
199
        }
200
 
201
        @Test
202
        public void testPostMailWithData() throws MessagingException,
9 daniel-mar 203
                        AuthentificateDataIncompleteException
3 daniel-mar 204
                        {
205
                PlainTextMailSender mailsender = new PlainTextMailSender();
206
 
207
                mailsender.setSmtpHost(TestConfiguration.getSmtpHost());
208
                mailsender.setSmtpPort(TestConfiguration.getSmtpPort());
209
 
210
                mailsender.setRecipient(TestConfiguration.getSpamAddress());
211
                mailsender.setSubject("JUnit-Test 2");
212
                mailsender.setMessage("JUnit-Test 2 von Compuglobal");
213
 
214
                try {
215
                        mailsender.sendMail();
216
                } catch (AuthenticationFailedException e) {
217
                        // Diese Fehlermeldung KANN vom SMTP geworfen werden. MUSS aber
218
                        // nicht.
219
                }
220
        }
221
 
222
        @Test
223
        public void testPostMailWithDataAndOrigin() throws MessagingException,
9 daniel-mar 224
                        AuthentificateDataIncompleteException {
3 daniel-mar 225
                PlainTextMailSender mailsender = new PlainTextMailSender();
226
 
227
                mailsender.setSmtpHost(TestConfiguration.getSmtpHost());
228
                mailsender.setSmtpPort(TestConfiguration.getSmtpPort());
229
 
230
                mailsender.setMailFrom(TestConfiguration.getMailFrom());
231
 
232
                mailsender.setRecipient(TestConfiguration.getSpamAddress());
233
                mailsender.setSubject("JUnit-Test 3");
234
                mailsender.setMessage("JUnit-Test 3 von Compuglobal");
235
 
236
                try {
237
                        mailsender.sendMail();
238
                } catch (AuthenticationFailedException e) {
239
                        // Diese Fehlermeldung KANN vom SMTP geworfen werden. MUSS aber
240
                        // nicht.
241
                }
242
        }
243
 
244
        @Test
245
        public void testPostMailWithDataAndOriginAndSmtpAuthIncompleteUserAndPwd()
9 daniel-mar 246
                        throws MessagingException {
3 daniel-mar 247
                PlainTextMailSender mailsender = new PlainTextMailSender();
248
 
249
                mailsender.setSmtpHost(TestConfiguration.getSmtpHost());
250
                // mailsender.setSmtpUsername(TestConfiguration.getSmtpUsername());
251
                // mailsender.setSmtpPassword(TestConfiguration.getSmtpPassword());
252
                mailsender.setSmtpPort(TestConfiguration.getSmtpPort());
253
 
254
                mailsender.setMailFrom(TestConfiguration.getMailFrom());
255
 
256
                mailsender.setSmtpAuth(true);
257
 
258
                mailsender.setRecipient(TestConfiguration.getSpamAddress());
259
                mailsender.setSubject("JUnit-Test 4a");
260
                mailsender.setMessage("JUnit-Test 4a von Compuglobal");
261
 
262
                try {
263
                        mailsender.sendMail();
264
 
265
                        fail();
266
                } catch (AuthentificateDataIncompleteException e) {
267
                }
268
        }
269
 
270
        @Test
271
        public void testPostMailWithDataAndOriginAndSmtpAuthIncompleteUser()
9 daniel-mar 272
                        throws MessagingException {
3 daniel-mar 273
                PlainTextMailSender mailsender = new PlainTextMailSender();
274
 
275
                mailsender.setSmtpHost(TestConfiguration.getSmtpHost());
276
                // mailsender.setSmtpUsername(TestConfiguration.getSmtpUsername());
277
                mailsender.setSmtpPassword(TestConfiguration.getSmtpPassword());
278
                mailsender.setSmtpPort(TestConfiguration.getSmtpPort());
279
 
280
                mailsender.setMailFrom(TestConfiguration.getMailFrom());
281
 
282
                mailsender.setSmtpAuth(true);
283
 
284
                mailsender.setRecipient(TestConfiguration.getSpamAddress());
285
                mailsender.setSubject("JUnit-Test 4b");
286
                mailsender.setMessage("JUnit-Test 4b von Compuglobal");
287
 
288
                try {
289
                        mailsender.sendMail();
290
 
291
                        fail();
292
                } catch (AuthentificateDataIncompleteException e) {
293
                }
294
        }
295
 
296
        @Test
297
        public void testPostMailWithDataAndOriginAndSmtpAuthIncompletePwd()
9 daniel-mar 298
                        throws MessagingException {
3 daniel-mar 299
                PlainTextMailSender mailsender = new PlainTextMailSender();
300
 
301
                mailsender.setSmtpHost(TestConfiguration.getSmtpHost());
302
                mailsender.setSmtpUsername(TestConfiguration.getSmtpUsername());
303
                // mailsender.setSmtpPassword(TestConfiguration.getSmtpPassword());
304
                mailsender.setSmtpPort(TestConfiguration.getSmtpPort());
305
 
306
                mailsender.setMailFrom(TestConfiguration.getMailFrom());
307
 
308
                mailsender.setSmtpAuth(true);
309
 
310
                mailsender.setRecipient(TestConfiguration.getSpamAddress());
311
                mailsender.setSubject("JUnit-Test 4c");
312
                mailsender.setMessage("JUnit-Test 4c von Compuglobal");
313
 
314
                try {
315
                        mailsender.sendMail();
316
 
317
                        fail();
318
                } catch (AuthentificateDataIncompleteException e) {
319
                }
320
        }
321
 
322
        @Test
323
        public void testPostMailWithDataAndOriginAndSmtpAuthComplete()
9 daniel-mar 324
                        throws MessagingException, AuthentificateDataIncompleteException {
3 daniel-mar 325
                PlainTextMailSender mailsender = new PlainTextMailSender();
326
 
327
                mailsender.setSmtpHost(TestConfiguration.getSmtpHost());
328
                mailsender.setSmtpUsername(TestConfiguration.getSmtpUsername());
329
                mailsender.setSmtpPassword(TestConfiguration.getSmtpPassword());
330
                mailsender.setSmtpPort(TestConfiguration.getSmtpPort());
331
 
332
                mailsender.setMailFrom(TestConfiguration.getMailFrom());
333
 
334
                mailsender.setSmtpAuth(true);
335
 
336
                mailsender.setRecipient(TestConfiguration.getSpamAddress());
337
                mailsender.setSubject("JUnit-Test 4d");
338
                mailsender.setMessage("JUnit-Test 4d von Compuglobal");
339
 
340
                mailsender.sendMail();
341
        }
342
}