Added comment and test
Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
parent
af0c8e91f8
commit
674333a42c
@ -43,10 +43,21 @@ import net.bigeon.gclc.exception.CommandRunException;
|
||||
import net.bigeon.gclc.manager.ConsoleInput;
|
||||
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 {
|
||||
|
||||
/** @param name the command name */
|
||||
/** Create the command.
|
||||
*
|
||||
* @param name the command name */
|
||||
public EchoCommand(final String 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.manager.ConsoleInput, java.lang.String[]) */
|
||||
@Override
|
||||
public void execute(final ConsoleOutput out, final ConsoleInput in, final String... args)
|
||||
throws CommandRunException {
|
||||
public void execute(final ConsoleOutput out, final ConsoleInput in,
|
||||
final String... args) throws CommandRunException {
|
||||
try {
|
||||
if (args.length == 1) {
|
||||
out.print(args[0]);
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user