INT-1374, INT-1375 added support for 'shouldMarkMessagesAsRead', fix to ensure message deletion happens after message processing

This commit is contained in:
Oleg Zhurakousky
2010-08-25 19:09:34 +00:00
parent f5f948da4c
commit 71a87d5d4d
7 changed files with 267 additions and 6 deletions

View File

@@ -0,0 +1,8 @@
log4j.rootCategory=DEBUG, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%c{1}: %m%n
log4j.category.org.springframework.integration=WARN
log4j.category.org.springframework.integration.file=WARN

View File

@@ -0,0 +1,116 @@
/*
* 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;
import javax.mail.Message;
import javax.mail.Flags.Flag;
import javax.mail.internet.MimeMessage;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
/**
* @author Oleg Zhurakousky
*
*/
public class AbstractMailReceiverTests {
@Test(expected=IllegalArgumentException.class)
public void validateDeleteAndReadIsNotAllowed(){
AbstractMailReceiver receiver = new ImapMailReceiver();
receiver.setShouldDeleteMessages(true);
receiver.setShouldMarkMessagesAsRead(true);
}
@Test
public void validateDeleteOrReadIsAllowed_Read(){
AbstractMailReceiver receiver = new ImapMailReceiver();
receiver.setShouldDeleteMessages(false);
receiver.setShouldMarkMessagesAsRead(true);
}
@Test
public void validateDeleteOrReadIsAllowed_Delete(){
AbstractMailReceiver receiver = new ImapMailReceiver();
receiver.setShouldDeleteMessages(true);
receiver.setShouldMarkMessagesAsRead(false);
}
@Test
public void receieveAndMarkAsRead() throws Exception{
AbstractMailReceiver receiver = new ImapMailReceiver();
receiver.setShouldMarkMessagesAsRead(true);
receiver = spy(receiver);
Message msg1 = mock(MimeMessage.class);
Message msg2 = mock(MimeMessage.class);
final Message[] messages = new Message[]{msg1, msg2};
doAnswer(new Answer<Object>() {
public Object answer(InvocationOnMock invocation) throws Throwable {
// just to avoid the exception
return null;
}
}).when(receiver).openFolder();
doAnswer(new Answer<Object>() {
public Object answer(InvocationOnMock invocation) throws Throwable {
return messages;
}
}).when(receiver).searchForNewMessages();
doAnswer(new Answer<Object>() {
public Object answer(InvocationOnMock invocation) throws Throwable {
return null;
}
}).when(receiver).fetchMessages(messages);
receiver.receive();
verify(msg1, times(1)).setFlag(Flag.SEEN, true);
verify(msg2, times(1)).setFlag(Flag.SEEN, true);
}
@Test
public void receieveAndDontMarkAsRead() throws Exception{
AbstractMailReceiver receiver = new ImapMailReceiver();
receiver = spy(receiver);
Message msg1 = mock(MimeMessage.class);
Message msg2 = mock(MimeMessage.class);
final Message[] messages = new Message[]{msg1, msg2};
doAnswer(new Answer<Object>() {
public Object answer(InvocationOnMock invocation) throws Throwable {
// just to avoid the exception
return null;
}
}).when(receiver).openFolder();
doAnswer(new Answer<Object>() {
public Object answer(InvocationOnMock invocation) throws Throwable {
return messages;
}
}).when(receiver).searchForNewMessages();
doAnswer(new Answer<Object>() {
public Object answer(InvocationOnMock invocation) throws Throwable {
return null;
}
}).when(receiver).fetchMessages(messages);
receiver.receive();
verify(msg1, times(0)).setFlag(Flag.SEEN, true);
verify(msg2, times(0)).setFlag(Flag.SEEN, true);
}
}

View File

@@ -0,0 +1,90 @@
/*
* 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;
import java.lang.reflect.Method;
import javax.mail.Flags;
import javax.mail.Flags.Flag;
import javax.mail.search.AndTerm;
import javax.mail.search.FlagTerm;
import javax.mail.search.NotTerm;
import javax.mail.search.SearchTerm;
import org.junit.Test;
import org.springframework.util.ReflectionUtils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/**
* @author Oleg Zhurakousky
*
*/
public class ImapMailSearchTermsTests {
@Test
public void validateSearchTermsWhenShouldMarkAsReadNoExistingFlags() throws Exception {
ImapMailReceiver receiver = new ImapMailReceiver();
receiver.setShouldMarkMessagesAsRead(true);
Method compileSearchTerms = ReflectionUtils.findMethod(receiver.getClass(), "compileSearchTerms", Flags.class);
compileSearchTerms.setAccessible(true);
Flags flags = new Flags();
SearchTerm searchTerms = (SearchTerm) compileSearchTerms.invoke(receiver, flags);
assertTrue(searchTerms instanceof NotTerm);
NotTerm notTerm = (NotTerm) searchTerms;
assertTrue(((FlagTerm)notTerm.getTerm()).getFlags().contains(Flag.SEEN));
}
@Test
public void validateSearchTermsWhenShouldMarkAsReadWithExistingFlags() throws Exception {
ImapMailReceiver receiver = new ImapMailReceiver();
receiver.setShouldMarkMessagesAsRead(true);
Method compileSearchTerms = ReflectionUtils.findMethod(receiver.getClass(), "compileSearchTerms", Flags.class);
compileSearchTerms.setAccessible(true);
Flags flags = new Flags();
flags.add(Flag.ANSWERED);
SearchTerm searchTerms = (SearchTerm) compileSearchTerms.invoke(receiver, flags);
assertTrue(searchTerms instanceof AndTerm);
AndTerm andTerm = (AndTerm) searchTerms;
SearchTerm[] terms = andTerm.getTerms();
assertEquals(2, terms.length);
NotTerm notTerm = (NotTerm) terms[1];
assertTrue(((FlagTerm)notTerm.getTerm()).getFlags().contains(Flag.SEEN));
}
@Test
public void validateSearchTermsWhenShouldNotMarkAsReadNoExistingFlags() throws Exception {
ImapMailReceiver receiver = new ImapMailReceiver();
Method compileSearchTerms = ReflectionUtils.findMethod(receiver.getClass(), "compileSearchTerms", Flags.class);
compileSearchTerms.setAccessible(true);
Flags flags = new Flags();
SearchTerm searchTerms = (SearchTerm) compileSearchTerms.invoke(receiver, flags);
assertNull(searchTerms);
}
@Test
public void validateSearchTermsWhenShouldNotMarkAsReadWithExistingFlags() throws Exception {
ImapMailReceiver receiver = new ImapMailReceiver();
Method compileSearchTerms = ReflectionUtils.findMethod(receiver.getClass(), "compileSearchTerms", Flags.class);
compileSearchTerms.setAccessible(true);
Flags flags = new Flags();
flags.add(Flag.ANSWERED);
SearchTerm searchTerms = (SearchTerm) compileSearchTerms.invoke(receiver, flags);
assertTrue(searchTerms instanceof FlagTerm);
FlagTerm flagTerm = (FlagTerm) searchTerms;
assertTrue(flagTerm.getFlags().contains(Flag.ANSWERED));
}
}