|
8 | 8 | import java.io.IOException; |
9 | 9 | import java.nio.file.Files; |
10 | 10 | import java.nio.file.Path; |
| 11 | +import java.util.stream.Stream; |
11 | 12 | import org.apache.commons.codec.binary.Base64; |
12 | 13 | import org.junit.jupiter.api.Assertions; |
13 | 14 | import org.junit.jupiter.api.Test; |
| 15 | +import org.junit.jupiter.params.ParameterizedTest; |
| 16 | +import org.junit.jupiter.params.provider.MethodSource; |
14 | 17 |
|
15 | 18 | public class LocalInputSourceTest { |
16 | 19 | void assertMultipagePDF(LocalInputSource inputSource, Path filePath) throws IOException { |
@@ -66,42 +69,59 @@ void loadPDF__withoutText_mustNotDetectSourceText() throws MindeeException, IOEx |
66 | 69 | Assertions.assertTrue(localInputSource.isPDF()); |
67 | 70 | } |
68 | 71 |
|
69 | | - void assertImage(LocalInputSource inputSource, Path filePath) throws IOException { |
| 72 | + void assertImage( |
| 73 | + LocalInputSource inputSource, |
| 74 | + Path filePath, |
| 75 | + String filename |
| 76 | + ) throws IOException { |
70 | 77 | Assertions.assertNotNull(inputSource); |
71 | 78 |
|
72 | 79 | Assertions.assertFalse(inputSource.isPDF()); |
73 | 80 | Assertions.assertEquals(1, inputSource.getPageCount()); |
74 | | - Assertions.assertEquals("receipt.jpg", inputSource.getFilename()); |
| 81 | + Assertions.assertEquals(filename, inputSource.getFilename()); |
75 | 82 | Assertions.assertArrayEquals(inputSource.getFile(), Files.readAllBytes(filePath)); |
76 | 83 | } |
77 | 84 |
|
78 | | - @Test |
79 | | - void loadImage_withFile_mustReturnAValidLocalInputSource() throws IOException { |
80 | | - File file = getResourcePath("file_types/receipt.jpg").toFile(); |
81 | | - var localInputSource = new LocalInputSource(file); |
82 | | - assertImage(localInputSource, file.toPath()); |
| 85 | + private static Stream<String> imageExtensions() { |
| 86 | + return Stream.of("heic", "heif", "jpg", "jpga", "png", "tif", "tiff", "webp"); |
83 | 87 | } |
84 | 88 |
|
85 | | - @Test |
86 | | - void loadImage_withInputStream_mustReturnAValidLocalInputSource() throws IOException { |
87 | | - Path filePath = getResourcePath("file_types/receipt.jpg"); |
88 | | - var localInputSource = new LocalInputSource(Files.newInputStream(filePath), "receipt.jpg"); |
89 | | - assertImage(localInputSource, filePath); |
| 89 | + @ParameterizedTest |
| 90 | + @MethodSource("imageExtensions") |
| 91 | + void loadImage_withFile_mustReturnAValidLocalInputSource(String extension) throws IOException { |
| 92 | + File file = getResourcePath("file_types/receipt." + extension).toFile(); |
| 93 | + var inputSource = new LocalInputSource(file); |
| 94 | + assertImage(inputSource, file.toPath(), "receipt." + extension); |
90 | 95 | } |
91 | 96 |
|
92 | | - @Test |
93 | | - void loadImage_withByteArray_mustReturnAValidLocalInputSource() throws IOException { |
94 | | - Path filePath = getResourcePath("file_types/receipt.jpg"); |
95 | | - var localInputSource = new LocalInputSource(Files.readAllBytes(filePath), "receipt.jpg"); |
96 | | - assertImage(localInputSource, filePath); |
| 97 | + @ParameterizedTest |
| 98 | + @MethodSource("imageExtensions") |
| 99 | + void loadImage_withInputStream_mustReturnAValidLocalInputSource( |
| 100 | + String extension |
| 101 | + ) throws IOException { |
| 102 | + Path filePath = getResourcePath("file_types/receipt." + extension); |
| 103 | + var inputSource = new LocalInputSource(Files.newInputStream(filePath), "receipt." + extension); |
| 104 | + assertImage(inputSource, filePath, "receipt." + extension); |
| 105 | + } |
| 106 | + |
| 107 | + @ParameterizedTest |
| 108 | + @MethodSource("imageExtensions") |
| 109 | + void loadImage_withByteArray_mustReturnAValidLocalInputSource( |
| 110 | + String extension |
| 111 | + ) throws IOException { |
| 112 | + Path filePath = getResourcePath("file_types/receipt." + extension); |
| 113 | + var inputSource = new LocalInputSource(Files.readAllBytes(filePath), "receipt." + extension); |
| 114 | + assertImage(inputSource, filePath, "receipt." + extension); |
97 | 115 | } |
98 | 116 |
|
99 | 117 | @Test |
100 | 118 | void loadImage_withBase64Encoded_mustReturnAValidLocalInputSource() throws IOException { |
101 | | - Path filePath = getResourcePath("file_types/receipt.jpg"); |
102 | | - String encodedFile = Base64.encodeBase64String(Files.readAllBytes(filePath)); |
103 | | - var localInputSource = new LocalInputSource(encodedFile, "receipt.jpg"); |
104 | | - assertImage(localInputSource, filePath); |
| 119 | + Path filePath = getResourcePath("file_types/receipt.txt"); |
| 120 | + String encodedFile = Files.readString(filePath, java.nio.charset.StandardCharsets.UTF_8); |
| 121 | + var inputSource = new LocalInputSource(encodedFile, "receipt.jpg"); |
| 122 | + Assertions.assertFalse(inputSource.isPDF()); |
| 123 | + Assertions.assertEquals(1, inputSource.getPageCount()); |
| 124 | + Assertions.assertEquals("receipt.jpg", inputSource.getFilename()); |
105 | 125 | } |
106 | 126 |
|
107 | 127 | } |
0 commit comments