Skip to content

Commit 241053d

Browse files
Improve code coverage
DEVSIX-5706
1 parent 1fdec34 commit 241053d

18 files changed

Lines changed: 692 additions & 25 deletions

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
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+
*/
123
package com.itextpdf.pdfocr;
224

325
import com.itextpdf.commons.actions.AbstractITextEvent;

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ This file is part of the iText (R) project.
2323
package com.itextpdf.pdfocr;
2424

2525
import com.itextpdf.commons.actions.EventManager;
26-
import com.itextpdf.commons.actions.data.ProductData;
2726
import com.itextpdf.commons.actions.sequence.SequenceId;
2827
import com.itextpdf.commons.utils.MessageFormatUtil;
2928
import com.itextpdf.io.font.otf.ActualTextIterator;
@@ -58,8 +57,8 @@ This file is part of the iText (R) project.
5857
import com.itextpdf.layout.font.FontProvider;
5958
import com.itextpdf.layout.properties.TextAlignment;
6059
import com.itextpdf.pdfa.PdfADocument;
61-
import com.itextpdf.pdfocr.statisctics.PdfOcrOutputType;
62-
import com.itextpdf.pdfocr.statisctics.PdfOcrOutputTypeStatisticsEvent;
60+
import com.itextpdf.pdfocr.statistics.PdfOcrOutputType;
61+
import com.itextpdf.pdfocr.statistics.PdfOcrOutputTypeStatisticsEvent;
6362

6463
import java.io.File;
6564
import java.io.IOException;
@@ -69,8 +68,6 @@ This file is part of the iText (R) project.
6968
import org.slf4j.Logger;
7069
import org.slf4j.LoggerFactory;
7170

72-
;
73-
7471
/**
7572
* {@link OcrPdfCreator} is the class that creates PDF documents containing input
7673
* images and text that was recognized using provided {@link IOcrEngine}.
@@ -461,7 +458,7 @@ private PdfDocument createPdfDocument(final PdfWriter pdfWriter,
461458
new PdfOcrOutputTypeStatisticsEvent(eventType, ((IProductAware) ocrEngine).getProductData());
462459
EventManager.getInstance().onEvent(docTypeStatisticsEvent);
463460
}
464-
461+
465462
return pdfDocument;
466463
}
467464

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ This file is part of the iText (R) project.
2828
import com.itextpdf.commons.actions.confirmations.EventConfirmationType;
2929
import com.itextpdf.commons.actions.contexts.IMetaInfo;
3030
import com.itextpdf.commons.actions.sequence.SequenceId;
31-
import com.itextpdf.pdfocr.statisctics.PdfOcrOutputTypeStatisticsEvent;
31+
import com.itextpdf.pdfocr.statistics.PdfOcrOutputTypeStatisticsEvent;
3232

3333
class OcrPdfCreatorEventHelper extends AbstractPdfOcrEventHelper {
3434
private final SequenceId sequenceId;

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
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+
*/
123
package com.itextpdf.pdfocr;
224

325
/**

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
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+
*/
123
package com.itextpdf.pdfocr;
224

325
import com.itextpdf.commons.actions.contexts.IMetaInfo;

pdfocr-api/src/main/java/com/itextpdf/pdfocr/statisctics/PdfOcrOutputType.java renamed to pdfocr-api/src/main/java/com/itextpdf/pdfocr/statistics/PdfOcrOutputType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ This file is part of the iText (R) project.
2020
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
*/
23-
package com.itextpdf.pdfocr.statisctics;
23+
package com.itextpdf.pdfocr.statistics;
2424

2525
/**
2626
* pdfOcr output types for statistics.

pdfocr-api/src/main/java/com/itextpdf/pdfocr/statisctics/PdfOcrOutputTypeStatisticsAggregator.java renamed to 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
@@ -20,7 +20,7 @@ This file is part of the iText (R) project.
2020
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
*/
23-
package com.itextpdf.pdfocr.statisctics;
23+
package com.itextpdf.pdfocr.statistics;
2424

2525
import com.itextpdf.commons.actions.AbstractStatisticsAggregator;
2626
import com.itextpdf.commons.actions.AbstractStatisticsEvent;

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ This file is part of the iText (R) project.
2020
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
*/
23-
package com.itextpdf.pdfocr.statisctics;
23+
package com.itextpdf.pdfocr.statistics;
2424

2525
import com.itextpdf.commons.actions.AbstractStatisticsAggregator;
2626
import com.itextpdf.commons.actions.AbstractStatisticsEvent;
@@ -29,7 +29,6 @@ This file is part of the iText (R) project.
2929

3030
import java.util.Collections;
3131
import java.util.List;
32-
import java.util.Objects;
3332

3433
/**
3534
* Class which represents an event for specifying type of an ocr processing.

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

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

25+
import com.itextpdf.commons.actions.contexts.IMetaInfo;
2526
import com.itextpdf.io.image.ImageData;
2627
import com.itextpdf.kernel.colors.DeviceRgb;
2728
import com.itextpdf.kernel.font.PdfFont;
2829
import com.itextpdf.kernel.geom.Rectangle;
30+
import com.itextpdf.kernel.pdf.DocumentProperties;
31+
import com.itextpdf.kernel.pdf.PdfAConformanceLevel;
32+
import com.itextpdf.kernel.pdf.PdfDocument;
33+
import com.itextpdf.kernel.pdf.PdfReader;
34+
import com.itextpdf.kernel.pdf.PdfWriter;
35+
import com.itextpdf.kernel.pdf.canvas.parser.PdfTextExtractor;
36+
import com.itextpdf.pdfa.PdfADocument;
2937
import com.itextpdf.pdfocr.helpers.CustomOcrEngine;
38+
import com.itextpdf.pdfocr.helpers.CustomProductAwareOcrEngine;
3039
import com.itextpdf.pdfocr.helpers.ExtractionStrategy;
3140
import com.itextpdf.pdfocr.helpers.PdfHelper;
3241
import com.itextpdf.test.ExtendedITextTest;
@@ -35,12 +44,15 @@ This file is part of the iText (R) project.
3544
import com.itextpdf.test.annotations.type.IntegrationTest;
3645

3746
import java.io.File;
47+
import java.io.FileNotFoundException;
3848
import java.io.IOException;
49+
import java.nio.charset.StandardCharsets;
3950
import java.util.Arrays;
4051
import java.util.Collections;
4152
import java.util.List;
4253
import java.util.Map;
4354
import org.junit.Assert;
55+
import org.junit.BeforeClass;
4456
import org.junit.Rule;
4557
import org.junit.Test;
4658
import org.junit.experimental.categories.Category;
@@ -49,9 +61,119 @@ This file is part of the iText (R) project.
4961
@Category(IntegrationTest.class)
5062
public class ApiTest extends ExtendedITextTest {
5163

64+
public static final String DESTINATION_FOLDER = "./target/test/com/itextpdf/pdfocr";
65+
66+
@BeforeClass
67+
public static void beforeClass() {
68+
createOrClearDestinationFolder(DESTINATION_FOLDER);
69+
}
70+
5271
@Rule
5372
public ExpectedException junitExpectedException = ExpectedException.none();
5473

74+
@Test
75+
public void createPdfWithFileTest() {
76+
OcrPdfCreatorProperties props = new OcrPdfCreatorProperties()
77+
.setMetaInfo(new DummyMetaInfo());
78+
OcrPdfCreator pdfCreator = new OcrPdfCreator(new CustomOcrEngine(), props);
79+
try (PdfDocument pdf = pdfCreator.createPdf(
80+
Collections.<File>singletonList(new File(PdfHelper.getDefaultImagePath())),
81+
PdfHelper.getPdfWriter(),
82+
new DocumentProperties().setEventCountingMetaInfo(new DummyMetaInfo())
83+
)) {
84+
String contentBytes = new String(pdf.getPage(1).getContentBytes(), StandardCharsets.UTF_8);
85+
Assert.assertTrue(contentBytes.contains("<00190014001c001400150014>"));
86+
}
87+
}
88+
89+
@Test
90+
public void createPdfFileWithFileTest() throws IOException {
91+
String output = DESTINATION_FOLDER + "createPdfFileWithFileTest.pdf";
92+
93+
OcrPdfCreatorProperties props = new OcrPdfCreatorProperties()
94+
.setMetaInfo(new DummyMetaInfo());
95+
OcrPdfCreator pdfCreator = new OcrPdfCreator(new CustomOcrEngine(), props);
96+
pdfCreator.createPdfFile(
97+
Collections.<File>singletonList(new File(PdfHelper.getDefaultImagePath())),
98+
new File(output));
99+
100+
try (PdfDocument pdf = new PdfDocument(new PdfReader(output))) {
101+
String contentBytes = new String(pdf.getPage(1).getContentBytes(), StandardCharsets.UTF_8);
102+
Assert.assertTrue(contentBytes.contains("<00190014001c001400150014>"));
103+
}
104+
}
105+
106+
@Test
107+
public void createPdfAWithFileTest() throws FileNotFoundException {
108+
OcrPdfCreatorProperties props = new OcrPdfCreatorProperties()
109+
.setMetaInfo(new DummyMetaInfo())
110+
.setPdfLang("en-US");
111+
OcrPdfCreator pdfCreator = new OcrPdfCreator(new CustomOcrEngine(), props);
112+
try (PdfDocument pdf = pdfCreator.createPdfA(
113+
Collections.<File>singletonList(new File(PdfHelper.getDefaultImagePath())),
114+
PdfHelper.getPdfWriter(),
115+
new DocumentProperties().setEventCountingMetaInfo(new DummyMetaInfo()),
116+
PdfHelper.getRGBPdfOutputIntent()
117+
)) {
118+
String contentBytes = new String(pdf.getPage(1).getContentBytes(), StandardCharsets.UTF_8);
119+
Assert.assertTrue(contentBytes.contains("<00190014001c001400150014>"));
120+
Assert.assertTrue(pdf instanceof PdfADocument);
121+
}
122+
}
123+
124+
@Test
125+
public void createPdfAFileWithFileTest() throws IOException {
126+
String output = DESTINATION_FOLDER + "createPdfAFileWithFileTest.pdf";
127+
OcrPdfCreatorProperties props = new OcrPdfCreatorProperties()
128+
.setMetaInfo(new DummyMetaInfo())
129+
.setPdfLang("en-US");
130+
OcrPdfCreator pdfCreator = new OcrPdfCreator(new CustomOcrEngine(), props);
131+
pdfCreator.createPdfAFile(
132+
Collections.<File>singletonList(new File(PdfHelper.getDefaultImagePath())),
133+
new File(output),
134+
PdfHelper.getRGBPdfOutputIntent());
135+
try (PdfDocument pdf = new PdfDocument(new PdfReader(output))) {
136+
String contentBytes = new String(pdf.getPage(1).getContentBytes(), StandardCharsets.UTF_8);
137+
Assert.assertTrue(contentBytes.contains("<00190014001c001400150014>"));
138+
PdfAConformanceLevel cl = pdf.getReader().getPdfAConformanceLevel();
139+
Assert.assertEquals(PdfAConformanceLevel.PDF_A_3U.getConformance(), cl.getConformance());
140+
Assert.assertEquals(PdfAConformanceLevel.PDF_A_3U.getPart(), cl.getPart());
141+
}
142+
}
143+
144+
@Test
145+
public void createPdfAFileWithFileNoMetaTest() throws IOException {
146+
String output = DESTINATION_FOLDER + "createPdfAFileWithFileNoMetaTest.pdf";
147+
OcrPdfCreatorProperties props = new OcrPdfCreatorProperties()
148+
.setPdfLang("en-US");
149+
OcrPdfCreator pdfCreator = new OcrPdfCreator(new CustomOcrEngine(), props);
150+
pdfCreator.createPdfAFile(
151+
Collections.<File>singletonList(new File(PdfHelper.getDefaultImagePath())),
152+
new File(output),
153+
PdfHelper.getRGBPdfOutputIntent());
154+
try (PdfDocument pdf = new PdfDocument(new PdfReader(output))) {
155+
String contentBytes = new String(pdf.getPage(1).getContentBytes(), StandardCharsets.UTF_8);
156+
Assert.assertTrue(contentBytes.contains("<00190014001c001400150014>"));
157+
PdfAConformanceLevel cl = pdf.getReader().getPdfAConformanceLevel();
158+
Assert.assertEquals(PdfAConformanceLevel.PDF_A_3U.getConformance(), cl.getConformance());
159+
Assert.assertEquals(PdfAConformanceLevel.PDF_A_3U.getPart(), cl.getPart());
160+
}
161+
}
162+
163+
@Test
164+
public void createPdfAFileWithFileProductAwareEngineTest() throws IOException {
165+
String output = DESTINATION_FOLDER + "createPdfAFileWithFileProductAwareEngineTest.pdf";
166+
OcrPdfCreatorProperties props = new OcrPdfCreatorProperties()
167+
.setPdfLang("en-US");
168+
CustomProductAwareOcrEngine ocrEngine = new CustomProductAwareOcrEngine();
169+
OcrPdfCreator pdfCreator = new OcrPdfCreator(ocrEngine, props);
170+
pdfCreator.createPdfAFile(
171+
Collections.<File>singletonList(new File(PdfHelper.getDefaultImagePath())),
172+
new File(output),
173+
PdfHelper.getRGBPdfOutputIntent());
174+
175+
Assert.assertTrue(ocrEngine.isGetMetaInfoContainerTriggered());
176+
}
55177

56178
@Test
57179
public void testTextInfo() {
@@ -130,4 +252,7 @@ public ImageData applyRotation(ImageData imageData) {
130252
throw new RuntimeException("applyRotation is not implemented");
131253
}
132254
}
255+
256+
private static class DummyMetaInfo implements IMetaInfo {
257+
}
133258
}

0 commit comments

Comments
 (0)