Reorganize code to avoid cyclic dependencies.

Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
2018-10-08 11:49:14 -04:00
parent c8745e00a9
commit d99d97b9d5
75 changed files with 12705 additions and 10120 deletions

View File

@@ -1,123 +1,156 @@
/*
* 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.CommandTestingApplication.java
* Created on: Jun 12, 2016
*/
package fr.bigeon.gclc;
import java.io.IOException;
import fr.bigeon.gclc.command.ICommand;
import fr.bigeon.gclc.exception.InvalidCommandName;
import fr.bigeon.gclc.manager.PipedConsoleInput;
import fr.bigeon.gclc.manager.PipedConsoleOutput;
/**
* <p>
* TODO
*
* @author Emmanuel Bigeon
*
*/
@SuppressWarnings("javadoc")
public class CommandTestingApplication implements AutoCloseable {
private final ConsoleApplication application;
private final Thread th;
private final PipedConsoleOutput out;
private final PipedConsoleInput in;
/** @throws IOException if the streams cannot be build */
public CommandTestingApplication() throws IOException {
out = new PipedConsoleOutput();
in = new PipedConsoleInput(null);
application = new ConsoleApplication(out, in, "", "");
new ConsoleTestApplication().attach(application);
th = new Thread(new Runnable() {
@SuppressWarnings("synthetic-access")
@Override
public void run() {
application.start();
}
});
th.start();
}
/** @param cmd the command
* @return if the command was added
* @throws InvalidCommandName if the command name is invalid
* @see fr.bigeon.gclc.ConsoleApplication#add(fr.bigeon.gclc.command.ICommand) */
public final boolean add(final ICommand cmd) throws InvalidCommandName {
return application.add(cmd);
}
@Override
public void close() throws IOException {
application.exit();
out.close();
in.close();
}
/** @return the application */
public ConsoleApplication getApplication() {
return application;
}
/** @return the next written line, null if it is the prompt
* @throws IOException if the reading failed */
public String readNextLine() throws IOException {
final String ret = out.readNextLine();
return ret;
}
public void sendCommand(final String cmd) throws IOException {
in.type(cmd);
}
/** @throws IOException if the writing failed */
public void waitAndStop() throws IOException {
in.type(ConsoleTestApplication.EXIT);
try {
th.join();
} catch (final InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*
* 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.CommandTestingApplication.java
* Created on: Jun 12, 2016
*/
package fr.bigeon.gclc;
/*-
* #%L
* Generic Command Ligne console
* %%
* Copyright (C) 2014 - 2018 bigeon.fr
* %%
* 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.
* #L%
*/
import java.io.IOException;
import fr.bigeon.gclc.command.ICommand;
import fr.bigeon.gclc.exception.InvalidCommandName;
import fr.bigeon.gclc.utils.PipedConsoleInput;
import fr.bigeon.gclc.utils.PipedConsoleOutput;
/**
* <p>
* TODO
*
* @author Emmanuel Bigeon
*
*/
@SuppressWarnings("javadoc")
public class CommandTestingApplication implements AutoCloseable {
private final ConsoleApplication application;
private final Thread th;
private final PipedConsoleOutput out;
private final PipedConsoleInput in;
/** @throws IOException if the streams cannot be build */
public CommandTestingApplication() throws IOException {
out = new PipedConsoleOutput();
in = new PipedConsoleInput(null);
application = new ConsoleApplication(out, in, "", "");
new ConsoleTestApplication().attach(application);
th = new Thread(new Runnable() {
@SuppressWarnings("synthetic-access")
@Override
public void run() {
application.start();
}
});
th.start();
}
/** @param cmd the command
* @return if the command was added
* @throws InvalidCommandName if the command name is invalid
* @see fr.bigeon.gclc.ConsoleApplication#add(fr.bigeon.gclc.command.ICommand) */
public final boolean add(final ICommand cmd) throws InvalidCommandName {
return application.add(cmd);
}
@Override
public void close() throws IOException {
application.exit();
out.close();
in.close();
}
/** @return the application */
public ConsoleApplication getApplication() {
return application;
}
/** @return the next written line, null if it is the prompt
* @throws IOException if the reading failed */
public String readNextLine() throws IOException {
final String ret = out.readNextLine();
return ret;
}
public void sendCommand(final String cmd) throws IOException {
in.type(cmd);
}
/** @throws IOException if the writing failed */
public void waitAndStop() throws IOException {
in.type(ConsoleTestApplication.EXIT);
try {
th.join();
} catch (final InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

View File

@@ -1,247 +1,279 @@
/*
* 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.ConsoleApplicationTest.java
* Created on: Jun 9, 2016
*/
package fr.bigeon.gclc;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
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.ConsoleInput;
import fr.bigeon.gclc.manager.ConsoleOutput;
import fr.bigeon.gclc.manager.PipedConsoleInput;
import fr.bigeon.gclc.manager.PipedConsoleOutput;
/** Test class for ConsoleApplication
*
* @author Emmanuel Bigeon */
@SuppressWarnings({"javadoc", "nls", "static-method"})
public class ConsoleApplicationTest {
/** 3 seconds in milliseconds */
protected static final long THREE_SECONDS = 3000;
/** Test the base of a console application */
@Test
public void testConsoleApplication() {
try (PipedOutputStream pout = new PipedOutputStream();
PipedInputStream pis = new PipedInputStream(pout);
BufferedReader buf = new BufferedReader(
new InputStreamReader(pis, StandardCharsets.UTF_8));
PipedConsoleInput in = new PipedConsoleInput(
new PrintStream(pout))) {
final ConsoleApplication app = new ConsoleApplication(null, in,
"", "");
app.exit();
} catch (final IOException e) {
fail("System Console Manager failed");
}
}
@Test
public void testExecution() throws IOException, InterruptedException,
InvalidCommandName {
try (CommandTestingApplication application = new CommandTestingApplication()) {
// remove welcome
assertEquals("Header should be preserved",
application.getApplication().header,
application.readNextLine());
// Remove first prompt
application.sendCommand("");
application.sendCommand("test");
assertEquals("Test should run", "Test command ran fine",
application.readNextLine());
application.sendCommand("toto");
assertEquals("Command fail should dispaly appropriate message",
Messages.getString("ConsoleApplication.cmd.failed", "toto"),
application.readNextLine());
assertEquals(
"Unrecognized comment should result in a specific message.",
Messages.getString("CommandProvider.unrecognized", "toto"),
application.readNextLine());
application.sendCommand("long");
assertEquals("Before wait should receive message", "Waita minute",
application.readNextLine());
assertEquals("Unexpected message", "done!",
application.readNextLine());
final CommandRequestListener crl = new CommandRequestListener() {
@Override
public void commandRequest(final String command) {
//
}
};
final CommandRequestListener crl2 = new CommandRequestListener() {
@Override
public void commandRequest(final String command) {
//
}
};
application.getApplication().addListener(crl);
application.getApplication().addListener(crl2);
application.sendCommand("test");
assertEquals("Unexpected message", "Test command ran fine",
application.readNextLine());
application.getApplication().removeListener(crl2);
application.getApplication().removeListener(crl);
application.getApplication().removeListener(crl);
assertTrue("Unclosed application should be running",
application.getApplication().isRunning());
application.sendCommand("exit");
assertEquals("Footer should be preserved",
application.getApplication().footer,
application.readNextLine());
assertFalse("Stopped application should not be running",
application.getApplication().isRunning());
}
ConsoleApplication appli = null;
try (PipedConsoleOutput manager = new PipedConsoleOutput();
PipedOutputStream pout = new PipedOutputStream();
PipedInputStream pis = new PipedInputStream(pout);
BufferedReader buf = new BufferedReader(
new InputStreamReader(pis, StandardCharsets.UTF_8));
PipedConsoleInput in = new PipedConsoleInput(
new PrintStream(pout))) {
final ConsoleApplication app = new ConsoleApplication(manager, in,
null, null);
appli = app;
app.add(new ExitCommand("exit", app));
final Thread th = new Thread(new Runnable() {
@SuppressWarnings("synthetic-access")
@Override
public void run() {
app.start();
}
});
th.start();
in.type("exit");
th.join();
}
assertNotNull(
"Application should still exist even if the console input and output are closed.",
appli);
appli.start();
assertFalse(
"Application should not start on closed console input and output",
appli.isRunning());
}
@Test
public void testInterpretCommand() throws InvalidCommandName, IOException {
try (PipedConsoleInput test = new PipedConsoleInput(null);
PipedConsoleOutput out = new PipedConsoleOutput()) {
final ConsoleApplication appl = new ConsoleApplication(out, test,
"", "");
appl.interpretCommand("invalid cmd \"due to misplaced\"quote");
assertEquals("Specific error message expected",
"Command line cannot be parsed", out.readNextLine());
appl.interpretCommand("");
final String message = "message";
appl.add(new ICommand() {
/* (non-Javadoc)
* @see fr.bigeon.gclc.command.ICommand#execute(fr.bigeon.gclc.
* manager.ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput,
* java.lang.String[]) */
@Override
public void execute(final ConsoleOutput out,
final ConsoleInput in,
final String... args) throws CommandRunException {
throw new CommandRunException(CommandRunExceptionType.USAGE,
message, this);
}
@Override
public String getCommandName() {
return "fail";
}
@Override
public void help(final ConsoleOutput manager,
final String... args) throws IOException {
manager.println(message);
}
@Override
public String tip() {
return "";
}
});
appl.interpretCommand("fail");
assertEquals("Unexpected message",
Messages.getString("ConsoleApplication.cmd.failed", "fail"),
out.readNextLine());
assertEquals("Unexpected message", message, out.readNextLine());
assertEquals("Unexpected message", message, out.readNextLine());
}
}
}
/*
* 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.ConsoleApplicationTest.java
* Created on: Jun 9, 2016
*/
package fr.bigeon.gclc;
/*-
* #%L
* Generic Command Ligne console
* %%
* Copyright (C) 2014 - 2018 bigeon.fr
* %%
* 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.
* #L%
*/
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import org.junit.Test;
import fr.bigeon.gclc.command.ICommand;
import fr.bigeon.gclc.command.base.ExitCommand;
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.ConsoleInput;
import fr.bigeon.gclc.manager.ConsoleOutput;
import fr.bigeon.gclc.utils.PipedConsoleInput;
import fr.bigeon.gclc.utils.PipedConsoleOutput;
/** Test class for ConsoleApplication
*
* @author Emmanuel Bigeon */
@SuppressWarnings({"javadoc", "nls", "static-method"})
public class ConsoleApplicationTest {
/** 3 seconds in milliseconds */
protected static final long THREE_SECONDS = 3000;
/** Test the base of a console application */
@Test
public void testConsoleApplication() {
try (PipedOutputStream pout = new PipedOutputStream();
PipedInputStream pis = new PipedInputStream(pout);
BufferedReader buf = new BufferedReader(
new InputStreamReader(pis, StandardCharsets.UTF_8));
PipedConsoleInput in = new PipedConsoleInput(
new PrintStream(pout))) {
final ConsoleApplication app = new ConsoleApplication(null, in,
"", "");
app.exit();
} catch (final IOException e) {
fail("System Console Manager failed");
}
}
@Test
public void testExecution() throws IOException, InterruptedException,
InvalidCommandName {
try (CommandTestingApplication application = new CommandTestingApplication()) {
// remove welcome
assertEquals("Header should be preserved",
application.getApplication().header,
application.readNextLine());
// Remove first prompt
application.sendCommand("");
application.sendCommand("test");
assertEquals("Test should run", "Test command ran fine",
application.readNextLine());
application.sendCommand("toto");
assertEquals("Command fail should dispaly appropriate message",
Messages.getString("ConsoleApplication.cmd.failed", "toto"),
application.readNextLine());
assertEquals(
"Unrecognized comment should result in a specific message.",
Messages.getString("CommandProvider.unrecognized", "toto"),
application.readNextLine());
application.sendCommand("long");
assertEquals("Before wait should receive message", "Waita minute",
application.readNextLine());
assertEquals("Unexpected message", "done!",
application.readNextLine());
final CommandRequestListener crl = new CommandRequestListener() {
@Override
public void commandRequest(final String command) {
//
}
};
final CommandRequestListener crl2 = new CommandRequestListener() {
@Override
public void commandRequest(final String command) {
//
}
};
application.getApplication().addListener(crl);
application.getApplication().addListener(crl2);
application.sendCommand("test");
assertEquals("Unexpected message", "Test command ran fine",
application.readNextLine());
application.getApplication().removeListener(crl2);
application.getApplication().removeListener(crl);
application.getApplication().removeListener(crl);
assertTrue("Unclosed application should be running",
application.getApplication().isRunning());
application.sendCommand("exit");
assertEquals("Footer should be preserved",
application.getApplication().footer,
application.readNextLine());
assertFalse("Stopped application should not be running",
application.getApplication().isRunning());
}
ConsoleApplication appli = null;
try (PipedConsoleOutput manager = new PipedConsoleOutput();
PipedOutputStream pout = new PipedOutputStream();
PipedInputStream pis = new PipedInputStream(pout);
BufferedReader buf = new BufferedReader(
new InputStreamReader(pis, StandardCharsets.UTF_8));
PipedConsoleInput in = new PipedConsoleInput(
new PrintStream(pout))) {
final ConsoleApplication app = new ConsoleApplication(manager, in,
null, null);
appli = app;
app.add(new ExitCommand("exit", app));
final Thread th = new Thread(new Runnable() {
@Override
public void run() {
app.start();
}
});
th.start();
in.type("exit");
th.join();
}
assertNotNull(
"Application should still exist even if the console input and output are closed.",
appli);
appli.start();
assertFalse(
"Application should not start on closed console input and output",
appli.isRunning());
}
@Test
public void testInterpretCommand() throws InvalidCommandName, IOException {
try (PipedConsoleInput test = new PipedConsoleInput(null);
PipedConsoleOutput out = new PipedConsoleOutput()) {
final ConsoleApplication appl = new ConsoleApplication(out, test,
"", "");
appl.interpretCommand("invalid cmd \"due to misplaced\"quote");
assertEquals("Specific error message expected",
"Command line cannot be parsed", out.readNextLine());
appl.interpretCommand("");
final String message = "message";
appl.add(new ICommand() {
/* (non-Javadoc)
* @see fr.bigeon.gclc.command.ICommand#execute(fr.bigeon.gclc.
* manager.ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput,
* java.lang.String[]) */
@Override
public void execute(final ConsoleOutput out,
final ConsoleInput in,
final String... args) throws CommandRunException {
throw new CommandRunException(CommandRunExceptionType.USAGE,
message);
}
@Override
public String getCommandName() {
return "fail";
}
@Override
public void help(final ConsoleOutput manager,
final String... args) throws IOException {
manager.println(message);
}
@Override
public String tip() {
return "";
}
});
appl.interpretCommand("fail");
assertEquals("Unexpected message",
Messages.getString("ConsoleApplication.cmd.failed", "fail"),
out.readNextLine());
assertEquals("Unexpected message", message, out.readNextLine());
assertEquals("Unexpected message", message, out.readNextLine());
}
}
}

View File

@@ -1,152 +1,179 @@
/*
* 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.
*/
package fr.bigeon.gclc;
import java.io.IOException;
import fr.bigeon.gclc.command.Command;
import fr.bigeon.gclc.command.ExitCommand;
import fr.bigeon.gclc.command.HelpExecutor;
import fr.bigeon.gclc.command.ICommandProvider;
import fr.bigeon.gclc.exception.CommandRunException;
import fr.bigeon.gclc.exception.InvalidCommandName;
import fr.bigeon.gclc.manager.ConsoleInput;
import fr.bigeon.gclc.manager.ConsoleOutput;
/** A test-purpose application
*
* @author Emmanuel Bigeon */
public class ConsoleTestApplication implements ApplicationAttachement {
/** Exit command */
public static final String EXIT = "exit"; //$NON-NLS-1$
/** Two seconds in milliseconds */
protected static final long TWO_SECONDS = 2000;
/***/
@Override
@SuppressWarnings("nls")
public void attach(final ICommandProvider application) {
try {
application.add(new ExitCommand(EXIT, (ConsoleApplication) application));
application.add(new HelpExecutor("help",
((ConsoleApplication) application).root));
application.add(new Command("test") {
/* (non-Javadoc)
* @see fr.bigeon.gclc.command.ICommand#execute(fr.bigeon.gclc.
* manager.ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput,
* java.lang.String[]) */
@Override
public void execute(final ConsoleOutput out,
final ConsoleInput in,
final String... args) throws CommandRunException {
try {
out.println("Test command ran fine");
} catch (final IOException e) {
throw new CommandRunException("manager closed", e,
this);
}
}
@Override
public String tip() {
return "A test command";
}
@Override
protected String usageDetail() {
return null;
}
});
application.add(new Command("long") {
/* (non-Javadoc)
* @see fr.bigeon.gclc.command.ICommand#execute(fr.bigeon.gclc.
* manager.ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput,
* java.lang.String[]) */
@Override
public void execute(final ConsoleOutput out,
final ConsoleInput in,
final String... args) throws CommandRunException {
try {
out.println("Waita minute");
Thread.sleep(TWO_SECONDS);
out.println("done!");
} catch (final IOException e) {
throw new CommandRunException("manager closed", e,
this);
} catch (final InterruptedException e) {
throw new CommandRunException("wait interrupted", e,
this);
}
}
@Override
public String tip() {
return "A long execution command";
}
@Override
protected String usageDetail() {
return null;
}
});
application.add(new Command("failingCmd") {
/* (non-Javadoc)
* @see fr.bigeon.gclc.command.ICommand#execute(fr.bigeon.gclc.
* manager.ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput,
* java.lang.String[]) */
@Override
public void execute(final ConsoleOutput out,
final ConsoleInput in,
final String... args) throws CommandRunException {
throw new CommandRunException("Failing command", this);
}
@Override
public String tip() {
return "A long execution command";
}
@Override
protected String usageDetail() {
return null;
}
});
} catch (final InvalidCommandName e) {
e.printStackTrace();
}
}
}
/*
* 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.
*/
package fr.bigeon.gclc;
/*-
* #%L
* Generic Command Ligne console
* %%
* Copyright (C) 2014 - 2018 bigeon.fr
* %%
* 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.
* #L%
*/
import java.io.IOException;
import fr.bigeon.gclc.command.Command;
import fr.bigeon.gclc.command.HelpExecutor;
import fr.bigeon.gclc.command.ICommandProvider;
import fr.bigeon.gclc.command.base.ExitCommand;
import fr.bigeon.gclc.exception.CommandRunException;
import fr.bigeon.gclc.exception.InvalidCommandName;
import fr.bigeon.gclc.manager.ConsoleInput;
import fr.bigeon.gclc.manager.ConsoleOutput;
/** A test-purpose application
*
* @author Emmanuel Bigeon */
public class ConsoleTestApplication implements ApplicationAttachement {
/** Exit command */
public static final String EXIT = "exit"; //$NON-NLS-1$
/** Two seconds in milliseconds */
protected static final long TWO_SECONDS = 2000;
/***/
@Override
@SuppressWarnings("nls")
public void attach(final ICommandProvider application) {
try {
application.add(new ExitCommand(EXIT, (ConsoleApplication) application));
application.add(
new HelpExecutor("help", ((ConsoleApplication) application).root));
application.add(new Command("test") {
/* (non-Javadoc)
* @see fr.bigeon.gclc.command.ICommand#execute(fr.bigeon.gclc.
* manager.ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput,
* java.lang.String[]) */
@Override
public void execute(final ConsoleOutput out, final ConsoleInput in,
final String... args) throws CommandRunException {
try {
out.println("Test command ran fine");
} catch (final IOException e) {
throw new CommandRunException("manager closed", e);
}
}
@Override
public String tip() {
return "A test command";
}
@Override
protected String usageDetail() {
return null;
}
});
application.add(new Command("long") {
/* (non-Javadoc)
* @see fr.bigeon.gclc.command.ICommand#execute(fr.bigeon.gclc.
* manager.ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput,
* java.lang.String[]) */
@Override
public void execute(final ConsoleOutput out, final ConsoleInput in,
final String... args) throws CommandRunException {
try {
out.println("Waita minute");
Thread.sleep(TWO_SECONDS);
out.println("done!");
} catch (final IOException e) {
throw new CommandRunException("manager closed", e);
} catch (final InterruptedException e) {
throw new CommandRunException("wait interrupted", e);
}
}
@Override
public String tip() {
return "A long execution command";
}
@Override
protected String usageDetail() {
return null;
}
});
application.add(new Command("failingCmd") {
/* (non-Javadoc)
* @see fr.bigeon.gclc.command.ICommand#execute(fr.bigeon.gclc.
* manager.ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput,
* java.lang.String[]) */
@Override
public void execute(final ConsoleOutput out, final ConsoleInput in,
final String... args) throws CommandRunException {
throw new CommandRunException("Failing command");
}
@Override
public String tip() {
return "A long execution command";
}
@Override
protected String usageDetail() {
return null;
}
});
} catch (final InvalidCommandName e) {
e.printStackTrace();
}
}
}

View File

@@ -1,128 +1,161 @@
/*
* 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.GCLCConstantsTest.java
* Created on: Jun 8, 2016
*/
package fr.bigeon.gclc;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.List;
import org.junit.Test;
import fr.bigeon.gclc.exception.CommandParsingException;
/** Test class for {@link GCLCConstants}
*
* @author Emmanuel Bigeon */
@SuppressWarnings({"nls", "static-method"})
public class GCLCConstantsTest {
/** Test method for
* {@link fr.bigeon.gclc.GCLCConstants#splitCommand(java.lang.String)}.
*
* @throws CommandParsingException if an error occured */
@Test
public void testSplitCommand() throws CommandParsingException {
List<String> res;
res = GCLCConstants.splitCommand("aCommand");
assertTrue("single word command should have one element",
res.size() == 1);
assertTrue("Command should be preserved",
res.get(0).equals("aCommand"));
res = GCLCConstants.splitCommand("aCommand with some arguments");
assertEquals("Command size", 4, res.size());
assertEquals("Elements should be preserved", "aCommand", res.get(0));
assertTrue("Elements should be preserved", res.get(1).equals("with"));
assertTrue("Elements should be preserved", res.get(2).equals("some"));
assertTrue("Elements should be preserved",
res.get(3).equals("arguments"));
res = GCLCConstants.splitCommand("aCommand with some arguments");
assertEquals("Command size", 4, res.size());
assertTrue("Elements should be preserved",
res.get(0).equals("aCommand"));
assertTrue("Elements should be preserved", res.get(1).equals("with"));
assertTrue("Elements should be preserved", res.get(2).equals("some"));
assertTrue("Elements should be preserved",
res.get(3).equals("arguments"));
res = GCLCConstants.splitCommand("aCommand \"with some\" arguments");
assertEquals("Command size", 3, res.size());
assertTrue("Elements should be preserved",
res.get(0).equals("aCommand"));
assertTrue("Elements should be preserved",
res.get(1).equals("with some"));
assertTrue("Elements should be preserved",
res.get(2).equals("arguments"));
res = GCLCConstants.splitCommand("aCommand with\\ some arguments");
assertEquals("Command size", 3, res.size());
assertTrue("Elements should be preserved",
res.get(0).equals("aCommand"));
assertTrue("Elements should be preserved",
res.get(1).equals("with some"));
assertTrue("Elements should be preserved",
res.get(2).equals("arguments"));
res = GCLCConstants.splitCommand("aCommand wi\\\"th some arguments");
assertEquals("Command size", 4, res.size());
assertTrue("Elements should be preserved",
res.get(0).equals("aCommand"));
assertTrue("Elements should be preserved", res.get(1).equals("wi\"th"));
assertTrue("Elements should be preserved", res.get(2).equals("some"));
assertTrue("Elements should be preserved",
res.get(3).equals("arguments"));
res = GCLCConstants.splitCommand("aCommand with \"some arguments\"");
assertEquals("Command size", 3, res.size());
assertTrue("Elements should be preserved",
res.get(0).equals("aCommand"));
assertTrue("Elements should be preserved", res.get(1).equals("with"));
assertTrue("Elements should be preserved",
res.get(2).equals("some arguments"));
try {
// Wrong lines?
res = GCLCConstants
.splitCommand("aCommand with \"some ar\"guments");
fail("Misplaced quotes should fail");
} catch (final CommandParsingException e) {
// ok
}
}
}
/*
* 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.GCLCConstantsTest.java
* Created on: Jun 8, 2016
*/
package fr.bigeon.gclc;
/*-
* #%L
* Generic Command Ligne console
* %%
* Copyright (C) 2014 - 2018 bigeon.fr
* %%
* 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.
* #L%
*/
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.List;
import org.junit.Test;
import fr.bigeon.gclc.exception.CommandParsingException;
/** Test class for {@link GCLCConstants}
*
* @author Emmanuel Bigeon */
@SuppressWarnings({"nls", "static-method"})
public class GCLCConstantsTest {
/** Test method for
* {@link fr.bigeon.gclc.GCLCConstants#splitCommand(java.lang.String)}.
*
* @throws CommandParsingException if an error occured */
@Test
public void testSplitCommand() throws CommandParsingException {
List<String> res;
res = GCLCConstants.splitCommand("aCommand");
assertTrue("single word command should have one element",
res.size() == 1);
assertTrue("Command should be preserved",
res.get(0).equals("aCommand"));
res = GCLCConstants.splitCommand("aCommand with some arguments");
assertEquals("Command size", 4, res.size());
assertEquals("Elements should be preserved", "aCommand", res.get(0));
assertTrue("Elements should be preserved", res.get(1).equals("with"));
assertTrue("Elements should be preserved", res.get(2).equals("some"));
assertTrue("Elements should be preserved",
res.get(3).equals("arguments"));
res = GCLCConstants.splitCommand("aCommand with some arguments");
assertEquals("Command size", 4, res.size());
assertTrue("Elements should be preserved",
res.get(0).equals("aCommand"));
assertTrue("Elements should be preserved", res.get(1).equals("with"));
assertTrue("Elements should be preserved", res.get(2).equals("some"));
assertTrue("Elements should be preserved",
res.get(3).equals("arguments"));
res = GCLCConstants.splitCommand("aCommand \"with some\" arguments");
assertEquals("Command size", 3, res.size());
assertTrue("Elements should be preserved",
res.get(0).equals("aCommand"));
assertTrue("Elements should be preserved",
res.get(1).equals("with some"));
assertTrue("Elements should be preserved",
res.get(2).equals("arguments"));
res = GCLCConstants.splitCommand("aCommand with\\ some arguments");
assertEquals("Command size", 3, res.size());
assertTrue("Elements should be preserved",
res.get(0).equals("aCommand"));
assertTrue("Elements should be preserved",
res.get(1).equals("with some"));
assertTrue("Elements should be preserved",
res.get(2).equals("arguments"));
res = GCLCConstants.splitCommand("aCommand wi\\\"th some arguments");
assertEquals("Command size", 4, res.size());
assertTrue("Elements should be preserved",
res.get(0).equals("aCommand"));
assertTrue("Elements should be preserved", res.get(1).equals("wi\"th"));
assertTrue("Elements should be preserved", res.get(2).equals("some"));
assertTrue("Elements should be preserved",
res.get(3).equals("arguments"));
res = GCLCConstants.splitCommand("aCommand with \"some arguments\"");
assertEquals("Command size", 3, res.size());
assertTrue("Elements should be preserved",
res.get(0).equals("aCommand"));
assertTrue("Elements should be preserved", res.get(1).equals("with"));
assertTrue("Elements should be preserved",
res.get(2).equals("some arguments"));
try {
// Wrong lines?
res = GCLCConstants
.splitCommand("aCommand with \"some ar\"guments");
fail("Misplaced quotes should fail");
} catch (final CommandParsingException e) {
// ok
}
}
}

View File

@@ -1,328 +1,361 @@
/*
* 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 static org.junit.Assert.fail;
import java.util.HashSet;
import java.util.Set;
import org.junit.Test;
import fr.bigeon.gclc.exception.CommandParsingException;
/**
* <p>
* TODO
*
* @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)}.
*
* @throws CommandParsingException if an unexpected exception is thrown */
@Test
public final void testCommandParameters() throws CommandParsingException {
final Set<String> strings = new HashSet<>();
final Set<String> bools = new HashSet<>();
CommandParameters parameters = new CommandParameters(bools, strings,
true);
try {
parameters.parseArgs("-ungivenFlag");
fail("parse of unknown in strict should fail");
} catch (final CommandParsingException e) {
// ok
}
parameters = new CommandParameters(bools, strings, false);
parameters.parseArgs("-ungivenFlag");
}
/** Test method for
* {@link fr.bigeon.gclc.command.CommandParameters#get(java.lang.String)}.
*
* @throws CommandParsingException if an exception occired */
@Test
public final void testGet() throws CommandParsingException {
final Set<String> strings = new HashSet<>();
final Set<String> bools = new HashSet<>();
bools.add("boolFlag");
strings.add("str");
final CommandParameters parameters = new CommandParameters(bools,
strings, true);
assertNull(parameters.get("ungiven"));
assertNull(parameters.get("str"));
try {
parameters.parseArgs("-ungiven", "val");
fail("Missing parameter should fail for strict element");
} catch (final CommandParsingException e) {
// ok
}
assertNull(parameters.get("ungiven"));
assertNull(parameters.get("str"));
parameters.parseArgs("-str", "val");
assertNull(parameters.get("ungiven"));
assertEquals("val", parameters.get("str"));
try {
parameters.parseArgs("-ungiven");
fail("Invalid argument type parsing should fail");
} catch (final CommandParsingException e) {
// ok
}
assertNull(parameters.get("ungiven"));
assertEquals("val", parameters.get("str"));
}
/** Test method for
* {@link fr.bigeon.gclc.command.CommandParameters#getAdditionals()}. */
@Test
public final void testGetAdditionals() throws CommandParsingException {
final Set<String> strings = new HashSet<>();
final Set<String> bools = new HashSet<>();
bools.add("boolFlag");
strings.add("str");
CommandParameters parameters = new CommandParameters(bools, strings,
true);
parameters.parseArgs("-boolFlag");
assertTrue(parameters.getAdditionals().isEmpty());
try {
parameters.parseArgs("-ungiven");
fail("Should fail");
} catch (final CommandParsingException e) {
// ok
}
assertTrue(parameters.getAdditionals().isEmpty());
parameters = new CommandParameters(bools, strings, false);
parameters.parseArgs("-boolFlag");
assertTrue(parameters.getAdditionals().isEmpty());
parameters.parseArgs("ungiven");
assertTrue(parameters.getAdditionals().contains("ungiven"));
assertEquals(1, parameters.getAdditionals().size());
}
/** Test method for
* {@link fr.bigeon.gclc.command.CommandParameters#getBool(java.lang.String)}.
*
* @throws CommandParsingException if a command parsing failed */
@Test
public final void testGetBool() throws CommandParsingException {
final Set<String> strings = new HashSet<>();
final Set<String> bools = new HashSet<>();
bools.add("boolFlag");
strings.add("str");
CommandParameters parameters = new CommandParameters(bools, strings,
true);
assertFalse(parameters.getBool("ungiven"));
assertFalse(parameters.getBool("boolFlag"));
parameters.parseArgs("-boolFlag");
assertTrue(parameters.getBool("boolFlag"));
assertFalse(parameters.getBool("ungiven"));
try {
parameters.parseArgs("-ungiven");
fail("unknown parameter should fail");
} catch (final CommandParsingException e) {
// ok
}
assertFalse(parameters.getBool("ungiven"));
assertTrue(parameters.getBool("boolFlag"));
parameters = new CommandParameters(bools, strings, false);
assertFalse(parameters.getBool("ungiven"));
assertFalse(parameters.getBool("boolFlag"));
try {
parameters.parseArgs("-boolFlag");
} catch (final CommandParsingException e) {
// ok
}
assertTrue(parameters.getBool("boolFlag"));
assertFalse(parameters.getBool("ungiven"));
try {
parameters.parseArgs("-ungiven");
} catch (final CommandParsingException e) {
// ok
}
assertFalse(parameters.getBool("ungiven"));
assertTrue(parameters.getBool("boolFlag"));
}
/** Test method for
* {@link fr.bigeon.gclc.command.CommandParameters#parseArgs(java.lang.String[])}.
*
* @throws CommandParsingException if a command parsing failed */
@Test
public final void testParseArgs() throws CommandParsingException {
final Set<String> strings = new HashSet<>();
final Set<String> bools = new HashSet<>();
bools.add("boolFlag");
strings.add("str");
final CommandParameters parameters = new CommandParameters(bools,
strings, true);
try {
parameters.parseArgs("-ungivenFlag");
fail("Strict should fail with flag");
} catch (final CommandParsingException e) {
// ok
}
try {
parameters.parseArgs("-str");
fail("String argument without second element should fail");
} catch (final CommandParsingException e) {
// ok
}
parameters.parseArgs("-boolFlag");
parameters.parseArgs("-str", "-boolFlag");
parameters.parseArgs("-boolFlag", "-str", "val");
}
/** Test method for
* {@link fr.bigeon.gclc.command.CommandParameters#set(java.lang.String, boolean)}. */
@Test
public final void testSetStringBoolean() {
final Set<String> strings = new HashSet<>();
final Set<String> bools = new HashSet<>();
bools.add("boolFlag");
strings.add("str");
CommandParameters parameters = new CommandParameters(bools, strings,
true);
assertFalse(parameters.getBool("ungiven"));
assertFalse(parameters.getBool("boolFlag"));
parameters.set("boolFlag", true);
assertTrue(parameters.getBool("boolFlag"));
assertFalse(parameters.getBool("ungiven"));
parameters.set("ungiven", true);
assertFalse(parameters.getBool("ungiven"));
assertTrue(parameters.getBool("boolFlag"));
parameters = new CommandParameters(bools, strings, false);
assertFalse(parameters.getBool("ungiven"));
assertFalse(parameters.getBool("boolFlag"));
parameters.set("boolFlag", true);
assertTrue(parameters.getBool("boolFlag"));
assertFalse(parameters.getBool("ungiven"));
parameters.set("ungiven", true);
assertFalse(parameters.getBool("ungiven"));
assertTrue(parameters.getBool("boolFlag"));
}
/** Test method for
* {@link fr.bigeon.gclc.command.CommandParameters#set(java.lang.String, java.lang.String)}. */
@Test
public final void testSetStringString() {
final Set<String> strings = new HashSet<>();
final Set<String> bools = new HashSet<>();
bools.add("boolFlag");
strings.add("str");
CommandParameters parameters = new CommandParameters(bools, strings,
true);
assertNull(parameters.get("ungiven"));
assertNull(parameters.get("str"));
parameters.set("ungiven", "val");
assertNull(parameters.get("ungiven"));
assertNull(parameters.get("str"));
parameters.set("str", "val");
assertNull(parameters.get("ungiven"));
assertEquals("val", parameters.get("str"));
parameters.set("ungiven", "val");
assertNull(parameters.get("ungiven"));
assertEquals("val", parameters.get("str"));
parameters = new CommandParameters(bools, strings, false);
assertNull(parameters.get("ungiven"));
assertNull(parameters.get("str"));
parameters.set("ungiven", "val");
assertNull(parameters.get("ungiven"));
assertNull(parameters.get("str"));
parameters.set("str", "val");
assertNull(parameters.get("ungiven"));
assertEquals("val", parameters.get("str"));
parameters.set("ungiven", "val");
assertNull(parameters.get("ungiven"));
assertEquals("val", parameters.get("str"));
}
}
/*
* 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;
/*-
* #%L
* Generic Command Ligne console
* %%
* Copyright (C) 2014 - 2018 bigeon.fr
* %%
* 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.
* #L%
*/
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 static org.junit.Assert.fail;
import java.util.HashSet;
import java.util.Set;
import org.junit.Test;
import fr.bigeon.gclc.exception.CommandParsingException;
/**
* <p>
* TODO
*
* @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)}.
*
* @throws CommandParsingException if an unexpected exception is thrown */
@Test
public final void testCommandParameters() throws CommandParsingException {
final Set<String> strings = new HashSet<>();
final Set<String> bools = new HashSet<>();
CommandParameters parameters = new CommandParameters(bools, strings,
true);
try {
parameters.parseArgs("-ungivenFlag");
fail("parse of unknown in strict should fail");
} catch (final CommandParsingException e) {
// ok
}
parameters = new CommandParameters(bools, strings, false);
parameters.parseArgs("-ungivenFlag");
}
/** Test method for
* {@link fr.bigeon.gclc.command.CommandParameters#get(java.lang.String)}.
*
* @throws CommandParsingException if an exception occired */
@Test
public final void testGet() throws CommandParsingException {
final Set<String> strings = new HashSet<>();
final Set<String> bools = new HashSet<>();
bools.add("boolFlag");
strings.add("str");
final CommandParameters parameters = new CommandParameters(bools,
strings, true);
assertNull(parameters.get("ungiven"));
assertNull(parameters.get("str"));
try {
parameters.parseArgs("-ungiven", "val");
fail("Missing parameter should fail for strict element");
} catch (final CommandParsingException e) {
// ok
}
assertNull(parameters.get("ungiven"));
assertNull(parameters.get("str"));
parameters.parseArgs("-str", "val");
assertNull(parameters.get("ungiven"));
assertEquals("val", parameters.get("str"));
try {
parameters.parseArgs("-ungiven");
fail("Invalid argument type parsing should fail");
} catch (final CommandParsingException e) {
// ok
}
assertNull(parameters.get("ungiven"));
assertEquals("val", parameters.get("str"));
}
/** Test method for
* {@link fr.bigeon.gclc.command.CommandParameters#getAdditionals()}. */
@Test
public final void testGetAdditionals() throws CommandParsingException {
final Set<String> strings = new HashSet<>();
final Set<String> bools = new HashSet<>();
bools.add("boolFlag");
strings.add("str");
CommandParameters parameters = new CommandParameters(bools, strings,
true);
parameters.parseArgs("-boolFlag");
assertTrue(parameters.getAdditionals().isEmpty());
try {
parameters.parseArgs("-ungiven");
fail("Should fail");
} catch (final CommandParsingException e) {
// ok
}
assertTrue(parameters.getAdditionals().isEmpty());
parameters = new CommandParameters(bools, strings, false);
parameters.parseArgs("-boolFlag");
assertTrue(parameters.getAdditionals().isEmpty());
parameters.parseArgs("ungiven");
assertTrue(parameters.getAdditionals().contains("ungiven"));
assertEquals(1, parameters.getAdditionals().size());
}
/** Test method for
* {@link fr.bigeon.gclc.command.CommandParameters#getBool(java.lang.String)}.
*
* @throws CommandParsingException if a command parsing failed */
@Test
public final void testGetBool() throws CommandParsingException {
final Set<String> strings = new HashSet<>();
final Set<String> bools = new HashSet<>();
bools.add("boolFlag");
strings.add("str");
CommandParameters parameters = new CommandParameters(bools, strings,
true);
assertFalse(parameters.getBool("ungiven"));
assertFalse(parameters.getBool("boolFlag"));
parameters.parseArgs("-boolFlag");
assertTrue(parameters.getBool("boolFlag"));
assertFalse(parameters.getBool("ungiven"));
try {
parameters.parseArgs("-ungiven");
fail("unknown parameter should fail");
} catch (final CommandParsingException e) {
// ok
}
assertFalse(parameters.getBool("ungiven"));
assertTrue(parameters.getBool("boolFlag"));
parameters = new CommandParameters(bools, strings, false);
assertFalse(parameters.getBool("ungiven"));
assertFalse(parameters.getBool("boolFlag"));
try {
parameters.parseArgs("-boolFlag");
} catch (final CommandParsingException e) {
// ok
}
assertTrue(parameters.getBool("boolFlag"));
assertFalse(parameters.getBool("ungiven"));
try {
parameters.parseArgs("-ungiven");
} catch (final CommandParsingException e) {
// ok
}
assertFalse(parameters.getBool("ungiven"));
assertTrue(parameters.getBool("boolFlag"));
}
/** Test method for
* {@link fr.bigeon.gclc.command.CommandParameters#parseArgs(java.lang.String[])}.
*
* @throws CommandParsingException if a command parsing failed */
@Test
public final void testParseArgs() throws CommandParsingException {
final Set<String> strings = new HashSet<>();
final Set<String> bools = new HashSet<>();
bools.add("boolFlag");
strings.add("str");
final CommandParameters parameters = new CommandParameters(bools,
strings, true);
try {
parameters.parseArgs("-ungivenFlag");
fail("Strict should fail with flag");
} catch (final CommandParsingException e) {
// ok
}
try {
parameters.parseArgs("-str");
fail("String argument without second element should fail");
} catch (final CommandParsingException e) {
// ok
}
parameters.parseArgs("-boolFlag");
parameters.parseArgs("-str", "-boolFlag");
parameters.parseArgs("-boolFlag", "-str", "val");
}
/** Test method for
* {@link fr.bigeon.gclc.command.CommandParameters#set(java.lang.String, boolean)}. */
@Test
public final void testSetStringBoolean() {
final Set<String> strings = new HashSet<>();
final Set<String> bools = new HashSet<>();
bools.add("boolFlag");
strings.add("str");
CommandParameters parameters = new CommandParameters(bools, strings,
true);
assertFalse(parameters.getBool("ungiven"));
assertFalse(parameters.getBool("boolFlag"));
parameters.set("boolFlag", true);
assertTrue(parameters.getBool("boolFlag"));
assertFalse(parameters.getBool("ungiven"));
parameters.set("ungiven", true);
assertFalse(parameters.getBool("ungiven"));
assertTrue(parameters.getBool("boolFlag"));
parameters = new CommandParameters(bools, strings, false);
assertFalse(parameters.getBool("ungiven"));
assertFalse(parameters.getBool("boolFlag"));
parameters.set("boolFlag", true);
assertTrue(parameters.getBool("boolFlag"));
assertFalse(parameters.getBool("ungiven"));
parameters.set("ungiven", true);
assertFalse(parameters.getBool("ungiven"));
assertTrue(parameters.getBool("boolFlag"));
}
/** Test method for
* {@link fr.bigeon.gclc.command.CommandParameters#set(java.lang.String, java.lang.String)}. */
@Test
public final void testSetStringString() {
final Set<String> strings = new HashSet<>();
final Set<String> bools = new HashSet<>();
bools.add("boolFlag");
strings.add("str");
CommandParameters parameters = new CommandParameters(bools, strings,
true);
assertNull(parameters.get("ungiven"));
assertNull(parameters.get("str"));
parameters.set("ungiven", "val");
assertNull(parameters.get("ungiven"));
assertNull(parameters.get("str"));
parameters.set("str", "val");
assertNull(parameters.get("ungiven"));
assertEquals("val", parameters.get("str"));
parameters.set("ungiven", "val");
assertNull(parameters.get("ungiven"));
assertEquals("val", parameters.get("str"));
parameters = new CommandParameters(bools, strings, false);
assertNull(parameters.get("ungiven"));
assertNull(parameters.get("str"));
parameters.set("ungiven", "val");
assertNull(parameters.get("ungiven"));
assertNull(parameters.get("str"));
parameters.set("str", "val");
assertNull(parameters.get("ungiven"));
assertEquals("val", parameters.get("str"));
parameters.set("ungiven", "val");
assertNull(parameters.get("ungiven"));
assertEquals("val", parameters.get("str"));
}
}

View File

@@ -1,98 +1,132 @@
/*
* 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.CommandProviderTest.java
* Created on: Nov 19, 2016
*/
package fr.bigeon.gclc.command;
import static org.junit.Assert.fail;
import org.junit.Test;
import fr.bigeon.gclc.exception.InvalidCommandName;
/**
* <p>
* TODO
*
* @author Emmanuel Bigeon */
public class CommandProviderTest {
/** Test method for
* {@link fr.bigeon.gclc.command.CommandProvider#add(fr.bigeon.gclc.command.ICommand)}.
*
* @throws InvalidCommandName if the test failed */
@Test
public final void testAdd() throws InvalidCommandName {
final CommandProvider provider = new CommandProvider();
try {
provider.add(new MockCommand(null));
fail("null name for command should be rejected");
} catch (final InvalidCommandName e) {
// ok
}
try {
provider.add(new MockCommand(""));
fail("null name for command should be rejected");
} catch (final InvalidCommandName e) {
// ok
}
try {
provider.add(new MockCommand("-name"));
fail("name with minus as starting character for command should be rejected");
} catch (final InvalidCommandName e) {
// ok
}
try {
provider.add(new MockCommand("name command"));
fail("name with space for command should be rejected");
} catch (final InvalidCommandName e) {
// ok
}
final ICommand mock = new MockCommand("name");
provider.add(mock);
try {
provider.add(new MockCommand(mock.getCommandName()));
fail("already existing command name should be rejected");
} catch (final InvalidCommandName e) {
// ok
}
provider.add(mock);
}
}
/*
* 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.CommandProviderTest.java
* Created on: Nov 19, 2016
*/
package fr.bigeon.gclc.command;
/*-
* #%L
* Generic Command Ligne console
* %%
* Copyright (C) 2014 - 2018 bigeon.fr
* %%
* 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.
* #L%
*/
import static org.junit.Assert.fail;
import org.junit.Test;
import fr.bigeon.gclc.command.base.MockCommand;
import fr.bigeon.gclc.exception.InvalidCommandName;
/**
* <p>
* TODO
*
* @author Emmanuel Bigeon */
public class CommandProviderTest {
/** Test method for
* {@link fr.bigeon.gclc.command.CommandProvider#add(fr.bigeon.gclc.command.ICommand)}.
*
* @throws InvalidCommandName if the test failed */
@Test
public final void testAdd() throws InvalidCommandName {
final CommandProvider provider = new CommandProvider();
try {
provider.add(new MockCommand(null));
fail("null name for command should be rejected");
} catch (final InvalidCommandName e) {
// ok
}
try {
provider.add(new MockCommand(""));
fail("null name for command should be rejected");
} catch (final InvalidCommandName e) {
// ok
}
try {
provider.add(new MockCommand("-name"));
fail("name with minus as starting character for command should be rejected");
} catch (final InvalidCommandName e) {
// ok
}
try {
provider.add(new MockCommand("name command"));
fail("name with space for command should be rejected");
} catch (final InvalidCommandName e) {
// ok
}
final ICommand mock = new MockCommand("name");
provider.add(mock);
try {
provider.add(new MockCommand(mock.getCommandName()));
fail("already existing command name should be rejected");
} catch (final InvalidCommandName e) {
// ok
}
provider.add(mock);
}
}

View File

@@ -1,376 +1,409 @@
/*
* 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.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import org.junit.Test;
import fr.bigeon.gclc.exception.CommandRunException;
import fr.bigeon.gclc.manager.ConsoleInput;
import fr.bigeon.gclc.manager.ConsoleOutput;
import fr.bigeon.gclc.manager.PipedConsoleOutput;
/** <p>
* TODO
*
* @author Emmanuel Bigeon */
public class CommandTest {
@Test
public final void testCommand() throws IOException {
try (PipedConsoleOutput test = new PipedConsoleOutput()) {
Command cmd;
cmd = new Command("name") {
@Override
public void execute(final ConsoleOutput out,
final ConsoleInput in,
final String... args) throws CommandRunException {
//
}
@Override
public String tip() {
return null;
}
@Override
protected String usageDetail() {
return null;
}
};
cmd.help(test);
// name
assertEquals("First line should be command name", "name",
test.readNextLine());
// tip
test.readNextLine();
//
assertTrue("Section line is empty", test.readNextLine().isEmpty());
// Usage
test.readNextLine();
// details
test.readNextLine();
//
assertTrue("Section line is empty", test.readNextLine().isEmpty());
assertFalse("Only 6 line should be printed", test.available());
cmd = new Command("name") {
@Override
public void execute(final ConsoleOutput out,
final ConsoleInput in,
final String... args) throws CommandRunException {
//
}
@Override
public String tip() {
return "";
}
@Override
protected String usageDetail() {
return null;
}
};
cmd.help(test);
// name
assertEquals("First line should be command name", "name",
test.readNextLine());
// tip
test.readNextLine();
//
assertTrue("Section line is empty", test.readNextLine().isEmpty());
// Usage
test.readNextLine();
// details
test.readNextLine();
//
assertTrue("Section line is empty", test.readNextLine().isEmpty());
assertFalse("Only 6 line should be printed", test.available());
cmd = new Command("name") {
/* (non-Javadoc)
* @see fr.bigeon.gclc.command.Command#brief() */
@Override
protected String brief() {
return null;
}
@Override
public void execute(final ConsoleOutput out,
final ConsoleInput in,
final String... args) throws CommandRunException {
//
}
@Override
public String tip() {
return "tip";
}
@Override
protected String usageDetail() {
return null;
}
};
cmd.help(test);
// name
assertEquals("First line should be command name", "name",
test.readNextLine());
// tip
test.readNextLine();
//
assertTrue("Section line is empty", test.readNextLine().isEmpty());
// Usage
test.readNextLine();
// details
test.readNextLine();
//
assertTrue("Section line is empty", test.readNextLine().isEmpty());
assertFalse("Only 6 line should be printed", test.available());
cmd = new Command("name") {
/* (non-Javadoc)
* @see fr.bigeon.gclc.command.Command#brief() */
@Override
protected String brief() {
return "brief";
}
@Override
public void execute(final ConsoleOutput out,
final ConsoleInput in,
final String... args) throws CommandRunException {
//
}
@Override
public String tip() {
return "tip";
}
@Override
protected String usageDetail() {
return null;
}
};
cmd.help(test);
// name
assertEquals("First line should be command name", "name",
test.readNextLine());
// tip
test.readNextLine();
//
assertTrue("Section line is empty", test.readNextLine().isEmpty());
// Usage
test.readNextLine();
// details
test.readNextLine();
//
assertTrue("Section line is empty", test.readNextLine().isEmpty());
assertFalse("Only 6 line should be printed", test.available());
cmd = new Command("name") {
@Override
public void execute(final ConsoleOutput out,
final ConsoleInput in,
final String... args) throws CommandRunException {
//
}
@Override
public String tip() {
return "tip";
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.command.Command#usageDetail() */
@Override
protected String usageDetail() {
return null;
}
};
cmd.help(test);
// name
assertEquals("First line should be command name", "name",
test.readNextLine());
// tip
test.readNextLine();
//
assertTrue("Section line is empty", test.readNextLine().isEmpty());
// Usage
test.readNextLine();
// details
test.readNextLine();
//
assertTrue("Section line is empty", test.readNextLine().isEmpty());
assertFalse("Only 6 line should be printed", test.available());
cmd = new Command("name") {
@Override
public void execute(final ConsoleOutput out,
final ConsoleInput in,
final String... args) throws CommandRunException {
//
}
@Override
public String tip() {
return "tip";
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.command.Command#usageDetail() */
@Override
protected String usageDetail() {
return "details";
}
};
cmd.help(test);
// name
assertEquals("First line should be command name", "name",
test.readNextLine());
// tip
test.readNextLine();
//
assertTrue("Section line is empty", test.readNextLine().isEmpty());
// Usage
test.readNextLine();
// pattern
test.readNextLine();
//
assertTrue("Section line is empty", test.readNextLine().isEmpty());
// details
assertEquals("Unexpected detail", "details", test.readNextLine());
assertFalse("Only 6 line should be printed", test.available());
cmd = new Command("name") {
@Override
public void execute(final ConsoleOutput out,
final ConsoleInput in,
final String... args) throws CommandRunException {
//
}
@Override
public String tip() {
return "tip";
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.command.Command#usageDetail() */
@Override
protected String usageDetail() {
return "details" + System.lineSeparator();
}
};
cmd.help(test);
// name
assertEquals("First line should be command name", "name",
test.readNextLine());
// tip
test.readNextLine();
//
assertTrue("Section line is empty", test.readNextLine().isEmpty());
// Usage
test.readNextLine();
// pattern
test.readNextLine();
//
assertTrue("Section line is empty", test.readNextLine().isEmpty());
// details
assertEquals("Unexpected detail", "details", test.readNextLine());
assertFalse("Only 6 line should be printed", test.available());
cmd = new Command("name") {
@Override
public void execute(final ConsoleOutput out,
final ConsoleInput in,
final String... args) throws CommandRunException {
//
}
@Override
public String tip() {
return "tip";
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.command.Command#usageDetail() */
@Override
protected String usageDetail() {
return "\n";
}
};
cmd.help(test);
// name
assertEquals("First line should be command name", "name",
test.readNextLine());
// tip
test.readNextLine();
//
assertTrue("Section line is empty", test.readNextLine().isEmpty());
// Usage
test.readNextLine();
// pattern
test.readNextLine();
//
assertTrue("Section line is empty", test.readNextLine().isEmpty());
//
assertTrue("Section line is empty", test.readNextLine().isEmpty());
assertFalse("Only 6 line should be printed", test.available());
}
}
}
/*
* 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;
/*-
* #%L
* Generic Command Ligne console
* %%
* Copyright (C) 2014 - 2018 bigeon.fr
* %%
* 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.
* #L%
*/
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import org.junit.Test;
import fr.bigeon.gclc.exception.CommandRunException;
import fr.bigeon.gclc.manager.ConsoleInput;
import fr.bigeon.gclc.manager.ConsoleOutput;
import fr.bigeon.gclc.utils.PipedConsoleOutput;
/** <p>
* TODO
*
* @author Emmanuel Bigeon */
public class CommandTest {
@Test
public final void testCommand() throws IOException {
try (PipedConsoleOutput test = new PipedConsoleOutput()) {
Command cmd;
cmd = new Command("name") {
@Override
public void execute(final ConsoleOutput out,
final ConsoleInput in,
final String... args) throws CommandRunException {
//
}
@Override
public String tip() {
return null;
}
@Override
protected String usageDetail() {
return null;
}
};
cmd.help(test);
// name
assertEquals("First line should be command name", "name",
test.readNextLine());
// tip
test.readNextLine();
//
assertTrue("Section line is empty", test.readNextLine().isEmpty());
// Usage
test.readNextLine();
// details
test.readNextLine();
//
assertTrue("Section line is empty", test.readNextLine().isEmpty());
assertFalse("Only 6 line should be printed", test.available());
cmd = new Command("name") {
@Override
public void execute(final ConsoleOutput out,
final ConsoleInput in,
final String... args) throws CommandRunException {
//
}
@Override
public String tip() {
return "";
}
@Override
protected String usageDetail() {
return null;
}
};
cmd.help(test);
// name
assertEquals("First line should be command name", "name",
test.readNextLine());
// tip
test.readNextLine();
//
assertTrue("Section line is empty", test.readNextLine().isEmpty());
// Usage
test.readNextLine();
// details
test.readNextLine();
//
assertTrue("Section line is empty", test.readNextLine().isEmpty());
assertFalse("Only 6 line should be printed", test.available());
cmd = new Command("name") {
/* (non-Javadoc)
* @see fr.bigeon.gclc.command.Command#brief() */
@Override
protected String brief() {
return null;
}
@Override
public void execute(final ConsoleOutput out,
final ConsoleInput in,
final String... args) throws CommandRunException {
//
}
@Override
public String tip() {
return "tip";
}
@Override
protected String usageDetail() {
return null;
}
};
cmd.help(test);
// name
assertEquals("First line should be command name", "name",
test.readNextLine());
// tip
test.readNextLine();
//
assertTrue("Section line is empty", test.readNextLine().isEmpty());
// Usage
test.readNextLine();
// details
test.readNextLine();
//
assertTrue("Section line is empty", test.readNextLine().isEmpty());
assertFalse("Only 6 line should be printed", test.available());
cmd = new Command("name") {
/* (non-Javadoc)
* @see fr.bigeon.gclc.command.Command#brief() */
@Override
protected String brief() {
return "brief";
}
@Override
public void execute(final ConsoleOutput out,
final ConsoleInput in,
final String... args) throws CommandRunException {
//
}
@Override
public String tip() {
return "tip";
}
@Override
protected String usageDetail() {
return null;
}
};
cmd.help(test);
// name
assertEquals("First line should be command name", "name",
test.readNextLine());
// tip
test.readNextLine();
//
assertTrue("Section line is empty", test.readNextLine().isEmpty());
// Usage
test.readNextLine();
// details
test.readNextLine();
//
assertTrue("Section line is empty", test.readNextLine().isEmpty());
assertFalse("Only 6 line should be printed", test.available());
cmd = new Command("name") {
@Override
public void execute(final ConsoleOutput out,
final ConsoleInput in,
final String... args) throws CommandRunException {
//
}
@Override
public String tip() {
return "tip";
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.command.Command#usageDetail() */
@Override
protected String usageDetail() {
return null;
}
};
cmd.help(test);
// name
assertEquals("First line should be command name", "name",
test.readNextLine());
// tip
test.readNextLine();
//
assertTrue("Section line is empty", test.readNextLine().isEmpty());
// Usage
test.readNextLine();
// details
test.readNextLine();
//
assertTrue("Section line is empty", test.readNextLine().isEmpty());
assertFalse("Only 6 line should be printed", test.available());
cmd = new Command("name") {
@Override
public void execute(final ConsoleOutput out,
final ConsoleInput in,
final String... args) throws CommandRunException {
//
}
@Override
public String tip() {
return "tip";
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.command.Command#usageDetail() */
@Override
protected String usageDetail() {
return "details";
}
};
cmd.help(test);
// name
assertEquals("First line should be command name", "name",
test.readNextLine());
// tip
test.readNextLine();
//
assertTrue("Section line is empty", test.readNextLine().isEmpty());
// Usage
test.readNextLine();
// pattern
test.readNextLine();
//
assertTrue("Section line is empty", test.readNextLine().isEmpty());
// details
assertEquals("Unexpected detail", "details", test.readNextLine());
assertFalse("Only 6 line should be printed", test.available());
cmd = new Command("name") {
@Override
public void execute(final ConsoleOutput out,
final ConsoleInput in,
final String... args) throws CommandRunException {
//
}
@Override
public String tip() {
return "tip";
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.command.Command#usageDetail() */
@Override
protected String usageDetail() {
return "details" + System.lineSeparator();
}
};
cmd.help(test);
// name
assertEquals("First line should be command name", "name",
test.readNextLine());
// tip
test.readNextLine();
//
assertTrue("Section line is empty", test.readNextLine().isEmpty());
// Usage
test.readNextLine();
// pattern
test.readNextLine();
//
assertTrue("Section line is empty", test.readNextLine().isEmpty());
// details
assertEquals("Unexpected detail", "details", test.readNextLine());
assertFalse("Only 6 line should be printed", test.available());
cmd = new Command("name") {
@Override
public void execute(final ConsoleOutput out,
final ConsoleInput in,
final String... args) throws CommandRunException {
//
}
@Override
public String tip() {
return "tip";
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.command.Command#usageDetail() */
@Override
protected String usageDetail() {
return "\n";
}
};
cmd.help(test);
// name
assertEquals("First line should be command name", "name",
test.readNextLine());
// tip
test.readNextLine();
//
assertTrue("Section line is empty", test.readNextLine().isEmpty());
// Usage
test.readNextLine();
// pattern
test.readNextLine();
//
assertTrue("Section line is empty", test.readNextLine().isEmpty());
//
assertTrue("Section line is empty", test.readNextLine().isEmpty());
assertFalse("Only 6 line should be printed", test.available());
}
}
}

View File

@@ -1,118 +1,152 @@
/*
* 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.fail;
import java.io.IOException;
import org.junit.Test;
import fr.bigeon.gclc.exception.CommandRunException;
import fr.bigeon.gclc.manager.ConsoleInput;
import fr.bigeon.gclc.manager.ConsoleOutput;
import fr.bigeon.gclc.manager.PipedConsoleOutput;
/**
* <p>
* TODO
*
* @author Emmanuel Bigeon */
public class HelpExecutorTest {
/** Test method for
* {@link fr.bigeon.gclc.command.HelpExecutor#execute(ConsoleOutput, ConsoleInput, String...)}.
*
* @throws CommandRunException if the test failed
* @throws IOException if the test failed */
@Test
public final void testExecute() throws CommandRunException, IOException {
final PipedConsoleOutput test = new PipedConsoleOutput();
final HelpExecutor help = new HelpExecutor("?", new Command("mock") {
@Override
public void execute(final ConsoleOutput out, final ConsoleInput in,
final String... args) throws CommandRunException {
//
}
@Override
public String tip() {
return "";
}
@Override
protected String usageDetail() {
return null;
}
});
help.execute(test, null);
test.close();
try {
help.execute(test, null);
fail("manager closed shall provoke failure of help command execution");
} catch (final Exception e) {
// ok
}
}
/** Test method for {@link fr.bigeon.gclc.command.HelpExecutor#tip()}.
*
* @throws IOException if the test failed */
@Test
public final void testTip() throws IOException {
try (PipedConsoleOutput test = new PipedConsoleOutput()) {
final HelpExecutor help = new HelpExecutor("?",
new MockCommand("mock"));
assertNotNull("Tip should be provided", help.tip());
help.help(test);
}
try (PipedConsoleOutput test = new PipedConsoleOutput()) {
final HelpExecutor help = new HelpExecutor("?",
new SubedCommand("sub", new MockCommand("mock")));
help.tip();
help.help(test);
}
}
}
/*
* 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;
/*-
* #%L
* Generic Command Ligne console
* %%
* Copyright (C) 2014 - 2018 bigeon.fr
* %%
* 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.
* #L%
*/
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import java.io.IOException;
import org.junit.Test;
import fr.bigeon.gclc.command.base.MockCommand;
import fr.bigeon.gclc.exception.CommandRunException;
import fr.bigeon.gclc.manager.ConsoleInput;
import fr.bigeon.gclc.manager.ConsoleOutput;
import fr.bigeon.gclc.utils.PipedConsoleOutput;
/**
* <p>
* TODO
*
* @author Emmanuel Bigeon */
public class HelpExecutorTest {
/** Test method for
* {@link fr.bigeon.gclc.command.HelpExecutor#execute(ConsoleOutput, ConsoleInput, String...)}.
*
* @throws CommandRunException if the test failed
* @throws IOException if the test failed */
@Test
public final void testExecute() throws CommandRunException, IOException {
final PipedConsoleOutput test = new PipedConsoleOutput();
final HelpExecutor help = new HelpExecutor("?", new Command("mock") {
@Override
public void execute(final ConsoleOutput out, final ConsoleInput in,
final String... args) throws CommandRunException {
//
}
@Override
public String tip() {
return "";
}
@Override
protected String usageDetail() {
return null;
}
});
help.execute(test, null);
test.close();
try {
help.execute(test, null);
fail("manager closed shall provoke failure of help command execution");
} catch (final Exception e) {
// ok
}
}
/** Test method for {@link fr.bigeon.gclc.command.HelpExecutor#tip()}.
*
* @throws IOException if the test failed */
@Test
public final void testTip() throws IOException {
try (PipedConsoleOutput test = new PipedConsoleOutput()) {
final HelpExecutor help = new HelpExecutor("?",
new MockCommand("mock"));
assertNotNull("Tip should be provided", help.tip());
help.help(test);
}
try (PipedConsoleOutput test = new PipedConsoleOutput()) {
final HelpExecutor help = new HelpExecutor("?",
new SubedCommand("sub", new MockCommand("mock")));
help.tip();
help.help(test);
}
}
}

View File

@@ -1,181 +1,209 @@
/*
* 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.ScriptExecutionTest.java
* Created on: Jun 12, 2016
*/
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.ConsoleApplication;
import fr.bigeon.gclc.ConsoleTestApplication;
import fr.bigeon.gclc.exception.CommandRunException;
import fr.bigeon.gclc.exception.CommandRunExceptionType;
import fr.bigeon.gclc.manager.PipedConsoleInput;
import fr.bigeon.gclc.manager.PipedConsoleOutput;
/** <p>
* Test class for {@link ScriptExecution}
*
* @author Emmanuel Bigeon */
@SuppressWarnings("static-method")
public class ScriptExecutionTest {
/** Test method for
* {@link fr.bigeon.gclc.command.ScriptExecution#execute(fr.bigeon.gclc.manager.ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput, String...)}. */
@Test
public void testExecute() {
PipedConsoleOutput test;
PipedConsoleInput in;
try {
in = new PipedConsoleInput(null);
test = new PipedConsoleOutput();
} catch (final IOException e2) {
fail("creation of console manager failed"); //$NON-NLS-1$
return;
}
final ConsoleApplication app = new ConsoleApplication(
test, in, "", "");
new ConsoleTestApplication().attach(app);
final ScriptExecution exec = new ScriptExecution("script", app, "#", //$NON-NLS-1$ //$NON-NLS-2$
Charset.forName("UTF-8"));
try {
exec.execute(test, in);
fail("execution of script command with no file should fail"); //$NON-NLS-1$
} catch (final CommandRunException e1) {
// ok
assertEquals(exec, e1.getSource());
assertEquals(CommandRunExceptionType.USAGE, e1.getType());
}
try {
exec.execute(test, in,
"src/test/resources/scripts/withprependSpace.txt"); //$NON-NLS-1$
fail("execution of script with lines begining with space should fail"); //$NON-NLS-1$
} catch (final CommandRunException e1) {
// ok
assertEquals(exec, e1.getSource());
assertEquals(CommandRunExceptionType.EXECUTION, e1.getType());
}
try {
exec.execute(test, in,
"src/test/resources/scripts/invalidCmdParse.txt"); //$NON-NLS-1$
fail("execution of script with invalid command line should fail"); //$NON-NLS-1$
} catch (final CommandRunException e1) {
// ok
assertEquals(exec, e1.getSource());
assertEquals(CommandRunExceptionType.EXECUTION, e1.getType());
}
try {
exec.execute(test, in,
"src/test/resources/scripts/invalidCmd.txt"); //$NON-NLS-1$
fail("execution of script with invalid command should fail"); //$NON-NLS-1$
} catch (final CommandRunException e1) {
// ok
assertEquals(exec, e1.getSource());
assertEquals(CommandRunExceptionType.EXECUTION, e1.getType());
}
try {
exec.execute(test, in,
"src/test/resources/scripts/failingCmdInvoc.txt"); //$NON-NLS-1$
fail("execution of script with failing command should fail"); //$NON-NLS-1$
} catch (final CommandRunException e1) {
// ok
assertEquals(exec, e1.getSource());
assertEquals(CommandRunExceptionType.EXECUTION, e1.getType());
}
try {
exec.execute(test, in,
"src/test/resources/scripts/someNonExisting.file"); //$NON-NLS-1$
fail("execution of script with unexisting file should fail"); //$NON-NLS-1$
} catch (final CommandRunException e1) {
// ok
assertEquals(exec, e1.getSource());
assertEquals(CommandRunExceptionType.EXECUTION, e1.getType());
}
try {
exec.execute(test, in, "src/test/resources/script1.txt"); //$NON-NLS-1$
exec.execute(test, in, "src/test/resources/script2.txt"); //$NON-NLS-1$
exec.execute(test, in, "src/test/resources/script3.txt"); //$NON-NLS-1$
exec.execute(test, in, "src/test/resources/script4.txt"); //$NON-NLS-1$
exec.execute(test, in, "src/test/resources/script5.txt", "test"); //$NON-NLS-1$ //$NON-NLS-2$
} catch (final CommandRunException e) {
e.printStackTrace();
fail("execution of wellformed script should not fail"); //$NON-NLS-1$
}
try {
test.close();
} catch (final IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/** Test method for
* {@link fr.bigeon.gclc.command.ScriptExecution#help(fr.bigeon.gclc.manager.ConsoleOutput, String...)}. */
@Test
public void testHelp() {
final ScriptExecution exec = new ScriptExecution("script", null, "#", //$NON-NLS-1$ //$NON-NLS-2$
Charset.forName("UTF-8"));
try (PipedConsoleOutput test = new PipedConsoleOutput()) {
exec.help(test);
exec.help(test, "ignored element");
} catch (final IOException e) {
e.printStackTrace();
fail("unexpected error in help invocation"); //$NON-NLS-1$
}
}
/** Test method for {@link fr.bigeon.gclc.command.ScriptExecution#tip()}. */
@Test
public void testTip() {
final ScriptExecution exec = new ScriptExecution("script", null, "#", //$NON-NLS-1$ //$NON-NLS-2$
Charset.forName("UTF-8"));
assertNotNull("Tip should not be null", exec.tip());
}
}
/*
* 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.ScriptExecutionTest.java
* Created on: Jun 12, 2016
*/
package fr.bigeon.gclc.command;
/*-
* #%L
* Generic Command Ligne console
* %%
* Copyright (C) 2014 - 2018 bigeon.fr
* %%
* 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.
* #L%
*/
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.ConsoleApplication;
import fr.bigeon.gclc.ConsoleTestApplication;
import fr.bigeon.gclc.command.base.ScriptExecution;
import fr.bigeon.gclc.exception.CommandRunException;
import fr.bigeon.gclc.exception.CommandRunExceptionType;
import fr.bigeon.gclc.utils.PipedConsoleInput;
import fr.bigeon.gclc.utils.PipedConsoleOutput;
/** <p>
* Test class for {@link ScriptExecution}
*
* @author Emmanuel Bigeon */
@SuppressWarnings("static-method")
public class ScriptExecutionTest {
/** Test method for
* {@link fr.bigeon.gclc.command.base.ScriptExecution#execute(fr.bigeon.gclc.manager.ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput, String...)}. */
@Test
public void testExecute() {
PipedConsoleOutput test;
PipedConsoleInput in;
try {
in = new PipedConsoleInput(null);
test = new PipedConsoleOutput();
} catch (final IOException e2) {
fail("creation of console manager failed"); //$NON-NLS-1$
return;
}
final ConsoleApplication app = new ConsoleApplication(
test, in, "", "");
new ConsoleTestApplication().attach(app);
final ScriptExecution exec = new ScriptExecution("script", app, "#", //$NON-NLS-1$ //$NON-NLS-2$
Charset.forName("UTF-8"));
try {
exec.execute(test, in);
fail("execution of script command with no file should fail"); //$NON-NLS-1$
} catch (final CommandRunException e1) {
// ok
assertEquals(CommandRunExceptionType.USAGE, e1.getType());
}
try {
exec.execute(test, in,
"src/test/resources/scripts/withprependSpace.txt"); //$NON-NLS-1$
fail("execution of script with lines begining with space should fail"); //$NON-NLS-1$
} catch (final CommandRunException e1) {
// ok
assertEquals(CommandRunExceptionType.EXECUTION, e1.getType());
}
try {
exec.execute(test, in,
"src/test/resources/scripts/invalidCmdParse.txt"); //$NON-NLS-1$
fail("execution of script with invalid command line should fail"); //$NON-NLS-1$
} catch (final CommandRunException e1) {
// ok
assertEquals(CommandRunExceptionType.EXECUTION, e1.getType());
}
try {
exec.execute(test, in,
"src/test/resources/scripts/invalidCmd.txt"); //$NON-NLS-1$
fail("execution of script with invalid command should fail"); //$NON-NLS-1$
} catch (final CommandRunException e1) {
// ok
assertEquals(CommandRunExceptionType.EXECUTION, e1.getType());
}
try {
exec.execute(test, in,
"src/test/resources/scripts/failingCmdInvoc.txt"); //$NON-NLS-1$
fail("execution of script with failing command should fail"); //$NON-NLS-1$
} catch (final CommandRunException e1) {
// ok
assertEquals(CommandRunExceptionType.EXECUTION, e1.getType());
}
try {
exec.execute(test, in,
"src/test/resources/scripts/someNonExisting.file"); //$NON-NLS-1$
fail("execution of script with unexisting file should fail"); //$NON-NLS-1$
} catch (final CommandRunException e1) {
// ok
assertEquals(CommandRunExceptionType.EXECUTION, e1.getType());
}
try {
exec.execute(test, in, "src/test/resources/script1.gclc"); //$NON-NLS-1$
exec.execute(test, in, "src/test/resources/script2.txt"); //$NON-NLS-1$
exec.execute(test, in, "src/test/resources/script3.txt"); //$NON-NLS-1$
exec.execute(test, in, "src/test/resources/script4.txt"); //$NON-NLS-1$
exec.execute(test, in, "src/test/resources/script5.txt", "test"); //$NON-NLS-1$ //$NON-NLS-2$
} catch (final CommandRunException e) {
e.printStackTrace();
fail("execution of wellformed script should not fail"); //$NON-NLS-1$
}
try {
test.close();
} catch (final IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/** Test method for
* {@link fr.bigeon.gclc.command.base.ScriptExecution#help(fr.bigeon.gclc.manager.ConsoleOutput, String...)}. */
@Test
public void testHelp() {
final ScriptExecution exec = new ScriptExecution("script", null, "#", //$NON-NLS-1$ //$NON-NLS-2$
Charset.forName("UTF-8"));
try (PipedConsoleOutput test = new PipedConsoleOutput()) {
exec.help(test);
exec.help(test, "ignored element");
} catch (final IOException e) {
e.printStackTrace();
fail("unexpected error in help invocation"); //$NON-NLS-1$
}
}
/** Test method for {@link fr.bigeon.gclc.command.base.ScriptExecution#tip()}. */
@Test
public void testTip() {
final ScriptExecution exec = new ScriptExecution("script", null, "#", //$NON-NLS-1$ //$NON-NLS-2$
Charset.forName("UTF-8"));
assertNotNull("Tip should not be null", exec.tip());
}
}

View File

@@ -1,408 +1,438 @@
/*
* 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.ConsoleInput;
import fr.bigeon.gclc.manager.ConsoleOutput;
import fr.bigeon.gclc.manager.PipedConsoleOutput;
/**
* <p>
* TODO
*
* @author Emmanuel Bigeon */
@SuppressWarnings("all")
public class SubedCommandTest {
/** Test method for
* {@link fr.bigeon.gclc.command.SubedCommand#add(fr.bigeon.gclc.command.ICommand)}. */
@Test
public final void testAdd() {
final SubedCommand cmd = new SubedCommand("name");
try {
cmd.add(new MockCommand("id"));
} catch (final InvalidCommandName e) {
fail("addition of command with valid id failed");
}
try {
cmd.add(new MockCommand("id"));
fail("addition of command with already used id succeeded");
} catch (final InvalidCommandName e) {
//
}
try {
cmd.add(new MockCommand(""));
fail("addition of command with invalid id succeeded");
} catch (final InvalidCommandName e) {
//
}
}
/** Test method for
* {@link fr.bigeon.gclc.command.SubedCommand#execute(ConsoleOutput, ConsoleInput, String...)}. */
@Test
public final void testExecute() {
SubedCommand cmd = new SubedCommand("name");
final MockCommand mock = new MockCommand("id");
try {
cmd.add(mock);
cmd.add(new Command("fail") {
@Override
public void execute(final ConsoleOutput out,
final ConsoleInput in,
final String... args) throws CommandRunException {
throw new CommandRunException("Failing command", null);
}
@Override
public String tip() {
return null;
}
@Override
protected String usageDetail() {
return null;
}
});
} catch (final InvalidCommandName e) {
fail("addition of command with valid id failed");
assertNotNull(e);
}
try {
cmd.execute(null, null, "id");
} catch (final CommandRunException e) {
fail("Unexpected exception when running mock command");
assertNotNull(e);
}
try {
cmd.execute(null, null, "fail");
fail("Fail command error should be re thrown");
} catch (final CommandRunException e) {
assertNotNull(e);
assertEquals(cmd, e.getSource());
}
try {
cmd.execute(null, null);
fail("Request for inexistent default command should fail");
} catch (final CommandRunException e) {
assertNotNull(e);
assertEquals(cmd, e.getSource());
}
cmd = new SubedCommand("name", mock);
try {
cmd.add(mock);
cmd.add(new Command("fail") {
@Override
public void execute(final ConsoleOutput out,
final ConsoleInput in,
final String... args) throws CommandRunException {
throw new CommandRunException("Failing command", this);
}
@Override
public String tip() {
return null;
}
@Override
protected String usageDetail() {
return null;
}
});
} catch (final InvalidCommandName e) {
fail("addition of command with valid id failed");
assertNotNull(e);
}
try {
cmd.execute(null, null, "id");
} catch (final CommandRunException e) {
fail("Unexpected exception when running mock command");
assertNotNull(e);
}
try {
cmd.execute(null, null, "fail");
fail("Fail command error should be re thrown");
} catch (final CommandRunException e) {
assertNotNull(e);
assertEquals(cmd.get("fail"), e.getSource());
}
try {
cmd.execute(null, null);
} catch (final CommandRunException e) {
fail("Request for default command should execute default command");
assertNotNull(e);
}
}
/** Test method for
* {@link fr.bigeon.gclc.command.SubedCommand#executeSub(ConsoleOutput, ConsoleInput, String, String...)}. */
@Test
public final void testExecuteSub() {
final SubedCommand cmd = new SubedCommand("name");
final MockCommand mock = new MockCommand("id");
try {
cmd.add(mock);
cmd.add(new Command("fail") {
@Override
public void execute(final ConsoleOutput out,
final ConsoleInput in,
final String... args) throws CommandRunException {
throw new CommandRunException("Failing command", this);
}
@Override
public String tip() {
return null;
}
@Override
protected String usageDetail() {
return null;
}
});
} catch (final InvalidCommandName e) {
fail("addition of command with valid id failed");
assertNotNull(e);
}
try {
cmd.executeSub(null, null,"id");
} catch (final CommandRunException e) {
fail("Unexpected exception when running mock command");
assertNotNull(e);
}
try {
cmd.executeSub(null, null, "fail");
fail("Fail command error should be re thrown");
} catch (final 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() {
final SubedCommand cmd = new SubedCommand("name");
assertNull(cmd.get("id"));
final MockCommand mock = new MockCommand("id");
try {
cmd.add(mock);
} catch (final 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(ConsoleOutput, String...)}. */
@Test
public final void testHelp() {
SubedCommand cmd = new SubedCommand("name");
ICommand mock = new MockCommand("id");
try {
cmd.add(mock);
} catch (final InvalidCommandName e) {
fail("addition of command with valid id failed");
assertNotNull(e);
}
try (PipedConsoleOutput manager = new PipedConsoleOutput()) {
cmd.help(manager);
assertEquals("\tid", manager.readNextLine());
cmd.help(manager, "id");
cmd.help(manager, "inexistent");
} catch (final IOException e) {
fail("Unexpected exception when running help");
assertNotNull(e);
}
cmd = new SubedCommand("name", mock);
try {
cmd.add(mock);
} catch (final InvalidCommandName e) {
fail("addition of command with valid id failed");
assertNotNull(e);
}
try (PipedConsoleOutput manager = new PipedConsoleOutput()) {
cmd.help(manager);
assertEquals("\tid", manager.readNextLine());
} catch (final IOException e) {
fail("Unexpected exception when running help");
assertNotNull(e);
}
mock = new ICommand() {
@Override
public void execute(final ConsoleOutput out, final ConsoleInput in,
final String... args) throws CommandRunException {
//
}
@Override
public String getCommandName() {
return "id";
}
@Override
public void help(final ConsoleOutput manager,
final String... args) throws IOException {
//
}
@Override
public String tip() {
return "tip";
}
};
cmd = new SubedCommand("name", mock);
try {
cmd.add(mock);
} catch (final InvalidCommandName e) {
fail("addition of command with valid id failed");
assertNotNull(e);
}
try (PipedConsoleOutput manager = new PipedConsoleOutput()) {
cmd.help(manager);
assertEquals("\ttip", manager.readNextLine());
assertEquals("\tid: tip", manager.readNextLine());
} catch (final IOException e) {
fail("Unexpected exception when running help");
assertNotNull(e);
}
}
/** 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#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());
}
}
/*
* 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;
/*-
* #%L
* Generic Command Ligne console
* %%
* Copyright (C) 2014 - 2018 bigeon.fr
* %%
* 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.
* #L%
*/
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.command.base.MockCommand;
import fr.bigeon.gclc.exception.CommandRunException;
import fr.bigeon.gclc.exception.InvalidCommandName;
import fr.bigeon.gclc.manager.ConsoleInput;
import fr.bigeon.gclc.manager.ConsoleOutput;
import fr.bigeon.gclc.utils.PipedConsoleOutput;
/**
* <p>
* TODO
*
* @author Emmanuel Bigeon */
@SuppressWarnings("all")
public class SubedCommandTest {
/** Test method for
* {@link fr.bigeon.gclc.command.SubedCommand#add(fr.bigeon.gclc.command.ICommand)}. */
@Test
public final void testAdd() {
final SubedCommand cmd = new SubedCommand("name");
try {
cmd.add(new MockCommand("id"));
} catch (final InvalidCommandName e) {
fail("addition of command with valid id failed");
}
try {
cmd.add(new MockCommand("id"));
fail("addition of command with already used id succeeded");
} catch (final InvalidCommandName e) {
//
}
try {
cmd.add(new MockCommand(""));
fail("addition of command with invalid id succeeded");
} catch (final InvalidCommandName e) {
//
}
}
/** Test method for
* {@link fr.bigeon.gclc.command.SubedCommand#execute(ConsoleOutput, ConsoleInput, String...)}. */
@Test
public final void testExecute() {
SubedCommand cmd = new SubedCommand("name");
final MockCommand mock = new MockCommand("id");
try {
cmd.add(mock);
cmd.add(new Command("fail") {
@Override
public void execute(final ConsoleOutput out,
final ConsoleInput in,
final String... args) throws CommandRunException {
throw new CommandRunException("Failing command", null);
}
@Override
public String tip() {
return null;
}
@Override
protected String usageDetail() {
return null;
}
});
} catch (final InvalidCommandName e) {
fail("addition of command with valid id failed");
assertNotNull(e);
}
try {
cmd.execute(null, null, "id");
} catch (final CommandRunException e) {
fail("Unexpected exception when running mock command");
assertNotNull(e);
}
try {
cmd.execute(null, null, "fail");
fail("Fail command error should be re thrown");
} catch (final CommandRunException e) {
assertNotNull(e);
}
try {
cmd.execute(null, null);
fail("Request for inexistent default command should fail");
} catch (final CommandRunException e) {
assertNotNull(e);
}
cmd = new SubedCommand("name", mock);
try {
cmd.add(mock);
cmd.add(new Command("fail") {
@Override
public void execute(final ConsoleOutput out,
final ConsoleInput in,
final String... args) throws CommandRunException {
throw new CommandRunException("Failing command");
}
@Override
public String tip() {
return null;
}
@Override
protected String usageDetail() {
return null;
}
});
} catch (final InvalidCommandName e) {
fail("addition of command with valid id failed");
assertNotNull(e);
}
try {
cmd.execute(null, null, "id");
} catch (final CommandRunException e) {
fail("Unexpected exception when running mock command");
assertNotNull(e);
}
try {
cmd.execute(null, null, "fail");
fail("Fail command error should be re thrown");
} catch (final CommandRunException e) {
assertNotNull(e);
}
try {
cmd.execute(null, null);
} catch (final CommandRunException e) {
fail("Request for default command should execute default command");
assertNotNull(e);
}
}
/** Test method for
* {@link fr.bigeon.gclc.command.SubedCommand#executeSub(ConsoleOutput, ConsoleInput, String, String...)}. */
@Test
public final void testExecuteSub() {
final SubedCommand cmd = new SubedCommand("name");
final MockCommand mock = new MockCommand("id");
try {
cmd.add(mock);
cmd.add(new Command("fail") {
@Override
public void execute(final ConsoleOutput out,
final ConsoleInput in,
final String... args) throws CommandRunException {
throw new CommandRunException("Failing command");
}
@Override
public String tip() {
return null;
}
@Override
protected String usageDetail() {
return null;
}
});
} catch (final InvalidCommandName e) {
fail("addition of command with valid id failed");
assertNotNull(e);
}
try {
cmd.executeSub(null, null,"id");
} catch (final CommandRunException e) {
fail("Unexpected exception when running mock command");
assertNotNull(e);
}
try {
cmd.executeSub(null, null, "fail");
fail("Fail command error should be re thrown");
} catch (final CommandRunException e) {
assertNotNull(e);
}
}
/** Test method for
* {@link fr.bigeon.gclc.command.SubedCommand#get(java.lang.String)}. */
@Test
public final void testGet() {
final SubedCommand cmd = new SubedCommand("name");
assertNull(cmd.get("id"));
final MockCommand mock = new MockCommand("id");
try {
cmd.add(mock);
} catch (final 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(ConsoleOutput, String...)}. */
@Test
public final void testHelp() {
SubedCommand cmd = new SubedCommand("name");
ICommand mock = new MockCommand("id");
try {
cmd.add(mock);
} catch (final InvalidCommandName e) {
fail("addition of command with valid id failed");
assertNotNull(e);
}
try (PipedConsoleOutput manager = new PipedConsoleOutput()) {
cmd.help(manager);
assertEquals("\tid", manager.readNextLine());
cmd.help(manager, "id");
cmd.help(manager, "inexistent");
} catch (final IOException e) {
fail("Unexpected exception when running help");
assertNotNull(e);
}
cmd = new SubedCommand("name", mock);
try {
cmd.add(mock);
} catch (final InvalidCommandName e) {
fail("addition of command with valid id failed");
assertNotNull(e);
}
try (PipedConsoleOutput manager = new PipedConsoleOutput()) {
cmd.help(manager);
assertEquals("\tid", manager.readNextLine());
} catch (final IOException e) {
fail("Unexpected exception when running help");
assertNotNull(e);
}
mock = new ICommand() {
@Override
public void execute(final ConsoleOutput out, final ConsoleInput in,
final String... args) throws CommandRunException {
//
}
@Override
public String getCommandName() {
return "id";
}
@Override
public void help(final ConsoleOutput manager,
final String... args) throws IOException {
//
}
@Override
public String tip() {
return "tip";
}
};
cmd = new SubedCommand("name", mock);
try {
cmd.add(mock);
} catch (final InvalidCommandName e) {
fail("addition of command with valid id failed");
assertNotNull(e);
}
try (PipedConsoleOutput manager = new PipedConsoleOutput()) {
cmd.help(manager);
assertEquals("\ttip", manager.readNextLine());
assertEquals("\tid: tip", manager.readNextLine());
} catch (final IOException e) {
fail("Unexpected exception when running help");
assertNotNull(e);
}
}
/** 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#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());
}
}

View File

@@ -1,75 +1,107 @@
/*
* 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;
/**
* <p>
* 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());
}
}
/*
* 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;
/*-
* #%L
* Generic Command Ligne console
* %%
* Copyright (C) 2014 - 2018 bigeon.fr
* %%
* 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.
* #L%
*/
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import fr.bigeon.gclc.command.ICommand;
import fr.bigeon.gclc.command.base.MockCommand;
/**
* <p>
* TODO
*
* @author Emmanuel Bigeon
*
*/
public class CommandRunExceptionTest {
/**
* Test method for {@link fr.bigeon.gclc.exception.CommandRunException#getLocalizedMessage()}.
*/
@Test
public final void testGetLocalizedMessage() {
CommandRunException e;
final ICommand cmd = new MockCommand("name");
final String messageInner = "inner";
final String message = "message";
e = new CommandRunException(message,
new CommandRunException(messageInner));
assertEquals(message + ": " + messageInner, e.getLocalizedMessage());
e = new CommandRunException(message);
assertEquals(message, e.getLocalizedMessage());
}
}

View File

@@ -1,142 +1,177 @@
/*
* 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.ReadingRunnableTest.java
* Created on: Dec 6, 2016
*/
package fr.bigeon.gclc.manager;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.nio.charset.Charset;
import org.junit.Before;
import org.junit.Test;
/**
* <p>
* TODO
*
* @author Emmanuel Bigeon */
public class ReadingRunnableTest {
/**
*/
@Before
public void setUp() {}
/** Test method for
* {@link fr.bigeon.gclc.manager.ReadingRunnable#getMessage()}. */
@Test
public final void testGetMessage() {
final BufferedReader reader = null;
final ReadingRunnable runnable = new ReadingRunnable(reader);
runnable.setRunning(false);
try {
runnable.getMessage();
fail("reading from closed runnable");
} catch (final IOException e) {
// ok
}
}
/** Test method for
* {@link fr.bigeon.gclc.manager.ReadingRunnable#getWaitForDelivery(java.lang.String)}.
*
* @throws InterruptedException if the test failed
* @throws IOException if the test failed */
@Test
public final void testGetWaitForDelivery() throws InterruptedException,
IOException {
try (PipedOutputStream out = new PipedOutputStream();
InputStream piped = new PipedInputStream(out);
BufferedReader reader = new BufferedReader(
new InputStreamReader(piped, "UTF-8"))) {
final ReadingRunnable runnable = new ReadingRunnable(reader);
final Thread th0 = new Thread(runnable, "read");
th0.start();
final Thread th = runnable.getWaitForDelivery("msg");
out.write(Charset.forName("UTF-8")
.encode("msg" + System.lineSeparator()).array());
final Thread th2 = new Thread(new Runnable() {
@Override
public void run() {
try {
runnable.getMessage();
} catch (final IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, "get");
th2.start();
th.join();
assertFalse("Runnable should have consumed every message",
runnable.hasMessage());
runnable.setRunning(false);
out.close();
}
}
/** Test method for
* {@link fr.bigeon.gclc.manager.ReadingRunnable#hasMessage()}. */
@Test
public final void testHasMessage() {
final BufferedReader reader = null;
final ReadingRunnable runnable = new ReadingRunnable(reader);
runnable.setRunning(false);
try {
runnable.getMessage();
fail("reading from closed runnable");
} catch (final IOException e) {
// ok
}
}
}
/*
* 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.ReadingRunnableTest.java
* Created on: Dec 6, 2016
*/
package fr.bigeon.gclc.manager;
/*-
* #%L
* Generic Command Ligne console
* %%
* Copyright (C) 2014 - 2018 bigeon.fr
* %%
* 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.
* #L%
*/
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.nio.charset.Charset;
import org.junit.Before;
import org.junit.Test;
import fr.bigeon.gclc.utils.ReadingRunnable;
/**
* <p>
* TODO
*
* @author Emmanuel Bigeon */
public class ReadingRunnableTest {
/**
*/
@Before
public void setUp() {}
/** Test method for
* {@link fr.bigeon.gclc.utils.ReadingRunnable#getMessage()}. */
@Test
public final void testGetMessage() {
final BufferedReader reader = null;
final ReadingRunnable runnable = new ReadingRunnable(reader);
runnable.setRunning(false);
try {
runnable.getMessage();
fail("reading from closed runnable");
} catch (final IOException e) {
// ok
}
}
/** Test method for
* {@link fr.bigeon.gclc.utils.ReadingRunnable#getWaitForDelivery(java.lang.String)}.
*
* @throws InterruptedException if the test failed
* @throws IOException if the test failed */
@Test
public final void testGetWaitForDelivery() throws InterruptedException,
IOException {
try (PipedOutputStream out = new PipedOutputStream();
InputStream piped = new PipedInputStream(out);
BufferedReader reader = new BufferedReader(
new InputStreamReader(piped, "UTF-8"))) {
final ReadingRunnable runnable = new ReadingRunnable(reader);
final Thread th0 = new Thread(runnable, "read");
th0.start();
final Thread th = runnable.getWaitForDelivery("msg");
out.write(Charset.forName("UTF-8")
.encode("msg" + System.lineSeparator()).array());
final Thread th2 = new Thread(new Runnable() {
@Override
public void run() {
try {
runnable.getMessage();
} catch (final IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, "get");
th2.start();
th.join();
assertFalse("Runnable should have consumed every message",
runnable.hasMessage());
runnable.setRunning(false);
out.close();
}
}
/** Test method for
* {@link fr.bigeon.gclc.utils.ReadingRunnable#hasMessage()}. */
@Test
public final void testHasMessage() {
final BufferedReader reader = null;
final ReadingRunnable runnable = new ReadingRunnable(reader);
runnable.setRunning(false);
try {
runnable.getMessage();
fail("reading from closed runnable");
} catch (final IOException e) {
// ok
}
}
}

View File

@@ -1,146 +1,181 @@
/*
* 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.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;
/**
* <p>
* TODO
*
* @author Emmanuel Bigeon */
public class SystemConsoleManagerTest {
/** Test method for {@link fr.bigeon.gclc.manager.ConsoleInput#isClosed()}.
*
* @throws IOException if the test failed
* @throws InterruptedException if the test failed */
@Test
public final void testIsClosed() throws IOException, InterruptedException {
final PipedOutputStream outStream = new PipedOutputStream();
final InputStream in = new PipedInputStream(outStream);
final PrintStream out = new PrintStream(outStream);
final String test = "test";
final StreamConsoleInput manager = new StreamConsoleInput(System.out,
in, Charset.forName("UTF-8"));
final 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 (final IOException e) {
// ok
}
th.join();
}
/** Test method for {@link fr.bigeon.gclc.manager.ConsoleInput#prompt()}.
*
* @throws IOException if the test failed
* @throws InterruptedException if the test failed */
@Test
public final void testPrompt() throws IOException, InterruptedException {
final String test = "test";
try (PipedOutputStream outStream = new PipedOutputStream();
InputStream in = new PipedInputStream(outStream);
final PrintStream out = new PrintStream(outStream);
StreamConsoleInput manager = new StreamConsoleInput(System.out, in,
Charset.forName("UTF-8"))) {
final Thread th = new Thread(new Runnable() {
@SuppressWarnings("synthetic-access")
@Override
public void run() {
out.println(test);
}
});
th.start();
assertEquals(test, manager.prompt());
th.join();
}
}
/** Test method for
* {@link fr.bigeon.gclc.manager.ConsoleInput#setPrompt(java.lang.String)}.
*
* @throws IOException if the test failed */
@Test
public final void testSetPrompt() throws IOException {
try (PipedOutputStream outStream = new PipedOutputStream();
InputStream in = new PipedInputStream(outStream);
final PrintStream out = new PrintStream(outStream);
StreamConsoleInput manager = new StreamConsoleInput(System.out, in,
Charset.forName("UTF-8"))) {
final String prt = "++";
manager.setPrompt(prt);
assertEquals(prt, manager.getPrompt().apply());
}
}
}
/*
* 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;
/*-
* #%L
* Generic Command Ligne console
* %%
* Copyright (C) 2014 - 2018 bigeon.fr
* %%
* 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.
* #L%
*/
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.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;
import java.nio.charset.Charset;
import org.junit.Test;
import fr.bigeon.gclc.utils.StreamConsoleInput;
/**
* <p>
* TODO
*
* @author Emmanuel Bigeon */
public class SystemConsoleManagerTest {
/** Test method for {@link fr.bigeon.gclc.manager.ConsoleInput#isClosed()}.
*
* @throws IOException if the test failed
* @throws InterruptedException if the test failed */
@Test
public final void testIsClosed() throws IOException, InterruptedException {
final PipedOutputStream outStream = new PipedOutputStream();
final InputStream in = new PipedInputStream(outStream);
final PrintStream out = new PrintStream(outStream);
final String test = "test";
final StreamConsoleInput manager = new StreamConsoleInput(System.out,
in, Charset.forName("UTF-8"));
final 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 (final IOException e) {
// ok
}
th.join();
}
/** Test method for {@link fr.bigeon.gclc.manager.ConsoleInput#prompt()}.
*
* @throws IOException if the test failed
* @throws InterruptedException if the test failed */
@Test
public final void testPrompt() throws IOException, InterruptedException {
final String test = "test";
try (PipedOutputStream outStream = new PipedOutputStream();
InputStream in = new PipedInputStream(outStream);
final PrintStream out = new PrintStream(outStream);
StreamConsoleInput manager = new StreamConsoleInput(System.out, in,
Charset.forName("UTF-8"))) {
final Thread th = new Thread(new Runnable() {
@SuppressWarnings("synthetic-access")
@Override
public void run() {
out.println(test);
}
});
th.start();
assertEquals(test, manager.prompt());
th.join();
}
}
/** Test method for
* {@link fr.bigeon.gclc.manager.ConsoleInput#setPrompt(java.lang.String)}.
*
* @throws IOException if the test failed */
@Test
public final void testSetPrompt() throws IOException {
try (PipedOutputStream outStream = new PipedOutputStream();
InputStream in = new PipedInputStream(outStream);
final PrintStream out = new PrintStream(outStream);
StreamConsoleInput manager = new StreamConsoleInput(System.out, in,
Charset.forName("UTF-8"))) {
final String prt = "++";
manager.setPrompt(prt);
assertEquals(prt, manager.getPrompt().apply());
}
}
}

View File

@@ -1,67 +1,100 @@
/*
* 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;
/**
* <p>
* 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[] {}));
}
}
/*
* 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;
/*-
* #%L
* Generic Command Ligne console
* %%
* Copyright (C) 2014 - 2018 bigeon.fr
* %%
* 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.
* #L%
*/
import static org.junit.Assert.assertEquals;
import org.junit.Test;
/**
* <p>
* 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[] {}));
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,140 +1,174 @@
/*
* 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.tools.AOutputForwardRunnableTest.java
* Created on: Dec 3, 2016
*/
package fr.bigeon.gclc.tools;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import org.junit.Test;
import fr.bigeon.gclc.manager.PipedConsoleOutput;
/** <p>
* TODO
*
* @author Emmanuel Bigeon */
public class AOutputForwardRunnableTest {
/** <p>
* TODO
*
* @author Emmanuel Bigeon */
private final class AOutputForwardTestRunnable
extends AOutputForwardRunnable {
private int count = 2;
private String message;
private AOutputForwardTestRunnable(final PipedConsoleOutput manager) {
super(manager);
}
@Override
protected void forwardLine(final String m) {
// Do nothing
message = m;
synchronized (this) {
notify();
try {
wait();
} catch (final InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/** @return the message */
public String getMessage() {
if (message == null) {
synchronized (this) {
try {
wait();
} catch (final InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
final String m = message;
count--;
message = null;
synchronized (this) {
notify();
}
return m;
}
@Override
protected boolean isRunning() {
return count != 0;
}
}
/** Test method for
* {@link fr.bigeon.gclc.tools.AOutputForwardRunnable#run()}. */
@Test
public final void testRun() {
try (PipedConsoleOutput manager = new PipedConsoleOutput()) {
final AOutputForwardTestRunnable runnable = new AOutputForwardTestRunnable(
manager);
final Thread th = new Thread(runnable, "forward");
manager.println("before");
th.start();
assertEquals("before", runnable.getMessage());
synchronized (this) {
wait(3000);
}
manager.println("after");
assertEquals("after", runnable.getMessage());
synchronized (this) {
wait(3000);
}
th.join();
} catch (IOException | InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*
* 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.tools.AOutputForwardRunnableTest.java
* Created on: Dec 3, 2016
*/
package fr.bigeon.gclc.tools;
/*-
* #%L
* Generic Command Ligne console
* %%
* Copyright (C) 2014 - 2018 bigeon.fr
* %%
* 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.
* #L%
*/
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import org.junit.Test;
import fr.bigeon.gclc.utils.AOutputForwardRunnable;
import fr.bigeon.gclc.utils.PipedConsoleOutput;
/** <p>
* TODO
*
* @author Emmanuel Bigeon */
public class AOutputForwardRunnableTest {
/** <p>
* TODO
*
* @author Emmanuel Bigeon */
private final class AOutputForwardTestRunnable
extends AOutputForwardRunnable {
private int count = 2;
private String message;
private AOutputForwardTestRunnable(final PipedConsoleOutput manager) {
super(manager);
}
@Override
protected void forwardLine(final String m) {
// Do nothing
message = m;
synchronized (this) {
notify();
try {
wait();
} catch (final InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/** @return the message */
public String getMessage() {
if (message == null) {
synchronized (this) {
try {
wait();
} catch (final InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
final String m = message;
count--;
message = null;
synchronized (this) {
notify();
}
return m;
}
@Override
protected boolean isRunning() {
return count != 0;
}
}
/** Test method for
* {@link fr.bigeon.gclc.utils.AOutputForwardRunnable#run()}. */
@Test
public final void testRun() {
try (PipedConsoleOutput manager = new PipedConsoleOutput()) {
final AOutputForwardTestRunnable runnable = new AOutputForwardTestRunnable(
manager);
final Thread th = new Thread(runnable, "forward");
manager.println("before");
th.start();
assertEquals("before", runnable.getMessage());
synchronized (this) {
wait(3000);
}
manager.println("after");
assertEquals("after", runnable.getMessage());
synchronized (this) {
wait(3000);
}
th.join();
} catch (IOException | InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

View File

@@ -1,94 +1,127 @@
/*
* 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.tools.PrintUtilsTest.java
* Created on: Nov 19, 2016
*/
package fr.bigeon.gclc.tools;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import org.junit.Test;
/** <p>
* TODO
*
* @author Emmanuel Bigeon */
public class PrintUtilsTest {
/** Test method for
* {@link fr.bigeon.gclc.tools.PrintUtils#print(java.lang.String, int, boolean)}. */
@Test
public final void testPrint() {
String text = "Some text";
String longText = text + " that may be abbreviated";
assertEquals(text, PrintUtils.print(text, text.length(), true));
assertEquals(text, PrintUtils.print(text, text.length(), false));
assertEquals(text + " ",
PrintUtils.print(text, text.length() + 3, false));
assertEquals(text + " ",
PrintUtils.print(text, text.length() + 3, true));
assertEquals(text, PrintUtils.print(longText, text.length(), false));
assertEquals(text + "...",
PrintUtils.print(longText, text.length() + 3, true));
}
/** Test method for
* {@link fr.bigeon.gclc.tools.PrintUtils#wrap(java.lang.String, int)}. */
@Test
public final void testWrap() {
String[] line = {"A text separated", "on several lines", "with cuts at",
"whitespace", "characters"};
String text = line[0] + " " + line[1] + " " + line[2] + " " + line[3] +
" " + line[4];
assertEquals(Arrays.asList(line),
PrintUtils.wrap(text, line[0].length() + 1));
assertEquals(Arrays.asList(line),
PrintUtils.wrap(text, line[0].length()));
// test with split word
line = new String[] {"A text separated", "on several lines", "",
"with cuts at", "whitespacecharac", "ters"};
text = line[0] + " " + line[1] + System.lineSeparator() + line[3] +
" " + line[4] + line[5];
assertEquals(Arrays.asList(line),
PrintUtils.wrap(text, line[0].length()));
assertEquals(Arrays.asList(""),
PrintUtils.wrap("", line[0].length()));
}
}
/*
* 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.tools.PrintUtilsTest.java
* Created on: Nov 19, 2016
*/
package fr.bigeon.gclc.tools;
/*-
* #%L
* Generic Command Ligne console
* %%
* Copyright (C) 2014 - 2018 bigeon.fr
* %%
* 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.
* #L%
*/
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import org.junit.Test;
/** <p>
* TODO
*
* @author Emmanuel Bigeon */
public class PrintUtilsTest {
/** Test method for
* {@link fr.bigeon.gclc.tools.PrintUtils#print(java.lang.String, int, boolean)}. */
@Test
public final void testPrint() {
String text = "Some text";
String longText = text + " that may be abbreviated";
assertEquals(text, PrintUtils.print(text, text.length(), true));
assertEquals(text, PrintUtils.print(text, text.length(), false));
assertEquals(text + " ",
PrintUtils.print(text, text.length() + 3, false));
assertEquals(text + " ",
PrintUtils.print(text, text.length() + 3, true));
assertEquals(text, PrintUtils.print(longText, text.length(), false));
assertEquals(text + "...",
PrintUtils.print(longText, text.length() + 3, true));
}
/** Test method for
* {@link fr.bigeon.gclc.tools.PrintUtils#wrap(java.lang.String, int)}. */
@Test
public final void testWrap() {
String[] line = {"A text separated", "on several lines", "with cuts at",
"whitespace", "characters"};
String text = line[0] + " " + line[1] + " " + line[2] + " " + line[3] +
" " + line[4];
assertEquals(Arrays.asList(line),
PrintUtils.wrap(text, line[0].length() + 1));
assertEquals(Arrays.asList(line),
PrintUtils.wrap(text, line[0].length()));
// test with split word
line = new String[] {"A text separated", "on several lines", "",
"with cuts at", "whitespacecharac", "ters"};
text = line[0] + " " + line[1] + System.lineSeparator() + line[3] +
" " + line[4] + line[5];
assertEquals(Arrays.asList(line),
PrintUtils.wrap(text, line[0].length()));
assertEquals(Arrays.asList(""),
PrintUtils.wrap("", line[0].length()));
}
}

View File

@@ -0,0 +1,33 @@
###
# #%L
# Generic Command Ligne console
# %%
# Copyright (C) 2014 - 2018 bigeon.fr
# %%
# 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.
# #L%
###

View File

@@ -1 +1,34 @@
# test only comment
###
# #%L
# Generic Command Ligne console
# %%
# Copyright (C) 2014 - 2018 Bigeon
# %%
# 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.
# #L%
###
# test only comment

View File

@@ -1 +1,34 @@
test
###
# #%L
# Generic Command Ligne console
# %%
# Copyright (C) 2014 - 2018 Bigeon
# %%
# 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.
# #L%
###
test

View File

@@ -1,7 +1,40 @@
# Test file with commands
test
# and comments
long
# interwined
# with empty line too
###
# #%L
# Generic Command Ligne console
# %%
# Copyright (C) 2014 - 2018 Bigeon
# %%
# 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.
# #L%
###
# Test file with commands
test
# and comments
long
# interwined
# with empty line too

View File

@@ -1,2 +1,35 @@
# A script with arguments
{0}
###
# #%L
# Generic Command Ligne console
# %%
# Copyright (C) 2014 - 2018 Bigeon
# %%
# 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.
# #L%
###
# A script with arguments
{0}

View File

@@ -1,2 +1,35 @@
# a script invoking a failing command
failingCmd
###
# #%L
# Generic Command Ligne console
# %%
# Copyright (C) 2014 - 2018 Bigeon
# %%
# 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.
# #L%
###
# a script invoking a failing command
failingCmd

View File

@@ -1,2 +1,35 @@
# a script with invalid commands
invalid
###
# #%L
# Generic Command Ligne console
# %%
# Copyright (C) 2014 - 2018 Bigeon
# %%
# 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.
# #L%
###
# a script with invalid commands
invalid

View File

@@ -1,2 +1,35 @@
# a script with invalid commands
test "po"m
###
# #%L
# Generic Command Ligne console
# %%
# Copyright (C) 2014 - 2018 Bigeon
# %%
# 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.
# #L%
###
# a script with invalid commands
test "po"m

View File

@@ -1,3 +1,36 @@
# a script with space in begining
test
should never be reached
###
# #%L
# Generic Command Ligne console
# %%
# Copyright (C) 2014 - 2018 Bigeon
# %%
# 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.
# #L%
###
# a script with space in begining
test
should never be reached