001: package com.canoo.webtest.plugins.emailtest;
002:
003: import java.io.ByteArrayInputStream;
004: import java.io.IOException;
005:
006: import javax.mail.BodyPart;
007: import javax.mail.Message;
008: import javax.mail.Multipart;
009: import javax.mail.Part;
010:
011: import com.canoo.webtest.steps.Step;
012:
013: /**
014: * Test for {@link EmailMessageContentFilter}.
015: *
016: * @author Paul King, ASERT
017: */
018: public class EmailMessageContentFilterTest extends BaseEmailTestCase {
019: private static final String MESSAGE_ID = "123";
020: private static final boolean DELETE_ON_EXIT = false;
021: private static final String PLAIN_CONTENT_TYPE = "text/plain";
022: private static final String SIMPLE_BODY = "Dummy Email Message";
023: private static final String LS = System
024: .getProperty("line.separator");
025: private static final String MESSAGE_BODY = "Simple Message with one attachment"
026: + LS
027: + "begin 644 dummy.txt"
028: + LS
029: + "51'5M;7D@5&5X=\"!!='1A8VAM96YT " + LS + "end" + LS;
030: private static final String DONT_CARE_DISPOSITION = Part.INLINE;
031: private static final String DUMMY_CONTENT = "Dummy Content";
032:
033: protected Step createStep() {
034: return new EmailMessageContentFilter();
035: }
036:
037: public void testInvalidMessageId() {
038: final EmailMessageContentFilter step = (EmailMessageContentFilter) getStep();
039: step.getContext().put("EmailConfigInfo", null);
040: assertErrorOnExecute(step, "invalid messageId",
041: "Can't parse messageId parameter with value 'null' as an integer.");
042: step.setMessageId("non-integer");
043: assertErrorOnExecute(step, "invalid messageId",
044: "Can't parse messageId parameter with value 'non-integer' as an integer.");
045: }
046:
047: public void testInvalidPartIndex() {
048: final EmailMessageContentFilter step = (EmailMessageContentFilter) getStep();
049: step.getContext().put("EmailConfigInfo", null);
050: step.setMessageId(MESSAGE_ID);
051: step.setPartIndex("non-integer");
052: assertErrorOnExecute(step, "invalid partIndex",
053: "Can't parse partIndex parameter with value 'non-integer' as an integer.");
054: }
055:
056: public void testNoPartIndex() throws Exception {
057: final EmailMessageContentFilter step = (EmailMessageContentFilter) getStep();
058: final EmailHelper helper = prepareHelper(step, -1);
059: final Message mockMessage = setUpGetMessageExpectations(step,
060: helper, DELETE_ON_EXIT);
061: mockMessage.getInputStream();
062: modify().returnValue(
063: new ByteArrayInputStream(SIMPLE_BODY.getBytes()));
064: mockMessage.getContentType();
065: modify().returnValue(PLAIN_CONTENT_TYPE);
066: setUpMessageOperationFinaliseExpectations(helper,
067: DELETE_ON_EXIT);
068: startVerification();
069: executeStep(step);
070: assertEquals(SIMPLE_BODY, step.getContext()
071: .getCurrentResponse().getWebResponse()
072: .getContentAsString());
073: assertEquals(PLAIN_CONTENT_TYPE, step.getContext()
074: .getCurrentResponse().getWebResponse().getContentType());
075: }
076:
077: public void testNoPartIndexIoException() throws Exception {
078: final EmailMessageContentFilter step = (EmailMessageContentFilter) getStep();
079: final EmailHelper helper = prepareHelper(step, -1);
080: final Message mockMessage = setUpGetMessageExpectations(step,
081: helper, DELETE_ON_EXIT);
082: mockMessage.getInputStream();
083: modify().throwException(new IOException("dummyIoException"));
084: setUpMessageOperationFinaliseExpectations(helper,
085: DELETE_ON_EXIT);
086: startVerification();
087: assertFailOnExecute(step, "expected to fail",
088: "Error performing operation: Error extracting message: dummyIoException");
089: }
090:
091: public void testPartIndexIoException() throws Exception {
092: final EmailMessageContentFilter step = (EmailMessageContentFilter) getStep();
093: final EmailHelper helper = prepareHelper(step, 0);
094: final Message mockMessage = setUpGetMessageExpectations(step,
095: helper, DELETE_ON_EXIT);
096: mockMessage.getContent();
097: modify().throwException(new IOException("dummyIoException"));
098: setUpMessageOperationFinaliseExpectations(helper,
099: DELETE_ON_EXIT);
100: startVerification();
101: assertFailOnExecute(step, "expected to fail",
102: "Error performing operation: Error processing email message: dummyIoException");
103: }
104:
105: public void testSimpleNoAttachments() throws Exception {
106: checkSimpleAttachments(0, null,
107: "Simple Message with no attachments " + LS,
108: "Unable to find part with index 0.");
109: }
110:
111: public void testSimpleBadUudecode() throws Exception {
112: checkSimpleAttachments(
113: 0,
114: null,
115: "Simple Message with bad attachment" + LS
116: + "begin 123 but no end" + LS,
117: "Error performing operation: Error Uudecoding attachment: UUDecoder: Short Buffer.");
118: }
119:
120: public void testSimpleWithAttachmentNoContentType()
121: throws Exception {
122: checkSimpleAttachments(0, null, MESSAGE_BODY,
123: "Attribute 'contentType' must be supplied for simple messages.");
124: }
125:
126: public void testSimpleWithPartIndexTooLarge() throws Exception {
127: checkSimpleAttachments(1, PLAIN_CONTENT_TYPE, MESSAGE_BODY,
128: "Unable to find part with index 1.");
129: }
130:
131: public void testSimpleWithAttachment() throws Exception {
132: final Step step = checkSimpleAttachments(0, PLAIN_CONTENT_TYPE,
133: MESSAGE_BODY, null);
134: assertEquals("Dummy Text Attachment", step.getContext()
135: .getCurrentResponse().getWebResponse()
136: .getContentAsString());
137: assertEquals(PLAIN_CONTENT_TYPE, step.getContext()
138: .getCurrentResponse().getWebResponse().getContentType());
139: }
140:
141: private EmailMessageContentFilter checkSimpleAttachments(
142: final int partIndex, final String contentType,
143: final String messageBody, final String failMessage)
144: throws Exception {
145: final EmailMessageContentFilter step = (EmailMessageContentFilter) getStep();
146: step.setContentType(contentType);
147: final EmailHelper helper = prepareHelper(step, partIndex);
148: final Message mockMessage = setUpGetMessageExpectations(step,
149: helper, DELETE_ON_EXIT);
150: mockMessage.getContent();
151: modify().returnValue(messageBody);
152: setUpMessageOperationFinaliseExpectations(helper,
153: DELETE_ON_EXIT);
154: startVerification();
155: if (failMessage != null) {
156: assertFailOnExecute(step, "expected to fail", failMessage);
157: } else {
158: executeStep(step);
159: }
160: return step;
161: }
162:
163: public void testMultipart1() throws Exception {
164: checkMultipart(0, 0, "PartIndex too large.", null, null, null);
165: }
166:
167: public void testMultipart2() throws Exception {
168: checkMultipart(
169: 1,
170: 0,
171: "Error performing operation: Actual contentType of 'text/plain' did not match expected contentType of 'text/html'",
172: DONT_CARE_DISPOSITION, "text/html", PLAIN_CONTENT_TYPE);
173: }
174:
175: public void testMultipart3() throws Exception {
176: final Step step = checkMultipart(1, 0, null, Part.ATTACHMENT,
177: null, PLAIN_CONTENT_TYPE);
178: assertEquals(DUMMY_CONTENT, step.getContext()
179: .getCurrentResponse().getWebResponse()
180: .getContentAsString());
181: assertEquals(PLAIN_CONTENT_TYPE, step.getContext()
182: .getCurrentResponse().getWebResponse().getContentType());
183: }
184:
185: public void testMultipart4() throws Exception {
186: final Step step = checkMultipart(1, 0, null, Part.INLINE, null,
187: PLAIN_CONTENT_TYPE);
188: assertEquals(DUMMY_CONTENT, step.getContext()
189: .getCurrentResponse().getWebResponse()
190: .getContentAsString());
191: assertEquals(PLAIN_CONTENT_TYPE, step.getContext()
192: .getCurrentResponse().getWebResponse().getContentType());
193: }
194:
195: private EmailMessageContentFilter checkMultipart(
196: final int partCount, final int partIndex,
197: final String failureMessage, final String disposition,
198: final String expectedContentType,
199: final String actualContentType) throws Exception {
200: final EmailMessageContentFilter step = (EmailMessageContentFilter) getStep();
201: step.setContentType(expectedContentType);
202: final EmailHelper helper = prepareHelper(step, partIndex);
203: final Message mockMessage = setUpGetMessageExpectations(step,
204: helper, DELETE_ON_EXIT);
205: final Multipart mockMultipart = setUpMultipart(partCount,
206: partIndex, disposition, expectedContentType,
207: actualContentType);
208: mockMessage.getContent();
209: modify().returnValue(mockMultipart);
210: setUpMessageOperationFinaliseExpectations(helper,
211: DELETE_ON_EXIT);
212: startVerification();
213: if (failureMessage != null) {
214: assertFailOnExecute(step, "expected to fail",
215: failureMessage);
216: } else {
217: executeStep(step);
218: }
219: return step;
220: }
221:
222: public void testMultipartWithIoException() throws Exception {
223: final EmailMessageContentFilter step = (EmailMessageContentFilter) getStep();
224: final EmailHelper helper = prepareHelper(step, 0);
225: final Message mockMessage = setUpGetMessageExpectations(step,
226: helper, DELETE_ON_EXIT);
227: final Multipart mockMultipart = (Multipart) intercept(
228: Multipart.class, "mockMultipart");
229: mockMultipart.getCount();
230: modify().throwException(
231: new IOException("Dummy Messaging Exception"));
232: mockMessage.getContent();
233: modify().returnValue(mockMultipart);
234: setUpMessageOperationFinaliseExpectations(helper,
235: DELETE_ON_EXIT);
236: startVerification();
237: assertFailOnExecute(step, "expected to fail",
238: "Error performing operation: Error extracting part: Dummy Messaging Exception");
239: }
240:
241: private BodyPart setUpBodyPart(final String disposition,
242: final String expectedContentType,
243: final String actualContentType) throws Exception {
244: final BodyPart mockBodyPart = (BodyPart) mock(BodyPart.class,
245: "mockBodyPart");
246: mockBodyPart.getContentType();
247: modify().returnValue(actualContentType);
248: if (expectedContentType == null
249: || expectedContentType.equals(actualContentType)) {
250: mockBodyPart.getDisposition();
251: modify().returnValue(disposition);
252: mockBodyPart.getFileName();
253: mockBodyPart.getInputStream();
254: modify().returnValue(
255: new ByteArrayInputStream(DUMMY_CONTENT.getBytes()));
256: }
257: return mockBodyPart;
258: }
259:
260: private Multipart setUpMultipart(final int partCount,
261: final int partIndex, final String disposition,
262: final String expectedContentType,
263: final String actualContentType) throws Exception {
264: final Multipart mockMultipart = (Multipart) intercept(
265: Multipart.class, "mockMultipart");
266: mockMultipart.getCount();
267: modify().returnValue(partCount);
268: if (partCount > partIndex) {
269: final BodyPart mockBodyPart = setUpBodyPart(disposition,
270: expectedContentType, actualContentType);
271: mockMultipart.getBodyPart(partIndex);
272: modify().returnValue(mockBodyPart);
273: }
274: return mockMultipart;
275: }
276:
277: private EmailHelper prepareHelper(
278: final EmailMessageContentFilter step, final int partIndex) {
279: if (partIndex != -1) {
280: step.setPartIndex(String.valueOf(partIndex));
281: }
282: final EmailHelper helper = (EmailHelper) mock(
283: EmailHelper.class, "helper");
284: step.setHelper(helper);
285: return helper;
286: }
287:
288: }
|