Skip to content

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 5 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -504,26 +504,19 @@ private Object byteArrayToContent(Map<String, Object> headers, ByteArrayOutputSt
}

private void postProcessFilteredMessages(Message[] filteredMessages) throws MessagingException {
// It is more intuitive use a local variable Message[] messages = filteredMessages;
// and then call setMessageFlagsAndMaybeDeleteMessages(messages); after the if, i.e. remove the else.
// However, in setMessageFlagsAndMaybeDeleteMessages we are calling Message#setFlag and Message#setFlags
// which have different implementations in different implementations of Message.
// e.g. IMAPMessage has a different implementation of those two methods.

// Copy messages to cause an eager fetch
Message[] messages = filteredMessages;
if (this.headerMapper == null && (this.autoCloseFolder || this.simpleContent)) {
Message[] originalMessages = new Message[filteredMessages.length];
messages = new Message[filteredMessages.length];
for (int i = 0; i < filteredMessages.length; i++) {
Message originalMessage = filteredMessages[i];
originalMessages[i] = originalMessage;
messages[i] = originalMessage;
MimeMessage mimeMessage = new IntegrationMimeMessage((MimeMessage) originalMessage);
filteredMessages[i] = mimeMessage;
}
setMessageFlagsAndMaybeDeleteMessages(originalMessages);
}
else {
setMessageFlagsAndMaybeDeleteMessages(filteredMessages);
}

setMessageFlagsAndMaybeDeleteMessages(messages);
}

private void setMessageFlagsAndMaybeDeleteMessages(Message[] messages) throws MessagingException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1005,10 +1005,12 @@ public void receiveAndMarkAsReadDontDeleteWithThrowingWhenCopying() throws Excep
.hasMessage("Simulated exception");
assertThat(msg1.getFlags().contains(Flag.SEEN)).isFalse();
assertThat(msg2.getFlags().contains(Flag.SEEN)).isFalse();
assertThat(msg2.getMyTestFlags().contains(Flag.SEEN)).isFalse();

receiver.receive();
assertThat(msg1.getFlags().contains(Flag.SEEN)).isTrue();
assertThat(msg2.getFlags().contains(Flag.SEEN)).isTrue();
assertThat(msg2.getMyTestFlags().contains(Flag.SEEN)).isTrue();
verify(receiver, times(0)).deleteMessages(Mockito.any());
}

Expand Down Expand Up @@ -1051,6 +1053,8 @@ private static class TestThrowingMimeMessage extends MimeMessage {

protected final AtomicInteger exceptionsBeforeWrite;

protected final Flags myTestFlags = new Flags();

private TestThrowingMimeMessage(MimeMessage source, int exceptionsBeforeWrite) throws MessagingException {
super(source);
this.exceptionsBeforeWrite = new AtomicInteger(exceptionsBeforeWrite);
Expand All @@ -1063,6 +1067,21 @@ public void writeTo(OutputStream os) throws IOException, MessagingException {
}
super.writeTo(os);
}

@Override
public synchronized void setFlags(Flags flag, boolean set) throws MessagingException {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to override this if there is that Message.getFlags()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to show that we are calling the setFlags on the original message and not the copied one. The Flags can be shared. The MimeMessage has

flags = source.getFlags();
if (flags == null)    // make sure flags is always set
    flags = new Flags();

So if the source has Flags and we call the setFlags on the filteredMessages then the test will still pass. The goal of the change is to show that the setFlags is called on the original message.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't look like you validate somehow in the test if message was original or IntegrationMimeMessage.
Therefore this extra precaution is redundant.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is

assertThat(msg2.getMyTestFlags().contains(Flag.SEEN)).isTrue();

In the test.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But looks like it is exactly the same as previous one:

assertThat(msg2.getFlags().contains(Flag.SEEN)).isTrue();

That's why I believe that is really enough.
Because you are not going to deal with mock messages in the real application.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this to validate that the setFlags is called on the original message and not on the IntegrationMimeMessage. Currently, GreenMailUtil#newMimeMessage does not set the flags. However, if you want I can set it up in such case that if the setMessageFlagsAndMaybeDeleteMessages is called on the IntegrationMimeMessage the test would fail.

assertThat(msg2.getFlags().contains(Flag.SEEN)).isTrue();

The test above passes as the Flags are shared. But

assertThat(msg2.getMyTestFlags().contains(Flag.SEEN)).isTrue();

would fail as it has it's own flags. If you think it is too much, I can remove it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. You probably can spy() on the GreenMailUtil#newMimeMessage result to verfiy that exactly its setFlags() is called.
It is still look redundant to have our own property when it is there in the super class.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point with the spy(). I'll add that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've replaced that with a spy. Hope it is good now.

super.setFlags(flag, set);
if (set) {
myTestFlags.add(flag);
}
else {
myTestFlags.remove(flag);
}
}

private Flags getMyTestFlags() {
return myTestFlags;
}
}

}