01: // Copyright © 2006-2007 ASERT. Released under the Canoo Webtest license.
02: package com.canoo.webtest.plugins.emailtest;
03:
04: import javax.mail.Folder;
05: import javax.mail.Message;
06: import javax.mail.MessagingException;
07:
08: import com.canoo.webtest.engine.StepFailedException;
09: import com.canoo.webtest.util.ConversionUtil;
10:
11: /**
12: * Abstract class used by email steps that perform an operation on a single message.
13: *
14: * @author Paul King
15: */
16: public abstract class AbstractMessageOperationStep extends
17: AbstractBaseStep implements ISingleMessageStep {
18: private String fMessageId;
19: private boolean fDeleteOnExit;
20:
21: protected AbstractMessageOperationStep(final boolean deleteOnExit) {
22: fDeleteOnExit = deleteOnExit;
23: }
24:
25: public String getMessageId() {
26: return fMessageId;
27: }
28:
29: /**
30: * Sets the message id.
31: *
32: * @param id The id of the message of interest
33: * @webtest.parameter
34: * required="yes"
35: * description="The message id."
36: */
37: public void setMessageId(final String id) {
38: fMessageId = id;
39: }
40:
41: protected abstract void performOperation(Message message)
42: throws MessagingException;
43:
44: public void doExecute() throws Exception {
45: final EmailConfigInfo configInfo = (EmailConfigInfo) getContext()
46: .get("EmailConfigInfo");
47: Folder folder = null;
48: try {
49: folder = getHelper().getInboxFolder(configInfo);
50: final int id = ConversionUtil.convertToInt(getMessageId(),
51: 0);
52: final Message message = getHelper().getMessage(id, folder);
53: if (message == null) {
54: throw new StepFailedException(
55: "Could not retrieve message.", this );
56: }
57: performOperation(message);
58: } catch (MessagingException e) {
59: throw new StepFailedException(
60: "Error performing operation: " + e.getMessage(),
61: this );
62: } finally {
63: getHelper().logout(folder, fDeleteOnExit);
64: }
65: }
66:
67: /**
68: * Verifies the parameters
69: */
70: protected void verifyParameters() {
71: super .verifyParameters();
72: integerParamCheck(fMessageId, "messageId", true);
73: }
74:
75: }
|