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 org.apache.commons.lang.StringUtils;
09: import org.apache.log4j.Logger;
10:
11: import com.canoo.webtest.util.ConversionUtil;
12:
13: /**
14: * Stores the message's id into a property.
15: *
16: * @author Paul King, ASERT
17: * @webtest.step category="Email"
18: * name="emailStoreMessageId"
19: * description="Stores the message id (optionally matching specific criteria) of a particular message."
20: */
21: public class EmailStoreMessageId extends AbstractSelectStep {
22: private static final Logger LOG = Logger
23: .getLogger(EmailStoreMessageId.class);
24: private String fMessageIndex;
25:
26: public String getMessageIndex() {
27: return fMessageIndex;
28: }
29:
30: /**
31: * Sets the message index.
32: *
33: * @param index The message index
34: * @webtest.parameter required="no"
35: * default="0, the first message"
36: * description="The messageIndex if more than one message match."
37: */
38: public void setMessageIndex(final String index) {
39: fMessageIndex = index;
40: }
41:
42: /**
43: * Calculate the message id
44: *
45: * @param folder
46: * @return The extracted count value
47: */
48: protected String processContent(final Folder folder)
49: throws MessagingException {
50: final Message[] messages = retrieveMatchingMessages(folder);
51: if (StringUtils.isEmpty(fMessageIndex)) {
52: LOG
53: .info("Multiple matching messages found, using the first.");
54: }
55: if (messages.length == 0) {
56: throw new MessagingException(
57: "No messages matching criteria.");
58: }
59: final int messageIndex = ConversionUtil.convertToInt(
60: getMessageIndex(), 0);
61: if (messageIndex >= messages.length) {
62: throw new MessagingException("Invalid messageIndex '"
63: + getMessageIndex() + "', valid range is 0.."
64: + (messages.length - 1));
65: }
66: return String
67: .valueOf(messages[messageIndex].getMessageNumber());
68: }
69:
70: protected void verifyParameters() {
71: super .verifyParameters();
72: optionalIntegerParamCheck(getMessageIndex(), "messageIndex",
73: true);
74: }
75: }
|