001: // Copyright © 2006-2007 ASERT. Released under the Canoo Webtest license.
002: package com.canoo.webtest.plugins.emailtest;
003:
004: import java.io.IOException;
005:
006: import javax.mail.Message;
007: import javax.mail.Multipart;
008: import javax.mail.MessagingException;
009:
010: import com.canoo.webtest.self.TestBlock;
011: import com.canoo.webtest.steps.Step;
012:
013: /**
014: * Test for {@link EmailStorePartCount}.
015: *
016: * @author Paul King, ASERT
017: */
018: public class EmailStorePartCountTest extends BaseEmailTestCase {
019: private static final String PROPERTY_NAME = "dummyProperty";
020: private static final String MESSAGE_ID = "123";
021: private static final boolean DELETE_ON_EXIT = false;
022: private static final String LS = System
023: .getProperty("line.separator");
024:
025: protected Step createStep() {
026: return new EmailStorePartCount();
027: }
028:
029: public void testMandatoryParams() {
030: final EmailStorePartCount step = (EmailStorePartCount) getStep();
031: step.getContext().put("EmailConfigInfo", null);
032: assertStepRejectsNullParam("property", new TestBlock() {
033: public void call() throws Throwable {
034: executeStep(step);
035: }
036: });
037: }
038:
039: public void testInvalidMessageId() {
040: final EmailStorePartCount step = (EmailStorePartCount) getStep();
041: step.getContext().put("EmailConfigInfo", null);
042: step.setProperty(PROPERTY_NAME);
043: step.setMessageId("non-integer");
044: assertErrorOnExecute(step, "invalid messageId",
045: "Can't parse messageId parameter with value 'non-integer' as an integer.");
046: }
047:
048: public void testSimpleNoAttachments() throws Exception {
049: checkSimpleAttachments("Simple Message with no attachments "
050: + LS, 0);
051: }
052:
053: public void testSimpleAttachment() throws Exception {
054: checkSimpleAttachments("Message" + LS + "begin 123 " + LS, 1);
055: }
056:
057: public void testSimpleAttachments() throws Exception {
058: checkSimpleAttachments("Message" + LS + "begin 123 " + LS
059: + "begin 345 " + LS, 2);
060: }
061:
062: private void checkSimpleAttachments(final String messageBody,
063: final int numAttachments) throws Exception {
064: final EmailStorePartCount step = (EmailStorePartCount) getStep();
065: final EmailHelper helper = prepareHelper(step);
066: final Message mockMessage = setUpGetMessageExpectations(step,
067: helper, DELETE_ON_EXIT);
068: mockMessage.getContent();
069: modify().returnValue(messageBody);
070: setUpMessageOperationFinaliseExpectations(helper,
071: DELETE_ON_EXIT);
072: startVerification();
073: executeStep(step);
074: assertEquals(numAttachments, Integer.parseInt(step
075: .getWebtestProperty(PROPERTY_NAME)));
076: }
077:
078: public void testMultipart1() throws Exception {
079: checkMultipart(1);
080: }
081:
082: public void testMultipart2() throws Exception {
083: checkMultipart(2);
084: }
085:
086: private void checkMultipart(final int partCount) throws Exception {
087: final EmailStorePartCount step = (EmailStorePartCount) getStep();
088: final EmailHelper helper = prepareHelper(step);
089: final Message mockMessage = setUpGetMessageExpectations(step,
090: helper, DELETE_ON_EXIT);
091: final Multipart multipart = setUpMultipart(partCount);
092: mockMessage.getContent();
093: modify().returnValue(multipart);
094: setUpMessageOperationFinaliseExpectations(helper,
095: DELETE_ON_EXIT);
096: startVerification();
097: executeStep(step);
098: assertEquals(partCount, Integer.parseInt(step
099: .getWebtestProperty(PROPERTY_NAME)));
100: }
101:
102: public void testIoException() throws Exception {
103: final EmailStorePartCount step = (EmailStorePartCount) getStep();
104: final EmailHelper helper = prepareHelper(step);
105: final Message mockMessage = setUpGetMessageExpectations(step,
106: helper, DELETE_ON_EXIT);
107: mockMessage.getContent();
108: modify().throwException(new IOException("dummyIoException"));
109: setUpMessageOperationFinaliseExpectations(helper,
110: DELETE_ON_EXIT);
111: startVerification();
112: assertFailOnExecute(step, "expected to fail",
113: "Error processing content: Error processing email message: dummyIoException");
114: }
115:
116: private Multipart setUpMultipart(int partCount)
117: throws MessagingException {
118: Multipart mockMultipart = (Multipart) intercept(
119: Multipart.class, "mockMultipart");
120: mockMultipart.getCount();
121: modify().returnValue(partCount);
122: return mockMultipart;
123: }
124:
125: private EmailHelper prepareHelper(final EmailStorePartCount step) {
126: step.setProperty(PROPERTY_NAME);
127: step.setMessageId(MESSAGE_ID);
128: final EmailHelper helper = (EmailHelper) mock(
129: EmailHelper.class, "helper");
130: step.setHelper(helper);
131: return helper;
132: }
133:
134: }
|