# Description This pull request includes several changes aimed at improving the code structure and removing redundant code. The most significant changes involve reordering methods, removing unnecessary annotations, and refactoring constructors to use dependency injection. Autowired now comes via constructor (which also doesn't need autowired annotation as its done by default for configuration) ## Checklist - [ ] I have read the [Contribution Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md) - [ ] I have performed a self-review of my own code - [ ] I have attached images of the change if it is UI based - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] If my code has heavily changed functionality I have updated relevant docs on [Stirling-PDFs doc repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/) - [ ] My changes generate no new warnings - [ ] I have read the section [Add New Translation Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md#add-new-translation-tags) (for new translation tags only)
56 lines
1.9 KiB
Java
56 lines
1.9 KiB
Java
package stirling.software.SPDF.utils;
|
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
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);
|
|
}
|
|
}
|