Example usage for javax.mail Flags Flags

List of usage examples for javax.mail Flags Flags

Introduction

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

Prototype

public Flags(String flag) 

Source Link

Document

Construct a Flags object initialized with the given user flag.

Usage

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.ja  va2  s .com*/
    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.apache.james.jmap.model.MailboxMessageTest.java

@Test
public void attachmentsShouldBeEmptyWhenNone() throws Exception {
    MailboxMessage testMail = new SimpleMailboxMessage(INTERNAL_DATE, 0, 0,
            new SharedByteArrayInputStream(
                    IOUtils.toByteArray(ClassLoader.getSystemResourceAsStream("spamMail.eml"))),
            new Flags(Flag.SEEN), new PropertyBuilder(), MAILBOX_ID);
    testMail.setModSeq(MOD_SEQ);/*from  ww w . j  av a 2  s.co  m*/
    Message testee = messageFactory.fromMailboxMessage(testMail, ImmutableList.of(),
            x -> MessageId.of("user|box|" + x));
    assertThat(testee.getAttachments()).isEmpty();
}

From source file:org.apache.james.jmap.model.MailboxMessageTest.java

@Test
public void attachmentsShouldBeRetrievedWhenSome() throws Exception {
    MailboxMessage testMail = new SimpleMailboxMessage(INTERNAL_DATE, 0, 0,
            new SharedByteArrayInputStream(
                    IOUtils.toByteArray(ClassLoader.getSystemResourceAsStream("spamMail.eml"))),
            new Flags(Flag.SEEN), new PropertyBuilder(), MAILBOX_ID);
    testMail.setModSeq(MOD_SEQ);/*from   www .java  2s  .  c  om*/

    String payload = "payload";
    BlobId blodId = BlobId.of("id1");
    String type = "content";
    Attachment expectedAttachment = Attachment.builder().blobId(blodId).size(payload.length()).type(type)
            .cid("cid").isInline(true).build();
    Message testee = messageFactory.fromMailboxMessage(testMail,
            ImmutableList.of(MessageAttachment.builder()
                    .attachment(org.apache.james.mailbox.store.mail.model.Attachment.builder()
                            .attachmentId(AttachmentId.from(blodId.getRawValue())).bytes(payload.getBytes())
                            .type(type).build())
                    .cid(Cid.from("cid")).isInline(true).build()),
            x -> MessageId.of("user|box|" + x));

    assertThat(testee.getAttachments()).hasSize(1);
    assertThat(testee.getAttachments().get(0)).isEqualToComparingFieldByField(expectedAttachment);
}

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   w  ww  .  j a  v a2s .  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.apache.james.mailbox.store.mail.model.MessageIdMapperTest.java

@Test
public void setFlagsShouldReturnUpdatedFlagsWhenMessageIsInOneMailbox() throws Exception {
    message1.setUid(mapperProvider.generateMessageUid());
    message1.setModSeq(mapperProvider.generateModSeq(benwaInboxMailbox));
    sut.save(message1);/*from w  w w .j  av a 2  s . c om*/

    MessageId messageId = message1.getMessageId();
    Flags newFlags = new Flags(Flag.ANSWERED);
    Map<MailboxId, UpdatedFlags> flags = sut.setFlags(messageId, ImmutableList.of(message1.getMailboxId()),
            newFlags, FlagsUpdateMode.ADD);

    long modSeq = mapperProvider.highestModSeq(benwaInboxMailbox);
    UpdatedFlags expectedUpdatedFlags = UpdatedFlags.builder().uid(message1.getUid()).modSeq(modSeq)
            .oldFlags(new Flags()).newFlags(newFlags).build();
    assertThat(flags).containsOnly(MapEntry.entry(benwaInboxMailbox.getMailboxId(), expectedUpdatedFlags));
}

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

@Test
public void receiveAndIgnoreMarkAsReadDontDelete() throws Exception {
    AbstractMailReceiver receiver = new ImapMailReceiver();
    receiver = spy(receiver);/*ww w  .  ja 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.apache.james.jmap.methods.integration.SetMessagesMethodTest.java

@Test
public void setMessagesShouldReturnUpdatedIdAndNoErrorWhenIsUnreadPassed() throws MailboxException {
    // Given//  w  w w  .  j a v a2 s . com
    jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "mailbox");

    jmapServer.serverProbe().appendMessage(username,
            new MailboxPath(MailboxConstants.USER_NAMESPACE, username, "mailbox"),
            new ByteArrayInputStream("Subject: test\r\n\r\ntestmail".getBytes(Charsets.UTF_8)), new Date(),
            false, new Flags(Flags.Flag.SEEN));
    await();

    String presumedMessageId = username + "|mailbox|1";
    given().header("Authorization", accessToken.serialize())
            .body(String.format(
                    "[[\"setMessages\", {\"update\": {\"%s\" : { \"isUnread\" : true } } }, \"#0\"]]",
                    presumedMessageId))
            // When
            .when().post("/jmap")
            // Then
            .then().log().ifValidationFails().spec(getSetMessagesUpdateOKResponseAssertions(presumedMessageId));
}

From source file:org.sourceforge.net.javamail4ews.store.EwsFolder.java

public synchronized Message[] search(SearchTerm paramSearchTerm) throws MessagingException {
    if (paramSearchTerm instanceof FlagTerm) {
        FlagTerm flagTerm = (FlagTerm) paramSearchTerm;
        Flags flags = flagTerm.getFlags();
        if (flags.equals(new Flags(Flags.Flag.SEEN))) {
            try {
                return getUnreadMessage(0);
            } catch (Exception e) {
                throw new MessagingException(e.getMessage(), e);
            }//from w w  w .  ja  v  a 2s .  c o  m
        }
    }
    throw new MessagingException("Ews folder don't support search with search term " + paramSearchTerm);
}

From source file:org.apache.james.jmap.methods.integration.SetMessagesMethodTest.java

@Test
public void setMessagesShouldMarkAsUnreadWhenIsUnreadPassed() throws MailboxException {
    // Given// w  w w  . j  av a 2s . c  o m
    jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, username, "mailbox");

    jmapServer.serverProbe().appendMessage(username,
            new MailboxPath(MailboxConstants.USER_NAMESPACE, username, "mailbox"),
            new ByteArrayInputStream("Subject: test\r\n\r\ntestmail".getBytes(Charsets.UTF_8)), new Date(),
            false, new Flags(Flags.Flag.SEEN));
    await();

    String presumedMessageId = username + "|mailbox|1";
    given().header("Authorization", accessToken.serialize())
            .body(String.format(
                    "[[\"setMessages\", {\"update\": {\"%s\" : { \"isUnread\" : true } } }, \"#0\"]]",
                    presumedMessageId))
            // When
            .when().post("/jmap");
    // Then
    with().header("Authorization", accessToken.serialize())
            .body("[[\"getMessages\", {\"ids\": [\"" + presumedMessageId + "\"]}, \"#0\"]]").post("/jmap")
            .then().log().ifValidationFails().body(NAME, equalTo("messages"))
            .body(ARGUMENTS + ".list", hasSize(1)).body(ARGUMENTS + ".list[0].isUnread", equalTo(true));
}

From source file:org.apache.james.mailbox.store.mail.model.MessageIdMapperTest.java

@Test
public void setFlagsShouldReturnUpdatedFlagsWhenRemoveMode() throws Exception {
    Flags messageFlags = new FlagsBuilder().add(Flags.Flag.RECENT, Flags.Flag.FLAGGED).build();

    message1.setUid(mapperProvider.generateMessageUid());
    message1.setFlags(messageFlags);/*w  w w  . j  av  a2 s . co  m*/
    message1.setModSeq(mapperProvider.generateModSeq(benwaInboxMailbox));
    sut.save(message1);

    MessageId messageId = message1.getMessageId();
    Flags newFlags = new FlagsBuilder().add(Flags.Flag.DELETED, Flags.Flag.FLAGGED).add("userflag").build();

    Map<MailboxId, UpdatedFlags> flags = sut.setFlags(messageId, ImmutableList.of(message1.getMailboxId()),
            newFlags, FlagsUpdateMode.REMOVE);

    long modSeq = mapperProvider.highestModSeq(benwaInboxMailbox);
    UpdatedFlags expectedUpdatedFlags = UpdatedFlags.builder().uid(message1.getUid()).modSeq(modSeq)
            .oldFlags(messageFlags).newFlags(new Flags(Flags.Flag.RECENT)).build();

    assertThat(flags).contains(MapEntry.entry(benwaInboxMailbox.getMailboxId(), expectedUpdatedFlags));
}