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.IOException;
019:
020: import org.apache.commons.mail.mocks.MockSimpleEmail;
021:
022: /**
023: * JUnit test case for SimpleEmailTest
024: * @since 1.0
025: * @version $Revision: 279300 $ $Date: 2005-09-07 13:43:52 +0200 (Wed, 07 Sep 2005) $
026: */
027:
028: public class SimpleEmailTest extends BaseEmailTestCase {
029: /** */
030: private MockSimpleEmail email = null;
031:
032: /**
033: * @param name name
034: */
035: public SimpleEmailTest(String name) {
036: super (name);
037: }
038:
039: /** */
040: protected void setUp() {
041: super .setUp();
042: // reusable objects to be used across multiple tests
043: this .email = new MockSimpleEmail();
044: }
045:
046: /** */
047: public void testGetSetMsg() {
048: // ====================================================================
049: // Test Success
050: // ====================================================================
051: try {
052: for (int i = 0; i < testCharsValid.length; i++) {
053: this .email.setMsg(testCharsValid[i]);
054: assertEquals(testCharsValid[i], this .email.getMsg());
055: }
056: } catch (EmailException e) {
057: e.printStackTrace();
058: fail("Unexpected exception thrown");
059: }
060:
061: // ====================================================================
062: // Test Exception
063: // ====================================================================
064: for (int i = 0; i < this .testCharsNotValid.length; i++) {
065: try {
066: this .email.setMsg(this .testCharsNotValid[i]);
067: fail("Should have thrown an exception");
068: } catch (EmailException e) {
069: assertTrue(true);
070: } catch (Exception e) {
071: e.printStackTrace();
072: fail("Unexpected exception thrown");
073: }
074: }
075:
076: }
077:
078: /**
079: * @todo Add code to test the popBeforeSmtp() settings
080: */
081: public void testSend() {
082: // ====================================================================
083: // Test Success
084: // ====================================================================
085: try {
086: this .getMailServer();
087:
088: this .email = new MockSimpleEmail();
089: this .email.setHostName(this .strTestMailServer);
090: this .email.setSmtpPort(this .getMailServerPort());
091: this .email.setFrom(this .strTestMailFrom);
092: this .email.addTo(this .strTestMailTo);
093:
094: if (this .strTestUser != null && this .strTestPasswd != null) {
095: this .email.setAuthentication(this .strTestUser,
096: this .strTestPasswd);
097: }
098:
099: String strSubject = "Test Msg Subject";
100: String strMessage = "Test Msg Body";
101:
102: this .email.setCharset(Email.ISO_8859_1);
103: this .email.setSubject(strSubject);
104:
105: this .email.setMsg(strMessage);
106:
107: this .email.send();
108:
109: this .fakeMailServer.stop();
110: validateSend(this .fakeMailServer, strSubject, this .email
111: .getMsg(), this .email.getFromAddress(), this .email
112: .getToList(), this .email.getCcList(), this .email
113: .getBccList(), true);
114: }
115:
116: catch (IOException e) {
117: e.printStackTrace();
118: fail("failed to save email to output file");
119: } catch (Exception e) {
120: e.printStackTrace();
121: fail("Unexpected exception thrown");
122: }
123: }
124: }
|