Skip to content
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

Add a test for MessageChangedListener #481

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

boris-petrov
Copy link
Contributor

@marcelmay - this is a test that MessageChangedListener is called when flags are changed. It isn't though. I'm not sure if I'm doing something wrong or this is not supported by Greenmail?

As for the test itself:

  1. I'm not sure saveChanges is needed?
  2. I use a CountDownLatch to wait for the event to be triggered. That's fine but it might be better to use the idle mechanism as in the test before this one. I'm not sure it would work, however, I'm not very familiar with IMAP's protocol.

@marcelmay
Copy link
Member

@boris-petrov , try

...
inboxFolder.addMessageChangedListener(listener);
inboxFolder.getMessages()[0].setFlag(DELETED, true); // Triggers the listener

The mail listeners need e.g. an IMAP operation (IDLE, FETCH ....) as trigger.

If you access an email via GreenMail server reference, you directly manipulate the mail 'on the server side' and 'raw', circumventing clients:

greenMail.getReceivedMessages()[0].setFlag(DELETED, true);

This is more of a GreenMail helper, and very limited as all the mail API objects were designed for client side usage.
E.g. jakarta.mail.Folder should be an interface but unfortunately is a an abstract class - very difficult to extend with a server side listener for setFlags ...

@boris-petrov boris-petrov force-pushed the add-message-changed-listener-test branch from 4c36d75 to ffd290e Compare September 7, 2022 18:40
@boris-petrov
Copy link
Contributor Author

@marcelmay thanks for the answer!

You're right that doing inboxFolder.getMessages()[0].setFlag(DELETED, true); triggers the listener. But I would like to have a listener for "external" events - that is, someone changing a flag not through the inboxFolder variable but through another mail client for example. Is that possible at all actually?

@marcelmay
Copy link
Member

A loop issuing IMAP IDLE in a thread should trigger any listener

@boris-petrov
Copy link
Contributor Author

@marcelmay I pushed a new commit that does what you're suggesting. I still can't make it work. Do you mind taking a look?

// Ignore
}

try {
Copy link
Member

@marcelmay marcelmay Sep 16, 2022

Choose a reason for hiding this comment

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

As commented, using the internal (server-side) API won't trigger any listeners. Because you directly manipulate the message in memory, and work around any JavaMail listener mechanism.

Instead, use IMAP / JavaMail:

inboxFolder.getMessages()[0].setFlag(DELETED, true); // Triggers the listener, as this results in an IMAP command

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@marcelmay I added another commit that does what you suggest. First I try with the same variable on which I've added the listener - that works as expected. Then I try with a new variable - then it doesn't. That's what I meant before by "external" event. Sorry, I should have removed the "server-side" setting of flags before. But please check the test now.

@marcelmay
Copy link
Member

@boris-petrov , your test succeeds . I would close this PR for now, unless you still have issues?

If you have still issues, you you please rebase? Sorry I cleaned up/moved some tests.

@boris-petrov boris-petrov force-pushed the add-message-changed-listener-test branch from 8ab7b8c to 0ee0af1 Compare February 27, 2023 08:56
@boris-petrov
Copy link
Contributor Author

@marcelmay the test still doesn't pass on my side... I rebased on master and force-pushed. Please check the code to see what the test does. Perhaps I'm doing something wrong? Or maybe there is still an issue in Greenmail.

@marcelmay
Copy link
Member

@boris-petrov : You're right, the test still fails. Thx for being persistent!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants