Skip to content

Commit

Permalink
Correctly test changing a message's flag
Browse files Browse the repository at this point in the history
  • Loading branch information
boris-petrov committed Sep 17, 2022
1 parent c224127 commit b832049
Showing 1 changed file with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -722,7 +724,7 @@ public void messagesAdded(MessageCountEvent e) {

@Test
public void testMessageChangedListener() throws MessagingException {
CountDownLatch latch = new CountDownLatch(1);
CountDownLatch latch = new CountDownLatch(2);

greenMail.setUser("foo@localhost", "pwd");

Expand All @@ -731,10 +733,11 @@ public void testMessageChangedListener() throws MessagingException {

GreenMailUtil.sendTextEmail("foo@localhost", "bar@localhost", "Test subject", "Test message",
ServerSetupTest.SMTP);
GreenMailUtil.sendTextEmail("foo@localhost", "bar@localhost", "Test subject 2", "Test message 2",
ServerSetupTest.SMTP);

Thread thread = null;
try {
Folder inboxFolder = store.getFolder("INBOX");
try (Folder inboxFolder = store.getFolder("INBOX")) {
inboxFolder.open(Folder.READ_ONLY);
thread = new Thread(() -> {
try {
Expand All @@ -747,12 +750,12 @@ public void testMessageChangedListener() throws MessagingException {
});
thread.start();

int[] messages = new int[] { 0 };
List<Integer> messages = new ArrayList<>(2);
MessageChangedListener listener = new MessageChangedListener() {
@Override
public void messageChanged(MessageChangedEvent e) {
assert e.getMessageChangeType() == MessageChangedEvent.FLAGS_CHANGED;
messages[0] = e.getMessage().getMessageNumber();
messages.add(e.getMessage().getMessageNumber());
latch.countDown();
}
};
Expand All @@ -765,9 +768,13 @@ public void messageChanged(MessageChangedEvent e) {
// Ignore
}

try {
greenMail.getReceivedMessages()[0].setFlag(Flags.Flag.DELETED, true);
greenMail.getReceivedMessages()[0].saveChanges();
try (Folder anotherInboxFolder = store.getFolder("INBOX")) {
// test changing a flag from the same folder that we're listening on
inboxFolder.getMessages()[0].setFlag(Flags.Flag.DELETED, true);

// test changing a flag from the "outside"
anotherInboxFolder.open(Folder.READ_ONLY);
anotherInboxFolder.getMessages()[1].setFlag(Flags.Flag.DELETED, true);
} catch (MessagingException ex) {
assertThat(false).isTrue();
}
Expand All @@ -778,9 +785,9 @@ public void messageChanged(MessageChangedEvent e) {
} catch (InterruptedException e1) {
// Ignore
}
assertThat(messages).hasSize(1);
assertThat(messages[0]).isGreaterThan(0);
inboxFolder.close();
assertThat(messages.size()).isEqualTo(2);
assertThat(messages.get(0)).isGreaterThan(0);
assertThat(messages.get(1)).isGreaterThan(0);
} finally {
if (thread != null) {
thread.interrupt();
Expand Down

0 comments on commit b832049

Please sign in to comment.