AttachmentReleaseFaultOutInterceptor extends AbstractPhaseInterceptor<Message> { @Override public void handleMessage(Message message) { Collection<Attachment> attachments = message.getExchange().getInMessage().getAttachments(); if (attachments != null) { for (Attachment attachment : attachments) { DataSource ds = getDataSource(attachment); if (ds instanceof AttachmentDataSource) { AttachmentDataSource ads = (AttachmentDataSource) ds; ads.release(); getLargeFileUtils().closeStreamWithoutException(ads.getInputStream()); } } } } AttachmentReleaseFaultOutInterceptor(); @Override void handleMessage(Message message);  }
@Test public void testHandleMessage() { final LargeFileUtils largeFileUtils = mock(LargeFileUtils.class); Message message = mock(Message.class); Exchange exchange = mock(Exchange.class); Collection<Attachment> attachments = new ArrayList<>(); Attachment attachment = mock(Attachment.class); attachments.add(attachment); DataHandler dataHandler = mock(DataHandler.class); AttachmentDataSource dataSource = mock(AttachmentDataSource.class); AttachmentReleaseFaultOutInterceptor interceptor = new AttachmentReleaseFaultOutInterceptor() { @Override protected LargeFileUtils getLargeFileUtils() { return largeFileUtils; } }; when(message.getExchange()).thenReturn(exchange); when(exchange.getInMessage()).thenReturn(message); when(message.getAttachments()).thenReturn(attachments); when(attachment.getDataHandler()).thenReturn(dataHandler); when(dataHandler.getDataSource()).thenReturn(dataSource); interceptor.handleMessage(message); verify(dataSource).release(); verify(largeFileUtils).closeStreamWithoutException(any(InputStream.class)); }