Corrected ScriptExecution and added tests

This commit is contained in:
Emmanuel Bigeon 2016-06-12 22:36:09 -04:00
parent 763b7361ec
commit f8aeef14e3
9 changed files with 157 additions and 3 deletions

View File

@ -84,9 +84,8 @@ public class ScriptExecution extends Command {
throw new CommandRunException(CommandRunExceptionType.USAGE, throw new CommandRunException(CommandRunExceptionType.USAGE,
"Expecting a file", this); //$NON-NLS-1$ "Expecting a file", this); //$NON-NLS-1$
} }
List<String> argsList = Arrays.asList(args); String scriptFile = args[0];
String scriptFile = argsList.remove(0); Object[] params = Arrays.copyOfRange(args, 1, args.length);
Object[] params = argsList.toArray();
try (FileReader fReader = new FileReader(new File(scriptFile)); try (FileReader fReader = new FileReader(new File(scriptFile));
BufferedReader reader = new BufferedReader(fReader)) { BufferedReader reader = new BufferedReader(fReader)) {
String cmd; String cmd;

View File

@ -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;
/**
* <p>
* 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();
}
}
}

View File

@ -69,6 +69,19 @@ public class ConsoleApplicationTest {
@Test @Test
public void executionTest() { 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 { try {
final PipedOutputStream src = new PipedOutputStream(); final PipedOutputStream src = new PipedOutputStream();
InputStream in = new PipedInputStream(src); InputStream in = new PipedInputStream(src);

View File

@ -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;
/**
* <p>
* 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");
}
}
}

View File

View File

@ -0,0 +1 @@
# test only comment

View File

@ -0,0 +1 @@
test

View File

@ -0,0 +1,5 @@
# Test file with commands
test
# and comments
long
# interwined

View File

@ -0,0 +1,2 @@
# A script with arguments
{0}