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.Folder;
009:
010: import com.canoo.webtest.self.TestBlock;
011: import com.canoo.webtest.steps.Step;
012:
013: /**
014: * Test for {@link EmailStoreMessageType}.
015: *
016: * @author Paul King, ASERT
017: */
018: public class EmailStoreMessageTypeTest 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:
023: protected Step createStep() {
024: return new EmailStoreMessageType();
025: }
026:
027: public void testMandatoryParams() {
028: final EmailStoreMessageType step = (EmailStoreMessageType) getStep();
029: step.getContext().put("EmailConfigInfo", null);
030: assertStepRejectsNullParam("property", new TestBlock() {
031: public void call() throws Throwable {
032: executeStep(step);
033: }
034: });
035: }
036:
037: public void testInvalidMessageId() {
038: final EmailStoreMessageType step = (EmailStoreMessageType) getStep();
039: step.getContext().put("EmailConfigInfo", null);
040: step.setProperty(PROPERTY_NAME);
041: step.setMessageId("non-integer");
042: assertErrorOnExecute(step, "invalid messageId",
043: "Can't parse messageId parameter with value 'non-integer' as an integer.");
044: }
045:
046: public void testSimple() throws Exception {
047: final EmailStoreMessageType step = (EmailStoreMessageType) getStep();
048: final EmailHelper helper = prepareHelper(step);
049: final Message mockMessage = setUpGetMessageExpectations(step,
050: helper, DELETE_ON_EXIT);
051: mockMessage.getContent();
052: modify().returnValue("Simple message will return a string");
053: setUpMessageOperationFinaliseExpectations(helper,
054: DELETE_ON_EXIT);
055: startVerification();
056: executeStep(step);
057: assertEquals("Simple", step.getWebtestProperty(PROPERTY_NAME));
058: }
059:
060: public void testMultipart() throws Exception {
061: final EmailStoreMessageType step = (EmailStoreMessageType) getStep();
062: final EmailHelper helper = prepareHelper(step);
063: final Message mockMessage = setUpGetMessageExpectations(step,
064: helper, DELETE_ON_EXIT);
065: mockMessage.getContent();
066: modify().returnValue(setUpMultipart());
067: setUpMessageOperationFinaliseExpectations(helper,
068: DELETE_ON_EXIT);
069: startVerification();
070: executeStep(step);
071: assertEquals("MultiPart", step
072: .getWebtestProperty(PROPERTY_NAME));
073: }
074:
075: public void testIoException() throws Exception {
076: final EmailStoreMessageType step = (EmailStoreMessageType) getStep();
077: final EmailHelper helper = prepareHelper(step);
078: final Message mockMessage = setUpGetMessageExpectations(step,
079: helper, DELETE_ON_EXIT);
080: mockMessage.getContent();
081: modify().throwException(new IOException("dummyIoException"));
082: setUpMessageOperationFinaliseExpectations(helper,
083: DELETE_ON_EXIT);
084: startVerification();
085: assertFailOnExecute(step, "expected to fail",
086: "Error processing content: Error processing email message: dummyIoException");
087: }
088:
089: public void testNullMessage() throws Exception {
090: final EmailStoreMessageType step = (EmailStoreMessageType) getStep();
091: final EmailHelper helper = prepareHelper(step);
092: final Folder mockFolder = createMockFolder();
093: step.getContext().put("EmailConfigInfo", MOCK_INFO);
094: final String mockMid = "99";
095: step.setMessageId(mockMid);
096: helper.getInboxFolder(MOCK_INFO);
097: modify().returnValue(mockFolder);
098: helper.getMessage(Integer.parseInt(mockMid), mockFolder);
099: modify().returnValue(null);
100: setUpMessageOperationFinaliseExpectations(helper,
101: DELETE_ON_EXIT);
102: startVerification();
103: assertFailOnExecute(step, "expected to fail",
104: "Could not find message with id '99' on server.");
105: }
106:
107: private Multipart setUpMultipart() {
108: return (Multipart) intercept(Multipart.class, "mockMultipart");
109: }
110:
111: private EmailHelper prepareHelper(final EmailStoreMessageType step) {
112: step.setProperty(PROPERTY_NAME);
113: step.setMessageId(MESSAGE_ID);
114: final EmailHelper helper = (EmailHelper) mock(
115: EmailHelper.class, "helper");
116: step.setHelper(helper);
117: return helper;
118: }
119:
120: }
|