Lambdas for Remaining Modules JPA -> ZK

Polishing - PR Comments and Closeable Warnings

Eclipse emits bogus warnings with exceptions in lambdas.
Even though the lambda might run on another thread, elipse thinks it could
cause the context to not be closed.

SPR-14854: MessageChannel is now a @FunctionalInterface

* Additional Lambda polishing and some code style fixes
This commit is contained in:
Gary Russell
2016-10-28 12:07:06 -04:00
committed by Artem Bilan
parent 16be9fc47d
commit 67d6cd0c89
83 changed files with 870 additions and 1324 deletions

View File

@@ -64,13 +64,9 @@ import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
@@ -127,17 +123,13 @@ public class ImapMailReceiverTests {
public void testIdleWithServerCustomSearch() throws Exception {
ImapMailReceiver receiver = new ImapMailReceiver("imap://user:pw@localhost:" + imapIdleServer.getPort()
+ "/INBOX");
receiver.setSearchTermStrategy(new SearchTermStrategy() {
@Override
public SearchTerm generateSearchTerm(Flags supportedFlags, Folder folder) {
try {
FromTerm fromTerm = new FromTerm(new InternetAddress("bar@baz"));
return new AndTerm(fromTerm, new FlagTerm(new Flags(Flag.SEEN), false));
}
catch (AddressException e) {
throw new RuntimeException(e);
}
receiver.setSearchTermStrategy((supportedFlags, folder) -> {
try {
FromTerm fromTerm = new FromTerm(new InternetAddress("bar@baz"));
return new AndTerm(fromTerm, new FlagTerm(new Flags(Flag.SEEN), false));
}
catch (AddressException e) {
throw new RuntimeException(e);
}
});
testIdleWithServerGuts(receiver, false);
@@ -268,35 +260,19 @@ public class ImapMailReceiverTests {
final Message[] messages = new Message[] { msg1, msg2 };
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
DirectFieldAccessor accessor = new DirectFieldAccessor(invocation.getMock());
int folderOpenMode = (Integer) accessor.getPropertyValue("folderOpenMode");
if (folderOpenMode != Folder.READ_WRITE) {
throw new IllegalArgumentException("Folder had to be open in READ_WRITE mode");
}
return null;
doAnswer(invocation -> {
DirectFieldAccessor accessor = new DirectFieldAccessor(invocation.getMock());
int folderOpenMode = (Integer) accessor.getPropertyValue("folderOpenMode");
if (folderOpenMode != Folder.READ_WRITE) {
throw new IllegalArgumentException("Folder had to be open in READ_WRITE mode");
}
return null;
}).when(receiver).openFolder();
doAnswer(new Answer<Object>() {
doAnswer(invocation -> messages).when(receiver).searchForNewMessages();
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return messages;
}
}).when(receiver).searchForNewMessages();
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return null;
}
}).when(receiver).fetchMessages(messages);
doAnswer(invocation -> null).when(receiver).fetchMessages(messages);
receiver.receive();
return receiver;
}
@@ -348,34 +324,18 @@ public class ImapMailReceiverTests {
Message msg1 = mock(MimeMessage.class);
Message msg2 = mock(MimeMessage.class);
final Message[] messages = new Message[] { msg1, msg2 };
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
DirectFieldAccessor accessor = new DirectFieldAccessor(invocation.getMock());
int folderOpenMode = (Integer) accessor.getPropertyValue("folderOpenMode");
if (folderOpenMode != Folder.READ_WRITE) {
throw new IllegalArgumentException("Folder had to be open in READ_WRITE mode");
}
return null;
doAnswer(invocation -> {
DirectFieldAccessor accessor = new DirectFieldAccessor(invocation.getMock());
int folderOpenMode = (Integer) accessor.getPropertyValue("folderOpenMode");
if (folderOpenMode != Folder.READ_WRITE) {
throw new IllegalArgumentException("Folder had to be open in READ_WRITE mode");
}
return null;
}).when(receiver).openFolder();
doAnswer(new Answer<Object>() {
doAnswer(invocation -> messages).when(receiver).searchForNewMessages();
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return messages;
}
}).when(receiver).searchForNewMessages();
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return null;
}
}).when(receiver).fetchMessages(messages);
doAnswer(invocation -> null).when(receiver).fetchMessages(messages);
receiver.receive();
verify(msg1, times(1)).setFlag(Flag.SEEN, true);
verify(msg2, times(1)).setFlag(Flag.SEEN, true);
@@ -400,29 +360,11 @@ public class ImapMailReceiverTests {
Message msg1 = mock(MimeMessage.class);
Message msg2 = mock(MimeMessage.class);
final Message[] messages = new Message[] { msg1, msg2 };
doAnswer(new Answer<Object>() {
doAnswer(invocation -> null).when(receiver).openFolder();
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return null;
}
}).when(receiver).openFolder();
doAnswer(invocation -> messages).when(receiver).searchForNewMessages();
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return messages;
}
}).when(receiver).searchForNewMessages();
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return null;
}
}).when(receiver).fetchMessages(messages);
doAnswer(invocation -> null).when(receiver).fetchMessages(messages);
receiver.afterPropertiesSet();
receiver.receive();
verify(msg1, times(0)).setFlag(Flag.SEEN, true);
@@ -432,7 +374,7 @@ public class ImapMailReceiverTests {
@Test
public void receiveAndDontMarkAsReadButDelete() throws Exception {
AbstractMailReceiver receiver = new ImapMailReceiver();
((ImapMailReceiver) receiver).setShouldDeleteMessages(true);
receiver.setShouldDeleteMessages(true);
((ImapMailReceiver) receiver).setShouldMarkMessagesAsRead(false);
receiver = spy(receiver);
receiver.setBeanFactory(mock(BeanFactory.class));
@@ -447,34 +389,18 @@ public class ImapMailReceiverTests {
Message msg1 = mock(MimeMessage.class);
Message msg2 = mock(MimeMessage.class);
final Message[] messages = new Message[] { msg1, msg2 };
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
DirectFieldAccessor accessor = new DirectFieldAccessor(invocation.getMock());
int folderOpenMode = (Integer) accessor.getPropertyValue("folderOpenMode");
if (folderOpenMode != Folder.READ_WRITE) {
throw new IllegalArgumentException("Folder had to be open in READ_WRITE mode");
}
return null;
doAnswer(invocation -> {
DirectFieldAccessor accessor = new DirectFieldAccessor(invocation.getMock());
int folderOpenMode = (Integer) accessor.getPropertyValue("folderOpenMode");
if (folderOpenMode != Folder.READ_WRITE) {
throw new IllegalArgumentException("Folder had to be open in READ_WRITE mode");
}
return null;
}).when(receiver).openFolder();
doAnswer(new Answer<Object>() {
doAnswer(invocation -> messages).when(receiver).searchForNewMessages();
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return messages;
}
}).when(receiver).searchForNewMessages();
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return null;
}
}).when(receiver).fetchMessages(messages);
doAnswer(invocation -> null).when(receiver).fetchMessages(messages);
receiver.afterPropertiesSet();
receiver.receive();
verify(msg1, times(0)).setFlag(Flag.SEEN, true);
@@ -499,40 +425,25 @@ public class ImapMailReceiverTests {
Message msg1 = mock(MimeMessage.class);
Message msg2 = mock(MimeMessage.class);
final Message[] messages = new Message[] { msg1, msg2 };
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
DirectFieldAccessor accessor = new DirectFieldAccessor(invocation.getMock());
int folderOpenMode = (Integer) accessor.getPropertyValue("folderOpenMode");
if (folderOpenMode != Folder.READ_WRITE) {
throw new IllegalArgumentException("Folder had to be open in READ_WRITE mode");
}
return null;
doAnswer(invocation -> {
DirectFieldAccessor accessor = new DirectFieldAccessor(invocation.getMock());
int folderOpenMode = (Integer) accessor.getPropertyValue("folderOpenMode");
if (folderOpenMode != Folder.READ_WRITE) {
throw new IllegalArgumentException("Folder had to be open in READ_WRITE mode");
}
return null;
}).when(receiver).openFolder();
doAnswer(new Answer<Object>() {
doAnswer(invocation -> messages).when(receiver).searchForNewMessages();
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return messages;
}
}).when(receiver).searchForNewMessages();
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return null;
}
}).when(receiver).fetchMessages(messages);
doAnswer(invocation -> null).when(receiver).fetchMessages(messages);
receiver.receive();
verify(msg1, times(1)).setFlag(Flag.SEEN, true);
verify(msg2, times(1)).setFlag(Flag.SEEN, true);
verify(receiver, times(0)).deleteMessages((Message[]) Mockito.any());
}
@SuppressWarnings("resource")
@Test
@Ignore
public void testMessageHistory() throws Exception {
@@ -553,33 +464,17 @@ public class ImapMailReceiverTests {
when(mailMessage.getFlags()).thenReturn(flags);
final Message[] messages = new Message[] { mailMessage };
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
DirectFieldAccessor accessor = new DirectFieldAccessor((invocation.getMock()));
IMAPFolder folder = mock(IMAPFolder.class);
accessor.setPropertyValue("folder", folder);
when(folder.hasNewMessages()).thenReturn(true);
return null;
}
doAnswer(invocation -> {
DirectFieldAccessor accessor = new DirectFieldAccessor((invocation.getMock()));
IMAPFolder folder = mock(IMAPFolder.class);
accessor.setPropertyValue("folder", folder);
when(folder.hasNewMessages()).thenReturn(true);
return null;
}).when(receiver).openFolder();
doAnswer(new Answer<Object>() {
doAnswer(invocation -> messages).when(receiver).searchForNewMessages();
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return messages;
}
}).when(receiver).searchForNewMessages();
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return null;
}
}).when(receiver).fetchMessages(messages);
doAnswer(invocation -> null).when(receiver).fetchMessages(messages);
PollableChannel channel = context.getBean("channel", PollableChannel.class);
@@ -626,21 +521,9 @@ public class ImapMailReceiverTests {
when(folder.getPermanentFlags()).thenReturn(new Flags(Flags.Flag.USER));
folderField.set(receiver, folder);
doAnswer(new Answer<Object>() {
doAnswer(invocation -> true).when(folder).isOpen();
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return true;
}
}).when(folder).isOpen();
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return null;
}
}).when(receiver).openFolder();
doAnswer(invocation -> null).when(receiver).openFolder();
DirectFieldAccessor adapterAccessor = new DirectFieldAccessor(adapter);
adapterAccessor.setPropertyValue("mailReceiver", receiver);
@@ -650,21 +533,9 @@ public class ImapMailReceiverTests {
when(mailMessage.getFlags()).thenReturn(flags);
final Message[] messages = new Message[] { mailMessage };
doAnswer(new Answer<Object>() {
doAnswer(invocation -> messages).when(receiver).searchForNewMessages();
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return messages;
}
}).when(receiver).searchForNewMessages();
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return null;
}
}).when(receiver).fetchMessages(messages);
doAnswer(invocation -> null).when(receiver).fetchMessages(messages);
adapter.start();
org.springframework.messaging.Message<?> replMessage = errorChannel.receive(10000);
@@ -674,6 +545,7 @@ public class ImapMailReceiverTests {
context.close();
}
@SuppressWarnings("resource")
@Test
public void testNoInitialIdleDelayWhenRecentNotSupported() throws Exception {
ConfigurableApplicationContext context =
@@ -703,13 +575,7 @@ public class ImapMailReceiverTests {
when(store.getFolder(Mockito.any(URLName.class))).thenReturn(folder);
storeField.set(receiver, store);
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return folder;
}
}).when(receiver).getFolder();
doAnswer(invocation -> folder).when(receiver).getFolder();
MimeMessage mailMessage = mock(MimeMessage.class);
Flags flags = mock(Flags.class);
@@ -717,41 +583,27 @@ public class ImapMailReceiverTests {
final Message[] messages = new Message[] { mailMessage };
final AtomicInteger shouldFindMessagesCounter = new AtomicInteger(2);
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
/*
* Return the message from first invocation of waitForMessages()
* and in receive(); then return false in the next call to
* waitForMessages() so we enter idle(); counter will be reset
* to 1 in the mocked idle().
*/
if (shouldFindMessagesCounter.decrementAndGet() >= 0) {
return messages;
}
else {
return new Message[0];
}
doAnswer(invocation -> {
/*
* Return the message from first invocation of waitForMessages()
* and in receive(); then return false in the next call to
* waitForMessages() so we enter idle(); counter will be reset
* to 1 in the mocked idle().
*/
if (shouldFindMessagesCounter.decrementAndGet() >= 0) {
return messages;
}
else {
return new Message[0];
}
}).when(receiver).searchForNewMessages();
doAnswer(new Answer<Object>() {
doAnswer(invocation -> null).when(receiver).fetchMessages(messages);
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return null;
}
}).when(receiver).fetchMessages(messages);
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Thread.sleep(5000);
shouldFindMessagesCounter.set(1);
return null;
}
doAnswer(invocation -> {
Thread.sleep(5000);
shouldFindMessagesCounter.set(1);
return null;
}).when(folder).idle();
adapter.start();
@@ -768,6 +620,7 @@ public class ImapMailReceiverTests {
context.close();
}
@SuppressWarnings("resource")
@Test
public void testInitialIdleDelayWhenRecentIsSupported() throws Exception {
ConfigurableApplicationContext context =
@@ -797,44 +650,22 @@ public class ImapMailReceiverTests {
when(store.getFolder(Mockito.any(URLName.class))).thenReturn(folder);
storeField.set(receiver, store);
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return folder;
}
}).when(receiver).getFolder();
doAnswer(invocation -> folder).when(receiver).getFolder();
MimeMessage mailMessage = mock(MimeMessage.class);
Flags flags = mock(Flags.class);
when(mailMessage.getFlags()).thenReturn(flags);
final Message[] messages = new Message[] { mailMessage };
doAnswer(new Answer<Object>() {
doAnswer(invocation -> messages).when(receiver).searchForNewMessages();
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return messages;
}
}).when(receiver).searchForNewMessages();
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return null;
}
}).when(receiver).fetchMessages(messages);
doAnswer(invocation -> null).when(receiver).fetchMessages(messages);
final CountDownLatch idles = new CountDownLatch(2);
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
idles.countDown();
Thread.sleep(5000);
return null;
}
doAnswer(invocation -> {
idles.countDown();
Thread.sleep(5000);
return null;
}).when(folder).idle();
adapter.start();
@@ -856,20 +687,10 @@ public class ImapMailReceiverTests {
ImapIdleChannelAdapter adapter = new ImapIdleChannelAdapter(mailReceiver);
final AtomicReference<ImapIdleExceptionEvent> theEvent = new AtomicReference<ImapIdleExceptionEvent>();
final CountDownLatch latch = new CountDownLatch(1);
adapter.setApplicationEventPublisher(new ApplicationEventPublisher() {
@Override
public void publishEvent(ApplicationEvent event) {
assertNull("only one event expected", theEvent.get());
theEvent.set((ImapIdleExceptionEvent) event);
latch.countDown();
}
@Override
public void publishEvent(Object event) {
}
adapter.setApplicationEventPublisher(event -> {
assertNull("only one event expected", theEvent.get());
theEvent.set((ImapIdleExceptionEvent) event);
latch.countDown();
});
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.initialize();
@@ -898,34 +719,24 @@ public class ImapMailReceiverTests {
receiver.setBeanFactory(mock(BeanFactory.class));
receiver.afterPropertiesSet();
new Thread(new Runnable() {
@Override
public void run() {
try {
receiver.receive();
}
catch (javax.mail.MessagingException e) {
if (e.getCause() instanceof NullPointerException) {
e.printStackTrace();
failed.getAndIncrement();
}
}
new Thread(() -> {
try {
receiver.receive();
}
catch (javax.mail.MessagingException e) {
if (e.getCause() instanceof NullPointerException) {
failed.getAndIncrement();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
try {
receiver.destroy();
}
catch (Exception ignore) {
// ignore
ignore.printStackTrace();
}
new Thread(() -> {
try {
receiver.destroy();
}
catch (Exception ignore) {
// ignore
}
}).start();
}

View File

@@ -60,6 +60,10 @@ public class MailReceivingMessageSourceTests {
private final ConcurrentLinkedQueue<javax.mail.Message[]> messages = new ConcurrentLinkedQueue<javax.mail.Message[]>();
StubMailReceiver() {
super();
}
@Override
public javax.mail.Message[] receive() {
return messages.poll();

View File

@@ -34,8 +34,6 @@ import javax.mail.Folder;
import javax.mail.Message;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.mail.ImapIdleChannelAdapter;
@@ -49,6 +47,7 @@ import org.springframework.util.ReflectionUtils;
*/
public class ImapIdleIntegrationTests {
@SuppressWarnings("resource")
@Test
public void testWithTransactionSynchronization() throws Exception {
final AtomicBoolean block = new AtomicBoolean(false);
@@ -62,17 +61,14 @@ public class ImapIdleIntegrationTests {
// setup mock scenario
receiver = spy(receiver);
doAnswer(new Answer<Object>() { // ensures that waitFornewMessages call blocks after a first execution
doAnswer(invocation -> {
// ensures that waitFornewMessages call blocks after a first execution
// to emulate the behavior of IDLE
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
if (block.get()) {
Thread.sleep(5000);
}
block.set(true);
return null;
if (block.get()) {
Thread.sleep(5000);
}
block.set(true);
return null;
}).when(receiver).waitForNewMessages();
Message m1 = mock(Message.class);
@@ -90,14 +86,9 @@ public class ImapIdleIntegrationTests {
// end mock setup
final CountDownLatch txProcessorLatch = new CountDownLatch(1);
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
txProcessorLatch.countDown();
return null;
}
doAnswer(invocation -> {
txProcessorLatch.countDown();
return null;
}).when(processor).process(any(Message.class));
adapter.start();