@Test public void saveMessageThrowsMailStoreExceptionWhenError() throws Exception { String batchId = UUID.randomUUID().toString(); String messageId = "ar1vm0Wca42E/dDn3dsH8ogs3/s="; ExtendedMimeMessage message = mock(ExtendedMimeMessage.class); when(message.getUniqueMessageId()).thenReturn(messageId); when(message.getContent()).thenReturn("Lorem ipsum dolor sit amet, consectetur adipiscing elit"); doThrow(new IOException()).when(message).writeTo(any(OutputStream.class)); Throwable exception = assertThrows(MailStoreException.class, () -> { this.store.save(batchId, message); }); assertTrue(exception.getMessage().startsWith( "Failed to save message (id [" + messageId + "], batch id [" + batchId + "]) into file")); }
@Override public void save(String batchId, ExtendedMimeMessage message) throws MailStoreException { List<File> temporaryFiles = extractTemporaryFilesFromHeaders(message); String uniqueMessageId = message.getUniqueMessageId(); File messageFile = getMessageFile(batchId, uniqueMessageId); messageFile.getParentFile().mkdirs(); try { if (message.ensureSaved()) { uniqueMessageId = message.getUniqueMessageId(); messageFile = getMessageFile(batchId, uniqueMessageId); } message.writeTo(new FileOutputStream(messageFile)); for (File temporaryFile : temporaryFiles) { FileUtils.forceDelete(temporaryFile); } } catch (Exception e) { throw new MailStoreException(String.format( "Failed to save message (id [%s], batch id [%s]) into file [%s]", uniqueMessageId, batchId, messageFile), e); } }
FileSystemMailContentStore implements MailContentStore, Initializable { @Override public void save(String batchId, ExtendedMimeMessage message) throws MailStoreException { List<File> temporaryFiles = extractTemporaryFilesFromHeaders(message); String uniqueMessageId = message.getUniqueMessageId(); File messageFile = getMessageFile(batchId, uniqueMessageId); messageFile.getParentFile().mkdirs(); try { if (message.ensureSaved()) { uniqueMessageId = message.getUniqueMessageId(); messageFile = getMessageFile(batchId, uniqueMessageId); } message.writeTo(new FileOutputStream(messageFile)); for (File temporaryFile : temporaryFiles) { FileUtils.forceDelete(temporaryFile); } } catch (Exception e) { throw new MailStoreException(String.format( "Failed to save message (id [%s], batch id [%s]) into file [%s]", uniqueMessageId, batchId, messageFile), e); } } }
FileSystemMailContentStore implements MailContentStore, Initializable { @Override public void save(String batchId, ExtendedMimeMessage message) throws MailStoreException { List<File> temporaryFiles = extractTemporaryFilesFromHeaders(message); String uniqueMessageId = message.getUniqueMessageId(); File messageFile = getMessageFile(batchId, uniqueMessageId); messageFile.getParentFile().mkdirs(); try { if (message.ensureSaved()) { uniqueMessageId = message.getUniqueMessageId(); messageFile = getMessageFile(batchId, uniqueMessageId); } message.writeTo(new FileOutputStream(messageFile)); for (File temporaryFile : temporaryFiles) { FileUtils.forceDelete(temporaryFile); } } catch (Exception e) { throw new MailStoreException(String.format( "Failed to save message (id [%s], batch id [%s]) into file [%s]", uniqueMessageId, batchId, messageFile), e); } }  }
FileSystemMailContentStore implements MailContentStore, Initializable { @Override public void save(String batchId, ExtendedMimeMessage message) throws MailStoreException { List<File> temporaryFiles = extractTemporaryFilesFromHeaders(message); String uniqueMessageId = message.getUniqueMessageId(); File messageFile = getMessageFile(batchId, uniqueMessageId); messageFile.getParentFile().mkdirs(); try { if (message.ensureSaved()) { uniqueMessageId = message.getUniqueMessageId(); messageFile = getMessageFile(batchId, uniqueMessageId); } message.writeTo(new FileOutputStream(messageFile)); for (File temporaryFile : temporaryFiles) { FileUtils.forceDelete(temporaryFile); } } catch (Exception e) { throw new MailStoreException(String.format( "Failed to save message (id [%s], batch id [%s]) into file [%s]", uniqueMessageId, batchId, messageFile), e); } }  @Override void initialize(); @Override void save(String batchId, ExtendedMimeMessage message); @Override ExtendedMimeMessage load(Session session, String batchId, String uniqueMessageId); @Override void delete(String batchId, String uniqueMessageId); }
FileSystemMailContentStore implements MailContentStore, Initializable { @Override public void save(String batchId, ExtendedMimeMessage message) throws MailStoreException { List<File> temporaryFiles = extractTemporaryFilesFromHeaders(message); String uniqueMessageId = message.getUniqueMessageId(); File messageFile = getMessageFile(batchId, uniqueMessageId); messageFile.getParentFile().mkdirs(); try { if (message.ensureSaved()) { uniqueMessageId = message.getUniqueMessageId(); messageFile = getMessageFile(batchId, uniqueMessageId); } message.writeTo(new FileOutputStream(messageFile)); for (File temporaryFile : temporaryFiles) { FileUtils.forceDelete(temporaryFile); } } catch (Exception e) { throw new MailStoreException(String.format( "Failed to save message (id [%s], batch id [%s]) into file [%s]", uniqueMessageId, batchId, messageFile), e); } }  @Override void initialize(); @Override void save(String batchId, ExtendedMimeMessage message); @Override ExtendedMimeMessage load(Session session, String batchId, String uniqueMessageId); @Override void delete(String batchId, String uniqueMessageId); static final String ROOT_DIRECTORY; }