From 19c13afafa547de7819ade56c327b698bc12e0de Mon Sep 17 00:00:00 2001 From: Timon Borter Date: Thu, 24 Oct 2024 14:58:16 +0200 Subject: [PATCH] chore(citrus-rmi): review and code cleanup pr: https://github.com/citrusframework/citrus/pull/1224 `citrus-rmi` module. --- .../citrusframework/rmi/client/RmiClient.java | 24 +++++----- .../citrusframework/rmi/server/RmiServer.java | 28 +++++------ .../rmi/integration/RmiDynamicEndpointIT.java | 2 +- .../rmi/integration/RmiEndpointIT.java | 1 - .../rmi/integration/RmiEndpointJavaIT.java | 1 - .../rmi/server/RmiServerTest.java | 46 +++++++++++-------- 6 files changed, 55 insertions(+), 47 deletions(-) diff --git a/endpoints/citrus-rmi/src/main/java/org/citrusframework/rmi/client/RmiClient.java b/endpoints/citrus-rmi/src/main/java/org/citrusframework/rmi/client/RmiClient.java index f58da3dcc8..1abc0c744a 100644 --- a/endpoints/citrus-rmi/src/main/java/org/citrusframework/rmi/client/RmiClient.java +++ b/endpoints/citrus-rmi/src/main/java/org/citrusframework/rmi/client/RmiClient.java @@ -16,14 +16,6 @@ package org.citrusframework.rmi.client; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.rmi.NotBoundException; -import java.rmi.Remote; -import java.rmi.RemoteException; -import java.rmi.registry.Registry; -import java.util.Arrays; - import org.citrusframework.context.TestContext; import org.citrusframework.endpoint.AbstractEndpoint; import org.citrusframework.exceptions.CitrusRuntimeException; @@ -46,6 +38,14 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.rmi.NotBoundException; +import java.rmi.Remote; +import java.rmi.RemoteException; +import java.rmi.registry.Registry; +import java.util.Arrays; + /** * @since 2.5 */ @@ -112,8 +112,8 @@ public void send(final Message message, TestContext context) { } if (logger.isDebugEnabled()) { - logger.debug("Sending message to RMI server: '" + binding + "'"); - logger.debug("Message to send:\n" + message.getPayload(String.class)); + logger.debug("Sending message to RMI server: '{}'", binding); + logger.debug("Message to send:\n{}", message.getPayload(String.class)); } context.onOutboundMessage(message); @@ -132,7 +132,7 @@ public void send(final Message message, TestContext context) { Message response = new DefaultMessage(payload.toString()); correlationManager.store(correlationKey, response); - logger.info("Message was sent to RMI server: '" + binding + "'"); + logger.info("Message was sent to RMI server: '{}'", binding); if (result != null) { context.onInboundMessage(response); } @@ -146,7 +146,7 @@ public void send(final Message message, TestContext context) { throw new CitrusRuntimeException("Failed to invoke method on remote target, because remote method not accessible", e); } - logger.info("Message was sent to RMI server: '" + binding + "'"); + logger.info("Message was sent to RMI server: '{}'", binding); } @Override diff --git a/endpoints/citrus-rmi/src/main/java/org/citrusframework/rmi/server/RmiServer.java b/endpoints/citrus-rmi/src/main/java/org/citrusframework/rmi/server/RmiServer.java index 900eec01ed..80484d5cb5 100644 --- a/endpoints/citrus-rmi/src/main/java/org/citrusframework/rmi/server/RmiServer.java +++ b/endpoints/citrus-rmi/src/main/java/org/citrusframework/rmi/server/RmiServer.java @@ -16,6 +16,17 @@ package org.citrusframework.rmi.server; +import org.citrusframework.exceptions.CitrusRuntimeException; +import org.citrusframework.message.Message; +import org.citrusframework.rmi.endpoint.RmiEndpointConfiguration; +import org.citrusframework.rmi.model.RmiServiceInvocation; +import org.citrusframework.rmi.model.RmiServiceResult; +import org.citrusframework.server.AbstractServer; +import org.citrusframework.util.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.xml.transform.Source; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; @@ -26,17 +37,6 @@ import java.rmi.registry.Registry; import java.rmi.server.UnicastRemoteObject; import java.util.List; -import javax.xml.transform.Source; - -import org.citrusframework.exceptions.CitrusRuntimeException; -import org.citrusframework.message.Message; -import org.citrusframework.rmi.endpoint.RmiEndpointConfiguration; -import org.citrusframework.rmi.model.RmiServiceInvocation; -import org.citrusframework.rmi.model.RmiServiceResult; -import org.citrusframework.server.AbstractServer; -import org.citrusframework.util.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * @since 2.5 @@ -78,7 +78,7 @@ public RmiServer(RmiEndpointConfiguration endpointConfiguration) { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (logger.isDebugEnabled()) { - logger.debug("Received message on RMI server: '" + endpointConfiguration.getBinding() + "'"); + logger.debug("Received message on RMI server: '{}'", endpointConfiguration.getBinding()); } Message response = getEndpointAdapter().handleMessage(endpointConfiguration.getMessageConverter() @@ -153,7 +153,7 @@ protected void shutdown() { try { registry.unbind(endpointConfiguration.getBinding()); } catch (Exception e) { - logger.warn("Failed to unbind from registry:" + e.getMessage()); + logger.warn("Failed to unbind from registry:{}", e.getMessage()); } } @@ -161,7 +161,7 @@ protected void shutdown() { try { UnicastRemoteObject.unexportObject(proxy, true); } catch (Exception e) { - logger.warn("Failed to unexport from remote object:" + e.getMessage()); + logger.warn("Failed to unexport from remote object:{}", e.getMessage()); } } diff --git a/endpoints/citrus-rmi/src/test/java/org/citrusframework/rmi/integration/RmiDynamicEndpointIT.java b/endpoints/citrus-rmi/src/test/java/org/citrusframework/rmi/integration/RmiDynamicEndpointIT.java index cac94f67a8..72fde4cb7c 100644 --- a/endpoints/citrus-rmi/src/test/java/org/citrusframework/rmi/integration/RmiDynamicEndpointIT.java +++ b/endpoints/citrus-rmi/src/test/java/org/citrusframework/rmi/integration/RmiDynamicEndpointIT.java @@ -19,7 +19,6 @@ import org.citrusframework.annotations.CitrusTestSource; import org.citrusframework.common.TestLoader; import org.citrusframework.testng.spring.TestNGCitrusSpringSupport; -import org.testng.annotations.Ignore; import org.testng.annotations.Test; /** @@ -27,6 +26,7 @@ */ public class RmiDynamicEndpointIT extends TestNGCitrusSpringSupport { + @Test @CitrusTestSource(type = TestLoader.SPRING, name = "RmiDynamicEndpointIT") public void testDynamicEndpoint() {} } diff --git a/endpoints/citrus-rmi/src/test/java/org/citrusframework/rmi/integration/RmiEndpointIT.java b/endpoints/citrus-rmi/src/test/java/org/citrusframework/rmi/integration/RmiEndpointIT.java index a7ae49082a..9175809993 100644 --- a/endpoints/citrus-rmi/src/test/java/org/citrusframework/rmi/integration/RmiEndpointIT.java +++ b/endpoints/citrus-rmi/src/test/java/org/citrusframework/rmi/integration/RmiEndpointIT.java @@ -19,7 +19,6 @@ import org.citrusframework.annotations.CitrusTestSource; import org.citrusframework.common.TestLoader; import org.citrusframework.testng.spring.TestNGCitrusSpringSupport; -import org.testng.annotations.Ignore; import org.testng.annotations.Test; /** diff --git a/endpoints/citrus-rmi/src/test/java/org/citrusframework/rmi/integration/RmiEndpointJavaIT.java b/endpoints/citrus-rmi/src/test/java/org/citrusframework/rmi/integration/RmiEndpointJavaIT.java index 52b3e88b98..e4de083396 100644 --- a/endpoints/citrus-rmi/src/test/java/org/citrusframework/rmi/integration/RmiEndpointJavaIT.java +++ b/endpoints/citrus-rmi/src/test/java/org/citrusframework/rmi/integration/RmiEndpointJavaIT.java @@ -25,7 +25,6 @@ import org.citrusframework.testng.spring.TestNGCitrusSpringSupport; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; -import org.testng.annotations.Ignore; import org.testng.annotations.Test; import static org.citrusframework.actions.ReceiveMessageAction.Builder.receive; diff --git a/endpoints/citrus-rmi/src/test/java/org/citrusframework/rmi/server/RmiServerTest.java b/endpoints/citrus-rmi/src/test/java/org/citrusframework/rmi/server/RmiServerTest.java index b673bada04..c234483b20 100644 --- a/endpoints/citrus-rmi/src/test/java/org/citrusframework/rmi/server/RmiServerTest.java +++ b/endpoints/citrus-rmi/src/test/java/org/citrusframework/rmi/server/RmiServerTest.java @@ -16,15 +16,6 @@ package org.citrusframework.rmi.server; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.doAnswer; -import static org.mockito.Mockito.reset; - -import java.io.IOException; -import java.rmi.Remote; -import java.rmi.registry.Registry; -import java.util.List; import org.citrusframework.endpoint.EndpointAdapter; import org.citrusframework.message.DefaultMessage; import org.citrusframework.message.Message; @@ -38,9 +29,20 @@ import org.mockito.MockitoAnnotations; import org.mockito.stubbing.Answer; import org.testng.Assert; -import org.testng.annotations.BeforeClass; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; +import java.io.IOException; +import java.rmi.Remote; +import java.rmi.registry.Registry; +import java.util.List; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.reset; + /** * @since 2.5 */ @@ -48,12 +50,20 @@ public class RmiServerTest extends AbstractTestNGUnitTest { @Mock private Registry registry; + @Mock private EndpointAdapter endpointAdapter; - @BeforeClass + private AutoCloseable mockitoContext; + + @BeforeMethod public void setup() { - MockitoAnnotations.openMocks(this); + mockitoContext = MockitoAnnotations.openMocks(this); + } + + @AfterMethod + public void teardown() throws Exception { + mockitoContext.close(); } @Test @@ -82,8 +92,8 @@ public void testServiceInvocationWithArgument() throws Exception { try { Assert.assertEquals( - message.getPayload(String.class).replaceAll("\\s", ""), - FileUtils.readToString(Resources.create("service-invocation.xml", RmiServer.class)).replaceAll("\\s", "") + message.getPayload(String.class).replaceAll("\\s", ""), + FileUtils.readToString(Resources.create("service-invocation.xml", RmiServer.class)).replaceAll("\\s", "") ); } catch (IOException e) { Assert.fail(e.getMessage()); @@ -95,7 +105,7 @@ public void testServiceInvocationWithArgument() throws Exception { rmiServer.startup(); try { - ((HelloService)remote[0]).sayHello("Hello RMI this is cool!"); + ((HelloService) remote[0]).sayHello("Hello RMI this is cool!"); } catch (Throwable throwable) { Assert.fail("Failed to invoke remote service", throwable); } @@ -127,8 +137,8 @@ public void testServiceInvocationWithResult() throws Exception { try { Assert.assertEquals( - message.getPayload(String.class).replaceAll("\\s", ""), - FileUtils.readToString(Resources.create("service-invocation-2.xml", RmiServer.class)).replaceAll("\\s", "") + message.getPayload(String.class).replaceAll("\\s", ""), + FileUtils.readToString(Resources.create("service-invocation-2.xml", RmiServer.class)).replaceAll("\\s", "") ); } catch (IOException e) { Assert.fail(e.getMessage()); @@ -140,7 +150,7 @@ public void testServiceInvocationWithResult() throws Exception { rmiServer.startup(); try { - Assert.assertEquals(((HelloService)remote[0]).getHelloCount(), 10); + Assert.assertEquals(((HelloService) remote[0]).getHelloCount(), 10); } catch (Throwable throwable) { Assert.fail("Failed to invoke remote service", throwable); }