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

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2016-2018 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.
@@ -17,6 +17,7 @@
package org.springframework.integration.scripting.dsl;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
@@ -44,7 +45,7 @@ import org.springframework.util.StringUtils;
* @since 5.0
*/
class DslScriptExecutingMessageProcessor
implements MessageProcessor<Object>, InitializingBean, ApplicationContextAware {
implements MessageProcessor<Object>, InitializingBean, ApplicationContextAware, BeanClassLoaderAware {
private Resource script;
@@ -60,6 +61,8 @@ class DslScriptExecutingMessageProcessor
private AbstractScriptExecutingMessageProcessor<?> delegate;
private ClassLoader classLoader;
DslScriptExecutingMessageProcessor(Resource script) {
this.script = script;
}
@@ -86,7 +89,12 @@ class DslScriptExecutingMessageProcessor
}
@Override
public void afterPropertiesSet() throws Exception {
public void setBeanClassLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
}
@Override
public void afterPropertiesSet() {
if (StringUtils.hasText(this.location)) {
this.script = this.applicationContext.getResource(this.location);
}
@@ -95,10 +103,13 @@ class DslScriptExecutingMessageProcessor
if (!StringUtils.hasText(this.lang)) {
String filename = this.script.getFilename();
int index = filename.lastIndexOf(".") + 1;
int index =
filename != null
? filename.lastIndexOf(".") + 1
: -1;
if (index < 1) {
throw new BeanCreationException("'lang' isn't provided and there is 'file extension' for script " +
"resource: " + this.script);
throw new BeanCreationException(
"'lang' isn't provided and there is no 'file extension' for script resource: " + this.script);
}
this.lang = filename.substring(index);
}
@@ -115,7 +126,7 @@ class DslScriptExecutingMessageProcessor
}
this.delegate.setBeanFactory(this.applicationContext);
this.delegate.setBeanClassLoader(this.applicationContext.getClassLoader());
this.delegate.setBeanClassLoader(this.classLoader);
}
@Override

View File

@@ -16,6 +16,7 @@
package org.springframework.integration.syslog.config;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.config.AbstractFactoryBean;
import org.springframework.context.ApplicationEventPublisher;
@@ -173,12 +174,13 @@ public class SyslogReceivingChannelAdapterFactoryBean extends AbstractFactoryBea
@Override
public Class<?> getObjectType() {
return this.adapter == null ? SyslogReceivingChannelAdapterSupport.class :
this.adapter.getClass();
return this.adapter == null
? SyslogReceivingChannelAdapterSupport.class
: this.adapter.getClass();
}
@Override
protected SyslogReceivingChannelAdapterSupport createInstance() throws Exception {
protected SyslogReceivingChannelAdapterSupport createInstance() {
SyslogReceivingChannelAdapterSupport adapter;
if (this.protocol == Protocol.tcp) {
adapter = new TcpSyslogReceivingChannelAdapter();
@@ -187,7 +189,8 @@ public class SyslogReceivingChannelAdapterFactoryBean extends AbstractFactoryBea
((TcpSyslogReceivingChannelAdapter) adapter).setConnectionFactory(this.connectionFactory);
}
else if (this.applicationEventPublisher != null) {
((TcpSyslogReceivingChannelAdapter) adapter).setApplicationEventPublisher(this.applicationEventPublisher);
((TcpSyslogReceivingChannelAdapter) adapter)
.setApplicationEventPublisher(this.applicationEventPublisher);
}
Assert.isNull(this.udpAdapter, "Cannot specify 'udp-attributes' when the protocol is 'tcp'");
}
@@ -222,12 +225,13 @@ public class SyslogReceivingChannelAdapterFactoryBean extends AbstractFactoryBea
if (this.beanName != null) {
adapter.setBeanName(this.beanName);
}
if (this.getBeanFactory() != null) {
adapter.setBeanFactory(this.getBeanFactory());
BeanFactory beanFactory = getBeanFactory();
if (beanFactory != null) {
adapter.setBeanFactory(beanFactory);
}
adapter.afterPropertiesSet();
this.adapter = adapter;
return adapter;
return this.adapter;
}
}

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;

View File

@@ -148,8 +148,10 @@ public class MockIntegrationContext implements BeanFactoryAware {
}
DirectFieldAccessor directFieldAccessor = new DirectFieldAccessor(endpoint);
Object targetMessageHandler = directFieldAccessor.getPropertyValue("handler");
Assert.notNull(targetMessageHandler, () -> "'handler' must not be null in the: " + endpoint);
if (endpoint instanceof ReactiveStreamsConsumer) {
Object targetSubscriber = directFieldAccessor.getPropertyValue("subscriber");
Assert.notNull(targetSubscriber, () -> "'subscriber' must not be null in the: " + endpoint);
this.beans.put(consumerEndpointId, Tuples.of(targetMessageHandler, targetSubscriber));
}
else {