2024-06-06 12:54:12 +03:00
|
|
|
package stirling.software.SPDF.utils;
|
|
|
|
|
2025-03-13 10:22:44 +01:00
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
2024-06-06 12:54:12 +03:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2025-03-13 10:22:44 +01:00
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
|
|
import org.junit.jupiter.api.Test;
|
2024-06-06 12:54:12 +03:00
|
|
|
|
|
|
|
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
|
2025-03-13 10:22:44 +01:00
|
|
|
ProcessExecutor.ProcessExecutorResult result =
|
|
|
|
processExecutor.runCommandWithOutputHandling(command);
|
2024-06-06 12:54:12 +03:00
|
|
|
|
|
|
|
// 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
|
2025-03-13 10:22:44 +01:00
|
|
|
IOException thrown =
|
|
|
|
assertThrows(
|
|
|
|
IOException.class,
|
|
|
|
() -> {
|
|
|
|
processExecutor.runCommandWithOutputHandling(command);
|
|
|
|
});
|
2024-06-06 12:54:12 +03:00
|
|
|
|
2024-06-07 10:38:05 +03:00
|
|
|
// Check the exception message to ensure it indicates the command was not found
|
|
|
|
String errorMessage = thrown.getMessage();
|
2025-03-13 10:22:44 +01:00
|
|
|
assertTrue(
|
|
|
|
errorMessage.contains("error=2")
|
|
|
|
|| errorMessage.contains("No such file or directory"),
|
|
|
|
"Unexpected error message: " + errorMessage);
|
2024-06-06 12:54:12 +03:00
|
|
|
}
|
|
|
|
}
|