diff --git a/gclc.system/pom.xml b/gclc.system/pom.xml
index c3b3e56..146249c 100644
--- a/gclc.system/pom.xml
+++ b/gclc.system/pom.xml
@@ -36,6 +36,6 @@
diff --git a/gclc/src/main/java/fr/bigeon/gclc/command/CommandParameters.java b/gclc/src/main/java/fr/bigeon/gclc/command/CommandParameters.java
index aec0b64..d30f3d2 100644
--- a/gclc/src/main/java/fr/bigeon/gclc/command/CommandParameters.java
+++ b/gclc/src/main/java/fr/bigeon/gclc/command/CommandParameters.java
@@ -56,9 +56,9 @@ public class CommandParameters {
*/
private static final int STRINGARG_NUMBER_OF_ELEMENTS = 2;
/** Boolean arguments */
- private final Map
+ * This class is intended for testing purpose only.
+ *
+ * @author Emmanuel Bigeon */
+public final class MockCommand implements ICommand {
+
+ /** The command name */
+ private final String name;
+
+ /** @param name the command name */
+ public MockCommand(String name) {
+ this.name = name;
+ }
+
+ /* (non-Javadoc)
+ * @see fr.bigeon.gclc.command.ICommand#execute(java.lang.String[]) */
+ @Override
+ public void execute(String... args) throws CommandRunException {
+ //
+ }
+
+ /* (non-Javadoc)
+ * @see fr.bigeon.gclc.command.ICommand#tip() */
+ @Override
+ public String tip() {
+ return null;
+ }
+
+ /* (non-Javadoc)
+ * @see fr.bigeon.gclc.command.ICommand#getCommandName() */
+ @Override
+ public String getCommandName() {
+ return name;
+ }
+
+ /* (non-Javadoc)
+ * @see fr.bigeon.gclc.command.ICommand#help(fr.bigeon.gclc.manager.
+ * ConsoleManager, java.lang.String[]) */
+ @Override
+ public void help(ConsoleManager manager,
+ String... args) throws IOException {
+ //
+ }
+
+}
diff --git a/gclc/src/main/java/fr/bigeon/gclc/command/ParametrizedCommand.java b/gclc/src/main/java/fr/bigeon/gclc/command/ParametrizedCommand.java
index 77f995a..5186f06 100644
--- a/gclc/src/main/java/fr/bigeon/gclc/command/ParametrizedCommand.java
+++ b/gclc/src/main/java/fr/bigeon/gclc/command/ParametrizedCommand.java
@@ -40,12 +40,17 @@ package fr.bigeon.gclc.command;
import java.io.IOException;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
import fr.bigeon.gclc.exception.CommandRunException;
import fr.bigeon.gclc.exception.CommandRunExceptionType;
+import fr.bigeon.gclc.exception.InvalidParameterException;
import fr.bigeon.gclc.manager.ConsoleManager;
/**
@@ -61,7 +66,7 @@ public abstract class ParametrizedCommand extends Command {
/** The manager */
protected final ConsoleManager manager;
/** The boolean parameters mandatory status */
- private final Map
* Add a parameter to the defined parameters
*
* @param param the parameter identification
- * @param stringOrBool if the parameter is a parameter with an argument
- * @param needed if the parameter is required */
- @SuppressWarnings("boxing")
- protected void addParameter(String param, boolean stringOrBool,
- boolean needed) {
+ * @param stringParameter if the parameter is a parameter with an argument
+ * @param needed if the parameter is required
+ * @throws InvalidParameterException if the parameter was invalid */
+ protected void addParameter(String param, boolean stringParameter,
+ boolean needed) throws InvalidParameterException {
if (params.containsKey(param)) {
+ checkParam(param, stringParameter, needed);
return;
}
- params.put(param, needed);
- if (stringOrBool) {
- stringParams.put(param, needed);
+ if (stringParameter) {
+ stringParams.put(param, Boolean.valueOf(needed));
+ params.put(param, Boolean.valueOf(needed));
} else {
if (needed) {
// ERROR the boolean parameters cannot be needed
+ throw new InvalidParameterException(
+ "Boolean parameter are present by their very nature. They should not be defined as needed"); //$NON-NLS-1$
}
- boolParams.put(param, needed);
+ boolParams.add(param);
+ params.put(param, Boolean.valueOf(false));
+ }
+ }
+
+ /** @param param the parameter
+ * @param stringParameter the string parameter type
+ * @param needed if the parameter is needed
+ * @throws InvalidParameterException if the new definition is invalid */
+ private void checkParam(String param, boolean stringParameter,
+ boolean needed) throws InvalidParameterException {
+ if (stringParameter) {
+ if (stringParams.containsKey(param)) {
+ Boolean need = Boolean.valueOf(
+ needed || stringParams.get(param).booleanValue());
+ stringParams.put(param, need);
+ params.put(param, need);
+ return;
+ }
+ throw new InvalidParameterException(
+ "Parameter is already defined as boolean"); //$NON-NLS-1$
+ }
+ if (stringParams.containsKey(param) || needed) {
+ throw new InvalidParameterException(
+ "Parameter is already defined as string"); //$NON-NLS-1$
}
}
@@ -120,19 +163,22 @@ public abstract class ParametrizedCommand extends Command {
@Override
public final void execute(String... args) throws CommandRunException {
final CommandParameters parameters = new CommandParameters(
- boolParams.keySet(), stringParams.keySet(), strict);
+ boolParams, stringParams.keySet(), strict);
if (!parameters.parseArgs(args)) {
- // ERROR the parameters could not be correctly parsed
+ // the parameters could not be correctly parsed
throw new CommandRunException(CommandRunExceptionType.USAGE,
"Unable to read arguments", this); //$NON-NLS-1$
}
final List
- * TODO
+/** The command run exception possible types
*
- * @author Emmanuel Bigeon
- *
- */
+ * @author Emmanuel Bigeon */
public enum CommandRunExceptionType {
/** Type of exception due to a wrong usage */
USAGE,
diff --git a/gclc/src/main/java/fr/bigeon/gclc/exception/InvalidCommandName.java b/gclc/src/main/java/fr/bigeon/gclc/exception/InvalidCommandName.java
index 8e7d46f..307170b 100644
--- a/gclc/src/main/java/fr/bigeon/gclc/exception/InvalidCommandName.java
+++ b/gclc/src/main/java/fr/bigeon/gclc/exception/InvalidCommandName.java
@@ -40,7 +40,7 @@ package fr.bigeon.gclc.exception;
/**
* Exception sent from the application when a command is added but the name of
- * the command
+ * the command is already used
*
* @author Emmanuel BIGEON */
public class InvalidCommandName extends Exception {
@@ -50,4 +50,25 @@ public class InvalidCommandName extends Exception {
*/
private static final long serialVersionUID = 1L;
+ /** Default constructor */
+ public InvalidCommandName() {
+ super();
+ }
+
+ /** @param message the message
+ * @param cause the cause */
+ public InvalidCommandName(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ /** @param message the message */
+ public InvalidCommandName(String message) {
+ super(message);
+ }
+
+ /** @param cause the cause */
+ public InvalidCommandName(Throwable cause) {
+ super(cause);
+ }
+
}
diff --git a/gclc/src/main/java/fr/bigeon/gclc/exception/InvalidParameterException.java b/gclc/src/main/java/fr/bigeon/gclc/exception/InvalidParameterException.java
new file mode 100644
index 0000000..0fb61bf
--- /dev/null
+++ b/gclc/src/main/java/fr/bigeon/gclc/exception/InvalidParameterException.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright Bigeon Emmanuel (2014)
+ *
+ * emmanuel@bigeon.fr
+ *
+ * This software is a computer program whose purpose is to
+ * provide a generic framework for console applications.
+ *
+ * This software is governed by the CeCILL license under French law and
+ * abiding by the rules of distribution of free software. You can use,
+ * modify and/or redistribute the software under the terms of the CeCILL
+ * license as circulated by CEA, CNRS and INRIA at the following URL
+ * "http://www.cecill.info".
+ *
+ * As a counterpart to the access to the source code and rights to copy,
+ * modify and redistribute granted by the license, users are provided only
+ * with a limited warranty and the software's author, the holder of the
+ * economic rights, and the successive licensors have only limited
+ * liability.
+ *
+ * In this respect, the user's attention is drawn to the risks associated
+ * with loading, using, modifying and/or developing or reproducing the
+ * software by the user in light of its specific status of free software,
+ * that may mean that it is complicated to manipulate, and that also
+ * therefore means that it is reserved for developers and experienced
+ * professionals having in-depth computer knowledge. Users are therefore
+ * encouraged to load and test the software's suitability as regards their
+ * requirements in conditions enabling the security of their systems and/or
+ * data to be ensured and, more generally, to use and operate it in the
+ * same conditions as regards security.
+ *
+ * The fact that you are presently reading this means that you have had
+ * knowledge of the CeCILL license and that you accept its terms.
+ */
+/**
+ * gclc:fr.bigeon.gclc.exception.InvalidParameterException.java
+ * Created on: Nov 19, 2016
+ */
+package fr.bigeon.gclc.exception;
+
+/** This exception is thrown during command definitions to indicate a wrong
+ * parameter definition.
+ *
+ * This class is particularly used by
+ * {@link fr.bigeon.gclc.command.ParametrizedCommand parameterized commands}.
+ *
+ * @author Emmanuel Bigeon */
+public class InvalidParameterException extends Exception {
+
+ /**
+ *
+ */
+ private static final long serialVersionUID = 1L;
+
+ /** @param message the message
+ * @param cause the cause */
+ public InvalidParameterException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ /** @param message the message */
+ public InvalidParameterException(String message) {
+ super(message);
+ }
+
+ /** @param cause the cause */
+ public InvalidParameterException(Throwable cause) {
+ super(cause);
+ }
+
+}
diff --git a/gclc/src/main/java/fr/bigeon/gclc/manager/SystemConsoleManager.java b/gclc/src/main/java/fr/bigeon/gclc/manager/SystemConsoleManager.java
index a8986ca..0151454 100644
--- a/gclc/src/main/java/fr/bigeon/gclc/manager/SystemConsoleManager.java
+++ b/gclc/src/main/java/fr/bigeon/gclc/manager/SystemConsoleManager.java
@@ -43,6 +43,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
+import java.nio.charset.Charset;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -54,12 +55,15 @@ import java.util.logging.Logger;
public class SystemConsoleManager implements ConsoleManager { // NOSONAR
/** The default prompt */
- public static final String DEFAULT_PROMPT = ">"; //$NON-NLS-1$
+ public static final String DEFAULT_PROMPT = "> "; //$NON-NLS-1$
/** The logger */
private static final Logger LOGGER = Logger
.getLogger(SystemConsoleManager.class.getName());
+ /** The empty string constant */
+ private static final String EMPTY = ""; //$NON-NLS-1$
+
/** The command prompt. It can be changed. */
private String prompt = DEFAULT_PROMPT;
@@ -74,15 +78,17 @@ public class SystemConsoleManager implements ConsoleManager { // NOSONAR
/** This default constructor relies on the system defined standart output
* and input stream. */
public SystemConsoleManager() {
- this(System.out, System.in);
+ this(System.out, System.in, Charset.defaultCharset());
}
/** @param out the output stream
- * @param in the input stream */
- public SystemConsoleManager(PrintStream out, InputStream in) {
+ * @param in the input stream
+ * @param charset the charset for the input */
+ public SystemConsoleManager(PrintStream out, InputStream in,
+ Charset charset) {
super();
this.out = out;
- this.in = new BufferedReader(new InputStreamReader(in));
+ this.in = new BufferedReader(new InputStreamReader(in, charset));
}
/** @return the prompt */
@@ -95,19 +101,22 @@ public class SystemConsoleManager implements ConsoleManager { // NOSONAR
* @see fr.bigeon.gclc.ConsoleManager#print(java.lang.Object) */
@Override
public void print(String object) throws IOException {
+ checkOpen();
+ out.print(object);
+ }
+
+ /** @throws IOException if the stream was closed */
+ private void checkOpen() throws IOException {
if (closed) {
throw new IOException();
}
- out.print(object);
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.ConsoleManager#println() */
@Override
public void println() throws IOException {
- if (closed) {
- throw new IOException();
- }
+ checkOpen();
out.println();
}
@@ -115,9 +124,7 @@ public class SystemConsoleManager implements ConsoleManager { // NOSONAR
* @see fr.bigeon.gclc.ConsoleManager#println(java.lang.Object) */
@Override
public void println(String object) throws IOException {
- if (closed) {
- throw new IOException();
- }
+ checkOpen();
out.println(object);
}
@@ -125,20 +132,22 @@ public class SystemConsoleManager implements ConsoleManager { // NOSONAR
* @see fr.bigeon.gclc.ConsoleManager#prompt() */
@Override
public String prompt() throws IOException {
- return prompt(new String() + prompt);
+ return prompt(prompt);
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.ConsoleManager#prompt(java.lang.String) */
@Override
public String prompt(String message) throws IOException {
- if (closed) {
- throw new IOException();
- }
- String result = new String();
- out.print(message + ' ');
+ checkOpen();
+ String result = EMPTY;
+ out.print(message);
try {
result = in.readLine();
+ while (result != null && result.length() > 0 &&
+ result.charAt(0) == 0) {
+ result = result.substring(1);
+ }
} catch (final IOException e) {
LOGGER.log(Level.SEVERE, "Unable to read prompt", e); //$NON-NLS-1$
}
diff --git a/gclc/src/main/java/fr/bigeon/gclc/prompt/CLIPrompter.java b/gclc/src/main/java/fr/bigeon/gclc/prompt/CLIPrompter.java
index 268c78d..311fb3a 100644
--- a/gclc/src/main/java/fr/bigeon/gclc/prompt/CLIPrompter.java
+++ b/gclc/src/main/java/fr/bigeon/gclc/prompt/CLIPrompter.java
@@ -90,9 +90,9 @@ public class CLIPrompter {
manager.println(index++ + ") " + u); //$NON-NLS-1$
}
if (cancel != null) {
- manager.println(index + ") " + cancel); //$NON-NLS-1$
+ manager.println(index++ + ") " + cancel); //$NON-NLS-1$
}
- return index;
+ return index - 1;
}
/** @param manager the manager
@@ -311,10 +311,10 @@ public class CLIPrompter {
String ender) throws IOException {
manager.println(message + CLIPrompterMessages
.getString("promptlongtext.exit.dispkey", ender)); //$NON-NLS-1$
- String res = manager.prompt(PROMPT);
+ String res = manager.prompt(CLIPrompterMessages.getString(PROMPT));
String line = res;
while (!line.equals(ender)) {
- line = manager.prompt(PROMPT);
+ line = manager.prompt(CLIPrompterMessages.getString(PROMPT));
if (!line.equals(ender)) {
res += System.lineSeparator() + line;
}
@@ -356,7 +356,7 @@ public class CLIPrompter {
final List
@@ -56,20 +52,16 @@ import fr.bigeon.gclc.manager.SystemConsoleManager;
*
*/
@SuppressWarnings("javadoc")
-public class CommandTestingApplication {
+public class CommandTestingApplication implements AutoCloseable {
- private final PrintWriter writer;
private final ConsoleTestApplication application;
private final Thread th;
+ private final TestConsoleManager manager;
/** @throws IOException if the streams cannot be build */
- @SuppressWarnings("resource")
public CommandTestingApplication() throws IOException {
- final PipedOutputStream src = new PipedOutputStream();
- InputStream in = new PipedInputStream(src);
-
- application = new ConsoleTestApplication(
- new SystemConsoleManager(System.out, in));
+ manager = new TestConsoleManager();
+ application = new ConsoleTestApplication(manager);
th = new Thread(new Runnable() {
@SuppressWarnings("synthetic-access")
@@ -80,16 +72,16 @@ public class CommandTestingApplication {
});
th.start();
- writer = new PrintWriter(src, true);
}
- public void stop() {
+ @Override
+ public void close() throws IOException {
application.exit();
- writer.println();
+ manager.close();
}
- public void sendCommand(String cmd) {
- writer.println(cmd);
+ public void sendCommand(String cmd) throws IOException {
+ manager.type(cmd);
}
/** @param cmd the command
@@ -105,11 +97,9 @@ public class CommandTestingApplication {
return application;
}
- /**
- *
- */
- public void waitAndStop() {
- writer.println(ConsoleTestApplication.EXIT);
+ /** @throws IOException if the writing failed */
+ public void waitAndStop() throws IOException {
+ manager.type(ConsoleTestApplication.EXIT);
try {
th.join();
} catch (InterruptedException e) {
@@ -117,4 +107,16 @@ public class CommandTestingApplication {
e.printStackTrace();
}
}
+
+ /** @return the next written line, null if it is the prompt
+ * @throws IOException if the reading failed
+ * @see fr.bigeon.gclc.test.utils.TestConsoleManager#readNextLine() */
+ public String readNextLine() throws IOException {
+ String ret = manager.readNextLine();
+ if (ret.equals(manager.getPrompt())) {
+ return null;
+ }
+ return ret;
+ }
+
}
diff --git a/gclc/src/test/java/fr/bigeon/gclc/ConsoleApplicationTest.java b/gclc/src/test/java/fr/bigeon/gclc/ConsoleApplicationTest.java
index 4ea782d..017f188 100644
--- a/gclc/src/test/java/fr/bigeon/gclc/ConsoleApplicationTest.java
+++ b/gclc/src/test/java/fr/bigeon/gclc/ConsoleApplicationTest.java
@@ -38,22 +38,30 @@
*/
package fr.bigeon.gclc;
-import static org.junit.Assert.fail;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
import java.io.IOException;
-import java.io.InputStream;
-import java.io.PipedInputStream;
-import java.io.PipedOutputStream;
-import java.io.PrintWriter;
import org.junit.Test;
+import fr.bigeon.gclc.command.ExitCommand;
+import fr.bigeon.gclc.command.ICommand;
+import fr.bigeon.gclc.exception.CommandRunException;
+import fr.bigeon.gclc.exception.CommandRunExceptionType;
+import fr.bigeon.gclc.exception.InvalidCommandName;
+import fr.bigeon.gclc.i18n.Messages;
+import fr.bigeon.gclc.manager.ConsoleManager;
import fr.bigeon.gclc.manager.SystemConsoleManager;
+import fr.bigeon.gclc.test.utils.TestConsoleManager;
/** Test class for ConsoleApplication
*
* @author Emmanuel Bigeon */
-@SuppressWarnings({"resource", "javadoc", "nls", "static-method"})
+@SuppressWarnings({"javadoc", "nls", "static-method"})
public class ConsoleApplicationTest {
/** 3 seconds in milliseconds */
@@ -69,27 +77,74 @@ public class ConsoleApplicationTest {
@Test
public void executionTest() {
- try {
- CommandTestingApplication application = new CommandTestingApplication();
+ try (CommandTestingApplication application = new CommandTestingApplication()) {
+ // remove welcome
+ assertEquals(application.getApplication().getHeader(),
+ application.readNextLine());
+ // Remove first prompt
+ assertNull(application.readNextLine());
+ application.sendCommand("");
+ assertNull(application.readNextLine());
application.sendCommand("test");
+ assertEquals("Test command ran fine", application.readNextLine());
+ assertNull(application.readNextLine());
application.sendCommand("toto");
+ assertEquals(
+ Messages.getString("ConsoleApplication.cmd.failed", "toto"),
+ application.readNextLine());
+ assertEquals(
+ Messages.getString("CommandProvider.unrecognized", "toto"),
+ application.readNextLine());
+ assertNull(application.readNextLine());
application.sendCommand("long");
- application.sendCommand("exit");
+ assertEquals("Waita minute", application.readNextLine());
+ assertEquals("done!", application.readNextLine());
+ CommandRequestListener crl = new CommandRequestListener() {
+
+ @Override
+ public void commandRequest(String command) {
+ //
+ }
+ };
+ CommandRequestListener crl2 = new CommandRequestListener() {
+
+ @Override
+ public void commandRequest(String command) {
+ //
+ }
+ };
+ application.getApplication().addListener(crl);
+ application.getApplication().addListener(crl2);
+ assertNull(application.readNextLine());
+ application.sendCommand("test");
+ assertEquals("Test command ran fine", application.readNextLine());
+ application.getApplication().removeListener(crl2);
+ application.getApplication().removeListener(crl);
+ application.getApplication().removeListener(crl);
+
+ assertTrue(application.getApplication().isRunning());
+
+ assertNull(application.readNextLine());
+ application.sendCommand("exit");
+ assertEquals(application.getApplication().getFooter(),
+ application.readNextLine());
+ assertFalse(application.getApplication().isRunning());
} catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
+ assertNull(e1);
}
- try {
- final PipedOutputStream src = new PipedOutputStream();
- InputStream in = new PipedInputStream(src);
+ ConsoleApplication appli = null;
+ try (TestConsoleManager manager = new TestConsoleManager()) {
+ final ConsoleApplication app = new ConsoleApplication(manager, null,
+ null);
+ appli = app;
+ app.add(new ExitCommand("exit", app));
- final ConsoleTestApplication app = new ConsoleTestApplication(
- new SystemConsoleManager(System.out, in));
Thread th = new Thread(new Runnable() {
+ @SuppressWarnings("synthetic-access")
@Override
public void run() {
app.start();
@@ -98,33 +153,67 @@ public class ConsoleApplicationTest {
th.start();
- Thread test = new Thread(new Runnable() {
+ manager.type("exit");
+ th.join();
- @Override
- public void run() {
- try (PrintWriter writer = new PrintWriter(src, true)) {
- writer.println("test");
- writer.println("toto");
- writer.println("long");
- writer.println("exit");
+ } catch (IOException | InvalidCommandName | InterruptedException e) {
+ assertNull(e);
+ }
+
+ assertNotNull(appli);
+ appli.start();
+ assertFalse(appli.isRunning());
+ }
+
+ @Test
+ public void interpretCommandTest() {
+ try (TestConsoleManager test = new TestConsoleManager()) {
+ ConsoleApplication appl = new ConsoleApplication(test, "", "");
+
+ appl.interpretCommand("invalid cmd \"due to misplaced\"quote");
+ assertEquals("Command line cannot be parsed", test.readNextLine());
+
+ appl.interpretCommand("");
+
+ final String message = "message";
+ try {
+ appl.add(new ICommand() {
+
+ @Override
+ public void execute(String... args) throws CommandRunException {
+ throw new CommandRunException(
+ CommandRunExceptionType.USAGE, message, this);
}
- }
- });
- test.start();
- try {
- th.join();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- try {
- test.join();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+
+ @Override
+ public String getCommandName() {
+ return "fail";
+ }
+
+ @Override
+ public void help(ConsoleManager manager,
+ String... args) throws IOException {
+ manager.println(message);
+ }
+
+ @Override
+ public String tip() {
+ return "";
+ }
+
+ });
+ } catch (InvalidCommandName e) {
+ assertNull(e);
}
+ appl.interpretCommand("fail");
+ assertEquals(
+ Messages.getString("ConsoleApplication.cmd.failed", "fail"),
+ test.readNextLine());
+ assertEquals(message, test.readNextLine());
+ assertEquals(message, test.readNextLine());
+
} catch (IOException e) {
- fail("pipe creation " + e.getLocalizedMessage()); //$NON-NLS-1$
+ assertNull(e);
}
}
}
diff --git a/gclc/src/test/java/fr/bigeon/gclc/ConsoleTestApplication.java b/gclc/src/test/java/fr/bigeon/gclc/ConsoleTestApplication.java
index ad75001..e157960 100644
--- a/gclc/src/test/java/fr/bigeon/gclc/ConsoleTestApplication.java
+++ b/gclc/src/test/java/fr/bigeon/gclc/ConsoleTestApplication.java
@@ -100,6 +100,18 @@ public class ConsoleTestApplication extends ConsoleApplication {
}
}
});
+ add(new Command("failingCmd") {
+
+ @Override
+ public String tip() {
+ return "A long execution command";
+ }
+
+ @Override
+ public void execute(String... args) throws CommandRunException {
+ throw new CommandRunException("Failing command", this);
+ }
+ });
} catch (final InvalidCommandName e) {
e.printStackTrace();
}
diff --git a/gclc/src/test/java/fr/bigeon/gclc/command/CommandParametersTest.java b/gclc/src/test/java/fr/bigeon/gclc/command/CommandParametersTest.java
new file mode 100644
index 0000000..782259b
--- /dev/null
+++ b/gclc/src/test/java/fr/bigeon/gclc/command/CommandParametersTest.java
@@ -0,0 +1,283 @@
+/*
+ * Copyright Bigeon Emmanuel (2014)
+ *
+ * emmanuel@bigeon.fr
+ *
+ * This software is a computer program whose purpose is to
+ * provide a generic framework for console applications.
+ *
+ * This software is governed by the CeCILL license under French law and
+ * abiding by the rules of distribution of free software. You can use,
+ * modify and/or redistribute the software under the terms of the CeCILL
+ * license as circulated by CEA, CNRS and INRIA at the following URL
+ * "http://www.cecill.info".
+ *
+ * As a counterpart to the access to the source code and rights to copy,
+ * modify and redistribute granted by the license, users are provided only
+ * with a limited warranty and the software's author, the holder of the
+ * economic rights, and the successive licensors have only limited
+ * liability.
+ *
+ * In this respect, the user's attention is drawn to the risks associated
+ * with loading, using, modifying and/or developing or reproducing the
+ * software by the user in light of its specific status of free software,
+ * that may mean that it is complicated to manipulate, and that also
+ * therefore means that it is reserved for developers and experienced
+ * professionals having in-depth computer knowledge. Users are therefore
+ * encouraged to load and test the software's suitability as regards their
+ * requirements in conditions enabling the security of their systems and/or
+ * data to be ensured and, more generally, to use and operate it in the
+ * same conditions as regards security.
+ *
+ * The fact that you are presently reading this means that you have had
+ * knowledge of the CeCILL license and that you accept its terms.
+ */
+/**
+ * gclc:fr.bigeon.gclc.command.CommandParametersTest.java
+ * Created on: Nov 18, 2016
+ */
+package fr.bigeon.gclc.command;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import org.junit.Test;
+
+/**
+ *
+ * TODO
+ *
+ * @author Emmanuel Bigeon
+ *
+ */
+public class CommandParametersTest {
+
+ /**
+ * Test method for {@link fr.bigeon.gclc.command.CommandParameters#CommandParameters(java.util.Set, java.util.Set, boolean)}.
+ */
+ @Test
+ public final void testCommandParameters(){
+ Set
+ * TODO
+ *
+ * @author Emmanuel Bigeon */
+public class CommandProviderTest {
+
+ /** Test method for
+ * {@link fr.bigeon.gclc.command.CommandProvider#add(fr.bigeon.gclc.command.ICommand)}. */
+ @Test
+ public final void testAdd() {
+ CommandProvider provider = new CommandProvider();
+ try {
+ provider.add(new MockCommand(null));
+ fail("null name for command should be rejected");
+ } catch (InvalidCommandName e) {
+ assertNotNull(e);
+ }
+ try {
+ provider.add(new MockCommand(""));
+ fail("null name for command should be rejected");
+ } catch (InvalidCommandName e) {
+ assertNotNull(e);
+ }
+ try {
+ provider.add(new MockCommand("-name"));
+ fail("name with minus as starting character for command should be rejected");
+ } catch (InvalidCommandName e) {
+ assertNotNull(e);
+ }
+ try {
+ provider.add(new MockCommand("name command"));
+ fail("name with space for command should be rejected");
+ } catch (InvalidCommandName e) {
+ assertNotNull(e);
+ }
+ ICommand mock = new MockCommand("name");
+ try {
+ provider.add(mock);
+ } catch (InvalidCommandName e) {
+ assertNull(e);
+ }
+
+ try {
+ provider.add(new MockCommand(mock.getCommandName()));
+ fail("already existing command name should be rejected");
+ } catch (InvalidCommandName e) {
+ assertNotNull(e);
+ }
+
+ try {
+ provider.add(mock);
+ } catch (InvalidCommandName e) {
+ assertNotNull(e);
+ }
+ }
+
+}
diff --git a/gclc/src/test/java/fr/bigeon/gclc/command/CommandTest.java b/gclc/src/test/java/fr/bigeon/gclc/command/CommandTest.java
new file mode 100644
index 0000000..8b74e4e
--- /dev/null
+++ b/gclc/src/test/java/fr/bigeon/gclc/command/CommandTest.java
@@ -0,0 +1,217 @@
+/*
+ * Copyright Bigeon Emmanuel (2014)
+ *
+ * emmanuel@bigeon.fr
+ *
+ * This software is a computer program whose purpose is to
+ * provide a generic framework for console applications.
+ *
+ * This software is governed by the CeCILL license under French law and
+ * abiding by the rules of distribution of free software. You can use,
+ * modify and/or redistribute the software under the terms of the CeCILL
+ * license as circulated by CEA, CNRS and INRIA at the following URL
+ * "http://www.cecill.info".
+ *
+ * As a counterpart to the access to the source code and rights to copy,
+ * modify and redistribute granted by the license, users are provided only
+ * with a limited warranty and the software's author, the holder of the
+ * economic rights, and the successive licensors have only limited
+ * liability.
+ *
+ * In this respect, the user's attention is drawn to the risks associated
+ * with loading, using, modifying and/or developing or reproducing the
+ * software by the user in light of its specific status of free software,
+ * that may mean that it is complicated to manipulate, and that also
+ * therefore means that it is reserved for developers and experienced
+ * professionals having in-depth computer knowledge. Users are therefore
+ * encouraged to load and test the software's suitability as regards their
+ * requirements in conditions enabling the security of their systems and/or
+ * data to be ensured and, more generally, to use and operate it in the
+ * same conditions as regards security.
+ *
+ * The fact that you are presently reading this means that you have had
+ * knowledge of the CeCILL license and that you accept its terms.
+ */
+/**
+ * gclc:fr.bigeon.gclc.command.CommandTest.java
+ * Created on: Nov 19, 2016
+ */
+package fr.bigeon.gclc.command;
+
+import static org.junit.Assert.assertNull;
+
+import java.io.IOException;
+
+import org.junit.Test;
+
+import fr.bigeon.gclc.exception.CommandRunException;
+import fr.bigeon.gclc.test.utils.TestConsoleManager;
+
+/**
+ * TODO
+ *
+ * @author Emmanuel Bigeon */
+public class CommandTest {
+
+ @Test
+ public final void test() {
+ try (TestConsoleManager test = new TestConsoleManager()) {
+ Command cmd;
+ cmd = new Command("name") {
+
+ @Override
+ public String tip() {
+ return null;
+ }
+
+ @Override
+ public void execute(String... args) throws CommandRunException {
+ //
+ }
+ };
+ cmd.help(test);
+
+ cmd = new Command("name") {
+
+ @Override
+ public String tip() {
+ return "";
+ }
+
+ @Override
+ public void execute(String... args) throws CommandRunException {
+ //
+ }
+ };
+ cmd.help(test);
+
+ cmd = new Command("name") {
+
+ @Override
+ public String tip() {
+ return "tip";
+ }
+
+ /* (non-Javadoc)
+ * @see fr.bigeon.gclc.command.Command#brief() */
+ @Override
+ protected String brief() {
+ return null;
+ }
+
+ @Override
+ public void execute(String... args) throws CommandRunException {
+ //
+ }
+ };
+ cmd.help(test);
+
+ cmd = new Command("name") {
+
+ @Override
+ public String tip() {
+ return "tip";
+ }
+
+ /* (non-Javadoc)
+ * @see fr.bigeon.gclc.command.Command#brief() */
+ @Override
+ protected String brief() {
+ return "brief";
+ }
+
+ @Override
+ public void execute(String... args) throws CommandRunException {
+ //
+ }
+ };
+ cmd.help(test);
+
+ cmd = new Command("name") {
+
+ @Override
+ public String tip() {
+ return "tip";
+ }
+
+ /* (non-Javadoc)
+ * @see fr.bigeon.gclc.command.Command#usageDetail() */
+ @Override
+ protected String usageDetail() {
+ return null;
+ }
+
+ @Override
+ public void execute(String... args) throws CommandRunException {
+ //
+ }
+ };
+ cmd.help(test);
+
+ cmd = new Command("name") {
+
+ @Override
+ public String tip() {
+ return "tip";
+ }
+
+ /* (non-Javadoc)
+ * @see fr.bigeon.gclc.command.Command#usageDetail() */
+ @Override
+ protected String usageDetail() {
+ return "details";
+ }
+
+ @Override
+ public void execute(String... args) throws CommandRunException {
+ //
+ }
+ };
+ cmd.help(test);
+ cmd = new Command("name") {
+
+ @Override
+ public String tip() {
+ return "tip";
+ }
+
+ /* (non-Javadoc)
+ * @see fr.bigeon.gclc.command.Command#usageDetail() */
+ @Override
+ protected String usageDetail() {
+ return "details" + System.lineSeparator();
+ }
+
+ @Override
+ public void execute(String... args) throws CommandRunException {
+ //
+ }
+ };
+ cmd.help(test);
+
+ cmd = new Command("name") {
+
+ @Override
+ public String tip() {
+ return "tip";
+ }
+
+ /* (non-Javadoc)
+ * @see fr.bigeon.gclc.command.Command#usageDetail() */
+ @Override
+ protected String usageDetail() {
+ return "\n";
+ }
+
+ @Override
+ public void execute(String... args) throws CommandRunException {
+ //
+ }
+ };
+ cmd.help(test);
+
+ } catch (IOException e) {
+ assertNull(e);
+ }
+ }
+}
diff --git a/gclc/src/test/java/fr/bigeon/gclc/command/HelpExecutorTest.java b/gclc/src/test/java/fr/bigeon/gclc/command/HelpExecutorTest.java
new file mode 100644
index 0000000..edb4568
--- /dev/null
+++ b/gclc/src/test/java/fr/bigeon/gclc/command/HelpExecutorTest.java
@@ -0,0 +1,137 @@
+/*
+ * Copyright Bigeon Emmanuel (2014)
+ *
+ * emmanuel@bigeon.fr
+ *
+ * This software is a computer program whose purpose is to
+ * provide a generic framework for console applications.
+ *
+ * This software is governed by the CeCILL license under French law and
+ * abiding by the rules of distribution of free software. You can use,
+ * modify and/or redistribute the software under the terms of the CeCILL
+ * license as circulated by CEA, CNRS and INRIA at the following URL
+ * "http://www.cecill.info".
+ *
+ * As a counterpart to the access to the source code and rights to copy,
+ * modify and redistribute granted by the license, users are provided only
+ * with a limited warranty and the software's author, the holder of the
+ * economic rights, and the successive licensors have only limited
+ * liability.
+ *
+ * In this respect, the user's attention is drawn to the risks associated
+ * with loading, using, modifying and/or developing or reproducing the
+ * software by the user in light of its specific status of free software,
+ * that may mean that it is complicated to manipulate, and that also
+ * therefore means that it is reserved for developers and experienced
+ * professionals having in-depth computer knowledge. Users are therefore
+ * encouraged to load and test the software's suitability as regards their
+ * requirements in conditions enabling the security of their systems and/or
+ * data to be ensured and, more generally, to use and operate it in the
+ * same conditions as regards security.
+ *
+ * The fact that you are presently reading this means that you have had
+ * knowledge of the CeCILL license and that you accept its terms.
+ */
+/**
+ * gclc:fr.bigeon.gclc.command.HelpExecutorTest.java
+ * Created on: Nov 19, 2016
+ */
+package fr.bigeon.gclc.command;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.fail;
+
+import org.junit.Test;
+
+import fr.bigeon.gclc.exception.CommandRunException;
+import fr.bigeon.gclc.test.utils.TestConsoleManager;
+
+/**
+ *
+ * TODO
+ *
+ * @author Emmanuel Bigeon
+ *
+ */
+public class HelpExecutorTest {
+
+ /**
+ * Test method for {@link fr.bigeon.gclc.command.HelpExecutor#HelpExecutor(java.lang.String, fr.bigeon.gclc.manager.ConsoleManager, fr.bigeon.gclc.command.ICommand)}.
+ */
+ @Test
+ public final void testHelpExecutor(){
+ HelpExecutor help;
+ try {
+ help = new HelpExecutor("?", null, new MockCommand("mock"));
+ fail("help is an interactive command, should need console manager");
+ } catch (Exception e) {
+ assertNotNull(e);
+ }
+ try (TestConsoleManager test = new TestConsoleManager()) {
+ help = new HelpExecutor("?", test, new MockCommand("mock"));
+ } catch (Exception e) {
+ assertNull(e);
+ }
+ }
+
+ /**
+ * Test method for {@link fr.bigeon.gclc.command.HelpExecutor#execute(java.lang.String[])}.
+ */
+ @Test
+ public final void testExecute(){
+ try {
+ TestConsoleManager test = new TestConsoleManager();
+ HelpExecutor help = new HelpExecutor("?", test,
+ new Command("mock") {
+
+ @Override
+ public void execute(String... args) throws CommandRunException {
+ //
+ }
+
+ @Override
+ public String tip() {
+ return "";
+ }
+
+ });
+
+ help.execute();
+ test.close();
+
+ try {
+ help.execute();
+ fail("manager closed shall provoke failure of help command execution");
+ } catch (Exception e) {
+ assertNotNull(e);
+ }
+ } catch (Exception e) {
+ assertNull(e);
+ }
+ }
+
+ /**
+ * Test method for {@link fr.bigeon.gclc.command.HelpExecutor#tip()}.
+ */
+ @Test
+ public final void testTip(){
+ try (TestConsoleManager test = new TestConsoleManager()) {
+ HelpExecutor help = new HelpExecutor("?", test,
+ new MockCommand("mock"));
+ help.tip();
+ help.help(test);
+ } catch (Exception e) {
+ assertNull(e);
+ }
+ try (TestConsoleManager test = new TestConsoleManager()) {
+ HelpExecutor help = new HelpExecutor("?", test,
+ new SubedCommand("sub", new MockCommand("mock")));
+ help.tip();
+ help.help(test);
+ } catch (Exception e) {
+ assertNull(e);
+ }
+ }
+
+}
diff --git a/gclc/src/test/java/fr/bigeon/gclc/command/ParametrizedCommandTest.java b/gclc/src/test/java/fr/bigeon/gclc/command/ParametrizedCommandTest.java
new file mode 100644
index 0000000..dc60e8a
--- /dev/null
+++ b/gclc/src/test/java/fr/bigeon/gclc/command/ParametrizedCommandTest.java
@@ -0,0 +1,649 @@
+/*
+ * Copyright Bigeon Emmanuel (2014)
+ *
+ * emmanuel@bigeon.fr
+ *
+ * This software is a computer program whose purpose is to
+ * provide a generic framework for console applications.
+ *
+ * This software is governed by the CeCILL license under French law and
+ * abiding by the rules of distribution of free software. You can use,
+ * modify and/or redistribute the software under the terms of the CeCILL
+ * license as circulated by CEA, CNRS and INRIA at the following URL
+ * "http://www.cecill.info".
+ *
+ * As a counterpart to the access to the source code and rights to copy,
+ * modify and redistribute granted by the license, users are provided only
+ * with a limited warranty and the software's author, the holder of the
+ * economic rights, and the successive licensors have only limited
+ * liability.
+ *
+ * In this respect, the user's attention is drawn to the risks associated
+ * with loading, using, modifying and/or developing or reproducing the
+ * software by the user in light of its specific status of free software,
+ * that may mean that it is complicated to manipulate, and that also
+ * therefore means that it is reserved for developers and experienced
+ * professionals having in-depth computer knowledge. Users are therefore
+ * encouraged to load and test the software's suitability as regards their
+ * requirements in conditions enabling the security of their systems and/or
+ * data to be ensured and, more generally, to use and operate it in the
+ * same conditions as regards security.
+ *
+ * The fact that you are presently reading this means that you have had
+ * knowledge of the CeCILL license and that you accept its terms.
+ */
+/**
+ * gclc:fr.bigeon.gclc.command.ParametrizedCommandTest.java
+ * Created on: Nov 18, 2016
+ */
+package fr.bigeon.gclc.command;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.IOException;
+
+import org.junit.Test;
+
+import fr.bigeon.gclc.exception.CommandRunException;
+import fr.bigeon.gclc.exception.InvalidParameterException;
+import fr.bigeon.gclc.test.utils.TestConsoleManager;
+
+/**
+ * TODO
+ *
+ * @author Emmanuel Bigeon */
+public class ParametrizedCommandTest {
+
+ /** Test method for
+ * {@link fr.bigeon.gclc.command.ParametrizedCommand#ParametrizedCommand(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String)}. */
+ @Test
+ public final void testParametrizedCommandConsoleManagerString() {
+ try (TestConsoleManager test = new TestConsoleManager()) {
+ ParametrizedCommand cmd = new ParametrizedCommand(test, "name") {
+
+ @Override
+ public String tip() {
+ return null;
+ }
+
+ @Override
+ protected void doExecute(CommandParameters parameters) {
+ //
+ }
+ };
+ assertTrue(cmd.isStrict());
+ assertTrue(cmd.isInteractive());
+ } catch (IOException e) {
+ fail("Unexpected exception in creation");
+ assertNull(e);
+ }
+ ParametrizedCommand cmd = new ParametrizedCommand(null, "name") {
+
+ @Override
+ public String tip() {
+ return null;
+ }
+
+ @Override
+ protected void doExecute(CommandParameters parameters) {
+ //
+ }
+ };
+ assertTrue(cmd.isStrict());
+ assertFalse(cmd.isInteractive());
+ cmd = new ParametrizedCommand("name") {
+
+ @Override
+ public String tip() {
+ return null;
+ }
+
+ @Override
+ protected void doExecute(CommandParameters parameters) {
+ //
+ }
+ };
+ assertTrue(cmd.isStrict());
+ assertFalse(cmd.isInteractive());
+ }
+
+ /** Test method for
+ * {@link fr.bigeon.gclc.command.ParametrizedCommand#ParametrizedCommand(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String, boolean)}. */
+ @Test
+ public final void testParametrizedCommandConsoleManagerStringBoolean() {
+ try (TestConsoleManager test = new TestConsoleManager()) {
+ ParametrizedCommand cmd = new ParametrizedCommand(test, "name",
+ false) {
+
+ @Override
+ public String tip() {
+ return null;
+ }
+
+ @Override
+ protected void doExecute(CommandParameters parameters) {
+ //
+ }
+ };
+ assertFalse(cmd.isStrict());
+ assertTrue(cmd.isInteractive());
+ } catch (IOException e) {
+ fail("Unexpected exception in creation");
+ assertNull(e);
+ }
+ ParametrizedCommand cmd = new ParametrizedCommand(null, "name", false) {
+
+ @Override
+ public String tip() {
+ return null;
+ }
+
+ @Override
+ protected void doExecute(CommandParameters parameters) {
+ //
+ }
+ };
+ assertFalse(cmd.isStrict());
+ assertFalse(cmd.isInteractive());
+ cmd = new ParametrizedCommand("name", false) {
+
+ @Override
+ public String tip() {
+ return null;
+ }
+
+ @Override
+ protected void doExecute(CommandParameters parameters) {
+ //
+ }
+ };
+ assertFalse(cmd.isStrict());
+ assertFalse(cmd.isInteractive());
+ }
+
+ /** Test method for
+ * {@link fr.bigeon.gclc.command.ParametrizedCommand#addParameter(java.lang.String, boolean, boolean)}. */
+ @Test
+ public final void testAddParameter() {
+ ParametrizedCommand cmd = new ParametrizedCommand(null, "name") {
+
+ @Override
+ public String tip() {
+ return null;
+ }
+
+ @Override
+ protected void doExecute(CommandParameters parameters) {
+ //
+ }
+ };
+ cmd = new ParametrizedCommand(null, "name", true) {
+
+ @Override
+ public String tip() {
+ return null;
+ }
+
+ @Override
+ protected void doExecute(CommandParameters parameters) {
+ //
+ }
+ };
+ // XXX Boolean flag should not be specified mandatory! They are by
+ // nature qualified
+ try {
+ cmd.addParameter("boolFlag", false, true);
+ fail("Boolean parameters should never be needed specified");
+ } catch (InvalidParameterException e) {
+ // OK
+ assertNotNull(e);
+ }
+ String str = "str";
+ try {
+ assertTrue(cmd.getBooleanParameters().isEmpty());
+ assertTrue(cmd.getStringParameters().isEmpty());
+ cmd.addParameter("boolFlag", false, false);
+ assertEquals(1, cmd.getBooleanParameters().size());
+ assertTrue(cmd.getStringParameters().isEmpty());
+ cmd.addParameter(str, true, false);
+ assertEquals(1, cmd.getBooleanParameters().size());
+ assertEquals(1, cmd.getStringParameters().size());
+ assertFalse(cmd.isNeeded(str));
+ cmd.addParameter("boolFlag", false, false);
+ assertEquals(1, cmd.getBooleanParameters().size());
+ assertEquals(1, cmd.getStringParameters().size());
+ cmd.addParameter(str, true, true);
+ assertEquals(1, cmd.getBooleanParameters().size());
+ assertEquals(1, cmd.getStringParameters().size());
+ assertTrue(cmd.isNeeded(str));
+ cmd.addParameter(str, true, false);
+ assertEquals(1, cmd.getBooleanParameters().size());
+ assertEquals(1, cmd.getStringParameters().size());
+ assertTrue(cmd.isNeeded(str));
+ } catch (InvalidParameterException e) {
+ fail("Unexpected error in addition of legitimate parameter");
+ assertNotNull(e);
+ }
+ try {
+ cmd.addParameter(str, false, false);
+ fail("parameter type conversion shall fail");
+ } catch (InvalidParameterException e) {
+ // OK
+ assertNotNull(e);
+ }
+ try {
+ cmd.addParameter("boolFlag", true, false);
+ fail("parameter type conversion shall fail");
+ } catch (InvalidParameterException e) {
+ // OK
+ assertNotNull(e);
+ }
+ try {
+ cmd.addParameter("boolFlag", false, true);
+ fail("parameter type conversion shall fail");
+ } catch (InvalidParameterException e) {
+ // OK
+ assertNotNull(e);
+ }
+ try {
+ cmd.addParameter("boolFlag", true, true);
+ fail("parameter type conversion shall fail");
+ } catch (InvalidParameterException e) {
+ // OK
+ assertNotNull(e);
+ }
+ }
+
+ /** Test method for
+ * {@link fr.bigeon.gclc.command.ParametrizedCommand#execute(java.lang.String[])}. */
+ @Test
+ public final void testExecute() {
+ final String addParam = "additional";
+ final String str1 = "str1";
+ final String str2 = "str2";
+ final String bool1 = "bool1";
+ final String bool2 = "bool2";
+
+ // Test on command with no needed elements
+ ParametrizedCommand cmd = new ParametrizedCommand("name", false) {
+ private boolean evenCall = true;
+
+ @Override
+ public String tip() {
+ return "";
+ }
+
+ @Override
+ protected void doExecute(CommandParameters parameters) {
+ assertTrue(parameters.getBooleanArgumentKeys().isEmpty());
+ assertTrue(parameters.getStringArgumentKeys().isEmpty());
+ if (evenCall) {
+ assertTrue(parameters.getAdditionals().isEmpty());
+ evenCall = false;
+ } else {
+ assertEquals(1, parameters.getAdditionals().size());
+ assertTrue(parameters.getAdditionals().contains(addParam));
+ evenCall = true;
+ }
+ }
+ };
+ try {
+ cmd.execute();
+ cmd.execute("-" + addParam);
+ cmd.execute(addParam);
+ cmd.execute("-" + addParam, addParam);
+ } catch (CommandRunException e) {
+ assertNull(e);
+ fail("unepected error");
+ }
+ cmd = new ParametrizedCommand("name", false) {
+ private int call = 0;
+ {
+
+ try {
+ addParameter(str1, true, false);
+ addParameter(str2, true, false);
+ addParameter(bool1, false, false);
+ addParameter(bool2, false, false);
+ } catch (InvalidParameterException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ @Override
+ public String tip() {
+ return "";
+ }
+
+ @Override
+ protected void doExecute(CommandParameters parameters) {
+ assertEquals(2, parameters.getBooleanArgumentKeys().size());
+ assertEquals(2, parameters.getStringArgumentKeys().size());
+ switch (call) {
+ case 0:
+ case 1:
+ case 2:
+ case 3:
+ assertNull(parameters.get(str1));
+ assertNull(parameters.get(str2));
+ assertFalse(parameters.getBool(bool1));
+ assertFalse(parameters.getBool(bool2));
+ call++;
+ break;
+ case 4:
+ assertEquals(str2, parameters.get(str1));
+ assertNull(parameters.get(str2));
+ assertFalse(parameters.getBool(bool1));
+ assertFalse(parameters.getBool(bool2));
+ call++;
+ break;
+ case 5:
+ assertEquals(str2, parameters.get(str1));
+ assertNull(parameters.get(str2));
+ assertTrue(parameters.getBool(bool1));
+ assertFalse(parameters.getBool(bool2));
+ call = 0;
+ break;
+ default:
+ break;
+ }
+ }
+ };
+ try {
+ cmd.execute();
+ cmd.execute("-" + addParam);
+ cmd.execute(addParam);
+ cmd.execute("-" + addParam, addParam);
+ cmd.execute("-" + str1, str2);
+ cmd.execute("-" + str1, str2, "-" + bool1);
+ } catch (CommandRunException e) {
+ assertNull(e);
+ fail("unepected error");
+ }
+ cmd = new ParametrizedCommand("name", true) {
+ private int call = 0;
+ {
+
+ try {
+ addParameter(str1, true, false);
+ addParameter(str2, true, false);
+ addParameter(bool1, false, false);
+ addParameter(bool2, false, false);
+ } catch (InvalidParameterException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ @Override
+ public String tip() {
+ return "";
+ }
+
+ @Override
+ protected void doExecute(CommandParameters parameters) {
+ assertEquals(2, parameters.getBooleanArgumentKeys().size());
+ assertEquals(2, parameters.getStringArgumentKeys().size());
+ switch (call) {
+ case 0:
+ case 1:
+ assertNull(parameters.get(str1));
+ assertNull(parameters.get(str2));
+ assertFalse(parameters.getBool(bool1));
+ assertFalse(parameters.getBool(bool2));
+ call++;
+ break;
+ case 2:
+ assertEquals(str2, parameters.get(str1));
+ assertNull(parameters.get(str2));
+ assertFalse(parameters.getBool(bool1));
+ assertFalse(parameters.getBool(bool2));
+ call++;
+ break;
+ case 3:
+ assertEquals(str2, parameters.get(str1));
+ assertNull(parameters.get(str2));
+ assertTrue(parameters.getBool(bool1));
+ assertFalse(parameters.getBool(bool2));
+ call = 0;
+ break;
+ default:
+ break;
+ }
+ }
+ };
+ try {
+ cmd.execute();
+ cmd.execute(addParam);
+ cmd.execute("-" + str1, str2);
+ cmd.execute("-" + str1, str2, "-" + bool1);
+ } catch (CommandRunException e) {
+ assertNull(e);
+ fail("unepected error");
+ }
+ try {
+ cmd.execute("-" + addParam);
+ fail("Strict should fail with unexpected argument");
+ } catch (CommandRunException e) {
+ assertNotNull(e);
+ }
+ // Test on command with missing needed elements
+ cmd = new ParametrizedCommand("name", false) {
+ private final int call = 0;
+ {
+
+ try {
+ addParameter(str1, true, true);
+ addParameter(str2, true, false);
+ addParameter(bool1, false, false);
+ addParameter(bool2, false, false);
+ } catch (InvalidParameterException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ @Override
+ public String tip() {
+ return "";
+ }
+
+ @Override
+ protected void doExecute(CommandParameters parameters) {
+ assertEquals(str2, parameters.get(str1));
+ }
+ };
+ try {
+ cmd.execute("-" + str1, str2);
+ cmd.execute("-" + str1, str2, "-" + bool1);
+ cmd.execute("-" + str1, str2, "-" + addParam);
+ cmd.execute("-" + str1, str2, addParam);
+ cmd.execute("-" + str1, str2, "-" + addParam, addParam);
+ } catch (CommandRunException e) {
+ assertNull(e);
+ fail("unepected error");
+ }
+ try {
+ cmd.execute();
+ fail("needed " + str1 + " not provided shall fail");
+ } catch (CommandRunException e) {
+ assertNotNull(e);
+ }
+ cmd = new ParametrizedCommand("name", true) {
+ private final int call = 0;
+ {
+
+ try {
+ addParameter(str1, true, true);
+ addParameter(str2, true, false);
+ addParameter(bool1, false, false);
+ addParameter(bool2, false, false);
+ } catch (InvalidParameterException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ @Override
+ public String tip() {
+ return "";
+ }
+
+ @Override
+ protected void doExecute(CommandParameters parameters) {
+ //
+ assertEquals(str2, parameters.get(str1));
+ }
+ };
+ try {
+ cmd.execute("-" + str1, str2);
+ cmd.execute("-" + str1, str2, "-" + bool1);
+ cmd.execute("-" + str1, str2, addParam);
+ } catch (CommandRunException e) {
+ assertNull(e);
+ fail("unepected error");
+ }
+ try {
+ cmd.execute();
+ fail("needed " + str1 + " not provided shall fail");
+ } catch (CommandRunException e) {
+ assertNotNull(e);
+ }
+ try {
+ cmd.execute("-" + str1, str2, "-" + addParam);
+ fail("unepected error");
+ } catch (CommandRunException e) {
+ assertNotNull(e);
+ }
+ try {
+ cmd.execute("-" + str1, str2, "-" + addParam, addParam);
+ fail("unepected error");
+ } catch (CommandRunException e) {
+ assertNotNull(e);
+ }
+ // TODO Test of interactive not providing and providing all needed
+ try (TestConsoleManager test = new TestConsoleManager()) {
+ cmd = new ParametrizedCommand(test, "name", false) {
+ {
+ try {
+ addParameter(str1, true, true);
+ addParameter(str2, true, false);
+ addParameter(bool1, false, false);
+ addParameter(bool2, false, false);
+ } catch (InvalidParameterException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ @Override
+ public String tip() {
+ return "";
+ }
+
+ @Override
+ protected void doExecute(CommandParameters parameters) {
+ assertEquals(str2, parameters.get(str1));
+ }
+ };
+ try {
+ cmd.execute("-" + str1, str2);
+ cmd.execute("-" + str1, str2, "-" + bool1);
+ cmd.execute("-" + str1, str2, addParam);
+ cmd.execute("-" + str1, str2, "-" + addParam);
+ cmd.execute("-" + str1, str2, "-" + addParam, addParam);
+ } catch (CommandRunException e) {
+ assertNull(e);
+ fail("unepected error");
+ }
+ try {
+
+ Thread th = new Thread(new Runnable() {
+
+ @Override
+ public void run() {
+ try {
+ assertEquals(str1, test.readNextLine());
+ test.type("");
+ assertEquals(str1, test.readNextLine());
+ test.type(str2);
+ } catch (IOException e) {
+ assertNull(e);
+ }
+ }
+ });
+ th.start();
+
+ cmd.execute();
+
+ th.join();
+
+ th = new Thread(new Runnable() {
+
+ @Override
+ public void run() {
+ try {
+ assertEquals(str1, test.readNextLine());
+ test.type(str2);
+ } catch (IOException e) {
+ assertNull(e);
+ }
+ }
+ });
+ th.start();
+
+ cmd.execute("-" + addParam);
+
+ th.join();
+ } catch (CommandRunException | InterruptedException e) {
+ assertNull(e);
+ fail("unepected error");
+ }
+ } catch (IOException e) {
+ assertNull(e);
+ fail("unepected error");
+ }
+ try {
+ TestConsoleManager test = new TestConsoleManager();
+ cmd = new ParametrizedCommand(test, "name") {
+ {
+ try {
+ addParameter(str1, true, true);
+ addParameter(str2, true, false);
+ addParameter(bool1, false, false);
+ addParameter(bool2, false, false);
+ } catch (InvalidParameterException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ @Override
+ public String tip() {
+ return "";
+ }
+
+ @Override
+ protected void doExecute(CommandParameters parameters) {
+ assertEquals(str2, parameters.get(str1));
+ }
+ };
+ test.close();
+ cmd.execute("-" + str1, str2);
+ cmd.execute("-" + addParam);
+ fail("Closed manager shall cause error");
+ } catch (IOException e) {
+ assertNull(e);
+ } catch (CommandRunException e) {
+ assertNotNull(e);
+ }
+
+ }
+
+}
diff --git a/gclc/src/test/java/fr/bigeon/gclc/command/ScriptExecutionTest.java b/gclc/src/test/java/fr/bigeon/gclc/command/ScriptExecutionTest.java
index 53bc6ae..4126dc9 100644
--- a/gclc/src/test/java/fr/bigeon/gclc/command/ScriptExecutionTest.java
+++ b/gclc/src/test/java/fr/bigeon/gclc/command/ScriptExecutionTest.java
@@ -38,14 +38,19 @@
*/
package fr.bigeon.gclc.command;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import java.io.IOException;
+import java.nio.charset.Charset;
import org.junit.Test;
-import fr.bigeon.gclc.CommandTestingApplication;
-import fr.bigeon.gclc.exception.InvalidCommandName;
+import fr.bigeon.gclc.ConsoleTestApplication;
+import fr.bigeon.gclc.exception.CommandRunException;
+import fr.bigeon.gclc.exception.CommandRunExceptionType;
+import fr.bigeon.gclc.test.utils.TestConsoleManager;
/**
*
@@ -62,24 +67,110 @@ public class ScriptExecutionTest {
*/
@Test
public void testExecute() {
+ TestConsoleManager test;
try {
- CommandTestingApplication application = new CommandTestingApplication();
+ test = new TestConsoleManager();
+ } catch (IOException e2) {
+ fail("creation of console manager failed"); //$NON-NLS-1$
+ assertNotNull(e2);
+ return;
+ }
+ ConsoleTestApplication app = new ConsoleTestApplication(
+ test);
+ ScriptExecution exec = new ScriptExecution("script", app, "#", //$NON-NLS-1$ //$NON-NLS-2$
+ Charset.forName("UTF-8"));
+ try {
+ exec.execute();
+ fail("execution of script command with no file should fail"); //$NON-NLS-1$
+ } catch (CommandRunException e1) {
+ // ok
+ assertEquals(exec, e1.getSource());
+ assertEquals(CommandRunExceptionType.USAGE, e1.getType());
+ }
- application.add(new ScriptExecution("script", //$NON-NLS-1$
- application.getApplication(), "#")); //$NON-NLS-1$
+ try {
+ exec.execute("src/test/resources/scripts/withprependSpace.txt"); //$NON-NLS-1$
+ fail("execution of script with lines begining with space should fail"); //$NON-NLS-1$
+ } catch (CommandRunException e1) {
+ // ok
+ assertEquals(exec, e1.getSource());
+ assertEquals(CommandRunExceptionType.EXECUTION, e1.getType());
+ }
- application.sendCommand("script src/test/resources/script1.txt"); //$NON-NLS-1$
- application.sendCommand("script src/test/resources/script2.txt"); //$NON-NLS-1$
- application.sendCommand("script src/test/resources/script3.txt"); //$NON-NLS-1$
- application.sendCommand("script src/test/resources/script4.txt"); //$NON-NLS-1$
- application
- .sendCommand("script src/test/resources/script5.txt test"); //$NON-NLS-1$
+ try {
+ exec.execute("src/test/resources/scripts/invalidCmdParse.txt"); //$NON-NLS-1$
+ fail("execution of script with invalid command line should fail"); //$NON-NLS-1$
+ } catch (CommandRunException e1) {
+ // ok
+ assertEquals(exec, e1.getSource());
+ assertEquals(CommandRunExceptionType.EXECUTION, e1.getType());
+ }
- application.waitAndStop();
+ try {
+ exec.execute("src/test/resources/scripts/invalidCmd.txt"); //$NON-NLS-1$
+ fail("execution of script with invalid command should fail"); //$NON-NLS-1$
+ } catch (CommandRunException e1) {
+ // ok
+ assertEquals(exec, e1.getSource());
+ assertEquals(CommandRunExceptionType.EXECUTION, e1.getType());
+ }
- } catch (IOException | InvalidCommandName e) {
- fail("Unexpected exception " + e.getLocalizedMessage()); //$NON-NLS-1$
+ try {
+ exec.execute("src/test/resources/scripts/failingCmdInvoc.txt"); //$NON-NLS-1$
+ fail("execution of script with failing command should fail"); //$NON-NLS-1$
+ } catch (CommandRunException e1) {
+ // ok
+ assertEquals(exec, e1.getSource());
+ assertEquals(CommandRunExceptionType.EXECUTION, e1.getType());
+ }
+
+ try {
+ exec.execute("src/test/resources/scripts/someNonExisting.file"); //$NON-NLS-1$
+ fail("execution of script with unexisting file should fail"); //$NON-NLS-1$
+ } catch (CommandRunException e1) {
+ // ok
+ assertEquals(exec, e1.getSource());
+ assertEquals(CommandRunExceptionType.EXECUTION, e1.getType());
+ }
+
+ try {
+ exec.execute("src/test/resources/script1.txt"); //$NON-NLS-1$
+ exec.execute("src/test/resources/script2.txt"); //$NON-NLS-1$
+ exec.execute("src/test/resources/script3.txt"); //$NON-NLS-1$
+ exec.execute("src/test/resources/script4.txt"); //$NON-NLS-1$
+ exec.execute("src/test/resources/script5.txt", "test"); //$NON-NLS-1$ //$NON-NLS-2$
+ } catch (CommandRunException e) {
+ e.printStackTrace();
+ fail("execution of wellformed script should not fail"); //$NON-NLS-1$
+ }
+ try {
+ test.close();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
}
}
+ /** Test method for {@link fr.bigeon.gclc.command.ScriptExecution#tip()}. */
+ @Test
+ public void testTip() {
+ ScriptExecution exec = new ScriptExecution("script", null, "#", //$NON-NLS-1$ //$NON-NLS-2$
+ Charset.forName("UTF-8"));
+ exec.tip();
+ }
+
+ /** Test method for
+ * {@link fr.bigeon.gclc.command.ScriptExecution#help(fr.bigeon.gclc.manager.ConsoleManager, String...)}. */
+ @Test
+ public void testHelp() {
+ ScriptExecution exec = new ScriptExecution("script", null, "#", //$NON-NLS-1$ //$NON-NLS-2$
+ Charset.forName("UTF-8"));
+ try (TestConsoleManager test = new TestConsoleManager()) {
+ exec.help(test);
+ exec.help(test, "ignored element");
+ } catch (IOException e) {
+ e.printStackTrace();
+ fail("unexpected error in help invocation"); //$NON-NLS-1$
+ }
+ }
}
diff --git a/gclc/src/test/java/fr/bigeon/gclc/command/SubedCommandTest.java b/gclc/src/test/java/fr/bigeon/gclc/command/SubedCommandTest.java
new file mode 100644
index 0000000..00998ce
--- /dev/null
+++ b/gclc/src/test/java/fr/bigeon/gclc/command/SubedCommandTest.java
@@ -0,0 +1,390 @@
+/*
+ * Copyright Bigeon Emmanuel (2014)
+ *
+ * emmanuel@bigeon.fr
+ *
+ * This software is a computer program whose purpose is to
+ * provide a generic framework for console applications.
+ *
+ * This software is governed by the CeCILL license under French law and
+ * abiding by the rules of distribution of free software. You can use,
+ * modify and/or redistribute the software under the terms of the CeCILL
+ * license as circulated by CEA, CNRS and INRIA at the following URL
+ * "http://www.cecill.info".
+ *
+ * As a counterpart to the access to the source code and rights to copy,
+ * modify and redistribute granted by the license, users are provided only
+ * with a limited warranty and the software's author, the holder of the
+ * economic rights, and the successive licensors have only limited
+ * liability.
+ *
+ * In this respect, the user's attention is drawn to the risks associated
+ * with loading, using, modifying and/or developing or reproducing the
+ * software by the user in light of its specific status of free software,
+ * that may mean that it is complicated to manipulate, and that also
+ * therefore means that it is reserved for developers and experienced
+ * professionals having in-depth computer knowledge. Users are therefore
+ * encouraged to load and test the software's suitability as regards their
+ * requirements in conditions enabling the security of their systems and/or
+ * data to be ensured and, more generally, to use and operate it in the
+ * same conditions as regards security.
+ *
+ * The fact that you are presently reading this means that you have had
+ * knowledge of the CeCILL license and that you accept its terms.
+ */
+/**
+ * gclc:fr.bigeon.gclc.command.SubedCommandTest.java
+ * Created on: Nov 18, 2016
+ */
+package fr.bigeon.gclc.command;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.fail;
+
+import java.io.IOException;
+
+import org.junit.Test;
+
+import fr.bigeon.gclc.exception.CommandRunException;
+import fr.bigeon.gclc.exception.InvalidCommandName;
+import fr.bigeon.gclc.manager.ConsoleManager;
+import fr.bigeon.gclc.test.utils.TestConsoleManager;
+
+/**
+ * TODO
+ *
+ * @author Emmanuel Bigeon */
+@SuppressWarnings("all")
+public class SubedCommandTest {
+
+ /** Test method for
+ * {@link fr.bigeon.gclc.command.SubedCommand#SubedCommand(java.lang.String)}. */
+ @Test
+ public final void testSubedCommandString() {
+ SubedCommand cmd = new SubedCommand("name");
+ assertEquals("name", cmd.getCommandName());
+ cmd = new SubedCommand("name with spaces");
+ assertNull(cmd.tip());
+ assertEquals("name with spaces", cmd.getCommandName());
+ }
+
+ /** Test method for
+ * {@link fr.bigeon.gclc.command.SubedCommand#SubedCommand(java.lang.String, fr.bigeon.gclc.command.ICommand)}. */
+ @Test
+ public final void testSubedCommandStringICommand() {
+ SubedCommand cmd = new SubedCommand("name", new MockCommand(""));
+ assertEquals("name", cmd.getCommandName());
+ cmd = new SubedCommand("name with spaces", new MockCommand(""));
+ assertNull(cmd.tip());
+ assertEquals("name with spaces", cmd.getCommandName());
+ }
+
+ /** Test method for
+ * {@link fr.bigeon.gclc.command.SubedCommand#SubedCommand(java.lang.String, fr.bigeon.gclc.command.ICommand, java.lang.String)}. */
+ @Test
+ public final void testSubedCommandStringICommandString() {
+ SubedCommand cmd = new SubedCommand("name", new MockCommand(""), "tip");
+ assertEquals("name", cmd.getCommandName());
+ cmd = new SubedCommand("name with spaces", new MockCommand(""), "tip");
+ assertEquals("name with spaces", cmd.getCommandName());
+ }
+
+ /** Test method for
+ * {@link fr.bigeon.gclc.command.SubedCommand#SubedCommand(java.lang.String, java.lang.String)}. */
+ @Test
+ public final void testSubedCommandStringString() {
+ SubedCommand cmd = new SubedCommand("name", "tip");
+ assertEquals("name", cmd.getCommandName());
+ cmd = new SubedCommand("name with spaces", "tip");
+ assertEquals("name with spaces", cmd.getCommandName());
+ }
+
+ /** Test method for
+ * {@link fr.bigeon.gclc.command.SubedCommand#add(fr.bigeon.gclc.command.ICommand)}. */
+ @Test
+ public final void testAdd() {
+ SubedCommand cmd = new SubedCommand("name");
+
+ try {
+ cmd.add(new MockCommand("id"));
+ } catch (InvalidCommandName e) {
+ fail("addition of command with valid id failed");
+ assertNotNull(e);
+ }
+ try {
+ cmd.add(new MockCommand("id"));
+ fail("addition of command with already used id succeeded");
+ } catch (InvalidCommandName e) {
+ //
+ assertNotNull(e);
+ }
+ try {
+ cmd.add(new MockCommand(""));
+ fail("addition of command with invalid id succeeded");
+ } catch (InvalidCommandName e) {
+ //
+ assertNotNull(e);
+ }
+
+ }
+
+ /** Test method for
+ * {@link fr.bigeon.gclc.command.SubedCommand#execute(java.lang.String[])}. */
+ @Test
+ public final void testExecute() {
+ SubedCommand cmd = new SubedCommand("name");
+
+ MockCommand mock = new MockCommand("id");
+ try {
+ cmd.add(mock);
+ cmd.add(new Command("fail") {
+
+ @Override
+ public String tip() {
+ return null;
+ }
+
+ @Override
+ public void execute(String... args) throws CommandRunException {
+ throw new CommandRunException("Failing command", null);
+ }
+ });
+ } catch (InvalidCommandName e) {
+ fail("addition of command with valid id failed");
+ assertNotNull(e);
+ }
+
+ try {
+ cmd.execute("id");
+ } catch (CommandRunException e) {
+ fail("Unexpected exception when running mock command");
+ assertNotNull(e);
+ }
+ try {
+ cmd.execute("fail");
+ fail("Fail command error should be re thrown");
+ } catch (CommandRunException e) {
+ assertNotNull(e);
+ assertEquals(cmd, e.getSource());
+ }
+ try {
+ cmd.execute();
+ fail("Request for inexistent default command should fail");
+ } catch (CommandRunException e) {
+ assertNotNull(e);
+ assertEquals(cmd, e.getSource());
+ }
+ cmd = new SubedCommand("name", mock);
+
+ try {
+ cmd.add(mock);
+ cmd.add(new Command("fail") {
+
+ @Override
+ public String tip() {
+ return null;
+ }
+
+ @Override
+ public void execute(String... args) throws CommandRunException {
+ throw new CommandRunException("Failing command", this);
+ }
+ });
+ } catch (InvalidCommandName e) {
+ fail("addition of command with valid id failed");
+ assertNotNull(e);
+ }
+
+ try {
+ cmd.execute("id");
+ } catch (CommandRunException e) {
+ fail("Unexpected exception when running mock command");
+ assertNotNull(e);
+ }
+ try {
+ cmd.execute("fail");
+ fail("Fail command error should be re thrown");
+ } catch (CommandRunException e) {
+ assertNotNull(e);
+ assertEquals(cmd.get("fail"), e.getSource());
+ }
+ try {
+ cmd.execute();
+ } catch (CommandRunException e) {
+ fail("Request for default command should execute default command");
+ assertNotNull(e);
+ }
+ }
+
+ /** Test method for
+ * {@link fr.bigeon.gclc.command.SubedCommand#executeSub(java.lang.String, java.lang.String[])}. */
+ @Test
+ public final void testExecuteSub() {
+ SubedCommand cmd = new SubedCommand("name");
+
+ MockCommand mock = new MockCommand("id");
+ try {
+ cmd.add(mock);
+ cmd.add(new Command("fail") {
+
+ @Override
+ public String tip() {
+ return null;
+ }
+
+ @Override
+ public void execute(String... args) throws CommandRunException {
+ throw new CommandRunException("Failing command", this);
+ }
+ });
+ } catch (InvalidCommandName e) {
+ fail("addition of command with valid id failed");
+ assertNotNull(e);
+ }
+
+ try {
+ cmd.executeSub("id");
+ } catch (CommandRunException e) {
+ fail("Unexpected exception when running mock command");
+ assertNotNull(e);
+ }
+ try {
+ cmd.executeSub("fail");
+ fail("Fail command error should be re thrown");
+ } catch (CommandRunException e) {
+ assertNotNull(e);
+ assertEquals(cmd.get("fail"), e.getSource());
+ }
+ }
+
+ /** Test method for
+ * {@link fr.bigeon.gclc.command.SubedCommand#get(java.lang.String)}. */
+ @Test
+ public final void testGet() {
+ SubedCommand cmd = new SubedCommand("name");
+
+ assertNull(cmd.get("id"));
+
+ MockCommand mock = new MockCommand("id");
+ try {
+ cmd.add(mock);
+ } catch (InvalidCommandName e) {
+ fail("addition of command with valid id failed");
+ assertNotNull(e);
+ }
+ assertEquals(mock, cmd.get("id"));
+ }
+
+ /** Test method for
+ * {@link fr.bigeon.gclc.command.SubedCommand#getCommandName()}. */
+ @Test
+ public final void testGetCommandName() {
+ SubedCommand cmd = new SubedCommand("name");
+ assertEquals("name", cmd.getCommandName());
+ cmd = new SubedCommand("name with spaces");
+ assertEquals("name with spaces", cmd.getCommandName());
+ cmd = new SubedCommand("name", "some tip");
+ assertEquals("name", cmd.getCommandName());
+ cmd = new SubedCommand("name", new MockCommand(""));
+ assertEquals("name", cmd.getCommandName());
+ cmd = new SubedCommand("name", new MockCommand(""), "some tip");
+ assertEquals("name", cmd.getCommandName());
+ }
+
+ /** Test method for
+ * {@link fr.bigeon.gclc.command.SubedCommand#help(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String[])}. */
+ @Test
+ public final void testHelp() {
+ SubedCommand cmd = new SubedCommand("name");
+
+ ICommand mock = new MockCommand("id");
+ try {
+ cmd.add(mock);
+ } catch (InvalidCommandName e) {
+ fail("addition of command with valid id failed");
+ assertNotNull(e);
+ }
+
+ try (TestConsoleManager manager = new TestConsoleManager();
+ TestConsoleManager manager2 = new TestConsoleManager()) {
+ cmd.help(manager);
+ assertEquals("\tid", manager.readNextLine());
+ cmd.help(manager, "id");
+ cmd.help(manager, "inexistent");
+ } catch (IOException e) {
+ fail("Unexpected exception when running help");
+ assertNotNull(e);
+ }
+
+ cmd = new SubedCommand("name", mock);
+
+ try {
+ cmd.add(mock);
+ } catch (InvalidCommandName e) {
+ fail("addition of command with valid id failed");
+ assertNotNull(e);
+ }
+
+ try (TestConsoleManager manager = new TestConsoleManager();
+ TestConsoleManager manager2 = new TestConsoleManager()) {
+ cmd.help(manager);
+ assertEquals("\tid", manager.readNextLine());
+ } catch (IOException e) {
+ fail("Unexpected exception when running help");
+ assertNotNull(e);
+ }
+
+ mock = new ICommand() {
+
+ @Override
+ public String tip() {
+ return "tip";
+ }
+
+ @Override
+ public void help(ConsoleManager manager,
+ String... args) throws IOException {
+ //
+ }
+
+ @Override
+ public String getCommandName() {
+ return "id";
+ }
+
+ @Override
+ public void execute(String... args) throws CommandRunException {
+ //
+ }
+ };
+ cmd = new SubedCommand("name", mock);
+
+ try {
+ cmd.add(mock);
+ } catch (InvalidCommandName e) {
+ fail("addition of command with valid id failed");
+ assertNotNull(e);
+ }
+
+ try (TestConsoleManager manager = new TestConsoleManager();
+ TestConsoleManager manager2 = new TestConsoleManager()) {
+ cmd.help(manager);
+ assertEquals("\ttip", manager.readNextLine());
+ assertEquals("\tid: tip", manager.readNextLine());
+ } catch (IOException e) {
+ fail("Unexpected exception when running help");
+ assertNotNull(e);
+ }
+ }
+
+ /** Test method for {@link fr.bigeon.gclc.command.SubedCommand#tip()}. */
+ @Test
+ public final void testTip() {
+ SubedCommand cmd = new SubedCommand("name");
+ assertNull(cmd.tip());
+ cmd = new SubedCommand("name with spaces", new MockCommand(""), "tip");
+ assertEquals("tip", cmd.tip());
+ }
+}
diff --git a/gclc/src/test/java/fr/bigeon/gclc/exception/CommandRunExceptionTest.java b/gclc/src/test/java/fr/bigeon/gclc/exception/CommandRunExceptionTest.java
new file mode 100644
index 0000000..2a29acc
--- /dev/null
+++ b/gclc/src/test/java/fr/bigeon/gclc/exception/CommandRunExceptionTest.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright Bigeon Emmanuel (2014)
+ *
+ * emmanuel@bigeon.fr
+ *
+ * This software is a computer program whose purpose is to
+ * provide a generic framework for console applications.
+ *
+ * This software is governed by the CeCILL license under French law and
+ * abiding by the rules of distribution of free software. You can use,
+ * modify and/or redistribute the software under the terms of the CeCILL
+ * license as circulated by CEA, CNRS and INRIA at the following URL
+ * "http://www.cecill.info".
+ *
+ * As a counterpart to the access to the source code and rights to copy,
+ * modify and redistribute granted by the license, users are provided only
+ * with a limited warranty and the software's author, the holder of the
+ * economic rights, and the successive licensors have only limited
+ * liability.
+ *
+ * In this respect, the user's attention is drawn to the risks associated
+ * with loading, using, modifying and/or developing or reproducing the
+ * software by the user in light of its specific status of free software,
+ * that may mean that it is complicated to manipulate, and that also
+ * therefore means that it is reserved for developers and experienced
+ * professionals having in-depth computer knowledge. Users are therefore
+ * encouraged to load and test the software's suitability as regards their
+ * requirements in conditions enabling the security of their systems and/or
+ * data to be ensured and, more generally, to use and operate it in the
+ * same conditions as regards security.
+ *
+ * The fact that you are presently reading this means that you have had
+ * knowledge of the CeCILL license and that you accept its terms.
+ */
+/**
+ * gclc:fr.bigeon.gclc.exception.CommandRunExceptionTest.java
+ * Created on: Nov 19, 2016
+ */
+package fr.bigeon.gclc.exception;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+import fr.bigeon.gclc.command.ICommand;
+import fr.bigeon.gclc.command.MockCommand;
+
+/**
+ *
+ * TODO
+ *
+ * @author Emmanuel Bigeon
+ *
+ */
+public class CommandRunExceptionTest {
+
+ /**
+ * Test method for {@link fr.bigeon.gclc.exception.CommandRunException#getLocalizedMessage()}.
+ */
+ @Test
+ public final void testGetLocalizedMessage() {
+ CommandRunException e;
+ ICommand cmd = new MockCommand("name");
+ String messageInner = "inner";
+ String message = "message";
+ e = new CommandRunException(message,
+ new CommandRunException(messageInner, new MockCommand("name")), //$NON-NLS-1$
+ cmd);
+
+ assertEquals(message + ": " + messageInner, e.getLocalizedMessage());
+ e = new CommandRunException(message, cmd);
+ assertEquals(message, e.getLocalizedMessage());
+ }
+
+}
diff --git a/gclc/src/test/java/fr/bigeon/gclc/manager/SystemConsoleManagerTest.java b/gclc/src/test/java/fr/bigeon/gclc/manager/SystemConsoleManagerTest.java
new file mode 100644
index 0000000..0210670
--- /dev/null
+++ b/gclc/src/test/java/fr/bigeon/gclc/manager/SystemConsoleManagerTest.java
@@ -0,0 +1,148 @@
+/*
+ * Copyright Bigeon Emmanuel (2014)
+ *
+ * emmanuel@bigeon.fr
+ *
+ * This software is a computer program whose purpose is to
+ * provide a generic framework for console applications.
+ *
+ * This software is governed by the CeCILL license under French law and
+ * abiding by the rules of distribution of free software. You can use,
+ * modify and/or redistribute the software under the terms of the CeCILL
+ * license as circulated by CEA, CNRS and INRIA at the following URL
+ * "http://www.cecill.info".
+ *
+ * As a counterpart to the access to the source code and rights to copy,
+ * modify and redistribute granted by the license, users are provided only
+ * with a limited warranty and the software's author, the holder of the
+ * economic rights, and the successive licensors have only limited
+ * liability.
+ *
+ * In this respect, the user's attention is drawn to the risks associated
+ * with loading, using, modifying and/or developing or reproducing the
+ * software by the user in light of its specific status of free software,
+ * that may mean that it is complicated to manipulate, and that also
+ * therefore means that it is reserved for developers and experienced
+ * professionals having in-depth computer knowledge. Users are therefore
+ * encouraged to load and test the software's suitability as regards their
+ * requirements in conditions enabling the security of their systems and/or
+ * data to be ensured and, more generally, to use and operate it in the
+ * same conditions as regards security.
+ *
+ * The fact that you are presently reading this means that you have had
+ * knowledge of the CeCILL license and that you accept its terms.
+ */
+/**
+ * gclc:fr.bigeon.gclc.manager.SystemConsoleManagerTest.java
+ * Created on: Nov 19, 2016
+ */
+package fr.bigeon.gclc.manager;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PipedInputStream;
+import java.io.PipedOutputStream;
+import java.io.PrintStream;
+import java.nio.charset.Charset;
+
+import org.junit.Test;
+
+/**
+ * TODO
+ *
+ * @author Emmanuel Bigeon */
+public class SystemConsoleManagerTest {
+
+ /** Test method for
+ * {@link fr.bigeon.gclc.manager.SystemConsoleManager#prompt()}. */
+ @Test
+ public final void testPrompt() {
+
+ try {
+ PipedOutputStream outStream = new PipedOutputStream();
+ InputStream in = new PipedInputStream(outStream);
+ final PrintStream out = new PrintStream(outStream);
+ final String test = "test";
+ SystemConsoleManager manager = new SystemConsoleManager(System.out,
+ in, Charset.forName("UTF-8"));
+
+ Thread th = new Thread(new Runnable() {
+
+ @SuppressWarnings("synthetic-access")
+ @Override
+ public void run() {
+ out.println(test);
+ }
+ });
+
+ th.start();
+ assertEquals(test, manager.prompt());
+
+ th.join();
+ } catch (IOException | InterruptedException e) {
+ assertNull(e);
+ }
+ }
+
+ /** Test method for
+ * {@link fr.bigeon.gclc.manager.SystemConsoleManager#setPrompt(java.lang.String)}. */
+ @Test
+ public final void testSetPrompt() {
+ SystemConsoleManager manager = new SystemConsoleManager();
+ String prt = "++";
+ manager.setPrompt(prt);
+ assertEquals(prt, manager.getPrompt());
+ }
+
+ /** Test method for
+ * {@link fr.bigeon.gclc.manager.SystemConsoleManager#isClosed()}. */
+ @Test
+ public final void testIsClosed() {
+ try {
+ PipedOutputStream outStream = new PipedOutputStream();
+ InputStream in = new PipedInputStream(outStream);
+ final PrintStream out = new PrintStream(outStream);
+ final String test = "test";
+ SystemConsoleManager manager = new SystemConsoleManager(System.out,
+ in, Charset.forName("UTF-8"));
+
+ Thread th = new Thread(new Runnable() {
+
+ @SuppressWarnings("synthetic-access")
+ @Override
+ public void run() {
+ out.println(test);
+ }
+ });
+
+ th.start();
+ assertEquals(test, manager.prompt());
+ assertFalse(manager.isClosed());
+ manager.close();
+ assertTrue(manager.isClosed());
+ try {
+ manager.prompt();
+ fail("prompt on closed manager");
+ } catch (IOException e) {
+ assertNotNull(e);
+ }
+ try {
+ manager.println(test);
+ fail("prompt on closed manager");
+ } catch (IOException e) {
+ assertNotNull(e);
+ }
+ th.join();
+ } catch (IOException | InterruptedException e) {
+ assertNull(e);
+ }
+ }
+
+}
diff --git a/gclc/src/test/java/fr/bigeon/gclc/prompt/CLIPrompterMessagesTest.java b/gclc/src/test/java/fr/bigeon/gclc/prompt/CLIPrompterMessagesTest.java
new file mode 100644
index 0000000..32537ef
--- /dev/null
+++ b/gclc/src/test/java/fr/bigeon/gclc/prompt/CLIPrompterMessagesTest.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright Bigeon Emmanuel (2014)
+ *
+ * emmanuel@bigeon.fr
+ *
+ * This software is a computer program whose purpose is to
+ * provide a generic framework for console applications.
+ *
+ * This software is governed by the CeCILL license under French law and
+ * abiding by the rules of distribution of free software. You can use,
+ * modify and/or redistribute the software under the terms of the CeCILL
+ * license as circulated by CEA, CNRS and INRIA at the following URL
+ * "http://www.cecill.info".
+ *
+ * As a counterpart to the access to the source code and rights to copy,
+ * modify and redistribute granted by the license, users are provided only
+ * with a limited warranty and the software's author, the holder of the
+ * economic rights, and the successive licensors have only limited
+ * liability.
+ *
+ * In this respect, the user's attention is drawn to the risks associated
+ * with loading, using, modifying and/or developing or reproducing the
+ * software by the user in light of its specific status of free software,
+ * that may mean that it is complicated to manipulate, and that also
+ * therefore means that it is reserved for developers and experienced
+ * professionals having in-depth computer knowledge. Users are therefore
+ * encouraged to load and test the software's suitability as regards their
+ * requirements in conditions enabling the security of their systems and/or
+ * data to be ensured and, more generally, to use and operate it in the
+ * same conditions as regards security.
+ *
+ * The fact that you are presently reading this means that you have had
+ * knowledge of the CeCILL license and that you accept its terms.
+ */
+/**
+ * gclc:fr.bigeon.gclc.prompt.CLIPrompterMessagesTest.java
+ * Created on: Nov 19, 2016
+ */
+package fr.bigeon.gclc.prompt;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+/**
+ *
+ * TODO
+ *
+ * @author Emmanuel Bigeon
+ *
+ */
+public class CLIPrompterMessagesTest {
+
+ /**
+ * Test method for {@link fr.bigeon.gclc.prompt.CLIPrompterMessages#getString(java.lang.String, java.lang.Object[])}.
+ */
+ @Test
+ public final void testGetString() {
+ String key = "bad.key";
+ assertEquals('!' + key + '!', CLIPrompterMessages.getString(key));
+ assertEquals('!' + key + '!',
+ CLIPrompterMessages.getString(key, "some arg"));
+ assertEquals('!' + key + '!',
+ CLIPrompterMessages.getString(key, new Object[] {}));
+ }
+
+}
diff --git a/gclc/src/test/java/fr/bigeon/gclc/prompt/CLIPrompterTest.java b/gclc/src/test/java/fr/bigeon/gclc/prompt/CLIPrompterTest.java
new file mode 100644
index 0000000..d522fe7
--- /dev/null
+++ b/gclc/src/test/java/fr/bigeon/gclc/prompt/CLIPrompterTest.java
@@ -0,0 +1,1080 @@
+/*
+ * Copyright Bigeon Emmanuel (2014)
+ *
+ * emmanuel@bigeon.fr
+ *
+ * This software is a computer program whose purpose is to
+ * provide a generic framework for console applications.
+ *
+ * This software is governed by the CeCILL license under French law and
+ * abiding by the rules of distribution of free software. You can use,
+ * modify and/or redistribute the software under the terms of the CeCILL
+ * license as circulated by CEA, CNRS and INRIA at the following URL
+ * "http://www.cecill.info".
+ *
+ * As a counterpart to the access to the source code and rights to copy,
+ * modify and redistribute granted by the license, users are provided only
+ * with a limited warranty and the software's author, the holder of the
+ * economic rights, and the successive licensors have only limited
+ * liability.
+ *
+ * In this respect, the user's attention is drawn to the risks associated
+ * with loading, using, modifying and/or developing or reproducing the
+ * software by the user in light of its specific status of free software,
+ * that may mean that it is complicated to manipulate, and that also
+ * therefore means that it is reserved for developers and experienced
+ * professionals having in-depth computer knowledge. Users are therefore
+ * encouraged to load and test the software's suitability as regards their
+ * requirements in conditions enabling the security of their systems and/or
+ * data to be ensured and, more generally, to use and operate it in the
+ * same conditions as regards security.
+ *
+ * The fact that you are presently reading this means that you have had
+ * knowledge of the CeCILL license and that you accept its terms.
+ */
+/**
+ * gclc:fr.bigeon.gclc.prompt.CLIPrompterTest.java
+ * Created on: Nov 18, 2016
+ */
+package fr.bigeon.gclc.prompt;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import fr.bigeon.gclc.test.utils.TestConsoleManager;
+
+/**
+ * TODO
+ *
+ * @author Emmanuel Bigeon */
+public class CLIPrompterTest {
+
+ /** @throws java.lang.Exception if there are exceptions */
+ @Before
+ public void setUp() throws Exception {
+ //
+ }
+
+ /** @throws java.lang.Exception if there are exceptions */
+ @After
+ public void tearDown() throws Exception {
+ //
+ }
+
+ /** Test method for
+ * {@link fr.bigeon.gclc.prompt.CLIPrompter#promptBoolean(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String)}. */
+ @Test
+ public final void testPromptBoolean() {
+ try (final TestConsoleManager test = new TestConsoleManager()) {
+ Thread th = new Thread(new Runnable() {
+
+ @Override
+ public void run() {
+ try {
+ assertTrue(
+ CLIPrompter.promptBoolean(test, "My message")); //$NON-NLS-1$
+ assertTrue(
+ CLIPrompter.promptBoolean(test, "My message")); //$NON-NLS-1$
+ assertFalse(
+ CLIPrompter.promptBoolean(test, "My message")); //$NON-NLS-1$
+ assertFalse(
+ CLIPrompter.promptBoolean(test, "My message")); //$NON-NLS-1$
+ } catch (IOException e) {
+ fail("Unexpected io excpetion"); //$NON-NLS-1$
+ e.printStackTrace();
+ }
+ }
+ });
+ th.start();
+ assertTrue(test.readNextLine().startsWith("My message")); //$NON-NLS-1$
+ test.type(""); //$NON-NLS-1$
+ test.readNextLine();
+ assertTrue(test.readNextLine().startsWith("My message")); //$NON-NLS-1$
+ test.type("Y"); //$NON-NLS-1$
+ assertTrue(test.readNextLine().startsWith("My message")); //$NON-NLS-1$
+ test.type("yes"); //$NON-NLS-1$
+ assertTrue(test.readNextLine().startsWith("My message")); //$NON-NLS-1$
+ test.type("N"); //$NON-NLS-1$
+ assertTrue(test.readNextLine().startsWith("My message")); //$NON-NLS-1$
+ test.type("nO"); //$NON-NLS-1$
+ th.join();
+ } catch (IOException | InterruptedException e) {
+ fail("Unexpected excpetion"); //$NON-NLS-1$
+ e.printStackTrace();
+ }
+
+ }
+
+ /** Test method for
+ * {@link fr.bigeon.gclc.prompt.CLIPrompter#promptChoice(fr.bigeon.gclc.manager.ConsoleManager, java.util.List, java.util.List, java.lang.String, java.lang.String)}. */
+ @Test
+ public final void testPromptChoiceConsoleManagerListOfStringListOfUStringString() {
+ try (final TestConsoleManager test = new TestConsoleManager()) {
+ final List