Use LogAccessor from SF

* Change main classes to use a `LogAccessor` API to simplify code flow
* Fix tests according `LogAccessor` property
* Fix some Sonar smells
This commit is contained in:
Artem Bilan
2020-10-06 13:56:50 -04:00
parent a66e82b0aa
commit c7ff99a4e8
95 changed files with 1165 additions and 1432 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@@ -110,9 +110,7 @@ public class NotificationListeningMessageProducer extends MessageProducerSupport
*/
@Override
public void handleNotification(Notification notification, Object handback) {
if (this.logger.isInfoEnabled()) {
this.logger.info("received notification: " + notification + ", and handback: " + handback);
}
this.logger.info(() -> "received notification: " + notification + ", and handback: " + handback);
AbstractIntegrationMessageBuilder<?> builder = getMessageBuilderFactory().withPayload(notification);
if (handback != null) {
builder.setHeader(JmxHeaders.NOTIFICATION_HANDBACK, handback);
@@ -152,7 +150,7 @@ public class NotificationListeningMessageProducer extends MessageProducerSupport
try {
Collection<ObjectName> objectNames = this.retrieveMBeanNames();
if (objectNames.size() < 1) {
this.logger.error("No MBeans found matching ObjectName pattern(s): " +
this.logger.error(() -> "No MBeans found matching ObjectName pattern(s): " +
Arrays.toString(this.mBeanObjectNames));
}
for (ObjectName objectName : objectNames) {
@@ -179,14 +177,14 @@ public class NotificationListeningMessageProducer extends MessageProducerSupport
try {
this.server.removeNotificationListener(objectName, this, this.filter, this.handback);
}
catch (InstanceNotFoundException e) {
this.logger.error("Failed to find MBean instance.", e);
catch (InstanceNotFoundException ex) {
this.logger.error(ex, "Failed to find MBean instance.");
}
catch (ListenerNotFoundException e) {
this.logger.error("Failed to find NotificationListener.", e);
catch (ListenerNotFoundException ex) {
this.logger.error(ex, "Failed to find NotificationListener.");
}
catch (IOException e) {
this.logger.error("IOException on MBeanServerConnection.", e);
catch (IOException ex) {
this.logger.error(ex, "IOException on MBeanServerConnection.");
}
}
}
@@ -202,13 +200,11 @@ public class NotificationListeningMessageProducer extends MessageProducerSupport
catch (IOException e) {
throw new IllegalStateException("IOException on MBeanServerConnection.", e);
}
if (mBeanInfos.size() == 0 && this.logger.isDebugEnabled()) {
this.logger.debug("No MBeans found matching pattern: " + pattern);
if (mBeanInfos.size() == 0) {
this.logger.debug(() -> "No MBeans found matching pattern: " + pattern);
}
for (ObjectInstance instance : mBeanInfos) {
if (this.logger.isDebugEnabled()) {
this.logger.debug("Found MBean: " + instance.getObjectName().toString());
}
this.logger.debug(() -> "Found MBean: " + instance.getObjectName().toString());
objectNames.add(instance.getObjectName());
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@@ -21,14 +21,13 @@ import static org.mockito.BDDMockito.willReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import org.apache.commons.logging.Log;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.log.LogAccessor;
import org.springframework.integration.handler.advice.AbstractRequestHandlerAdvice;
import org.springframework.integration.jmx.JmxHeaders;
import org.springframework.integration.jmx.OperationInvokingMessageHandler;
@@ -38,7 +37,7 @@ import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
/**
* @author Mark Fisher
@@ -48,7 +47,7 @@ import org.springframework.test.context.junit4.SpringRunner;
*
* @since 2.0
*/
@RunWith(SpringRunner.class)
@SpringJUnitConfig
@DirtiesContext
public class OperationInvokingChannelAdapterParserTests {
@@ -80,7 +79,7 @@ public class OperationInvokingChannelAdapterParserTests {
private static int adviceCalled;
@After
@AfterEach
public void resetLists() {
testBean.messages.clear();
}
@@ -98,7 +97,9 @@ public class OperationInvokingChannelAdapterParserTests {
@Test
public void testOutboundAdapterWithNonNullReturn() {
Log logger = spy(TestUtils.getPropertyValue(this.operationWithNonNullReturnHandler, "logger", Log.class));
LogAccessor logger = spy(TestUtils.getPropertyValue(this.operationWithNonNullReturnHandler, "logger",
LogAccessor.class));
willReturn(true)
.given(logger)
@@ -132,9 +133,9 @@ public class OperationInvokingChannelAdapterParserTests {
@Test
public void testOperationWithinChainWithNonNullReturn() {
Log logger =
LogAccessor logger =
spy(TestUtils.getPropertyValue(this.operationWithinChainWithNonNullReturnHandler, "logger",
Log.class));
LogAccessor.class));
willReturn(true)
.given(logger)