Corrected ScriptExecution and added tests
This commit is contained in:
parent
763b7361ec
commit
f8aeef14e3
@ -84,9 +84,8 @@ public class ScriptExecution extends Command {
|
||||
throw new CommandRunException(CommandRunExceptionType.USAGE,
|
||||
"Expecting a file", this); //$NON-NLS-1$
|
||||
}
|
||||
List<String> 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;
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
0
gclc/src/test/resources/script1.txt
Normal file
0
gclc/src/test/resources/script1.txt
Normal file
1
gclc/src/test/resources/script2.txt
Normal file
1
gclc/src/test/resources/script2.txt
Normal file
@ -0,0 +1 @@
|
||||
# test only comment
|
1
gclc/src/test/resources/script3.txt
Normal file
1
gclc/src/test/resources/script3.txt
Normal file
@ -0,0 +1 @@
|
||||
test
|
5
gclc/src/test/resources/script4.txt
Normal file
5
gclc/src/test/resources/script4.txt
Normal file
@ -0,0 +1,5 @@
|
||||
# Test file with commands
|
||||
test
|
||||
# and comments
|
||||
long
|
||||
# interwined
|
2
gclc/src/test/resources/script5.txt
Normal file
2
gclc/src/test/resources/script5.txt
Normal file
@ -0,0 +1,2 @@
|
||||
# A script with arguments
|
||||
{0}
|
Loading…
Reference in New Issue
Block a user