From f8aeef14e36ab44ac66739c86dd718d883f007b5 Mon Sep 17 00:00:00 2001 From: Emmanuel Bigeon Date: Sun, 12 Jun 2016 22:36:09 -0400 Subject: [PATCH] Corrected ScriptExecution and added tests --- .../bigeon/gclc/command/ScriptExecution.java | 5 +- .../gclc/CommandTestingApplication.java | 83 +++++++++++++++++++ .../bigeon/gclc/ConsoleApplicationTest.java | 13 +++ .../gclc/command/ScriptExecutionTest.java | 50 +++++++++++ gclc/src/test/resources/script1.txt | 0 gclc/src/test/resources/script2.txt | 1 + gclc/src/test/resources/script3.txt | 1 + gclc/src/test/resources/script4.txt | 5 ++ gclc/src/test/resources/script5.txt | 2 + 9 files changed, 157 insertions(+), 3 deletions(-) create mode 100644 gclc/src/test/java/fr/bigeon/gclc/CommandTestingApplication.java create mode 100644 gclc/src/test/java/fr/bigeon/gclc/command/ScriptExecutionTest.java create mode 100644 gclc/src/test/resources/script1.txt create mode 100644 gclc/src/test/resources/script2.txt create mode 100644 gclc/src/test/resources/script3.txt create mode 100644 gclc/src/test/resources/script4.txt create mode 100644 gclc/src/test/resources/script5.txt diff --git a/gclc/src/main/java/fr/bigeon/gclc/command/ScriptExecution.java b/gclc/src/main/java/fr/bigeon/gclc/command/ScriptExecution.java index cd6f265..6d122c2 100644 --- a/gclc/src/main/java/fr/bigeon/gclc/command/ScriptExecution.java +++ b/gclc/src/main/java/fr/bigeon/gclc/command/ScriptExecution.java @@ -84,9 +84,8 @@ public class ScriptExecution extends Command { throw new CommandRunException(CommandRunExceptionType.USAGE, "Expecting a file", this); //$NON-NLS-1$ } - List argsList = Arrays.asList(args); - String scriptFile = argsList.remove(0); - Object[] params = argsList.toArray(); + String scriptFile = args[0]; + Object[] params = Arrays.copyOfRange(args, 1, args.length); try (FileReader fReader = new FileReader(new File(scriptFile)); BufferedReader reader = new BufferedReader(fReader)) { String cmd; diff --git a/gclc/src/test/java/fr/bigeon/gclc/CommandTestingApplication.java b/gclc/src/test/java/fr/bigeon/gclc/CommandTestingApplication.java new file mode 100644 index 0000000..86ecb22 --- /dev/null +++ b/gclc/src/test/java/fr/bigeon/gclc/CommandTestingApplication.java @@ -0,0 +1,83 @@ +/** + * gclc:fr.bigeon.gclc.command.CommandTestingApplication.java + * Created on: Jun 12, 2016 + */ +package fr.bigeon.gclc; + +import java.io.IOException; +import java.io.InputStream; +import java.io.PipedInputStream; +import java.io.PipedOutputStream; +import java.io.PrintWriter; + +import fr.bigeon.gclc.command.ICommand; +import fr.bigeon.gclc.exception.InvalidCommandName; +import fr.bigeon.gclc.manager.SystemConsoleManager; + +/** + *

+ * TODO + * + * @author Emmanuel Bigeon + * + */ +public class CommandTestingApplication { + + private final PrintWriter writer; + private final ConsoleTestApplication application; + private final Thread th; + + /** @throws IOException if the streams cannot be build */ + public CommandTestingApplication() throws IOException { + final PipedOutputStream src = new PipedOutputStream(); + InputStream in = new PipedInputStream(src); + + application = new ConsoleTestApplication( + new SystemConsoleManager(System.out, in)); + th = new Thread(new Runnable() { + + @Override + public void run() { + application.start(); + } + }); + + th.start(); + writer = new PrintWriter(src, true); + } + + public void stop() { + application.exit(); + writer.println(); + } + + public void sendCommand(String cmd) { + writer.println(cmd); + } + + /** @param cmd the command + * @return if the command was added + * @throws InvalidCommandName if the command name is invalid + * @see fr.bigeon.gclc.ConsoleApplication#add(fr.bigeon.gclc.command.ICommand) */ + public final boolean add(ICommand cmd) throws InvalidCommandName { + return application.add(cmd); + } + + /** @return the application */ + public ConsoleTestApplication getApplication() { + return application; + } + + /** + * + */ + public void waitAndStop() { + writer.println(ConsoleTestApplication.EXIT); + try { + th.join(); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } +} diff --git a/gclc/src/test/java/fr/bigeon/gclc/ConsoleApplicationTest.java b/gclc/src/test/java/fr/bigeon/gclc/ConsoleApplicationTest.java index 437e86d..d4aa070 100644 --- a/gclc/src/test/java/fr/bigeon/gclc/ConsoleApplicationTest.java +++ b/gclc/src/test/java/fr/bigeon/gclc/ConsoleApplicationTest.java @@ -69,6 +69,19 @@ public class ConsoleApplicationTest { @Test public void executionTest() { + try { + CommandTestingApplication application = new CommandTestingApplication(); + + application.sendCommand("test"); + application.sendCommand("toto"); + application.sendCommand("long"); + application.sendCommand("exit"); + + } catch (IOException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + try { final PipedOutputStream src = new PipedOutputStream(); InputStream in = new PipedInputStream(src); diff --git a/gclc/src/test/java/fr/bigeon/gclc/command/ScriptExecutionTest.java b/gclc/src/test/java/fr/bigeon/gclc/command/ScriptExecutionTest.java new file mode 100644 index 0000000..7b18233 --- /dev/null +++ b/gclc/src/test/java/fr/bigeon/gclc/command/ScriptExecutionTest.java @@ -0,0 +1,50 @@ +/** + * gclc:fr.bigeon.gclc.command.ScriptExecutionTest.java + * Created on: Jun 12, 2016 + */ +package fr.bigeon.gclc.command; + +import static org.junit.Assert.fail; + +import java.io.IOException; + +import org.junit.Test; + +import fr.bigeon.gclc.CommandTestingApplication; +import fr.bigeon.gclc.exception.InvalidCommandName; + +/** + *

+ * TODO + * + * @author Emmanuel Bigeon + * + */ +public class ScriptExecutionTest { + + /** + * Test method for {@link fr.bigeon.gclc.command.ScriptExecution#execute(java.lang.String[])}. + */ + @Test + public void testExecute() { + try { + CommandTestingApplication application = new CommandTestingApplication(); + + application.add(new ScriptExecution("script", + application.getApplication(), "#")); + + application.sendCommand("script src/test/resources/script1.txt"); + application.sendCommand("script src/test/resources/script2.txt"); + application.sendCommand("script src/test/resources/script3.txt"); + application.sendCommand("script src/test/resources/script4.txt"); + application + .sendCommand("script src/test/resources/script5.txt test"); + + application.waitAndStop(); + + } catch (IOException | InvalidCommandName e) { + fail("Unexpected exception"); + } + } + +} diff --git a/gclc/src/test/resources/script1.txt b/gclc/src/test/resources/script1.txt new file mode 100644 index 0000000..e69de29 diff --git a/gclc/src/test/resources/script2.txt b/gclc/src/test/resources/script2.txt new file mode 100644 index 0000000..5bafb5f --- /dev/null +++ b/gclc/src/test/resources/script2.txt @@ -0,0 +1 @@ +# test only comment \ No newline at end of file diff --git a/gclc/src/test/resources/script3.txt b/gclc/src/test/resources/script3.txt new file mode 100644 index 0000000..30d74d2 --- /dev/null +++ b/gclc/src/test/resources/script3.txt @@ -0,0 +1 @@ +test \ No newline at end of file diff --git a/gclc/src/test/resources/script4.txt b/gclc/src/test/resources/script4.txt new file mode 100644 index 0000000..5d1ac92 --- /dev/null +++ b/gclc/src/test/resources/script4.txt @@ -0,0 +1,5 @@ +# Test file with commands +test +# and comments +long +# interwined \ No newline at end of file diff --git a/gclc/src/test/resources/script5.txt b/gclc/src/test/resources/script5.txt new file mode 100644 index 0000000..6bf36ac --- /dev/null +++ b/gclc/src/test/resources/script5.txt @@ -0,0 +1,2 @@ +# A script with arguments +{0}