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: import javax.mail.BodyPart;
010: import javax.mail.Part;
011:
012: import com.canoo.webtest.self.TestBlock;
013: import com.canoo.webtest.steps.Step;
014:
015: /**
016: * Test for {@link EmailStorePartFilename}.
017: *
018: * @author Paul King, ASERT
019: */
020: public class EmailStorePartFilenameTest extends BaseEmailTestCase {
021: private static final String PROPERTY_NAME = "dummyProperty";
022: private static final String MESSAGE_ID = "123";
023: private static final boolean DELETE_ON_EXIT = false;
024: private static final String LS = System
025: .getProperty("line.separator");
026:
027: protected Step createStep() {
028: return new EmailStorePartFilename();
029: }
030:
031: public void testMandatoryParams() {
032: final EmailStorePartFilename step = (EmailStorePartFilename) getStep();
033: step.getContext().put("EmailConfigInfo", null);
034: assertStepRejectsNullParam("property", new TestBlock() {
035: public void call() throws Throwable {
036: executeStep(step);
037: }
038: });
039: }
040:
041: public void testInvalidMessageId() {
042: final EmailStorePartFilename step = (EmailStorePartFilename) getStep();
043: step.getContext().put("EmailConfigInfo", null);
044: step.setProperty(PROPERTY_NAME);
045: step.setMessageId("non-integer");
046: assertErrorOnExecute(step, "invalid messageId",
047: "Can't parse messageId parameter with value 'non-integer' as an integer.");
048: }
049:
050: public void testInvalidPartIndex() {
051: final EmailStorePartFilename step = (EmailStorePartFilename) getStep();
052: step.getContext().put("EmailConfigInfo", null);
053: step.setProperty(PROPERTY_NAME);
054: step.setPartIndex("non-integer");
055: assertErrorOnExecute(step, "invalid partIndex",
056: "Can't parse partIndex parameter with value 'non-integer' as an integer.");
057: }
058:
059: public void testIoException() throws Exception {
060: final EmailStorePartFilename step = (EmailStorePartFilename) getStep();
061: final EmailHelper helper = prepareHelper(step, 0);
062: final Message mockMessage = setUpGetMessageExpectations(step,
063: helper, DELETE_ON_EXIT);
064: mockMessage.getContent();
065: modify().throwException(new IOException("dummyIoException"));
066: setUpMessageOperationFinaliseExpectations(helper,
067: DELETE_ON_EXIT);
068: startVerification();
069: assertFailOnExecute(step, "expected to fail",
070: "Error processing content: Error processing email message: dummyIoException");
071: }
072:
073: public void testSimpleNoAttachments() throws Exception {
074: checkSimpleAttachments("Simple Message with no attachments "
075: + LS, null, true, 0);
076: }
077:
078: public void testSimpleAttachment() throws Exception {
079: checkSimpleAttachments("Message" + LS + "begin 123 abcdef.txt"
080: + LS, "abcdef.txt", false, 0);
081: }
082:
083: public void testSimpleAttachments1() throws Exception {
084: checkSimpleAttachments("Message" + LS + "begin 123 abc.txt"
085: + LS + "begin 345 def.txt" + LS, "abc.txt", false, 0);
086: }
087:
088: public void testSimpleAttachments2() throws Exception {
089: checkSimpleAttachments("Message" + LS + "begin 123 abc.txt"
090: + LS + "begin 345 def.txt" + LS, "def.txt", false, 1);
091: }
092:
093: private void checkSimpleAttachments(final String messageBody,
094: final String expectedFilename, final boolean willFail,
095: final int partIndex) throws Exception {
096: final EmailStorePartFilename step = (EmailStorePartFilename) getStep();
097: final EmailHelper helper = prepareHelper(step, partIndex);
098: final Message mockMessage = setUpGetMessageExpectations(step,
099: helper, DELETE_ON_EXIT);
100: mockMessage.getContent();
101: modify().returnValue(messageBody);
102: setUpMessageOperationFinaliseExpectations(helper,
103: DELETE_ON_EXIT);
104: startVerification();
105: if (willFail) {
106: assertFailOnExecute(step, "expected to fail",
107: "No matching part found.");
108: } else {
109: executeStep(step);
110: assertEquals(expectedFilename, step
111: .getWebtestProperty(PROPERTY_NAME));
112: }
113: }
114:
115: public void testMultipart1() throws Exception {
116: checkMultipart(0, 0, "PartIndex too large.", null, null);
117: }
118:
119: public void testMultipart2() throws Exception {
120: checkMultipart(1, 0, "No filename for inline Message Part.",
121: Part.INLINE, null);
122: }
123:
124: public void testMultipart3() throws Exception {
125: checkMultipart(1, 0, null, Part.ATTACHMENT, "dummyFilename1");
126: }
127:
128: public void testMultipart4() throws Exception {
129: checkMultipart(2, 1, null, Part.ATTACHMENT, "dummyFilename2");
130: }
131:
132: private void checkMultipart(final int partCount,
133: final int partIndex, final String failureMessage,
134: final String disposition, final String expectedFilename)
135: throws Exception {
136: final EmailStorePartFilename step = (EmailStorePartFilename) getStep();
137: final EmailHelper helper = prepareHelper(step, partIndex);
138: final Message mockMessage = setUpGetMessageExpectations(step,
139: helper, DELETE_ON_EXIT);
140: final Multipart mockMultipart = setUpMultipart(partCount,
141: partIndex, disposition, expectedFilename);
142: mockMessage.getContent();
143: modify().returnValue(mockMultipart);
144: setUpMessageOperationFinaliseExpectations(helper,
145: DELETE_ON_EXIT);
146: startVerification();
147: if (failureMessage != null) {
148: assertFailOnExecute(step, "expected to fail",
149: failureMessage);
150: } else {
151: executeStep(step);
152: assertEquals(expectedFilename, step
153: .getWebtestProperty(PROPERTY_NAME));
154: }
155: }
156:
157: private BodyPart setUpBodyPart(final String disposition,
158: final String filename) throws MessagingException {
159: final BodyPart mockBodyPart = (BodyPart) mock(BodyPart.class,
160: "mockBodyPart");
161: mockBodyPart.getDisposition();
162: modify().returnValue(disposition);
163: if (disposition.equals(Part.ATTACHMENT)) {
164: mockBodyPart.getFileName();
165: modify().returnValue(filename);
166: }
167: return mockBodyPart;
168: }
169:
170: private Multipart setUpMultipart(final int partCount,
171: final int partIndex, final String disposition,
172: final String expectedFilename) throws MessagingException {
173: final Multipart mockMultipart = (Multipart) intercept(
174: Multipart.class, "mockMultipart");
175: mockMultipart.getCount();
176: modify().returnValue(partCount);
177: if (partCount > partIndex) {
178: final BodyPart mockBodyPart = setUpBodyPart(disposition,
179: expectedFilename);
180: mockMultipart.getBodyPart(partIndex);
181: modify().returnValue(mockBodyPart);
182: }
183: return mockMultipart;
184: }
185:
186: private EmailHelper prepareHelper(
187: final EmailStorePartFilename step, final int partIndex) {
188: step.setProperty(PROPERTY_NAME);
189: step.setMessageId(MESSAGE_ID);
190: step.setPartIndex(String.valueOf(partIndex));
191: final EmailHelper helper = (EmailHelper) mock(
192: EmailHelper.class, "helper");
193: step.setHelper(helper);
194: return helper;
195: }
196:
197: }
|