Skip to content

Commit fc2402d

Browse files
Improve code coverage for DefaultITextProductEventProcessor
DEVSIX-5706
1 parent 12d544b commit fc2402d

File tree

2 files changed

+67
-13
lines changed

2 files changed

+67
-13
lines changed

commons/src/main/java/com/itextpdf/commons/actions/processors/DefaultITextProductEventProcessor.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ This file is part of the iText (R) project.
3636
*/
3737
public class DefaultITextProductEventProcessor extends AbstractITextProductEventProcessor {
3838

39-
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultITextProductEventProcessor.class);
40-
41-
private static final byte[] messageForLogging = Base64
39+
static final byte[] MESSAGE_FOR_LOGGING = Base64
4240
.decode("WW91IGFyZSB1c2luZyBpVGV4dCB1bmRlciB0aGUgQUdQTC4KCklmIHRoaXMgaXMgeW9"
4341
+ "1ciBpbnRlbnRpb24sIHlvdSBoYXZlIHB1Ymxpc2hlZCB5b3VyIG93biBzb3VyY2UgY2"
4442
+ "9kZSBhcyBBR1BMIHNvZnR3YXJlIHRvby4KUGxlYXNlIGxldCB1cyBrbm93IHdoZXJlI"
@@ -54,6 +52,8 @@ public class DefaultITextProductEventProcessor extends AbstractITextProductEvent
5452
+ "byBhdm9pZCB0aGlzIG1lc3NhZ2UuCklmIHlvdSdyZSBub3QgYSBjdXN0b21lciwgd2U"
5553
+ "nbGwgZXhwbGFpbiB0aGUgYmVuZWZpdHMgb2YgYmVjb21pbmcgYSBjdXN0b21lci4=");
5654

55+
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultITextProductEventProcessor.class);
56+
5757
private static final long[] REPEAT = {10000L, 5000L, 1000L};
5858

5959
private static final int MAX_LVL = REPEAT.length - 1;
@@ -73,7 +73,7 @@ public class DefaultITextProductEventProcessor extends AbstractITextProductEvent
7373
*/
7474
public DefaultITextProductEventProcessor(String productName) {
7575
super(productName);
76-
repeatLevel = new AtomicLong(REPEAT[(int) level.get()]);
76+
repeatLevel = new AtomicLong(acquireRepeatLevel((int) level.get()));
7777
}
7878

7979
@Override
@@ -88,13 +88,13 @@ public void onEvent(AbstractProductProcessITextEvent event) {
8888
if (level.incrementAndGet() > MAX_LVL) {
8989
level.set(MAX_LVL);
9090
}
91-
repeatLevel.set(REPEAT[(int) level.get()]);
91+
repeatLevel.set(acquireRepeatLevel((int) level.get()));
9292
isNeededToLogMessage = true;
9393
}
9494
}
9595

9696
if (isNeededToLogMessage) {
97-
String message = new String(messageForLogging, StandardCharsets.ISO_8859_1);
97+
String message = new String(MESSAGE_FOR_LOGGING, StandardCharsets.ISO_8859_1);
9898
LOGGER.info(message);
9999
System.out.println(message);
100100
}
@@ -104,4 +104,8 @@ public void onEvent(AbstractProductProcessITextEvent event) {
104104
public String getUsageType() {
105105
return "AGPL";
106106
}
107+
108+
long acquireRepeatLevel(int lvl) {
109+
return REPEAT[lvl];
110+
}
107111
}

commons/src/test/java/com/itextpdf/commons/actions/processors/DefaultITextProductEventProcessorTest.java

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,74 @@ This file is part of the iText (R) project.
2222
*/
2323
package com.itextpdf.commons.actions.processors;
2424

25+
import com.itextpdf.commons.actions.confirmations.ConfirmEvent;
26+
import com.itextpdf.commons.actions.data.CommonsProductData;
27+
import com.itextpdf.commons.actions.sequence.SequenceId;
28+
import com.itextpdf.commons.ecosystem.ITextTestEvent;
2529
import com.itextpdf.commons.exceptions.CommonsExceptionMessageConstant;
30+
import com.itextpdf.test.AssertUtil;
2631
import com.itextpdf.test.ExtendedITextTest;
32+
import com.itextpdf.test.LogLevelConstants;
33+
import com.itextpdf.test.annotations.LogMessage;
34+
import com.itextpdf.test.annotations.LogMessages;
2735
import com.itextpdf.test.annotations.type.UnitTest;
2836

29-
import org.junit.Rule;
37+
import org.junit.Assert;
3038
import org.junit.Test;
3139
import org.junit.experimental.categories.Category;
32-
import org.junit.rules.ExpectedException;
3340

3441
@Category(UnitTest.class)
3542
public class DefaultITextProductEventProcessorTest extends ExtendedITextTest {
36-
@Rule
37-
public ExpectedException junitExpectedException = ExpectedException.none();
3843

3944
@Test
4045
public void constructorWithNullProductNameTest() {
41-
junitExpectedException.expect(IllegalArgumentException.class);
42-
junitExpectedException.expectMessage(CommonsExceptionMessageConstant.PRODUCT_NAME_CAN_NOT_BE_NULL);
43-
new DefaultITextProductEventProcessor(null);
46+
Exception e =
47+
Assert.assertThrows(IllegalArgumentException.class, () -> new DefaultITextProductEventProcessor(null));
48+
Assert.assertEquals(CommonsExceptionMessageConstant.PRODUCT_NAME_CAN_NOT_BE_NULL, e.getMessage());
49+
}
50+
51+
@Test
52+
@LogMessages(messages = @LogMessage(messageTemplate = "{0} you are probably {1}", logLevel = LogLevelConstants.INFO))
53+
public void messageIsLoggedTest() {
54+
TestDefaultITextProductEventProcessor testProcessor = new TestDefaultITextProductEventProcessor();
55+
ITextTestEvent e = new ITextTestEvent(new SequenceId(), CommonsProductData.getInstance(), null, "test event");
56+
AssertUtil.doesNotThrow(() -> testProcessor.onEvent(new ConfirmEvent(e)));
57+
}
58+
59+
@Test
60+
@LogMessages(messages =
61+
@LogMessage(messageTemplate = "{0} you are probably {1}", logLevel = LogLevelConstants.INFO, count = 4)
62+
)
63+
public void messageIsLoggedThreeTimesTest() {
64+
int iterationsNumber = 15;
65+
// "1" correspond to expected iterations with log messages:
66+
// 1 0 0 0 0
67+
// 0 1 0 0 0
68+
// 1 0 0 0 1
69+
TestDefaultITextProductEventProcessor testProcessor = new TestDefaultITextProductEventProcessor();
70+
ITextTestEvent e = new ITextTestEvent(new SequenceId(), CommonsProductData.getInstance(), null, "test event");
71+
for (int i = 0; i < iterationsNumber; ++i) {
72+
AssertUtil.doesNotThrow(() -> testProcessor.onEvent(new ConfirmEvent(e)));
73+
}
74+
}
75+
76+
private static class TestDefaultITextProductEventProcessor extends DefaultITextProductEventProcessor {
77+
78+
public TestDefaultITextProductEventProcessor() {
79+
super("test product");
80+
}
81+
82+
@Override
83+
long acquireRepeatLevel(int lvl) {
84+
switch (lvl) {
85+
case 0:
86+
return 0;
87+
case 1:
88+
return 5;
89+
case 2:
90+
return 3;
91+
}
92+
return 0;
93+
}
4494
}
4595
}

0 commit comments

Comments
 (0)