diff --git a/gclc/src/main/java/net/bigeon/gclc/command/base/EchoCommand.java b/gclc/src/main/java/net/bigeon/gclc/command/base/EchoCommand.java index 9464a16..5e9bfd8 100644 --- a/gclc/src/main/java/net/bigeon/gclc/command/base/EchoCommand.java +++ b/gclc/src/main/java/net/bigeon/gclc/command/base/EchoCommand.java @@ -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. + *
+ * This command mimics the effect of the echo command in linux system. The + * difference is this command accepts java like formatting. + * + *
+ * echo.execute(out, in, "My Formatted {0}, with {1}", "message", "short arguments");
+ *
+ *
+ * @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]);
diff --git a/gclc/src/test/java/net/bigeon/gclc/command/base/EchoCommandTest.java b/gclc/src/test/java/net/bigeon/gclc/command/base/EchoCommandTest.java
new file mode 100644
index 0000000..96b04ab
--- /dev/null
+++ b/gclc/src/test/java/net/bigeon/gclc/command/base/EchoCommandTest.java
@@ -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
+ }
+ }
+
+}