-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1385 from jimdouk/main
Enhance Test Coverage for SPDF/Utils Classes
- Loading branch information
Showing
10 changed files
with
580 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
src/test/java/stirling/software/SPDF/utils/ErrorUtilsTest.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,45 @@ | ||
package stirling.software.SPDF.utils; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.ui.Model; | ||
import org.springframework.web.servlet.ModelAndView; | ||
|
||
public class ErrorUtilsTest { | ||
|
||
@Test | ||
public void testExceptionToModel() { | ||
// Create a mock Model | ||
Model model = new org.springframework.ui.ExtendedModelMap(); | ||
|
||
// Create a test exception | ||
Exception ex = new Exception("Test Exception"); | ||
|
||
// Call the method under test | ||
Model resultModel = ErrorUtils.exceptionToModel(model, ex); | ||
|
||
// Verify the result | ||
assertNotNull(resultModel); | ||
assertEquals("Test Exception", resultModel.getAttribute("errorMessage")); | ||
assertNotNull(resultModel.getAttribute("stackTrace")); | ||
} | ||
|
||
@Test | ||
public void testExceptionToModelView() { | ||
// Create a mock Model | ||
Model model = new org.springframework.ui.ExtendedModelMap(); | ||
|
||
// Create a test exception | ||
Exception ex = new Exception("Test Exception"); | ||
|
||
// Call the method under test | ||
ModelAndView modelAndView = ErrorUtils.exceptionToModelView(model, ex); | ||
|
||
// Verify the result | ||
assertNotNull(modelAndView); | ||
assertEquals("Test Exception", modelAndView.getModel().get("errorMessage")); | ||
assertNotNull(modelAndView.getModel().get("stackTrace")); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/test/java/stirling/software/SPDF/utils/FileToPdfTest.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,36 @@ | ||
package stirling.software.SPDF.utils; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import java.io.IOException; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import stirling.software.SPDF.model.api.converters.HTMLToPdfRequest; | ||
|
||
public class FileToPdfTest { | ||
|
||
@Test | ||
public void testConvertHtmlToPdf() { | ||
HTMLToPdfRequest request = new HTMLToPdfRequest(); | ||
byte[] fileBytes = new byte[10]; // Sample file bytes | ||
String fileName = "test.html"; // Sample file name | ||
boolean htmlFormatsInstalled = true; // Sample boolean value | ||
|
||
// Check if the method throws IOException | ||
assertThrows(IOException.class, () -> { | ||
FileToPdf.convertHtmlToPdf(request, fileBytes, fileName, htmlFormatsInstalled); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testConvertBookTypeToPdf() { | ||
byte[] bytes = new byte[10]; // Sample bytes | ||
String originalFilename = "test.epub"; // Sample original filename | ||
|
||
// Check if the method throws IOException | ||
assertThrows(IOException.class, () -> { | ||
FileToPdf.convertBookTypeToPdf(bytes, originalFilename); | ||
}); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/test/java/stirling/software/SPDF/utils/ImageProcessingUtilsTest.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,76 @@ | ||
package stirling.software.SPDF.utils; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.awt.image.BufferedImage; | ||
import java.awt.Color; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class ImageProcessingUtilsTest { | ||
|
||
@Test | ||
void testConvertColorTypeToGreyscale() { | ||
BufferedImage sourceImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); | ||
fillImageWithColor(sourceImage, Color.RED); | ||
|
||
BufferedImage convertedImage = ImageProcessingUtils.convertColorType(sourceImage, "greyscale"); | ||
|
||
assertNotNull(convertedImage); | ||
assertEquals(BufferedImage.TYPE_BYTE_GRAY, convertedImage.getType()); | ||
assertEquals(sourceImage.getWidth(), convertedImage.getWidth()); | ||
assertEquals(sourceImage.getHeight(), convertedImage.getHeight()); | ||
|
||
// Check if a pixel is correctly converted to greyscale | ||
Color grey = new Color(convertedImage.getRGB(0, 0)); | ||
assertEquals(grey.getRed(), grey.getGreen()); | ||
assertEquals(grey.getGreen(), grey.getBlue()); | ||
} | ||
|
||
@Test | ||
void testConvertColorTypeToBlackWhite() { | ||
BufferedImage sourceImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); | ||
fillImageWithColor(sourceImage, Color.RED); | ||
|
||
BufferedImage convertedImage = ImageProcessingUtils.convertColorType(sourceImage, "blackwhite"); | ||
|
||
assertNotNull(convertedImage); | ||
assertEquals(BufferedImage.TYPE_BYTE_BINARY, convertedImage.getType()); | ||
assertEquals(sourceImage.getWidth(), convertedImage.getWidth()); | ||
assertEquals(sourceImage.getHeight(), convertedImage.getHeight()); | ||
|
||
// Check if a pixel is converted correctly (binary image will be either black or white) | ||
int rgb = convertedImage.getRGB(0, 0); | ||
assertTrue(rgb == Color.BLACK.getRGB() || rgb == Color.WHITE.getRGB()); | ||
} | ||
|
||
@Test | ||
void testConvertColorTypeToFullColor() { | ||
BufferedImage sourceImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); | ||
fillImageWithColor(sourceImage, Color.RED); | ||
|
||
BufferedImage convertedImage = ImageProcessingUtils.convertColorType(sourceImage, "fullcolor"); | ||
|
||
assertNotNull(convertedImage); | ||
assertEquals(sourceImage, convertedImage); | ||
} | ||
|
||
@Test | ||
void testConvertColorTypeInvalid() { | ||
BufferedImage sourceImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); | ||
fillImageWithColor(sourceImage, Color.RED); | ||
|
||
BufferedImage convertedImage = ImageProcessingUtils.convertColorType(sourceImage, "invalidtype"); | ||
|
||
assertNotNull(convertedImage); | ||
assertEquals(sourceImage, convertedImage); | ||
} | ||
|
||
private void fillImageWithColor(BufferedImage image, Color color) { | ||
for (int y = 0; y < image.getHeight(); y++) { | ||
for (int x = 0; x < image.getWidth(); x++) { | ||
image.setRGB(x, y, color.getRGB()); | ||
} | ||
} | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/test/java/stirling/software/SPDF/utils/PdfUtilsTest.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,60 @@ | ||
package stirling.software.SPDF.utils; | ||
|
||
import org.apache.pdfbox.pdmodel.PDDocument; | ||
import org.apache.pdfbox.pdmodel.PDPage; | ||
import org.apache.pdfbox.pdmodel.PDResources; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
import stirling.software.SPDF.model.PdfMetadata; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import org.apache.pdfbox.pdmodel.common.PDRectangle; | ||
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject; | ||
import org.apache.pdfbox.cos.COSName; | ||
|
||
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)); | ||
} | ||
|
||
@Test | ||
void testExtractMetadataFromPdf() throws IOException { | ||
PDDocument document = Mockito.mock(PDDocument.class); | ||
Mockito.when(document.getDocumentInformation()).thenReturn(Mockito.mock(org.apache.pdfbox.pdmodel.PDDocumentInformation.class)); | ||
PdfMetadata metadata = PdfUtils.extractMetadataFromPdf(document); | ||
|
||
assertNotNull(metadata); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/test/java/stirling/software/SPDF/utils/ProcessExecutorTest.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,55 @@ | ||
package stirling.software.SPDF.utils; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class ProcessExecutorTest { | ||
|
||
private ProcessExecutor processExecutor; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
// Initialize the ProcessExecutor instance | ||
processExecutor = ProcessExecutor.getInstance(ProcessExecutor.Processes.LIBRE_OFFICE); | ||
} | ||
|
||
@Test | ||
public void testRunCommandWithOutputHandling() throws IOException, InterruptedException { | ||
// Mock the command to execute | ||
List<String> command = new ArrayList<>(); | ||
command.add("java"); | ||
command.add("-version"); | ||
|
||
// Execute the command | ||
ProcessExecutor.ProcessExecutorResult result = processExecutor.runCommandWithOutputHandling(command); | ||
|
||
// Check the exit code and output messages | ||
assertEquals(0, result.getRc()); | ||
assertNotNull(result.getMessages()); // Check if messages are not null | ||
} | ||
|
||
@Test | ||
public void testRunCommandWithOutputHandling_Error() { | ||
// Mock the command to execute | ||
List<String> command = new ArrayList<>(); | ||
command.add("nonexistent-command"); | ||
|
||
// Execute the command and expect an IOException | ||
IOException thrown = assertThrows(IOException.class, () -> { | ||
processExecutor.runCommandWithOutputHandling(command); | ||
}); | ||
|
||
// Log the actual error message | ||
System.out.println("Caught IOException: " + thrown.getMessage()); | ||
|
||
// Check the exception message to ensure it indicates the command was not found | ||
String errorMessage = thrown.getMessage(); | ||
assertTrue(errorMessage.contains("error=2") || errorMessage.contains("No such file or directory"), "Unexpected error message: " + errorMessage); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/test/java/stirling/software/SPDF/utils/PropertyConfigsTest.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,69 @@ | ||
package stirling.software.SPDF.utils; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class PropertyConfigsTest { | ||
|
||
@Test | ||
public void testGetBooleanValue_WithKeys() { | ||
// Define keys and default value | ||
List<String> keys = Arrays.asList("test.key1", "test.key2", "test.key3"); | ||
boolean defaultValue = false; | ||
|
||
// Set property for one of the keys | ||
System.setProperty("test.key2", "true"); | ||
|
||
// Call the method under test | ||
boolean result = PropertyConfigs.getBooleanValue(keys, defaultValue); | ||
|
||
// Verify the result | ||
assertEquals(true, result); | ||
} | ||
|
||
@Test | ||
public void testGetStringValue_WithKeys() { | ||
// Define keys and default value | ||
List<String> keys = Arrays.asList("test.key1", "test.key2", "test.key3"); | ||
String defaultValue = "default"; | ||
|
||
// Set property for one of the keys | ||
System.setProperty("test.key2", "value"); | ||
|
||
// Call the method under test | ||
String result = PropertyConfigs.getStringValue(keys, defaultValue); | ||
|
||
// Verify the result | ||
assertEquals("value", result); | ||
} | ||
|
||
@Test | ||
public void testGetBooleanValue_WithKey() { | ||
// Define key and default value | ||
String key = "test.key"; | ||
boolean defaultValue = true; | ||
|
||
// Call the method under test | ||
boolean result = PropertyConfigs.getBooleanValue(key, defaultValue); | ||
|
||
// Verify the result | ||
assertEquals(true, result); | ||
} | ||
|
||
@Test | ||
public void testGetStringValue_WithKey() { | ||
// Define key and default value | ||
String key = "test.key"; | ||
String defaultValue = "default"; | ||
|
||
// Call the method under test | ||
String result = PropertyConfigs.getStringValue(key, defaultValue); | ||
|
||
// Verify the result | ||
assertEquals("default", result); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/test/java/stirling/software/SPDF/utils/RequestUriUtilsTest.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,26 @@ | ||
package stirling.software.SPDF.utils; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class RequestUriUtilsTest { | ||
|
||
@Test | ||
public void testIsStaticResource() { | ||
assertTrue(RequestUriUtils.isStaticResource("/css/styles.css")); | ||
assertTrue(RequestUriUtils.isStaticResource("/js/script.js")); | ||
assertTrue(RequestUriUtils.isStaticResource("/images/logo.png")); | ||
assertTrue(RequestUriUtils.isStaticResource("/public/index.html")); | ||
assertTrue(RequestUriUtils.isStaticResource("/pdfjs/pdf.worker.js")); | ||
assertTrue(RequestUriUtils.isStaticResource("/api/v1/info/status")); | ||
assertTrue(RequestUriUtils.isStaticResource("/some-path/icon.svg")); | ||
assertFalse(RequestUriUtils.isStaticResource("/api/v1/users")); | ||
assertFalse(RequestUriUtils.isStaticResource("/api/v1/orders")); | ||
assertFalse(RequestUriUtils.isStaticResource("/")); | ||
assertFalse(RequestUriUtils.isStaticResource("/login")); | ||
assertFalse(RequestUriUtils.isStaticResource("/register")); | ||
assertFalse(RequestUriUtils.isStaticResource("/api/v1/products")); | ||
} | ||
} |
Oops, something went wrong.