-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Flag and maybe delete messages after messages have been copied #9546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
package org.springframework.integration.mail; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.lang.reflect.Field; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
|
@@ -88,6 +89,7 @@ | |
import org.springframework.util.MimeTypeUtils; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.BDDMockito.given; | ||
|
@@ -299,6 +301,11 @@ public void receiveAndMarkAsReadDontDelete() throws Exception { | |
|
||
private AbstractMailReceiver receiveAndMarkAsReadDontDeleteGuts(AbstractMailReceiver receiver, Message msg1, | ||
Message msg2) throws NoSuchFieldException, IllegalAccessException, MessagingException { | ||
return receiveAndMarkAsReadDontDeleteGuts(receiver, msg1, msg2, true); | ||
} | ||
|
||
private AbstractMailReceiver receiveAndMarkAsReadDontDeleteGuts(AbstractMailReceiver receiver, Message msg1, | ||
Message msg2, boolean receive) throws NoSuchFieldException, IllegalAccessException, MessagingException { | ||
|
||
((ImapMailReceiver) receiver).setShouldMarkMessagesAsRead(true); | ||
receiver = spy(receiver); | ||
|
@@ -326,7 +333,9 @@ private AbstractMailReceiver receiveAndMarkAsReadDontDeleteGuts(AbstractMailRece | |
willAnswer(invocation -> messages).given(folder).search(any(SearchTerm.class)); | ||
|
||
willAnswer(invocation -> null).given(receiver).fetchMessages(messages); | ||
receiver.receive(); | ||
if (receive) { | ||
receiver.receive(); | ||
} | ||
return receiver; | ||
} | ||
|
||
|
@@ -980,6 +989,28 @@ private void setUpScheduler(ImapMailReceiver mailReceiver, ThreadPoolTaskSchedul | |
mailReceiver.setBeanFactory(bf); | ||
} | ||
|
||
@Test | ||
public void receiveAndMarkAsReadDontDeleteWithThrowingWhenCopying() throws Exception { | ||
AbstractMailReceiver receiver = new ImapMailReceiver(); | ||
MimeMessage msg1 = GreenMailUtil.newMimeMessage("test1"); | ||
MimeMessage greenMailMsg2 = GreenMailUtil.newMimeMessage("test2"); | ||
TestThrowingMimeMessage msg2 = new TestThrowingMimeMessage(greenMailMsg2, 1); | ||
receiver = receiveAndMarkAsReadDontDeleteGuts(receiver, msg1, msg2, false); | ||
assertThatThrownBy(receiver::receive) | ||
.isInstanceOf(MessagingException.class) | ||
.hasMessage("IOException while copying message") | ||
.cause() | ||
.isInstanceOf(IOException.class) | ||
.hasMessage("Simulated exception"); | ||
assertThat(msg1.getFlags().contains(Flag.SEEN)).isFalse(); | ||
assertThat(msg2.getFlags().contains(Flag.SEEN)).isFalse(); | ||
|
||
receiver.receive(); | ||
assertThat(msg1.getFlags().contains(Flag.SEEN)).isTrue(); | ||
assertThat(msg2.getFlags().contains(Flag.SEEN)).isTrue(); | ||
verify(receiver, times(0)).deleteMessages(Mockito.any()); | ||
} | ||
|
||
private static class ImapSearchLoggingHandler extends Handler { | ||
|
||
private final List<String> searches = new ArrayList<>(); | ||
|
@@ -1015,4 +1046,22 @@ public void close() throws SecurityException { | |
|
||
} | ||
|
||
private static class TestThrowingMimeMessage extends MimeMessage { | ||
|
||
protected final AtomicInteger exceptionsBeforeWrite; | ||
|
||
private TestThrowingMimeMessage(MimeMessage source, int exceptionsBeforeWrite) throws MessagingException { | ||
super(source); | ||
this.exceptionsBeforeWrite = new AtomicInteger(exceptionsBeforeWrite); | ||
} | ||
|
||
@Override | ||
public void writeTo(OutputStream os) throws IOException, MessagingException { | ||
if (this.exceptionsBeforeWrite.decrementAndGet() >= 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are right. I've adjusted it |
||
throw new IOException("Simulated exception"); | ||
} | ||
super.writeTo(os); | ||
} | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May we consider a local method variable for
Message[]
instead ofelse
branch with the same method call?I mean something like:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was wanted to do that initially as well. However, in the
if
we are creating a newMessage
of theIntegrationMimeMessage
. This means that when we callsetMessageFlagsAndMaybeDeleteMessages
and we invokeMessage#setFlags
the actual implementation differs. If we look into theIMAPMessage#setFlags
the implementation looks likewhereas the
MimeMessage#setFlags
looks likeI am going to add a comment there to explain why it is done in the way I did it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m not sure why does that matter since you call
setMessageFlagsAndMaybeDeleteMessages
in both cases? So, one local variable for both cases feels right.Ok, I’ll look into that locally on merge.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@artembilan it matters a lot because what will be invoked in the end is not the same. The behaviour of the
setFlags
will change due to the fact that there will be different implementations.Prior to this PR the
setFlags
was invoked on the original messages received from the folder. The goal of the PR is to still invoke thesetFlags
on the original messages. However, instead of doing it before copying it, to do it after it has been copied.I have an idea for a test case. I'll add it to illustrate the problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am still failing to understand why this code is not OK with you:
You see, we still fulfill that
messagesToProcess
with original messages.And then call
setMessageFlagsAndMaybeDeleteMessages()
only against a single source of truth.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry @artembilan, I misunderstood you. I know what you mean now. The
messages
will be the original messages populated in the loop.I adjusted the PR. Please correct me if I am wrong.