Skip to content

Commit 6736eea

Browse files
ar3emars18wrw
authored andcommitted
Add event handling tests and fix issue for createTxtFile
DEVSIX-5887
1 parent 77d4fc7 commit 6736eea

21 files changed

+1276
-30
lines changed

pdfocr-api/src/main/java/com/itextpdf/pdfocr/OcrProcessContext.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ This file is part of the iText (R) project.
2626
* Class for storing ocr processing context.
2727
*/
2828
public class OcrProcessContext {
29-
private final AbstractPdfOcrEventHelper ocrEventHelper;
29+
private AbstractPdfOcrEventHelper ocrEventHelper;
3030

3131
/**
3232
* Creates an instance of ocr process context
@@ -45,4 +45,13 @@ public OcrProcessContext(AbstractPdfOcrEventHelper eventHelper) {
4545
public AbstractPdfOcrEventHelper getOcrEventHelper() {
4646
return ocrEventHelper;
4747
}
48+
49+
/**
50+
* Sets ocr event helper.
51+
*
52+
* @param eventHelper event helper
53+
*/
54+
public void setOcrEventHelper(AbstractPdfOcrEventHelper eventHelper) {
55+
this.ocrEventHelper = eventHelper;
56+
}
4857
}

pdfocr-api/src/main/java/com/itextpdf/pdfocr/statistics/PdfOcrOutputTypeStatisticsAggregator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ This file is part of the iText (R) project.
3535
/**
3636
* Statistics aggregator which aggregates types of ocr processing.
3737
*/
38-
public class PdfOcrOutputTypeStatisticsAggregator extends AbstractStatisticsAggregator {
38+
class PdfOcrOutputTypeStatisticsAggregator extends AbstractStatisticsAggregator {
3939

4040
private static final String STRING_FOR_DATA = "data";
4141
private static final String STRING_FOR_PDF = "pdf";

pdfocr-api/src/main/java/com/itextpdf/pdfocr/statistics/PdfOcrOutputTypeStatisticsEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public List<String> getStatisticsNames() {
8181
*
8282
* @return the statistics event type
8383
*/
84-
PdfOcrOutputType getPdfOcrStatisticsEventType() {
84+
public PdfOcrOutputType getPdfOcrStatisticsEventType() {
8585
return type;
8686
}
8787
}

pdfocr-api/src/test/java/com/itextpdf/pdfocr/OcrPdfCreatorEventHelperTest.java

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ This file is part of the iText (R) project.
2424

2525
import com.itextpdf.commons.actions.AbstractProductITextEvent;
2626
import com.itextpdf.commons.actions.AbstractProductProcessITextEvent;
27+
import com.itextpdf.commons.actions.AbstractStatisticsEvent;
28+
import com.itextpdf.commons.actions.EventManager;
29+
import com.itextpdf.commons.actions.IEvent;
30+
import com.itextpdf.commons.actions.IEventHandler;
2731
import com.itextpdf.commons.actions.confirmations.EventConfirmationType;
2832
import com.itextpdf.commons.actions.contexts.IMetaInfo;
2933
import com.itextpdf.commons.actions.data.ProductData;
@@ -34,30 +38,51 @@ This file is part of the iText (R) project.
3438
import com.itextpdf.test.ExtendedITextTest;
3539
import com.itextpdf.test.annotations.type.UnitTest;
3640

41+
import java.util.ArrayList;
42+
import java.util.Collections;
43+
import java.util.List;
44+
import org.junit.After;
45+
import org.junit.Assert;
46+
import org.junit.Before;
3747
import org.junit.Test;
3848
import org.junit.experimental.categories.Category;
3949

4050
@Category(UnitTest.class)
4151
public class OcrPdfCreatorEventHelperTest extends ExtendedITextTest {
4252
private static final ProductData DUMMY_PRODUCT_DATA =
4353
new ProductData("test-product", "inner_product", "1.0.0", 1900, 2100);
54+
private StoreEventsHandler storeEventsHandler;
55+
56+
@Before
57+
public void before() {
58+
storeEventsHandler = new StoreEventsHandler();
59+
EventManager.getInstance().register(storeEventsHandler);
60+
}
61+
62+
@After
63+
public void after() {
64+
EventManager.getInstance().unregister(storeEventsHandler);
65+
storeEventsHandler = null;
66+
}
4467

4568
@Test
4669
public void productContextBasedEventTest() {
4770
OcrPdfCreatorEventHelper helper = new OcrPdfCreatorEventHelper(new SequenceId(), new DummyMetaInfo());
4871
DummyITextEvent event = new DummyITextEvent();
4972
helper.onEvent(event);
5073

51-
// TODO DEVSIX-5887 assert event reached EventManager
74+
Assert.assertEquals(1, storeEventsHandler.getEvents().size());
75+
Assert.assertEquals(event, storeEventsHandler.getEvents().get(0));
5276
}
5377

5478
@Test
55-
public void statisticsEventTest() {
79+
public void pdfOcrStatisticsEventTest() {
5680
OcrPdfCreatorEventHelper helper = new OcrPdfCreatorEventHelper(new SequenceId(), new DummyMetaInfo());
57-
PdfOcrOutputTypeStatisticsEvent e = new PdfOcrOutputTypeStatisticsEvent(PdfOcrOutputType.PDF, DUMMY_PRODUCT_DATA);
81+
PdfOcrOutputTypeStatisticsEvent e = new PdfOcrOutputTypeStatisticsEvent(PdfOcrOutputType.PDF,
82+
DUMMY_PRODUCT_DATA);
5883
helper.onEvent(e);
5984

60-
// TODO DEVSIX-5887 assert event didn't reach EventManager
85+
Assert.assertEquals(0, storeEventsHandler.getEvents().size());
6186
}
6287

6388
@Test
@@ -66,7 +91,18 @@ public void customProductEventTest() {
6691
AbstractProductITextEvent event = new CustomProductITextEvent(DUMMY_PRODUCT_DATA);
6792
helper.onEvent(event);
6893

69-
// TODO DEVSIX-5887 assert event reached reach EventManager
94+
Assert.assertEquals(1, storeEventsHandler.getEvents().size());
95+
Assert.assertEquals(event, storeEventsHandler.getEvents().get(0));
96+
}
97+
98+
@Test
99+
public void customStatisticsEventTest() {
100+
OcrPdfCreatorEventHelper helper = new OcrPdfCreatorEventHelper(new SequenceId(), new DummyMetaInfo());
101+
CustomStatisticsEvent event = new CustomStatisticsEvent(DUMMY_PRODUCT_DATA);
102+
helper.onEvent(event);
103+
104+
Assert.assertEquals(1, storeEventsHandler.getEvents().size());
105+
Assert.assertEquals(event, storeEventsHandler.getEvents().get(0));
70106
}
71107

72108
private static class DummyMetaInfo implements IMetaInfo {
@@ -89,4 +125,29 @@ protected CustomProductITextEvent(ProductData productData) {
89125
super(productData);
90126
}
91127
}
128+
129+
private static class CustomStatisticsEvent extends AbstractStatisticsEvent {
130+
131+
protected CustomStatisticsEvent(ProductData productData) {
132+
super(productData);
133+
}
134+
135+
@Override
136+
public List<String> getStatisticsNames() {
137+
return Collections.singletonList("custom-statistics");
138+
}
139+
}
140+
141+
private static class StoreEventsHandler implements IEventHandler {
142+
private List<IEvent> events = new ArrayList<>();
143+
144+
public List<IEvent> getEvents() {
145+
return events;
146+
}
147+
148+
@Override
149+
public void onEvent(IEvent event) {
150+
events.add(event);
151+
}
152+
}
92153
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2021 iText Group NV
4+
Authors: iText Software.
5+
6+
This program is offered under a commercial and under the AGPL license.
7+
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
8+
9+
AGPL licensing:
10+
This program is free software: you can redistribute it and/or modify
11+
it under the terms of the GNU Affero General Public License as published by
12+
the Free Software Foundation, either version 3 of the License, or
13+
(at your option) any later version.
14+
15+
This program is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
GNU Affero General Public License for more details.
19+
20+
You should have received a copy of the GNU Affero General Public License
21+
along with this program. If not, see <https://www.gnu.org/licenses/>.
22+
*/
23+
package com.itextpdf.pdfocr;
24+
25+
import com.itextpdf.commons.actions.AbstractProductITextEvent;
26+
import com.itextpdf.commons.actions.confirmations.EventConfirmationType;
27+
import com.itextpdf.commons.actions.sequence.SequenceId;
28+
import com.itextpdf.test.ExtendedITextTest;
29+
import com.itextpdf.test.annotations.type.UnitTest;
30+
31+
import org.junit.Assert;
32+
import org.junit.Test;
33+
import org.junit.experimental.categories.Category;
34+
35+
@Category(UnitTest.class)
36+
public class OcrProcessContextTest extends ExtendedITextTest {
37+
38+
@Test
39+
public void setOcrEventHelperTest() {
40+
AbstractPdfOcrEventHelper eventHelper = new CustomEventHelper();
41+
OcrProcessContext context = new OcrProcessContext(eventHelper);
42+
Assert.assertSame(eventHelper, context.getOcrEventHelper());
43+
}
44+
45+
private static class CustomEventHelper extends AbstractPdfOcrEventHelper {
46+
@Override
47+
public void onEvent(AbstractProductITextEvent event) {
48+
// Do nothing
49+
}
50+
51+
@Override
52+
public SequenceId getSequenceId() {
53+
return null;
54+
}
55+
56+
@Override
57+
public EventConfirmationType getConfirmationType() {
58+
return EventConfirmationType.ON_DEMAND;
59+
}
60+
}
61+
}

pdfocr-tesseract4/src/main/java/com/itextpdf/pdfocr/tesseract4/AbstractTesseract4OcrEngine.java

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ This file is part of the iText (R) project.
2222
*/
2323
package com.itextpdf.pdfocr.tesseract4;
2424

25+
import com.itextpdf.commons.actions.confirmations.ConfirmEvent;
26+
import com.itextpdf.commons.actions.confirmations.EventConfirmationType;
2527
import com.itextpdf.commons.actions.contexts.IMetaInfo;
2628
import com.itextpdf.commons.actions.data.ProductData;
2729
import com.itextpdf.commons.utils.MessageFormatUtil;
@@ -32,8 +34,8 @@ This file is part of the iText (R) project.
3234
import com.itextpdf.pdfocr.OcrProcessContext;
3335
import com.itextpdf.pdfocr.PdfOcrMetaInfoContainer;
3436
import com.itextpdf.pdfocr.TextInfo;
35-
import com.itextpdf.pdfocr.statistics.PdfOcrOutputTypeStatisticsEvent;
3637
import com.itextpdf.pdfocr.statistics.PdfOcrOutputType;
38+
import com.itextpdf.pdfocr.statistics.PdfOcrOutputTypeStatisticsEvent;
3739
import com.itextpdf.pdfocr.tesseract4.actions.data.PdfOcrTesseract4ProductData;
3840
import com.itextpdf.pdfocr.tesseract4.actions.events.PdfOcrTesseract4ProductEvent;
3941
import com.itextpdf.pdfocr.tesseract4.exceptions.Tesseract4OcrException;
@@ -138,14 +140,36 @@ public void createTxtFile(final List<File> inputImages, final File txtFile,
138140
Tesseract4LogMessageConstant.START_OCR_FOR_IMAGES,
139141
inputImages.size()));
140142

141-
StringBuilder content = new StringBuilder();
142-
for (File inputImage : inputImages) {
143-
content.append(doImageOcr(inputImage, OutputFormat.TXT, ocrProcessContext));
143+
AbstractPdfOcrEventHelper storedEventHelper;
144+
if (ocrProcessContext.getOcrEventHelper() == null) {
145+
storedEventHelper = new Tesseract4EventHelper();
146+
} else {
147+
storedEventHelper = ocrProcessContext.getOcrEventHelper();
144148
}
145149

146-
// write to file
147-
TesseractHelper.writeToTextFile(txtFile.getAbsolutePath(),
148-
content.toString());
150+
PdfOcrTesseract4ProductEvent event = PdfOcrTesseract4ProductEvent.createProcessImageEvent(
151+
storedEventHelper.getSequenceId(), null, storedEventHelper.getConfirmationType());
152+
storedEventHelper.onEvent(event);
153+
154+
try {
155+
// set Tesseract4FileResultEventHelper
156+
ocrProcessContext.setOcrEventHelper(new Tesseract4FileResultEventHelper(storedEventHelper));
157+
158+
StringBuilder content = new StringBuilder();
159+
for (File inputImage : inputImages) {
160+
content.append(doImageOcr(inputImage, OutputFormat.TXT, ocrProcessContext));
161+
}
162+
163+
// write to file
164+
TesseractHelper.writeToTextFile(txtFile.getAbsolutePath(),
165+
content.toString());
166+
167+
if (event.getConfirmationType() == EventConfirmationType.ON_DEMAND) {
168+
storedEventHelper.onEvent(new ConfirmEvent(event));
169+
}
170+
} finally {
171+
ocrProcessContext.setOcrEventHelper(storedEventHelper);
172+
}
149173
}
150174

151175
/**
@@ -235,7 +259,8 @@ public final String doImageOcr(final File input,
235259
final OcrProcessContext ocrProcessContext) {
236260
String result = "";
237261
verifyImageFormatValidity(input);
238-
ITesseractOcrResult processedData = processInputFiles(input, outputFormat, ocrProcessContext.getOcrEventHelper());
262+
ITesseractOcrResult processedData = processInputFiles(input, outputFormat,
263+
ocrProcessContext.getOcrEventHelper());
239264
if (processedData != null) {
240265
if (outputFormat.equals(OutputFormat.TXT)) {
241266
result = ((StringTesseractOcrResult)processedData).getData();

pdfocr-tesseract4/src/main/java/com/itextpdf/pdfocr/tesseract4/Tesseract4EventHelper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ This file is part of the iText (R) project.
3434
*/
3535
class Tesseract4EventHelper extends AbstractPdfOcrEventHelper {
3636

37+
Tesseract4EventHelper() {
38+
// do nothing
39+
}
40+
3741
@Override
3842
public void onEvent(AbstractProductITextEvent event) {
3943
if (event instanceof AbstractContextBasedITextEvent) {

pdfocr-tesseract4/src/main/java/com/itextpdf/pdfocr/tesseract4/Tesseract4ExecutableOcrEngine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ void doTesseractOcr(final File inputImage,
196196

197197
// confrim on_demand event
198198
if (event != null && event.getConfirmationType() == EventConfirmationType.ON_DEMAND) {
199-
EventManager.getInstance().onEvent(new ConfirmEvent(event));
199+
eventHelper.onEvent(new ConfirmEvent(event));
200200
}
201201
} catch (Tesseract4OcrException e) {
202202
LoggerFactory.getLogger(getClass())
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2021 iText Group NV
4+
Authors: iText Software.
5+
6+
This program is offered under a commercial and under the AGPL license.
7+
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
8+
9+
AGPL licensing:
10+
This program is free software: you can redistribute it and/or modify
11+
it under the terms of the GNU Affero General Public License as published by
12+
the Free Software Foundation, either version 3 of the License, or
13+
(at your option) any later version.
14+
15+
This program is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
GNU Affero General Public License for more details.
19+
20+
You should have received a copy of the GNU Affero General Public License
21+
along with this program. If not, see <https://www.gnu.org/licenses/>.
22+
*/
23+
package com.itextpdf.pdfocr.tesseract4;
24+
25+
import com.itextpdf.commons.actions.AbstractProductITextEvent;
26+
import com.itextpdf.commons.actions.confirmations.ConfirmEvent;
27+
import com.itextpdf.commons.actions.confirmations.EventConfirmationType;
28+
import com.itextpdf.commons.actions.sequence.SequenceId;
29+
import com.itextpdf.pdfocr.AbstractPdfOcrEventHelper;
30+
import com.itextpdf.pdfocr.tesseract4.actions.events.PdfOcrTesseract4ProductEvent;
31+
32+
/**
33+
* Helper class for working with events.
34+
*/
35+
class Tesseract4FileResultEventHelper extends AbstractPdfOcrEventHelper {
36+
37+
private AbstractPdfOcrEventHelper wrappedEventHelper;
38+
39+
Tesseract4FileResultEventHelper() {
40+
this(null);
41+
}
42+
43+
Tesseract4FileResultEventHelper(AbstractPdfOcrEventHelper wrappedEventHelper) {
44+
this.wrappedEventHelper = wrappedEventHelper == null ? new Tesseract4EventHelper() : wrappedEventHelper;
45+
}
46+
47+
@Override
48+
public void onEvent(AbstractProductITextEvent event) {
49+
if (!isProcessImageEvent(event)
50+
&& !isConfirmForProcessImageEvent(event)) {
51+
wrappedEventHelper.onEvent(event);
52+
}
53+
}
54+
55+
@Override
56+
public SequenceId getSequenceId() {
57+
return wrappedEventHelper.getSequenceId();
58+
}
59+
60+
@Override
61+
public EventConfirmationType getConfirmationType() {
62+
return wrappedEventHelper.getConfirmationType();
63+
}
64+
65+
private static boolean isProcessImageEvent(AbstractProductITextEvent event) {
66+
return event instanceof PdfOcrTesseract4ProductEvent
67+
&& PdfOcrTesseract4ProductEvent.PROCESS_IMAGE.equals(
68+
((PdfOcrTesseract4ProductEvent) event).getEventType());
69+
}
70+
71+
private static boolean isConfirmForProcessImageEvent(AbstractProductITextEvent event) {
72+
return event instanceof ConfirmEvent
73+
&& ((ConfirmEvent) event).getConfirmedEvent() instanceof PdfOcrTesseract4ProductEvent
74+
&& PdfOcrTesseract4ProductEvent.PROCESS_IMAGE.equals(
75+
((ConfirmEvent) event).getConfirmedEvent().getEventType());
76+
}
77+
}

0 commit comments

Comments
 (0)