Avoid throws Exception where possible - Phase I
* Polishing - PR Comments
This commit is contained in:
committed by
Artem Bilan
parent
005bc80680
commit
b187bca36e
@@ -653,7 +653,7 @@ public class ParserUnitTests {
|
||||
public static class FooAdvice extends AbstractRequestHandlerAdvice {
|
||||
|
||||
@Override
|
||||
protected Object doInvoke(ExecutionCallback callback, Object target, Message<?> message) throws Exception {
|
||||
protected Object doInvoke(ExecutionCallback callback, Object target, Message<?> message) {
|
||||
adviceCalled.countDown();
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.springframework.integration.ip.tcp;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.catchThrowable;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -27,6 +28,7 @@ import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketTimeoutException;
|
||||
@@ -74,6 +76,8 @@ import org.springframework.integration.test.rule.Log4j2LevelAdjuster;
|
||||
import org.springframework.integration.test.support.LongRunningIntegrationTest;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHandlingException;
|
||||
import org.springframework.messaging.MessagingException;
|
||||
import org.springframework.messaging.PollableChannel;
|
||||
import org.springframework.messaging.support.ErrorMessage;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
@@ -485,7 +489,8 @@ public class TcpOutboundGatewayTests {
|
||||
AbstractClientConnectionFactory factory1 = mock(AbstractClientConnectionFactory.class);
|
||||
TcpConnectionSupport mockConn1 = makeMockConnection();
|
||||
when(factory1.getConnection()).thenReturn(mockConn1);
|
||||
doThrow(new IOException("fail")).when(mockConn1).send(Mockito.any(Message.class));
|
||||
doThrow(new UncheckedIOException(new IOException("fail")))
|
||||
.when(mockConn1).send(Mockito.any(Message.class));
|
||||
|
||||
AbstractClientConnectionFactory factory2 = new TcpNetClientConnectionFactory("localhost",
|
||||
serverSocket.get().getLocalPort());
|
||||
@@ -567,7 +572,8 @@ public class TcpOutboundGatewayTests {
|
||||
TcpConnectionSupport mockConn1 = makeMockConnection();
|
||||
when(factory1.getConnection()).thenReturn(mockConn1);
|
||||
when(factory1.isSingleUse()).thenReturn(true);
|
||||
doThrow(new IOException("fail")).when(mockConn1).send(Mockito.any(Message.class));
|
||||
doThrow(new UncheckedIOException(new IOException("fail")))
|
||||
.when(mockConn1).send(Mockito.any(Message.class));
|
||||
CachingClientConnectionFactory cachingFactory1 = new CachingClientConnectionFactory(factory1, 1);
|
||||
|
||||
AbstractClientConnectionFactory factory2 = new TcpNetClientConnectionFactory("localhost",
|
||||
@@ -727,13 +733,10 @@ public class TcpOutboundGatewayTests {
|
||||
gateway.setBeanFactory(mock(BeanFactory.class));
|
||||
gateway.afterPropertiesSet();
|
||||
gateway.start();
|
||||
try {
|
||||
gateway.handleMessage(MessageBuilder.withPayload("Test").build());
|
||||
fail("expected failure");
|
||||
}
|
||||
catch (Exception e) {
|
||||
assertThat(e.getCause().getCause()).isInstanceOf(EOFException.class);
|
||||
}
|
||||
Throwable thrown = catchThrowable(() -> gateway.handleMessage(MessageBuilder.withPayload("Test").build()));
|
||||
assertThat(thrown).isInstanceOf(MessageHandlingException.class);
|
||||
assertThat(thrown.getCause()).isInstanceOf(MessagingException.class);
|
||||
assertThat(thrown.getCause().getCause()).isInstanceOf(EOFException.class);
|
||||
assertThat(TestUtils.getPropertyValue(gateway, "pendingReplies", Map.class).size()).isEqualTo(0);
|
||||
Message<?> reply = replyChannel.receive(0);
|
||||
assertThat(reply).isNull();
|
||||
@@ -837,13 +840,10 @@ public class TcpOutboundGatewayTests {
|
||||
gateway.setBeanFactory(mock(BeanFactory.class));
|
||||
gateway.afterPropertiesSet();
|
||||
gateway.start();
|
||||
try {
|
||||
gateway.handleMessage(MessageBuilder.withPayload("Test").build());
|
||||
fail("expected failure");
|
||||
}
|
||||
catch (Exception e) {
|
||||
assertThat(e.getCause().getCause()).isInstanceOf(SocketTimeoutException.class);
|
||||
}
|
||||
Throwable thrown = catchThrowable(() -> gateway.handleMessage(MessageBuilder.withPayload("Test").build()));
|
||||
assertThat(thrown).isInstanceOf(MessageHandlingException.class);
|
||||
assertThat(thrown.getCause()).isInstanceOf(MessagingException.class);
|
||||
assertThat(thrown.getCause().getCause()).isInstanceOf(SocketTimeoutException.class);
|
||||
assertThat(TestUtils.getPropertyValue(gateway, "pendingReplies", Map.class).size()).isEqualTo(0);
|
||||
Message<?> reply = replyChannel.receive(0);
|
||||
assertThat(reply).isNull();
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
package org.springframework.integration.ip.tcp.connection;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
@@ -32,6 +32,7 @@ import static org.mockito.Mockito.when;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.net.Socket;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.SocketChannel;
|
||||
@@ -73,6 +74,7 @@ import org.springframework.integration.test.util.TestUtils;
|
||||
import org.springframework.integration.util.PoolItemNotAvailableException;
|
||||
import org.springframework.integration.util.SimplePool;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessagingException;
|
||||
import org.springframework.messaging.PollableChannel;
|
||||
import org.springframework.messaging.SubscribableChannel;
|
||||
import org.springframework.messaging.support.ErrorMessage;
|
||||
@@ -363,13 +365,8 @@ public class CachingClientConnectionFactoryTests {
|
||||
private void doTestCloseOnSendError(TcpConnection conn1, TcpConnection conn2,
|
||||
CachingClientConnectionFactory cccf) throws Exception {
|
||||
TcpConnection cached1 = cccf.getConnection();
|
||||
try {
|
||||
cached1.send(new GenericMessage<String>("foo"));
|
||||
fail("Expected IOException");
|
||||
}
|
||||
catch (IOException e) {
|
||||
assertThat(e.getMessage()).isEqualTo("Foo");
|
||||
}
|
||||
assertThatExceptionOfType(MessagingException.class)
|
||||
.isThrownBy(() -> cached1.send(new GenericMessage<String>("foo")));
|
||||
// Before INT-3163 this failed with a timeout - connection not returned to pool after failure on send()
|
||||
TcpConnection cached2 = cccf.getConnection();
|
||||
assertThat(cached1.getConnectionId().contains(conn1.getConnectionId())).isTrue();
|
||||
@@ -550,7 +547,7 @@ public class CachingClientConnectionFactoryTests {
|
||||
when(factory2.getConnection()).thenReturn(mockConn2);
|
||||
when(factory1.isActive()).thenReturn(true);
|
||||
when(factory2.isActive()).thenReturn(true);
|
||||
doThrow(new IOException("fail")).when(mockConn1).send(Mockito.any(Message.class));
|
||||
doThrow(new UncheckedIOException(new IOException("fail"))).when(mockConn1).send(Mockito.any(Message.class));
|
||||
doAnswer(invocation -> null).when(mockConn2).send(Mockito.any(Message.class));
|
||||
FailoverClientConnectionFactory failoverFactory = new FailoverClientConnectionFactory(factories);
|
||||
failoverFactory.start();
|
||||
|
||||
@@ -27,6 +27,7 @@ import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import java.io.OutputStream;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.net.BindException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
@@ -258,13 +259,7 @@ public class ConnectionEventTests {
|
||||
}
|
||||
|
||||
private void testServerExceptionGuts(AbstractServerConnectionFactory factory) throws Exception {
|
||||
ServerSocket ss = null;
|
||||
try {
|
||||
ss = ServerSocketFactory.getDefault().createServerSocket(0);
|
||||
}
|
||||
catch (Exception e) {
|
||||
fail("Failed to get a server socket");
|
||||
}
|
||||
ServerSocket ss = ServerSocketFactory.getDefault().createServerSocket(0);
|
||||
factory.setPort(ss.getLocalPort());
|
||||
final AtomicReference<TcpConnectionServerExceptionEvent> theEvent =
|
||||
new AtomicReference<TcpConnectionServerExceptionEvent>();
|
||||
@@ -315,8 +310,8 @@ public class ConnectionEventTests {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected TcpConnectionSupport buildNewConnection() throws Exception {
|
||||
throw new UnknownHostException("Mocking for test ");
|
||||
protected TcpConnectionSupport buildNewConnection() {
|
||||
throw new UncheckedIOException(new UnknownHostException("Mocking for test "));
|
||||
}
|
||||
|
||||
};
|
||||
@@ -340,7 +335,7 @@ public class ConnectionEventTests {
|
||||
fail("expected exception");
|
||||
}
|
||||
catch (Exception e) {
|
||||
assertThat(e).isInstanceOf(UnknownHostException.class);
|
||||
assertThat(e.getCause()).isInstanceOf(UnknownHostException.class);
|
||||
TcpConnectionFailedEvent event = (TcpConnectionFailedEvent) failEvent.get();
|
||||
assertThat(event.getCause()).isSameAs(e);
|
||||
}
|
||||
|
||||
@@ -171,7 +171,7 @@ public class ConnectionTimeoutTests {
|
||||
connection.send(MessageBuilder.withPayload("foo").build());
|
||||
Thread.sleep(1400);
|
||||
assertThat(connection.isOpen()).isTrue();
|
||||
assertThat(clientCloseLatch.await(2000, TimeUnit.SECONDS)).isTrue();
|
||||
assertThat(clientCloseLatch.await(5, TimeUnit.SECONDS)).isTrue();
|
||||
assertThat(reply.get()).isNull();
|
||||
assertThat(connection.isOpen()).isFalse();
|
||||
server.stop();
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
package org.springframework.integration.ip.tcp.connection;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -25,6 +25,7 @@ import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.net.Socket;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.util.ArrayList;
|
||||
@@ -100,7 +101,8 @@ public class FailoverClientConnectionFactoryTests {
|
||||
when(factory2.getConnection()).thenReturn(conn2);
|
||||
when(factory1.isActive()).thenReturn(true);
|
||||
when(factory2.isActive()).thenReturn(true);
|
||||
doThrow(new IOException("fail")).when(conn1).send(Mockito.any(Message.class));
|
||||
doThrow(new UncheckedIOException(new IOException("fail")))
|
||||
.when(conn1).send(Mockito.any(Message.class));
|
||||
doAnswer(invocation -> null).when(conn2).send(Mockito.any(Message.class));
|
||||
FailoverClientConnectionFactory failoverFactory = new FailoverClientConnectionFactory(factories);
|
||||
failoverFactory.start();
|
||||
@@ -109,7 +111,7 @@ public class FailoverClientConnectionFactoryTests {
|
||||
Mockito.verify(conn2).send(message);
|
||||
}
|
||||
|
||||
@Test(expected = IOException.class)
|
||||
@Test(expected = UncheckedIOException.class)
|
||||
public void testFailoverAllDead() throws Exception {
|
||||
AbstractClientConnectionFactory factory1 = mock(AbstractClientConnectionFactory.class);
|
||||
AbstractClientConnectionFactory factory2 = mock(AbstractClientConnectionFactory.class);
|
||||
@@ -122,8 +124,10 @@ public class FailoverClientConnectionFactoryTests {
|
||||
when(factory2.getConnection()).thenReturn(conn2);
|
||||
when(factory1.isActive()).thenReturn(true);
|
||||
when(factory2.isActive()).thenReturn(true);
|
||||
doThrow(new IOException("fail")).when(conn1).send(Mockito.any(Message.class));
|
||||
doThrow(new IOException("fail")).when(conn2).send(Mockito.any(Message.class));
|
||||
doThrow(new UncheckedIOException(new IOException("fail")))
|
||||
.when(conn1).send(Mockito.any(Message.class));
|
||||
doThrow(new UncheckedIOException(new IOException("fail")))
|
||||
.when(conn2).send(Mockito.any(Message.class));
|
||||
FailoverClientConnectionFactory failoverFactory = new FailoverClientConnectionFactory(factories);
|
||||
failoverFactory.start();
|
||||
GenericMessage<String> message = new GenericMessage<String>("foo");
|
||||
@@ -148,11 +152,12 @@ public class FailoverClientConnectionFactoryTests {
|
||||
doAnswer(invocation -> {
|
||||
if (!failedOnce.get()) {
|
||||
failedOnce.set(true);
|
||||
throw new IOException("fail");
|
||||
throw new UncheckedIOException(new IOException("fail"));
|
||||
}
|
||||
return null;
|
||||
}).when(conn1).send(Mockito.any(Message.class));
|
||||
doThrow(new IOException("fail")).when(conn2).send(Mockito.any(Message.class));
|
||||
doThrow(new UncheckedIOException(new IOException("fail")))
|
||||
.when(conn2).send(Mockito.any(Message.class));
|
||||
FailoverClientConnectionFactory failoverFactory = new FailoverClientConnectionFactory(factories);
|
||||
failoverFactory.start();
|
||||
GenericMessage<String> message = new GenericMessage<String>("foo");
|
||||
@@ -161,15 +166,15 @@ public class FailoverClientConnectionFactoryTests {
|
||||
Mockito.verify(conn1, times(2)).send(message);
|
||||
}
|
||||
|
||||
@Test(expected = IOException.class)
|
||||
@Test(expected = UncheckedIOException.class)
|
||||
public void testFailoverConnectNone() throws Exception {
|
||||
AbstractClientConnectionFactory factory1 = mock(AbstractClientConnectionFactory.class);
|
||||
AbstractClientConnectionFactory factory2 = mock(AbstractClientConnectionFactory.class);
|
||||
List<AbstractClientConnectionFactory> factories = new ArrayList<AbstractClientConnectionFactory>();
|
||||
factories.add(factory1);
|
||||
factories.add(factory2);
|
||||
when(factory1.getConnection()).thenThrow(new IOException("fail"));
|
||||
when(factory2.getConnection()).thenThrow(new IOException("fail"));
|
||||
when(factory1.getConnection()).thenThrow(new UncheckedIOException(new IOException("fail")));
|
||||
when(factory2.getConnection()).thenThrow(new UncheckedIOException(new IOException("fail")));
|
||||
when(factory1.isActive()).thenReturn(true);
|
||||
when(factory2.isActive()).thenReturn(true);
|
||||
FailoverClientConnectionFactory failoverFactory = new FailoverClientConnectionFactory(factories);
|
||||
@@ -187,8 +192,11 @@ public class FailoverClientConnectionFactoryTests {
|
||||
factories.add(factory2);
|
||||
TcpConnectionSupport conn1 = makeMockConnection();
|
||||
doAnswer(invocation -> null).when(conn1).send(Mockito.any(Message.class));
|
||||
when(factory1.getConnection()).thenThrow(new IOException("fail")).thenReturn(conn1);
|
||||
when(factory2.getConnection()).thenThrow(new IOException("fail"));
|
||||
when(factory1.getConnection())
|
||||
.thenThrow(new UncheckedIOException(new IOException("fail")))
|
||||
.thenReturn(conn1);
|
||||
when(factory2.getConnection())
|
||||
.thenThrow(new UncheckedIOException(new IOException("fail")));
|
||||
when(factory1.isActive()).thenReturn(true);
|
||||
when(factory2.isActive()).thenReturn(true);
|
||||
FailoverClientConnectionFactory failoverFactory = new FailoverClientConnectionFactory(factories);
|
||||
@@ -214,20 +222,17 @@ public class FailoverClientConnectionFactoryTests {
|
||||
final AtomicInteger failCount = new AtomicInteger();
|
||||
doAnswer(invocation -> {
|
||||
if (failCount.incrementAndGet() < 3) {
|
||||
throw new IOException("fail");
|
||||
throw new UncheckedIOException(new IOException("fail"));
|
||||
}
|
||||
return null;
|
||||
}).when(conn1).send(Mockito.any(Message.class));
|
||||
doThrow(new IOException("fail")).when(conn2).send(Mockito.any(Message.class));
|
||||
doThrow(new UncheckedIOException(new IOException("fail")))
|
||||
.when(conn2).send(Mockito.any(Message.class));
|
||||
FailoverClientConnectionFactory failoverFactory = new FailoverClientConnectionFactory(factories);
|
||||
failoverFactory.start();
|
||||
GenericMessage<String> message = new GenericMessage<String>("foo");
|
||||
try {
|
||||
failoverFactory.getConnection().send(message);
|
||||
fail("ExpectedFailure");
|
||||
}
|
||||
catch (IOException e) {
|
||||
}
|
||||
assertThatExceptionOfType(UncheckedIOException.class)
|
||||
.isThrownBy(() -> failoverFactory.getConnection().send(message));
|
||||
failoverFactory.getConnection().send(message);
|
||||
Mockito.verify(conn2).send(message);
|
||||
Mockito.verify(conn1, times(3)).send(message);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,9 +19,6 @@ package org.springframework.integration.ip.tcp.connection;
|
||||
import java.util.concurrent.Semaphore;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.messaging.Message;
|
||||
@@ -34,8 +31,6 @@ import org.springframework.messaging.MessagingException;
|
||||
*/
|
||||
public class HelloWorldInterceptor extends TcpConnectionInterceptorSupport {
|
||||
|
||||
Log logger = LogFactory.getLog(this.getClass());
|
||||
|
||||
private volatile boolean negotiated;
|
||||
|
||||
private final Semaphore negotiationSemaphore = new Semaphore(0);
|
||||
@@ -109,14 +104,19 @@ public class HelloWorldInterceptor extends TcpConnectionInterceptorSupport {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(Message<?> message) throws Exception {
|
||||
public void send(Message<?> message) {
|
||||
this.pendingSend = true;
|
||||
try {
|
||||
if (!this.negotiated) {
|
||||
if (!this.isServer()) {
|
||||
logger.debug(this.toString() + " Sending " + hello);
|
||||
super.send(MessageBuilder.withPayload(hello).build());
|
||||
this.negotiationSemaphore.tryAcquire(this.timeout, TimeUnit.MILLISECONDS);
|
||||
try {
|
||||
this.negotiationSemaphore.tryAcquire(this.timeout, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
catch (@SuppressWarnings("unused") InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
if (!this.negotiated) {
|
||||
throw new MessagingException("Negotiation error");
|
||||
}
|
||||
|
||||
@@ -17,18 +17,15 @@
|
||||
package org.springframework.integration.ip.tcp.connection;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.InetAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketException;
|
||||
import java.nio.channels.ClosedChannelException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@@ -41,7 +38,6 @@ import java.util.concurrent.atomic.AtomicReference;
|
||||
import javax.net.ServerSocketFactory;
|
||||
import javax.net.SocketFactory;
|
||||
import javax.net.ssl.SSLEngine;
|
||||
import javax.net.ssl.SSLException;
|
||||
import javax.net.ssl.SSLServerSocket;
|
||||
|
||||
import org.junit.Test;
|
||||
@@ -51,6 +47,7 @@ import org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer
|
||||
import org.springframework.integration.ip.util.TestingUtilities;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessagingException;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
|
||||
/**
|
||||
@@ -366,13 +363,8 @@ public class SocketSupportTests {
|
||||
@Test
|
||||
public void testNetClientAndServerSSLDifferentContexts() throws Exception {
|
||||
testNetClientAndServerSSLDifferentContexts(false);
|
||||
try {
|
||||
testNetClientAndServerSSLDifferentContexts(true);
|
||||
fail("expected Exception");
|
||||
}
|
||||
catch (SSLException | SocketException e) {
|
||||
// NOSONAR
|
||||
}
|
||||
assertThatExceptionOfType(MessagingException.class)
|
||||
.isThrownBy(() -> testNetClientAndServerSSLDifferentContexts(true));
|
||||
}
|
||||
|
||||
private void testNetClientAndServerSSLDifferentContexts(boolean badClient) throws Exception {
|
||||
@@ -478,19 +470,10 @@ public class SocketSupportTests {
|
||||
@Test
|
||||
public void testNioClientAndServerSSLDifferentContexts() throws Exception {
|
||||
testNioClientAndServerSSLDifferentContexts(false);
|
||||
try {
|
||||
testNioClientAndServerSSLDifferentContexts(true);
|
||||
fail("expected Exception");
|
||||
}
|
||||
catch (IOException e) {
|
||||
if (!(e instanceof ClosedChannelException)) {
|
||||
assertThat(e.getMessage())
|
||||
.satisfiesAnyOf(
|
||||
s -> assertThat(s).contains("Socket closed during SSL Handshake"),
|
||||
s -> assertThat(s).contains("Broken pipe"),
|
||||
s -> assertThat(s).contains("Connection reset by peer"));
|
||||
}
|
||||
}
|
||||
assertThatExceptionOfType(MessagingException.class)
|
||||
.isThrownBy(() -> testNioClientAndServerSSLDifferentContexts(true))
|
||||
.withMessageMatching(".*(Socket closed during SSL Handshake|Broken pipe"
|
||||
+ "|Connection reset by peer|AsynchronousCloseException).*");
|
||||
}
|
||||
|
||||
private void testNioClientAndServerSSLDifferentContexts(boolean badClient) throws Exception {
|
||||
|
||||
@@ -146,7 +146,7 @@ public class TcpMessageMapperTests {
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testToMessageWithBadContentType() throws Exception {
|
||||
public void testToMessageWithBadContentType() {
|
||||
TcpMessageMapper mapper = new TcpMessageMapper();
|
||||
mapper.setAddContentTypeHeader(true);
|
||||
try {
|
||||
@@ -169,7 +169,7 @@ public class TcpMessageMapperTests {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(Message<?> message) throws Exception {
|
||||
public void send(Message<?> message) {
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -183,7 +183,7 @@ public class TcpMessageMapperTests {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getPayload() throws Exception {
|
||||
public Object getPayload() {
|
||||
return TEST_PAYLOAD.getBytes();
|
||||
}
|
||||
|
||||
@@ -252,7 +252,7 @@ public class TcpMessageMapperTests {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(Message<?> message) throws Exception {
|
||||
public void send(Message<?> message) {
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -266,7 +266,7 @@ public class TcpMessageMapperTests {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getPayload() throws Exception {
|
||||
public Object getPayload() {
|
||||
return TEST_PAYLOAD.getBytes();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2018 the original author or authors.
|
||||
* Copyright 2018-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -52,13 +52,13 @@ public class TcpNetConnectionSupportTests {
|
||||
server.setTcpNetConnectionSupport(new DefaultTcpNetConnectionSupport() {
|
||||
|
||||
@Override
|
||||
public TcpNetConnection createNewConnection(Socket socket, boolean server, boolean lookupHost,
|
||||
ApplicationEventPublisher applicationEventPublisher, String connectionFactoryName)
|
||||
throws Exception {
|
||||
public TcpNetConnection createNewConnection(Socket socket, boolean isServer, boolean lookupHost,
|
||||
ApplicationEventPublisher applicationEventPublisher, String connectionFactoryName) {
|
||||
|
||||
if (firstTime.getAndSet(false)) {
|
||||
throw new RuntimeException("intended");
|
||||
}
|
||||
return super.createNewConnection(socket, server, lookupHost, applicationEventPublisher, connectionFactoryName);
|
||||
return super.createNewConnection(socket, isServer, lookupHost, applicationEventPublisher, connectionFactoryName);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.springframework.integration.ip.tcp.connection;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
@@ -63,6 +64,7 @@ public class TcpNetConnectionTests {
|
||||
connection.setDeserializer(new ByteArrayStxEtxSerializer());
|
||||
final AtomicReference<Object> log = new AtomicReference<Object>();
|
||||
Log logger = mock(Log.class);
|
||||
given(logger.isErrorEnabled()).willReturn(true);
|
||||
doAnswer(invocation -> {
|
||||
log.set(invocation.getArguments()[0]);
|
||||
return null;
|
||||
|
||||
@@ -83,6 +83,7 @@ import org.springframework.integration.test.rule.Log4j2LevelAdjuster;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
import org.springframework.integration.util.CompositeExecutor;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessagingException;
|
||||
import org.springframework.messaging.support.ErrorMessage;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
@@ -143,8 +144,8 @@ public class TcpNioConnectionTests {
|
||||
TcpConnection connection = factory.getConnection();
|
||||
connection.send(MessageBuilder.withPayload(new byte[1000000]).build());
|
||||
}
|
||||
catch (Exception e) {
|
||||
assertThat(e instanceof SocketTimeoutException)
|
||||
catch (MessagingException e) {
|
||||
assertThat(e.getCause() instanceof SocketTimeoutException)
|
||||
.as("Expected SocketTimeoutException, got " + e.getClass().getSimpleName() +
|
||||
":" + e.getMessage()).isTrue();
|
||||
}
|
||||
|
||||
@@ -102,7 +102,7 @@ public class UdpChannelAdapterTests {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected DatagramPacket receive() throws Exception {
|
||||
protected DatagramPacket receive() throws IOException {
|
||||
if (stopping.get()) {
|
||||
return new DatagramPacket(new byte[0], 0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user