|
| 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.io.image.ImageData; |
| 26 | +import com.itextpdf.io.image.ImageType; |
| 27 | +import com.itextpdf.io.image.JpegImageData; |
| 28 | +import com.itextpdf.io.image.TiffImageData; |
| 29 | +import com.itextpdf.io.util.MessageFormatUtil; |
| 30 | +import com.itextpdf.pdfocr.helpers.PdfHelper; |
| 31 | +import com.itextpdf.test.ExtendedITextTest; |
| 32 | +import com.itextpdf.test.annotations.LogMessage; |
| 33 | +import com.itextpdf.test.annotations.LogMessages; |
| 34 | +import com.itextpdf.test.annotations.type.UnitTest; |
| 35 | + |
| 36 | +import java.io.File; |
| 37 | +import java.io.IOException; |
| 38 | +import java.util.List; |
| 39 | +import org.junit.Assert; |
| 40 | +import org.junit.Rule; |
| 41 | +import org.junit.Test; |
| 42 | +import org.junit.experimental.categories.Category; |
| 43 | +import org.junit.rules.ExpectedException; |
| 44 | + |
| 45 | +@Category(UnitTest.class) |
| 46 | +public class PdfCreatorUtilTest extends ExtendedITextTest { |
| 47 | + |
| 48 | + @Rule |
| 49 | + public ExpectedException junitExpectedException = ExpectedException.none(); |
| 50 | + |
| 51 | + @Test |
| 52 | + public void getImageDataFromValidSinglePagedTiffTest() throws IOException { |
| 53 | + File image = new File(PdfHelper.getImagesTestDirectory() + "single7x5cm.tif"); |
| 54 | + List<ImageData> images = PdfCreatorUtil.getImageData(image, null); |
| 55 | + |
| 56 | + Assert.assertEquals(1, images.size()); |
| 57 | + |
| 58 | + ImageData imageDate = images.get(0); |
| 59 | + Assert.assertNotNull(imageDate); |
| 60 | + Assert.assertTrue(imageDate instanceof TiffImageData); |
| 61 | + Assert.assertEquals(ImageType.TIFF, imageDate.getOriginalType()); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void getImageDataFromValidMultiPagedTiffTest() throws IOException { |
| 66 | + File image = new File(PdfHelper.getImagesTestDirectory() + "multipage.tiff"); |
| 67 | + List<ImageData> images = PdfCreatorUtil.getImageData(image, null); |
| 68 | + |
| 69 | + Assert.assertEquals(9, images.size()); |
| 70 | + for (ImageData imageDate : images) { |
| 71 | + Assert.assertNotNull(imageDate); |
| 72 | + Assert.assertTrue(imageDate instanceof TiffImageData); |
| 73 | + Assert.assertEquals(ImageType.TIFF, imageDate.getOriginalType()); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void getImageDataFromValidNotTiffTest() throws IOException { |
| 79 | + File image = new File(PdfHelper.getImagesTestDirectory() + "numbers_01.jpg"); |
| 80 | + List<ImageData> images = PdfCreatorUtil.getImageData(image, null); |
| 81 | + |
| 82 | + Assert.assertEquals(1, images.size()); |
| 83 | + |
| 84 | + ImageData imageDate = images.get(0); |
| 85 | + Assert.assertNotNull(imageDate); |
| 86 | + Assert.assertTrue(imageDate instanceof JpegImageData); |
| 87 | + Assert.assertEquals(ImageType.JPEG, imageDate.getOriginalType()); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + @LogMessages(messages = { |
| 92 | + @LogMessage(messageTemplate = PdfOcrLogMessageConstant.CANNOT_READ_INPUT_IMAGE) |
| 93 | + }) |
| 94 | + public void getImageDataFromNotExistingImageTest() throws IOException { |
| 95 | + junitExpectedException.expect(OcrException.class); |
| 96 | + |
| 97 | + PdfCreatorUtil.getImageData(new File("no such path"), null); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + @LogMessages(messages = { |
| 102 | + @LogMessage(messageTemplate = PdfOcrLogMessageConstant.CANNOT_READ_INPUT_IMAGE) |
| 103 | + }) |
| 104 | + public void getImageDataFromInvalidImageTest() throws IOException { |
| 105 | + junitExpectedException.expect(OcrException.class); |
| 106 | + junitExpectedException.expectMessage(MessageFormatUtil.format( |
| 107 | + OcrException.CANNOT_READ_INPUT_IMAGE)); |
| 108 | + |
| 109 | + PdfCreatorUtil.getImageData(new File(PdfHelper.getImagesTestDirectory() + "corrupted.jpg"), |
| 110 | + null); |
| 111 | + } |
| 112 | +} |
0 commit comments