Added comment and test

Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
Emmanuel Bigeon 2018-10-26 10:09:33 -04:00
parent af0c8e91f8
commit 674333a42c
2 changed files with 59 additions and 4 deletions

View File

@ -43,10 +43,21 @@ import net.bigeon.gclc.exception.CommandRunException;
import net.bigeon.gclc.manager.ConsoleInput; import net.bigeon.gclc.manager.ConsoleInput;
import net.bigeon.gclc.manager.ConsoleOutput; import net.bigeon.gclc.manager.ConsoleOutput;
/** @author Emmanuel Bigeon */ /** A command printing test to the console.
* <p>
* This command mimics the effect of the echo command in linux system. The
* difference is this command accepts java like formatting.
*
* <pre>
* echo.execute(out, in, "My Formatted {0}, with {1}", "message", "short arguments");
* </pre>
*
* @author Emmanuel Bigeon */
public class EchoCommand extends Command { public class EchoCommand extends Command {
/** @param name the command name */ /** Create the command.
*
* @param name the command name */
public EchoCommand(final String name) { public EchoCommand(final String name) {
super(name); super(name);
} }
@ -56,8 +67,8 @@ public class EchoCommand extends Command {
* fr.bigeon.gclc.command.ICommand#execute(fr.bigeon.gclc.manager.ConsoleOutput, * fr.bigeon.gclc.command.ICommand#execute(fr.bigeon.gclc.manager.ConsoleOutput,
* fr.bigeon.gclc.manager.ConsoleInput, java.lang.String[]) */ * fr.bigeon.gclc.manager.ConsoleInput, java.lang.String[]) */
@Override @Override
public void execute(final ConsoleOutput out, final ConsoleInput in, final String... args) public void execute(final ConsoleOutput out, final ConsoleInput in,
throws CommandRunException { final String... args) throws CommandRunException {
try { try {
if (args.length == 1) { if (args.length == 1) {
out.print(args[0]); out.print(args[0]);

View File

@ -0,0 +1,44 @@
package net.bigeon.gclc.command.base;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import java.io.IOException;
import org.junit.Test;
import net.bigeon.gclc.exception.CommandRunException;
import net.bigeon.gclc.utils.PipedConsoleOutput;
public class EchoCommandTest {
@Test
public void testExecute() throws IOException, CommandRunException {
final EchoCommand echo = new EchoCommand("echo");
assertNotNull("Help tip should be specified", echo.tip());
assertNotNull("Usage should be specified", echo.usageDetail());
try (PipedConsoleOutput out = new PipedConsoleOutput()) {
echo.execute(out, null);
assertEquals("Echo should print a line", "", out.readNextLine());
echo.execute(out, null, "Message");
assertEquals("Echo should print argument", "Message", out.readNextLine());
echo.execute(out, null, "Message {0}", "with parameter");
assertEquals("Echo should format string and print", "Message with parameter",
out.readNextLine());
}
final PipedConsoleOutput out = new PipedConsoleOutput();
out.close();
try {
echo.execute(out, null);
fail("Should not be able to print in closed outputs");
} catch (final CommandRunException e) {
// expected
}
}
}