Source Code Cross Referenced for BaseEmailTestCase.java in  » Net » apache-common-Email » org » apache » commons » mail » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » apache common Email » org.apache.commons.mail 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2001-2005 The Apache Software Foundation
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *     http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:        package org.apache.commons.mail;
017:
018:        import java.io.File;
019:        import java.io.FileWriter;
020:        import java.io.IOException;
021:        import java.util.Date;
022:        import java.util.Iterator;
023:        import java.util.List;
024:
025:        import javax.mail.Multipart;
026:        import javax.mail.internet.InternetAddress;
027:
028:        import junit.framework.TestCase;
029:
030:        import org.apache.commons.mail.settings.EmailConfiguration;
031:
032:        import com.dumbster.smtp.SimpleSmtpServer;
033:        import com.dumbster.smtp.SmtpMessage;
034:
035:        /**
036:         * Base test case for Email test classes
037:         *
038:         * @since 1.0
039:         * @author <a href="mailto:corey.scott@gmail.com">Corey Scott</a>
040:         * @author <a href="mailto:epugh@opensourceconnections.com">Eric Pugh</a>
041:         * @version $Id: BaseEmailTestCase.java 279300 2005-09-07 11:43:52Z henning $
042:         */
043:
044:        public class BaseEmailTestCase extends TestCase {
045:            private static int mailServerPort = EmailConfiguration.MAIL_SERVER_PORT;
046:
047:            /** The fake Dumbster email server */
048:            protected SimpleSmtpServer fakeMailServer = null;
049:
050:            /** Mail server used for testing */
051:            protected String strTestMailServer = EmailConfiguration.MAIL_SERVER;
052:            /** From address for the test email */
053:            protected String strTestMailFrom = EmailConfiguration.TEST_FROM;
054:            /** Destination address for the test email */
055:            protected String strTestMailTo = EmailConfiguration.TEST_TO;
056:            /** Mailserver username (set if needed) */
057:            protected String strTestUser = EmailConfiguration.TEST_USER;
058:            /** Mailserver strTestPasswd (set if needed) */
059:            protected String strTestPasswd = EmailConfiguration.TEST_PASSWD;
060:            /** URL to used to test URL attachmetns (Must be valid) */
061:            protected String strTestURL = EmailConfiguration.TEST_URL;
062:
063:            /** Padding at end of body added by dumbser/send */
064:            public static final int BODY_END_PAD = 3;
065:            /** Padding at start of body added by dumbser/send */
066:            public static final int BODY_START_PAD = 2;
067:
068:            /** Where to save email output **/
069:            private File emailOutputDir;
070:
071:            protected int getMailServerPort() {
072:                return mailServerPort;
073:            }
074:
075:            /** Test characters acceptable to email */
076:            protected String[] testCharsValid = { " ", "a", "A", "\uc5ec",
077:                    "0123456789", "012345678901234567890", "\n" };
078:
079:            /** Array of test strings */
080:            protected String[] testCharsNotValid = { "", null };
081:
082:            /**
083:             * @param name name
084:             */
085:            public BaseEmailTestCase(String name) {
086:                super (name);
087:                emailOutputDir = new File("target/test-emails");
088:                if (!emailOutputDir.exists()) {
089:                    emailOutputDir.mkdirs();
090:                }
091:            }
092:
093:            /** */
094:            protected void setUp() {
095:
096:            }
097:
098:            /** */
099:            protected void tearDown() {
100:                //stop the fake email server (if started)
101:                if (this .fakeMailServer != null
102:                        && !this .fakeMailServer.isStopped()) {
103:                    this .fakeMailServer.stop();
104:                    assertTrue(this .fakeMailServer.isStopped());
105:                }
106:
107:                this .fakeMailServer = null;
108:            }
109:
110:            /**
111:             *
112:             * @param email email
113:             * @throws IOException Exception
114:             */
115:            protected void saveEmailToFile(SmtpMessage email)
116:                    throws IOException {
117:                File emailFile = new File(emailOutputDir, "email"
118:                        + new Date().getTime() + ".txt");
119:                FileWriter fw = new FileWriter(emailFile);
120:                fw.write(email.toString());
121:                fw.close();
122:            }
123:
124:            /**
125:             * @param intMsgNo the message to retrieve
126:             * @return message as string
127:             */
128:            public String getMessageAsString(int intMsgNo) {
129:                assertTrue(this .fakeMailServer.getReceivedEmailSize() >= intMsgNo);
130:                Iterator emailIter = fakeMailServer.getReceivedEmail();
131:                SmtpMessage emailMessage = null;
132:                for (int intCurMsg = 0; intCurMsg < intMsgNo; intCurMsg++) {
133:                    emailMessage = (SmtpMessage) emailIter.next();
134:                }
135:
136:                if (emailMessage != null) {
137:                    return emailMessage.toString();
138:                }
139:                fail("Message note found");
140:                return "";
141:            }
142:
143:            /** */
144:            public void getMailServer() {
145:                if (this .fakeMailServer == null
146:                        || this .fakeMailServer.isStopped()) {
147:                    mailServerPort++;
148:
149:                    this .fakeMailServer = SimpleSmtpServer
150:                            .start(getMailServerPort());
151:
152:                    assertFalse(this .fakeMailServer.isStopped());
153:
154:                    Date dtStartWait = new Date();
155:                    while (this .fakeMailServer.isStopped()) {
156:                        // test for connected
157:                        if (this .fakeMailServer != null
158:                                && !this .fakeMailServer.isStopped()) {
159:                            break;
160:                        }
161:
162:                        // test for timeout
163:                        if ((dtStartWait.getTime() + EmailConfiguration.TIME_OUT) <= new Date()
164:                                .getTime()) {
165:                            fail("Mail server failed to start");
166:                        }
167:                    }
168:                }
169:            }
170:
171:            /**
172:             * Validate the message was sent properly
173:             * @param mailServer reference to the fake mail server
174:             * @param strSubject expected subject
175:             * @param fromAdd expected from address
176:             * @param toAdd list of expected to addresses
177:             * @param ccAdd list of expected cc addresses
178:             * @param bccAdd list of expected bcc addresses
179:             * @param boolSaveToFile true will output to file, false doesnt
180:             * @return SmtpMessage email to check
181:             * @throws IOException Exception
182:             */
183:            protected SmtpMessage validateSend(SimpleSmtpServer mailServer,
184:                    String strSubject, InternetAddress fromAdd, List toAdd,
185:                    List ccAdd, List bccAdd, boolean boolSaveToFile)
186:                    throws IOException {
187:                assertTrue(mailServer.getReceivedEmailSize() == 1);
188:                Iterator emailIter = fakeMailServer.getReceivedEmail();
189:                SmtpMessage emailMessage = (SmtpMessage) emailIter.next();
190:
191:                if (boolSaveToFile) {
192:                    this .saveEmailToFile(emailMessage);
193:                }
194:
195:                // test subject
196:                assertEquals(strSubject, emailMessage.getHeaderValue("Subject"));
197:
198:                //test from address
199:                assertEquals(fromAdd.toString(), emailMessage
200:                        .getHeaderValue("From"));
201:
202:                //test to address
203:                assertTrue(toAdd.toString().indexOf(
204:                        emailMessage.getHeaderValue("To")) != -1);
205:
206:                //test cc address
207:                if (ccAdd.size() > 0) {
208:                    assertTrue(ccAdd.toString().indexOf(
209:                            emailMessage.getHeaderValue("Cc")) != -1);
210:                }
211:
212:                //test bcc address
213:                if (bccAdd.size() > 0) {
214:                    assertTrue(bccAdd.toString().indexOf(
215:                            emailMessage.getHeaderValue("Bcc")) != -1);
216:                }
217:
218:                return emailMessage;
219:            }
220:
221:            /**
222:             * Validate the message was sent properly
223:             * @param mailServer reference to the fake mail server
224:             * @param strSubject expected subject
225:             * @param content the expected message content
226:             * @param fromAdd expected from address
227:             * @param toAdd list of expected to addresses
228:             * @param ccAdd list of expected cc addresses
229:             * @param bccAdd list of expected bcc addresses
230:             * @param boolSaveToFile true will output to file, false doesnt
231:             * @throws IOException Exception
232:             */
233:            protected void validateSend(SimpleSmtpServer mailServer,
234:                    String strSubject, Multipart content,
235:                    InternetAddress fromAdd, List toAdd, List ccAdd,
236:                    List bccAdd, boolean boolSaveToFile) throws IOException {
237:                // test other properties
238:                SmtpMessage emailMessage = this .validateSend(mailServer,
239:                        strSubject, fromAdd, toAdd, ccAdd, bccAdd,
240:                        boolSaveToFile);
241:
242:                // test message content
243:
244:                // get sent email content
245:                String strSentContent = content.getContentType();
246:                // get received email content (chop off the auto-added \n
247:                // and -- (front and end)
248:                String strMessageBody = emailMessage.getBody().substring(
249:                        BaseEmailTestCase.BODY_START_PAD,
250:                        emailMessage.getBody().length()
251:                                - BaseEmailTestCase.BODY_END_PAD);
252:                assertTrue(strMessageBody.indexOf(strSentContent) != -1);
253:            }
254:
255:            /**
256:             * Validate the message was sent properly
257:             * @param mailServer reference to the fake mail server
258:             * @param strSubject expected subject
259:             * @param strMessage the expected message as a string
260:             * @param fromAdd expected from address
261:             * @param toAdd list of expected to addresses
262:             * @param ccAdd list of expected cc addresses
263:             * @param bccAdd list of expected bcc addresses
264:             * @param boolSaveToFile true will output to file, false doesnt
265:             * @throws IOException Exception
266:             */
267:            protected void validateSend(SimpleSmtpServer mailServer,
268:                    String strSubject, String strMessage,
269:                    InternetAddress fromAdd, List toAdd, List ccAdd,
270:                    List bccAdd, boolean boolSaveToFile) throws IOException {
271:                // test other properties
272:                SmtpMessage emailMessage = this .validateSend(mailServer,
273:                        strSubject, fromAdd, toAdd, ccAdd, bccAdd, true);
274:
275:                // test message content
276:                assertTrue(emailMessage.getBody().indexOf(strMessage) != -1);
277:            }
278:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.