@@ -20,7 +20,9 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
20
You should have received a copy of the GNU Affero General Public License
21
21
along with this program. If not, see <https://www.gnu.org/licenses/>.
22
22
*/
23
+ using System ;
23
24
using iText . Commons . Actions . Confirmations ;
25
+ using iText . Commons . Actions . Processors ;
24
26
using iText . Commons . Actions . Sequence ;
25
27
using iText . Commons . Ecosystem ;
26
28
using iText . Commons . Exceptions ;
@@ -29,14 +31,20 @@ You should have received a copy of the GNU Affero General Public License
29
31
30
32
namespace iText . Commons . Actions {
31
33
public class ProductEventHandlerTest : ExtendedITextTest {
34
+ [ NUnit . Framework . SetUp ]
35
+ public virtual void ClearProcessors ( ) {
36
+ ProductEventHandler . INSTANCE . ClearProcessors ( ) ;
37
+ }
38
+
32
39
[ NUnit . Framework . Test ]
33
40
public virtual void UnknownProductTest ( ) {
34
41
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 ) ;
40
48
}
41
49
42
50
[ NUnit . Framework . Test ]
@@ -83,5 +91,63 @@ public virtual void ConfirmEventTest() {
83
91
NUnit . Framework . Assert . AreEqual ( @event , ( ( ConfirmedEventWrapper ) handler . GetEvents ( sequenceId ) [ 0 ] ) . GetEvent
84
92
( ) ) ;
85
93
}
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
+ }
86
152
}
87
153
}
0 commit comments