org.apache.mailet.Mailet - java examples

Here are the examples of the java api org.apache.mailet.Mailet taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

66 Examples 7

19 View Complete Implementation : SetMimeHeaderTest.java
Copyright Apache License 2.0
Author : apache
clreplaced SetMimeHeaderTest {

    private Mailet mailet;

    @BeforeEach
    void setUp() {
        mailet = new SetMimeHeader();
    }

    @Test
    void shouldAddHeaderToMime() throws MessagingException {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty("name", "header-name").setProperty("value", "test-value").build();
        mailet.init(mailetConfig);
        MimeMessage mimeMessage = MailUtil.createMimeMessage();
        Mail mail = MailUtil.createMockMail2Recipients(mimeMessage);
        mailet.service(mail);
        replacedertThat(mail.getMessage().getHeader("header-name")).containsExactly("test-value");
    }

    @Test
    void shouldAddHeaderWhenAlreadyPresent() throws MessagingException {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty("name", "header-name").setProperty("value", "test-value").build();
        mailet.init(mailetConfig);
        MimeMessage mimeMessage = MailUtil.createMimeMessage();
        mimeMessage.addHeader("header-name", "first-value");
        Mail mail = MailUtil.createMockMail2Recipients(mimeMessage);
        mailet.service(mail);
        replacedertThat(mail.getMessage().getHeader("header-name")).containsOnly("test-value", "first-value");
    }

    @Test
    void shouldThrowOnMessagingException() throws MessagingException {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty("name", "header-name").setProperty("value", "test-value").build();
        mailet.init(mailetConfig);
        Mail mail = mock(Mail.clreplaced);
        when(mail.getMessage()).thenThrow(new MessagingException());
        replacedertThatThrownBy(() -> mailet.service(mail)).isInstanceOf(MessagingException.clreplaced);
    }

    @Test
    void shouldThrowWhenNoConfiguration() {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").build();
        replacedertThatThrownBy(() -> mailet.init(mailetConfig)).isInstanceOf(MessagingException.clreplaced);
    }

    @Test
    void shouldThrowWhenNoValue() {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty("name", "correct").build();
        replacedertThatThrownBy(() -> mailet.init(mailetConfig)).isInstanceOf(MessagingException.clreplaced);
    }

    @Test
    void shouldThrowWhenNoHeader() {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty("value", "correct").build();
        replacedertThatThrownBy(() -> mailet.init(mailetConfig)).isInstanceOf(MessagingException.clreplaced);
    }

    @Test
    void shouldThrowWhenEmptyValue() {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty("value", "").setProperty("name", "correct").build();
        replacedertThatThrownBy(() -> mailet.init(mailetConfig)).isInstanceOf(MessagingException.clreplaced);
    }

    @Test
    void shouldThrowWhenEmptyHeader() {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty("name", "").setProperty("value", "correct").build();
        replacedertThatThrownBy(() -> mailet.init(mailetConfig)).isInstanceOf(MessagingException.clreplaced);
    }
}

19 View Complete Implementation : SetMailAttributeTest.java
Copyright Apache License 2.0
Author : apache
clreplaced SetMailAttributeTest {

    private Mailet mailet;

    @BeforeEach
    void setupMailet() {
        mailet = new SetMailAttribute();
    }

    @Test
    void shouldAddConfiguredAttributes() throws MessagingException {
        AttributeName name1 = AttributeName.of("org.apache.james.junit1");
        AttributeName name2 = AttributeName.of("org.apache.james.junit2");
        AttributeValue<String> value1 = AttributeValue.of("true");
        AttributeValue<String> value2 = AttributeValue.of("happy");
        Attribute attribute1 = new Attribute(name1, value1);
        Attribute attribute2 = new Attribute(name2, value2);
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty(name1.replacedtring(), value1.value()).setProperty(name2.replacedtring(), value2.value()).build();
        mailet.init(mailetConfig);
        Mail mail = MailUtil.createMockMail2Recipients(MimeMessageUtil.defaultMimeMessage());
        mailet.service(mail);
        replacedertThat(mail.getAttribute(name1)).contains(attribute1);
        replacedertThat(mail.getAttribute(name2)).contains(attribute2);
    }

    @Test
    void shouldAddNothingWhenNoConfiguredAttribute() throws MessagingException {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").build();
        mailet.init(mailetConfig);
        Mail mail = MailUtil.createMockMail2Recipients(MimeMessageUtil.defaultMimeMessage());
        mailet.service(mail);
        replacedertThat(mail.attributes()).isEmpty();
    }

    @Test
    void shouldOverwriteAttributeWhenAttributeAlreadyPresent() throws MessagingException {
        AttributeName name = AttributeName.of("org.apache.james.junit1");
        Attribute mailAttribute = new Attribute(name, AttributeValue.of("foo"));
        Attribute replacedAttribute = new Attribute(name, AttributeValue.of("bar"));
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty(name.replacedtring(), (String) replacedAttribute.getValue().value()).build();
        mailet.init(mailetConfig);
        Mail mail = MailUtil.createMockMail2Recipients(MimeMessageUtil.defaultMimeMessage());
        mail.setAttribute(mailAttribute);
        mailet.service(mail);
        replacedertThat(mail.getAttribute(name)).contains(replacedAttribute);
    }
}

19 View Complete Implementation : SetErrorMessageTest.java
Copyright Apache License 2.0
Author : apache
clreplaced SetErrorMessageTest {

    private static final String MY_MESSAGE = "my message";

    private Mailet testee;

    @BeforeEach
    void setUp() {
        testee = new SetErrorMessage();
    }

    @Test
    void initShouldThrowWhenNoErrorMessage() {
        replacedertThatThrownBy(() -> testee.init(FakeMailetConfig.builder().build())).isInstanceOf(IllegalStateException.clreplaced).hasMessage("'errorMessage' needs to be specified and cannot be empty");
    }

    @Test
    void initShouldThrowOnEmptyErrorMessage() {
        replacedertThatThrownBy(() -> testee.init(FakeMailetConfig.builder().setProperty("errorMessage", "").build())).isInstanceOf(IllegalStateException.clreplaced).hasMessage("'errorMessage' needs to be specified and cannot be empty");
    }

    @Test
    void serviceShouldPositionErrorMessage() throws Exception {
        testee.init(FakeMailetConfig.builder().setProperty("errorMessage", MY_MESSAGE).build());
        FakeMail myMail = FakeMail.builder().name("myMail").build();
        testee.service(myMail);
        replacedertThat(myMail.getErrorMessage()).isEqualTo(MY_MESSAGE);
    }

    @Test
    void serviceShouldOverwriteErrorMessage() throws Exception {
        testee.init(FakeMailetConfig.builder().setProperty("errorMessage", MY_MESSAGE).build());
        FakeMail myMail = FakeMail.builder().name("myMail").errorMessage("Old error message").build();
        testee.service(myMail);
        replacedertThat(myMail.getErrorMessage()).isEqualTo(MY_MESSAGE);
    }
}

19 View Complete Implementation : AddSubjectPrefixTest.java
Copyright Apache License 2.0
Author : apache
clreplaced AddSubjectPrefixTest {

    private Mailet mailet;

    @BeforeEach
    void setup() {
        mailet = new AddSubjectPrefix();
    }

    @Test
    void shouldAddPrefixToSubject() throws MessagingException {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty("subjectPrefix", "JUNIT").build();
        mailet.init(mailetConfig);
        MimeMessage mimeMessage = MailUtil.createMimeMessageWithSubject("test");
        FakeMail mail = MailUtil.createMockMail2Recipients(mimeMessage);
        mailet.service(mail);
        replacedertThat(mail.getMessage().getSubject()).isEqualTo("JUNIT test");
    }

    @Test
    void shouldAddPrefixToEncodedSubject() throws MessagingException {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty("subjectPrefix", "Русский").build();
        mailet.init(mailetConfig);
        String subject = "=?iso8859-15?Q?Beno=EEt_TELLIER_vous_a_d=E9pos=E9_des_fichiers?=";
        MimeMessage mimeMessage = MailUtil.createMimeMessageWithSubject(subject);
        FakeMail mail = MailUtil.createMockMail2Recipients(mimeMessage);
        mailet.service(mail);
        replacedertThat(mail.getMessage().getSubject()).startsWith("Русский").endsWith("Benoît TELLIER vous a déposé des fichiers");
    }

    @Test
    void shouldDefinePrefixreplacedubjectWhenNoSubject() throws MessagingException {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty("subjectPrefix", "JUNIT").build();
        mailet.init(mailetConfig);
        String noSubject = null;
        MimeMessage mimeMessage = MailUtil.createMimeMessageWithSubject(noSubject);
        FakeMail mail = MailUtil.createMockMail2Recipients(mimeMessage);
        mailet.service(mail);
        replacedertThat(mail.getMessage().getSubject()).isEqualTo("JUNIT");
    }

    @Test
    void shouldDefinePrefixreplacedubjectWhenEmptySubject() throws MessagingException {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty("subjectPrefix", "JUNIT").build();
        mailet.init(mailetConfig);
        MimeMessage mimeMessage = MailUtil.createMimeMessageWithSubject("");
        FakeMail mail = MailUtil.createMockMail2Recipients(mimeMessage);
        mailet.service(mail);
        replacedertThat(mail.getMessage().getSubject()).isEqualTo("JUNIT");
    }

    @Test
    void shouldThrowWhenEmptyPrefix() throws MessagingException {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty("subjectPrefix", "").build();
        replacedertThatThrownBy(() -> mailet.init(mailetConfig)).isInstanceOf(MessagingException.clreplaced);
    }
}

19 View Complete Implementation : AddFooterTest.java
Copyright Apache License 2.0
Author : apache
clreplaced AddFooterTest {

    private static final String MY_FOOTER = "my footer";

    private Mailet mailet;

    static clreplaced CharsetTuples implements ArgumentsProvider {

        @Override
        public Stream<? extends Arguments> provideArguments(ExtensionContext context) throws Exception {
            // javamail has its own charset handling logic, it needs to be exercised
            List<String> charsetNamesToTest = Lists.newArrayList("ANSI_X3.4-1968", "iso-ir-6", "ANSI_X3.4-1986", "ISO_646.irv:1991", "ASCII", "ISO646-US", "US-ASCII", "us", "IBM367", "cp367", "csASCII");
            return charsetNamesToTest.stream().flatMap(from -> charsetNamesToTest.stream().map(to -> Arguments.of(from, to)));
        }
    }

    @BeforeEach
    void setup() {
        mailet = new AddFooter();
    }

    @ParameterizedTest
    @ArgumentsSource(CharsetTuples.clreplaced)
    void shouldAddFooterWhenQuotedPrintableTextPlainMessage(String javaCharset, String javaMailCharset) throws MessagingException, IOException {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty("text", "------ " + MY_FOOTER + " à/€ ------").build();
        mailet.init(mailetConfig);
        String quotedPrintableTextPlainMessage = Joiner.on("\r\n").join("Subject: test", "Content-Type: text/plain; charset=ISO-8859-15", "MIME-Version: 1.0", "Content-Transfer-Encoding: quoted-printable", "", "Test=E0 and one =A4", "");
        String expectedFooter = "------ " + MY_FOOTER + " =E0/=A4 ------";
        Mail mail = FakeMail.fromMime(quotedPrintableTextPlainMessage, javaCharset, javaMailCharset);
        mailet.service(mail);
        replacedertThat(MailUtil.toString(mail, javaCharset)).endsWith(expectedFooter);
    }

    @ParameterizedTest
    @ArgumentsSource(CharsetTuples.clreplaced)
    void shouldEnsureCarriageReturnWhenAddFooterWithTextPlainMessage(String javaCharset, String javaMailCharset) throws MessagingException, IOException {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty("text", "------ " + MY_FOOTER + " à/€ ------").build();
        mailet.init(mailetConfig);
        String quotedPrintableTextPlainMessage = Joiner.on("\r\n").join("Subject: test", "Content-Type: text/plain; charset=ISO-8859-15", "MIME-Version: 1.0", "Content-Transfer-Encoding: quoted-printable", "", "Test=E0 and one =A4");
        String expectedFooter = "------ " + MY_FOOTER + " =E0/=A4 ------";
        Mail mail = FakeMail.fromMime(quotedPrintableTextPlainMessage, javaCharset, javaMailCharset);
        mailet.service(mail);
        replacedertThat(MailUtil.toString(mail, javaCharset)).endsWith("\r\n" + expectedFooter);
    }

    @ParameterizedTest
    @ArgumentsSource(CharsetTuples.clreplaced)
    void shouldNotAddFooterWhenUnsupportedEncoding(String javaCharset, String javaMailCharset) throws MessagingException, IOException {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty("text", "------ " + MY_FOOTER + " à/€ ------").build();
        mailet.init(mailetConfig);
        String quotedPrintableTextPlainMessage = Joiner.on("\r\n").join("Subject: test", "Content-Type: text/plain; charset=UNSUPPORTED_ENCODING", "MIME-Version: 1.0", "Content-Transfer-Encoding: quoted-printable", "", "Test=E0 and one", "");
        Mail mail = FakeMail.fromMime(quotedPrintableTextPlainMessage, javaCharset, javaMailCharset);
        mailet.service(mail);
        replacedertThat(MailUtil.toString(mail, javaCharset)).doesNotContain(MY_FOOTER);
    }

    @ParameterizedTest
    @ArgumentsSource(CharsetTuples.clreplaced)
    void shouldNotAddFooterWhenUnsupportedTextContentType(String javaCharset, String javaMailCharset) throws MessagingException, IOException {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty("text", "------ " + MY_FOOTER + " à/€ ------").build();
        mailet.init(mailetConfig);
        String quotedPrintableTextPlainMessage = Joiner.on("\r\n").join("Subject: test", "Content-Type: text/calendar; charset=ASCII", "MIME-Version: 1.0", "Content-Transfer-Encoding: quoted-printable", "", "Test=E0 and one", "");
        Mail mail = FakeMail.fromMime(quotedPrintableTextPlainMessage, javaCharset, javaMailCharset);
        mailet.service(mail);
        replacedertThat(MailUtil.toString(mail, javaCharset)).isEqualTo(quotedPrintableTextPlainMessage);
    }

    /*
     * Test for JAMES-443
     * This should not add the header and should leave the multipart/mixed Content-Type intact
     */
    @ParameterizedTest
    @ArgumentsSource(CharsetTuples.clreplaced)
    void shouldNotAddFooterWhenNestedUnsupportedMultipart(String javaCharset, String javaMailCharset) throws MessagingException, IOException {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty("text", "------ " + MY_FOOTER + " à/€ ------").build();
        mailet.init(mailetConfig);
        String quotedPrintableMultipartMixedMessage = Joiner.on("\r\n").join("MIME-Version: 1.0", "Content-Type: multipart/mixed; boundary=\"===============0204599088==\"", "", "This is a cryptographically signed message in MIME format.", "", "--===============0204599088==", "Content-Type: multipart/unsupported; boundary=\"------------ms050404020900070803030808\"", "", "--------------ms050404020900070803030808", "Content-Type: text/plain; charset=ISO-8859-1", "", "test", "", "--------------ms050404020900070803030808--", "", "--===============0204599088==--", "");
        Mail mail = FakeMail.fromMime(quotedPrintableMultipartMixedMessage, javaCharset, javaMailCharset);
        mailet.service(mail);
        replacedertThat(MailUtil.toString(mail, javaCharset)).isEqualTo(quotedPrintableMultipartMixedMessage);
    }

    /*
     * Test for JAMES-368
     */
    @ParameterizedTest
    @ArgumentsSource(CharsetTuples.clreplaced)
    void shouldAddFooterWhenMultipartRelatedHtmlMessage(String javaCharset, String javaMailCharset) throws MessagingException, IOException {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty("text", "------ " + MY_FOOTER + " à/€ ------").build();
        mailet.init(mailetConfig);
        String htmlMultipartRelatedMessagePart1 = Joiner.on("\r\n").join("MIME-Version: 1.0", "Subject: test", "Content-Type: multipart/related;", "  boundary=\"------------050206010102010306090507\"", "", "--------------050206010102010306090507", "Content-Type: text/html; charset=ISO-8859-15", "Content-Transfer-Encoding: quoted-printable", "", "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">", "<html>", "<head>", "<meta content=3D\"text/html;charset=3DISO-8859-15\" http-equiv=3D\"Content-Typ=", "e\">", "</head>", "<body bgcolor=3D\"#ffffff\" text=3D\"#000000\">", "<br>", "<div clreplaced=3D\"moz-signature\">-- <br>", "<img src=3D\"cid:[email protected]\" border=3D\"0\"></div>", "");
        String htmlMultipartRelatedMessagePart2 = Joiner.on("\r\n").join("</body>", "</html>", "", "--------------050206010102010306090507", "Content-Type: image/gif", "Content-Transfer-Encoding: base64", "Content-ID: <[email protected]>", "Content-Disposition: inline;", "", "YQ==", "--------------050206010102010306090507--", "");
        String expectedFooter = "<br />------ " + MY_FOOTER + " =E0/=A4 ------";
        Mail mail = FakeMail.fromMime(htmlMultipartRelatedMessagePart1 + htmlMultipartRelatedMessagePart2, javaCharset, javaMailCharset);
        mailet.service(mail);
        replacedertThat(MailUtil.toString(mail, javaCharset)).contains(expectedFooter);
    }

    @ParameterizedTest
    @ArgumentsSource(CharsetTuples.clreplaced)
    void shouldAddFooterWhenMultipartAlternivateMessage(String javaCharset, String javaMailCharset) throws MessagingException, IOException {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty("text", "------ " + MY_FOOTER + " à/€ ------").build();
        mailet.init(mailetConfig);
        String multipartAlternativeMessage = Joiner.on("\r\n").join("Subject: test", "Content-Type: multipart/alternative;", "    boundary=\"--==--\"", "MIME-Version: 1.0", "", "----==--", "Content-Type: text/plain;", "    charset=\"ISO-8859-15\"", "Content-Transfer-Encoding: quoted-printable", "", "Test=E0 and @=80", "", "----==--", "Content-Type: text/html;", "    charset=\"CP1252\"", "Content-Transfer-Encoding: quoted-printable", "", "<html><body>test =80 ss</body></html>", "----==----");
        Mail mail = FakeMail.fromMime(multipartAlternativeMessage, javaCharset, javaMailCharset);
        mailet.service(mail);
        replacedertThat(MailUtil.toString(mail, javaCharset)).matches("(.|\n|\r)*" + MY_FOOTER + "(.|\n|\r)*" + MY_FOOTER + "(.|\n|\r)*");
    }

    @ParameterizedTest
    @ArgumentsSource(CharsetTuples.clreplaced)
    void shouldAddFooterWhenHtmlMessageWithMixedCaseBodyTag(String javaCharset, String javaMailCharset) throws MessagingException, IOException {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty("text", "------ " + MY_FOOTER + " à/€ ------").build();
        mailet.init(mailetConfig);
        String htmlMessage = Joiner.on("\r\n").join("Subject: test", "MIME-Version: 1.0", "Content-Type: text/html;", "    charset=\"CP1252\"", "Content-Transfer-Encoding: quoted-printable", "", "<html><body>test =80 ss</bOdY></html>", "");
        Mail mail = FakeMail.fromMime(htmlMessage, javaCharset, javaMailCharset);
        mailet.service(mail);
        String htmlContent = "<html><body>test =80 ss<br />------ " + MY_FOOTER + " =E0/=80 ------</bOdY></html>";
        replacedertThat(MailUtil.toString(mail, javaCharset)).contains(htmlContent);
    }

    @ParameterizedTest
    @ArgumentsSource(CharsetTuples.clreplaced)
    void shouldAddFooterWhenHtmlMessageWithNoBodyTag(String javaCharset, String javaMailCharset) throws MessagingException, IOException {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty("text", "------ " + MY_FOOTER + " à/€ ------").build();
        mailet.init(mailetConfig);
        String htmlMessage = Joiner.on("\r\n").join("Subject: test", "MIME-Version: 1.0", "Content-Type: text/html;", "    charset=\"CP1252\"", "Content-Transfer-Encoding: quoted-printable", "", "<html><body>test =80 ss", "");
        Mail mail = FakeMail.fromMime(htmlMessage, javaCharset, javaMailCharset);
        mailet.service(mail);
        String expectedFooter = "<br />------ " + MY_FOOTER + " =E0/=80 ------";
        replacedertThat(MailUtil.toString(mail, javaCharset)).endsWith(expectedFooter);
    }
}

19 View Complete Implementation : NullTest.java
Copyright Apache License 2.0
Author : apache
public clreplaced NullTest {

    private Mailet mailet;

    @BeforeEach
    public void setup() {
        mailet = new Null();
    }

    @Test
    public void shouldChangeStateToGhost() throws MessagingException {
        FakeMail mail = FakeMail.builder().name("mail").build();
        mailet.service(mail);
        replacedertThat(mail.getState()).isEqualTo("ghost");
    }
}

18 View Complete Implementation : ToProcessorTest.java
Copyright Apache License 2.0
Author : apache
clreplaced ToProcessorTest {

    private Mailet mailet;

    private FakeMailContext mailContext;

    @BeforeEach
    void setup() {
        mailet = new ToProcessor();
        Logger logger = mock(Logger.clreplaced);
        mailContext = FakeMailContext.builder().logger(logger).build();
    }

    @Test
    void getMailetInfoShouldReturnValue() {
        replacedertThat(mailet.getMailetInfo()).isEqualTo("ToProcessor Mailet");
    }

    @Test
    void initShouldThrowWhenProcessorIsNotGiven() {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").mailetContext(mailContext).setProperty("notice", "error in message").build();
        replacedertThatThrownBy(() -> mailet.init(mailetConfig)).isInstanceOf(MailetException.clreplaced);
    }

    @Test
    void serviceShouldSetTheStateOfTheMail() throws MessagingException {
        String processor = "error";
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").mailetContext(mailContext).setProperty("processor", processor).build();
        mailet.init(mailetConfig);
        Mail mail = FakeMail.builder().name("mail").recipients(new MailAddress("[email protected]"), new MailAddress("[email protected]")).build();
        mailet.service(mail);
        replacedertThat(mail.getState()).isEqualTo(processor);
    }

    @Test
    void serviceShouldSetTheErrorMessageOfTheMailWhenNotAlreadySet() throws MessagingException {
        String processor = "error";
        String notice = "error in message";
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").mailetContext(mailContext).setProperty("processor", processor).setProperty("notice", notice).build();
        mailet.init(mailetConfig);
        Mail mail = FakeMail.builder().name("mail").recipients(new MailAddress("[email protected]"), new MailAddress("[email protected]")).build();
        mailet.service(mail);
        replacedertThat(mail.getErrorMessage()).isEqualTo(notice);
    }

    @Test
    void serviceShouldAppendTheErrorMessageOfTheMailWhenSomeErrorMessageOnMail() throws MessagingException {
        String processor = "error";
        String notice = "error in message";
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").mailetContext(mailContext).setProperty("processor", processor).setProperty("notice", notice).build();
        mailet.init(mailetConfig);
        Mail mail = FakeMail.builder().name("mail").recipients(new MailAddress("[email protected]"), new MailAddress("[email protected]")).build();
        String initialErrorMessage = "first";
        mail.setErrorMessage(initialErrorMessage);
        mailet.service(mail);
        replacedertThat(mail.getErrorMessage()).isEqualTo(initialErrorMessage + "\r\n" + notice);
    }
}

18 View Complete Implementation : JMXStateMailetProcessorListener.java
Copyright Apache License 2.0
Author : apache
/**
 * Register the Mailets as JMX MBeans
 */
private void registerMailets(String parentMBeanName, Iterator<Mailet> mailets) throws JMException {
    int i = 0;
    while (mailets.hasNext()) {
        Mailet mailet = mailets.next();
        MailetManagement mailetManagement = new MailetManagement(mailet.getMailetConfig());
        String mailetMBeanName = parentMBeanName + ",subtype=mailet,index=" + (i++) + ",mailetname=" + mailetManagement.getMailetName();
        registerMBean(mailetMBeanName, mailetManagement);
        mailetMap.put(mailet, mailetManagement);
    }
}

18 View Complete Implementation : MatcherMailetPair.java
Copyright Apache License 2.0
Author : apache
/**
 * A pair of {@link Matcher} and {@link Mailet}
 */
public clreplaced MatcherMailetPair {

    private final Matcher matcher;

    private final Mailet mailet;

    public MatcherMailetPair(Matcher matcher, Mailet mailet) {
        this.matcher = matcher;
        this.mailet = mailet;
    }

    /**
     * Return the {@link Matcher} of this pair
     *
     * @return matcher
     */
    public Matcher getMatcher() {
        return matcher;
    }

    /**
     * Return the {@link Mailet} of this pair
     *
     * @return mailet
     */
    public Mailet getMailet() {
        return mailet;
    }

    public String getOnMatchException() {
        return mailet.getMailetConfig().getInitParameter("onMatchException");
    }
}

18 View Complete Implementation : RemoveAllMailAttributesTest.java
Copyright Apache License 2.0
Author : apache
public clreplaced RemoveAllMailAttributesTest {

    private Mail mail;

    private Mailet mailet;

    @BeforeEach
    public void setUp() throws Exception {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").build();
        mailet = new RemoveAllMailAttributes();
        mailet.init(mailetConfig);
    }

    @Test
    public void getMailetInfoShouldReturnValue() {
        replacedertThat(mailet.getMailetInfo()).isEqualTo("Remove All Mail Attributes Mailet");
    }

    @Test
    public void serviceShouldRemoveAllMailAttributes() throws MessagingException {
        mail = MailUtil.createMockMail2Recipients();
        mail.setAttribute(Attribute.convertToAttribute("org.apache.james.test.junit", "true"));
        mailet.service(mail);
        replacedertThat(mail.attributes()).isEmpty();
    }

    @Test
    public void serviceShouldRemoveAllMailAttributesWhenNone() throws MessagingException {
        mail = MailUtil.createMockMail2Recipients();
        mailet.service(mail);
        replacedertThat(mail.attributes()).isEmpty();
    }
}

17 View Complete Implementation : JMXStateMailetProcessorListener.java
Copyright Apache License 2.0
Author : apache
@Override
public void afterMailet(Mailet m, String mailName, String state, long processTime, Exception e) {
    MailetManagement mgmt = mailetMap.get(m);
    if (mgmt != null) {
        mgmt.update(processTime, e == null);
    }
}

17 View Complete Implementation : CamelProcessor.java
Copyright Apache License 2.0
Author : apache
/**
 * Mailet wrapper which execute a Mailet in a Processor
 */
public clreplaced CamelProcessor {

    private static final Logger LOGGER = LoggerFactory.getLogger(CamelProcessor.clreplaced);

    private final MetricFactory metricFactory;

    private final Mailet mailet;

    private final CamelMailetProcessor processor;

    public CamelProcessor(MetricFactory metricFactory, CamelMailetProcessor processor, Mailet mailet) {
        this.metricFactory = metricFactory;
        this.processor = processor;
        this.mailet = mailet;
    }

    /**
     * Call the wrapped mailet for the exchange
     */
    public void process(Mail mail) throws Exception {
        long start = System.currentTimeMillis();
        TimeMetric timeMetric = metricFactory.timer(mailet.getClreplaced().getSimpleName());
        Exception ex = null;
        try (Closeable closeable = MDCBuilder.create().addContext(MDCBuilder.PROTOCOL, "MAILET").addContext(MDCBuilder.ACTION, "MAILET").addContext(MDCBuilder.HOST, mail.getRemoteHost()).addContext("state", mail.getState()).addContext("mailet", mailet.getClreplaced().getSimpleName()).addContext("mail", mail.getName()).addContext("recipients", ImmutableList.copyOf(mail.getRecipients())).addContext("sender", mail.getMaybeSender()).build()) {
            MailetPipelineLogging.logBeginOfMailetProcess(mailet, mail);
            mailet.service(mail);
        } catch (Exception me) {
            ex = me;
            String onMailetException = null;
            MailetConfig mailetConfig = mailet.getMailetConfig();
            if (mailetConfig instanceof MailetConfigImpl) {
                onMailetException = mailetConfig.getInitParameter("onMailetException");
            }
            if (onMailetException == null) {
                onMailetException = Mail.ERROR;
            } else {
                onMailetException = onMailetException.trim().toLowerCase(Locale.US);
            }
            if (onMailetException.equalsIgnoreCase("ignore")) {
                // ignore the exception and continue
                // this option should not be used if the mail object can be
                // changed by the mailet
                LOGGER.warn("Encountered error while executing mailet {}. Ignoring it.", mailet, ex);
                ProcessorUtil.verifyMailAddresses(mail.getRecipients());
            } else {
                ProcessorUtil.handleException(me, mail, mailet.getMailetConfig().getMailetName(), onMailetException, LOGGER);
            }
        } finally {
            timeMetric.stopAndPublish();
            MailetPipelineLogging.logEndOfMailetProcess(mailet, mail);
            List<MailetProcessorListener> listeners = processor.getListeners();
            long complete = System.currentTimeMillis() - start;
            if (mail.getRecipients().isEmpty()) {
                mail.setState(Mail.GHOST);
            }
            for (MailetProcessorListener listener : listeners) {
                listener.afterMailet(mailet, mail.getName(), mail.getState(), complete, ex);
            }
        }
    }
}

17 View Complete Implementation : MailAttributesListToMimeHeadersTest.java
Copyright Apache License 2.0
Author : apache
clreplaced MailAttributesListToMimeHeadersTest {

    private static final String VALUE_1_1 = "test1.1";

    private static final String VALUE_1_2 = "test1.2";

    private static final String VALUE_2_1 = "test2.1";

    private static final String VALUE_2_2 = "test2.2";

    private static final ImmutableList<String> MAIL_ATTRIBUTE_VALUE1 = ImmutableList.of(VALUE_1_1, VALUE_1_2);

    private static final ImmutableList<String> MAIL_ATTRIBUTE_VALUE2 = ImmutableList.of(VALUE_2_1, VALUE_2_2);

    private static final String MAIL_ATTRIBUTE_NAME1 = "org.apache.james.test";

    private static final String MAIL_ATTRIBUTE_NAME2 = "org.apache.james.test2";

    private static final String HEADER_NAME1 = "JUNIT";

    private static final String HEADER_NAME2 = "JUNIT2";

    private static final Attribute MAIL_ATTRIBUTE1 = Attribute.convertToAttribute(MAIL_ATTRIBUTE_NAME1, MAIL_ATTRIBUTE_VALUE1);

    private static final Attribute MAIL_ATTRIBUTE2 = Attribute.convertToAttribute(MAIL_ATTRIBUTE_NAME2, MAIL_ATTRIBUTE_VALUE2);

    private Mailet mailet;

    @BeforeEach
    void setup() {
        mailet = new MailAttributesListToMimeHeaders();
    }

    @Test
    void shouldThrowMessagingExceptionIfMappingIsNotGiven() {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").build();
        replacedertThatThrownBy(() -> mailet.init(mailetConfig)).isInstanceOf(MessagingException.clreplaced);
    }

    @Test
    void shouldThrowMessagingExceptionIfMappingIsEmpty() {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty("simplemmapping", "").build();
        replacedertThatThrownBy(() -> mailet.init(mailetConfig)).isInstanceOf(MessagingException.clreplaced);
    }

    @Test
    void shouldIgnoreAttributeOfMappingThatDoesNotExistOnTheMessage() throws MessagingException {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty("simplemapping", MAIL_ATTRIBUTE_NAME1 + "; " + HEADER_NAME1 + "," + MAIL_ATTRIBUTE_NAME2 + "; " + HEADER_NAME2 + "," + "another.attribute" + "; " + "Another-Header").build();
        mailet.init(mailetConfig);
        FakeMail mail = FakeMail.builder().name("mail").mimeMessage(MailUtil.createMimeMessage()).attribute(MAIL_ATTRIBUTE1).attribute(MAIL_ATTRIBUTE2).build();
        mailet.service(mail);
        replacedertThat(mail.getMessage().getHeader("another.attribute")).isNull();
    }

    @Test
    void shouldWorkWithMappingWithASingleBinding() throws MessagingException {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty("simplemapping", MAIL_ATTRIBUTE_NAME1 + "; " + HEADER_NAME1).build();
        mailet.init(mailetConfig);
        FakeMail mail = FakeMail.builder().name("mail").mimeMessage(MailUtil.createMimeMessage()).attribute(MAIL_ATTRIBUTE1).build();
        mailet.service(mail);
        replacedertThat(mail.getMessage().getHeader(HEADER_NAME1)).containsExactly(VALUE_1_1, VALUE_1_2);
    }

    @Test
    void shouldIgnoreNullValueInsideList() throws MessagingException {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty("simplemapping", MAIL_ATTRIBUTE_NAME1 + "; " + HEADER_NAME1).build();
        mailet.init(mailetConfig);
        ArrayList<String> listWithNull = new ArrayList<>();
        listWithNull.add("1");
        listWithNull.add(null);
        listWithNull.add("2");
        FakeMail mail = FakeMail.builder().name("mail").mimeMessage(MailUtil.createMimeMessage()).attribute(Attribute.convertToAttribute(MAIL_ATTRIBUTE_NAME1, listWithNull)).build();
        mailet.service(mail);
        replacedertThat(mail.getMessage().getHeader(HEADER_NAME1)).containsExactly("1", "2");
    }

    @Test
    void shouldPutAttributesIntoHeadersWhenMappingDefined() throws MessagingException {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty("simplemapping", MAIL_ATTRIBUTE_NAME1 + "; " + HEADER_NAME1 + "," + MAIL_ATTRIBUTE_NAME2 + "; " + HEADER_NAME2 + "," + "another.attribute" + "; " + "Another-Header").build();
        mailet.init(mailetConfig);
        FakeMail mail = FakeMail.builder().name("mail").mimeMessage(MailUtil.createMimeMessage()).attribute(MAIL_ATTRIBUTE1).attribute(MAIL_ATTRIBUTE2).attribute(Attribute.convertToAttribute("unmatched.attribute", "value")).build();
        mailet.service(mail);
        replacedertThat(mail.getMessage().getHeader(HEADER_NAME1)).containsExactlyElementsOf(MAIL_ATTRIBUTE_VALUE1);
        replacedertThat(mail.getMessage().getHeader(HEADER_NAME2)).containsExactlyElementsOf(MAIL_ATTRIBUTE_VALUE2);
    }

    @Test
    void shouldNotRemovePreviousAttributeValueWhenAttributeAlreadyPresent() throws MessagingException {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty("simplemapping", MAIL_ATTRIBUTE_NAME1 + "; " + HEADER_NAME1).build();
        mailet.init(mailetConfig);
        String firstValue = "first value";
        FakeMail mail = FakeMail.builder().name("mail").mimeMessage(MimeMessageBuilder.mimeMessageBuilder().addHeader(HEADER_NAME1, firstValue)).attribute(MAIL_ATTRIBUTE1).build();
        mailet.service(mail);
        replacedertThat(mail.getMessage().getHeader(HEADER_NAME1)).containsOnly(VALUE_1_1, VALUE_1_2, firstValue);
    }

    @Test
    void shouldFilterAttributeOfWrongClreplaced() throws MessagingException {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty("simplemapping", MAIL_ATTRIBUTE_NAME1 + "; " + HEADER_NAME1 + "," + MAIL_ATTRIBUTE_NAME2 + "; " + HEADER_NAME2).build();
        mailet.init(mailetConfig);
        FakeMail mail = FakeMail.builder().name("mail").mimeMessage(MimeMessageBuilder.mimeMessageBuilder()).attribute(Attribute.convertToAttribute(MAIL_ATTRIBUTE_NAME1, 3L)).attribute(MAIL_ATTRIBUTE2).build();
        mailet.service(mail);
        replacedertThat(mail.getMessage().getHeader(HEADER_NAME1)).isNull();
        replacedertThat(mail.getMessage().getHeader(HEADER_NAME2)).containsExactlyElementsOf(MAIL_ATTRIBUTE_VALUE2);
    }

    @Test
    void shouldFilterAttributeElementsOfWrongClreplaced() throws MessagingException {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty("simplemapping", MAIL_ATTRIBUTE_NAME1 + "; " + HEADER_NAME1).build();
        mailet.init(mailetConfig);
        String value = "value";
        FakeMail mail = FakeMail.builder().name("mail").mimeMessage(MimeMessageBuilder.mimeMessageBuilder()).attribute(Attribute.convertToAttribute(MAIL_ATTRIBUTE_NAME1, ImmutableList.of(3L, value))).build();
        mailet.service(mail);
        replacedertThat(mail.getMessage().getHeader(HEADER_NAME1)).containsOnly(value);
    }

    @Test
    void shouldThrowAtInitWhenNoSemicolumnInConfigurationEntry() {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty("simplemapping", "invalidConfigEntry").build();
        replacedertThatThrownBy(() -> mailet.init(mailetConfig)).isInstanceOf(IllegalArgumentException.clreplaced);
    }

    @Test
    void shouldThrowAtInitWhenTwoSemicolumnsInConfigurationEntry() {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty("simplemapping", "first;second;third").build();
        replacedertThatThrownBy(() -> mailet.init(mailetConfig)).isInstanceOf(IllegalArgumentException.clreplaced);
    }

    @Test
    void shouldThrowAtInitWhenNoConfigurationEntry() {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").build();
        replacedertThatThrownBy(() -> mailet.init(mailetConfig)).isInstanceOf(MessagingException.clreplaced);
    }
}

17 View Complete Implementation : MailAttributesToMimeHeadersTest.java
Copyright Apache License 2.0
Author : apache
clreplaced MailAttributesToMimeHeadersTest {

    private Mailet mailet;

    private static final String HEADER_NAME1 = "JUNIT";

    private static final String HEADER_NAME2 = "JUNIT2";

    private static final AttributeName MAIL_ATTRIBUTE_NAME1 = AttributeName.of("org.apache.james.test");

    private static final AttributeValue<String> MAIL_ATTRIBUTE_VALUE1 = AttributeValue.of("test1");

    private static final Attribute MAIL_ATTRIBUTE1 = new Attribute(MAIL_ATTRIBUTE_NAME1, MAIL_ATTRIBUTE_VALUE1);

    private static final AttributeName MAIL_ATTRIBUTE_NAME2 = AttributeName.of("org.apache.james.test2");

    private static final AttributeValue<String> MAIL_ATTRIBUTE_VALUE2 = AttributeValue.of("test2");

    private static final Attribute MAIL_ATTRIBUTE2 = new Attribute(MAIL_ATTRIBUTE_NAME2, MAIL_ATTRIBUTE_VALUE2);

    @BeforeEach
    void setup() {
        mailet = new MailAttributesToMimeHeaders();
    }

    @Test
    void shouldThrowMessagingExceptionIfMappingIsNotGiven() {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").build();
        replacedertThatThrownBy(() -> mailet.init(mailetConfig)).isInstanceOf(MessagingException.clreplaced);
    }

    @Test
    void shouldThrowMessagingExceptionIfMappingIsEmpty() {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty("simplemmapping", "").build();
        replacedertThatThrownBy(() -> mailet.init(mailetConfig)).isInstanceOf(MessagingException.clreplaced);
    }

    @Test
    void shouldIgnoreAttributeOfMappingThatDoesNotExistOnTheMessage() throws MessagingException {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty("simplemapping", MAIL_ATTRIBUTE_NAME1.replacedtring() + "; " + HEADER_NAME1 + "," + MAIL_ATTRIBUTE_NAME2.replacedtring() + "; " + HEADER_NAME2 + "," + "another.attribute" + "; " + "Another-Header").build();
        mailet.init(mailetConfig);
        FakeMail mockedMail = MailUtil.createMockMail2Recipients(MailUtil.createMimeMessage());
        mockedMail.setAttribute(MAIL_ATTRIBUTE1);
        mockedMail.setAttribute(MAIL_ATTRIBUTE2);
        mailet.service(mockedMail);
        replacedertThat(mockedMail.getMessage().getHeader("another.attribute")).isNull();
    }

    @Test
    void shouldWorkWithMappingWithASingleBinding() throws MessagingException {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty("simplemapping", MAIL_ATTRIBUTE_NAME1.replacedtring() + "; " + HEADER_NAME1).build();
        mailet.init(mailetConfig);
        FakeMail mockedMail = MailUtil.createMockMail2Recipients(MailUtil.createMimeMessage());
        mockedMail.setAttribute(MAIL_ATTRIBUTE1);
        mailet.service(mockedMail);
        replacedertThat(mockedMail.getMessage().getHeader(HEADER_NAME1)).containsExactly(MAIL_ATTRIBUTE_VALUE1.value());
    }

    @Test
    void shouldPutAttributesIntoHeadersWhenMappingDefined() throws MessagingException {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty("simplemapping", MAIL_ATTRIBUTE_NAME1.replacedtring() + "; " + HEADER_NAME1 + "," + MAIL_ATTRIBUTE_NAME2.replacedtring() + "; " + HEADER_NAME2 + "," + "another.attribute" + "; " + "Another-Header").build();
        mailet.init(mailetConfig);
        FakeMail mockedMail = MailUtil.createMockMail2Recipients(MailUtil.createMimeMessage());
        mockedMail.setAttribute(MAIL_ATTRIBUTE1);
        mockedMail.setAttribute(MAIL_ATTRIBUTE2);
        mockedMail.setAttribute(Attribute.convertToAttribute("unmatched.attribute", "value"));
        mailet.service(mockedMail);
        replacedertThat(mockedMail.getMessage().getHeader(HEADER_NAME1)).containsExactly(MAIL_ATTRIBUTE_VALUE1.value());
        replacedertThat(mockedMail.getMessage().getHeader(HEADER_NAME2)).containsExactly(MAIL_ATTRIBUTE_VALUE2.value());
    }

    @Test
    void shouldAddAttributeIntoHeadersWhenHeaderAlreadyPresent() throws MessagingException {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty("simplemapping", MAIL_ATTRIBUTE_NAME1.replacedtring() + "; " + HEADER_NAME1).build();
        mailet.init(mailetConfig);
        FakeMail mockedMail = MailUtil.createMockMail2Recipients(MimeMessageBuilder.mimeMessageBuilder().addHeader(HEADER_NAME1, "first value").build());
        mockedMail.setAttribute(MAIL_ATTRIBUTE1);
        mailet.service(mockedMail);
        replacedertThat(mockedMail.getMessage().getHeader(HEADER_NAME1)).containsExactly("first value", MAIL_ATTRIBUTE_VALUE1.value());
    }

    @Test
    void shouldThrowAtInitWhenNoSemicolumnInConfigurationEntry() {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty("simplemapping", "invalidConfigEntry").build();
        replacedertThatThrownBy(() -> mailet.init(mailetConfig)).isInstanceOf(IllegalArgumentException.clreplaced);
    }

    @Test
    void shouldThrowAtInitWhenTwoSemicolumnsInConfigurationEntry() {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty("simplemapping", "first;second;third").build();
        replacedertThatThrownBy(() -> mailet.init(mailetConfig)).isInstanceOf(IllegalArgumentException.clreplaced);
    }

    @Test
    void shouldThrowAtInitWhenNoConfigurationEntry() {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").build();
        replacedertThatThrownBy(() -> mailet.init(mailetConfig)).isInstanceOf(MessagingException.clreplaced);
    }
}

17 View Complete Implementation : RemoveMailAttributeTest.java
Copyright Apache License 2.0
Author : apache
clreplaced RemoveMailAttributeTest {

    private static final AttributeName ATTRIBUTE_1 = AttributeName.of("attribute1");

    private static final AttributeName ATTRIBUTE_2 = AttributeName.of("attribute2");

    private static final AttributeName ATTRIBUTE_3 = AttributeName.of("attribute3");

    private static final Attribute VALUE_1 = new Attribute(ATTRIBUTE_1, AttributeValue.of("value1"));

    private static final Attribute VALUE_2 = new Attribute(ATTRIBUTE_2, AttributeValue.of("value2"));

    private static final Attribute VALUE_3 = new Attribute(ATTRIBUTE_3, AttributeValue.of("value3"));

    private static final String ATTRIBUTE1_ATTRIBUTE2 = "attribute1, attribute2";

    private Mailet removeMailet;

    @BeforeEach
    void setup() {
        removeMailet = new RemoveMailAttribute();
    }

    @Test
    void getMailetInfoShouldReturnCorrectInformation() {
        replacedertThat(removeMailet.getMailetInfo()).isEqualTo("Remove Mail Attribute Mailet");
    }

    @Test
    void initShouldThrowExceptionIfMailetConfigDoesNotContainAttribute() {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").build();
        replacedertThatThrownBy(() -> removeMailet.init(mailetConfig)).isInstanceOf(MailetException.clreplaced);
    }

    @Test
    void serviceShouldThrowExceptionWithMailNull() {
        replacedertThatThrownBy(() -> removeMailet.service(null)).isInstanceOf(NullPointerException.clreplaced);
    }

    @Test
    void serviceShouldDoNothingWhenMailHasEmptyAttribute() throws MessagingException {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty(RemoveMailAttribute.MAILET_NAME_PARAMETER, ATTRIBUTE1_ATTRIBUTE2).build();
        removeMailet.init(mailetConfig);
        Mail mail = FakeMail.builder().name("mail").build();
        removeMailet.service(mail);
        replacedertThat(mail.attributes()).isEmpty();
    }

    @Test
    void serviceShouldDoNothingWhenMailDoNotMatchAttribute() throws MessagingException {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty(RemoveMailAttribute.MAILET_NAME_PARAMETER, ATTRIBUTE1_ATTRIBUTE2).build();
        removeMailet.init(mailetConfig);
        Mail mail = FakeMail.builder().name("mail").attribute(VALUE_3).build();
        removeMailet.service(mail);
        replacedertThat(mail.attributes()).containsExactly(VALUE_3);
    }

    @Test
    void serviceShouldRemoveSpecifiedAttribute() throws MessagingException {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty(RemoveMailAttribute.MAILET_NAME_PARAMETER, ATTRIBUTE_1.replacedtring()).build();
        removeMailet.init(mailetConfig);
        Mail mail = FakeMail.builder().name("mail").attribute(VALUE_1).attribute(VALUE_2).attribute(VALUE_3).build();
        removeMailet.service(mail);
        replacedertThat(mail.attributes()).containsOnly(VALUE_2, VALUE_3);
    }

    @Test
    void serviceShouldRemoveSpecifiedAttributes() throws MessagingException {
        FakeMailetConfig mailetConfig = FakeMailetConfig.builder().mailetName("Test").setProperty(RemoveMailAttribute.MAILET_NAME_PARAMETER, ATTRIBUTE1_ATTRIBUTE2).build();
        removeMailet.init(mailetConfig);
        Mail mail = FakeMail.builder().name("mail").attribute(VALUE_1).attribute(VALUE_2).attribute(VALUE_3).build();
        removeMailet.service(mail);
        replacedertThat(mail.attributes()).containsExactly(VALUE_3);
    }
}

17 View Complete Implementation : GuiceMailetLoader.java
Copyright Apache License 2.0
Author : apache
@Override
public Mailet getMailet(MailetConfig config) throws MessagingException {
    try {
        ClreplacedName clreplacedName = new ClreplacedName(config.getMailetName());
        Mailet result = genericLoader.<Mailet>withNamingSheme(MAILET_NAMING_SCHEME).instantiate(clreplacedName);
        result.init(resolveConfiguration(result, config));
        return result;
    } catch (Exception e) {
        throw new MessagingException("Can not load mailet " + config.getMailetName(), e);
    }
}

17 View Complete Implementation : MailetLoaderBeanFactory.java
Copyright Apache License 2.0
Author : apache
@Override
public Mailet getMailet(MailetConfig config) throws MessagingException {
    String mailetName = config.getMailetName();
    try {
        final Mailet mailet = load(mailetName);
        // init the mailet
        mailet.init(config);
        return mailet;
    } catch (MessagingException me) {
        throw me;
    } catch (Exception e) {
        throw loadFailed(mailetName, "mailet", e);
    }
}

16 View Complete Implementation : MailetPipelineLogging.java
Copyright Apache License 2.0
Author : apache
public static void logBeginOfMailetProcess(Mailet mailet, Mail mail) {
    LOGGER.debug("Mail: {} Entering mailet: {}", mail.getState(), getMailetInfo(mailet));
}

16 View Complete Implementation : MailetPipelineLogging.java
Copyright Apache License 2.0
Author : apache
public static void logEndOfMailetProcess(Mailet mailet, Mail mail) {
    LOGGER.debug("Mail: {} End of mailet: {}", mail.getState(), getMailetInfo(mailet));
}

16 View Complete Implementation : MailetPipelineLogging.java
Copyright Apache License 2.0
Author : apache
@VisibleForTesting
static String getMailetInfo(Mailet mailet) {
    return Optional.ofNullable(Strings.emptyToNull(mailet.getMailetInfo())).orElse(mailet.getClreplaced().getSimpleName());
}

16 View Complete Implementation : GuiceMailetLoader.java
Copyright Apache License 2.0
Author : apache
private MailetConfig resolveConfiguration(Mailet result, MailetConfig providedConfiguration) {
    return Optional.ofNullable(configurationOverrides.get(result.getClreplaced())).orElse(providedConfiguration);
}

16 View Complete Implementation : GuiceMailetLoaderTest.java
Copyright Apache License 2.0
Author : apache
@Test
public void getMailetShouldLoadClreplacedWhenInSubPackageFromDefaultPackage() throws Exception {
    GuiceGenericLoader genericLoader = GuiceGenericLoader.forTesting(new ExtendedClreplacedLoader(THROWING_FILE_SYSTEM));
    GuiceMailetLoader guiceMailetLoader = new GuiceMailetLoader(genericLoader, NO_MAILET_CONFIG_OVERRIDES);
    Mailet mailet = guiceMailetLoader.getMailet(FakeMailetConfig.builder().mailetName("sub.TestMailet").mailetContext(FakeMailContext.defaultContext()).build());
    replacedertThat(mailet).isInstanceOf(TestMailet.clreplaced);
}

16 View Complete Implementation : GuiceMailetLoaderTest.java
Copyright Apache License 2.0
Author : apache
@Test
public void getMailetShouldLoadClreplaced() throws Exception {
    GuiceGenericLoader genericLoader = GuiceGenericLoader.forTesting(new ExtendedClreplacedLoader(THROWING_FILE_SYSTEM));
    GuiceMailetLoader guiceMailetLoader = new GuiceMailetLoader(genericLoader, NO_MAILET_CONFIG_OVERRIDES);
    Mailet mailet = guiceMailetLoader.getMailet(FakeMailetConfig.builder().mailetName("AddFooter").mailetContext(FakeMailContext.defaultContext()).build());
    replacedertThat(mailet).isInstanceOf(AddFooter.clreplaced);
}

16 View Complete Implementation : GuiceMailetLoaderTest.java
Copyright Apache License 2.0
Author : apache
@Test
public void allMailetsShouldShareTheSameSingleton() throws Exception {
    GuiceGenericLoader genericLoader = new GuiceGenericLoader(Guice.createInjector(), new ExtendedClreplacedLoader(RECURSIVE_CLreplacedPATH_FILE_SYSTEM), new ExtensionConfiguration(ImmutableList.of(new ClreplacedName("org.apache.james.transport.mailets.MyExtensionModule"))));
    GuiceMailetLoader guiceMailetLoader = new GuiceMailetLoader(genericLoader, NO_MAILET_CONFIG_OVERRIDES);
    Mailet mailet1 = guiceMailetLoader.getMailet(FakeMailetConfig.builder().mailetName("MyGenericMailet").mailetContext(FakeMailContext.defaultContext()).build());
    Mailet mailet2 = guiceMailetLoader.getMailet(FakeMailetConfig.builder().mailetName("MyGenericMailet").mailetContext(FakeMailContext.defaultContext()).build());
    replacedertThat(mailet1).isEqualTo(mailet2);
}

16 View Complete Implementation : MockMailetLoader.java
Copyright Apache License 2.0
Author : apache
@SuppressWarnings("unchecked")
@Override
public Mailet getMailet(MailetConfig config) throws MessagingException {
    try {
        Clreplaced<Mailet> clazz = (Clreplaced<Mailet>) Thread.currentThread().getContextClreplacedLoader().loadClreplaced(config.getMailetName());
        Mailet m = clazz.newInstance();
        m.init(config);
        return m;
    } catch (Exception e) {
        throw new MessagingException("Unable to load mailet " + config.getMailetName());
    }
}

15 View Complete Implementation : GuiceMailetLoaderTest.java
Copyright Apache License 2.0
Author : apache
@Test
public void getMailetShouldLoadClreplacedWhenInExtensionsJars() throws Exception {
    GuiceGenericLoader genericLoader = GuiceGenericLoader.forTesting(new ExtendedClreplacedLoader(CLreplacedPATH_FILE_SYSTEM));
    GuiceMailetLoader guiceMailetLoader = new GuiceMailetLoader(genericLoader, NO_MAILET_CONFIG_OVERRIDES);
    Mailet mailet = guiceMailetLoader.getMailet(FakeMailetConfig.builder().mailetName("CustomMailet").mailetContext(FakeMailContext.defaultContext()).build());
    replacedertThat(mailet.getClreplaced().getCanonicalName()).isEqualTo("org.apache.james.transport.mailets.CustomMailet");
}

15 View Complete Implementation : GuiceMailetLoaderTest.java
Copyright Apache License 2.0
Author : apache
@Test
public void getMailedShouldAllowCustomPackages() throws Exception {
    GuiceGenericLoader genericLoader = GuiceGenericLoader.forTesting(new ExtendedClreplacedLoader(CLreplacedPATH_FILE_SYSTEM));
    GuiceMailetLoader guiceMailetLoader = new GuiceMailetLoader(genericLoader, NO_MAILET_CONFIG_OVERRIDES);
    Mailet mailet = guiceMailetLoader.getMailet(FakeMailetConfig.builder().mailetName("com.custom.mailets.AnotherMailet").mailetContext(FakeMailContext.defaultContext()).build());
    replacedertThat(mailet.getClreplaced().getCanonicalName()).isEqualTo("com.custom.mailets.AnotherMailet");
}

15 View Complete Implementation : GuiceMailetLoaderTest.java
Copyright Apache License 2.0
Author : apache
@Test
public void getMailetShouldBrowseRecursivelyExtensionsJars() throws Exception {
    GuiceGenericLoader genericLoader = GuiceGenericLoader.forTesting(new ExtendedClreplacedLoader(RECURSIVE_CLreplacedPATH_FILE_SYSTEM));
    GuiceMailetLoader guiceMailetLoader = new GuiceMailetLoader(genericLoader, NO_MAILET_CONFIG_OVERRIDES);
    Mailet mailet = guiceMailetLoader.getMailet(FakeMailetConfig.builder().mailetName("CustomMailet").mailetContext(FakeMailContext.defaultContext()).build());
    replacedertThat(mailet.getClreplaced().getCanonicalName()).isEqualTo("org.apache.james.transport.mailets.CustomMailet");
}

15 View Complete Implementation : GuiceMailetLoaderTest.java
Copyright Apache License 2.0
Author : apache
@Test
public void getMailetShouldLoadMailetsWithCustomDependencyInConstructor() throws Exception {
    GuiceGenericLoader genericLoader = GuiceGenericLoader.forTesting(new ExtendedClreplacedLoader(RECURSIVE_CLreplacedPATH_FILE_SYSTEM));
    GuiceMailetLoader guiceMailetLoader = new GuiceMailetLoader(genericLoader, NO_MAILET_CONFIG_OVERRIDES);
    Mailet mailet = guiceMailetLoader.getMailet(FakeMailetConfig.builder().mailetName("CustomMailetWithCustomDependencyInConstructor").mailetContext(FakeMailContext.defaultContext()).build());
    replacedertThat(mailet.getClreplaced().getCanonicalName()).isEqualTo("org.apache.james.transport.mailets.CustomMailetWithCustomDependencyInConstructor");
}

15 View Complete Implementation : StripAttachmentTest.java
Copyright Apache License 2.0
Author : apache
@Test
void initShouldThrowWhenWrongPattern() {
    Mailet mailet = new StripAttachment();
    FakeMailetConfig mci = FakeMailetConfig.builder().mailetName("Test").setProperty("pattern", ".****\\.tmp").build();
    replacedertThatThrownBy(() -> mailet.init(mci)).isInstanceOf(MailetException.clreplaced).hasMessage("Could not compile regex [.****\\.tmp].");
}

15 View Complete Implementation : StripAttachmentTest.java
Copyright Apache License 2.0
Author : apache
@Test
void initShouldWorkWhenPatternIsDefinedAndValid() throws MessagingException {
    Mailet mailet = new StripAttachment();
    FakeMailetConfig mci = FakeMailetConfig.builder().mailetName("Test").setProperty("pattern", ".*\\.tmp").build();
    mailet.init(mci);
}

15 View Complete Implementation : StripAttachmentTest.java
Copyright Apache License 2.0
Author : apache
@Test
void initShouldThrowWhenWrongNotPattern() {
    Mailet mailet = new StripAttachment();
    FakeMailetConfig mci = FakeMailetConfig.builder().mailetName("Test").setProperty("notpattern", ".****\\.tmp").build();
    replacedertThatThrownBy(() -> mailet.init(mci)).isInstanceOf(MailetException.clreplaced).hasMessage("Could not compile regex [.****\\.tmp].");
}

15 View Complete Implementation : StripAttachmentTest.java
Copyright Apache License 2.0
Author : apache
@Test
void initShouldThrowWhenPatternAndNotPatternAndMimeTypeAreNull() {
    Mailet mailet = new StripAttachment();
    FakeMailetConfig mci = FakeMailetConfig.builder().mailetName("Test").build();
    replacedertThatThrownBy(() -> mailet.init(mci)).isInstanceOf(MailetException.clreplaced).hasMessage("At least one of 'pattern', 'notpattern' or 'mimeType' parameter should be provided.");
}

15 View Complete Implementation : StripAttachmentTest.java
Copyright Apache License 2.0
Author : apache
@Test
void initShouldThrowWhenMimeTypeIsEmpty() {
    Mailet mailet = new StripAttachment();
    FakeMailetConfig mci = FakeMailetConfig.builder().mailetName("Test").setProperty("mimeType", "").build();
    replacedertThatThrownBy(() -> mailet.init(mci)).isInstanceOf(MailetException.clreplaced).hasMessage("At least one of 'pattern', 'notpattern' or 'mimeType' parameter should be provided.");
}

15 View Complete Implementation : StripAttachmentTest.java
Copyright Apache License 2.0
Author : apache
@Test
void initShouldWorkWhenMimeTypeIsDefined() throws MessagingException {
    Mailet mailet = new StripAttachment();
    FakeMailetConfig mci = FakeMailetConfig.builder().mailetName("Test").setProperty("mimeType", "text/calendar").build();
    mailet.init(mci);
}

15 View Complete Implementation : StripAttachmentTest.java
Copyright Apache License 2.0
Author : apache
@Test
void initShouldWorkWhenNotPatternIsDefinedAndValid() throws MessagingException {
    Mailet mailet = new StripAttachment();
    FakeMailetConfig mci = FakeMailetConfig.builder().mailetName("Test").setProperty("notpattern", ".*\\.tmp").build();
    mailet.init(mci);
}

14 View Complete Implementation : StripAttachmentTest.java
Copyright Apache License 2.0
Author : apache
@Test
void serviceShouldThrowWhenUnretrievableMessage(TemporaryFolder temporaryFolder) throws MessagingException {
    Mailet mailet = initMailet(temporaryFolder);
    Mail mail = mock(Mail.clreplaced);
    when(mail.getMessage()).thenThrow(new MessagingException("Test exception"));
    replacedertThatThrownBy(() -> mailet.service(mail)).isInstanceOf(MailetException.clreplaced).hasMessage("Could not retrieve message from Mail object");
}

14 View Complete Implementation : StripAttachmentTest.java
Copyright Apache License 2.0
Author : apache
private Mailet initMailet(TemporaryFolder temporaryFolder) throws MessagingException {
    Mailet mailet = new StripAttachment();
    FakeMailetConfig mci = FakeMailetConfig.builder().mailetName("Test").setProperty("directory", temporaryFolder.getFolderPath()).setProperty("remove", "all").setProperty("pattern", ".*\\.tmp").setProperty("decodeFilename", "true").setProperty("replaceFilenamePattern", "/[ÀÁÂÃÄÅ]/A//," + "/[Æ]/AE//," + "/[ÈÉÊË]/E//," + "/[ÌÍÎÏ]/I//," + "/[ÒÓÔÕÖ]/O//," + "/[×]/x//," + "/[ÙÚÛÜ]/U//," + "/[àáâãäå]/a//," + "/[æ]/ae//," + "/[èéêë]/e/r/," + "/[ìíîï]/i//," + "/[òóôõö]/o//," + "/[ùúûü]/u//," + "/[^A-Za-z0-9._-]+/_/r/").build();
    mailet.init(mci);
    return mailet;
}

14 View Complete Implementation : GuiceMailetLoaderTest.java
Copyright Apache License 2.0
Author : apache
@Test
public void getMailetShouldLoadMailetsWithCustomDependencyInService() throws Exception {
    GuiceGenericLoader genericLoader = GuiceGenericLoader.forTesting(new ExtendedClreplacedLoader(RECURSIVE_CLreplacedPATH_FILE_SYSTEM));
    GuiceMailetLoader guiceMailetLoader = new GuiceMailetLoader(genericLoader, NO_MAILET_CONFIG_OVERRIDES);
    Mailet mailet = guiceMailetLoader.getMailet(FakeMailetConfig.builder().mailetName("CustomMailetWithCustomDependencyInService").mailetContext(FakeMailContext.defaultContext()).build());
    replacedertThatCode(() -> mailet.service(FakeMail.defaultFakeMail())).doesNotThrowAnyException();
}

14 View Complete Implementation : GuiceMailetLoaderTest.java
Copyright Apache License 2.0
Author : apache
@Test
public void getMailetShouldAllowCustomInjections() throws Exception {
    GuiceGenericLoader genericLoader = new GuiceGenericLoader(Guice.createInjector(), new ExtendedClreplacedLoader(RECURSIVE_CLreplacedPATH_FILE_SYSTEM), new ExtensionConfiguration(ImmutableList.of(new ClreplacedName("org.apache.james.transport.mailets.MyExtensionModule"))));
    GuiceMailetLoader guiceMailetLoader = new GuiceMailetLoader(genericLoader, NO_MAILET_CONFIG_OVERRIDES);
    Mailet mailet = guiceMailetLoader.getMailet(FakeMailetConfig.builder().mailetName("MyGenericMailet").mailetContext(FakeMailContext.defaultContext()).build());
    replacedertThatCode(() -> mailet.service(FakeMail.defaultFakeMail())).doesNotThrowAnyException();
}

13 View Complete Implementation : HeadersToHTTPTest.java
Copyright Apache License 2.0
Author : apache
@Test
void shouldBeFailedWhenServiceNotExists() throws Exception {
    urlTestPattern = "/path/to/service/failed";
    FakeMailetConfig mailetConfig = FakeMailetConfig.builder().setProperty("parameterKey", "pKey").setProperty("parameterValue", "pValue").setProperty("url", "http://qwerty.localhost:12345" + urlTestPattern).build();
    Mailet mailet = new HeadersToHTTP();
    mailet.init(mailetConfig);
    mailet.service(mail);
    replacedertThat(mail.getMessage().getHeader("X-headerToHTTP")).hreplacedize(1).allSatisfy((header) -> replacedertThat(header).isEqualTo("Failed"));
    replacedertThat(mail.getMessage().getHeader("X-headerToHTTPFailure")).hreplacedize(1).allSatisfy((header) -> replacedertThat(header).isNotBlank());
}

13 View Complete Implementation : SerialiseToHTTPTest.java
Copyright Apache License 2.0
Author : apache
@Test
void shouldBeFailedWhenServiceNotExists() throws Exception {
    urlTestPattern = "/path/to/service/failed";
    FakeMailetConfig mailetConfig = FakeMailetConfig.builder().setProperty("parameterKey", "pKey").setProperty("parameterValue", "pValue").setProperty("messageKey", "mKey").setProperty("url", "ftp://" + server.getInetAddress().getHostAddress() + ":" + server.getLocalPort() + urlTestPattern).build();
    Mailet mailet = new SerialiseToHTTP();
    mailet.init(mailetConfig);
    mailet.service(mail);
    replacedertThat(mail.getMessage().getHeader("X-toHTTP")).hreplacedize(1).allSatisfy((header) -> replacedertThat(header).isEqualTo("Failed"));
    replacedertThat(mail.getMessage().getHeader("X-toHTTPFailure")).hreplacedize(1).allSatisfy((header) -> replacedertThat(header).isNotBlank());
}

12 View Complete Implementation : StripAttachmentTest.java
Copyright Apache License 2.0
Author : apache
@Test
void serviceShouldThrowWhenUnretrievableContentTypeMessage(TemporaryFolder temporaryFolder) throws MessagingException {
    Mailet mailet = initMailet(temporaryFolder);
    MimeMessage message = mock(MimeMessage.clreplaced);
    Mail mail = mock(Mail.clreplaced);
    when(mail.getMessage()).thenReturn(message);
    when(message.isMimeType("multipart/*")).thenThrow(new MessagingException("Test exception"));
    replacedertThatThrownBy(() -> mailet.service(mail)).isInstanceOf(MailetException.clreplaced).hasMessage("Could not retrieve contenttype of MimePart.");
}

12 View Complete Implementation : DKIMVerifyTest.java
Copyright Apache License 2.0
Author : apache
private Mail process(String message) throws Exception {
    Mailet mailet = new DKIMVerify((new MockPublicKeyRecordRetriever("v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDYDaYKXzwVYwqWbLhmuJ66aTAN8wmDR+rfHE8HfnkSOax0oIoTM5zquZrTLo30870YMfYzxwfB6j/Nz3QdwrUD/t0YMYJiUKyWJnCKfZXHJBJ+yfRHr7oW+UW3cVo9CG2bBfIxsInwYe175g9UjyntJpWueqdEIo1c2bhv9Mp66QIDAQAB;", "selector", "example.com")));
    FakeMailetConfig mci = FakeMailetConfig.builder().mailetName("Test").mailetContext(FakeMailContext.defaultContext()).build();
    mailet.init(mci);
    Mail mail = FakeMail.builder().name("test").mimeMessage(new MimeMessage(Session.getDefaultInstance(new Properties()), new ByteArrayInputStream(message.getBytes()))).build();
    mailet.service(mail);
    return mail;
}

11 View Complete Implementation : HeadersToHTTPTest.java
Copyright Apache License 2.0
Author : apache
@Test
void shouldBeSucceededWhenServiceResponseIsOk() throws Exception {
    urlTestPattern = "/path/to/service/succeeded";
    FakeMailetConfig mailetConfig = FakeMailetConfig.builder().setProperty("parameterKey", "pKey").setProperty("parameterValue", "pValue").setProperty("url", "http://" + server.getInetAddress().getHostAddress() + ":" + server.getLocalPort() + urlTestPattern).build();
    mapper.register(urlTestPattern, (request, response, context) -> response.setStatusCode(HttpStatus.SC_OK));
    Mailet mailet = new HeadersToHTTP();
    mailet.init(mailetConfig);
    mailet.service(mail);
    replacedertThat(mail.getMessage().getHeader("X-headerToHTTP")).hreplacedize(1).allSatisfy((header) -> replacedertThat(header).isEqualTo("Succeeded"));
}

11 View Complete Implementation : SerialiseToHTTPTest.java
Copyright Apache License 2.0
Author : apache
@Test
void shouldBeFailedWhenServiceResponseIsNotOk() throws Exception {
    urlTestPattern = "/path/to/service/internal/error";
    FakeMailetConfig mailetConfig = FakeMailetConfig.builder().setProperty("parameterKey", "pKey").setProperty("parameterValue", "pValue").setProperty("messageKey", "mKey").setProperty("url", "http://" + server.getInetAddress().getHostAddress() + ":" + server.getLocalPort() + urlTestPattern).build();
    mapper.register(urlTestPattern, (request, response, context) -> {
        response.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
    });
    Mailet mailet = new SerialiseToHTTP();
    mailet.init(mailetConfig);
    mailet.service(mail);
    replacedertThat(mail.getMessage().getHeader("X-toHTTP")).hreplacedize(1).allSatisfy((header) -> replacedertThat(header).isEqualTo("Failed"));
    replacedertThat(mail.getMessage().getHeader("X-toHTTPFailure")).hreplacedize(1).allSatisfy((header) -> replacedertThat(header).isEqualTo("HTTP/1.1 500 Internal Server Error"));
}

11 View Complete Implementation : SerialiseToHTTPTest.java
Copyright Apache License 2.0
Author : apache
@Test
void shouldBeSucceededWhenServiceResponseIsOk() throws Exception {
    urlTestPattern = "/path/to/service/succeeded";
    FakeMailetConfig mailetConfig = FakeMailetConfig.builder().setProperty("parameterKey", "pKey").setProperty("parameterValue", "pValue").setProperty("messageKey", "mKey").setProperty("url", "http://" + server.getInetAddress().getHostAddress() + ":" + server.getLocalPort() + urlTestPattern).build();
    mapper.register(urlTestPattern, (request, response, context) -> {
        response.setStatusCode(HttpStatus.SC_OK);
    });
    Mailet mailet = new SerialiseToHTTP();
    mailet.init(mailetConfig);
    mailet.service(mail);
    replacedertThat(mail.getMessage().getHeader("X-toHTTP")).hreplacedize(1).allSatisfy((header) -> replacedertThat(header).isEqualTo("Succeeded"));
}

11 View Complete Implementation : StripAttachmentTest.java
Copyright Apache License 2.0
Author : apache
@Test
void serviceShouldNotModifyMailWhenNotMultipart(TemporaryFolder temporaryFolder) throws MessagingException, IOException {
    Mailet mailet = initMailet(temporaryFolder);
    MimeMessageBuilder message = MimeMessageBuilder.mimeMessageBuilder().setSubject("test").setText("simple text");
    MimeMessageBuilder expectedMessage = MimeMessageBuilder.mimeMessageBuilder().setSubject("test").setText("simple text");
    Mail mail = FakeMail.fromMessage(message);
    Mail expectedMail = FakeMail.fromMessage(expectedMessage);
    mailet.service(mail);
    replacedertThat(mail).usingRecursiveComparison().ignoringFields("msg").isEqualTo(expectedMail);
    replacedertThat(mail.getMessage().getContent()).isEqualTo("simple text");
}

11 View Complete Implementation : DKIMSignTest.java
Copyright Apache License 2.0
Author : apache
@ParameterizedTest
@ValueSource(strings = { PKCS1_PEM_FILE, PKCS8_PEM_FILE })
void testDKIMSign(String pemFile) throws MessagingException, IOException, FailException {
    String message = "Received: by 10.XX.XX.12 with SMTP id dfgskldjfhgkljsdfhgkljdhfg;\r\n\tTue, 06 Oct 2009 07:37:34 -0700 (PDT)\r\nReturn-Path: <[email protected]>\r\nReceived: from example.co.uk (example.co.uk [XX.XXX.125.19])\r\n\tby mx.example.com with ESMTP id dgdfgsdfgsd.97.2009.10.06.07.37.32;\r\n\tTue, 06 Oct 2009 07:37:32 -0700 (PDT)\r\nFrom: [email protected]\r\nTo: [email protected]\r\n\r\nbody\r\nprova\r\n";
    Mailet mailet = new DKIMSign();
    FakeMailetConfig mci = FakeMailetConfig.builder().mailetName("Test").mailetContext(FAKE_MAIL_CONTEXT).setProperty("signatureTemplate", "v=1; s=selector; d=example.com; h=from:to:received:received; a=rsa-sha256; bh=; b=;").setProperty("privateKeyFilepath", pemFile).build();
    mailet.init(mci);
    Mail mail = FakeMail.builder().name("test").mimeMessage(new MimeMessage(Session.getDefaultInstance(new Properties()), new ByteArrayInputStream(message.getBytes()))).build();
    mailet.service(mail);
    Mailet m7bit = new ConvertTo7Bit();
    m7bit.init(mci);
    m7bit.service(mail);
    ByteArrayOutputStream rawMessage = new ByteArrayOutputStream();
    mail.getMessage().writeTo(rawMessage);
    MockPublicKeyRecordRetriever mockPublicKeyRecordRetriever = new MockPublicKeyRecordRetriever("v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDYDaYKXzwVYwqWbLhmuJ66aTAN8wmDR+rfHE8HfnkSOax0oIoTM5zquZrTLo30870YMfYzxwfB6j/Nz3QdwrUD/t0YMYJiUKyWJnCKfZXHJBJ+yfRHr7oW+UW3cVo9CG2bBfIxsInwYe175g9UjyntJpWueqdEIo1c2bhv9Mp66QIDAQAB;", "selector", "example.com");
    verify(rawMessage, mockPublicKeyRecordRetriever);
}

11 View Complete Implementation : DKIMSignTest.java
Copyright Apache License 2.0
Author : apache
@ParameterizedTest
@ValueSource(strings = { PKCS1_PEM_FILE, PKCS8_PEM_FILE })
void testDKIMSignFuture(String pemFile) throws MessagingException, IOException, FailException {
    String message = "Received: by 10.XX.XX.12 with SMTP id dfgskldjfhgkljsdfhgkljdhfg;\r\n\tTue, 06 Oct 2009 07:37:34 -0700 (PDT)\r\nReturn-Path: <[email protected]>\r\nReceived: from example.co.uk (example.co.uk [XX.XXX.125.19])\r\n\tby mx.example.com with ESMTP id dgdfgsdfgsd.97.2009.10.06.07.37.32;\r\n\tTue, 06 Oct 2009 07:37:32 -0700 (PDT)\r\nFrom: [email protected]\r\nTo: [email protected]\r\n\r\nbody\r\nprova\r\n";
    Mailet mailet = new DKIMSign();
    FakeMailetConfig mci = FakeMailetConfig.builder().mailetName("Test").mailetContext(FAKE_MAIL_CONTEXT).setProperty("signatureTemplate", "v=1; t=" + ((System.currentTimeMillis() / 1000) + 1000) + "; s=selector; d=example.com; h=from:to:received:received; a=rsa-sha256; bh=; b=;").setProperty("privateKeyFilepath", pemFile).build();
    mailet.init(mci);
    Mail mail = FakeMail.builder().name("test").mimeMessage(new MimeMessage(Session.getDefaultInstance(new Properties()), new ByteArrayInputStream(message.getBytes()))).build();
    mailet.service(mail);
    Mailet m7bit = new ConvertTo7Bit();
    m7bit.init(mci);
    m7bit.service(mail);
    ByteArrayOutputStream rawMessage = new ByteArrayOutputStream();
    mail.getMessage().writeTo(rawMessage);
    MockPublicKeyRecordRetriever mockPublicKeyRecordRetriever = new MockPublicKeyRecordRetriever("v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDYDaYKXzwVYwqWbLhmuJ66aTAN8wmDR+rfHE8HfnkSOax0oIoTM5zquZrTLo30870YMfYzxwfB6j/Nz3QdwrUD/t0YMYJiUKyWJnCKfZXHJBJ+yfRHr7oW+UW3cVo9CG2bBfIxsInwYe175g9UjyntJpWueqdEIo1c2bhv9Mp66QIDAQAB;", "selector", "example.com");
    try {
        verify(rawMessage, mockPublicKeyRecordRetriever);
        fail("Expecting signature to be ignored");
    } catch (PermFailException e) {
    // signature ignored, so fail for missing signatures.
    }
}