Example usage for javax.mail Folder getPermanentFlags

List of usage examples for javax.mail Folder getPermanentFlags

Introduction

In this page you can find the example usage for javax.mail Folder getPermanentFlags.

Prototype

public abstract Flags getPermanentFlags();

Source Link

Document

Get the permanent flags supported by this Folder.

Usage

From source file:org.springframework.integration.mail.ImapMailReceiverTests.java

private AbstractMailReceiver receiveAndMarkAsReadDontDeleteGuts(AbstractMailReceiver receiver, Message msg1,
        Message msg2) throws NoSuchFieldException, IllegalAccessException, MessagingException {
    ((ImapMailReceiver) receiver).setShouldMarkMessagesAsRead(true);
    receiver = spy(receiver);/*w  w w.ja v  a  2s.c o m*/
    receiver.setBeanFactory(mock(BeanFactory.class));
    receiver.afterPropertiesSet();
    Field folderField = AbstractMailReceiver.class.getDeclaredField("folder");
    folderField.setAccessible(true);
    Folder folder = mock(Folder.class);
    given(folder.getPermanentFlags()).willReturn(new Flags(Flags.Flag.USER));
    folderField.set(receiver, folder);

    final Message[] messages = new Message[] { msg1, msg2 };

    willAnswer(invocation -> {
        DirectFieldAccessor accessor = new DirectFieldAccessor(invocation.getMock());
        int folderOpenMode = (Integer) accessor.getPropertyValue("folderOpenMode");
        if (folderOpenMode != Folder.READ_WRITE) {
            throw new IllegalArgumentException("Folder had to be open in READ_WRITE mode");
        }

        return null;
    }).given(receiver).openFolder();

    willAnswer(invocation -> messages).given(receiver).searchForNewMessages();

    willAnswer(invocation -> null).given(receiver).fetchMessages(messages);
    receiver.receive();
    return receiver;
}

From source file:org.springframework.integration.mail.ImapMailReceiverTests.java

@Test
public void receiveMarkAsReadAndDelete() throws Exception {
    AbstractMailReceiver receiver = new ImapMailReceiver();
    ((ImapMailReceiver) receiver).setShouldMarkMessagesAsRead(true);
    receiver.setShouldDeleteMessages(true);
    receiver = spy(receiver);//from  w ww .java2 s.  c om
    receiver.setBeanFactory(mock(BeanFactory.class));
    receiver.afterPropertiesSet();

    Field folderField = AbstractMailReceiver.class.getDeclaredField("folder");
    folderField.setAccessible(true);
    Folder folder = mock(Folder.class);
    given(folder.getPermanentFlags()).willReturn(new Flags(Flags.Flag.USER));
    folderField.set(receiver, folder);

    Message msg1 = mock(MimeMessage.class);
    Message msg2 = mock(MimeMessage.class);
    final Message[] messages = new Message[] { msg1, msg2 };
    willAnswer(invocation -> {
        DirectFieldAccessor accessor = new DirectFieldAccessor(invocation.getMock());
        int folderOpenMode = (Integer) accessor.getPropertyValue("folderOpenMode");
        if (folderOpenMode != Folder.READ_WRITE) {
            throw new IllegalArgumentException("Folder had to be open in READ_WRITE mode");
        }
        return null;
    }).given(receiver).openFolder();

    willAnswer(invocation -> messages).given(receiver).searchForNewMessages();

    willAnswer(invocation -> null).given(receiver).fetchMessages(messages);
    receiver.receive();
    verify(msg1, times(1)).setFlag(Flag.SEEN, true);
    verify(msg2, times(1)).setFlag(Flag.SEEN, true);
    verify(receiver, times(1)).deleteMessages((Message[]) Mockito.any());
}

From source file:org.springframework.integration.mail.ImapMailReceiverTests.java

@Test
public void receiveAndDontMarkAsRead() throws Exception {
    AbstractMailReceiver receiver = new ImapMailReceiver();
    ((ImapMailReceiver) receiver).setShouldMarkMessagesAsRead(false);
    receiver = spy(receiver);//from   w  w w. j av a  2  s .c o  m
    receiver.setBeanFactory(mock(BeanFactory.class));
    receiver.afterPropertiesSet();

    Field folderField = AbstractMailReceiver.class.getDeclaredField("folder");
    folderField.setAccessible(true);
    Folder folder = mock(Folder.class);
    given(folder.getPermanentFlags()).willReturn(new Flags(Flags.Flag.USER));
    folderField.set(receiver, folder);

    Message msg1 = mock(MimeMessage.class);
    Message msg2 = mock(MimeMessage.class);
    final Message[] messages = new Message[] { msg1, msg2 };
    willAnswer(invocation -> null).given(receiver).openFolder();

    willAnswer(invocation -> messages).given(receiver).searchForNewMessages();

    willAnswer(invocation -> null).given(receiver).fetchMessages(messages);
    receiver.afterPropertiesSet();
    receiver.receive();
    verify(msg1, times(0)).setFlag(Flag.SEEN, true);
    verify(msg2, times(0)).setFlag(Flag.SEEN, true);
}

From source file:org.springframework.integration.mail.ImapMailReceiverTests.java

@Test
public void receiveAndDontMarkAsReadButDelete() throws Exception {
    AbstractMailReceiver receiver = new ImapMailReceiver();
    receiver.setShouldDeleteMessages(true);
    ((ImapMailReceiver) receiver).setShouldMarkMessagesAsRead(false);
    receiver = spy(receiver);/*from ww  w. jav a  2s . c om*/
    receiver.setBeanFactory(mock(BeanFactory.class));
    receiver.afterPropertiesSet();

    Field folderField = AbstractMailReceiver.class.getDeclaredField("folder");
    folderField.setAccessible(true);
    Folder folder = mock(Folder.class);
    given(folder.getPermanentFlags()).willReturn(new Flags(Flags.Flag.USER));
    folderField.set(receiver, folder);

    Message msg1 = mock(MimeMessage.class);
    Message msg2 = mock(MimeMessage.class);
    final Message[] messages = new Message[] { msg1, msg2 };
    willAnswer(invocation -> {
        DirectFieldAccessor accessor = new DirectFieldAccessor(invocation.getMock());
        int folderOpenMode = (Integer) accessor.getPropertyValue("folderOpenMode");
        if (folderOpenMode != Folder.READ_WRITE) {
            throw new IllegalArgumentException("Folder had to be open in READ_WRITE mode");
        }
        return null;
    }).given(receiver).openFolder();

    willAnswer(invocation -> messages).given(receiver).searchForNewMessages();

    willAnswer(invocation -> null).given(receiver).fetchMessages(messages);
    receiver.afterPropertiesSet();
    receiver.receive();
    verify(msg1, times(0)).setFlag(Flag.SEEN, true);
    verify(msg2, times(0)).setFlag(Flag.SEEN, true);
    verify(msg1, times(1)).setFlag(Flag.DELETED, true);
    verify(msg2, times(1)).setFlag(Flag.DELETED, true);
}

From source file:org.springframework.integration.mail.ImapMailReceiverTests.java

@Test
public void receiveAndIgnoreMarkAsReadDontDelete() throws Exception {
    AbstractMailReceiver receiver = new ImapMailReceiver();
    receiver = spy(receiver);/*  w  ww. j a va  2  s  .  c  o m*/
    receiver.setBeanFactory(mock(BeanFactory.class));
    receiver.afterPropertiesSet();

    Field folderField = AbstractMailReceiver.class.getDeclaredField("folder");
    folderField.setAccessible(true);
    Folder folder = mock(Folder.class);
    given(folder.getPermanentFlags()).willReturn(new Flags(Flags.Flag.USER));
    folderField.set(receiver, folder);

    Message msg1 = mock(MimeMessage.class);
    Message msg2 = mock(MimeMessage.class);
    final Message[] messages = new Message[] { msg1, msg2 };
    willAnswer(invocation -> {
        DirectFieldAccessor accessor = new DirectFieldAccessor(invocation.getMock());
        int folderOpenMode = (Integer) accessor.getPropertyValue("folderOpenMode");
        if (folderOpenMode != Folder.READ_WRITE) {
            throw new IllegalArgumentException("Folder had to be open in READ_WRITE mode");
        }
        return null;
    }).given(receiver).openFolder();

    willAnswer(invocation -> messages).given(receiver).searchForNewMessages();

    willAnswer(invocation -> null).given(receiver).fetchMessages(messages);
    receiver.receive();
    verify(msg1, times(1)).setFlag(Flag.SEEN, true);
    verify(msg2, times(1)).setFlag(Flag.SEEN, true);
    verify(receiver, times(0)).deleteMessages((Message[]) Mockito.any());
}

From source file:org.springframework.integration.mail.ImapMailReceiverTests.java

@Test
public void testIdleChannelAdapterException() throws Exception {
    ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(
            "ImapIdleChannelAdapterParserTests-context.xml", ImapIdleChannelAdapterParserTests.class);
    ImapIdleChannelAdapter adapter = context.getBean("simpleAdapter", ImapIdleChannelAdapter.class);

    //ImapMailReceiver receiver = (ImapMailReceiver) TestUtils.getPropertyValue(adapter, "mailReceiver");

    DirectChannel channel = new DirectChannel();
    channel.subscribe(new AbstractReplyProducingMessageHandler() {

        @Override/* w  w  w.java2  s .  com*/
        protected Object handleRequestMessage(org.springframework.messaging.Message<?> requestMessage) {
            throw new RuntimeException("Failed");
        }
    });
    adapter.setOutputChannel(channel);
    QueueChannel errorChannel = new QueueChannel();
    adapter.setErrorChannel(errorChannel);

    AbstractMailReceiver receiver = new ImapMailReceiver();
    receiver = spy(receiver);
    receiver.setBeanFactory(mock(BeanFactory.class));
    receiver.afterPropertiesSet();

    Field folderField = AbstractMailReceiver.class.getDeclaredField("folder");
    folderField.setAccessible(true);
    Folder folder = mock(IMAPFolder.class);
    given(folder.getPermanentFlags()).willReturn(new Flags(Flags.Flag.USER));
    folderField.set(receiver, folder);

    willAnswer(invocation -> true).given(folder).isOpen();

    willAnswer(invocation -> null).given(receiver).openFolder();

    DirectFieldAccessor adapterAccessor = new DirectFieldAccessor(adapter);
    adapterAccessor.setPropertyValue("mailReceiver", receiver);

    MimeMessage mailMessage = mock(MimeMessage.class);
    Flags flags = mock(Flags.class);
    given(mailMessage.getFlags()).willReturn(flags);
    final Message[] messages = new Message[] { mailMessage };

    willAnswer(invocation -> messages).given(receiver).searchForNewMessages();

    willAnswer(invocation -> null).given(receiver).fetchMessages(messages);

    adapter.start();
    org.springframework.messaging.Message<?> replMessage = errorChannel.receive(10000);
    assertNotNull(replMessage);
    assertEquals("Failed", ((Exception) replMessage.getPayload()).getCause().getMessage());
    adapter.stop();
    context.close();
}

From source file:org.springframework.integration.mail.ImapMailReceiverTests.java

@Test // see INT-1801
public void testImapLifecycleForRaceCondition() throws Exception {

    for (int i = 0; i < 1000; i++) {
        final ImapMailReceiver receiver = new ImapMailReceiver("imap://foo");
        Store store = mock(Store.class);
        Folder folder = mock(Folder.class);
        given(folder.exists()).willReturn(true);
        given(folder.isOpen()).willReturn(true);
        given(folder.search((SearchTerm) Mockito.any())).willReturn(new Message[] {});
        given(store.getFolder(Mockito.any(URLName.class))).willReturn(folder);
        given(folder.getPermanentFlags()).willReturn(new Flags(Flags.Flag.USER));

        DirectFieldAccessor df = new DirectFieldAccessor(receiver);
        df.setPropertyValue("store", store);
        receiver.setBeanFactory(mock(BeanFactory.class));
        receiver.afterPropertiesSet();/*from  ww  w .j av  a  2 s  .c  om*/

        new Thread(() -> {
            try {
                receiver.receive();
            } catch (javax.mail.MessagingException e) {
                if (e.getCause() instanceof NullPointerException) {
                    failed.getAndIncrement();
                }
            }

        }).start();

        new Thread(() -> {
            try {
                receiver.destroy();
            } catch (Exception ignore) {
                // ignore
            }
        }).start();
    }
    assertEquals(0, failed.get());
}

From source file:org.springframework.integration.mail.ImapMailReceiverTests.java

private Folder testAttachmentsGuts(final ImapMailReceiver receiver) throws MessagingException, IOException {
    Store store = mock(Store.class);
    Folder folder = mock(Folder.class);
    given(folder.exists()).willReturn(true);
    given(folder.isOpen()).willReturn(true);

    Message message = new MimeMessage(null, new ClassPathResource("test.mail").getInputStream());
    given(folder.search((SearchTerm) Mockito.any())).willReturn(new Message[] { message });
    given(store.getFolder(Mockito.any(URLName.class))).willReturn(folder);
    given(folder.getPermanentFlags()).willReturn(new Flags(Flags.Flag.USER));
    DirectFieldAccessor df = new DirectFieldAccessor(receiver);
    df.setPropertyValue("store", store);
    receiver.setBeanFactory(mock(BeanFactory.class));
    receiver.afterPropertiesSet();/*w w w.  ja  v  a2  s .  c o  m*/

    return folder;
}

From source file:org.springframework.ws.transport.mail.monitor.AbstractMonitoringStrategy.java

/**
 * Retrieves new messages from the given folder. This implementation creates a {@link SearchTerm} that searches for
 * all messages in the folder that are {@link javax.mail.Flags.Flag#RECENT RECENT}, not {@link
 * javax.mail.Flags.Flag#ANSWERED ANSWERED}, and not {@link javax.mail.Flags.Flag#DELETED DELETED}. The search term
 * is used to {@link Folder#search(SearchTerm) search} for new messages.
 *
 * @param folder the folder to retrieve new messages from
 * @return the new messages//  www.  ja  v a  2  s .  co  m
 * @throws MessagingException in case of JavaMail errors
 */
protected Message[] searchForNewMessages(Folder folder) throws MessagingException {
    if (!folder.isOpen()) {
        return new Message[0];
    }
    Flags supportedFlags = folder.getPermanentFlags();
    SearchTerm searchTerm = null;
    if (supportedFlags != null) {
        if (supportedFlags.contains(Flags.Flag.RECENT)) {
            searchTerm = new FlagTerm(new Flags(Flags.Flag.RECENT), true);
        }
        if (supportedFlags.contains(Flags.Flag.ANSWERED)) {
            FlagTerm answeredTerm = new FlagTerm(new Flags(Flags.Flag.ANSWERED), false);
            if (searchTerm == null) {
                searchTerm = answeredTerm;
            } else {
                searchTerm = new AndTerm(searchTerm, answeredTerm);
            }
        }
        if (supportedFlags.contains(Flags.Flag.DELETED)) {
            FlagTerm deletedTerm = new FlagTerm(new Flags(Flags.Flag.DELETED), false);
            if (searchTerm == null) {
                searchTerm = deletedTerm;
            } else {
                searchTerm = new AndTerm(searchTerm, deletedTerm);
            }
        }
    }
    return searchTerm != null ? folder.search(searchTerm) : folder.getMessages();
}