Skip to content

Commit 1b5571f

Browse files
Improve code coverage for DefaultITextProductEventProcessor
DEVSIX-5706
1 parent 9f3dd31 commit 1b5571f

File tree

2 files changed

+68
-11
lines changed

2 files changed

+68
-11
lines changed

itext.tests/itext.commons.tests/itext/commons/actions/processors/DefaultITextProductEventProcessorTest.cs

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,71 @@ You should have received a copy of the GNU Affero General Public License
2121
along with this program. If not, see <https://www.gnu.org/licenses/>.
2222
*/
2323
using System;
24+
using iText.Commons.Actions.Confirmations;
25+
using iText.Commons.Actions.Data;
26+
using iText.Commons.Actions.Sequence;
27+
using iText.Commons.Ecosystem;
2428
using iText.Commons.Exceptions;
2529
using iText.Test;
30+
using iText.Test.Attributes;
2631

2732
namespace iText.Commons.Actions.Processors {
2833
public class DefaultITextProductEventProcessorTest : ExtendedITextTest {
2934
[NUnit.Framework.Test]
3035
public virtual void ConstructorWithNullProductNameTest() {
31-
NUnit.Framework.Assert.That(() => {
32-
new DefaultITextProductEventProcessor(null);
36+
Exception e = NUnit.Framework.Assert.Catch(typeof(ArgumentException), () => new DefaultITextProductEventProcessor
37+
(null));
38+
NUnit.Framework.Assert.AreEqual(CommonsExceptionMessageConstant.PRODUCT_NAME_CAN_NOT_BE_NULL, e.Message);
39+
}
40+
41+
[NUnit.Framework.Test]
42+
[LogMessage("{0} you are probably {1}", LogLevel = LogLevelConstants.INFO)]
43+
public virtual void MessageIsLoggedTest() {
44+
DefaultITextProductEventProcessorTest.TestDefaultITextProductEventProcessor testProcessor = new DefaultITextProductEventProcessorTest.TestDefaultITextProductEventProcessor
45+
();
46+
ITextTestEvent e = new ITextTestEvent(new SequenceId(), CommonsProductData.GetInstance(), null, "test event"
47+
);
48+
NUnit.Framework.Assert.DoesNotThrow(() => testProcessor.OnEvent(new ConfirmEvent(e)));
49+
}
50+
51+
[NUnit.Framework.Test]
52+
[LogMessage("{0} you are probably {1}", LogLevel = LogLevelConstants.INFO, Count = 4)]
53+
public virtual void MessageIsLoggedThreeTimesTest() {
54+
int iterationsNumber = 15;
55+
// "1" correspond to expected iterations with log messages:
56+
// 1 0 0 0 0
57+
// 0 1 0 0 0
58+
// 1 0 0 0 1
59+
DefaultITextProductEventProcessorTest.TestDefaultITextProductEventProcessor testProcessor = new DefaultITextProductEventProcessorTest.TestDefaultITextProductEventProcessor
60+
();
61+
ITextTestEvent e = new ITextTestEvent(new SequenceId(), CommonsProductData.GetInstance(), null, "test event"
62+
);
63+
for (int i = 0; i < iterationsNumber; ++i) {
64+
NUnit.Framework.Assert.DoesNotThrow(() => testProcessor.OnEvent(new ConfirmEvent(e)));
65+
}
66+
}
67+
68+
private class TestDefaultITextProductEventProcessor : DefaultITextProductEventProcessor {
69+
public TestDefaultITextProductEventProcessor()
70+
: base("test product") {
71+
}
72+
73+
internal override long AcquireRepeatLevel(int lvl) {
74+
switch (lvl) {
75+
case 0: {
76+
return 0;
77+
}
78+
79+
case 1: {
80+
return 5;
81+
}
82+
83+
case 2: {
84+
return 3;
85+
}
86+
}
87+
return 0;
3388
}
34-
, NUnit.Framework.Throws.InstanceOf<ArgumentException>().With.Message.EqualTo(CommonsExceptionMessageConstant.PRODUCT_NAME_CAN_NOT_BE_NULL))
35-
;
3689
}
3790
}
3891
}

itext/itext.commons/itext/commons/actions/processors/DefaultITextProductEventProcessor.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ You should have received a copy of the GNU Affero General Public License
3030
namespace iText.Commons.Actions.Processors {
3131
/// <summary>Defines a default strategy of product event processing.</summary>
3232
public class DefaultITextProductEventProcessor : AbstractITextProductEventProcessor {
33-
private static readonly ILogger LOGGER = ITextLogManager.GetLogger(typeof(iText.Commons.Actions.Processors.DefaultITextProductEventProcessor
34-
));
35-
36-
private static readonly byte[] messageForLogging = Convert.FromBase64String("WW91IGFyZSB1c2luZyBpVGV4dCB1bmRlciB0aGUgQUdQTC4KCklmIHRoaXMgaXMgeW9"
33+
internal static readonly byte[] MESSAGE_FOR_LOGGING = Convert.FromBase64String("WW91IGFyZSB1c2luZyBpVGV4dCB1bmRlciB0aGUgQUdQTC4KCklmIHRoaXMgaXMgeW9"
3734
+ "1ciBpbnRlbnRpb24sIHlvdSBoYXZlIHB1Ymxpc2hlZCB5b3VyIG93biBzb3VyY2UgY2" + "9kZSBhcyBBR1BMIHNvZnR3YXJlIHRvby4KUGxlYXNlIGxldCB1cyBrbm93IHdoZXJlI"
3835
+ "HRvIGZpbmQgeW91ciBzb3VyY2UgY29kZSBieSBzZW5kaW5nIGEgbWFpbCB0byBhZ3Bs" + "QGl0ZXh0cGRmLmNvbQpXZSdkIGJlIGhvbm9yZWQgdG8gYWRkIGl0IHRvIG91ciBsaXN"
3936
+ "0IG9mIEFHUEwgcHJvamVjdHMgYnVpbHQgb24gdG9wIG9mIGlUZXh0IDcKYW5kIHdlJ2" + "xsIGV4cGxhaW4gaG93IHRvIHJlbW92ZSB0aGlzIG1lc3NhZ2UgZnJvbSB5b3VyIGVyc"
@@ -42,6 +39,9 @@ public class DefaultITextProductEventProcessor : AbstractITextProductEventProces
4239
+ "XIsIHdlJ2xsIGV4cGxhaW4gaG93IHRvIGluc3RhbGwgeW91ciBsaWNlbnNlIGtleSB0" + "byBhdm9pZCB0aGlzIG1lc3NhZ2UuCklmIHlvdSdyZSBub3QgYSBjdXN0b21lciwgd2U"
4340
+ "nbGwgZXhwbGFpbiB0aGUgYmVuZWZpdHMgb2YgYmVjb21pbmcgYSBjdXN0b21lci4=");
4441

42+
private static readonly ILogger LOGGER = ITextLogManager.GetLogger(typeof(iText.Commons.Actions.Processors.DefaultITextProductEventProcessor
43+
));
44+
4545
private static readonly long[] REPEAT = new long[] { 10000L, 5000L, 1000L };
4646

4747
private static readonly int MAX_LVL = REPEAT.Length - 1;
@@ -58,7 +58,7 @@ public class DefaultITextProductEventProcessor : AbstractITextProductEventProces
5858
/// <param name="productName">is a product name</param>
5959
public DefaultITextProductEventProcessor(String productName)
6060
: base(productName) {
61-
repeatLevel = new AtomicLong(REPEAT[(int)level.Get()]);
61+
repeatLevel = new AtomicLong(AcquireRepeatLevel((int)level.Get()));
6262
}
6363

6464
public override void OnEvent(AbstractProductProcessITextEvent @event) {
@@ -72,12 +72,12 @@ public override void OnEvent(AbstractProductProcessITextEvent @event) {
7272
if (level.IncrementAndGet() > MAX_LVL) {
7373
level.Set(MAX_LVL);
7474
}
75-
repeatLevel.Set(REPEAT[(int)level.Get()]);
75+
repeatLevel.Set(AcquireRepeatLevel((int)level.Get()));
7676
isNeededToLogMessage = true;
7777
}
7878
}
7979
if (isNeededToLogMessage) {
80-
String message = iText.Commons.Utils.JavaUtil.GetStringForBytes(messageForLogging, iText.Commons.Utils.EncodingUtil.ISO_8859_1
80+
String message = iText.Commons.Utils.JavaUtil.GetStringForBytes(MESSAGE_FOR_LOGGING, iText.Commons.Utils.EncodingUtil.ISO_8859_1
8181
);
8282
LOGGER.LogInformation(message);
8383
System.Console.Out.WriteLine(message);
@@ -87,5 +87,9 @@ public override void OnEvent(AbstractProductProcessITextEvent @event) {
8787
public override String GetUsageType() {
8888
return "AGPL";
8989
}
90+
91+
internal virtual long AcquireRepeatLevel(int lvl) {
92+
return REPEAT[lvl];
93+
}
9094
}
9195
}

0 commit comments

Comments
 (0)