|
1 | 1 | /*
|
2 |
| - * Copyright 2006-2023 the original author or authors. |
| 2 | + * Copyright 2006-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
@@ -328,6 +328,87 @@ public void afterChunkError(ChunkContext context) {
|
328 | 328 | assertTrue(writeListener.trail.startsWith("1234"), "Listener order not as expected: " + writeListener.trail);
|
329 | 329 | }
|
330 | 330 |
|
| 331 | + @Test |
| 332 | + void testChunkListenersThrowException() throws Exception { |
| 333 | + String[] items = new String[] { "1", "2", "3", "4", "5", "6", "7" }; |
| 334 | + int commitInterval = 3; |
| 335 | + |
| 336 | + SimpleStepFactoryBean<String, String> factory = getStepFactory(items); |
| 337 | + class AssertingWriteListener extends StepListenerSupport<Object, Object> { |
| 338 | + |
| 339 | + String trail = ""; |
| 340 | + |
| 341 | + @Override |
| 342 | + public void beforeWrite(Chunk<?> chunk) { |
| 343 | + trail = trail + "2"; |
| 344 | + } |
| 345 | + |
| 346 | + @Override |
| 347 | + public void afterWrite(Chunk<?> items) { |
| 348 | + trail = trail + "3"; |
| 349 | + } |
| 350 | + |
| 351 | + } |
| 352 | + class CountingChunkListener implements ChunkListener { |
| 353 | + |
| 354 | + int beforeCount = 0; |
| 355 | + |
| 356 | + int afterCount = 0; |
| 357 | + |
| 358 | + int failedCount = 0; |
| 359 | + |
| 360 | + private final AssertingWriteListener writeListener; |
| 361 | + |
| 362 | + public CountingChunkListener(AssertingWriteListener writeListener) { |
| 363 | + super(); |
| 364 | + this.writeListener = writeListener; |
| 365 | + } |
| 366 | + |
| 367 | + @Override |
| 368 | + public void afterChunk(ChunkContext context) { |
| 369 | + writeListener.trail = writeListener.trail + "4"; |
| 370 | + afterCount++; |
| 371 | + throw new RuntimeException("Step will be terminated when ChunkListener throws exceptions."); |
| 372 | + } |
| 373 | + |
| 374 | + @Override |
| 375 | + public void beforeChunk(ChunkContext context) { |
| 376 | + writeListener.trail = writeListener.trail + "1"; |
| 377 | + beforeCount++; |
| 378 | + throw new RuntimeException("Step will be terminated when ChunkListener throws exceptions."); |
| 379 | + } |
| 380 | + |
| 381 | + @Override |
| 382 | + public void afterChunkError(ChunkContext context) { |
| 383 | + writeListener.trail = writeListener.trail + "5"; |
| 384 | + failedCount++; |
| 385 | + throw new RuntimeException("Step will be terminated when ChunkListener throws exceptions."); |
| 386 | + } |
| 387 | + |
| 388 | + } |
| 389 | + AssertingWriteListener writeListener = new AssertingWriteListener(); |
| 390 | + CountingChunkListener chunkListener = new CountingChunkListener(writeListener); |
| 391 | + factory.setListeners(new StepListener[] { chunkListener, writeListener }); |
| 392 | + factory.setCommitInterval(commitInterval); |
| 393 | + |
| 394 | + AbstractStep step = (AbstractStep) factory.getObject(); |
| 395 | + |
| 396 | + job.setSteps(Collections.singletonList((Step) step)); |
| 397 | + |
| 398 | + JobExecution jobExecution = repository.createJobExecution(job.getName(), new JobParameters()); |
| 399 | + job.execute(jobExecution); |
| 400 | + |
| 401 | + assertEquals(BatchStatus.FAILED, jobExecution.getStatus()); |
| 402 | + assertEquals("1", reader.read()); |
| 403 | + assertEquals(0, written.size()); |
| 404 | + |
| 405 | + assertEquals(0, chunkListener.afterCount); |
| 406 | + assertEquals(1, chunkListener.beforeCount); |
| 407 | + assertEquals(1, chunkListener.failedCount); |
| 408 | + assertEquals("15", writeListener.trail); |
| 409 | + assertTrue(writeListener.trail.startsWith("15"), "Listener order not as expected: " + writeListener.trail); |
| 410 | + } |
| 411 | + |
331 | 412 | /*
|
332 | 413 | * Commit interval specified is not allowed to be zero or negative.
|
333 | 414 | */
|
|
0 commit comments