From b326225df7bf83e592a0683d5c525163c9f42933 Mon Sep 17 00:00:00 2001 From: abilan Date: Wed, 29 Mar 2023 10:25:29 -0400 Subject: [PATCH] Fix tests for replyTimeout Some tests deliberately don't expect a reply, but they still block on a gateway's `sendAndReceive()` * Improve `Jsr223ScriptExecutingMessageProcessorTests` to verify that script variables work --- build.gradle | 5 +++ .../amqp/inbound/InboundEndpointTests.java | 40 ++----------------- .../dsl/transformers/TransformerTests.java | 3 +- ...3ScriptExecutingMessageProcessorTests.java | 38 +++++++++--------- .../scripting/jsr223/print_message.rb | 5 +-- 5 files changed, 31 insertions(+), 60 deletions(-) diff --git a/build.gradle b/build.gradle index e5cb08dca6..65d2caa4ed 100644 --- a/build.gradle +++ b/build.gradle @@ -878,6 +878,11 @@ project('spring-integration-scripting') { testRuntimeOnly 'org.jetbrains.kotlin:kotlin-scripting-compiler-embeddable' } + + tasks.withType(JavaForkOptions) { + jvmArgs '--add-opens', 'java.base/sun.nio.ch=ALL-UNNAMED', + '--add-opens', 'java.base/java.io=ALL-UNNAMED' + } } project('spring-integration-security') { diff --git a/spring-integration-amqp/src/test/java/org/springframework/integration/amqp/inbound/InboundEndpointTests.java b/spring-integration-amqp/src/test/java/org/springframework/integration/amqp/inbound/InboundEndpointTests.java index 2bc2efd394..478b1f7f94 100644 --- a/spring-integration-amqp/src/test/java/org/springframework/integration/amqp/inbound/InboundEndpointTests.java +++ b/spring-integration-amqp/src/test/java/org/springframework/integration/amqp/inbound/InboundEndpointTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2022 the original author or authors. + * Copyright 2013-2023 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,7 +19,6 @@ package org.springframework.integration.amqp.inbound; import java.util.ArrayList; import java.util.List; import java.util.Map; -import java.util.Objects; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; @@ -494,6 +493,7 @@ public class InboundEndpointTests { QueueChannel out = new QueueChannel(); gateway.setRequestChannel(out); gateway.setBindSourceMessage(true); + gateway.setReplyTimeout(0); gateway.afterPropertiesSet(); ChannelAwareMessageListener listener = (ChannelAwareMessageListener) container.getMessageListener(); SimpleBatchingStrategy bs = new SimpleBatchingStrategy(2, 10_000, 10_000L); @@ -784,41 +784,7 @@ public class InboundEndpointTests { assertThat(recoveredMessages.get()).isSameAs(messages); } - public static class Foo { - - private String bar; - - public Foo() { - } - - public Foo(String bar) { - this.bar = bar; - } - - public String getBar() { - return bar; - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - - Foo foo = (Foo) o; - - return Objects.equals(bar, foo.bar); - - } - - @Override - public int hashCode() { - return bar != null ? bar.hashCode() : 0; - } - + public record Foo(String bar) { } } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/dsl/transformers/TransformerTests.java b/spring-integration-core/src/test/java/org/springframework/integration/dsl/transformers/TransformerTests.java index ddaa0bf4aa..3695909145 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/dsl/transformers/TransformerTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/dsl/transformers/TransformerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2017-2022 the original author or authors. + * Copyright 2017-2023 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. @@ -292,6 +292,7 @@ public class TransformerTests { .errorChannel(enricherErrorChannel()) .requestPayloadExpression("payload") .shouldClonePayload(false) + .replyTimeout(1L) .propertyExpression("name", "payload['name']") .propertyFunction("date", m -> new Date()) .headerExpression("foo", "payload['name']") diff --git a/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/jsr223/Jsr223ScriptExecutingMessageProcessorTests.java b/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/jsr223/Jsr223ScriptExecutingMessageProcessorTests.java index 70eccb6fc5..13430d4386 100644 --- a/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/jsr223/Jsr223ScriptExecutingMessageProcessorTests.java +++ b/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/jsr223/Jsr223ScriptExecutingMessageProcessorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 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,8 +19,7 @@ package org.springframework.integration.scripting.jsr223; import java.util.HashMap; import java.util.Map; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.springframework.beans.factory.BeanFactory; @@ -35,49 +34,50 @@ import static org.assertj.core.api.Assertions.assertThat; /** * @author David Turanski + * @author Artem Bilan * */ public class Jsr223ScriptExecutingMessageProcessorTests { - ScriptExecutor executor; - - @Before - public void setUp() { - executor = ScriptExecutorFactory.getScriptExecutor("jruby"); - } + private static final ScriptExecutor SCRIPT_EXECUTOR = ScriptExecutorFactory.getScriptExecutor("jruby"); @Test public void testExecuteWithVariables() { - Map vars = new HashMap(); + Map vars = new HashMap<>(); vars.put("one", 1); vars.put("two", "two"); vars.put("three", 3); - ScriptSource scriptSource = new ResourceScriptSource(new ClassPathResource("/org/springframework/integration/scripting/jsr223/print_message.rb")); + ScriptSource scriptSource = + new ResourceScriptSource( + new ClassPathResource("/org/springframework/integration/scripting/jsr223/print_message.rb")); - ScriptExecutingMessageProcessor messageProcessor = new ScriptExecutingMessageProcessor(scriptSource, executor, vars); + ScriptExecutingMessageProcessor messageProcessor = + new ScriptExecutingMessageProcessor(scriptSource, SCRIPT_EXECUTOR, vars); messageProcessor.setBeanFactory(Mockito.mock(BeanFactory.class)); - Message message = new GenericMessage("hello"); + Message message = new GenericMessage<>("hello"); Object obj = messageProcessor.processMessage(message); - assertThat(obj.toString().substring(0, "hello modified".length())).isEqualTo("hello modified"); + assertThat(obj.toString()).contains("hello modified 1 two 3"); } @Test public void testWithNoVars() { - ScriptSource scriptSource = new ResourceScriptSource(new ClassPathResource("/org/springframework/integration/scripting/jsr223/print_message.rb")); + ScriptSource scriptSource = + new ResourceScriptSource( + new ClassPathResource("/org/springframework/integration/scripting/jsr223/print_message.rb")); - ScriptExecutingMessageProcessor messageProcessor = new ScriptExecutingMessageProcessor(scriptSource, executor); + ScriptExecutingMessageProcessor messageProcessor = + new ScriptExecutingMessageProcessor(scriptSource, SCRIPT_EXECUTOR); messageProcessor.setBeanFactory(Mockito.mock(BeanFactory.class)); - Message message = new GenericMessage("hello"); + Message message = new GenericMessage<>("hello"); Object obj = messageProcessor.processMessage(message); - assertThat(obj.toString().substring(0, "hello modified".length())).isEqualTo("hello modified"); + assertThat(obj.toString()).contains("hello modified"); } } - diff --git a/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/jsr223/print_message.rb b/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/jsr223/print_message.rb index 677ed132a1..41fb1007d0 100755 --- a/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/jsr223/print_message.rb +++ b/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/jsr223/print_message.rb @@ -2,11 +2,10 @@ require "java" java_import 'java.util.Date' #payload and headers a global variable if payload - payload = payload+" modified #{Date.new}" + payload = payload+" modified #{(defined? one) ? one : ''} #{(defined? two) ? two : ''} #{(defined? three) ? three : ''} #{Date.new}" end -puts payload if headers headers.each {|key, value| puts "#{key} is #{value}" } end -puts "#{$one} #{$two} #{$three}" +puts payload payload