@@ -20,7 +20,9 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2020You should have received a copy of the GNU Affero General Public License
2121along with this program. If not, see <https://www.gnu.org/licenses/>.
2222*/
23+ using System ;
2324using iText . Commons . Actions . Confirmations ;
25+ using iText . Commons . Actions . Processors ;
2426using iText . Commons . Actions . Sequence ;
2527using iText . Commons . Ecosystem ;
2628using iText . Commons . Exceptions ;
@@ -29,14 +31,20 @@ You should have received a copy of the GNU Affero General Public License
2931
3032namespace iText . Commons . Actions {
3133 public class ProductEventHandlerTest : ExtendedITextTest {
34+ [ NUnit . Framework . SetUp ]
35+ public virtual void ClearProcessors ( ) {
36+ ProductEventHandler . INSTANCE . ClearProcessors ( ) ;
37+ }
38+
3239 [ NUnit . Framework . Test ]
3340 public virtual void UnknownProductTest ( ) {
3441 ProductEventHandler handler = ProductEventHandler . INSTANCE ;
35- NUnit . Framework . Assert . That ( ( ) => {
36- handler . OnAcceptedEvent ( new ITextTestEvent ( new SequenceId ( ) , null , "test-event" , "Unknown Product" ) ) ;
37- }
38- , NUnit . Framework . Throws . InstanceOf < UnknownProductException > ( ) . With . Message . EqualTo ( MessageFormatUtil . Format ( UnknownProductException . UNKNOWN_PRODUCT , "Unknown Product" ) ) )
39- ;
42+ AbstractContextBasedITextEvent @event = new ITextTestEvent ( new SequenceId ( ) , null , "test-event" , "Unknown Product"
43+ ) ;
44+ Exception ex = NUnit . Framework . Assert . Catch ( typeof ( UnknownProductException ) , ( ) => handler . OnAcceptedEvent
45+ ( @event ) ) ;
46+ NUnit . Framework . Assert . AreEqual ( MessageFormatUtil . Format ( UnknownProductException . UNKNOWN_PRODUCT , "Unknown Product"
47+ ) , ex . Message ) ;
4048 }
4149
4250 [ NUnit . Framework . Test ]
@@ -83,5 +91,63 @@ public virtual void ConfirmEventTest() {
8391 NUnit . Framework . Assert . AreEqual ( @event , ( ( ConfirmedEventWrapper ) handler . GetEvents ( sequenceId ) [ 0 ] ) . GetEvent
8492 ( ) ) ;
8593 }
94+
95+ [ NUnit . Framework . Test ]
96+ public virtual void RepeatEventHandlingWithFiveExceptionOnProcessingTest ( ) {
97+ ProductEventHandler handler = ProductEventHandler . INSTANCE ;
98+ handler . AddProcessor ( new ProductEventHandlerTest . RepeatEventProcessor ( 5 ) ) ;
99+ AbstractContextBasedITextEvent @event = new ITextTestEvent ( new SequenceId ( ) , null , "test" , ProductNameConstant
100+ . ITEXT_CORE ) ;
101+ Exception e = NUnit . Framework . Assert . Catch ( typeof ( ProductEventHandlerRepeatException ) , ( ) => handler . OnAcceptedEvent
102+ ( @event ) ) ;
103+ NUnit . Framework . Assert . AreEqual ( "customMessage5" , e . Message ) ;
104+ }
105+
106+ [ NUnit . Framework . Test ]
107+ public virtual void RepeatEventHandlingWithFourExceptionOnProcessingTest ( ) {
108+ ProductEventHandler handler = ProductEventHandler . INSTANCE ;
109+ handler . AddProcessor ( new ProductEventHandlerTest . RepeatEventProcessor ( 4 ) ) ;
110+ AbstractContextBasedITextEvent @event = new ITextTestEvent ( new SequenceId ( ) , null , "test" , ProductNameConstant
111+ . ITEXT_CORE ) ;
112+ NUnit . Framework . Assert . DoesNotThrow ( ( ) => handler . OnAcceptedEvent ( @event ) ) ;
113+ }
114+
115+ [ NUnit . Framework . Test ]
116+ public virtual void RepeatEventHandlingWithOneExceptionOnProcessingTest ( ) {
117+ ProductEventHandler handler = ProductEventHandler . INSTANCE ;
118+ handler . AddProcessor ( new ProductEventHandlerTest . RepeatEventProcessor ( 1 ) ) ;
119+ AbstractContextBasedITextEvent @event = new ITextTestEvent ( new SequenceId ( ) , null , "test" , ProductNameConstant
120+ . ITEXT_CORE ) ;
121+ NUnit . Framework . Assert . DoesNotThrow ( ( ) => handler . OnAcceptedEvent ( @event ) ) ;
122+ }
123+
124+ private class RepeatEventProcessor : ITextProductEventProcessor {
125+ private readonly int exceptionsCount ;
126+
127+ private int exceptionCounter = 0 ;
128+
129+ public RepeatEventProcessor ( int exceptionsCount ) {
130+ this . exceptionsCount = exceptionsCount ;
131+ }
132+
133+ public virtual void OnEvent ( AbstractProductProcessITextEvent @event ) {
134+ if ( exceptionCounter < exceptionsCount ) {
135+ exceptionCounter ++ ;
136+ throw new ProductEventHandlerRepeatException ( "customMessage" + exceptionCounter ) ;
137+ }
138+ }
139+
140+ public virtual String GetProductName ( ) {
141+ return ProductNameConstant . ITEXT_CORE ;
142+ }
143+
144+ public virtual String GetUsageType ( ) {
145+ return "someUsage" ;
146+ }
147+
148+ public virtual String GetProducer ( ) {
149+ return "someProducer" ;
150+ }
151+ }
86152 }
87153}
0 commit comments