Search in sources :

Example 1 with AndTerm

use of jakarta.mail.search.AndTerm in project spring-integration by spring-projects.

the class ImapMailSearchTermsTests method validateSearchTermsWhenShouldMarkAsReadWithExistingFlags.

@Test
public void validateSearchTermsWhenShouldMarkAsReadWithExistingFlags() throws Exception {
    ImapMailReceiver receiver = new ImapMailReceiver();
    receiver.setShouldMarkMessagesAsRead(true);
    receiver.setBeanFactory(mock(BeanFactory.class));
    receiver.afterPropertiesSet();
    Field folderField = AbstractMailReceiver.class.getDeclaredField("folder");
    folderField.setAccessible(true);
    Folder folder = mock(Folder.class);
    when(folder.getPermanentFlags()).thenReturn(new Flags(Flags.Flag.USER));
    folderField.set(receiver, folder);
    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);
    assertThat(searchTerms instanceof AndTerm).isTrue();
    AndTerm andTerm = (AndTerm) searchTerms;
    SearchTerm[] terms = andTerm.getTerms();
    assertThat(terms.length).isEqualTo(2);
    NotTerm notTerm = (NotTerm) terms[0];
    assertThat(((FlagTerm) notTerm.getTerm()).getFlags().contains(Flag.ANSWERED)).isTrue();
    notTerm = (NotTerm) terms[1];
    Flags siFlags = new Flags();
    siFlags.add(AbstractMailReceiver.DEFAULT_SI_USER_FLAG);
    assertThat(((FlagTerm) notTerm.getTerm()).getFlags().contains(siFlags)).isTrue();
}
Also used : AndTerm(jakarta.mail.search.AndTerm) Field(java.lang.reflect.Field) NotTerm(jakarta.mail.search.NotTerm) BeanFactory(org.springframework.beans.factory.BeanFactory) Flags(jakarta.mail.Flags) Method(java.lang.reflect.Method) Folder(jakarta.mail.Folder) SearchTerm(jakarta.mail.search.SearchTerm) Test(org.junit.jupiter.api.Test)

Example 2 with AndTerm

use of jakarta.mail.search.AndTerm in project spring-integration by spring-projects.

the class ImapMailReceiverTests method testIdleWithServerCustomSearch.

@Test
public void testIdleWithServerCustomSearch() throws Exception {
    ImapMailReceiver receiver = new ImapMailReceiver("imap://user:pw@localhost:" + this.imapIdleServer.getImap().getPort() + "/INBOX");
    receiver.setSearchTermStrategy((supportedFlags, folder) -> {
        try {
            FromTerm fromTerm = new FromTerm(new InternetAddress("bar@baz"));
            return new AndTerm(fromTerm, new FlagTerm(new Flags(Flag.SEEN), false));
        } catch (AddressException e) {
            throw new RuntimeException(e);
        }
    });
    testIdleWithServerGuts(receiver, false);
}
Also used : AndTerm(jakarta.mail.search.AndTerm) InternetAddress(jakarta.mail.internet.InternetAddress) FlagTerm(jakarta.mail.search.FlagTerm) AddressException(jakarta.mail.internet.AddressException) Flags(jakarta.mail.Flags) FromTerm(jakarta.mail.search.FromTerm) Test(org.junit.jupiter.api.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Aggregations

Flags (jakarta.mail.Flags)2 AndTerm (jakarta.mail.search.AndTerm)2 Test (org.junit.jupiter.api.Test)2 ServerSetupTest (com.icegreen.greenmail.util.ServerSetupTest)1 Folder (jakarta.mail.Folder)1 AddressException (jakarta.mail.internet.AddressException)1 InternetAddress (jakarta.mail.internet.InternetAddress)1 FlagTerm (jakarta.mail.search.FlagTerm)1 FromTerm (jakarta.mail.search.FromTerm)1 NotTerm (jakarta.mail.search.NotTerm)1 SearchTerm (jakarta.mail.search.SearchTerm)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 BeanFactory (org.springframework.beans.factory.BeanFactory)1