Skip to content

Commit 53f620e

Browse files
committed
Fix listener API allowing invalid listeners
Before listeners could be created without annotations which then would be ignored during runtime. Now listeners without annotations will throw an IllegalArgumentException Resolves #4808 Signed-off-by: Sacha Leemann <[email protected]>
1 parent 639d64f commit 53f620e

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/job/builder/JobBuilderHelper.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ public B listener(Object listener) {
161161
factory.setDelegate(listener);
162162
properties.addJobExecutionListener((JobExecutionListener) factory.getObject());
163163
}
164+
else {
165+
throw new IllegalArgumentException("Missing @BeforeJob or @AfterJob annotations on Listener.");
166+
}
164167

165168
@SuppressWarnings("unchecked")
166169
B result = (B) this;

spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/StepBuilderHelper.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ public B listener(Object listener) {
125125
factory.setDelegate(listener);
126126
properties.addStepExecutionListener((StepExecutionListener) factory.getObject());
127127
}
128+
else {
129+
throw new IllegalArgumentException("Missing @BeforeStep or @AfterStep annotations on Listener.");
130+
}
128131

129132
return self();
130133
}

spring-batch-core/src/test/java/org/springframework/batch/core/job/builder/JobBuilderTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import org.junit.jupiter.api.Test;
2121

22+
import org.mockito.Mockito;
2223
import org.springframework.batch.core.ExitStatus;
2324
import org.springframework.batch.core.job.Job;
2425
import org.springframework.batch.core.job.JobExecution;
@@ -40,6 +41,7 @@
4041
import org.springframework.transaction.PlatformTransactionManager;
4142

4243
import static org.junit.jupiter.api.Assertions.assertEquals;
44+
import static org.junit.jupiter.api.Assertions.assertThrows;
4345

4446
/**
4547
* @author Mahmoud Ben Hassine
@@ -65,6 +67,16 @@ void testListeners() throws Exception {
6567

6668
}
6769

70+
@Test
71+
void testInvalidListener() {
72+
assertThrows(IllegalArgumentException.class,
73+
() -> new JobBuilder("job", Mockito.mock()).listener(new InvalidListener())
74+
.start(new StepBuilder("step", Mockito.mock())
75+
.tasklet((contribution, chunkContext) -> RepeatStatus.FINISHED, Mockito.mock())
76+
.build())
77+
.build());
78+
}
79+
6880
@Configuration
6981
@EnableBatchProcessing
7082
static class MyJobConfiguration {
@@ -130,4 +142,14 @@ public void afterJob(JobExecution jobExecution) {
130142

131143
}
132144

145+
public static class InvalidListener {
146+
147+
public void beforeStep() {
148+
}
149+
150+
public void afterStep() {
151+
}
152+
153+
}
154+
133155
}

spring-batch-core/src/test/java/org/springframework/batch/core/step/builder/StepBuilderTests.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import org.springframework.transaction.PlatformTransactionManager;
6161

6262
import static org.junit.jupiter.api.Assertions.assertEquals;
63+
import static org.junit.jupiter.api.Assertions.assertThrows;
6364

6465
/**
6566
* @author Dave Syer
@@ -117,6 +118,13 @@ void testListeners() throws Exception {
117118
assertEquals(1, AnnotationBasedStepExecutionListener.afterChunkCount);
118119
}
119120

121+
@Test
122+
void testMissingAnnotationsForListeners() {
123+
assertThrows(IllegalArgumentException.class,
124+
() -> new StepBuilder("step", jobRepository).listener(new InvalidListener())
125+
.tasklet((contribution, chunkContext) -> null, transactionManager));
126+
}
127+
120128
@Test
121129
void testAnnotationBasedChunkListenerForTaskletStep() throws Exception {
122130
TaskletStepBuilder builder = new StepBuilder("step", jobRepository)
@@ -465,4 +473,14 @@ public void afterChunkError() {
465473

466474
}
467475

476+
public static class InvalidListener {
477+
478+
public void beforeStep() {
479+
}
480+
481+
public void afterStep() {
482+
}
483+
484+
}
485+
468486
}

0 commit comments

Comments
 (0)