diff --git a/org.springframework.integration.mail/src/main/java/org/springframework/integration/mail/AbstractMailReceiver.java b/org.springframework.integration.mail/src/main/java/org/springframework/integration/mail/AbstractMailReceiver.java index e80dba18c8..8c9aae5f6a 100755 --- a/org.springframework.integration.mail/src/main/java/org/springframework/integration/mail/AbstractMailReceiver.java +++ b/org.springframework.integration.mail/src/main/java/org/springframework/integration/mail/AbstractMailReceiver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2010 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -49,6 +49,8 @@ public abstract class AbstractMailReceiver implements MailReceiver, DisposableBe private final URLName url; + private volatile String protocol; + private volatile int maxFetchSize = -1; private volatile Session session; @@ -57,6 +59,8 @@ public abstract class AbstractMailReceiver implements MailReceiver, DisposableBe private volatile Folder folder; + private volatile boolean shouldDeleteMessages = false; + private volatile Properties javaMailProperties = new Properties(); protected volatile boolean initialized; @@ -65,31 +69,90 @@ public abstract class AbstractMailReceiver implements MailReceiver, DisposableBe private Authenticator javaMailAuthenticator; + + public AbstractMailReceiver() { + this.url = null; + } + public AbstractMailReceiver(URLName urlName) { Assert.notNull(urlName, "urlName must not be null"); this.url = urlName; + this.shouldDeleteMessages = urlName.getProtocol().startsWith("pop3"); } public AbstractMailReceiver(String url) { - Assert.notNull(url, "url must not be null"); - this.url = new URLName(url); + if (url != null) { + this.url = new URLName(url); + this.shouldDeleteMessages = this.url.getProtocol().startsWith("pop3"); + } + else { + this.url = null; + } } + + public void setProtocol(String protocol) { + if (this.url != null) { + Assert.isTrue(this.url.getProtocol().equals(protocol), + "The 'protocol' does not match that provided by the Store URI."); + } + this.protocol = protocol; + } + + /** + * Set the {@link Session}. Otherwise, the Session will be created by invocation of + * {@link Session#getInstance(Properties)} or {@link Session#getInstance(Properties, Authenticator)}. + * + * @see #setJavaMailProperties(Properties) + * @see #setJavaMailAuthenticator(Authenticator) + */ + public void setSession(Session session) { + Assert.notNull(session, "Session must not be null"); + this.session = session; + } + + /** + * A new {@link Session} will be created with these properties (and the JavaMailAuthenticator if provided). + * Use either this method or {@link #setSession}, but not both. + * + * @see #setJavaMailAuthenticator(Authenticator) + * @see #setSession(Session) + */ public void setJavaMailProperties(Properties javaMailProperties) { this.javaMailProperties = javaMailProperties; } /** - * Optional, sets the Authenticator to be used to obtain a session + * Optional, sets the Authenticator to be used to obtain a session. This will not be used if + * {@link AbstractMailReceiver#setSession} has been used to configure the {@link Session} directly. + * + * @see #setSession(Session) */ public void setJavaMailAuthenticator(Authenticator javaMailAuthenticator) { this.javaMailAuthenticator = javaMailAuthenticator; } + /** + * Specify the maximum number of Messages to fetch per call to {@link #receive()}. + */ public void setMaxFetchSize(int maxFetchSize) { this.maxFetchSize = maxFetchSize; } + /** + * Specify whether mail messages should be deleted after retrieval. + */ + public void setShouldDeleteMessages(boolean shouldDeleteMessages) { + this.shouldDeleteMessages = shouldDeleteMessages; + } + + /** + * Indicates whether the mail messages should be deleted after being received. + */ + protected boolean shouldDeleteMessages() { + return this.shouldDeleteMessages; + } + protected Folder getFolder() { return this.folder; } @@ -99,18 +162,25 @@ public abstract class AbstractMailReceiver implements MailReceiver, DisposableBe */ protected abstract Message[] searchForNewMessages() throws MessagingException; - /** - * Subclasses must implement this method to indicate whether the mail - * messages should be deleted after being received. - */ - protected abstract boolean shouldDeleteMessages(); - private void openSession() throws MessagingException { if (this.session == null) { - this.session = Session.getInstance(this.javaMailProperties, this.javaMailAuthenticator); + if (this.javaMailAuthenticator != null) { + this.session = Session.getInstance(this.javaMailProperties, this.javaMailAuthenticator); + } + else { + this.session = Session.getInstance(this.javaMailProperties); + } } if (this.store == null) { - this.store = this.session.getStore(this.url); + if (this.url != null) { + this.store = this.session.getStore(this.url); + } + else if (this.protocol != null) { + this.store = this.session.getStore(this.protocol); + } + else { + this.store = this.session.getStore(); + } } if (!this.store.isConnected()) { if (logger.isDebugEnabled()) { diff --git a/org.springframework.integration.mail/src/main/java/org/springframework/integration/mail/ImapMailReceiver.java b/org.springframework.integration.mail/src/main/java/org/springframework/integration/mail/ImapMailReceiver.java index 8dba528024..db65c081d7 100755 --- a/org.springframework.integration.mail/src/main/java/org/springframework/integration/mail/ImapMailReceiver.java +++ b/org.springframework.integration.mail/src/main/java/org/springframework/integration/mail/ImapMailReceiver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2010 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -44,31 +44,26 @@ import com.sun.mail.imap.IMAPFolder; */ public class ImapMailReceiver extends AbstractMailReceiver { - private volatile boolean shouldDeleteMessages = true; - private final MessageCountListener messageCountListener = new SimpleMessageCountListener(); + public ImapMailReceiver() { + super(); + this.setProtocol("imap"); + } + public ImapMailReceiver(String url) { super(url); - Assert.isTrue(url.toLowerCase().startsWith("imap"), - "URL must start with 'imap' for the IMAP Mail receiver."); + if (url != null) { + Assert.isTrue(url.toLowerCase().startsWith("imap"), + "URL must start with 'imap' for the IMAP Mail receiver."); + } + else { + this.setProtocol("imap"); + } } - /** - * Specify whether mail messages should be deleted after retrieval. - * The default is true. - */ - public void setShouldDeleteMessages(boolean shouldDeleteMessages) { - this.shouldDeleteMessages = shouldDeleteMessages; - } - - @Override - protected boolean shouldDeleteMessages() { - return this.shouldDeleteMessages; - } - /** * This method is unique to the IMAP receiver and only works if IMAP IDLE * is supported (see RFC 2177 for more detail). diff --git a/org.springframework.integration.mail/src/main/java/org/springframework/integration/mail/Pop3MailReceiver.java b/org.springframework.integration.mail/src/main/java/org/springframework/integration/mail/Pop3MailReceiver.java index 8bd4b6876d..0fa3df95a0 100755 --- a/org.springframework.integration.mail/src/main/java/org/springframework/integration/mail/Pop3MailReceiver.java +++ b/org.springframework.integration.mail/src/main/java/org/springframework/integration/mail/Pop3MailReceiver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2010 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,9 +32,19 @@ import org.springframework.util.Assert; */ public class Pop3MailReceiver extends AbstractMailReceiver { + public Pop3MailReceiver() { + super(); + this.setProtocol("pop3"); + } + public Pop3MailReceiver(String url) { super(url); - Assert.isTrue(url.startsWith("pop3"), "url must start with 'pop3'"); + if (url != null) { + Assert.isTrue(url.startsWith("pop3"), "url must start with 'pop3'"); + } + else { + this.setProtocol("pop3"); + } } public Pop3MailReceiver(String host, String username, String password) { @@ -47,14 +57,6 @@ public class Pop3MailReceiver extends AbstractMailReceiver { } - /** - * POP3 is unable to detect new Messages, so this always returns true. - */ - @Override - protected final boolean shouldDeleteMessages() { - return true; - } - @Override protected Message[] searchForNewMessages() throws MessagingException { int messageCount = this.getFolder().getMessageCount(); diff --git a/org.springframework.integration.mail/src/main/java/org/springframework/integration/mail/config/ImapIdleChannelAdapterParser.java b/org.springframework.integration.mail/src/main/java/org/springframework/integration/mail/config/ImapIdleChannelAdapterParser.java index b9a1a0f711..8afa6c3e2c 100644 --- a/org.springframework.integration.mail/src/main/java/org/springframework/integration/mail/config/ImapIdleChannelAdapterParser.java +++ b/org.springframework.integration.mail/src/main/java/org/springframework/integration/mail/config/ImapIdleChannelAdapterParser.java @@ -24,6 +24,7 @@ import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; import org.springframework.beans.factory.xml.ParserContext; import org.springframework.integration.config.xml.IntegrationNamespaceUtils; import org.springframework.util.Assert; +import org.springframework.util.StringUtils; /** * Parser for the <imap-idle-channel-adapter> element in the 'mail' namespace. @@ -58,12 +59,25 @@ public class ImapIdleChannelAdapterParser extends AbstractSingleBeanDefinitionPa } private BeanDefinition parseImapMailReceiver(Element element, ParserContext parserContext) { + BeanDefinitionBuilder receiverBuilder = BeanDefinitionBuilder.genericBeanDefinition(BASE_PACKAGE + ".ImapMailReceiver"); + Object source = parserContext.extractSource(element); String uri = element.getAttribute("store-uri"); - Assert.hasText(uri, "the 'store-uri' attribute is required"); - BeanDefinitionBuilder receiverBuilder = BeanDefinitionBuilder.genericBeanDefinition( - BASE_PACKAGE + ".ImapMailReceiver"); - receiverBuilder.addConstructorArgValue(uri); - IntegrationNamespaceUtils.setReferenceIfAttributeDefined(receiverBuilder, element, "java-mail-properties"); + if (StringUtils.hasText(uri)) { + receiverBuilder.addConstructorArgValue(uri); + } + String session = element.getAttribute("session"); + if (StringUtils.hasText(session)) { + if (element.hasAttribute("java-mail-properties") || element.hasAttribute("authenticator")) { + parserContext.getReaderContext().error("Neither 'java-mail-properties' nor 'authenticator' " + + "references are allowed when a 'session' reference has been provided.", source); + } + receiverBuilder.addPropertyReference("session", session); + } + else { + IntegrationNamespaceUtils.setReferenceIfAttributeDefined(receiverBuilder, element, "java-mail-properties"); + IntegrationNamespaceUtils.setReferenceIfAttributeDefined(receiverBuilder, element, "authenticator", "javaMailAuthenticator"); + } + IntegrationNamespaceUtils.setValueIfAttributeDefined(receiverBuilder, element, "max-fetch-size"); receiverBuilder.addPropertyValue("shouldDeleteMessages", element.getAttribute("should-delete-messages")); return receiverBuilder.getBeanDefinition(); } diff --git a/org.springframework.integration.mail/src/main/java/org/springframework/integration/mail/config/MailInboundChannelAdapterParser.java b/org.springframework.integration.mail/src/main/java/org/springframework/integration/mail/config/MailInboundChannelAdapterParser.java index 80bb6b71c5..53c3d85f69 100644 --- a/org.springframework.integration.mail/src/main/java/org/springframework/integration/mail/config/MailInboundChannelAdapterParser.java +++ b/org.springframework.integration.mail/src/main/java/org/springframework/integration/mail/config/MailInboundChannelAdapterParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2010 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,11 +18,12 @@ package org.springframework.integration.mail.config; import org.w3c.dom.Element; +import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionReaderUtils; import org.springframework.beans.factory.xml.ParserContext; import org.springframework.integration.config.xml.AbstractPollingInboundChannelAdapterParser; -import org.springframework.util.Assert; +import org.springframework.integration.config.xml.IntegrationNamespaceUtils; import org.springframework.util.StringUtils; import org.springframework.util.xml.DomUtils; @@ -42,30 +43,44 @@ public class MailInboundChannelAdapterParser extends AbstractPollingInboundChann protected String parseSource(Element element, ParserContext parserContext) { BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition( BASE_PACKAGE + ".MailReceivingMessageSource"); - builder.addConstructorArgReference(this.parseMailReceiver(element, parserContext)); + builder.addConstructorArgValue(this.parseMailReceiver(element, parserContext)); return BeanDefinitionReaderUtils.registerWithGeneratedName( builder.getBeanDefinition(), parserContext.getRegistry()); } - private String parseMailReceiver(Element element, ParserContext parserContext) { - String storeUri = element.getAttribute("store-uri"); - Assert.hasText(storeUri, "the 'store-uri' attribute is required"); + private BeanDefinition parseMailReceiver(Element element, ParserContext parserContext) { BeanDefinitionBuilder receiverBuilder = BeanDefinitionBuilder.genericBeanDefinition( BASE_PACKAGE + ".config.MailReceiverFactoryBean"); - receiverBuilder.addPropertyValue("storeUri", storeUri); - String propertiesRef = element.getAttribute("java-mail-properties"); - if (StringUtils.hasText(propertiesRef)) { - receiverBuilder.addPropertyReference("javaMailProperties", propertiesRef); + Object source = parserContext.extractSource(element); + IntegrationNamespaceUtils.setValueIfAttributeDefined(receiverBuilder, element, "store-uri"); + IntegrationNamespaceUtils.setValueIfAttributeDefined(receiverBuilder, element, "protocol"); + String session = element.getAttribute("session"); + if (StringUtils.hasText(session)) { + if (element.hasAttribute("java-mail-properties") || element.hasAttribute("authenticator")) { + parserContext.getReaderContext().error("Neither 'java-mail-properties' nor 'authenticator' " + + "references are allowed when a 'session' reference has been provided.", source); + } + receiverBuilder.addPropertyReference("session", session); } - Element pollerElement = DomUtils.getChildElementByTagName(element, "poller"); - if (pollerElement != null) { - String mmpp = pollerElement.getAttribute("max-messages-per-poll"); - if (StringUtils.hasText(mmpp)) { - receiverBuilder.addPropertyValue("maxFetchSize", mmpp); + else { + IntegrationNamespaceUtils.setReferenceIfAttributeDefined(receiverBuilder, element, "java-mail-properties"); + IntegrationNamespaceUtils.setReferenceIfAttributeDefined(receiverBuilder, element, "authenticator"); + } + String maxFetchSize = element.getAttribute("max-fetch-size"); + if (StringUtils.hasText(maxFetchSize)) { + receiverBuilder.addPropertyValue("maxFetchSize", maxFetchSize); + } + else { + Element pollerElement = DomUtils.getChildElementByTagName(element, "poller"); + if (pollerElement != null) { + String mmpp = pollerElement.getAttribute("max-messages-per-poll"); + if (StringUtils.hasText(mmpp)) { + receiverBuilder.addPropertyValue("maxFetchSize", mmpp); + } } } - return BeanDefinitionReaderUtils.registerWithGeneratedName( - receiverBuilder.getBeanDefinition(), parserContext.getRegistry()); + receiverBuilder.addPropertyValue("shouldDeleteMessages", element.getAttribute("should-delete-messages")); + return receiverBuilder.getBeanDefinition(); } } diff --git a/org.springframework.integration.mail/src/main/java/org/springframework/integration/mail/config/MailReceiverFactoryBean.java b/org.springframework.integration.mail/src/main/java/org/springframework/integration/mail/config/MailReceiverFactoryBean.java index 8c8b4dae3d..75e6c53fc0 100644 --- a/org.springframework.integration.mail/src/main/java/org/springframework/integration/mail/config/MailReceiverFactoryBean.java +++ b/org.springframework.integration.mail/src/main/java/org/springframework/integration/mail/config/MailReceiverFactoryBean.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2010 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,6 +18,10 @@ package org.springframework.integration.mail.config; import java.util.Properties; +import javax.mail.Authenticator; +import javax.mail.Session; +import javax.mail.URLName; + import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.FactoryBean; import org.springframework.integration.mail.AbstractMailReceiver; @@ -25,6 +29,7 @@ import org.springframework.integration.mail.ImapMailReceiver; import org.springframework.integration.mail.MailReceiver; import org.springframework.integration.mail.Pop3MailReceiver; import org.springframework.util.Assert; +import org.springframework.util.StringUtils; /** * @author Mark Fisher @@ -34,10 +39,22 @@ public class MailReceiverFactoryBean implements FactoryBean, DisposableBean { private volatile String storeUri; + private volatile String protocol; + + private volatile Session session; + private volatile MailReceiver receiver; private volatile Properties javaMailProperties; + private volatile Authenticator authenticator; + + /** + * Indicates whether retrieved messages should be deleted from the server. + * This value will be null unless explicitly configured. + */ + private volatile Boolean shouldDeleteMessages = null; + private volatile int maxFetchSize = 1; @@ -45,10 +62,26 @@ public class MailReceiverFactoryBean implements FactoryBean, DisposableBean { this.storeUri = storeUri; } + public void setProtocol(String protocol) { + this.protocol = protocol; + } + + public void setSession(Session session) { + this.session = session; + } + public void setJavaMailProperties(Properties javaMailProperties) { this.javaMailProperties = javaMailProperties; } + public void setAuthenticator(Authenticator authenticator) { + this.authenticator = authenticator; + } + + public void setShouldDeleteMessages(Boolean shouldDeleteMessages) { + this.shouldDeleteMessages = shouldDeleteMessages; + } + public void setMaxFetchSize(int maxFetchSize) { this.maxFetchSize = maxFetchSize; } @@ -68,15 +101,45 @@ public class MailReceiverFactoryBean implements FactoryBean, DisposableBean { return true; } + private void verifyProtocol() { + if (StringUtils.hasText(this.storeUri)) { + URLName urlName = new URLName(this.storeUri); + if (this.protocol == null) { + this.protocol = urlName.getProtocol(); + } + else { + Assert.isTrue(this.protocol.equals(urlName.getProtocol()), + "The provided 'protocol' does not match that in the 'storeUri'."); + } + } + else { + Assert.hasText(this.protocol, "Either the 'storeUri' or 'protocol' is required."); + } + Assert.hasText(this.protocol, "Unable to resolve protocol."); + } + private MailReceiver createReceiver() { - Assert.hasText(this.storeUri, "the store URI is required"); - boolean isPop3 = this.storeUri.toLowerCase().startsWith("pop3"); - boolean isImap = this.storeUri.toLowerCase().startsWith("imap"); + this.verifyProtocol(); + boolean isPop3 = this.protocol.toLowerCase().startsWith("pop3"); + boolean isImap = this.protocol.toLowerCase().startsWith("imap"); Assert.isTrue(isPop3 || isImap, "the store URI must begin with 'pop3' or 'imap'"); AbstractMailReceiver receiver = isPop3 ? new Pop3MailReceiver(this.storeUri) : new ImapMailReceiver(this.storeUri); + if (this.session != null) { + Assert.isNull(this.javaMailProperties, "JavaMail Properties are not allowed when a Session has been provided."); + Assert.isNull(this.authenticator, "A JavaMail Authenticator is not allowed when a Session has been provied."); + receiver.setSession(this.session); + } if (this.javaMailProperties != null) { receiver.setJavaMailProperties(this.javaMailProperties); } + if (this.authenticator != null) { + receiver.setJavaMailAuthenticator(this.authenticator); + } + if (this.shouldDeleteMessages != null) { + // always set the value if configured explicitly + // otherwise, the default is true for POP3 but false for IMAP + receiver.setShouldDeleteMessages(this.shouldDeleteMessages); + } receiver.setMaxFetchSize(this.maxFetchSize); return receiver; } diff --git a/org.springframework.integration.mail/src/main/resources/org/springframework/integration/mail/config/spring-integration-mail-1.0.xsd b/org.springframework.integration.mail/src/main/resources/org/springframework/integration/mail/config/spring-integration-mail-1.0.xsd index 2b3c99c3df..0eb680692b 100644 --- a/org.springframework.integration.mail/src/main/resources/org/springframework/integration/mail/config/spring-integration-mail-1.0.xsd +++ b/org.springframework.integration.mail/src/main/resources/org/springframework/integration/mail/config/spring-integration-mail-1.0.xsd @@ -63,85 +63,146 @@ - - - + + Defines an inbound Channel Adapter that polls a mailbox for mail messages. - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + - - - + + Defines an IMAP IDLE channel adapter. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + Reference to a TaskExecutor for sending inbound Mail to a MessageChannel asynchronously. + + + + + + + + + + + + + + + + A unique identifier for this Channel Adapter. + + + + + + + Reference for the MessageChannel to which this adapter will send Messages. + + + + + + + + + + + + + + + + + Specify the javax.mail.Session reference. + NOTE: if this is provided, then 'java-mail-properties' should not be. + + + + + + + + + + + + Reference to a 'java.util.Properties' instance with settings for the JavaMail Session. + NOTE: if this is provided, then 'session' should not be. + + + + + + + + + + + + Specify the javax.mail.Authenticator. + NOTE: if this is provided, then 'session' should not be. + + + + + + + + + + + + + + + + + + + + + + Specify whether this endpoint should be started automatically. The default is TRUE. + + + + diff --git a/org.springframework.integration.mail/src/test/java/org/springframework/integration/mail/config/InboundChannelAdapterParserTests-context.xml b/org.springframework.integration.mail/src/test/java/org/springframework/integration/mail/config/InboundChannelAdapterParserTests-context.xml new file mode 100644 index 0000000000..5d2ebe5ac8 --- /dev/null +++ b/org.springframework.integration.mail/src/test/java/org/springframework/integration/mail/config/InboundChannelAdapterParserTests-context.xml @@ -0,0 +1,106 @@ + + + + + + + + + + + + + + + + + + + + true + false + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + bar + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/org.springframework.integration.mail/src/test/java/org/springframework/integration/mail/config/InboundChannelAdapterParserTests-invalidContext.xml b/org.springframework.integration.mail/src/test/java/org/springframework/integration/mail/config/InboundChannelAdapterParserTests-invalidContext.xml new file mode 100644 index 0000000000..3f3e56b120 --- /dev/null +++ b/org.springframework.integration.mail/src/test/java/org/springframework/integration/mail/config/InboundChannelAdapterParserTests-invalidContext.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + diff --git a/org.springframework.integration.mail/src/test/java/org/springframework/integration/mail/config/InboundChannelAdapterParserTests.java b/org.springframework.integration.mail/src/test/java/org/springframework/integration/mail/config/InboundChannelAdapterParserTests.java new file mode 100644 index 0000000000..7fdacb6a03 --- /dev/null +++ b/org.springframework.integration.mail/src/test/java/org/springframework/integration/mail/config/InboundChannelAdapterParserTests.java @@ -0,0 +1,277 @@ +/* + * Copyright 2002-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.integration.mail.config; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import javax.mail.Authenticator; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.xml.sax.SAXParseException; + +import org.springframework.beans.DirectFieldAccessor; +import org.springframework.beans.factory.BeanDefinitionStoreException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.integration.mail.AbstractMailReceiver; +import org.springframework.integration.mail.ImapIdleChannelAdapter; +import org.springframework.integration.mail.ImapMailReceiver; +import org.springframework.integration.mail.Pop3MailReceiver; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * @author Mark Fisher + * @since 1.0.5 + */ +@ContextConfiguration +@RunWith(SpringJUnit4ClassRunner.class) +public class InboundChannelAdapterParserTests { + + @Autowired + private ApplicationContext context; + + + //==================== INT-982 ===================== + + @Test + public void pop3ShouldDeleteTrue() { + AbstractMailReceiver receiver = this.getReceiver("pop3ShouldDeleteTrue"); + assertEquals(Pop3MailReceiver.class, receiver.getClass()); + Boolean value = (Boolean) new DirectFieldAccessor(receiver).getPropertyValue("shouldDeleteMessages"); + assertTrue(value); + } + + @Test + public void pop3ShouldDeleteFalse() { + AbstractMailReceiver receiver = this.getReceiver("pop3ShouldDeleteFalse"); + assertEquals(Pop3MailReceiver.class, receiver.getClass()); + Boolean value = (Boolean) new DirectFieldAccessor(receiver).getPropertyValue("shouldDeleteMessages"); + assertFalse(value); + } + + @Test + public void imapShouldDeleteTrue() { + AbstractMailReceiver receiver = this.getReceiver("imapShouldDeleteTrue"); + assertEquals(ImapMailReceiver.class, receiver.getClass()); + Boolean value = (Boolean) new DirectFieldAccessor(receiver).getPropertyValue("shouldDeleteMessages"); + assertTrue(value); + } + + @Test + public void imapShouldDeleteFalse() { + AbstractMailReceiver receiver = this.getReceiver("imapShouldDeleteFalse"); + assertEquals(ImapMailReceiver.class, receiver.getClass()); + Boolean value = (Boolean) new DirectFieldAccessor(receiver).getPropertyValue("shouldDeleteMessages"); + assertFalse(value); + } + + + //==================== INT-1158 ==================== + + @Test + public void pop3ShouldDeleteTrueProperty() { + AbstractMailReceiver receiver = this.getReceiver("pop3ShouldDeleteTrueProperty"); + assertEquals(Pop3MailReceiver.class, receiver.getClass()); + Boolean value = (Boolean) new DirectFieldAccessor(receiver).getPropertyValue("shouldDeleteMessages"); + assertTrue(value); + } + + @Test + public void pop3ShouldDeleteFalseProperty() { + AbstractMailReceiver receiver = this.getReceiver("pop3ShouldDeleteFalseProperty"); + assertEquals(Pop3MailReceiver.class, receiver.getClass()); + Boolean value = (Boolean) new DirectFieldAccessor(receiver).getPropertyValue("shouldDeleteMessages"); + assertFalse(value); + } + + @Test + public void imapShouldDeleteTrueProperty() { + AbstractMailReceiver receiver = this.getReceiver("imapShouldDeleteTrueProperty"); + assertEquals(ImapMailReceiver.class, receiver.getClass()); + Boolean value = (Boolean) new DirectFieldAccessor(receiver).getPropertyValue("shouldDeleteMessages"); + assertTrue(value); + } + + @Test + public void imapShouldDeleteFalseProperty() { + AbstractMailReceiver receiver = this.getReceiver("imapShouldDeleteFalseProperty"); + assertEquals(ImapMailReceiver.class, receiver.getClass()); + Boolean value = (Boolean) new DirectFieldAccessor(receiver).getPropertyValue("shouldDeleteMessages"); + assertFalse(value); + } + + + //==================== INT-1159 ==================== + + @Test + public void pop3WithAuthenticator() { + AbstractMailReceiver receiver = this.getReceiver("pop3WithAuthenticator"); + assertEquals(Pop3MailReceiver.class, receiver.getClass()); + Object authenticator = new DirectFieldAccessor(receiver).getPropertyValue("javaMailAuthenticator"); + assertNotNull(authenticator); + assertEquals(context.getBean("testAuthenticator"), authenticator); + } + + @Test + public void imapWithAuthenticator() { + AbstractMailReceiver receiver = this.getReceiver("imapWithAuthenticator"); + assertEquals(ImapMailReceiver.class, receiver.getClass()); + Object authenticator = new DirectFieldAccessor(receiver).getPropertyValue("javaMailAuthenticator"); + assertNotNull(authenticator); + assertEquals(context.getBean("testAuthenticator"), authenticator); + } + + @Test + public void imapIdleWithAuthenticator() { + AbstractMailReceiver receiver = this.getReceiver("imapIdleWithAuthenticator"); + assertEquals(ImapMailReceiver.class, receiver.getClass()); + Object authenticator = new DirectFieldAccessor(receiver).getPropertyValue("javaMailAuthenticator"); + assertNotNull(authenticator); + assertEquals(context.getBean("testAuthenticator"), authenticator); + } + + + @SuppressWarnings("unused") + private static class TestAuthenticator extends Authenticator { + } + + + //==================== INT-1160 ==================== + + @Test + public void pop3WithMaxFetchSize() { + AbstractMailReceiver receiver = this.getReceiver("pop3WithMaxFetchSize"); + assertEquals(Pop3MailReceiver.class, receiver.getClass()); + Object value = new DirectFieldAccessor(receiver).getPropertyValue("maxFetchSize"); + assertEquals(11, value); + } + + @Test + public void pop3WithMaxFetchSizeFallsBackToPollerMax() { + AbstractMailReceiver receiver = this.getReceiver("pop3WithMaxFetchSizeFallsBackToPollerMax"); + assertEquals(Pop3MailReceiver.class, receiver.getClass()); + Object value = new DirectFieldAccessor(receiver).getPropertyValue("maxFetchSize"); + assertEquals(99, value); + } + + @Test + public void imapWithMaxFetchSize() { + AbstractMailReceiver receiver = this.getReceiver("imapWithMaxFetchSize"); + assertEquals(ImapMailReceiver.class, receiver.getClass()); + Object value = new DirectFieldAccessor(receiver).getPropertyValue("maxFetchSize"); + assertEquals(22, value); + } + + @Test + public void imapIdleWithMaxFetchSize() { + AbstractMailReceiver receiver = this.getReceiver("imapIdleWithMaxFetchSize"); + assertEquals(ImapMailReceiver.class, receiver.getClass()); + Object value = new DirectFieldAccessor(receiver).getPropertyValue("maxFetchSize"); + assertEquals(33, value); + } + + + //==================== INT-1161 ==================== + + @Test + public void pop3WithSession() { + AbstractMailReceiver receiver = this.getReceiver("pop3WithSession"); + assertEquals(Pop3MailReceiver.class, receiver.getClass()); + Object session = new DirectFieldAccessor(receiver).getPropertyValue("session"); + assertNotNull(session); + assertEquals(context.getBean("testSession"), session); + } + + @Test + public void imapWithSession() { + AbstractMailReceiver receiver = this.getReceiver("imapWithSession"); + assertEquals(ImapMailReceiver.class, receiver.getClass()); + Object session = new DirectFieldAccessor(receiver).getPropertyValue("session"); + assertNotNull(session); + assertEquals(context.getBean("testSession"), session); + } + + @Test + public void imapIdleWithSession() { + AbstractMailReceiver receiver = this.getReceiver("imapIdleWithSession"); + assertEquals(ImapMailReceiver.class, receiver.getClass()); + Object session = new DirectFieldAccessor(receiver).getPropertyValue("session"); + assertNotNull(session); + assertEquals(context.getBean("testSession"), session); + } + + + //==================== INT-1162 ==================== + + @Test + public void pop3WithoutStoreUri() { + AbstractMailReceiver receiver = this.getReceiver("pop3WithoutStoreUri"); + assertEquals(Pop3MailReceiver.class, receiver.getClass()); + Object url = new DirectFieldAccessor(receiver).getPropertyValue("url"); + assertNull(url); + } + + @Test + public void imapWithoutStoreUri() { + AbstractMailReceiver receiver = this.getReceiver("imapWithoutStoreUri"); + assertEquals(ImapMailReceiver.class, receiver.getClass()); + Object url = new DirectFieldAccessor(receiver).getPropertyValue("url"); + assertNull(url); + } + + @Test + public void imapIdleWithoutStoreUri() { + AbstractMailReceiver receiver = this.getReceiver("imapIdleWithoutStoreUri"); + assertEquals(ImapMailReceiver.class, receiver.getClass()); + Object url = new DirectFieldAccessor(receiver).getPropertyValue("url"); + assertNull(url); + } + + + //==================== INT-1163 ==================== + + @Test + public void inboundChannelAdapterRequiresShouldDeleteMessages() { + try { + new ClassPathXmlApplicationContext( + "org/springframework/integration/mail/config/InboundChannelAdapterParserTests-invalidContext.xml"); + fail("expected a parser error"); + } + catch(BeanDefinitionStoreException e) { + assertEquals(SAXParseException.class, e.getCause().getClass()); + } + } + + + //===================== COMMON ===================== + + private AbstractMailReceiver getReceiver(String name) { + Object adapter = context.getBean(name); + Object target = (adapter instanceof ImapIdleChannelAdapter) ? adapter + : new DirectFieldAccessor(adapter).getPropertyValue("source"); + return (AbstractMailReceiver) new DirectFieldAccessor(target).getPropertyValue("mailReceiver"); + } + +} diff --git a/org.springframework.integration.mail/src/test/java/org/springframework/integration/mail/config/PollingMailSourceParserTests-context.xml b/org.springframework.integration.mail/src/test/java/org/springframework/integration/mail/config/PollingMailSourceParserTests-context.xml index 47041656c0..a1eae95c2c 100644 --- a/org.springframework.integration.mail/src/test/java/org/springframework/integration/mail/config/PollingMailSourceParserTests-context.xml +++ b/org.springframework.integration.mail/src/test/java/org/springframework/integration/mail/config/PollingMailSourceParserTests-context.xml @@ -16,10 +16,10 @@ + store-uri="imap:foo" java-mail-properties="props" channel="channel" auto-startup="false" should-delete-messages="false"/> + store-uri="pop3:bar" java-mail-properties="props" channel="channel" auto-startup="false" should-delete-messages="true"/> bar