1 | /*
|
---|
2 | * Copyright 2003-2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 |
|
---|
18 | package org.apache.tools.mail;
|
---|
19 |
|
---|
20 | import java.io.BufferedReader;
|
---|
21 | import java.io.BufferedWriter;
|
---|
22 | import java.io.ByteArrayOutputStream;
|
---|
23 | import java.io.IOException;
|
---|
24 | import java.io.InputStreamReader;
|
---|
25 | import java.io.OutputStreamWriter;
|
---|
26 | import java.io.PrintStream;
|
---|
27 | import java.net.InetAddress;
|
---|
28 | import java.net.Socket;
|
---|
29 | import java.net.ServerSocket;
|
---|
30 | import java.util.Enumeration;
|
---|
31 | import java.util.Vector;
|
---|
32 |
|
---|
33 | import org.apache.tools.mail.MailMessage;
|
---|
34 |
|
---|
35 | import junit.framework.TestCase;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * JUnit 3 testcases for org.apache.tools.mail.MailMessage.
|
---|
39 | *
|
---|
40 | * @since Ant 1.6
|
---|
41 | */
|
---|
42 | public class MailMessageTest extends TestCase {
|
---|
43 |
|
---|
44 | // 27224 = magic (a random port which is unlikely to be in use)
|
---|
45 | private static int TEST_PORT = 27224;
|
---|
46 |
|
---|
47 | private String local = null;
|
---|
48 |
|
---|
49 | public MailMessageTest(String name) {
|
---|
50 | super(name);
|
---|
51 | }
|
---|
52 |
|
---|
53 | public void setUp() {
|
---|
54 | try {
|
---|
55 | local = InetAddress.getLocalHost().getHostName();
|
---|
56 | } catch (java.net.UnknownHostException uhe) {
|
---|
57 | // ignore
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Test an example that is similar to the one given in the API
|
---|
63 | * If this testcase takes >90s to complete, it is very likely that
|
---|
64 | * the two threads are blocked waiting for each other and Thread.join()
|
---|
65 | * timed out.
|
---|
66 | */
|
---|
67 | public void testAPIExample() {
|
---|
68 |
|
---|
69 | ServerThread testMailServer = new ServerThread();
|
---|
70 | Thread server = new Thread(testMailServer);
|
---|
71 | server.start();
|
---|
72 |
|
---|
73 | ClientThread testMailClient = new ClientThread();
|
---|
74 |
|
---|
75 | testMailClient.from("Mail Message <[email protected]>");
|
---|
76 | testMailClient.to("[email protected]");
|
---|
77 | testMailClient.cc("[email protected]");
|
---|
78 | testMailClient.cc("[email protected]");
|
---|
79 | testMailClient.bcc("[email protected]");
|
---|
80 | testMailClient.setSubject("Test subject");
|
---|
81 | testMailClient.setMessage( "test line 1\n" +
|
---|
82 | "test line 2" );
|
---|
83 |
|
---|
84 | Thread client = new Thread(testMailClient);
|
---|
85 | client.start();
|
---|
86 |
|
---|
87 | try {
|
---|
88 | server.join(60 * 1000); // 60s
|
---|
89 | client.join(30 * 1000); // a further 30s
|
---|
90 | } catch (InterruptedException ie ) {
|
---|
91 | fail( "InterruptedException: " + ie );
|
---|
92 | }
|
---|
93 |
|
---|
94 | String result = testMailServer.getResult();
|
---|
95 | String expectedResult = "220 test SMTP EmailTaskTest\r\n" +
|
---|
96 | "HELO " + local + "\r\n" +
|
---|
97 | "250 " + local + " Hello " + local + " [127.0.0.1], pleased to meet you\r\n" +
|
---|
98 | "MAIL FROM: <[email protected]>\r\n" +
|
---|
99 | "250\r\n" +
|
---|
100 | "RCPT TO: <[email protected]>\r\n" +
|
---|
101 | "250\r\n" +
|
---|
102 | "RCPT TO: <[email protected]>\r\n" +
|
---|
103 | "250\r\n" +
|
---|
104 | "RCPT TO: <[email protected]>\r\n" +
|
---|
105 | "250\r\n" +
|
---|
106 | "RCPT TO: <[email protected]>\r\n" +
|
---|
107 | "250\r\n" +
|
---|
108 | "DATA\r\n" +
|
---|
109 | "354\r\n" +
|
---|
110 | "Subject: Test subject\r\n" +
|
---|
111 | "From: Mail Message <[email protected]>\r\n" +
|
---|
112 | "To: [email protected]\r\n" +
|
---|
113 | "Cc: [email protected], [email protected]\r\n" +
|
---|
114 | "X-Mailer: org.apache.tools.mail.MailMessage (ant.apache.org)\r\n" +
|
---|
115 | "\r\n" +
|
---|
116 | "test line 1\r\n" +
|
---|
117 | "test line 2\r\n" +
|
---|
118 | "\r\n" +
|
---|
119 | ".\r\n" +
|
---|
120 | "250\r\n" +
|
---|
121 | "QUIT\r\n" +
|
---|
122 | "221\r\n";
|
---|
123 | for (int icounter = 0; icounter<expectedResult.length(); icounter++) {
|
---|
124 | if (icounter < result.length()) {
|
---|
125 | if (expectedResult.charAt(icounter) != result.charAt(icounter)) {
|
---|
126 | System.out.println("posit " + icounter + " expected "
|
---|
127 | + expectedResult.charAt(icounter)
|
---|
128 | + " result " + result.charAt(icounter));
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|
132 | if (expectedResult.length()>result.length()) {
|
---|
133 | System.out.println("excedent of expected result "
|
---|
134 | + expectedResult.substring(result.length()));
|
---|
135 | }
|
---|
136 | if (expectedResult.length()<result.length()) {
|
---|
137 | System.out.println("excedent of result "
|
---|
138 | + result.substring(expectedResult.length()));
|
---|
139 | }
|
---|
140 | assertEquals(expectedResult.length(), result.length());
|
---|
141 | assertEquals(expectedResult, result); // order of headers cannot be guaranteed
|
---|
142 | if (testMailClient.isFailed()) {
|
---|
143 | fail(testMailClient.getFailMessage());
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Test a MailMessage with no cc or bcc lines
|
---|
149 | */
|
---|
150 | public void testToOnly() {
|
---|
151 | ServerThread testMailServer = new ServerThread();
|
---|
152 | Thread server = new Thread(testMailServer);
|
---|
153 | server.start();
|
---|
154 |
|
---|
155 | ClientThread testMailClient = new ClientThread();
|
---|
156 |
|
---|
157 | testMailClient.from("Mail Message <[email protected]>");
|
---|
158 | testMailClient.to("[email protected]");
|
---|
159 | testMailClient.setSubject("Test subject");
|
---|
160 | testMailClient.setMessage( "test line 1\n" +
|
---|
161 | "test line 2" );
|
---|
162 |
|
---|
163 | Thread client = new Thread(testMailClient);
|
---|
164 | client.start();
|
---|
165 |
|
---|
166 | try {
|
---|
167 | server.join(60 * 1000); // 60s
|
---|
168 | client.join(30 * 1000); // a further 30s
|
---|
169 | } catch (InterruptedException ie ) {
|
---|
170 | fail("InterruptedException: " + ie);
|
---|
171 | }
|
---|
172 |
|
---|
173 | String result = testMailServer.getResult();
|
---|
174 | String expectedResult = "220 test SMTP EmailTaskTest\r\n" +
|
---|
175 | "HELO " + local + "\r\n" +
|
---|
176 | "250 " + local + " Hello " + local + " [127.0.0.1], pleased to meet you\r\n" +
|
---|
177 | "MAIL FROM: <[email protected]>\r\n" +
|
---|
178 | "250\r\n" +
|
---|
179 | "RCPT TO: <[email protected]>\r\n" +
|
---|
180 | "250\r\n" +
|
---|
181 | "DATA\r\n" +
|
---|
182 | "354\r\n" +
|
---|
183 | "Subject: Test subject\r\n" +
|
---|
184 | "From: Mail Message <[email protected]>\r\n" +
|
---|
185 | "To: [email protected]\r\n" +
|
---|
186 | "X-Mailer: org.apache.tools.mail.MailMessage (ant.apache.org)\r\n" +
|
---|
187 | "\r\n" +
|
---|
188 | "test line 1\r\n" +
|
---|
189 | "test line 2\r\n" +
|
---|
190 | "\r\n" +
|
---|
191 | ".\r\n" +
|
---|
192 | "250\r\n" +
|
---|
193 | "QUIT\r\n" +
|
---|
194 | "221\r\n";
|
---|
195 | assertEquals(expectedResult.length(), result.length());
|
---|
196 | assertEquals(expectedResult, result); // order of headers cannot be guaranteed
|
---|
197 | if (testMailClient.isFailed()) {
|
---|
198 | fail(testMailClient.getFailMessage());
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 |
|
---|
203 | /**
|
---|
204 | * Test a MailMessage with no to or bcc lines
|
---|
205 | */
|
---|
206 | public void testCcOnly() {
|
---|
207 | ServerThread testMailServer = new ServerThread();
|
---|
208 | Thread server = new Thread(testMailServer);
|
---|
209 | server.start();
|
---|
210 |
|
---|
211 | ClientThread testMailClient = new ClientThread();
|
---|
212 |
|
---|
213 | testMailClient.from("Mail Message <[email protected]>");
|
---|
214 | testMailClient.cc("[email protected]");
|
---|
215 | testMailClient.setSubject("Test subject");
|
---|
216 | testMailClient.setMessage( "test line 1\n" +
|
---|
217 | "test line 2" );
|
---|
218 |
|
---|
219 | Thread client = new Thread(testMailClient);
|
---|
220 | client.start();
|
---|
221 |
|
---|
222 | try {
|
---|
223 | server.join(60 * 1000); // 60s
|
---|
224 | client.join(30 * 1000); // a further 30s
|
---|
225 | } catch (InterruptedException ie ) {
|
---|
226 | fail( "InterruptedException: " + ie );
|
---|
227 | }
|
---|
228 |
|
---|
229 | String result = testMailServer.getResult();
|
---|
230 | String expectedResult = "220 test SMTP EmailTaskTest\r\n" +
|
---|
231 | "HELO " + local + "\r\n" +
|
---|
232 | "250 " + local + " Hello " + local + " [127.0.0.1], pleased to meet you\r\n" +
|
---|
233 | "MAIL FROM: <[email protected]>\r\n" +
|
---|
234 | "250\r\n" +
|
---|
235 | "RCPT TO: <[email protected]>\r\n" +
|
---|
236 | "250\r\n" +
|
---|
237 | "DATA\r\n" +
|
---|
238 | "354\r\n" +
|
---|
239 | "Subject: Test subject\r\n" +
|
---|
240 | "From: Mail Message <[email protected]>\r\n" +
|
---|
241 | "Cc: [email protected]\r\n" +
|
---|
242 | "X-Mailer: org.apache.tools.mail.MailMessage (ant.apache.org)\r\n" +
|
---|
243 | "\r\n" +
|
---|
244 | "test line 1\r\n" +
|
---|
245 | "test line 2\r\n" +
|
---|
246 | "\r\n" +
|
---|
247 | ".\r\n" +
|
---|
248 | "250\r\n" +
|
---|
249 | "QUIT\r\n" +
|
---|
250 | "221\r\n";
|
---|
251 | assertEquals(expectedResult.length(), result.length());
|
---|
252 | assertEquals(expectedResult, result);
|
---|
253 | if (testMailClient.isFailed()) {
|
---|
254 | fail(testMailClient.getFailMessage());
|
---|
255 | }
|
---|
256 | }
|
---|
257 |
|
---|
258 |
|
---|
259 | /**
|
---|
260 | * Test a MailMessage with no to or cc lines
|
---|
261 | */
|
---|
262 | public void testBccOnly() {
|
---|
263 | ServerThread testMailServer = new ServerThread();
|
---|
264 | Thread server = new Thread(testMailServer);
|
---|
265 | server.start();
|
---|
266 |
|
---|
267 | ClientThread testMailClient = new ClientThread();
|
---|
268 |
|
---|
269 | testMailClient.from("Mail Message <[email protected]>");
|
---|
270 | testMailClient.bcc("[email protected]");
|
---|
271 | testMailClient.setSubject("Test subject");
|
---|
272 | testMailClient.setMessage( "test line 1\n" +
|
---|
273 | "test line 2" );
|
---|
274 |
|
---|
275 | Thread client = new Thread(testMailClient);
|
---|
276 | client.start();
|
---|
277 |
|
---|
278 | try {
|
---|
279 | server.join(60 * 1000); // 60s
|
---|
280 | client.join(30 * 1000); // a further 30s
|
---|
281 | } catch (InterruptedException ie ) {
|
---|
282 | fail( "InterruptedException: " + ie );
|
---|
283 | }
|
---|
284 |
|
---|
285 | String result = testMailServer.getResult();
|
---|
286 | String expectedResult = "220 test SMTP EmailTaskTest\r\n" +
|
---|
287 | "HELO " + local + "\r\n" +
|
---|
288 | "250 " + local + " Hello " + local + " [127.0.0.1], pleased to meet you\r\n" +
|
---|
289 | "MAIL FROM: <[email protected]>\r\n" +
|
---|
290 | "250\r\n" +
|
---|
291 | "RCPT TO: <[email protected]>\r\n" +
|
---|
292 | "250\r\n" +
|
---|
293 | "DATA\r\n" +
|
---|
294 | "354\r\n" +
|
---|
295 | "Subject: Test subject\r\n" +
|
---|
296 | "From: Mail Message <[email protected]>\r\n" +
|
---|
297 | "X-Mailer: org.apache.tools.mail.MailMessage (ant.apache.org)\r\n" +
|
---|
298 | "\r\n" +
|
---|
299 | "test line 1\r\n" +
|
---|
300 | "test line 2\r\n" +
|
---|
301 | "\r\n" +
|
---|
302 | ".\r\n" +
|
---|
303 | "250\r\n" +
|
---|
304 | "QUIT\r\n" +
|
---|
305 | "221\r\n";
|
---|
306 | assertEquals( expectedResult.length(), result.length() );
|
---|
307 | assertEquals( expectedResult, result );
|
---|
308 | if ( testMailClient.isFailed() ) {
|
---|
309 | fail( testMailClient.getFailMessage() );
|
---|
310 | }
|
---|
311 | }
|
---|
312 |
|
---|
313 |
|
---|
314 | /**
|
---|
315 | * Test a MailMessage with no subject line
|
---|
316 | * Subject is an optional field (RFC 822 s4.1)
|
---|
317 | */
|
---|
318 | public void testNoSubject() {
|
---|
319 | ServerThread testMailServer = new ServerThread();
|
---|
320 | Thread server = new Thread(testMailServer);
|
---|
321 | server.start();
|
---|
322 |
|
---|
323 | ClientThread testMailClient = new ClientThread();
|
---|
324 |
|
---|
325 | testMailClient.from("Mail Message <[email protected]>");
|
---|
326 | testMailClient.to("[email protected]");
|
---|
327 | testMailClient.setMessage( "test line 1\n" +
|
---|
328 | "test line 2" );
|
---|
329 |
|
---|
330 | Thread client = new Thread(testMailClient);
|
---|
331 | client.start();
|
---|
332 |
|
---|
333 | try {
|
---|
334 | server.join(60 * 1000); // 60s
|
---|
335 | client.join(30 * 1000); // a further 30s
|
---|
336 | } catch (InterruptedException ie ) {
|
---|
337 | fail( "InterruptedException: " + ie );
|
---|
338 | }
|
---|
339 |
|
---|
340 | String result = testMailServer.getResult();
|
---|
341 | String expectedResult = "220 test SMTP EmailTaskTest\r\n" +
|
---|
342 | "HELO " + local + "\r\n" +
|
---|
343 | "250 " + local + " Hello " + local + " [127.0.0.1], pleased to meet you\r\n" +
|
---|
344 | "MAIL FROM: <[email protected]>\r\n" +
|
---|
345 | "250\r\n" +
|
---|
346 | "RCPT TO: <[email protected]>\r\n" +
|
---|
347 | "250\r\n" +
|
---|
348 | "DATA\r\n" +
|
---|
349 | "354\r\n" +
|
---|
350 | "From: Mail Message <[email protected]>\r\n" +
|
---|
351 | "To: [email protected]\r\n" +
|
---|
352 | "X-Mailer: org.apache.tools.mail.MailMessage (ant.apache.org)\r\n" +
|
---|
353 | "\r\n" +
|
---|
354 | "test line 1\r\n" +
|
---|
355 | "test line 2\r\n" +
|
---|
356 | "\r\n" +
|
---|
357 | ".\r\n" +
|
---|
358 | "250\r\n" +
|
---|
359 | "QUIT\r\n" +
|
---|
360 | "221\r\n";
|
---|
361 | assertEquals( expectedResult.length(), result.length() );
|
---|
362 | assertEquals( expectedResult, result );
|
---|
363 | if ( testMailClient.isFailed() ) {
|
---|
364 | fail( testMailClient.getFailMessage() );
|
---|
365 | }
|
---|
366 | }
|
---|
367 |
|
---|
368 |
|
---|
369 | /**
|
---|
370 | * Test a MailMessage with empty body message
|
---|
371 | */
|
---|
372 | public void testEmptyBody() {
|
---|
373 | ServerThread testMailServer = new ServerThread();
|
---|
374 | Thread server = new Thread(testMailServer);
|
---|
375 | server.start();
|
---|
376 |
|
---|
377 | ClientThread testMailClient = new ClientThread();
|
---|
378 |
|
---|
379 | testMailClient.from("Mail Message <[email protected]>");
|
---|
380 | testMailClient.to("[email protected]");
|
---|
381 | testMailClient.setSubject("Test subject");
|
---|
382 | testMailClient.setMessage("");
|
---|
383 |
|
---|
384 | Thread client = new Thread(testMailClient);
|
---|
385 | client.start();
|
---|
386 |
|
---|
387 | try {
|
---|
388 | server.join(60 * 1000); // 60s
|
---|
389 | client.join(30 * 1000); // a further 30s
|
---|
390 | } catch (InterruptedException ie ) {
|
---|
391 | fail( "InterruptedException: " + ie );
|
---|
392 | }
|
---|
393 |
|
---|
394 | String result = testMailServer.getResult();
|
---|
395 | String expectedResult = "220 test SMTP EmailTaskTest\r\n" +
|
---|
396 | "HELO " + local + "\r\n" +
|
---|
397 | "250 " + local + " Hello " + local + " [127.0.0.1], pleased to meet you\r\n" +
|
---|
398 | "MAIL FROM: <[email protected]>\r\n" +
|
---|
399 | "250\r\n" +
|
---|
400 | "RCPT TO: <[email protected]>\r\n" +
|
---|
401 | "250\r\n" +
|
---|
402 | "DATA\r\n" +
|
---|
403 | "354\r\n" +
|
---|
404 | "Subject: Test subject\r\n" +
|
---|
405 | "From: Mail Message <[email protected]>\r\n" +
|
---|
406 | "To: [email protected]\r\n" +
|
---|
407 | "X-Mailer: org.apache.tools.mail.MailMessage (ant.apache.org)\r\n" +
|
---|
408 | "\r\n" +
|
---|
409 | "\r\n" +
|
---|
410 | "\r\n" +
|
---|
411 | ".\r\n" +
|
---|
412 | "250\r\n" +
|
---|
413 | "QUIT\r\n" +
|
---|
414 | "221\r\n";
|
---|
415 | assertEquals(expectedResult.length(), result.length());
|
---|
416 | assertEquals(expectedResult, result);
|
---|
417 | if (testMailClient.isFailed()) {
|
---|
418 | fail(testMailClient.getFailMessage());
|
---|
419 | }
|
---|
420 | }
|
---|
421 |
|
---|
422 |
|
---|
423 | /**
|
---|
424 | * Test a MailMessage with US-ASCII character set
|
---|
425 | * The next four testcase can be kinda hard to debug as Ant will often
|
---|
426 | * print the junit failure in US-ASCII.
|
---|
427 | */
|
---|
428 | public void testAsciiCharset() {
|
---|
429 |
|
---|
430 | ServerThread testMailServer = new ServerThread();
|
---|
431 | Thread server = new Thread(testMailServer);
|
---|
432 | server.start();
|
---|
433 |
|
---|
434 | ClientThread testMailClient = new ClientThread();
|
---|
435 |
|
---|
436 | testMailClient.from("Mail Message <[email protected]>");
|
---|
437 | testMailClient.to("Ceki G\u00fclc\u00fc <[email protected]>");
|
---|
438 | testMailClient.setSubject("Test subject");
|
---|
439 | testMailClient.setMessage("");
|
---|
440 |
|
---|
441 | Thread client = new Thread(testMailClient);
|
---|
442 | client.start();
|
---|
443 |
|
---|
444 | try {
|
---|
445 | server.join(60 * 1000); // 60s
|
---|
446 | client.join(30 * 1000); // a further 30s
|
---|
447 | } catch (InterruptedException ie ) {
|
---|
448 | fail("InterruptedException: " + ie);
|
---|
449 | }
|
---|
450 |
|
---|
451 | String result = testMailServer.getResult();
|
---|
452 | String expectedResult = "220 test SMTP EmailTaskTest\r\n" +
|
---|
453 | "HELO " + local + "\r\n" +
|
---|
454 | "250 " + local + " Hello " + local + " [127.0.0.1], pleased to meet you\r\n" +
|
---|
455 | "MAIL FROM: <[email protected]>\r\n" +
|
---|
456 | "250\r\n" +
|
---|
457 | "RCPT TO: <[email protected]>\r\n" +
|
---|
458 | "250\r\n" +
|
---|
459 | "DATA\r\n" +
|
---|
460 | "354\r\n" +
|
---|
461 | "Subject: Test subject\r\n" +
|
---|
462 | "From: Mail Message <[email protected]>\r\n" +
|
---|
463 | "To: Ceki G\u00fclc\u00fc <[email protected]>\r\n" +
|
---|
464 | "X-Mailer: org.apache.tools.mail.MailMessage (ant.apache.org)\r\n" +
|
---|
465 | "\r\n" +
|
---|
466 | "\r\n" +
|
---|
467 | "\r\n" +
|
---|
468 | ".\r\n" +
|
---|
469 | "250\r\n" +
|
---|
470 | "QUIT\r\n" +
|
---|
471 | "221\r\n";
|
---|
472 | ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
|
---|
473 | ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
|
---|
474 | PrintStream bos1 = new PrintStream(baos1, true);
|
---|
475 | PrintStream bos2 = new PrintStream(baos2, true);
|
---|
476 |
|
---|
477 | bos1.print(expectedResult);
|
---|
478 | bos2.print(result);
|
---|
479 |
|
---|
480 | assertEquals( "expected message length != actual message length "
|
---|
481 | + "in testAsciiCharset()", expectedResult.length(), result.length() );
|
---|
482 | assertEquals( "baos1 and baos2 should be the same in testAsciiCharset()",
|
---|
483 | baos1.toString(), baos2.toString() ); // order of headers cannot be guaranteed
|
---|
484 | if (testMailClient.isFailed()) {
|
---|
485 | fail(testMailClient.getFailMessage());
|
---|
486 | }
|
---|
487 | }
|
---|
488 |
|
---|
489 |
|
---|
490 |
|
---|
491 |
|
---|
492 | /**
|
---|
493 | * A private test class that pretends to be a mail transfer agent
|
---|
494 | */
|
---|
495 | private class ServerThread implements Runnable {
|
---|
496 |
|
---|
497 | private StringBuffer sb = null;
|
---|
498 | private boolean loop = false;
|
---|
499 | ServerSocket ssock = null;
|
---|
500 | Socket sock = null;
|
---|
501 | BufferedWriter out = null;
|
---|
502 | BufferedReader in = null;
|
---|
503 | private boolean data = false; // state engine: false=envelope, true=message
|
---|
504 |
|
---|
505 | public void run() {
|
---|
506 |
|
---|
507 | try {
|
---|
508 | ssock = new ServerSocket(TEST_PORT);
|
---|
509 | sock = ssock.accept(); // wait for connection
|
---|
510 | in = new BufferedReader( new InputStreamReader(
|
---|
511 | sock.getInputStream()) );
|
---|
512 | out = new BufferedWriter( new OutputStreamWriter(
|
---|
513 | sock.getOutputStream() ) );
|
---|
514 | sb = new StringBuffer();
|
---|
515 | send( "220 test SMTP EmailTaskTest\r\n" );
|
---|
516 | loop = true;
|
---|
517 | while ( loop ) {
|
---|
518 | String response = in.readLine();
|
---|
519 | if ( response == null ) {
|
---|
520 | loop = false;
|
---|
521 | break;
|
---|
522 | }
|
---|
523 | sb.append( response + "\r\n" );
|
---|
524 |
|
---|
525 | if ( !data && response.startsWith( "HELO" ) ) {
|
---|
526 | send( "250 " + local + " Hello " + local + " " +
|
---|
527 | "[127.0.0.1], pleased to meet you\r\n" );
|
---|
528 | } else if ( !data && response.startsWith("MAIL") ) {
|
---|
529 | send( "250\r\n" );
|
---|
530 | } else if ( !data && response.startsWith("RCPT")) {
|
---|
531 | send( "250\r\n" );
|
---|
532 | } else if (!data && response.startsWith("DATA")) {
|
---|
533 | send( "354\r\n" );
|
---|
534 | data = true;
|
---|
535 | } else if (data && response.equals(".") ) {
|
---|
536 | send( "250\r\n" );
|
---|
537 | data = false;
|
---|
538 | } else if (!data && response.startsWith("QUIT")) {
|
---|
539 | send( "221\r\n" );
|
---|
540 | loop = false;
|
---|
541 | } else if (!data) {
|
---|
542 | //throw new IllegalStateException("Command unrecognized: "
|
---|
543 | // + response);
|
---|
544 | send( "500 5.5.1 Command unrecognized: \"" +
|
---|
545 | response + "\"\r\n" );
|
---|
546 | loop = false;
|
---|
547 | } else {
|
---|
548 | // sb.append( response + "\r\n" );
|
---|
549 | }
|
---|
550 |
|
---|
551 | } // while
|
---|
552 | } catch (IOException ioe) {
|
---|
553 | fail();
|
---|
554 | } finally {
|
---|
555 | disconnect();
|
---|
556 | }
|
---|
557 | }
|
---|
558 |
|
---|
559 | private void send(String retmsg) throws IOException {
|
---|
560 | out.write( retmsg );
|
---|
561 | out.flush();
|
---|
562 | sb.append( retmsg );
|
---|
563 | }
|
---|
564 |
|
---|
565 | private void disconnect() {
|
---|
566 | if (out != null) {
|
---|
567 | try {
|
---|
568 | out.flush();
|
---|
569 | out.close();
|
---|
570 | out = null;
|
---|
571 | } catch (IOException e) {
|
---|
572 | // ignore
|
---|
573 | }
|
---|
574 | }
|
---|
575 | if (in != null) {
|
---|
576 | try {
|
---|
577 | in.close();
|
---|
578 | in = null;
|
---|
579 | } catch (IOException e) {
|
---|
580 | // ignore
|
---|
581 | }
|
---|
582 | }
|
---|
583 | if (sock != null) {
|
---|
584 | try {
|
---|
585 | sock.close();
|
---|
586 | sock = null;
|
---|
587 | } catch (IOException e) {
|
---|
588 | // ignore
|
---|
589 | }
|
---|
590 | }
|
---|
591 | if (ssock != null) {
|
---|
592 | try {
|
---|
593 | ssock.close();
|
---|
594 | ssock = null;
|
---|
595 | } catch (IOException e) {
|
---|
596 | // ignore
|
---|
597 | }
|
---|
598 | }
|
---|
599 | }
|
---|
600 |
|
---|
601 | public synchronized String getResult() {
|
---|
602 | loop = false;
|
---|
603 | return sb.toString();
|
---|
604 | }
|
---|
605 |
|
---|
606 | }
|
---|
607 |
|
---|
608 | /**
|
---|
609 | * A private test class that wraps MailMessage
|
---|
610 | */
|
---|
611 | private class ClientThread implements Runnable {
|
---|
612 |
|
---|
613 | private MailMessage msg;
|
---|
614 | private boolean fail = false;
|
---|
615 | private String failMessage = null;
|
---|
616 |
|
---|
617 | protected String from = null;
|
---|
618 | protected String subject = null;
|
---|
619 | protected String message = null;
|
---|
620 |
|
---|
621 | protected Vector replyToList = new Vector();
|
---|
622 | protected Vector toList = new Vector();
|
---|
623 | protected Vector ccList = new Vector();
|
---|
624 | protected Vector bccList = new Vector();
|
---|
625 |
|
---|
626 |
|
---|
627 | public void run() {
|
---|
628 | for (int i = 9; i > 0; i--) {
|
---|
629 | try {
|
---|
630 | msg = new MailMessage("localhost", TEST_PORT);
|
---|
631 | } catch (java.net.ConnectException ce) {
|
---|
632 | try {
|
---|
633 | Thread.sleep(10 * 1000);
|
---|
634 | } catch (InterruptedException ie) {
|
---|
635 | // ignore
|
---|
636 | }
|
---|
637 | } catch (IOException ioe) {
|
---|
638 | fail = true;
|
---|
639 | failMessage = "IOException: " + ioe;
|
---|
640 | return;
|
---|
641 | }
|
---|
642 | if (msg != null) {
|
---|
643 | break;
|
---|
644 | }
|
---|
645 | }
|
---|
646 |
|
---|
647 | if (msg == null) {
|
---|
648 | fail = true;
|
---|
649 | failMessage = "java.net.ConnectException: Connection refused";
|
---|
650 | return;
|
---|
651 | }
|
---|
652 |
|
---|
653 | try {
|
---|
654 | msg.from(from);
|
---|
655 |
|
---|
656 | Enumeration e;
|
---|
657 |
|
---|
658 | e = replyToList.elements();
|
---|
659 | while (e.hasMoreElements()) {
|
---|
660 | msg.replyto(e.nextElement().toString());
|
---|
661 | }
|
---|
662 |
|
---|
663 | e = toList.elements();
|
---|
664 | while (e.hasMoreElements()) {
|
---|
665 | msg.to(e.nextElement().toString());
|
---|
666 | }
|
---|
667 |
|
---|
668 | e = ccList.elements();
|
---|
669 | while (e.hasMoreElements()) {
|
---|
670 | msg.cc(e.nextElement().toString());
|
---|
671 | }
|
---|
672 |
|
---|
673 | e = bccList.elements();
|
---|
674 | while (e.hasMoreElements()) {
|
---|
675 | msg.bcc(e.nextElement().toString());
|
---|
676 | }
|
---|
677 |
|
---|
678 | if (subject != null) {
|
---|
679 | msg.setSubject(subject);
|
---|
680 | }
|
---|
681 |
|
---|
682 | if (message != null ) {
|
---|
683 | PrintStream out = msg.getPrintStream();
|
---|
684 | out.println( message );
|
---|
685 | }
|
---|
686 |
|
---|
687 | msg.sendAndClose();
|
---|
688 | } catch (IOException ioe) {
|
---|
689 | fail = true;
|
---|
690 | failMessage = "IOException: " + ioe;
|
---|
691 | return;
|
---|
692 | }
|
---|
693 | }
|
---|
694 |
|
---|
695 | public boolean isFailed() {
|
---|
696 | return fail;
|
---|
697 | }
|
---|
698 |
|
---|
699 | public String getFailMessage() {
|
---|
700 | return failMessage;
|
---|
701 | }
|
---|
702 |
|
---|
703 | public void replyTo(String replyTo) {
|
---|
704 | replyToList.add(replyTo);
|
---|
705 | }
|
---|
706 |
|
---|
707 | public void to(String to) {
|
---|
708 | toList.add(to);
|
---|
709 | }
|
---|
710 |
|
---|
711 | public void cc(String cc) {
|
---|
712 | ccList.add(cc);
|
---|
713 | }
|
---|
714 |
|
---|
715 | public void bcc(String bcc) {
|
---|
716 | bccList.add(bcc);
|
---|
717 | }
|
---|
718 |
|
---|
719 | public void setSubject(String subject) {
|
---|
720 | this.subject = subject;
|
---|
721 | }
|
---|
722 |
|
---|
723 | public void from(String from) {
|
---|
724 | this.from = from;
|
---|
725 | }
|
---|
726 |
|
---|
727 | public void setMessage(String message) {
|
---|
728 | this.message = message;
|
---|
729 | }
|
---|
730 |
|
---|
731 | }
|
---|
732 |
|
---|
733 | }
|
---|