Compare commits

..

10 Commits

15 changed files with 365 additions and 147 deletions

View File

@@ -70,7 +70,7 @@ of Emmanuel Bigeon. -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>gclc-socket</artifactId>
<version>1.1.4</version>
<version>1.1.6</version>
<packaging>jar</packaging>
<url>http://www.bigeon.fr/emmanuel</url>
<properties>
@@ -78,16 +78,10 @@ of Emmanuel Bigeon. -->
<project.scm.id>git.bigeon.net</project.scm.id>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>fr.bigeon</groupId>
<artifactId>gclc</artifactId>
<version>1.3.2</version>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>fr.bigeon</groupId>
@@ -104,6 +98,6 @@ of Emmanuel Bigeon. -->
<description>Socket implementation of GCLC</description>
<scm>
<developerConnection>scm:git:gogs@git.code.bigeon.net:emmanuel/gclc.git</developerConnection>
<tag>gclc-socket-1.1.4</tag>
<tag>gclc-socket-1.1.6</tag>
</scm>
</project>

View File

@@ -95,12 +95,12 @@ public class SocketConsoleApplicationShell implements Runnable, AutoCloseable {
@Override
public void run() {
try {
while (!socket.isOutputShutdown()) {
while (!socket.isOutputShutdown() &&
while (!socket.isClosed()) {
while (!socket.isClosed() &&
!consoleManager.available()) {
waitASec();
}
if (socket.isOutputShutdown()) {
if (socket.isClosed()) {
return;
}
String m = consoleManager.readNextLine();
@@ -259,14 +259,13 @@ public class SocketConsoleApplicationShell implements Runnable, AutoCloseable {
* @throws IOException if the communication failed */
private void communicate(final Socket socket, final PrintWriter writer,
BufferedReader in) throws IOException {
Thread th = new Thread(new OutputForwardRunnable(writer, socket),
"ClientComm"); //$NON-NLS-1$
OutputForwardRunnable cc = new OutputForwardRunnable(writer, socket);
Thread th = new Thread(cc, "ClientComm"); //$NON-NLS-1$
th.start();
if (autoClose) {
communicateOnce(socket, in);
} else {
communicateLoop(socket, in);
}
}
@@ -279,8 +278,21 @@ public class SocketConsoleApplicationShell implements Runnable, AutoCloseable {
Thread th = new Thread(reading, "gclcToApp"); //$NON-NLS-1$
th.start();
communicationContent(reading);
doEndCommunication(reading);
}
/** @param reading the reading runnable
* @throws IOException if the end of communication failed */
private void doEndCommunication(ReadingRunnable reading) throws IOException {
reading.setRunning(false);
socket.shutdownOutput();
Thread wait = consoleManager.getWaitForDelivery("Bye."); //$NON-NLS-1$
consoleManager.println("Bye."); //$NON-NLS-1$
try {
wait.join();
} catch (InterruptedException e) {
LOGGER.warning("The Bye wait was interrupted."); //$NON-NLS-1$
LOGGER.log(Level.FINE, "An interruption occured", e); //$NON-NLS-1$
}
}
/** @param socket the socket
@@ -294,12 +306,7 @@ public class SocketConsoleApplicationShell implements Runnable, AutoCloseable {
while (app.isRunning() && communicationContent(reading)) {
// keep on going
}
reading.setRunning(false);
while (consoleManager.available()) {
waitASec();
}
socket.shutdownOutput();
doEndCommunication(reading);
}
/** @param reading the reading
@@ -322,7 +329,6 @@ public class SocketConsoleApplicationShell implements Runnable, AutoCloseable {
}
String ln = reading.getMessage();
if (ln.equals(close)) {
consoleManager.println("Bye."); //$NON-NLS-1$
return false;
}
// Pass command to application

View File

@@ -85,27 +85,20 @@ public class SocketConsoleApplicationTest {
new InputStreamReader(kkSocket.getInputStream()));) {
String fromServer;
int i = 0;
int i = -1;
String[] cmds = {"help", "toto", "test", "bye"};
while ((fromServer = in.readLine()) != null) {
LOGGER.fine("Server: \n" + fromServer);
if (fromServer.equals("Bye.")) {
i++;
fromServer = consumeToPrompt(fromServer, in);
if (fromServer == null || fromServer.equals("Bye.")) {
break;
}
while (fromServer != null && !fromServer.equals("> ")) {
fromServer = in.readLine();
LOGGER.fine("Server: \n" + fromServer);
}
if (fromServer == null) {
fail("Null pointer");
}
final String fromUser = cmds[i];
if (fromUser != null) {
LOGGER.fine("Client: " + fromUser);
out.println(fromUser);
}
i++;
}
assertEquals(4, i);
} catch (final IOException e) {
@@ -123,19 +116,15 @@ public class SocketConsoleApplicationTest {
String[] cmds = {"help", "toto", "test",
ConsoleTestApplication.EXIT};
while ((fromServer = in.readLine()) != null) {
LOGGER.fine("Server: \n" + fromServer);
while (fromServer != null && !fromServer.equals("> ")) {
LOGGER.fine("Server: \n" + fromServer);
fromServer = in.readLine();
}
if (fromServer == null) {
fromServer = consumeToPrompt(fromServer, in);
if (fromServer == null || fromServer.equals("Bye.")) {
break;
}
LOGGER.fine("Server: \n" + fromServer);
LOGGER.info("Server: \n" + fromServer);
final String fromUser = cmds[i];
if (fromUser != null) {
LOGGER.fine("Client: " + fromUser);
LOGGER.info("Client: " + fromUser);
out.println(fromUser);
}
i++;
@@ -161,11 +150,8 @@ public class SocketConsoleApplicationTest {
String[] cmds = {"help", "toto", "test",
ConsoleTestApplication.EXIT};
while ((fromServer = in.readLine()) != null) {
while (fromServer != null && !fromServer.equals("> ")) {
fromServer = in.readLine();
LOGGER.fine("Server: \n" + fromServer);
}
if (fromServer == null) {
fromServer = consumeToPrompt(fromServer, in);
if (fromServer == null || fromServer.equals("Bye.")) {
break;
}
@@ -207,17 +193,11 @@ public class SocketConsoleApplicationTest {
int i = 0;
String[] cmds = {"help", "test", "close"};
while ((fromServer = in.readLine()) != null) {
assertTrue(i < 1);
while (fromServer != null && !fromServer.equals("> ") &&
!fromServer.equals("See you")) {
fromServer = in.readLine();
LOGGER.fine("Server: \n" + fromServer);
}
if (fromServer == null || fromServer.equals("Bye.") ||
fromServer.equals("See you")) {
fromServer = consumeToPrompt(fromServer, in);
if (fromServer == null || fromServer.equals("Bye.")) {
break;
}
assertTrue(i < 1);
final String fromUser = cmds[i];
if (fromUser != null) {
LOGGER.fine("Client: " + fromUser);
@@ -243,4 +223,19 @@ public class SocketConsoleApplicationTest {
e.printStackTrace();
}
}
/** @param in the input
* @return the string
* @throws IOException if the input reading failed */
private String consumeToPrompt(String server,
BufferedReader in) throws IOException {
String fromServer = server;
LOGGER.fine("Server: \n" + fromServer);
while (fromServer != null && !fromServer.equals("Bye.") &&
!fromServer.equals("> ")) {
fromServer = in.readLine();
LOGGER.fine("Server: \n" + fromServer);
}
return fromServer;
}
}

View File

@@ -51,7 +51,7 @@
<dependency>
<groupId>fr.bigeon</groupId>
<artifactId>gclc</artifactId>
<version>1.3.2</version>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>fr.bigeon</groupId>

View File

@@ -35,7 +35,7 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>gclc</artifactId>
<version>1.3.3-SNAPSHOT</version>
<version>1.3.4-SNAPSHOT</version>
<packaging>jar</packaging>
<url>http://www.bigeon.fr/emmanuel</url>
<properties>

View File

@@ -148,7 +148,7 @@ public class ConsoleApplication implements ICommandProvider {
args = GCLCConstants.splitCommand(cmd);
} catch (CommandParsingException e1) {
manager.println("Command line cannot be parsed"); //$NON-NLS-1$
LOGGER.log(Level.INFO, "Invalid user command " + cmd, e1); //$NON-NLS-1$
LOGGER.log(Level.FINE, "Invalid user command " + cmd, e1); //$NON-NLS-1$
return;
}
if (!args.isEmpty()) {
@@ -156,7 +156,7 @@ public class ConsoleApplication implements ICommandProvider {
executeSub(args.get(0), Arrays.copyOfRange(
args.toArray(new String[0]), 1, args.size()));
} catch (final CommandRunException e) {
LOGGER.log(Level.WARNING, "Command failed: " + cmd, e); //$NON-NLS-1$
LOGGER.log(Level.FINE, "Command failed: " + cmd, e); //$NON-NLS-1$
manager.println(Messages
.getString("ConsoleApplication.cmd.failed", cmd)); //$NON-NLS-1$
manager.println(e.getLocalizedMessage());
@@ -187,8 +187,10 @@ public class ConsoleApplication implements ICommandProvider {
} catch (IOException e) {
// The manager was closed
running = false;
LOGGER.log(Level.WARNING,
"The console manager was closed. Closing the application as no one can reach it.", //$NON-NLS-1$
LOGGER.warning(
"The console manager was closed. Closing the application as no one can reach it."); //$NON-NLS-1$
LOGGER.log(Level.FINE,
"An exception caused the closing of the application", //$NON-NLS-1$
e);
return;
}
@@ -201,8 +203,9 @@ public class ConsoleApplication implements ICommandProvider {
} catch (IOException e) {
// The manager was closed
running = false;
LOGGER.log(Level.WARNING,
"The console manager was closed.", //$NON-NLS-1$
LOGGER.warning("The console manager was closed."); //$NON-NLS-1$
LOGGER.log(Level.FINE,
"An exception occured when trying to print the good by e message... The application will still close.", //$NON-NLS-1$
e);
}
}
@@ -225,14 +228,17 @@ public class ConsoleApplication implements ICommandProvider {
}
interpretCommand(cmd);
} catch (InterruptedIOException e) {
LOGGER.log(Level.INFO,
"Prompt interrupted. It is likely the application is closing.", //$NON-NLS-1$
LOGGER.info(
"Prompt interrupted. It is likely the application is closing."); //$NON-NLS-1$
LOGGER.log(Level.FINER, "Interruption of the prompt.", //$NON-NLS-1$
e);
} catch (IOException e) {
// The manager was closed
running = false;
LOGGER.log(Level.WARNING,
"The console manager was closed. Closing the application as no one can reach it.", //$NON-NLS-1$
LOGGER.warning(
"The console manager was closed. Closing the application as no one can reach it."); //$NON-NLS-1$
LOGGER.log(Level.FINE,
"An exception caused the closing of the application", //$NON-NLS-1$
e);
}
}

View File

@@ -45,6 +45,8 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import fr.bigeon.gclc.exception.CommandParsingException;
/** <p>
* An object representing a collection of parameters. It is used for defaulting
* values.
@@ -98,8 +100,8 @@ public class CommandParameters {
}
/** @param args the arguments to parse
* @return if the arguments were parsed */
public boolean parseArgs(String... args) {
* @throws CommandParsingException if the arguments parsing failed */
public void parseArgs(String... args) throws CommandParsingException {
int i = 0;
while (i < args.length) {
String next = null;
@@ -108,11 +110,11 @@ public class CommandParameters {
}
int p = parseArg(args[i], next);
if (p == 0) {
return false;
throw new CommandParsingException(
"Invalid parameter " + args[i]); //$NON-NLS-1$
}
i += p;
}
return true;
}
@@ -126,7 +128,7 @@ public class CommandParameters {
* @return the number of element read */
private int parseArg(String arg, String next) {
if (!arg.startsWith("-")) { //$NON-NLS-1$
return 1;
return strict ? 0 : 1;
}
String name = arg.substring(1);
if (booleanArguments.containsKey(name)) {

View File

@@ -48,6 +48,7 @@ import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import fr.bigeon.gclc.exception.CommandParsingException;
import fr.bigeon.gclc.exception.CommandRunException;
import fr.bigeon.gclc.exception.CommandRunExceptionType;
import fr.bigeon.gclc.exception.InvalidParameterException;
@@ -60,8 +61,7 @@ import fr.bigeon.gclc.manager.ConsoleManager;
public abstract class ParametrizedCommand extends Command {
/** If the command may use interactive prompting for required parameters
* that
* were not provided on execution */
* that were not provided on execution */
private boolean interactive = true;
/** The manager */
protected final ConsoleManager manager;
@@ -72,8 +72,7 @@ public abstract class ParametrizedCommand extends Command {
/** The parameters mandatory status */
private final Map<String, Boolean> params = new HashMap<>();
/** The restriction of provided parameters on execution to declared
* paramters
* in the status maps. */
* paramters in the status maps. */
private final boolean strict;
/** @param manager the manager
@@ -110,7 +109,11 @@ public abstract class ParametrizedCommand extends Command {
* @param param the parameter identification
* @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 */
* @throws InvalidParameterException if the parameter was invalid
* @deprecated since gclc-1.3.3, use the
* {@link #addStringParameter(String, boolean)} and
* {@link #addBooleanParameter(String)} */
@Deprecated
protected void addParameter(String param, boolean stringParameter,
boolean needed) throws InvalidParameterException {
if (params.containsKey(param)) {
@@ -131,10 +134,41 @@ public abstract class ParametrizedCommand extends Command {
}
}
/** Add a boolean parameter to defined parmaters.
*
* @param flag the boolean flag
* @throws InvalidParameterException if the parameter is already defined as
* a string parameter */
protected void addBooleanParameter(String flag) throws InvalidParameterException {
if (params.containsKey(flag) && stringParams.containsKey(flag)) {
throw new InvalidParameterException(
"Parameter is already defined as string"); //$NON-NLS-1$
}
boolParams.add(flag);
params.put(flag, Boolean.valueOf(false));
}
/** Add a string parameter to defined parmaters.
*
* @param flag the parameter flag
* @param needed if the parameter's absence should cause an exception
* @throws InvalidParameterException if the parameter is already defined as
* a boolean parameter */
protected void addStringParameter(String flag,
boolean needed) throws InvalidParameterException {
if (params.containsKey(flag)) {
checkParam(flag, needed);
return;
}
stringParams.put(flag, Boolean.valueOf(needed));
params.put(flag, Boolean.valueOf(needed));
}
/** @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 */
@Deprecated
private void checkParam(String param, boolean stringParameter,
boolean needed) throws InvalidParameterException {
if (stringParameter) {
@@ -154,6 +188,22 @@ public abstract class ParametrizedCommand extends Command {
}
}
/** @param param the string parameter
* @param needed if the parameter is needed
* @throws InvalidParameterException if the new definition is invalid */
private void checkParam(String param,
boolean needed) throws InvalidParameterException {
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$
}
/** @param parameters the command parameters
* @throws CommandRunException if the command failed */
protected abstract void doExecute(CommandParameters parameters) throws CommandRunException;
@@ -163,12 +213,13 @@ public abstract class ParametrizedCommand extends Command {
@SuppressWarnings("boxing")
@Override
public final void execute(String... args) throws CommandRunException {
final CommandParameters parameters = new CommandParameters(
boolParams, stringParams.keySet(), strict);
if (!parameters.parseArgs(args)) {
// the parameters could not be correctly parsed
final CommandParameters parameters = new CommandParameters(boolParams,
stringParams.keySet(), strict);
try {
parameters.parseArgs(args);
} catch (CommandParsingException e) {
throw new CommandRunException(CommandRunExceptionType.USAGE,
"Unable to read arguments", this); //$NON-NLS-1$
"Unable to read arguments", e, this); //$NON-NLS-1$
}
final List<String> toProvide = new ArrayList<>();
for (final Entry<String, Boolean> string : params.entrySet()) {

View File

@@ -172,4 +172,12 @@ public final class PipedConsoleManager
public void interruptPrompt() {
innerManager.interruptPrompt();
}
/** @param message the message
* @return the thread to join to wait for message delivery
* @see fr.bigeon.gclc.manager.ReadingRunnable#getWaitForDelivery(java.lang.String) */
public Thread getWaitForDelivery(String message) {
return reading.getWaitForDelivery(message);
}
}

View File

@@ -43,6 +43,8 @@ import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -69,6 +71,15 @@ public class ReadingRunnable implements Runnable {
private final Object lock = new Object();
/** The waiting status for a message */
private boolean waiting;
/**
* The blocker for a given message
*/
private final Map<String, Object> messageBlocker = new HashMap<>();
/**
* The lock
*/
private final Object messageBlockerLock = new Object();
private String delivering;
/** @param reader the input to read from */
public ReadingRunnable(BufferedReader reader) {
@@ -96,10 +107,13 @@ public class ReadingRunnable implements Runnable {
lock.notify();
}
} catch (InterruptedIOException e) {
LOGGER.log(Level.INFO, "Reading interrupted", e); //$NON-NLS-1$
LOGGER.info("Reading interrupted"); //$NON-NLS-1$
LOGGER.log(Level.FINER,
"Read interruption was caused by an exception", e); //$NON-NLS-1$
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Unable to read from stream", e); //$NON-NLS-1$
LOGGER.severe("Unable to read from stream"); //$NON-NLS-1$
LOGGER.log(Level.FINE, "The stream reading threw an exception", //$NON-NLS-1$
e);
running = false;
return;
}
@@ -141,6 +155,7 @@ public class ReadingRunnable implements Runnable {
}
LOGGER.finest("Polled: " + messages.peek()); //$NON-NLS-1$
waiting = false;
notifyMessage(messages.peek());
return messages.poll();
}
}
@@ -179,4 +194,63 @@ public class ReadingRunnable implements Runnable {
}
}
}
/** @param message the message */
private void notifyMessage(String message) {
synchronized (messageBlockerLock) {
delivering = message;
if (messageBlocker.containsKey(message)) {
Object mLock = messageBlocker.get(message);
synchronized (mLock) {
mLock.notify();
}
messageBlocker.remove(message);
}
}
}
/** @param message the message
* @return the thread to join to wait for message delivery */
public Thread getWaitForDelivery(final String message) {
synchronized (messageBlockerLock) {
if (!messageBlocker.containsKey(message)) {
messageBlocker.put(message, new Object());
}
final Object obj = messageBlocker.get(message);
final Object start = new Object();
Thread th = new Thread(new Runnable() {
@SuppressWarnings("synthetic-access")
@Override
public void run() {
synchronized (obj) {
synchronized (start) {
start.notify();
}
while (isRunning()) {
try {
obj.wait();
if (delivering.equals(message)) {
return;
}
} catch (InterruptedException e) {
LOGGER.log(Level.SEVERE,
"Thread interruption exception.", e); //$NON-NLS-1$
}
}
}
}
});
synchronized (start) {
th.start();
try {
start.wait();
} catch (InterruptedException e) {
LOGGER.log(Level.SEVERE, "Thread interruption exception.", //$NON-NLS-1$
e);
}
}
return th;
}
}
}

View File

@@ -245,8 +245,8 @@ public class CLIPrompter {
r = Integer.parseInt(result);
still = false;
} catch (final NumberFormatException e) {
LOGGER.log(Level.INFO,
"User input a non parsable integer: " + result, e); //$NON-NLS-1$
LOGGER.info("User input a non parsable integer: " + result); //$NON-NLS-1$
LOGGER.log(Level.FINEST, "Unrecognized integer", e); //$NON-NLS-1$
still = true;
}
}

View File

@@ -74,7 +74,8 @@ public class CLIPrompterMessages {
try {
return MessageFormat.format(RESOURCE_BUNDLE.getString(key), args);
} catch (final MissingResourceException e) {
LOGGER.log(Level.WARNING, "Unrecognized key: " + key, e); //$NON-NLS-1$
LOGGER.warning("Unrecognized key: " + key); //$NON-NLS-1$
LOGGER.log(Level.FINE, "Missing key in " + BUNDLE_NAME, e); //$NON-NLS-1$
return '!' + key + '!';
}
}

View File

@@ -16,7 +16,8 @@ promptbool.choices.no1=N
promptbool.choices.no2=no
promptchoice.outofbounds=Please choose something between {0} and {1}. The choices were:
promptchoice.formaterr=The input seems to be something that is not an integer.\nPlease choose something between {0} and {1}. The choices were:
promptchoice.formaterr=The input seems to be something that is not an integer.\
Please choose something between {0} and {1}. The choices were:
promptlongtext.exit.defaultkey=\\q
promptlongtext.exit.dispkey=\ (exit with a new line made of "{0}")

View File

@@ -40,45 +40,54 @@ 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.util.HashSet;
import java.util.Set;
import org.junit.Test;
/**
* <p>
import fr.bigeon.gclc.exception.CommandParsingException;
/** <p>
* TODO
*
* @author Emmanuel Bigeon
*
*/
* @author Emmanuel Bigeon */
@SuppressWarnings({"static-method", "nls"})
public class CommandParametersTest {
/**
* Test method for {@link fr.bigeon.gclc.command.CommandParameters#CommandParameters(java.util.Set, java.util.Set, boolean)}.
*/
/** Test method for
* {@link fr.bigeon.gclc.command.CommandParameters#CommandParameters(java.util.Set, java.util.Set, boolean)}. */
@Test
public final void testCommandParameters(){
public final void testCommandParameters() {
Set<String> strings = new HashSet<>();
Set<String> bools = new HashSet<>();
CommandParameters parameters = new CommandParameters(bools, strings,
true);
assertFalse(parameters.parseArgs("-ungivenFlag"));
try {
parameters.parseArgs("-ungivenFlag");
fail("parse of unknown in strict should fail");
} catch (CommandParsingException e) {
assertNotNull(e);
}
parameters = new CommandParameters(bools, strings, false);
assertTrue(parameters.parseArgs("-ungivenFlag"));
try {
parameters.parseArgs("-ungivenFlag");
} catch (CommandParsingException e) {
fail("parse of unknown in non strict should suceed");
assertNull(e);
}
}
/**
* Test method for {@link fr.bigeon.gclc.command.CommandParameters#get(java.lang.String)}.
*/
/** Test method for
* {@link fr.bigeon.gclc.command.CommandParameters#get(java.lang.String)}. */
@Test
public final void testGet(){
public final void testGet() {
Set<String> strings = new HashSet<>();
Set<String> bools = new HashSet<>();
@@ -91,24 +100,35 @@ public class CommandParametersTest {
assertNull(parameters.get("ungiven"));
assertNull(parameters.get("str"));
parameters.parseArgs("-ungiven", "val");
try {
parameters.parseArgs("-ungiven", "val");
} catch (CommandParsingException e) {
assertNotNull(e);
}
assertNull(parameters.get("ungiven"));
assertNull(parameters.get("str"));
parameters.parseArgs("-str", "val");
try {
parameters.parseArgs("-str", "val");
} catch (CommandParsingException e) {
assertNull(e);
}
assertNull(parameters.get("ungiven"));
assertEquals("val", parameters.get("str"));
parameters.parseArgs("-ungiven");
try {
parameters.parseArgs("-ungiven");
} catch (CommandParsingException e) {
assertNotNull(e);
}
assertNull(parameters.get("ungiven"));
assertEquals("val", parameters.get("str"));
}
/**
* Test method for {@link fr.bigeon.gclc.command.CommandParameters#getAdditionals()}.
*/
/** Test method for
* {@link fr.bigeon.gclc.command.CommandParameters#getAdditionals()}. */
@Test
public final void testGetAdditionals(){
public final void testGetAdditionals() {
Set<String> strings = new HashSet<>();
Set<String> bools = new HashSet<>();
@@ -118,27 +138,42 @@ public class CommandParametersTest {
CommandParameters parameters = new CommandParameters(bools, strings,
true);
parameters.parseArgs("-boolFlag");
try {
parameters.parseArgs("-boolFlag");
} catch (CommandParsingException e) {
assertNull(e);
}
assertTrue(parameters.getAdditionals().isEmpty());
parameters.parseArgs("-ungiven");
try {
parameters.parseArgs("-ungiven");
} catch (CommandParsingException e) {
assertNotNull(e);
}
assertTrue(parameters.getAdditionals().isEmpty());
parameters = new CommandParameters(bools, strings, false);
parameters.parseArgs("-boolFlag");
try {
parameters.parseArgs("-boolFlag");
} catch (CommandParsingException e) {
assertNull(e);
}
assertTrue(parameters.getAdditionals().isEmpty());
parameters.parseArgs("-ungiven");
try {
parameters.parseArgs("-ungiven");
} catch (CommandParsingException e) {
assertNotNull(e);
}
assertTrue(parameters.getAdditionals().contains("ungiven"));
assertEquals(1, parameters.getAdditionals().size());
}
/**
* Test method for {@link fr.bigeon.gclc.command.CommandParameters#getBool(java.lang.String)}.
*/
/** Test method for
* {@link fr.bigeon.gclc.command.CommandParameters#getBool(java.lang.String)}. */
@Test
public final void testGetBool(){
public final void testGetBool() {
Set<String> strings = new HashSet<>();
Set<String> bools = new HashSet<>();
@@ -151,11 +186,20 @@ public class CommandParametersTest {
assertFalse(parameters.getBool("ungiven"));
assertFalse(parameters.getBool("boolFlag"));
parameters.parseArgs("-boolFlag");
try {
parameters.parseArgs("-boolFlag");
} catch (CommandParsingException e) {
assertNull(e);
}
assertTrue(parameters.getBool("boolFlag"));
assertFalse(parameters.getBool("ungiven"));
parameters.parseArgs("-ungiven");
try {
parameters.parseArgs("-ungiven");
fail("unknown parameter should fail");
} catch (CommandParsingException e) {
assertNotNull(e);
}
assertFalse(parameters.getBool("ungiven"));
assertTrue(parameters.getBool("boolFlag"));
@@ -164,20 +208,27 @@ public class CommandParametersTest {
assertFalse(parameters.getBool("ungiven"));
assertFalse(parameters.getBool("boolFlag"));
parameters.parseArgs("-boolFlag");
try {
parameters.parseArgs("-boolFlag");
} catch (CommandParsingException e) {
assertNull(e);
}
assertTrue(parameters.getBool("boolFlag"));
assertFalse(parameters.getBool("ungiven"));
parameters.parseArgs("-ungiven");
try {
parameters.parseArgs("-ungiven");
} catch (CommandParsingException e) {
assertNull(e);
}
assertFalse(parameters.getBool("ungiven"));
assertTrue(parameters.getBool("boolFlag"));
}
/**
* Test method for {@link fr.bigeon.gclc.command.CommandParameters#parseArgs(java.lang.String[])}.
*/
/** Test method for
* {@link fr.bigeon.gclc.command.CommandParameters#parseArgs(java.lang.String[])}. */
@Test
public final void testParseArgs(){
public final void testParseArgs() {
Set<String> strings = new HashSet<>();
Set<String> bools = new HashSet<>();
@@ -187,19 +238,40 @@ public class CommandParametersTest {
CommandParameters parameters = new CommandParameters(bools, strings,
true);
assertFalse(parameters.parseArgs("-ungivenFlag"));
assertFalse(parameters.parseArgs("-str"));
assertTrue(parameters.parseArgs("-boolFlag"));
assertTrue(parameters.parseArgs("-str", "-boolFlag"));
assertTrue(parameters.parseArgs("-boolFlag", "-str", "val"));
try {
parameters.parseArgs("-ungivenFlag");
fail("unknown argument should fail in strict");
} catch (CommandParsingException e) {
assertNotNull(e);
}
try {
parameters.parseArgs("-str");
fail("missing string argument value should fail");
} catch (CommandParsingException e) {
assertNotNull(e);
}
try {
parameters.parseArgs("-boolFlag");
} catch (CommandParsingException e) {
assertNull(e);
}
try {
parameters.parseArgs("-str", "-boolFlag");
} catch (CommandParsingException e) {
assertNull(e);
}
try {
parameters.parseArgs("-boolFlag", "-str", "val");
} catch (CommandParsingException e) {
assertNull(e);
}
}
/**
* Test method for {@link fr.bigeon.gclc.command.CommandParameters#set(java.lang.String, boolean)}.
*/
/** Test method for
* {@link fr.bigeon.gclc.command.CommandParameters#set(java.lang.String, boolean)}. */
@Test
public final void testSetStringBoolean(){
public final void testSetStringBoolean() {
Set<String> strings = new HashSet<>();
Set<String> bools = new HashSet<>();
@@ -234,11 +306,10 @@ public class CommandParametersTest {
assertTrue(parameters.getBool("boolFlag"));
}
/**
* Test method for {@link fr.bigeon.gclc.command.CommandParameters#set(java.lang.String, java.lang.String)}.
*/
/** Test method for
* {@link fr.bigeon.gclc.command.CommandParameters#set(java.lang.String, java.lang.String)}. */
@Test
public final void testSetStringString(){
public final void testSetStringString() {
Set<String> strings = new HashSet<>();
Set<String> bools = new HashSet<>();

View File

@@ -392,21 +392,20 @@ public class ParametrizedCommandTest {
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:
case 1:
assertEquals(str2, parameters.get(str1));
assertNull(parameters.get(str2));
assertFalse(parameters.getBool(bool1));
assertFalse(parameters.getBool(bool2));
call++;
break;
case 3:
case 2:
assertEquals(str2, parameters.get(str1));
assertNull(parameters.get(str2));
assertTrue(parameters.getBool(bool1));
@@ -420,12 +419,17 @@ public class ParametrizedCommandTest {
};
try {
cmd.execute();
cmd.execute(addParam);
cmd.execute("-" + str1, str2);
cmd.execute("-" + str1, str2, "-" + bool1);
} catch (CommandRunException e) {
assertNull(e);
fail("unepected error");
fail("unexpected error");
}
try {
cmd.execute(addParam);
fail("Strict should fail with unexpected argument");
} catch (CommandRunException e) {
assertNotNull(e);
}
try {
cmd.execute("-" + addParam);
@@ -504,11 +508,16 @@ public class ParametrizedCommandTest {
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("-" + str1, str2, addParam);
fail("Additional parameter should cause failure");
} catch (CommandRunException e) {
assertNotNull(e);
}
try {
cmd.execute();
fail("needed " + str1 + " not provided shall fail");