Improve image resource management by ensuring BufferedImage objects are flushed after processing; add tests for cover extraction and image scaling (#1688)
Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>
This commit is contained in:
+42
-3
@@ -12,9 +12,13 @@ import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.awt.image.BufferedImage;
|
||||
import javax.imageio.ImageIO;
|
||||
import java.io.ByteArrayInputStream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class PdfMetadataExtractorTest {
|
||||
|
||||
@@ -51,12 +55,10 @@ class PdfMetadataExtractorTest {
|
||||
@Test
|
||||
void extractMetadata_shouldUseFilenameWithoutExtension_whenMetadataMissing() throws IOException {
|
||||
// Arrange: Create a PDF with NO metadata title
|
||||
// Name the file "Dune.pdf"
|
||||
File pdfFile = tempDir.resolve("Dune.pdf").toFile();
|
||||
|
||||
try (PDDocument doc = new PDDocument()) {
|
||||
doc.addPage(new PDPage());
|
||||
// explicitly leaving metadata empty
|
||||
doc.save(pdfFile);
|
||||
}
|
||||
|
||||
@@ -83,4 +85,41 @@ class PdfMetadataExtractorTest {
|
||||
// Assert
|
||||
assertEquals("Harry Potter and the Sorcerer's Stone", result.getTitle());
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractCover_validPdf_returnsJpegBytes() throws IOException {
|
||||
// Arrange: Create a simple one-page PDF
|
||||
File pdfFile = tempDir.resolve("cover-test.pdf").toFile();
|
||||
|
||||
try (PDDocument doc = new PDDocument()) {
|
||||
PDPage page = new PDPage();
|
||||
doc.addPage(page);
|
||||
doc.save(pdfFile);
|
||||
}
|
||||
|
||||
// Act
|
||||
byte[] coverBytes = extractor.extractCover(pdfFile);
|
||||
|
||||
// Assert: Should return non-null, decodable JPEG bytes
|
||||
assertNotNull(coverBytes);
|
||||
assertTrue(coverBytes.length > 0);
|
||||
|
||||
BufferedImage coverImage = ImageIO.read(new ByteArrayInputStream(coverBytes));
|
||||
assertNotNull(coverImage);
|
||||
assertTrue(coverImage.getWidth() > 0);
|
||||
assertTrue(coverImage.getHeight() > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractCover_invalidPdf_returnsNull() throws IOException {
|
||||
// Arrange: Create a file that looks like a PDF but isn't
|
||||
File invalidPdf = tempDir.resolve("invalid-pdf.pdf").toFile();
|
||||
java.nio.file.Files.writeString(invalidPdf.toPath(), "this is not a real pdf");
|
||||
|
||||
// Act
|
||||
byte[] coverBytes = extractor.extractCover(invalidPdf);
|
||||
|
||||
// Assert: Should return null for invalid PDF
|
||||
assertNull(coverBytes);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -449,7 +449,7 @@ class FileServiceTest {
|
||||
byte[] invalidData = "not an image".getBytes();
|
||||
Path outputPath = tempDir.resolve("invalid.jpg");
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () ->
|
||||
assertThrows(IOException.class, () ->
|
||||
fileService.saveImage(invalidData, outputPath.toString()));
|
||||
}
|
||||
|
||||
@@ -458,7 +458,7 @@ class FileServiceTest {
|
||||
byte[] emptyData = new byte[0];
|
||||
Path outputPath = tempDir.resolve("empty.jpg");
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () ->
|
||||
assertThrows(IOException.class, () ->
|
||||
fileService.saveImage(emptyData, outputPath.toString()));
|
||||
}
|
||||
|
||||
@@ -534,7 +534,6 @@ class FileServiceTest {
|
||||
|
||||
assertTrue(result);
|
||||
|
||||
// Verify the saved image has no transparency
|
||||
BufferedImage saved = ImageIO.read(
|
||||
new File(fileService.getCoverFile(3L)));
|
||||
assertFalse(saved.getColorModel().hasAlpha(), "Saved image should not have transparency");
|
||||
@@ -565,6 +564,54 @@ class FileServiceTest {
|
||||
() -> assertEquals(1200, saved.getHeight())
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void largeImage_isScaledDownToMaxDimensions() throws IOException {
|
||||
// Create a very large image that will trigger scaling
|
||||
int largeWidth = 2000; // > MAX_ORIGINAL_WIDTH (1000)
|
||||
int largeHeight = 3000; // > MAX_ORIGINAL_HEIGHT (1500)
|
||||
|
||||
BufferedImage largeImage = createTestImage(largeWidth, largeHeight);
|
||||
boolean result = fileService.saveCoverImages(largeImage, 5L);
|
||||
|
||||
assertTrue(result);
|
||||
|
||||
BufferedImage savedCover = ImageIO.read(
|
||||
new File(fileService.getCoverFile(5L)));
|
||||
|
||||
assertNotNull(savedCover);
|
||||
|
||||
assertTrue(savedCover.getWidth() <= 1000,
|
||||
"Cover width should be <= MAX_ORIGINAL_WIDTH (1000), was: " + savedCover.getWidth());
|
||||
assertTrue(savedCover.getHeight() <= 1500,
|
||||
"Cover height should be <= MAX_ORIGINAL_HEIGHT (1500), was: " + savedCover.getHeight());
|
||||
|
||||
double originalRatio = (double) largeWidth / largeHeight;
|
||||
double savedRatio = (double) savedCover.getWidth() / savedCover.getHeight();
|
||||
assertEquals(originalRatio, savedRatio, 0.01, "Aspect ratio should be preserved");
|
||||
}
|
||||
|
||||
@Test
|
||||
void smallImage_maintainsOriginalDimensions() throws IOException {
|
||||
// Create a small image that should NOT be scaled down
|
||||
int smallWidth = 400; // < MAX_ORIGINAL_WIDTH (1000)
|
||||
int smallHeight = 600; // < MAX_ORIGINAL_HEIGHT (1500)
|
||||
|
||||
BufferedImage smallImage = createTestImage(smallWidth, smallHeight);
|
||||
boolean result = fileService.saveCoverImages(smallImage, 6L);
|
||||
|
||||
assertTrue(result);
|
||||
|
||||
BufferedImage savedCover = ImageIO.read(
|
||||
new File(fileService.getCoverFile(6L)));
|
||||
|
||||
assertNotNull(savedCover);
|
||||
|
||||
assertEquals(smallWidth, savedCover.getWidth(),
|
||||
"Small image width should be preserved");
|
||||
assertEquals(smallHeight, savedCover.getHeight(),
|
||||
"Small image height should be preserved");
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
|
||||
Reference in New Issue
Block a user