Fix code smell in test and syslog modules (#2667)

* Fix code smell in test and syslog modules

* * Fix code smell in the scripting module
This commit is contained in:
Artem Bilan
2018-12-18 15:58:40 -05:00
committed by Gary Russell
parent 12dd73d219
commit 271181247d
4 changed files with 51 additions and 26 deletions

View File

@@ -174,7 +174,7 @@ public abstract class TestUtils {
private String getComponentNameIfNamed(final MessageChannel channel) {
Set<Class<?>> interfaces = ClassUtils.getAllInterfacesAsSet(channel);
final AtomicReference<String> componentName = new AtomicReference<String>();
final AtomicReference<String> componentName = new AtomicReference<>();
for (Class<?> intface : interfaces) {
if ("org.springframework.integration.support.context.NamedComponent".equals(intface.getName())) {
ReflectionUtils.doWithMethods(channel.getClass(), method -> {
@@ -235,12 +235,12 @@ public abstract class TestUtils {
}
@Override
public void handleError(Throwable t) {
MessageChannel errorChannel = this.resolveErrorChannel(t);
public void handleError(Throwable throwable) {
MessageChannel errorChannel = resolveErrorChannel(throwable);
boolean sent = false;
if (errorChannel != null) {
try {
sent = errorChannel.send(new ErrorMessage(t), 10000);
sent = errorChannel.send(new ErrorMessage(throwable), 10000);
}
catch (Throwable errorDeliveryError) { //NOSONAR
// message will be logged only
@@ -253,13 +253,15 @@ public abstract class TestUtils {
}
}
if (!sent && logger.isErrorEnabled()) {
Message<?> failedMessage = (t instanceof MessagingException) ?
((MessagingException) t).getFailedMessage() : null;
Message<?> failedMessage =
throwable instanceof MessagingException
? ((MessagingException) throwable).getFailedMessage()
: null;
if (failedMessage != null) {
logger.error("failure occurred in messaging task with message: " + failedMessage, t);
logger.error("failure occurred in messaging task with message: " + failedMessage, throwable);
}
else {
logger.error("failure occurred in messaging task", t);
logger.error("failure occurred in messaging task", throwable);
}
}
@@ -268,14 +270,20 @@ public abstract class TestUtils {
private MessageChannel resolveErrorChannel(Throwable t) {
if (t instanceof MessagingException) {
Message<?> failedMessage = ((MessagingException) t).getFailedMessage();
if (failedMessage == null) {
return null;
}
Object errorChannelHeader = failedMessage.getHeaders().getErrorChannel();
if (errorChannelHeader instanceof MessageChannel) {
return (MessageChannel) errorChannelHeader;
}
Assert.isInstanceOf(String.class, errorChannelHeader, "Unsupported error channel header type. " +
"Expected MessageChannel or String, but actual type is [" +
errorChannelHeader.getClass() + "]");
return this.context.getBean((String) errorChannelHeader, MessageChannel.class);
else if (errorChannelHeader instanceof String) {
return this.context.getBean((String) errorChannelHeader, MessageChannel.class);
}
else {
throw new IllegalStateException("Unsupported error channel header type. " +
"Expected MessageChannel or String, but actual header is [" + errorChannelHeader + "]");
}
}
else {
return null;