/** * {@inheritDoc} */ public boolean isConnected() { return getServiceStatus() == ExternalServiceStatus.CONNECTED; }
/** * Parses the SMTP properties and checks whether a connection can be established. * * @param sendTestMailIfEnabled * send a test email if this feature is enabled */ private void onSmtpPropertiesChanged(boolean sendTestMailIfEnabled) { parseAdditionalPropertiesString(); checkConnection(sendTestMailIfEnabled && testMailEnabled); }
/** * Prepares an email object including the default recipients. * * @param recipients * recipient to send to. * @return Returns a prepared {@link HtmlEmail} object. * @throws EmailException * is thrown when the from address could not be set */ private HtmlEmail prepareHtmlEmail(List<String> recipients) throws EmailException { return prepareHtmlEmail(recipients, true); }
@Test public void testMailFailedNoRecipient() throws Exception { when(objectFactoryMock.getSmtpTransport()).thenReturn(transportMock); mailSender.smtpHost = "host"; mailSender.smtpPort = 25; mailSender.smtpUser = "user"; mailSender.smtpPassword = "passwd"; mailSender.testMailEnabled = true; mailSender.smtpEnabled = true; mailSender.onSmtpPropertiesChanged(); verify(objectFactoryMock).getSmtpTransport(); verify(transportMock).connect("host", 25, "user", "passwd"); verify(transportMock).close(); verifyNoMoreInteractions(mailMock, objectFactoryMock, transportMock); assertThat(mailSender.isConnected(), is(true)); assertThat(getAdditionalProperties().entrySet(), hasSize(0)); assertThat(getDefaultRecipients(), hasSize(0)); }
@Test public void recipientsAndPopertiesAreNull() throws Exception { when(objectFactoryMock.getSmtpTransport()).thenReturn(transportMock); mailSender.defaultRecipientString = null; mailSender.smtpPropertiesString = null; mailSender.smtpHost = "host"; mailSender.smtpPort = 25; mailSender.smtpUser = "user"; mailSender.smtpPassword = "passwd"; mailSender.smtpEnabled = true; mailSender.init(); verify(objectFactoryMock).getSmtpTransport(); verify(transportMock).connect("host", 25, "user", "passwd"); verify(transportMock).close(); verifyNoMoreInteractions(objectFactoryMock, transportMock); assertThat(mailSender.isConnected(), is(true)); assertThat(getAdditionalProperties().entrySet(), hasSize(0)); assertThat(getDefaultRecipients(), hasSize(0)); }
@Test public void connected() throws MessagingException { when(objectFactoryMock.getSmtpTransport()).thenReturn(transportMock); mailSender.smtpEnabled = true; mailSender.init(); boolean result = mailSender.isConnected(); assertThat(result, is(true)); verify(objectFactoryMock).getSmtpTransport(); verify(transportMock).connect(any(String.class), any(Integer.class), any(String.class), any(String.class)); verify(transportMock).close(); verifyNoMoreInteractions(objectFactoryMock, transportMock); }
@Test public void connected() throws MessagingException { mailSender.smtpEnabled = true; when(objectFactoryMock.getSmtpTransport()).thenReturn(transportMock); mailSender.init(); ExternalServiceStatus result = mailSender.getServiceStatus(); assertThat(result, is(ExternalServiceStatus.CONNECTED)); verify(objectFactoryMock).getSmtpTransport(); verify(transportMock).connect(any(String.class), any(Integer.class), any(String.class), any(String.class)); verify(transportMock).close(); verifyNoMoreInteractions(objectFactoryMock, transportMock); }
@Test(expectedExceptions = IllegalArgumentException.class) public void htmlBodyIsEmpty() throws Exception { try { mailSender.smtpEnabled = true; mailSender.sendEMail("subject", "", "textBody", Arrays.asList("three@example.com")); } finally { verifyZeroInteractions(mailMock, transportMock, objectFactoryMock); } }
@Test public void disabled() throws MessagingException { mailSender.smtpEnabled = false; mailSender.init(); ExternalServiceStatus result = mailSender.getServiceStatus(); assertThat(result, is(ExternalServiceStatus.DISABLED)); verifyZeroInteractions(objectFactoryMock, transportMock); } }
/** * Initialize E-Mail service. * * @param sendTestMailIfEnabled * send a test email if this feature is enabled */ private void init(boolean sendTestMailIfEnabled) { if (!smtpEnabled) { return; } else if (log.isInfoEnabled()) { log.info("|-eMail Service initialized"); } parseRecipientsString(); onSmtpPropertiesChanged(sendTestMailIfEnabled); }
/** * Method which is executed after bean is created. */ @PostConstruct public void postConstruct() { init(false); }
/** * Parses the SMTP properties and checks whether a connection can be established. */ @PropertyUpdate(properties = { "mail.smtp.host", "mail.smtp.port", "mail.smtp.user", "mail.smtp.passwd", "mail.smtp.properties", "mail.test.enabled" }) public void onSmtpPropertiesChanged() { if (!smtpEnabled) { return; } onSmtpPropertiesChanged(true); }
@Test public void notConnected() throws MessagingException { when(objectFactoryMock.getSmtpTransport()).thenReturn(transportMock); doThrow(MessagingException.class).when(transportMock).connect(any(String.class), any(Integer.class), any(String.class), any(String.class)); mailSender.smtpEnabled = true; mailSender.init(); boolean result = mailSender.isConnected(); assertThat(result, is(false)); verify(objectFactoryMock).getSmtpTransport(); verify(transportMock).connect(any(String.class), any(Integer.class), any(String.class), any(String.class)); verifyNoMoreInteractions(objectFactoryMock, transportMock); } }
@Test public void notConnected() throws MessagingException { mailSender.smtpEnabled = true; when(objectFactoryMock.getSmtpTransport()).thenReturn(transportMock); doThrow(MessagingException.class).when(transportMock).connect(any(String.class), any(Integer.class), any(String.class), any(String.class)); mailSender.init(); ExternalServiceStatus result = mailSender.getServiceStatus(); assertThat(result, is(ExternalServiceStatus.DISCONNECTED)); verify(objectFactoryMock).getSmtpTransport(); verify(transportMock).connect(any(String.class), any(Integer.class), any(String.class), any(String.class)); verifyNoMoreInteractions(objectFactoryMock, transportMock); }
@Test(expectedExceptions = IllegalArgumentException.class) public void htmlBodyIsNull() throws Exception { try { mailSender.smtpEnabled = true; mailSender.sendEMail("subject", null, "textBody", Arrays.asList("three@example.com")); } finally { verifyZeroInteractions(mailMock, transportMock, objectFactoryMock); } }
/** * Initialize E-Mail service. */ @PropertyUpdate(properties = { "mail.enable" }) public void init() { init(true); }
@Test(expectedExceptions = IllegalArgumentException.class) public void textBodyIsEmpty() throws Exception { try { mailSender.smtpEnabled = true; mailSender.sendEMail("subject", "htmlBody", "", Arrays.asList("three@example.com")); } finally { verifyZeroInteractions(mailMock, transportMock, objectFactoryMock); } } }
@Test(expectedExceptions = IllegalArgumentException.class) public void subjectIsEmpty() throws Exception { try { mailSender.smtpEnabled = true; mailSender.sendEMail("", "htmlBody", "textBody", Arrays.asList("three@example.com")); } finally { verifyZeroInteractions(mailMock, transportMock, objectFactoryMock); } }
@Test(expectedExceptions = IllegalArgumentException.class) public void subjectIsNull() throws Exception { try { mailSender.smtpEnabled = true; mailSender.sendEMail(null, "htmlBody", "textBody", Arrays.asList("three@example.com")); } finally { verifyZeroInteractions(mailMock, transportMock, objectFactoryMock); } }
@Test(expectedExceptions = IllegalArgumentException.class) public void textBodyIsNull() throws Exception { try { mailSender.smtpEnabled = true; mailSender.sendEMail("subject", "htmlBody", null, Arrays.asList("three@example.com")); } finally { verifyZeroInteractions(mailMock, transportMock, objectFactoryMock); } }