Back to Repositories

Validating PDF Utility Functions in Stirling-PDF

This test suite validates core PDF utility functions in the Stirling-PDF project, focusing on page size conversions and image detection capabilities. The tests ensure reliable PDF manipulation operations through comprehensive unit testing.

Test Coverage Overview

The test suite covers essential PDF utility functions with specific focus on page size conversion and image presence detection.

Key areas tested include:
  • Page size conversion from text to PDRectangle objects
  • Image detection within PDF pages
  • Edge case handling for invalid page sizes
  • Resource management verification

Implementation Analysis

The implementation utilizes JUnit 5 framework with Mockito for mocking PDF components. The testing approach combines direct assertion testing for size conversions with mock-based testing for image detection scenarios.

Notable patterns include:
  • Mock object creation for PDPage and PDResources
  • Exception testing for invalid inputs
  • Behavior verification using Mockito when/then patterns

Technical Details

Testing infrastructure includes:
  • JUnit Jupiter for test execution
  • Mockito framework for PDF object mocking
  • Apache PDFBox library integration
  • Custom utility methods from PdfUtils class

Best Practices Demonstrated

The test suite demonstrates strong testing practices through isolated unit tests and comprehensive mock usage. Each test method focuses on a single functionality with clear assertions and error cases covered.

Notable practices include:
  • Proper test method naming conventions
  • Thorough mock setup and verification
  • Clear separation of test scenarios
  • Exception path testing

stirling-tools/stirling-pdf

src/test/java/stirling/software/SPDF/utils/PdfUtilsTest.java

            
package stirling.software.SPDF.utils;

import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.*;

public class PdfUtilsTest {

    @Test
    void testTextToPageSize() {
        assertEquals(PDRectangle.A4, PdfUtils.textToPageSize("A4"));
        assertEquals(PDRectangle.LETTER, PdfUtils.textToPageSize("LETTER"));
        assertThrows(IllegalArgumentException.class, () -> PdfUtils.textToPageSize("INVALID"));
    }

    @Test
    void testHasImagesOnPage() throws IOException {
        // Mock a PDPage and its resources
        PDPage page = Mockito.mock(PDPage.class);
        PDResources resources = Mockito.mock(PDResources.class);
        Mockito.when(page.getResources()).thenReturn(resources);

        // Case 1: No images in resources
        Mockito.when(resources.getXObjectNames()).thenReturn(Collections.emptySet());
        assertFalse(PdfUtils.hasImagesOnPage(page));

        // Case 2: Resources with an image
        Set<COSName> xObjectNames = new HashSet<>();
        COSName cosName = Mockito.mock(COSName.class);
        xObjectNames.add(cosName);

        PDImageXObject imageXObject = Mockito.mock(PDImageXObject.class);
        Mockito.when(resources.getXObjectNames()).thenReturn(xObjectNames);
        Mockito.when(resources.getXObject(cosName)).thenReturn(imageXObject);

        assertTrue(PdfUtils.hasImagesOnPage(page));
    }


}