|
1 | 1 | /*
|
2 |
| - * Copyright 2006-2007 the original author or authors. |
| 2 | + * Copyright 2006-2018 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.
|
|
16 | 16 | package org.springframework.batch.core.listener;
|
17 | 17 |
|
18 | 18 | import static org.junit.Assert.assertEquals;
|
| 19 | +import static org.junit.Assert.assertTrue; |
19 | 20 | import static org.junit.Assert.fail;
|
20 | 21 |
|
21 | 22 | import java.util.Arrays;
|
|
26 | 27 | import org.springframework.batch.core.ExitStatus;
|
27 | 28 | import org.springframework.batch.core.JobExecution;
|
28 | 29 | import org.springframework.batch.core.StepExecution;
|
| 30 | +import org.springframework.batch.core.StepListener; |
| 31 | +import org.springframework.batch.core.annotation.AfterChunk; |
| 32 | +import org.springframework.batch.core.annotation.AfterProcess; |
| 33 | +import org.springframework.batch.core.annotation.AfterRead; |
| 34 | +import org.springframework.batch.core.annotation.AfterWrite; |
| 35 | +import org.springframework.batch.core.annotation.BeforeChunk; |
| 36 | +import org.springframework.batch.core.annotation.BeforeProcess; |
| 37 | +import org.springframework.batch.core.annotation.BeforeRead; |
| 38 | +import org.springframework.batch.core.annotation.BeforeWrite; |
29 | 39 | import org.springframework.batch.core.scope.context.ChunkContext;
|
30 | 40 |
|
31 | 41 | /**
|
32 | 42 | * @author Dave Syer
|
| 43 | + * @author Mahmoud Ben Hassine |
33 | 44 | *
|
34 | 45 | */
|
35 | 46 | public class MulticasterBatchListenerTests {
|
@@ -512,6 +523,188 @@ public void onSkipInProcess(Object item, Throwable t) {
|
512 | 523 | assertEquals(1, count);
|
513 | 524 | }
|
514 | 525 |
|
| 526 | + @Test |
| 527 | + public void testBeforeReadFails_withAnnotatedListener() { |
| 528 | + StepListener listener = StepListenerFactoryBean.getListener(new AnnotationBasedStepListener()); |
| 529 | + multicast.register(listener); |
| 530 | + |
| 531 | + try { |
| 532 | + multicast.beforeRead(); |
| 533 | + fail("Expected StepListenerFailedException"); |
| 534 | + } catch (StepListenerFailedException e) { |
| 535 | + // expected |
| 536 | + Throwable cause = e.getCause(); |
| 537 | + String message = cause.getMessage(); |
| 538 | + assertTrue(cause instanceof IllegalStateException); |
| 539 | + assertEquals("Wrong message: " + message, "listener error", message); |
| 540 | + } |
| 541 | + } |
| 542 | + |
| 543 | + @Test |
| 544 | + public void testAfterReadFails_withAnnotatedListener() { |
| 545 | + StepListener listener = StepListenerFactoryBean.getListener(new AnnotationBasedStepListener()); |
| 546 | + multicast.register(listener); |
| 547 | + |
| 548 | + try { |
| 549 | + multicast.afterRead(null); |
| 550 | + fail("Expected StepListenerFailedException"); |
| 551 | + } catch (StepListenerFailedException e) { |
| 552 | + // expected |
| 553 | + Throwable cause = e.getCause(); |
| 554 | + String message = cause.getMessage(); |
| 555 | + assertTrue(cause instanceof IllegalStateException); |
| 556 | + assertEquals("Wrong message: " + message, "listener error", message); |
| 557 | + } |
| 558 | + } |
| 559 | + |
| 560 | + @Test |
| 561 | + public void testBeforeProcessFails_withAnnotatedListener() { |
| 562 | + StepListener listener = StepListenerFactoryBean.getListener(new AnnotationBasedStepListener()); |
| 563 | + multicast.register(listener); |
| 564 | + |
| 565 | + try { |
| 566 | + multicast.beforeProcess(null); |
| 567 | + fail("Expected StepListenerFailedException"); |
| 568 | + } catch (StepListenerFailedException e) { |
| 569 | + // expected |
| 570 | + Throwable cause = e.getCause(); |
| 571 | + String message = cause.getMessage(); |
| 572 | + assertTrue(cause instanceof IllegalStateException); |
| 573 | + assertEquals("Wrong message: " + message, "listener error", message); |
| 574 | + } |
| 575 | + } |
| 576 | + |
| 577 | + @Test |
| 578 | + public void testAfterProcessFails_withAnnotatedListener() { |
| 579 | + StepListener listener = StepListenerFactoryBean.getListener(new AnnotationBasedStepListener()); |
| 580 | + multicast.register(listener); |
| 581 | + |
| 582 | + try { |
| 583 | + multicast.afterProcess(null, null); |
| 584 | + fail("Expected StepListenerFailedException"); |
| 585 | + } catch (StepListenerFailedException e) { |
| 586 | + // expected |
| 587 | + Throwable cause = e.getCause(); |
| 588 | + String message = cause.getMessage(); |
| 589 | + assertTrue(cause instanceof IllegalStateException); |
| 590 | + assertEquals("Wrong message: " + message, "listener error", message); |
| 591 | + } |
| 592 | + } |
| 593 | + |
| 594 | + @Test |
| 595 | + public void testBeforeWriteFails_withAnnotatedListener() { |
| 596 | + StepListener listener = StepListenerFactoryBean.getListener(new AnnotationBasedStepListener()); |
| 597 | + multicast.register(listener); |
| 598 | + |
| 599 | + try { |
| 600 | + multicast.beforeWrite(null); |
| 601 | + fail("Expected StepListenerFailedException"); |
| 602 | + } catch (StepListenerFailedException e) { |
| 603 | + // expected |
| 604 | + Throwable cause = e.getCause(); |
| 605 | + String message = cause.getMessage(); |
| 606 | + assertTrue(cause instanceof IllegalStateException); |
| 607 | + assertEquals("Wrong message: " + message, "listener error", message); |
| 608 | + } |
| 609 | + } |
| 610 | + |
| 611 | + @Test |
| 612 | + public void testAfterWriteFails_withAnnotatedListener() { |
| 613 | + StepListener listener = StepListenerFactoryBean.getListener(new AnnotationBasedStepListener()); |
| 614 | + multicast.register(listener); |
| 615 | + |
| 616 | + try { |
| 617 | + multicast.afterWrite(null); |
| 618 | + fail("Expected StepListenerFailedException"); |
| 619 | + } catch (StepListenerFailedException e) { |
| 620 | + // expected |
| 621 | + Throwable cause = e.getCause(); |
| 622 | + String message = cause.getMessage(); |
| 623 | + assertTrue(cause instanceof IllegalStateException); |
| 624 | + assertEquals("Wrong message: " + message, "listener error", message); |
| 625 | + } |
| 626 | + } |
| 627 | + |
| 628 | + @Test |
| 629 | + public void testBeforeChunkFails_withAnnotatedListener() { |
| 630 | + StepListener listener = StepListenerFactoryBean.getListener(new AnnotationBasedStepListener()); |
| 631 | + multicast.register(listener); |
| 632 | + |
| 633 | + try { |
| 634 | + multicast.beforeChunk(null); |
| 635 | + fail("Expected StepListenerFailedException"); |
| 636 | + } catch (StepListenerFailedException e) { |
| 637 | + // expected |
| 638 | + Throwable cause = e.getCause(); |
| 639 | + String message = cause.getMessage(); |
| 640 | + assertTrue(cause instanceof IllegalStateException); |
| 641 | + assertEquals("Wrong message: " + message, "listener error", message); |
| 642 | + } |
| 643 | + } |
| 644 | + |
| 645 | + @Test |
| 646 | + public void testAfterChunkFails_withAnnotatedListener() { |
| 647 | + StepListener listener = StepListenerFactoryBean.getListener(new AnnotationBasedStepListener()); |
| 648 | + multicast.register(listener); |
| 649 | + |
| 650 | + try { |
| 651 | + multicast.afterChunk(null); |
| 652 | + fail("Expected StepListenerFailedException"); |
| 653 | + } catch (StepListenerFailedException e) { |
| 654 | + // expected |
| 655 | + Throwable cause = e.getCause(); |
| 656 | + String message = cause.getMessage(); |
| 657 | + assertTrue(cause instanceof IllegalStateException); |
| 658 | + assertEquals("Wrong message: " + message, "listener error", message); |
| 659 | + } |
| 660 | + } |
| 661 | + |
| 662 | + private final class AnnotationBasedStepListener { |
| 663 | + |
| 664 | + private IllegalStateException exception = new IllegalStateException("listener error"); |
| 665 | + |
| 666 | + @BeforeRead |
| 667 | + public void beforeRead() { |
| 668 | + throw exception; |
| 669 | + } |
| 670 | + |
| 671 | + @AfterRead |
| 672 | + public void afterRead() { |
| 673 | + throw exception; |
| 674 | + } |
| 675 | + |
| 676 | + @BeforeProcess |
| 677 | + public void beforeProcess() { |
| 678 | + throw exception; |
| 679 | + } |
| 680 | + |
| 681 | + @AfterProcess |
| 682 | + public void afterProcess() { |
| 683 | + throw exception; |
| 684 | + } |
| 685 | + |
| 686 | + @BeforeWrite |
| 687 | + public void beforeWrite() { |
| 688 | + throw exception; |
| 689 | + } |
| 690 | + |
| 691 | + @AfterWrite |
| 692 | + public void afterWrite() { |
| 693 | + throw exception; |
| 694 | + } |
| 695 | + |
| 696 | + @BeforeChunk |
| 697 | + public void beforeChunk() { |
| 698 | + throw exception; |
| 699 | + } |
| 700 | + |
| 701 | + @AfterChunk |
| 702 | + public void afterChunk() { |
| 703 | + throw exception; |
| 704 | + } |
| 705 | + |
| 706 | + } |
| 707 | + |
515 | 708 | /**
|
516 | 709 | * @author Dave Syer
|
517 | 710 | *
|
|
0 commit comments