001: package com.canoo.webtest.plugins.emailtest;
002:
003: import java.io.IOException;
004: import java.util.StringTokenizer;
005: import java.util.Enumeration;
006:
007: import javax.mail.Message;
008: import javax.mail.Multipart;
009: import javax.mail.Part;
010: import javax.mail.BodyPart;
011: import javax.mail.MessagingException;
012: import javax.mail.Header;
013:
014: import com.canoo.webtest.steps.Step;
015:
016: /**
017: * Test for {@link EmailMessageStructureFilter}.
018: *
019: * @author Paul King, ASERT
020: */
021: public class EmailMessageStructureFilterTest extends BaseEmailTestCase {
022: private static final boolean DELETE_ON_EXIT = false;
023: private static final String LS = System
024: .getProperty("line.separator");
025: private static final String PLAIN_CONTENT_TYPE = "text/plain";
026: private static final String XML_CONTENT_TYPE = "text/xml";
027:
028: private static final String BODY_1 = "Simple Message Body";
029: private static final String BODY_2 = "Simple Message with one attachment"
030: + LS
031: + "begin 644 dummy.txt"
032: + LS
033: + "51'5M;7D@5&5X=\"!!='1A8VAM96YT " + LS + "end" + LS;
034: private static final String STRUCTURE_1 = "<message type=\"Simple\" contentType=\"text/plain\">"
035: + LS + "</message>";
036: private static final String STRUCTURE_2 = "<message type=\"Simple\" contentType=\"text/plain\">"
037: + LS
038: + " <part type=\"uuencoded\" filename=\"dummy.txt\"/>"
039: + LS + "</message>";
040: private static final String STRUCTURE_3 = "<message type=\"MIME\" contentType=\"text/plain\">"
041: + LS + "</message>";
042: private static final String STRUCTURE_4 = "<message type=\"MIME\" contentType=\"text/plain\">"
043: + LS
044: + " <part type=\"attachment\" filename=\"dummyFilename0\" contentType=\"text/xml\"/>"
045: + LS
046: + " <part type=\"inline\" contentType=\"text/plain\"/>"
047: + LS + "</message>";
048: private static final String STRUCTURE_5 = "<message type=\"Simple\" contentType=\"text/plain\">"
049: + LS
050: + " <header name=\"subject\" value=\"dummyHeaderValue0\"/>"
051: + LS
052: + " <header name=\"from\" value=\"dummyHeaderValue1\"/>"
053: + LS + "</message>";
054:
055: protected Step createStep() {
056: return new EmailMessageStructureFilter();
057: }
058:
059: public void testInvalidMessageId() {
060: final EmailMessageStructureFilter step = (EmailMessageStructureFilter) getStep();
061: step.getContext().put("EmailConfigInfo", null);
062: assertErrorOnExecute(step, "invalid messageId",
063: "Can't parse messageId parameter with value 'null' as an integer.");
064: step.setMessageId("non-integer");
065: assertErrorOnExecute(step, "invalid messageId",
066: "Can't parse messageId parameter with value 'non-integer' as an integer.");
067: }
068:
069: public void testNoHeaderSimpleMessage1() throws Exception {
070: checkNoHeaderSimpleMessage(BODY_1, STRUCTURE_1,
071: PLAIN_CONTENT_TYPE, null);
072: }
073:
074: public void testNoHeaderSimpleMessage2() throws Exception {
075: checkNoHeaderSimpleMessage(BODY_2, STRUCTURE_2,
076: "text/plain; charset='US-ASCII'", null);
077: }
078:
079: public void testNoHeaderSimpleMessage3() throws Exception {
080: checkNoHeaderSimpleMessage(BODY_1, STRUCTURE_5,
081: PLAIN_CONTENT_TYPE, "subject,from");
082: }
083:
084: private void checkNoHeaderSimpleMessage(final String body,
085: final String structure, final String contentType,
086: final String headers) throws Exception {
087: final EmailMessageStructureFilter step = (EmailMessageStructureFilter) getStep();
088: final EmailHelper helper = prepareHelper(step);
089: final Message mockMessage = setUpGetMessageExpectations(step,
090: helper, DELETE_ON_EXIT);
091: mockMessage.getContent();
092: modify().returnValue(body);
093: mockMessage.getContentType();
094: modify().returnValue(contentType);
095: if (headers != null) {
096: step.setHeaders(headers);
097: setUpHeaders(mockMessage, headers);
098: }
099: setUpMessageOperationFinaliseExpectations(helper,
100: DELETE_ON_EXIT);
101: startVerification();
102: executeStep(step);
103: assertEquals(structure, step.getContext().getCurrentResponse()
104: .getWebResponse().getContentAsString());
105: assertEquals(XML_CONTENT_TYPE, step.getContext()
106: .getCurrentResponse().getWebResponse().getContentType());
107: }
108:
109: private void setUpHeaders(final Message mockMessage,
110: final String headerStr) throws MessagingException {
111: final StringTokenizer tokens = new StringTokenizer(headerStr,
112: ", ");
113: final Header[] headers = new Header[tokens.countTokens() + 1];
114: for (int i = 0; i < headers.length - 1; i++) {
115: headers[i] = new Header(tokens.nextToken(),
116: "dummyHeaderValue" + i);
117: }
118: headers[headers.length - 1] = new Header("foo", "bar"); // add unused header
119: final Enumeration headerEnum = new Enumeration() {
120: private int fCount;
121:
122: public boolean hasMoreElements() {
123: return fCount < headers.length;
124: }
125:
126: public Object nextElement() {
127: return headers[fCount++];
128: }
129: };
130: mockMessage.getAllHeaders();
131: modify().returnValue(headerEnum);
132: }
133:
134: public void testIoException() throws Exception {
135: final EmailMessageStructureFilter step = (EmailMessageStructureFilter) getStep();
136: final EmailHelper helper = prepareHelper(step);
137: final Message mockMessage = setUpGetMessageExpectations(step,
138: helper, DELETE_ON_EXIT);
139: mockMessage.getContent();
140: modify().throwException(new IOException("dummyIoException"));
141: setUpMessageOperationFinaliseExpectations(helper,
142: DELETE_ON_EXIT);
143: startVerification();
144: assertFailOnExecute(step, "expected to fail",
145: "Error performing operation: Error processing email message: dummyIoException");
146: }
147:
148: public void testSaveParams() throws Exception {
149: final EmailMessageStructureFilter step = (EmailMessageStructureFilter) getStep();
150: final EmailHelper helper = prepareHelper(step);
151: step.setSaveResponse("MyResponse");
152: step.setSavePrefix("MyPrefix");
153: final Message mockMessage = setUpGetMessageExpectations(step,
154: helper, DELETE_ON_EXIT);
155: mockMessage.getContent();
156: modify().returnValue("dummy");
157: mockMessage.getContentType();
158: modify().returnValue("dummy");
159: setUpMessageOperationFinaliseExpectations(helper,
160: DELETE_ON_EXIT);
161: startVerification();
162: executeStep(step);
163: }
164:
165: public void testMultipart1() throws Exception {
166: checkMultipart(0, STRUCTURE_3);
167: }
168:
169: public void testMultipart2() throws Exception {
170: checkMultipart(2, STRUCTURE_4);
171: }
172:
173: private void checkMultipart(final int partCount,
174: final String expectedStructure) throws Exception {
175: final EmailMessageStructureFilter step = (EmailMessageStructureFilter) getStep();
176: final EmailHelper helper = prepareHelper(step);
177: final Message mockMessage = setUpGetMessageExpectations(step,
178: helper, DELETE_ON_EXIT);
179: final Multipart mockMultipart = setUpMultipart(partCount);
180: mockMessage.getContent();
181: modify().returnValue(mockMultipart);
182: mockMessage.getContentType();
183: modify().returnValue(PLAIN_CONTENT_TYPE);
184: setUpMessageOperationFinaliseExpectations(helper,
185: DELETE_ON_EXIT);
186: startVerification();
187: executeStep(step);
188: assertEquals(expectedStructure, step.getContext()
189: .getCurrentResponse().getWebResponse()
190: .getContentAsString());
191: assertEquals(XML_CONTENT_TYPE, step.getContext()
192: .getCurrentResponse().getWebResponse().getContentType());
193: }
194:
195: private BodyPart setUpBodyPart(final String disposition,
196: final String contentType, final String filename,
197: final String id) throws Exception {
198: final BodyPart mockBodyPart = (BodyPart) intercept(
199: BodyPart.class, id);
200: mockBodyPart.getDisposition();
201: modify().returnValue(disposition);
202: mockBodyPart.getContentType();
203: modify().returnValue(contentType);
204: if (disposition.equals(Part.ATTACHMENT)) {
205: mockBodyPart.getFileName();
206: modify().returnValue(filename);
207: }
208: return mockBodyPart;
209: }
210:
211: private static final String[] DISP = { Part.ATTACHMENT, Part.INLINE };
212: private static final String[] TYPE = { XML_CONTENT_TYPE,
213: PLAIN_CONTENT_TYPE };
214:
215: private Multipart setUpMultipart(final int partCount)
216: throws Exception {
217: final Multipart mockMultipart = (Multipart) intercept(
218: Multipart.class, "mockMultipart");
219: mockMultipart.getCount();
220: modify().returnValue(partCount);
221: for (int i = 0; i < partCount; i++) {
222: final Part part = setUpBodyPart(DISP[i % 2], TYPE[i % 2],
223: "dummyFilename" + i, "mockBodyPart" + i);
224: mockMultipart.getBodyPart(i);
225: modify().returnValue(part);
226: }
227: return mockMultipart;
228: }
229:
230: private EmailHelper prepareHelper(
231: final EmailMessageStructureFilter step) {
232: final EmailHelper helper = (EmailHelper) mock(
233: EmailHelper.class, "helper");
234: step.setHelper(helper);
235: return helper;
236: }
237:
238: }
|