messagePublisher;
private int order = Ordered.LOWEST_PRECEDENCE;
- public EventEmittingStepExecutionListener(MessageChannel output) {
- Assert.notNull(output, "An output channel is required");
- this.messagePublisher = new MessagePublisher<>(output);
+ private TaskEventProperties properties;
+
+ public EventEmittingStepExecutionListener(MessagePublisher messagePublisher, TaskEventProperties properties) {
+ Assert.notNull(messagePublisher, "messagePublisher is required");
+ Assert.notNull(properties, "properties is required");
+
+ this.messagePublisher = messagePublisher;
+ this.properties = properties;
}
- public EventEmittingStepExecutionListener(MessageChannel output, int order) {
- this(output);
+ public EventEmittingStepExecutionListener(MessagePublisher messagePublisher, int order, TaskEventProperties properties) {
+ this(messagePublisher, properties);
this.order = order;
}
@Override
public void beforeStep(StepExecution stepExecution) {
- this.messagePublisher.publish(new StepExecutionEvent(stepExecution));
+ this.messagePublisher.publish(this.properties.getStepExecutionEventBindingName(), new StepExecutionEvent(stepExecution));
}
@Override
public ExitStatus afterStep(StepExecution stepExecution) {
- this.messagePublisher.publish(new StepExecutionEvent(stepExecution));
+ this.messagePublisher.publish(this.properties.getStepExecutionEventBindingName(), new StepExecutionEvent(stepExecution));
return stepExecution.getExitStatus();
}
diff --git a/spring-cloud-task-stream/src/main/java/org/springframework/cloud/task/batch/listener/support/MessagePublisher.java b/spring-cloud-task-stream/src/main/java/org/springframework/cloud/task/batch/listener/support/MessagePublisher.java
index 2734642e..d500f785 100644
--- a/spring-cloud-task-stream/src/main/java/org/springframework/cloud/task/batch/listener/support/MessagePublisher.java
+++ b/spring-cloud-task-stream/src/main/java/org/springframework/cloud/task/batch/listener/support/MessagePublisher.java
@@ -16,8 +16,8 @@
package org.springframework.cloud.task.batch.listener.support;
+import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.messaging.Message;
-import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.util.Assert;
@@ -29,31 +29,32 @@ import org.springframework.util.Assert;
*/
public class MessagePublisher {
- private final MessageChannel listenerEventsChannel;
- public MessagePublisher(MessageChannel listenerEventsChannel) {
- Assert.notNull(listenerEventsChannel, "listenerEventsChannel must not be null");
- this.listenerEventsChannel = listenerEventsChannel;
+ private final StreamBridge streamBridge;
+
+ public MessagePublisher(StreamBridge streamBridge) {
+ Assert.notNull(streamBridge, "streamBridge must not be null");
+ this.streamBridge = streamBridge;
}
- public final void publish(P payload) {
+ public final void publish(String bindingName, P payload) {
if (payload instanceof Message) {
- this.publishMessage((Message>) payload);
+ this.publishMessage(bindingName, (Message>) payload);
}
else {
Message
message = MessageBuilder.withPayload(payload).build();
- this.listenerEventsChannel.send(message);
+ this.streamBridge.send(bindingName, message);
}
}
- private void publishMessage(Message> message) {
- this.listenerEventsChannel.send(message);
+ private void publishMessage(String bindingName, Message> message) {
+ this.streamBridge.send(bindingName, message);
}
- public void publishWithThrowableHeader(P payload, String header) {
+ public void publishWithThrowableHeader(String bindingName, P payload, String header) {
Message
message = MessageBuilder.withPayload(payload)
.setHeader(BatchJobHeaders.BATCH_EXCEPTION, header).build();
- publishMessage(message);
+ publishMessage(bindingName, message);
}
}
diff --git a/spring-cloud-task-stream/src/main/java/org/springframework/cloud/task/batch/listener/support/TaskEventProperties.java b/spring-cloud-task-stream/src/main/java/org/springframework/cloud/task/batch/listener/support/TaskEventProperties.java
index 18d95aa8..ea77d0ad 100644
--- a/spring-cloud-task-stream/src/main/java/org/springframework/cloud/task/batch/listener/support/TaskEventProperties.java
+++ b/spring-cloud-task-stream/src/main/java/org/springframework/cloud/task/batch/listener/support/TaskEventProperties.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2017-2019 the original author or authors.
+ * Copyright 2017-2021 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.
@@ -67,6 +67,15 @@ public class TaskEventProperties {
*/
private int skipOrder = Ordered.LOWEST_PRECEDENCE;
+ private String jobExecutionEventBindingName = "job-execution-events";
+ private String skipEventBindingName = "skip-events";
+ private String chunkEventBindingName = "chunk-events";
+ private String itemProcessEventBindingName = "item-process-events";
+ private String itemReadEventBindingName = "item-read-events";
+ private String itemWriteEventBindingName = "item-write-events";
+ private String stepExecutionEventBindingName = "step-execution-events";
+ private String taskEventBindingName = "task-events";
+
public int getJobExecutionOrder() {
return this.jobExecutionOrder;
}
@@ -123,4 +132,68 @@ public class TaskEventProperties {
this.skipOrder = skipOrder;
}
+
+ public String getJobExecutionEventBindingName() {
+ return jobExecutionEventBindingName;
+ }
+
+ public void setJobExecutionEventBindingName(String jobExecutionEventBindingName) {
+ this.jobExecutionEventBindingName = jobExecutionEventBindingName;
+ }
+
+ public String getSkipEventBindingName() {
+ return skipEventBindingName;
+ }
+
+ public void setSkipEventBindingName(String skipEventBindingName) {
+ this.skipEventBindingName = skipEventBindingName;
+ }
+
+ public String getChunkEventBindingName() {
+ return chunkEventBindingName;
+ }
+
+ public void setChunkEventBindingName(String chunkEventBindingName) {
+ this.chunkEventBindingName = chunkEventBindingName;
+ }
+
+ public String getItemProcessEventBindingName() {
+ return itemProcessEventBindingName;
+ }
+
+ public void setItemProcessEventBindingName(String itemProcessEventBindingName) {
+ this.itemProcessEventBindingName = itemProcessEventBindingName;
+ }
+
+ public String getItemReadEventBindingName() {
+ return itemReadEventBindingName;
+ }
+
+ public void setItemReadEventBindingName(String itemReadEventBindingName) {
+ this.itemReadEventBindingName = itemReadEventBindingName;
+ }
+
+ public String getItemWriteEventBindingName() {
+ return itemWriteEventBindingName;
+ }
+
+ public void setItemWriteEventBindingName(String itemWriteEventBindingName) {
+ this.itemWriteEventBindingName = itemWriteEventBindingName;
+ }
+
+ public String getStepExecutionEventBindingName() {
+ return stepExecutionEventBindingName;
+ }
+
+ public void setStepExecutionEventBindingName(String stepExecutionEventBindingName) {
+ this.stepExecutionEventBindingName = stepExecutionEventBindingName;
+ }
+
+ public String getTaskEventBindingName() {
+ return taskEventBindingName;
+ }
+
+ public void setTaskEventBindingName(String taskEventBindingName) {
+ this.taskEventBindingName = taskEventBindingName;
+ }
}
diff --git a/spring-cloud-task-stream/src/main/java/org/springframework/cloud/task/launcher/TaskLauncherSink.java b/spring-cloud-task-stream/src/main/java/org/springframework/cloud/task/launcher/TaskLauncherSink.java
index 3ce3469a..82f46881 100644
--- a/spring-cloud-task-stream/src/main/java/org/springframework/cloud/task/launcher/TaskLauncherSink.java
+++ b/spring-cloud-task-stream/src/main/java/org/springframework/cloud/task/launcher/TaskLauncherSink.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2016-2019 the original author or authors.
+ * Copyright 2016-2021 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.
@@ -16,6 +16,8 @@
package org.springframework.cloud.task.launcher;
+import java.util.function.Consumer;
+
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -24,10 +26,9 @@ import org.springframework.cloud.deployer.resource.support.DelegatingResourceLoa
import org.springframework.cloud.deployer.spi.core.AppDefinition;
import org.springframework.cloud.deployer.spi.core.AppDeploymentRequest;
import org.springframework.cloud.deployer.spi.task.TaskLauncher;
-import org.springframework.cloud.stream.annotation.EnableBinding;
-import org.springframework.cloud.stream.messaging.Sink;
+import org.springframework.context.annotation.Bean;
import org.springframework.core.io.Resource;
-import org.springframework.integration.annotation.ServiceActivator;
+import org.springframework.messaging.Message;
import org.springframework.util.Assert;
/**
@@ -36,7 +37,6 @@ import org.springframework.util.Assert;
* @author Glenn Renfro
*/
-@EnableBinding(Sink.class)
public class TaskLauncherSink {
private final static Logger logger = LoggerFactory.getLogger(TaskLauncherSink.class);
@@ -52,13 +52,13 @@ public class TaskLauncherSink {
/**
* Launches a task upon the receipt of a valid TaskLaunchRequest.
- * @param taskLaunchRequest is a TaskLaunchRequest containing the information required
- * to launch a task.
- * @throws Exception if error occurs during task launch.
+ * @return the {@link Consumer} that will retrieve messages from binder.
*/
- @ServiceActivator(inputChannel = Sink.INPUT)
- public void taskLauncherSink(TaskLaunchRequest taskLaunchRequest) throws Exception {
- launchTask(taskLaunchRequest);
+ @Bean
+ public Consumer> taskLauncherSink() {
+ return messagePayload -> {
+ launchTask(messagePayload.getPayload());
+ };
}
private void launchTask(TaskLaunchRequest taskLaunchRequest) {
diff --git a/spring-cloud-task-stream/src/main/java/org/springframework/cloud/task/listener/TaskEventAutoConfiguration.java b/spring-cloud-task-stream/src/main/java/org/springframework/cloud/task/listener/TaskEventAutoConfiguration.java
index f6d7526c..7a61dad8 100644
--- a/spring-cloud-task-stream/src/main/java/org/springframework/cloud/task/listener/TaskEventAutoConfiguration.java
+++ b/spring-cloud-task-stream/src/main/java/org/springframework/cloud/task/listener/TaskEventAutoConfiguration.java
@@ -22,22 +22,22 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
-import org.springframework.cloud.stream.annotation.EnableBinding;
-import org.springframework.cloud.stream.annotation.Output;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.stream.config.BindingServiceConfiguration;
+import org.springframework.cloud.stream.function.StreamBridge;
+import org.springframework.cloud.task.batch.listener.support.TaskEventProperties;
import org.springframework.cloud.task.configuration.SimpleTaskAutoConfiguration;
+import org.springframework.cloud.task.repository.TaskExecution;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
-import org.springframework.integration.gateway.GatewayProxyFactoryBean;
-import org.springframework.messaging.MessageChannel;
/**
* @author Michael Minella
* @author Glenn Renfro
*/
@Configuration(proxyBeanMethods = false)
-@ConditionalOnClass(EnableBinding.class)
+@ConditionalOnClass(StreamBridge.class)
@ConditionalOnBean(TaskLifecycleListener.class)
@ConditionalOnExpression("T(org.springframework.util.StringUtils).isEmpty('${spring.batch.job.jobName:}')")
// @checkstyle:off
@@ -47,38 +47,32 @@ import org.springframework.messaging.MessageChannel;
@PropertySource("classpath:/org/springframework/cloud/task/application.properties")
@AutoConfigureBefore(BindingServiceConfiguration.class)
@AutoConfigureAfter(SimpleTaskAutoConfiguration.class)
+@EnableConfigurationProperties(TaskEventProperties.class)
public class TaskEventAutoConfiguration {
- /**
- * Task Event channels definition.
- */
- public interface TaskEventChannels {
-
- /**
- * Name of the default task events channel.
- */
- String TASK_EVENTS = "task-events";
-
- @Output(TASK_EVENTS)
- MessageChannel taskEvents();
-
- }
-
/**
* Configuration for a {@link TaskExecutionListener}.
*/
@Configuration(proxyBeanMethods = false)
- @EnableBinding(TaskEventChannels.class)
public static class ListenerConfiguration {
-
@Bean
- public GatewayProxyFactoryBean taskEventListener() {
- GatewayProxyFactoryBean factoryBean = new GatewayProxyFactoryBean(
- TaskExecutionListener.class);
+ public TaskExecutionListener taskEventEmitter(StreamBridge streamBridge, TaskEventProperties taskEventProperties) {
+ return new TaskExecutionListener() {
+ @Override
+ public void onTaskStartup(TaskExecution taskExecution) {
+ streamBridge.send(taskEventProperties.getTaskEventBindingName(), taskExecution);
+ }
- factoryBean.setDefaultRequestChannelName(TaskEventChannels.TASK_EVENTS);
+ @Override
+ public void onTaskEnd(TaskExecution taskExecution) {
+ streamBridge.send(taskEventProperties.getTaskEventBindingName(), taskExecution);
+ }
- return factoryBean;
+ @Override
+ public void onTaskFailed(TaskExecution taskExecution, Throwable throwable) {
+ streamBridge.send(taskEventProperties.getTaskEventBindingName(), taskExecution);
+ }
+ };
}
}
diff --git a/spring-cloud-task-stream/src/test/java/org/springframework/cloud/task/batch/listener/EventListenerTests.java b/spring-cloud-task-stream/src/test/java/org/springframework/cloud/task/batch/listener/EventListenerTests.java
index e489fbfb..4ec7a9b4 100644
--- a/spring-cloud-task-stream/src/test/java/org/springframework/cloud/task/batch/listener/EventListenerTests.java
+++ b/spring-cloud-task-stream/src/test/java/org/springframework/cloud/task/batch/listener/EventListenerTests.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2016-2019 the original author or authors.
+ * Copyright 2016-2021 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.
@@ -16,10 +16,14 @@
package org.springframework.cloud.task.batch.listener;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
+import com.fasterxml.jackson.databind.DeserializationFeature;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -29,10 +33,18 @@ import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.scope.context.StepContext;
+import org.springframework.boot.WebApplicationType;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.builder.SpringApplicationBuilder;
+import org.springframework.cloud.stream.binder.test.OutputDestination;
+import org.springframework.cloud.stream.binder.test.TestChannelBinderConfiguration;
+import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.cloud.task.batch.listener.support.JobExecutionEvent;
+import org.springframework.cloud.task.batch.listener.support.MessagePublisher;
import org.springframework.cloud.task.batch.listener.support.StepExecutionEvent;
+import org.springframework.cloud.task.batch.listener.support.TaskEventProperties;
+import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.Ordered;
-import org.springframework.integration.channel.QueueChannel;
import org.springframework.messaging.Message;
import static org.assertj.core.api.Assertions.assertThat;
@@ -43,8 +55,6 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class EventListenerTests {
- private QueueChannel queueChannel;
-
private EventEmittingSkipListener eventEmittingSkipListener;
private EventEmittingItemProcessListener eventEmittingItemProcessListener;
@@ -59,22 +69,43 @@ public class EventListenerTests {
private EventEmittingChunkListener eventEmittingChunkListener;
+ private ConfigurableApplicationContext applicationContext;
+
+ private final TaskEventProperties taskEventProperties = new TaskEventProperties();
+
+ private final ObjectMapper objectMapper = new ObjectMapper();
+
@BeforeEach
public void beforeTests() {
- this.queueChannel = new QueueChannel(1);
- this.eventEmittingSkipListener = new EventEmittingSkipListener(this.queueChannel);
+ this.applicationContext = new SpringApplicationBuilder()
+ .sources(TestChannelBinderConfiguration
+ .getCompleteConfiguration(BatchEventsApplication.class)).web(WebApplicationType.NONE).build()
+ .run();
+ StreamBridge streamBridge = this.applicationContext.getBean(StreamBridge.class);
+ MessagePublisher messagePublisher = new MessagePublisher(streamBridge);
+ objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
+
+ this.eventEmittingSkipListener = new EventEmittingSkipListener(
+ messagePublisher, this.taskEventProperties);
this.eventEmittingItemProcessListener = new EventEmittingItemProcessListener(
- this.queueChannel);
+ messagePublisher, this.taskEventProperties);
this.eventEmittingItemReadListener = new EventEmittingItemReadListener(
- this.queueChannel);
+ messagePublisher, this.taskEventProperties);
this.eventEmittingItemWriteListener = new EventEmittingItemWriteListener(
- this.queueChannel);
+ messagePublisher, this.taskEventProperties);
this.eventEmittingJobExecutionListener = new EventEmittingJobExecutionListener(
- this.queueChannel);
+ messagePublisher, this.taskEventProperties);
this.eventEmittingStepExecutionListener = new EventEmittingStepExecutionListener(
- this.queueChannel);
+ messagePublisher, this.taskEventProperties);
this.eventEmittingChunkListener = new EventEmittingChunkListener(
- this.queueChannel, 0);
+ messagePublisher, 0, this.taskEventProperties);
+ }
+
+ @AfterEach
+ public void tearDown() {
+ if (this.applicationContext != null && this.applicationContext.isActive()) {
+ this.applicationContext.close();
+ }
}
@Test
@@ -96,188 +127,171 @@ public class EventListenerTests {
@Test
public void testItemProcessListenerOnProcessorError() {
- RuntimeException exeption = new RuntimeException("Test Exception");
- this.eventEmittingItemProcessListener.onProcessError("HELLO", exeption);
- assertThat(this.queueChannel.getQueueSize()).isEqualTo(1);
+ this.eventEmittingItemProcessListener.onProcessError("HELLO",
+ new RuntimeException("Test Exception"));
- Message msg = this.queueChannel.receive();
- assertThat(msg.getPayload())
- .isEqualTo("Exception while item was being processed");
+ assertThat(getStringFromDestination(this.taskEventProperties.getItemProcessEventBindingName()))
+ .isEqualTo("\"Exception while item was being processed\"");
}
@Test
public void testItemProcessListenerAfterProcess() {
this.eventEmittingItemProcessListener.afterProcess("HELLO_AFTER_PROCESS_EQUAL",
"HELLO_AFTER_PROCESS_EQUAL");
- assertThat(this.queueChannel.getQueueSize()).isEqualTo(1);
- Message msg = this.queueChannel.receive();
- assertThat(msg.getPayload()).isEqualTo("item equaled result after processing");
+ assertThat(getStringFromDestination(this.taskEventProperties.getItemProcessEventBindingName()))
+ .isEqualTo("\"item equaled result after processing\"");
this.eventEmittingItemProcessListener.afterProcess("HELLO_NOT_EQUAL", "WORLD");
- assertThat(this.queueChannel.getQueueSize()).isEqualTo(1);
- msg = this.queueChannel.receive();
- assertThat(msg.getPayload())
- .isEqualTo("item did not equal result after processing");
+ assertThat(getStringFromDestination(this.taskEventProperties.getItemProcessEventBindingName()))
+ .isEqualTo("\"item did not equal result after processing\"");
this.eventEmittingItemProcessListener.afterProcess("HELLO_AFTER_PROCESS", null);
- assertThat(this.queueChannel.getQueueSize()).isEqualTo(1);
- msg = this.queueChannel.receive();
- assertThat(msg.getPayload()).isEqualTo("1 item was filtered");
+ assertThat(getStringFromDestination(this.taskEventProperties.
+ getItemProcessEventBindingName())).isEqualTo("\"1 item was filtered\"");
}
@Test
public void testItemProcessBeforeProcessor() {
this.eventEmittingItemProcessListener.beforeProcess("HELLO_BEFORE_PROCESS");
- assertThat(this.queueChannel.getQueueSize()).isEqualTo(0);
+ assertNoMessageFromDestination(this.taskEventProperties.getItemProcessEventBindingName());
}
@Test
public void EventEmittingSkipListenerSkipRead() {
- RuntimeException exeption = new RuntimeException("Text Exception");
- this.eventEmittingSkipListener.onSkipInRead(exeption);
- assertThat(this.queueChannel.getQueueSize()).isEqualTo(1);
- Message msg = this.queueChannel.receive();
- assertThat(msg.getPayload()).isEqualTo("Skipped when reading.");
+ this.eventEmittingSkipListener.onSkipInRead(new RuntimeException("Text Exception"));
+ assertThat(getStringFromDestination(this.taskEventProperties.
+ getSkipEventBindingName())).isEqualTo("\"Skipped when reading.\"");
}
@Test
public void EventEmittingSkipListenerSkipWrite() {
- final String MESSAGE = "HELLO_SKIP_WRITE";
- RuntimeException exeption = new RuntimeException("Text Exception");
- this.eventEmittingSkipListener.onSkipInWrite(MESSAGE, exeption);
- assertThat(this.queueChannel.getQueueSize()).isEqualTo(1);
- Message msg = this.queueChannel.receive();
- assertThat(msg.getPayload()).isEqualTo(MESSAGE);
+ final String MESSAGE = "\"HELLO_SKIP_WRITE\"";
+ this.eventEmittingSkipListener.onSkipInWrite(MESSAGE,
+ new RuntimeException("Text Exception"));
+ assertThat(getStringFromDestination(this.taskEventProperties.
+ getSkipEventBindingName())).isEqualTo(MESSAGE);
}
@Test
public void EventEmittingSkipListenerSkipProcess() {
- final String MESSAGE = "HELLO_SKIP_PROCESS";
- RuntimeException exeption = new RuntimeException("Text Exception");
- this.eventEmittingSkipListener.onSkipInProcess(MESSAGE, exeption);
- assertThat(this.queueChannel.getQueueSize()).isEqualTo(1);
- Message msg = this.queueChannel.receive();
- assertThat(msg.getPayload()).isEqualTo(MESSAGE);
+ final String MESSAGE = "\"HELLO_SKIP_PROCESS\"";
+ this.eventEmittingSkipListener.onSkipInProcess(MESSAGE,
+ new RuntimeException("Text Exception"));
+ assertThat(getStringFromDestination(this.taskEventProperties.
+ getSkipEventBindingName())).isEqualTo(MESSAGE);
}
@Test
public void EventEmittingItemReadListener() {
- RuntimeException exeption = new RuntimeException("Text Exception");
- this.eventEmittingItemReadListener.onReadError(exeption);
- assertThat(this.queueChannel.getQueueSize()).isEqualTo(1);
- Message msg = this.queueChannel.receive();
- assertThat(msg.getPayload()).isEqualTo("Exception while item was being read");
+ this.eventEmittingItemReadListener.onReadError(new RuntimeException("Text Exception"));
+ assertThat(getStringFromDestination(this.taskEventProperties.
+ getItemReadEventBindingName())).isEqualTo("\"Exception while item was being read\"");
}
@Test
public void EventEmittingItemReadListenerBeforeRead() {
this.eventEmittingItemReadListener.beforeRead();
- assertThat(this.queueChannel.getQueueSize()).isEqualTo(0);
+ assertNoMessageFromDestination(this.taskEventProperties.getItemReadEventBindingName());
}
@Test
public void EventEmittingItemReadListenerAfterRead() {
this.eventEmittingItemReadListener.afterRead("HELLO_AFTER_READ");
- assertThat(this.queueChannel.getQueueSize()).isEqualTo(0);
+ assertNoMessageFromDestination(this.taskEventProperties.getItemReadEventBindingName());
}
@Test
public void EventEmittingItemWriteListenerBeforeWrite() {
this.eventEmittingItemWriteListener.beforeWrite(getSampleList());
- assertThat(this.queueChannel.getQueueSize()).isEqualTo(1);
- Message msg = this.queueChannel.receive();
- assertThat(msg.getPayload()).isEqualTo("3 items to be written.");
+ assertThat(getStringFromDestination(this.taskEventProperties.getItemWriteEventBindingName()))
+ .isEqualTo("\"3 items to be written.\"");
}
@Test
public void EventEmittingItemWriteListenerAfterWrite() {
this.eventEmittingItemWriteListener.afterWrite(getSampleList());
- assertThat(this.queueChannel.getQueueSize()).isEqualTo(1);
- Message msg = this.queueChannel.receive();
- assertThat(msg.getPayload()).isEqualTo("3 items have been written.");
+ assertThat(getStringFromDestination(this.taskEventProperties.getItemWriteEventBindingName()))
+ .isEqualTo("\"3 items have been written.\"");
}
@Test
public void EventEmittingItemWriteListenerWriteError() {
- RuntimeException exeption = new RuntimeException("Text Exception");
- this.eventEmittingItemWriteListener.onWriteError(exeption, getSampleList());
- assertThat(this.queueChannel.getQueueSize()).isEqualTo(1);
- Message msg = this.queueChannel.receive();
- assertThat(msg.getPayload())
- .isEqualTo("Exception while 3 items are attempted to be written.");
+ RuntimeException exception = new RuntimeException("Text Exception");
+ this.eventEmittingItemWriteListener.onWriteError(exception, getSampleList());
+
+ assertThat(getStringFromDestination(this.taskEventProperties.getItemWriteEventBindingName()))
+ .isEqualTo("\"Exception while 3 items are attempted to be written.\"");
}
@Test
- public void EventEmittingJobExecutionListenerBeforeJob() {
+ public void EventEmittingJobExecutionListenerBeforeJob() throws IOException {
JobExecution jobExecution = getJobExecution();
this.eventEmittingJobExecutionListener.beforeJob(jobExecution);
- assertThat(this.queueChannel.getQueueSize()).isEqualTo(1);
- Message msg = this.queueChannel.receive();
- JobExecutionEvent jobEvent = (JobExecutionEvent) msg.getPayload();
+ List> result = testListener(this.taskEventProperties.getJobExecutionEventBindingName(), 1);
+ assertThat(result.get(0)).isNotNull();
+
+ JobExecutionEvent jobEvent = this.objectMapper.readValue(result.get(0).getPayload(), JobExecutionEvent.class);
assertThat(jobEvent.getJobInstance().getJobName())
.isEqualTo(jobExecution.getJobInstance().getJobName());
}
@Test
- public void EventEmittingJobExecutionListenerAfterJob() {
+ public void EventEmittingJobExecutionListenerAfterJob() throws IOException {
JobExecution jobExecution = getJobExecution();
this.eventEmittingJobExecutionListener.afterJob(jobExecution);
- assertThat(this.queueChannel.getQueueSize()).isEqualTo(1);
- Message msg = this.queueChannel.receive();
- JobExecutionEvent jobEvent = (JobExecutionEvent) msg.getPayload();
+ List> result = testListener(this.taskEventProperties.getJobExecutionEventBindingName(), 1);
+ assertThat(result.get(0)).isNotNull();
+
+ JobExecutionEvent jobEvent = this.objectMapper.readValue(result.get(0).getPayload(), JobExecutionEvent.class);
assertThat(jobEvent.getJobInstance().getJobName())
.isEqualTo(jobExecution.getJobInstance().getJobName());
}
@Test
- public void EventEmittingStepExecutionListenerBeforeStep() {
+ public void EventEmittingStepExecutionListenerBeforeStep() throws IOException {
final String STEP_MESSAGE = "BEFORE_STEP_MESSAGE";
- JobExecution jobExecution = getJobExecution();
- StepExecution stepExecution = new StepExecution(STEP_MESSAGE, jobExecution);
+ StepExecution stepExecution = new StepExecution(STEP_MESSAGE, getJobExecution());
this.eventEmittingStepExecutionListener.beforeStep(stepExecution);
- assertThat(this.queueChannel.getQueueSize()).isEqualTo(1);
- Message msg = this.queueChannel.receive();
- StepExecutionEvent stepExecutionEvent = (StepExecutionEvent) msg.getPayload();
+
+ List> result = testListener(this.taskEventProperties.getStepExecutionEventBindingName(), 1);
+ assertThat(result.get(0)).isNotNull();
+
+ StepExecutionEvent stepExecutionEvent = this.objectMapper.readValue(result.get(0).getPayload(), StepExecutionEvent.class);
assertThat(stepExecutionEvent.getStepName()).isEqualTo(STEP_MESSAGE);
}
@Test
- public void EventEmittingStepExecutionListenerAfterStep() {
+ public void EventEmittingStepExecutionListenerAfterStep() throws IOException {
final String STEP_MESSAGE = "AFTER_STEP_MESSAGE";
- JobExecution jobExecution = getJobExecution();
- StepExecution stepExecution = new StepExecution(STEP_MESSAGE, jobExecution);
+ StepExecution stepExecution = new StepExecution(STEP_MESSAGE, getJobExecution());
this.eventEmittingStepExecutionListener.afterStep(stepExecution);
- assertThat(this.queueChannel.getQueueSize()).isEqualTo(1);
- Message msg = this.queueChannel.receive();
- StepExecutionEvent stepExecutionEvent = (StepExecutionEvent) msg.getPayload();
+ List> result = testListener(this.taskEventProperties.getStepExecutionEventBindingName(), 1);
+
+ assertThat(result.get(0)).isNotNull();
+ StepExecutionEvent stepExecutionEvent = this.objectMapper.readValue(result.get(0).getPayload(), StepExecutionEvent.class);
assertThat(stepExecutionEvent.getStepName()).isEqualTo(STEP_MESSAGE);
}
@Test
public void EventEmittingChunkExecutionListenerBeforeChunk() {
- final String CHUNK_MESSAGE = "Before Chunk Processing";
- ChunkContext chunkContext = getChunkContext();
- this.eventEmittingChunkListener.beforeChunk(chunkContext);
- assertThat(this.queueChannel.getQueueSize()).isEqualTo(1);
- Message msg = this.queueChannel.receive();
- assertThat(msg.getPayload()).isEqualTo(CHUNK_MESSAGE);
+ final String CHUNK_MESSAGE = "\"Before Chunk Processing\"";
+ this.eventEmittingChunkListener.beforeChunk(getChunkContext());
+ assertThat(getStringFromDestination(this.taskEventProperties.getChunkEventBindingName()))
+ .isEqualTo(CHUNK_MESSAGE);
}
@Test
public void EventEmittingChunkExecutionListenerAfterChunk() {
- final String CHUNK_MESSAGE = "After Chunk Processing";
- ChunkContext chunkContext = getChunkContext();
- this.eventEmittingChunkListener.afterChunk(chunkContext);
- assertThat(this.queueChannel.getQueueSize()).isEqualTo(1);
- Message msg = this.queueChannel.receive();
- assertThat(msg.getPayload()).isEqualTo(CHUNK_MESSAGE);
+ final String CHUNK_MESSAGE = "\"After Chunk Processing\"";
+ this.eventEmittingChunkListener.afterChunk(getChunkContext());
+ assertThat(getStringFromDestination(this.taskEventProperties.getChunkEventBindingName()))
+ .isEqualTo(CHUNK_MESSAGE);
}
@Test
public void EventEmittingChunkExecutionListenerAfterChunkError() {
- ChunkContext chunkContext = getChunkContext();
- this.eventEmittingChunkListener.afterChunkError(chunkContext);
- assertThat(this.queueChannel.getQueueSize()).isEqualTo(0);
+ this.eventEmittingChunkListener.afterChunkError(getChunkContext());
+ assertNoMessageFromDestination(this.taskEventProperties.getChunkEventBindingName());
}
private JobExecution getJobExecution() {
@@ -303,4 +317,29 @@ public class EventListenerTests {
return chunkContext;
}
+ private List> testListener(String bindingName, int numberToRead) {
+ List> results = new ArrayList<>();
+ OutputDestination target = this.applicationContext.getBean(OutputDestination.class);
+ for (int i = 0; i < numberToRead; i++) {
+ results.add(target.receive(10000, bindingName));
+ }
+ return results;
+ }
+
+ private String getStringFromDestination(String bindingName) {
+ List> result = testListener(bindingName, 1);
+ assertThat(result.get(0)).isNotNull();
+
+ assertThat(new String(result.get(0).getPayload()));
+ return new String(result.get(0).getPayload());
+ }
+
+ private void assertNoMessageFromDestination(String bindingName) {
+ List> result = testListener(bindingName, 1);
+ assertThat(result.get(0)).isNull();
+ }
+
+ @SpringBootApplication
+ public static class BatchEventsApplication {
+ }
}
diff --git a/spring-cloud-task-stream/src/test/java/org/springframework/cloud/task/batch/listener/JobExecutionEventTests.java b/spring-cloud-task-stream/src/test/java/org/springframework/cloud/task/batch/listener/JobExecutionEventTests.java
index 48933910..8a0d41a8 100644
--- a/spring-cloud-task-stream/src/test/java/org/springframework/cloud/task/batch/listener/JobExecutionEventTests.java
+++ b/spring-cloud-task-stream/src/test/java/org/springframework/cloud/task/batch/listener/JobExecutionEventTests.java
@@ -36,15 +36,14 @@ import org.springframework.batch.core.StepExecution;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.autoconfigure.AutoConfigurations;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
-import org.springframework.cloud.stream.test.binder.TestSupportBinderAutoConfiguration;
import org.springframework.cloud.task.batch.listener.support.JobExecutionEvent;
import org.springframework.cloud.task.batch.listener.support.JobInstanceEvent;
import org.springframework.cloud.task.batch.listener.support.StepExecutionEvent;
import org.springframework.cloud.task.configuration.SimpleTaskAutoConfiguration;
import org.springframework.cloud.task.configuration.SingleTaskConfiguration;
-import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import static org.assertj.core.api.Assertions.assertThat;
@@ -64,18 +63,19 @@ public class JobExecutionEventTests {
private static final String JOB_CONFIGURATION_NAME = "FOO_JOB_CONFIG";
private static final String[] LISTENER_BEAN_NAMES = {
- BatchEventAutoConfiguration.JOB_EXECUTION_EVENTS_LISTENER,
- BatchEventAutoConfiguration.STEP_EXECUTION_EVENTS_LISTENER,
- BatchEventAutoConfiguration.CHUNK_EVENTS_LISTENER,
- BatchEventAutoConfiguration.ITEM_READ_EVENTS_LISTENER,
- BatchEventAutoConfiguration.ITEM_WRITE_EVENTS_LISTENER,
- BatchEventAutoConfiguration.ITEM_PROCESS_EVENTS_LISTENER,
- BatchEventAutoConfiguration.SKIP_EVENTS_LISTENER };
+ BatchEventAutoConfiguration.JOB_EXECUTION_EVENTS_LISTENER,
+ BatchEventAutoConfiguration.STEP_EXECUTION_EVENTS_LISTENER,
+ BatchEventAutoConfiguration.CHUNK_EVENTS_LISTENER,
+ BatchEventAutoConfiguration.ITEM_READ_EVENTS_LISTENER,
+ BatchEventAutoConfiguration.ITEM_WRITE_EVENTS_LISTENER,
+ BatchEventAutoConfiguration.ITEM_PROCESS_EVENTS_LISTENER,
+ BatchEventAutoConfiguration.SKIP_EVENTS_LISTENER};
private JobParameters jobParameters;
private JobInstance jobInstance;
+ //
@BeforeEach
public void setup() {
this.jobInstance = new JobInstance(JOB_INSTANCE_ID, JOB_NAME);
@@ -86,33 +86,33 @@ public class JobExecutionEventTests {
public void testBasic() {
JobExecution jobExecution;
jobExecution = new JobExecution(this.jobInstance, JOB_EXECUTION_ID,
- this.jobParameters, JOB_CONFIGURATION_NAME);
+ this.jobParameters, JOB_CONFIGURATION_NAME);
JobExecutionEvent jobExecutionEvent = new JobExecutionEvent(jobExecution);
assertThat(jobExecutionEvent.getJobInstance())
- .as("jobInstance should not be null").isNotNull();
+ .as("jobInstance should not be null").isNotNull();
assertThat(jobExecutionEvent.getJobParameters())
- .as("jobParameters should not be null").isNotNull();
+ .as("jobParameters should not be null").isNotNull();
assertThat(jobExecutionEvent.getJobConfigurationName())
- .as("jobConfigurationName did not match expected")
- .isEqualTo(JOB_CONFIGURATION_NAME);
+ .as("jobConfigurationName did not match expected")
+ .isEqualTo(JOB_CONFIGURATION_NAME);
assertThat(jobExecutionEvent.getJobParameters().getParameters().size())
- .as("jobParameters size did not match").isEqualTo(0);
+ .as("jobParameters size did not match").isEqualTo(0);
assertThat(jobExecutionEvent.getJobInstance().getJobName())
- .as("jobInstance name did not match").isEqualTo(JOB_NAME);
+ .as("jobInstance name did not match").isEqualTo(JOB_NAME);
assertThat(jobExecutionEvent.getStepExecutions().size())
- .as("no step executions were expected").isEqualTo(0);
+ .as("no step executions were expected").isEqualTo(0);
assertThat(jobExecutionEvent.getExitStatus().getExitCode())
- .as("exitStatus did not match expected").isEqualTo("UNKNOWN");
+ .as("exitStatus did not match expected").isEqualTo("UNKNOWN");
}
@Test
public void testJobParameters() {
- String[] JOB_PARAM_KEYS = { "A", "B", "C", "D" };
+ String[] JOB_PARAM_KEYS = {"A", "B", "C", "D"};
Date testDate = new Date();
- JobParameter[] PARAMETERS = { new JobParameter("FOO", true),
- new JobParameter(1L, true), new JobParameter(1D, true),
- new JobParameter(testDate, false) };
+ JobParameter[] PARAMETERS = {new JobParameter("FOO", true),
+ new JobParameter(1L, true), new JobParameter(1D, true),
+ new JobParameter(testDate, false)};
Map jobParamMap = new LinkedHashMap<>();
for (int paramCount = 0; paramCount < JOB_PARAM_KEYS.length; paramCount++) {
@@ -121,33 +121,33 @@ public class JobExecutionEventTests {
this.jobParameters = new JobParameters(jobParamMap);
JobExecution jobExecution;
jobExecution = new JobExecution(this.jobInstance, JOB_EXECUTION_ID,
- this.jobParameters, JOB_CONFIGURATION_NAME);
+ this.jobParameters, JOB_CONFIGURATION_NAME);
JobExecutionEvent jobExecutionEvent = new JobExecutionEvent(jobExecution);
assertThat(jobExecutionEvent.getJobParameters().getString("A"))
- .as("Job Parameter A was expected").isNotNull();
+ .as("Job Parameter A was expected").isNotNull();
assertThat(jobExecutionEvent.getJobParameters().getLong("B"))
- .as("Job Parameter B was expected").isNotNull();
+ .as("Job Parameter B was expected").isNotNull();
assertThat(jobExecutionEvent.getJobParameters().getDouble("C"))
- .as("Job Parameter C was expected").isNotNull();
+ .as("Job Parameter C was expected").isNotNull();
assertThat(jobExecutionEvent.getJobParameters().getDate("D"))
- .as("Job Parameter D was expected").isNotNull();
+ .as("Job Parameter D was expected").isNotNull();
assertThat(jobExecutionEvent.getJobParameters().getString("A"))
- .as("Job Parameter A value was not correct").isEqualTo("FOO");
+ .as("Job Parameter A value was not correct").isEqualTo("FOO");
assertThat(jobExecutionEvent.getJobParameters().getLong("B"))
- .as("Job Parameter B value was not correct").isEqualTo(new Long(1));
+ .as("Job Parameter B value was not correct").isEqualTo(new Long(1));
assertThat(jobExecutionEvent.getJobParameters().getDouble("C"))
- .as("Job Parameter C value was not correct").isEqualTo(new Double(1));
+ .as("Job Parameter C value was not correct").isEqualTo(new Double(1));
assertThat(jobExecutionEvent.getJobParameters().getDate("D"))
- .as("Job Parameter D value was not correct").isEqualTo(testDate);
+ .as("Job Parameter D value was not correct").isEqualTo(testDate);
}
@Test
public void testStepExecutions() {
JobExecution jobExecution;
jobExecution = new JobExecution(this.jobInstance, JOB_EXECUTION_ID,
- this.jobParameters, JOB_CONFIGURATION_NAME);
+ this.jobParameters, JOB_CONFIGURATION_NAME);
List stepsExecutions = new ArrayList<>();
stepsExecutions.add(new StepExecution("foo", jobExecution));
stepsExecutions.add(new StepExecution("bar", jobExecution));
@@ -156,15 +156,15 @@ public class JobExecutionEventTests {
JobExecutionEvent jobExecutionsEvent = new JobExecutionEvent(jobExecution);
assertThat(jobExecutionsEvent.getStepExecutions().size())
- .as("stepExecutions count is incorrect").isEqualTo(3);
+ .as("stepExecutions count is incorrect").isEqualTo(3);
Iterator iter = jobExecutionsEvent.getStepExecutions()
- .iterator();
+ .iterator();
assertThat(iter.next().getStepName()).as("foo stepExecution is not present")
- .isEqualTo("foo");
+ .isEqualTo("foo");
assertThat(iter.next().getStepName()).as("bar stepExecution is not present")
- .isEqualTo("bar");
+ .isEqualTo("bar");
assertThat(iter.next().getStepName()).as("baz stepExecution is not present")
- .isEqualTo("baz");
+ .isEqualTo("baz");
}
@Test
@@ -172,6 +172,7 @@ public class JobExecutionEventTests {
testDisabledConfiguration(null, null);
}
+
@Test
public void testDisabledJobExecutionListener() {
testDisabledConfiguration("spring.cloud.task.batch.events.job-execution.enabled",
@@ -181,9 +182,10 @@ public class JobExecutionEventTests {
@Test
public void testDisabledStepExecutionListener() {
testDisabledConfiguration("spring.cloud.task.batch.events.step-execution.enabled",
- BatchEventAutoConfiguration.STEP_EXECUTION_EVENTS_LISTENER);
+ BatchEventAutoConfiguration.STEP_EXECUTION_EVENTS_LISTENER);
}
+
@Test
public void testDisabledChunkListener() {
testDisabledConfiguration("spring.cloud.task.batch.events.chunk.enabled",
@@ -315,12 +317,11 @@ public class JobExecutionEventTests {
public void testOrderConfiguration() {
ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
- EventJobExecutionConfiguration.class,
PropertyPlaceholderAutoConfiguration.class,
- TestSupportBinderAutoConfiguration.class,
SimpleTaskAutoConfiguration.class, SingleTaskConfiguration.class))
.withUserConfiguration(
- BatchEventAutoConfiguration.JobExecutionListenerConfiguration.class)
+ BatchEventAutoConfiguration.JobExecutionListenerConfiguration.class,
+ BatchEventTestApplication.class)
.withPropertyValues("--spring.cloud.task.closecontext_enabled=false",
"--spring.main.web-environment=false",
"--spring.cloud.task.batch.events.chunk-order=5",
@@ -344,12 +345,11 @@ public class JobExecutionEventTests {
public void singleStepBatchJobSkip() {
ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
- EventJobExecutionConfiguration.class,
PropertyPlaceholderAutoConfiguration.class,
- TestSupportBinderAutoConfiguration.class,
SimpleTaskAutoConfiguration.class, SingleTaskConfiguration.class))
.withUserConfiguration(
- BatchEventAutoConfiguration.JobExecutionListenerConfiguration.class)
+ BatchEventAutoConfiguration.JobExecutionListenerConfiguration.class,
+ BatchEventTestApplication.class)
.withPropertyValues("--spring.cloud.task.closecontext_enabled=false",
"--spring.main.web-environment=false", "spring.batch.job.jobName=FOO");
applicationContextRunner.run((context) -> {
@@ -364,15 +364,14 @@ public class JobExecutionEventTests {
private void testDisabledConfiguration(String property, String disabledListener) {
String disabledPropertyArg = (property != null) ? "--" + property + "=false" : "";
ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
- .withConfiguration(AutoConfigurations.of(
- EventJobExecutionConfiguration.class,
- PropertyPlaceholderAutoConfiguration.class,
- TestSupportBinderAutoConfiguration.class,
- SimpleTaskAutoConfiguration.class, SingleTaskConfiguration.class))
- .withUserConfiguration(
- BatchEventAutoConfiguration.JobExecutionListenerConfiguration.class)
- .withPropertyValues("--spring.cloud.task.closecontext_enabled=false",
- "--spring.main.web-environment=false", disabledPropertyArg);
+ .withConfiguration(AutoConfigurations.of(
+ PropertyPlaceholderAutoConfiguration.class,
+ SimpleTaskAutoConfiguration.class, SingleTaskConfiguration.class))
+ .withUserConfiguration(
+ BatchEventAutoConfiguration.JobExecutionListenerConfiguration.class,
+ BatchEventTestApplication.class)
+ .withPropertyValues("--spring.cloud.task.closecontext_enabled=false",
+ "--spring.main.web-environment=false", disabledPropertyArg);
applicationContextRunner.run((context) -> {
boolean exceptionThrown = false;
for (String beanName : LISTENER_BEAN_NAMES) {
@@ -385,19 +384,16 @@ public class JobExecutionEventTests {
}
assertThat(exceptionThrown).as(
String.format("Did not expect %s bean in context", beanName))
- .isTrue();
+ .isTrue();
}
else {
context.getBean(beanName);
}
}
});
-
}
- @Configuration(proxyBeanMethods = false)
- public static class EventJobExecutionConfiguration {
-
+ @SpringBootApplication
+ public static class BatchEventTestApplication {
}
-
}
diff --git a/spring-cloud-task-stream/src/test/java/org/springframework/cloud/task/batch/listener/JobParameterEventTests.java b/spring-cloud-task-stream/src/test/java/org/springframework/cloud/task/batch/listener/JobParameterEventTests.java
index 8044b96a..2e3797fb 100644
--- a/spring-cloud-task-stream/src/test/java/org/springframework/cloud/task/batch/listener/JobParameterEventTests.java
+++ b/spring-cloud-task-stream/src/test/java/org/springframework/cloud/task/batch/listener/JobParameterEventTests.java
@@ -23,8 +23,6 @@ import org.junit.jupiter.api.Test;
import org.springframework.batch.core.JobParameter;
import org.springframework.cloud.task.batch.listener.support.JobParameterEvent;
-import static junit.framework.TestCase.assertFalse;
-import static junit.framework.TestCase.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
/**
@@ -37,7 +35,7 @@ public class JobParameterEventTests {
JobParameterEvent jobParameterEvent = new JobParameterEvent();
assertThat(jobParameterEvent.getValue()).isNull();
assertThat(jobParameterEvent.getType()).isNull();
- assertFalse(jobParameterEvent.isIdentifying());
+ assertThat(jobParameterEvent.isIdentifying()).isFalse();
assertThat(jobParameterEvent).isEqualTo(new JobParameterEvent());
}
@@ -50,15 +48,15 @@ public class JobParameterEventTests {
assertThat(jobParameterEvent.getValue()).isEqualTo(EXPECTED_VALUE);
assertThat(jobParameterEvent.getType())
.isEqualTo(JobParameterEvent.ParameterType.STRING);
- assertTrue(jobParameterEvent.isIdentifying());
+ assertThat(jobParameterEvent.isIdentifying()).isTrue();
jobParameter = new JobParameter(EXPECTED_DATE_VALUE, true);
jobParameterEvent = new JobParameterEvent(jobParameter);
assertThat(jobParameterEvent.getValue()).isEqualTo(EXPECTED_DATE_VALUE);
assertThat(jobParameterEvent.getType())
.isEqualTo(JobParameterEvent.ParameterType.DATE);
- assertTrue(jobParameterEvent.isIdentifying());
- assertTrue(new JobParameterEvent(jobParameter).equals(jobParameterEvent));
+ assertThat(jobParameterEvent.isIdentifying()).isTrue();
+ assertThat(new JobParameterEvent(jobParameter).equals(jobParameterEvent)).isTrue();
}
@Test
@@ -68,9 +66,9 @@ public class JobParameterEventTests {
JobParameterEvent jobParameterEvent = new JobParameterEvent(jobParameter);
JobParameterEvent anotherJobParameterEvent = new JobParameterEvent(jobParameter);
- assertTrue(jobParameterEvent.equals(jobParameterEvent));
- assertFalse(jobParameterEvent.equals("nope"));
- assertTrue(jobParameterEvent.equals(anotherJobParameterEvent));
+ assertThat(jobParameterEvent.equals(jobParameterEvent)).isTrue();
+ assertThat(jobParameterEvent.equals("nope")).isFalse();
+ assertThat(jobParameterEvent.equals(anotherJobParameterEvent)).isTrue();
}
@Test
diff --git a/spring-cloud-task-stream/src/test/java/org/springframework/cloud/task/batch/listener/JobParametersEventTests.java b/spring-cloud-task-stream/src/test/java/org/springframework/cloud/task/batch/listener/JobParametersEventTests.java
index df0f9f75..5b00fcac 100644
--- a/spring-cloud-task-stream/src/test/java/org/springframework/cloud/task/batch/listener/JobParametersEventTests.java
+++ b/spring-cloud-task-stream/src/test/java/org/springframework/cloud/task/batch/listener/JobParametersEventTests.java
@@ -26,8 +26,6 @@ import org.junit.jupiter.api.Test;
import org.springframework.batch.core.JobParameter;
import org.springframework.cloud.task.batch.listener.support.JobParametersEvent;
-import static junit.framework.TestCase.assertFalse;
-import static junit.framework.TestCase.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
/**
@@ -55,7 +53,7 @@ public class JobParametersEventTests {
public void testDefaultConstructor() {
JobParametersEvent jobParametersEvent = new JobParametersEvent();
assertThat(jobParametersEvent.getParameters().size()).isEqualTo(0);
- assertTrue(jobParametersEvent.isEmpty());
+ assertThat(jobParametersEvent.isEmpty()).isTrue();
}
@Test
@@ -74,10 +72,10 @@ public class JobParametersEventTests {
@Test
public void testEquals() {
- assertTrue(getPopulatedParametersEvent().equals(getPopulatedParametersEvent()));
+ assertThat(getPopulatedParametersEvent().equals(getPopulatedParametersEvent())).isTrue();
JobParametersEvent jobParametersEvent = getPopulatedParametersEvent();
- assertFalse(jobParametersEvent.equals("FOO"));
- assertTrue(jobParametersEvent.equals(jobParametersEvent));
+ assertThat(jobParametersEvent.equals("FOO")).isFalse();
+ assertThat(jobParametersEvent.equals(jobParametersEvent)).isTrue();
}
@Test
diff --git a/spring-cloud-task-stream/src/test/java/org/springframework/cloud/task/launcher/TaskLaunchConfigurationExistingTests.java b/spring-cloud-task-stream/src/test/java/org/springframework/cloud/task/launcher/TaskLaunchConfigurationExistingTests.java
index 4baa6e8a..210d709b 100644
--- a/spring-cloud-task-stream/src/test/java/org/springframework/cloud/task/launcher/TaskLaunchConfigurationExistingTests.java
+++ b/spring-cloud-task-stream/src/test/java/org/springframework/cloud/task/launcher/TaskLaunchConfigurationExistingTests.java
@@ -19,14 +19,13 @@ package org.springframework.cloud.task.launcher;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.WebApplicationType;
+import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.deployer.spi.local.LocalDeployerProperties;
import org.springframework.cloud.deployer.spi.local.LocalTaskLauncher;
import org.springframework.cloud.deployer.spi.task.TaskLauncher;
-import org.springframework.context.ApplicationContext;
+import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.assertj.core.api.Assertions.assertThat;
@@ -37,25 +36,23 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Glenn Renfro
*/
@ExtendWith(SpringExtension.class)
-@SpringBootTest(classes = {
- TaskLaunchConfigurationExistingTests.TestTaskDeployerConfiguration.class })
public class TaskLaunchConfigurationExistingTests {
private static TaskLauncher testTaskLauncher;
- @Autowired
- private ApplicationContext context;
-
@Test
public void testTaskLauncher() {
- LocalTaskLauncher taskLauncher = this.context.getBean(LocalTaskLauncher.class);
- assertThat(testTaskLauncher).isNotNull();
- assertThat(taskLauncher).isNotNull();
- assertThat(taskLauncher).isEqualTo(testTaskLauncher);
+ try (ConfigurableApplicationContext context = new SpringApplicationBuilder(
+ TaskLaunchConfigurationExistingTests.TestTaskDeployerConfiguration.class).web(WebApplicationType.NONE).run(
+ "--spring.jmx.enabled=false")) {
+ LocalTaskLauncher taskLauncher = context.getBean(LocalTaskLauncher.class);
+ assertThat(testTaskLauncher).isNotNull();
+ assertThat(taskLauncher).isNotNull();
+ assertThat(taskLauncher).isEqualTo(testTaskLauncher);
+ }
}
- @Configuration(proxyBeanMethods = false)
- protected static class TestTaskDeployerConfiguration {
+ private static class TestTaskDeployerConfiguration {
@Bean
public TaskLauncher taskLauncher() {
diff --git a/spring-cloud-task-stream/src/test/java/org/springframework/cloud/task/launcher/TaskLauncherFunctionTests.java b/spring-cloud-task-stream/src/test/java/org/springframework/cloud/task/launcher/TaskLauncherFunctionTests.java
new file mode 100644
index 00000000..8575ed94
--- /dev/null
+++ b/spring-cloud-task-stream/src/test/java/org/springframework/cloud/task/launcher/TaskLauncherFunctionTests.java
@@ -0,0 +1,213 @@
+/*
+ * Copyright 2021-2021 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.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.cloud.task.launcher;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.junit.jupiter.api.Test;
+
+import org.springframework.boot.WebApplicationType;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.builder.SpringApplicationBuilder;
+import org.springframework.cloud.deployer.spi.task.LaunchState;
+import org.springframework.cloud.stream.binder.test.InputDestination;
+import org.springframework.cloud.stream.binder.test.TestChannelBinderConfiguration;
+import org.springframework.cloud.task.launcher.configuration.TaskConfiguration;
+import org.springframework.context.ConfigurableApplicationContext;
+import org.springframework.context.annotation.Import;
+import org.springframework.messaging.support.GenericMessage;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class TaskLauncherFunctionTests {
+
+ private final static String TASK_NAME_PREFIX = "Task-";
+
+ private final static String APP_NAME = "MY_APP_NAME";
+
+ private final static String PARAM1 = "FOO";
+
+ private final static String PARAM2 = "BAR";
+
+ private final static String VALID_URL = "maven://org.springframework.cloud.task.app:"
+ + "timestamp-task:jar:1.0.1.RELEASE";
+
+ private final static String INVALID_URL = "maven://not.real.group:"
+ + "invalid:jar:1.0.0.BUILD-SNAPSHOT";
+
+ private final static String DEFAULT_STATUS = "test_status";
+
+ private Map properties;
+
+ @Test
+ public void testProcessorFromFunction() {
+ try (ConfigurableApplicationContext context = new SpringApplicationBuilder(
+ TestChannelBinderConfiguration.getCompleteConfiguration(
+ TaskLauncherSinkTestApplication.class)).web(WebApplicationType.NONE).run(
+ "--spring.jmx.enabled=false")) {
+
+ InputDestination source = context.getBean(InputDestination.class);
+ TaskLaunchRequest request = new TaskLaunchRequest(VALID_URL, Collections.emptyList(),
+ Collections.emptyMap(), null, "TESTAPP1");
+ GenericMessage message = new GenericMessage<>(request);
+ source.send(message);
+ TaskConfiguration.TestTaskLauncher target = context.getBean(TaskConfiguration.TestTaskLauncher.class);
+ assertThat(target.status(DEFAULT_STATUS).getState())
+ .isEqualTo(LaunchState.complete);
+ }
+ }
+
+ @Test
+ public void testSuccessWithParams() throws Exception {
+ List commandLineArgs = new ArrayList<>();
+ commandLineArgs.add(PARAM1);
+ commandLineArgs.add(PARAM2);
+ try (ConfigurableApplicationContext context = new SpringApplicationBuilder(
+ TestChannelBinderConfiguration.getCompleteConfiguration(
+ TaskLauncherSinkTestApplication.class)).web(WebApplicationType.NONE).run(
+ "--spring.jmx.enabled=false")) {
+ TaskConfiguration.TestTaskLauncher testTaskLauncher = launchTaskString(VALID_URL,
+ commandLineArgs, null, context);
+ verifySuccessWithParams(testTaskLauncher);
+
+ testTaskLauncher = launchTaskByteArray(VALID_URL, commandLineArgs, null, context);
+ verifySuccessWithParams(testTaskLauncher);
+
+ testTaskLauncher = launchTaskTaskLaunchRequest(VALID_URL, commandLineArgs, null, context);
+ verifySuccessWithParams(testTaskLauncher);
+ }
+ }
+
+ @Test
+ public void testSuccessWithAppName() throws Exception {
+ try (ConfigurableApplicationContext context = new SpringApplicationBuilder(
+ TestChannelBinderConfiguration.getCompleteConfiguration(
+ TaskLauncherSinkTestApplication.class)).web(WebApplicationType.NONE).run(
+ "--spring.jmx.enabled=false")) {
+ TaskConfiguration.TestTaskLauncher testTaskLauncher = launchTaskString(VALID_URL,
+ null, APP_NAME, context);
+ verifySuccessWithAppName(testTaskLauncher);
+
+ testTaskLauncher = launchTaskByteArray(VALID_URL, null, APP_NAME, context);
+ verifySuccessWithAppName(testTaskLauncher);
+
+ testTaskLauncher = launchTaskTaskLaunchRequest(VALID_URL, null, APP_NAME, context);
+ verifySuccessWithAppName(testTaskLauncher);
+ }
+ }
+
+ @Test
+ public void testInvalidJar() throws Exception {
+ try (ConfigurableApplicationContext context = new SpringApplicationBuilder(
+ TestChannelBinderConfiguration.getCompleteConfiguration(
+ TaskLauncherSinkTestApplication.class)).web(WebApplicationType.NONE).run(
+ "--spring.jmx.enabled=false")) {
+ TaskConfiguration.TestTaskLauncher testTaskLauncher = launchTaskTaskLaunchRequest(
+ INVALID_URL, null, APP_NAME, context);
+ verifySuccessWithAppName(testTaskLauncher);
+ }
+ }
+
+ @Test
+ public void testNoRun() {
+ try (ConfigurableApplicationContext context = new SpringApplicationBuilder(
+ TestChannelBinderConfiguration.getCompleteConfiguration(
+ TaskLauncherSinkTestApplication.class)).web(WebApplicationType.NONE).run(
+ "--spring.jmx.enabled=false")) {
+ TaskConfiguration.TestTaskLauncher testTaskLauncher = context
+ .getBean(TaskConfiguration.TestTaskLauncher.class);
+ assertThat(testTaskLauncher.status(DEFAULT_STATUS).getState())
+ .isEqualTo(LaunchState.unknown);
+ }
+ }
+
+ private void verifySuccessWithAppName(
+ TaskConfiguration.TestTaskLauncher testTaskLauncher) {
+ assertThat(testTaskLauncher.status(DEFAULT_STATUS).getState())
+ .isEqualTo(LaunchState.complete);
+ assertThat(testTaskLauncher.getCommandlineArguments().size()).isEqualTo(0);
+ assertThat(testTaskLauncher.getApplicationName()).isEqualTo(APP_NAME);
+ }
+
+ private String getStringTaskLaunchRequest(String artifactURL,
+ List commandLineArgs, String applicationName) throws Exception {
+ TaskLaunchRequest request = new TaskLaunchRequest(artifactURL, commandLineArgs,
+ this.properties, null, applicationName);
+ ObjectMapper mapper = new ObjectMapper();
+ return mapper.writeValueAsString(request);
+ }
+
+ private void verifySuccessWithParams(
+ TaskConfiguration.TestTaskLauncher testTaskLauncher) {
+ assertThat(testTaskLauncher.status(DEFAULT_STATUS).getState())
+ .isEqualTo(LaunchState.complete);
+ assertThat(testTaskLauncher.getCommandlineArguments().size()).isEqualTo(2);
+ assertThat(testTaskLauncher.getCommandlineArguments().get(0)).isEqualTo(PARAM1);
+ assertThat(testTaskLauncher.getCommandlineArguments().get(1)).isEqualTo(PARAM2);
+ assertThat(testTaskLauncher.getApplicationName().startsWith(TASK_NAME_PREFIX))
+ .isTrue();
+ }
+
+ private TaskConfiguration.TestTaskLauncher launchTaskString(String artifactURL,
+ List commandLineArgs, String applicationName,
+ ConfigurableApplicationContext context) throws Exception {
+ TaskConfiguration.TestTaskLauncher testTaskLauncher = context
+ .getBean(TaskConfiguration.TestTaskLauncher.class);
+ String stringRequest = getStringTaskLaunchRequest(artifactURL, commandLineArgs,
+ applicationName);
+ GenericMessage message = new GenericMessage<>(stringRequest);
+ InputDestination source = context.getBean(InputDestination.class);
+ source.send(message);
+ return testTaskLauncher;
+ }
+
+ private TaskConfiguration.TestTaskLauncher launchTaskByteArray(String artifactURL,
+ List commandLineArgs, String applicationName,
+ ConfigurableApplicationContext context) throws Exception {
+ TaskConfiguration.TestTaskLauncher testTaskLauncher = context
+ .getBean(TaskConfiguration.TestTaskLauncher.class);
+ String stringRequest = getStringTaskLaunchRequest(artifactURL, commandLineArgs,
+ applicationName);
+ GenericMessage message = new GenericMessage<>(stringRequest.getBytes());
+ InputDestination source = context.getBean(InputDestination.class);
+ source.send(message);
+ return testTaskLauncher;
+ }
+
+ private TaskConfiguration.TestTaskLauncher launchTaskTaskLaunchRequest(
+ String artifactURL, List commandLineArgs, String applicationName,
+ ConfigurableApplicationContext context)
+ throws Exception {
+ TaskConfiguration.TestTaskLauncher testTaskLauncher = context
+ .getBean(TaskConfiguration.TestTaskLauncher.class);
+ TaskLaunchRequest request = new TaskLaunchRequest(artifactURL, commandLineArgs,
+ this.properties, null, applicationName);
+ GenericMessage message = new GenericMessage<>(request);
+ InputDestination source = context.getBean(InputDestination.class);
+ source.send(message);
+ return testTaskLauncher;
+ }
+
+ @SpringBootApplication
+ @Import({TaskLauncherSink.class})
+ public static class TaskLauncherSinkTestApplication {
+ }
+}
diff --git a/spring-cloud-task-stream/src/test/java/org/springframework/cloud/task/launcher/TaskLauncherSinkTests.java b/spring-cloud-task-stream/src/test/java/org/springframework/cloud/task/launcher/TaskLauncherSinkTests.java
deleted file mode 100644
index 3f2b28c5..00000000
--- a/spring-cloud-task-stream/src/test/java/org/springframework/cloud/task/launcher/TaskLauncherSinkTests.java
+++ /dev/null
@@ -1,206 +0,0 @@
-/*
- * Copyright 2016-2019 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.
- * You may obtain a copy of the License at
- *
- * https://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.cloud.task.launcher;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import com.fasterxml.jackson.databind.ObjectMapper;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.ExtendWith;
-
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.test.context.SpringBootTest;
-import org.springframework.cloud.deployer.spi.task.LaunchState;
-import org.springframework.cloud.stream.messaging.Sink;
-import org.springframework.cloud.task.launcher.app.TaskLauncherSinkApplication;
-import org.springframework.cloud.task.launcher.configuration.TaskConfiguration;
-import org.springframework.context.ApplicationContext;
-import org.springframework.messaging.support.GenericMessage;
-import org.springframework.test.context.junit.jupiter.SpringExtension;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-@ExtendWith(SpringExtension.class)
-@SpringBootTest(classes = { TaskLauncherSinkApplication.class, TaskConfiguration.class })
-public class TaskLauncherSinkTests {
-
- private final static String TASK_NAME_PREFIX = "Task-";
-
- private final static String APP_NAME = "MY_APP_NAME";
-
- private final static String PARAM1 = "FOO";
-
- private final static String PARAM2 = "BAR";
-
- private final static String VALID_URL = "maven://org.springframework.cloud.task.app:"
- + "timestamp-task:jar:1.0.1.RELEASE";
-
- private final static String INVALID_URL = "maven://not.real.group:"
- + "invalid:jar:1.0.0.BUILD-SNAPSHOT";
-
- private final static String DEFAULT_STATUS = "test_status";
-
- private Map properties;
-
- @Autowired
- private ApplicationContext context;
-
- @Autowired
- private Sink sink;
-
- @BeforeEach
- public void setup() {
- this.properties = new HashMap<>();
- this.properties.put("server.port", "0");
- }
-
- @Test
- public void testSuccessWithParams() throws Exception {
- List commandLineArgs = new ArrayList<>();
- commandLineArgs.add(PARAM1);
- commandLineArgs.add(PARAM2);
-
- TaskConfiguration.TestTaskLauncher testTaskLauncher = launchTaskString(VALID_URL,
- commandLineArgs, null);
- verifySuccessWithParams(testTaskLauncher);
-
- testTaskLauncher = launchTaskByteArray(VALID_URL, commandLineArgs, null);
- verifySuccessWithParams(testTaskLauncher);
-
- testTaskLauncher = launchTaskTaskLaunchRequest(VALID_URL, commandLineArgs, null);
- verifySuccessWithParams(testTaskLauncher);
- }
-
- private void verifySuccessWithParams(
- TaskConfiguration.TestTaskLauncher testTaskLauncher) {
- assertThat(testTaskLauncher.status(DEFAULT_STATUS).getState())
- .isEqualTo(LaunchState.complete);
- assertThat(testTaskLauncher.getCommandlineArguments().size()).isEqualTo(2);
- assertThat(testTaskLauncher.getCommandlineArguments().get(0)).isEqualTo(PARAM1);
- assertThat(testTaskLauncher.getCommandlineArguments().get(1)).isEqualTo(PARAM2);
- assertThat(testTaskLauncher.getApplicationName().startsWith(TASK_NAME_PREFIX))
- .isTrue();
- }
-
- @Test
- public void testSuccessWithAppName() throws Exception {
- TaskConfiguration.TestTaskLauncher testTaskLauncher = launchTaskString(VALID_URL,
- null, APP_NAME);
- verifySuccessWithAppName(testTaskLauncher);
-
- testTaskLauncher = launchTaskByteArray(VALID_URL, null, APP_NAME);
- verifySuccessWithAppName(testTaskLauncher);
-
- testTaskLauncher = launchTaskTaskLaunchRequest(VALID_URL, null, APP_NAME);
- verifySuccessWithAppName(testTaskLauncher);
- }
-
- @Test
- public void testInvalidJar() throws Exception {
- TaskConfiguration.TestTaskLauncher testTaskLauncher = launchTaskTaskLaunchRequest(
- INVALID_URL, null, APP_NAME);
- verifySuccessWithAppName(testTaskLauncher);
- }
-
- private void verifySuccessWithAppName(
- TaskConfiguration.TestTaskLauncher testTaskLauncher) {
- assertThat(testTaskLauncher.status(DEFAULT_STATUS).getState())
- .isEqualTo(LaunchState.complete);
- assertThat(testTaskLauncher.getCommandlineArguments().size()).isEqualTo(0);
- assertThat(testTaskLauncher.getApplicationName()).isEqualTo(APP_NAME);
- }
-
- @Test
- public void testSuccessNoParams() throws Exception {
- TaskConfiguration.TestTaskLauncher testTaskLauncher = launchTaskString(VALID_URL,
- null, null);
- verifySuccessWithNoParams(testTaskLauncher);
-
- testTaskLauncher = launchTaskByteArray(VALID_URL, null, null);
- verifySuccessWithNoParams(testTaskLauncher);
-
- testTaskLauncher = launchTaskTaskLaunchRequest(VALID_URL, null, null);
- verifySuccessWithNoParams(testTaskLauncher);
- }
-
- private void verifySuccessWithNoParams(
- TaskConfiguration.TestTaskLauncher testTaskLauncher) {
- assertThat(testTaskLauncher.status(DEFAULT_STATUS).getState())
- .isEqualTo(LaunchState.complete);
- assertThat(testTaskLauncher.getCommandlineArguments().size()).isEqualTo(0);
- assertThat(testTaskLauncher.getApplicationName().startsWith(TASK_NAME_PREFIX))
- .isTrue();
- }
-
- @Test
- public void testNoRun() {
- TaskConfiguration.TestTaskLauncher testTaskLauncher = this.context
- .getBean(TaskConfiguration.TestTaskLauncher.class);
- assertThat(testTaskLauncher.status(DEFAULT_STATUS).getState())
- .isEqualTo(LaunchState.unknown);
- }
-
- private TaskConfiguration.TestTaskLauncher launchTaskString(String artifactURL,
- List commandLineArgs, String applicationName) throws Exception {
- TaskConfiguration.TestTaskLauncher testTaskLauncher = this.context
- .getBean(TaskConfiguration.TestTaskLauncher.class);
- String stringRequest = getStringTaskLaunchRequest(artifactURL, commandLineArgs,
- applicationName);
- GenericMessage message = new GenericMessage<>(stringRequest);
-
- this.sink.input().send(message);
- return testTaskLauncher;
- }
-
- private TaskConfiguration.TestTaskLauncher launchTaskByteArray(String artifactURL,
- List commandLineArgs, String applicationName) throws Exception {
- TaskConfiguration.TestTaskLauncher testTaskLauncher = this.context
- .getBean(TaskConfiguration.TestTaskLauncher.class);
- String stringRequest = getStringTaskLaunchRequest(artifactURL, commandLineArgs,
- applicationName);
- GenericMessage message = new GenericMessage<>(stringRequest.getBytes());
-
- this.sink.input().send(message);
- return testTaskLauncher;
- }
-
- private String getStringTaskLaunchRequest(String artifactURL,
- List commandLineArgs, String applicationName) throws Exception {
- TaskLaunchRequest request = new TaskLaunchRequest(artifactURL, commandLineArgs,
- this.properties, null, applicationName);
- ObjectMapper mapper = new ObjectMapper();
- return mapper.writeValueAsString(request);
- }
-
- private TaskConfiguration.TestTaskLauncher launchTaskTaskLaunchRequest(
- String artifactURL, List commandLineArgs, String applicationName)
- throws Exception {
- TaskConfiguration.TestTaskLauncher testTaskLauncher = this.context
- .getBean(TaskConfiguration.TestTaskLauncher.class);
- TaskLaunchRequest request = new TaskLaunchRequest(artifactURL, commandLineArgs,
- this.properties, null, applicationName);
- GenericMessage message = new GenericMessage<>(request);
-
- this.sink.input().send(message);
- return testTaskLauncher;
- }
-
-}
diff --git a/spring-cloud-task-stream/src/test/java/org/springframework/cloud/task/listener/TaskEventTests.java b/spring-cloud-task-stream/src/test/java/org/springframework/cloud/task/listener/TaskEventTests.java
index 6133df9f..bcbd180e 100644
--- a/spring-cloud-task-stream/src/test/java/org/springframework/cloud/task/listener/TaskEventTests.java
+++ b/spring-cloud-task-stream/src/test/java/org/springframework/cloud/task/listener/TaskEventTests.java
@@ -18,19 +18,12 @@ package org.springframework.cloud.task.listener;
import org.junit.jupiter.api.Test;
-import org.springframework.boot.autoconfigure.AutoConfigurations;
-import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
-import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
-import org.springframework.boot.test.context.runner.ApplicationContextRunner;
-import org.springframework.cloud.stream.config.BindingServiceConfiguration;
-import org.springframework.cloud.stream.test.binder.TestSupportBinderAutoConfiguration;
+import org.springframework.boot.WebApplicationType;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.builder.SpringApplicationBuilder;
+import org.springframework.cloud.stream.binder.test.TestChannelBinderConfiguration;
import org.springframework.cloud.task.configuration.EnableTask;
-import org.springframework.cloud.task.configuration.SimpleTaskAutoConfiguration;
-import org.springframework.cloud.task.configuration.SingleTaskConfiguration;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-import org.springframework.integration.annotation.BridgeFrom;
-import org.springframework.integration.channel.NullChannel;
+import org.springframework.context.ConfigurableApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
@@ -43,35 +36,16 @@ public class TaskEventTests {
@Test
public void testDefaultConfiguration() {
- ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
- .withConfiguration(AutoConfigurations.of(
- EmbeddedDataSourceConfiguration.class,
- TaskEventAutoConfiguration.class,
- PropertyPlaceholderAutoConfiguration.class,
- TestSupportBinderAutoConfiguration.class,
- SimpleTaskAutoConfiguration.class, SingleTaskConfiguration.class,
- BindingServiceConfiguration.class))
- .withUserConfiguration(TaskEventsConfiguration.class)
- .withPropertyValues("spring.cloud.task.closecontext_enabled=false",
- "spring.main.web-environment=false");
- applicationContextRunner.run((context) -> {
- assertThat(context.getBean("taskEventListener")).isNotNull();
- assertThat(
- context.getBean(TaskEventAutoConfiguration.TaskEventChannels.class))
- .isNotNull();
- });
+ ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder()
+ .sources(TestChannelBinderConfiguration
+ .getCompleteConfiguration(TaskEventsApplication.class)).web(WebApplicationType.NONE).build()
+ .run();
+ assertThat(applicationContext.getBean("taskEventEmitter")).isNotNull();
}
@EnableTask
- @Configuration
- public static class TaskEventsConfiguration {
-
- @Bean
- @BridgeFrom(TaskEventAutoConfiguration.TaskEventChannels.TASK_EVENTS)
- public NullChannel testEmptyChannel() {
- return new NullChannel();
- }
-
+ @SpringBootApplication
+ public static class TaskEventsApplication {
}
}
diff --git a/spring-cloud-task-stream/src/test/resources/application.properties b/spring-cloud-task-stream/src/test/resources/application.properties
index fce9ea93..00470d5f 100644
--- a/spring-cloud-task-stream/src/test/resources/application.properties
+++ b/spring-cloud-task-stream/src/test/resources/application.properties
@@ -1,2 +1,6 @@
maven.remoteRepositories.springRepo.url=https://repo.spring.io/libs-snapshot
logging.level.org.springframework.cloud.task.batch.listener=DEBUG
+spring.cloud.stream.function.definition=taskLauncherSink
+spring.cloud.stream.function.bindings.taskLauncherSink-in-0=input
+spring.cloud.stream.bindings.input.group=taskLauncherSink
+spring.cloud.stream.bindings.input.destination=taskLauncherSinkExchange