-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Consider image's type from its data rather than its extension
DEVSIX-5172
- Loading branch information
Showing
4 changed files
with
170 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
pdfocr-api/src/test/java/com/itextpdf/pdfocr/PdfCreatorUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
This file is part of the iText (R) project. | ||
Copyright (c) 1998-2021 iText Group NV | ||
Authors: iText Software. | ||
This program is offered under a commercial and under the AGPL license. | ||
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below. | ||
AGPL licensing: | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Affero General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Affero General Public License for more details. | ||
You should have received a copy of the GNU Affero General Public License | ||
along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package com.itextpdf.pdfocr; | ||
|
||
import com.itextpdf.io.image.ImageData; | ||
import com.itextpdf.io.image.ImageType; | ||
import com.itextpdf.io.image.JpegImageData; | ||
import com.itextpdf.io.image.TiffImageData; | ||
import com.itextpdf.io.util.MessageFormatUtil; | ||
import com.itextpdf.pdfocr.helpers.PdfHelper; | ||
import com.itextpdf.test.ExtendedITextTest; | ||
import com.itextpdf.test.annotations.LogMessage; | ||
import com.itextpdf.test.annotations.LogMessages; | ||
import com.itextpdf.test.annotations.type.UnitTest; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.List; | ||
import org.junit.Assert; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.experimental.categories.Category; | ||
import org.junit.rules.ExpectedException; | ||
|
||
@Category(UnitTest.class) | ||
public class PdfCreatorUtilTest extends ExtendedITextTest { | ||
|
||
@Rule | ||
public ExpectedException junitExpectedException = ExpectedException.none(); | ||
|
||
@Test | ||
public void getImageDataFromValidSinglePagedTiffTest() throws IOException { | ||
File image = new File(PdfHelper.getImagesTestDirectory() + "single7x5cm.tif"); | ||
List<ImageData> images = PdfCreatorUtil.getImageData(image, null); | ||
|
||
Assert.assertEquals(1, images.size()); | ||
|
||
ImageData imageDate = images.get(0); | ||
Assert.assertNotNull(imageDate); | ||
Assert.assertTrue(imageDate instanceof TiffImageData); | ||
Assert.assertEquals(ImageType.TIFF, imageDate.getOriginalType()); | ||
} | ||
|
||
@Test | ||
public void getImageDataFromValidMultiPagedTiffTest() throws IOException { | ||
File image = new File(PdfHelper.getImagesTestDirectory() + "multipage.tiff"); | ||
List<ImageData> images = PdfCreatorUtil.getImageData(image, null); | ||
|
||
Assert.assertEquals(9, images.size()); | ||
for (ImageData imageDate : images) { | ||
Assert.assertNotNull(imageDate); | ||
Assert.assertTrue(imageDate instanceof TiffImageData); | ||
Assert.assertEquals(ImageType.TIFF, imageDate.getOriginalType()); | ||
} | ||
} | ||
|
||
@Test | ||
public void getImageDataFromValidNotTiffTest() throws IOException { | ||
File image = new File(PdfHelper.getImagesTestDirectory() + "numbers_01.jpg"); | ||
List<ImageData> images = PdfCreatorUtil.getImageData(image, null); | ||
|
||
Assert.assertEquals(1, images.size()); | ||
|
||
ImageData imageDate = images.get(0); | ||
Assert.assertNotNull(imageDate); | ||
Assert.assertTrue(imageDate instanceof JpegImageData); | ||
Assert.assertEquals(ImageType.JPEG, imageDate.getOriginalType()); | ||
} | ||
|
||
@Test | ||
@LogMessages(messages = { | ||
@LogMessage(messageTemplate = PdfOcrLogMessageConstant.CANNOT_READ_INPUT_IMAGE) | ||
}) | ||
public void getImageDataFromNotExistingImageTest() throws IOException { | ||
junitExpectedException.expect(OcrException.class); | ||
|
||
PdfCreatorUtil.getImageData(new File("no such path"), null); | ||
} | ||
|
||
@Test | ||
@LogMessages(messages = { | ||
@LogMessage(messageTemplate = PdfOcrLogMessageConstant.CANNOT_READ_INPUT_IMAGE) | ||
}) | ||
public void getImageDataFromInvalidImageTest() throws IOException { | ||
junitExpectedException.expect(OcrException.class); | ||
junitExpectedException.expectMessage(MessageFormatUtil.format( | ||
OcrException.CANNOT_READ_INPUT_IMAGE)); | ||
|
||
PdfCreatorUtil.getImageData(new File(PdfHelper.getImagesTestDirectory() + "corrupted.jpg"), | ||
null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+12.2 KB
pdfocr-api/src/test/resources/com/itextpdf/pdfocr/images/single7x5cm.tif
Binary file not shown.