@@ -24,34 +24,37 @@ This file is part of the iText (R) project.
2424
2525import com .itextpdf .commons .actions .confirmations .ConfirmEvent ;
2626import com .itextpdf .commons .actions .confirmations .ConfirmedEventWrapper ;
27+ import com .itextpdf .commons .actions .processors .ITextProductEventProcessor ;
2728import com .itextpdf .commons .actions .sequence .SequenceId ;
2829import com .itextpdf .commons .ecosystem .ITextTestEvent ;
30+ import com .itextpdf .commons .exceptions .ProductEventHandlerRepeatException ;
2931import com .itextpdf .commons .exceptions .UnknownProductException ;
3032import com .itextpdf .commons .utils .MessageFormatUtil ;
33+ import com .itextpdf .test .AssertUtil ;
3134import com .itextpdf .test .ExtendedITextTest ;
3235import com .itextpdf .test .annotations .type .UnitTest ;
3336
3437import org .junit .Assert ;
35- import org .junit .Rule ;
38+ import org .junit .Before ;
3639import org .junit .Test ;
3740import org .junit .experimental .categories .Category ;
38- import org .junit .rules .ExpectedException ;
3941
4042@ Category (UnitTest .class )
4143public class ProductEventHandlerTest extends ExtendedITextTest {
42- @ Rule
43- public ExpectedException junitExpectedException = ExpectedException .none ();
44+ @ Before
45+ public void clearProcessors () {
46+ ProductEventHandler .INSTANCE .clearProcessors ();
47+ }
4448
4549 @ Test
4650 public void unknownProductTest () {
4751 ProductEventHandler handler = ProductEventHandler .INSTANCE ;
4852
49- junitExpectedException .expect (UnknownProductException .class );
50- junitExpectedException .expectMessage (
51- MessageFormatUtil .format (UnknownProductException .UNKNOWN_PRODUCT , "Unknown Product" ));
52-
53- handler .onAcceptedEvent (new ITextTestEvent (new SequenceId (), null , "test-event" ,
54- "Unknown Product" ));
53+ AbstractContextBasedITextEvent event = new ITextTestEvent (new SequenceId (), null , "test-event" , "Unknown Product" );
54+ Exception ex = Assert .assertThrows (UnknownProductException .class ,
55+ () -> handler .onAcceptedEvent (event ));
56+ Assert .assertEquals (MessageFormatUtil .format (UnknownProductException .UNKNOWN_PRODUCT , "Unknown Product" ),
57+ ex .getMessage ());
5558 }
5659
5760 @ Test
@@ -114,4 +117,75 @@ public void confirmEventTest() {
114117 Assert .assertTrue (handler .getEvents (sequenceId ).get (0 ) instanceof ConfirmedEventWrapper );
115118 Assert .assertEquals (event , ((ConfirmedEventWrapper ) handler .getEvents (sequenceId ).get (0 )).getEvent ());
116119 }
120+
121+ @ Test
122+ public void repeatEventHandlingWithFiveExceptionOnProcessingTest () {
123+ ProductEventHandler handler = ProductEventHandler .INSTANCE ;
124+
125+ handler .addProcessor (new RepeatEventProcessor (5 ));
126+
127+ AbstractContextBasedITextEvent event = new ITextTestEvent (new SequenceId (), null , "test" ,
128+ ProductNameConstant .ITEXT_CORE );
129+
130+ Exception e = Assert .assertThrows (ProductEventHandlerRepeatException .class ,
131+ () -> handler .onAcceptedEvent (event ));
132+ Assert .assertEquals ("customMessage5" , e .getMessage ());
133+ }
134+
135+ @ Test
136+ public void repeatEventHandlingWithFourExceptionOnProcessingTest () {
137+ ProductEventHandler handler = ProductEventHandler .INSTANCE ;
138+
139+ handler .addProcessor (new RepeatEventProcessor (4 ));
140+
141+ AbstractContextBasedITextEvent event = new ITextTestEvent (new SequenceId (), null , "test" ,
142+ ProductNameConstant .ITEXT_CORE );
143+
144+ AssertUtil .doesNotThrow (() -> handler .onAcceptedEvent (event ));
145+ }
146+
147+ @ Test
148+ public void repeatEventHandlingWithOneExceptionOnProcessingTest () {
149+ ProductEventHandler handler = ProductEventHandler .INSTANCE ;
150+
151+ handler .addProcessor (new RepeatEventProcessor (1 ));
152+
153+ AbstractContextBasedITextEvent event = new ITextTestEvent (new SequenceId (), null , "test" ,
154+ ProductNameConstant .ITEXT_CORE );
155+
156+ AssertUtil .doesNotThrow (() -> handler .onAcceptedEvent (event ));
157+ }
158+
159+ private static class RepeatEventProcessor implements ITextProductEventProcessor {
160+ private final int exceptionsCount ;
161+ private int exceptionCounter = 0 ;
162+
163+ public RepeatEventProcessor (int exceptionsCount ) {
164+ this .exceptionsCount = exceptionsCount ;
165+ }
166+
167+ @ Override
168+ public void onEvent (AbstractProductProcessITextEvent event ) {
169+ if (exceptionCounter < exceptionsCount ) {
170+ exceptionCounter ++;
171+ throw new ProductEventHandlerRepeatException ("customMessage" + exceptionCounter );
172+ }
173+
174+ }
175+
176+ @ Override
177+ public String getProductName () {
178+ return ProductNameConstant .ITEXT_CORE ;
179+ }
180+
181+ @ Override
182+ public String getUsageType () {
183+ return "someUsage" ;
184+ }
185+
186+ @ Override
187+ public String getProducer () {
188+ return "someProducer" ;
189+ }
190+ }
117191}
0 commit comments