Added test for command

Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
Emmanuel Bigeon 2018-10-15 10:19:39 -04:00
parent 3f8cee20e1
commit 2145473706
2 changed files with 45 additions and 3 deletions

View File

@ -89,8 +89,7 @@ public class ExecSystemCommand extends Command {
try { try {
proc = Runtime.getRuntime().exec(args); proc = Runtime.getRuntime().exec(args);
} catch (final IOException e2) { } catch (final IOException e2) {
LOGGER.log(Level.SEVERE, "Unable to run process", e2); //$NON-NLS-1$ throw new CommandRunException("Unable to run process", e2);
return;
} }
final InputStream is = proc.getInputStream(); final InputStream is = proc.getInputStream();
@ -114,8 +113,10 @@ public class ExecSystemCommand extends Command {
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os))) { try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os))) {
while (th.isAlive()) { while (th.isAlive()) {
String user = in.prompt(); String user = in.prompt();
if (!user.isEmpty()) {
writer.write(user + EOL); writer.write(user + EOL);
} }
}
} catch (final IOException e1) { } catch (final IOException e1) {
throw new CommandRunException(CommandRunExceptionType.INTERACTION, throw new CommandRunException(CommandRunExceptionType.INTERACTION,
MANAGER_WAS_CLOSED, e1); // $NON-NLS-1$ MANAGER_WAS_CLOSED, e1); // $NON-NLS-1$

View File

@ -0,0 +1,41 @@
package net.bigeon.gclc.system;
import static org.junit.Assert.*;
import org.junit.Test;
import net.bigeon.gclc.exception.CommandRunException;
import net.bigeon.gclc.manager.ConsoleInput;
import net.bigeon.gclc.manager.ConsoleOutput;
import net.bigeon.gclc.utils.EmptyInput;
import net.bigeon.gclc.utils.SinkOutput;
public class ExecSystemCommandTest {
@Test
public void testExecSystemCommand() {
ExecSystemCommand cmd = new ExecSystemCommand("test");
assertEquals("Name should be preserved", "test", cmd.getCommandName());
assertNotNull("tip should be defined", cmd.tip());
assertNotNull("usage should be defined", cmd.usagePattern());
assertNotNull("usage should be defined", cmd.usageDetail());
}
@Test
public void testExecute() throws CommandRunException {
ConsoleOutput out = SinkOutput.INSTANCE;
ConsoleInput in = EmptyInput.INSTANCE;
ExecSystemCommand cmd = new ExecSystemCommand();
if (System.getProperty("os.name").contains("indows")) {
cmd.execute(out, in, "cmd", "/C", "dir");
try {
cmd.execute(out, in, "inexistent");
fail("Able to execute inexistent command in system");
} catch (CommandRunException e) {
// ok
}
}
}
}