1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.mail;
17
18 import java.io.IOException;
19
20 import org.apache.commons.mail.mocks.MockSimpleEmail;
21
22
23
24
25
26
27
28 public class SimpleEmailTest extends BaseEmailTestCase
29 {
30
31 private MockSimpleEmail email = null;
32
33
34
35
36 public SimpleEmailTest(String name)
37 {
38 super(name);
39 }
40
41
42 protected void setUp()
43 {
44 super.setUp();
45
46 this.email = new MockSimpleEmail();
47 }
48
49
50 public void testGetSetMsg()
51 {
52
53
54
55 try
56 {
57 for (int i = 0; i < testCharsValid.length; i++)
58 {
59 this.email.setMsg(testCharsValid[i]);
60 assertEquals(testCharsValid[i], this.email.getMsg());
61 }
62 }
63 catch (EmailException e)
64 {
65 e.printStackTrace();
66 fail("Unexpected exception thrown");
67 }
68
69
70
71
72 for (int i = 0; i < this.testCharsNotValid.length; i++)
73 {
74 try
75 {
76 this.email.setMsg(this.testCharsNotValid[i]);
77 fail("Should have thrown an exception");
78 }
79 catch (EmailException e)
80 {
81 assertTrue(true);
82 }
83 catch (Exception e)
84 {
85 e.printStackTrace();
86 fail("Unexpected exception thrown");
87 }
88 }
89
90 }
91
92
93
94
95 public void testSend()
96 {
97
98
99
100 try
101 {
102 this.getMailServer();
103
104 this.email = new MockSimpleEmail();
105 this.email.setHostName(this.strTestMailServer);
106 this.email.setSmtpPort(this.getMailServerPort());
107 this.email.setFrom(this.strTestMailFrom);
108 this.email.addTo(this.strTestMailTo);
109
110 if (this.strTestUser != null && this.strTestPasswd != null)
111 {
112 this.email.setAuthentication(
113 this.strTestUser,
114 this.strTestPasswd);
115 }
116
117 String strSubject = "Test Msg Subject";
118 String strMessage = "Test Msg Body";
119
120 this.email.setCharset(Email.ISO_8859_1);
121 this.email.setSubject(strSubject);
122
123 this.email.setMsg(strMessage);
124
125 this.email.send();
126
127 this.fakeMailServer.stop();
128 validateSend(
129 this.fakeMailServer,
130 strSubject,
131 this.email.getMsg(),
132 this.email.getFromAddress(),
133 this.email.getToList(),
134 this.email.getCcList(),
135 this.email.getBccList(),
136 true);
137 }
138
139 catch (IOException e)
140 {
141 e.printStackTrace();
142 fail("failed to save email to output file");
143 }
144 catch (Exception e)
145 {
146 e.printStackTrace();
147 fail("Unexpected exception thrown");
148 }
149 }
150 }