@@ -24,34 +24,37 @@ This file is part of the iText (R) project.
24
24
25
25
import com .itextpdf .commons .actions .confirmations .ConfirmEvent ;
26
26
import com .itextpdf .commons .actions .confirmations .ConfirmedEventWrapper ;
27
+ import com .itextpdf .commons .actions .processors .ITextProductEventProcessor ;
27
28
import com .itextpdf .commons .actions .sequence .SequenceId ;
28
29
import com .itextpdf .commons .ecosystem .ITextTestEvent ;
30
+ import com .itextpdf .commons .exceptions .ProductEventHandlerRepeatException ;
29
31
import com .itextpdf .commons .exceptions .UnknownProductException ;
30
32
import com .itextpdf .commons .utils .MessageFormatUtil ;
33
+ import com .itextpdf .test .AssertUtil ;
31
34
import com .itextpdf .test .ExtendedITextTest ;
32
35
import com .itextpdf .test .annotations .type .UnitTest ;
33
36
34
37
import org .junit .Assert ;
35
- import org .junit .Rule ;
38
+ import org .junit .Before ;
36
39
import org .junit .Test ;
37
40
import org .junit .experimental .categories .Category ;
38
- import org .junit .rules .ExpectedException ;
39
41
40
42
@ Category (UnitTest .class )
41
43
public class ProductEventHandlerTest extends ExtendedITextTest {
42
- @ Rule
43
- public ExpectedException junitExpectedException = ExpectedException .none ();
44
+ @ Before
45
+ public void clearProcessors () {
46
+ ProductEventHandler .INSTANCE .clearProcessors ();
47
+ }
44
48
45
49
@ Test
46
50
public void unknownProductTest () {
47
51
ProductEventHandler handler = ProductEventHandler .INSTANCE ;
48
52
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 ());
55
58
}
56
59
57
60
@ Test
@@ -114,4 +117,75 @@ public void confirmEventTest() {
114
117
Assert .assertTrue (handler .getEvents (sequenceId ).get (0 ) instanceof ConfirmedEventWrapper );
115
118
Assert .assertEquals (event , ((ConfirmedEventWrapper ) handler .getEvents (sequenceId ).get (0 )).getEvent ());
116
119
}
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
+ }
117
191
}
0 commit comments