if (getServiceStatus() == ExternalServiceStatus.DISABLED) { return; } else if (!isConnected()) { if (log.isInfoEnabled()) { log.info("Cannot send a test e-mail because the SMTP connection is not established.");
@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 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 connectionToSmtpFailed() throws Exception { when(objectFactoryMock.getSmtpTransport()).thenReturn(transportMock); doThrow(MessagingException.class).when(transportMock).connect("host", 25, "user", "passwd"); 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"); verifyNoMoreInteractions(objectFactoryMock, transportMock); assertThat(mailSender.isConnected(), is(false)); } }
@Test public void authenticationFailes() throws Exception { when(objectFactoryMock.getSmtpTransport()).thenReturn(transportMock); doThrow(AuthenticationFailedException.class).when(transportMock).connect("host", 25, "user", "passwd"); 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"); verifyNoMoreInteractions(objectFactoryMock, transportMock); assertThat(mailSender.isConnected(), is(false)); }
@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 testMailFailedNoConnection() throws Exception { when(objectFactoryMock.getSmtpTransport()).thenReturn(transportMock); doThrow(MessagingException.class).when(transportMock).connect("host", 25, "user", "passwd"); mailSender.smtpHost = "host"; mailSender.smtpPort = 25; mailSender.smtpUser = "user"; mailSender.smtpPassword = "passwd"; mailSender.testMailEnabled = true; mailSender.testMailRecipient = "@test@example.com"; mailSender.smtpEnabled = true; mailSender.onSmtpPropertiesChanged(); verify(objectFactoryMock).getSmtpTransport(); verify(transportMock).connect("host", 25, "user", "passwd"); verifyNoMoreInteractions(mailMock, objectFactoryMock, transportMock); assertThat(mailSender.isConnected(), is(false)); 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 propertiesChanged() throws Exception { when(objectFactoryMock.getSmtpTransport()).thenReturn(transportMock); mailSender.defaultRecipientString = "one@example.com,two@example.com,invalid"; mailSender.smtpPropertiesString = "key1=val1,key2=val2,=noKey,noVal=,=,invalid"; mailSender.smtpHost = "host"; mailSender.smtpPort = 25; mailSender.smtpUser = "user"; mailSender.smtpPassword = "passwd"; 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(2)); assertThat(getAdditionalProperties(), hasEntry((Object) "key1", (Object) "val1")); assertThat(getAdditionalProperties(), hasEntry((Object) "key2", (Object) "val2")); assertThat(getDefaultRecipients(), hasSize(0)); }
@Test public void successfully() throws Exception { when(objectFactoryMock.getSmtpTransport()).thenReturn(transportMock); mailSender.defaultRecipientString = "one@example.com,two@example.com,invalid"; mailSender.smtpPropertiesString = "key1=val1,key2=val2,=noKey,noVal=,=,invalid"; mailSender.smtpHost = "host"; mailSender.smtpPort = 25; mailSender.smtpUser = "user"; mailSender.smtpPassword = "passwd"; mailSender.smtpEnabled = true; mailSender.init(); verify(objectFactoryMock).getSmtpTransport(); verifyNoMoreInteractions(objectFactoryMock); verify(transportMock).connect("host", 25, "user", "passwd"); verify(transportMock).close(); verifyNoMoreInteractions(transportMock); assertThat(mailSender.isConnected(), is(true)); assertThat(getAdditionalProperties().entrySet(), hasSize(2)); assertThat(getAdditionalProperties(), hasEntry((Object) "key1", (Object) "val1")); assertThat(getAdditionalProperties(), hasEntry((Object) "key2", (Object) "val2")); assertThat(getDefaultRecipients(), hasSize(2)); assertThat(getDefaultRecipients(), hasItems("one@example.com", "two@example.com")); }
@Test public void testMailFailedInvalidRecipient() throws Exception { when(objectFactoryMock.getSmtpTransport()).thenReturn(transportMock); mailSender.defaultRecipientString = "one@example.com,two@example.com,invalid"; mailSender.smtpPropertiesString = "key1=val1,key2=val2,=noKey,noVal=,=,invalid"; mailSender.smtpHost = "host"; mailSender.smtpPort = 25; mailSender.smtpUser = "user"; mailSender.smtpPassword = "passwd"; mailSender.testMailEnabled = true; mailSender.testMailRecipient = "@test@example.com"; 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(2)); assertThat(getAdditionalProperties(), hasEntry((Object) "key1", (Object) "val1")); assertThat(getAdditionalProperties(), hasEntry((Object) "key2", (Object) "val2")); assertThat(getDefaultRecipients(), hasSize(0)); }
verify(mailMock).setTextMsg(any(String.class)); verifyNoMoreInteractions(mailMock, objectFactoryMock, transportMock); assertThat(mailSender.isConnected(), is(true)); assertThat(getAdditionalProperties().entrySet(), hasSize(0)); assertThat(getDefaultRecipients(), hasSize(0));
verify(mailMock).send(); verifyNoMoreInteractions(mailMock, objectFactoryMock, transportMock); assertThat(mailSender.isConnected(), is(true)); assertThat(getAdditionalProperties().entrySet(), hasSize(0)); assertThat(getDefaultRecipients(), hasSize(0));