Compare commits
6 Commits
gclc-1.2.6
...
gclc-1.3.0
| Author | SHA1 | Date | |
|---|---|---|---|
| 0cf54c4837 | |||
| c1692019a6 | |||
| 72362936be | |||
| 65b6be8283 | |||
| fd8dde32f1 | |||
| fff9f4ea74 |
@@ -87,7 +87,7 @@ of Emmanuel Bigeon. -->
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>fr.bigeon</groupId>
|
<groupId>fr.bigeon</groupId>
|
||||||
<artifactId>gclc</artifactId>
|
<artifactId>gclc</artifactId>
|
||||||
<version>1.2.5</version>
|
<version>1.2.6</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>fr.bigeon</groupId>
|
<groupId>fr.bigeon</groupId>
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ import fr.bigeon.gclc.ConsoleApplication;
|
|||||||
*
|
*
|
||||||
* @author Emmanuel Bigeon */
|
* @author Emmanuel Bigeon */
|
||||||
public class ConsoleRunnable implements Runnable {
|
public class ConsoleRunnable implements Runnable {
|
||||||
|
|
||||||
/** The actual application */
|
/** The actual application */
|
||||||
private final ConsoleApplication app;
|
private final ConsoleApplication app;
|
||||||
/** The synchronization object */
|
/** The synchronization object */
|
||||||
@@ -74,4 +74,9 @@ public class ConsoleRunnable implements Runnable {
|
|||||||
app.exit();
|
app.exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @return if the application is running */
|
||||||
|
public boolean isRunning() {
|
||||||
|
return app.isRunning();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -110,7 +110,7 @@ public class SocketConsoleApplicationShell implements Runnable {
|
|||||||
/** The application shutdown string */
|
/** The application shutdown string */
|
||||||
private final String applicationShutdown;
|
private final String applicationShutdown;
|
||||||
/** The charset for the communication. */
|
/** The charset for the communication. */
|
||||||
private Charset charset;
|
private final Charset charset;
|
||||||
|
|
||||||
/** Create a socket application shell which will listen on the given port
|
/** Create a socket application shell which will listen on the given port
|
||||||
* and close session upon the provided string reception by client
|
* and close session upon the provided string reception by client
|
||||||
@@ -156,8 +156,7 @@ public class SocketConsoleApplicationShell implements Runnable {
|
|||||||
// Create the streams
|
// Create the streams
|
||||||
try (PipedOutputStream outStream = new PipedOutputStream();
|
try (PipedOutputStream outStream = new PipedOutputStream();
|
||||||
BufferedWriter writer = new BufferedWriter(
|
BufferedWriter writer = new BufferedWriter(
|
||||||
new OutputStreamWriter(outStream,
|
new OutputStreamWriter(outStream, charset));
|
||||||
charset));
|
|
||||||
InputStreamReader isr = new InputStreamReader(consoleInput,
|
InputStreamReader isr = new InputStreamReader(consoleInput,
|
||||||
charset);
|
charset);
|
||||||
BufferedReader inBuf = new BufferedReader(isr)) {
|
BufferedReader inBuf = new BufferedReader(isr)) {
|
||||||
@@ -187,24 +186,24 @@ public class SocketConsoleApplicationShell implements Runnable {
|
|||||||
Thread appThOld = null;
|
Thread appThOld = null;
|
||||||
Thread appThNext = new Thread(runnable);
|
Thread appThNext = new Thread(runnable);
|
||||||
while (running) {
|
while (running) {
|
||||||
|
LOGGER.info("Opening client"); //$NON-NLS-1$
|
||||||
try (Socket clientSocket = serverSocket.accept();
|
try (Socket clientSocket = serverSocket.accept();
|
||||||
PrintWriter out = new PrintWriter(
|
PrintWriter out = new PrintWriter(new OutputStreamWriter(
|
||||||
new OutputStreamWriter(clientSocket.getOutputStream(),
|
clientSocket.getOutputStream(), charset), true);
|
||||||
charset),
|
|
||||||
true);
|
|
||||||
InputStreamReader isr = new InputStreamReader(
|
InputStreamReader isr = new InputStreamReader(
|
||||||
clientSocket.getInputStream(),
|
clientSocket.getInputStream(), charset);
|
||||||
charset);
|
|
||||||
BufferedReader in = new BufferedReader(isr);) {
|
BufferedReader in = new BufferedReader(isr);) {
|
||||||
// this is not threaded to avoid several clients at the same
|
// this is not threaded to avoid several clients at the same
|
||||||
// time
|
// time
|
||||||
consoleManager.setOutput(out);
|
consoleManager.setOutput(out);
|
||||||
// Initiate application
|
// Initiate application
|
||||||
if (appThOld == null || !appThOld.isAlive()) {
|
|
||||||
|
if (appThOld == null || !appThOld.isAlive() ||
|
||||||
|
!runnable.isRunning()) {
|
||||||
appThNext.start();
|
appThNext.start();
|
||||||
// Prepare next start
|
// Prepare next start
|
||||||
appThOld = appThNext;
|
appThOld = appThNext;
|
||||||
appThNext = new Thread(runnable);
|
appThNext = new Thread(runnable, "gclc-ctrl"); //$NON-NLS-1$
|
||||||
} else {
|
} else {
|
||||||
out.println("Reconnected"); //$NON-NLS-1$
|
out.println("Reconnected"); //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
@@ -215,7 +214,9 @@ public class SocketConsoleApplicationShell implements Runnable {
|
|||||||
"Socket closed with exception (probably due to server interruption)", //$NON-NLS-1$
|
"Socket closed with exception (probably due to server interruption)", //$NON-NLS-1$
|
||||||
e);
|
e);
|
||||||
}
|
}
|
||||||
|
LOGGER.info("Closing client"); //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
|
LOGGER.info("Out client"); //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
|
|
||||||
/** active communication between server and client
|
/** active communication between server and client
|
||||||
|
|||||||
@@ -38,6 +38,9 @@
|
|||||||
*/
|
*/
|
||||||
package fr.bigeon.gclc.socket;
|
package fr.bigeon.gclc.socket;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
@@ -60,7 +63,8 @@ public class SocketConsoleApplicationTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void integrationTest() {
|
public void integrationTest() {
|
||||||
Thread server = TestServer.startServer(false);
|
Thread server;
|
||||||
|
server = TestServer.startServer("bye");
|
||||||
try {
|
try {
|
||||||
Thread.sleep(100);
|
Thread.sleep(100);
|
||||||
} catch (InterruptedException e1) {
|
} catch (InterruptedException e1) {
|
||||||
@@ -70,27 +74,89 @@ public class SocketConsoleApplicationTest {
|
|||||||
final int portNumber = 3300;
|
final int portNumber = 3300;
|
||||||
|
|
||||||
try (Socket kkSocket = new Socket(hostName, portNumber);
|
try (Socket kkSocket = new Socket(hostName, portNumber);
|
||||||
PrintWriter out = new PrintWriter(kkSocket.getOutputStream(),
|
PrintWriter out = new PrintWriter(kkSocket.getOutputStream(),
|
||||||
true);
|
true);
|
||||||
BufferedReader in = new BufferedReader(
|
BufferedReader in = new BufferedReader(
|
||||||
new InputStreamReader(kkSocket.getInputStream()));) {
|
new InputStreamReader(kkSocket.getInputStream()));) {
|
||||||
|
|
||||||
String fromServer;
|
String fromServer;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
String[] cmds = {"help", "toto", "test", "close"};
|
String[] cmds = {"help", "toto", "test", "bye"};
|
||||||
while ((fromServer = in.readLine()) != null) {
|
while ((fromServer = in.readLine()) != null) {
|
||||||
System.out.println("Server: \n" + ENCODER.decode(fromServer));
|
// System.out.println("Server: \n" + ENCODER.decode(fromServer));
|
||||||
if (fromServer.equals("Bye.")) {
|
if (fromServer.equals("Bye.")) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
final String fromUser = cmds[i];
|
final String fromUser = cmds[i];
|
||||||
if (fromUser != null) {
|
if (fromUser != null) {
|
||||||
System.out.println("Client: " + fromUser);
|
// System.out.println("Client: " + fromUser);
|
||||||
out.println(fromUser);
|
out.println(fromUser);
|
||||||
}
|
}
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
assertEquals(4, i);
|
||||||
|
} catch (final IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
try (Socket kkSocket = new Socket(hostName, portNumber);
|
||||||
|
PrintWriter out = new PrintWriter(kkSocket.getOutputStream(),
|
||||||
|
true);
|
||||||
|
BufferedReader in = new BufferedReader(
|
||||||
|
new InputStreamReader(kkSocket.getInputStream()));) {
|
||||||
|
|
||||||
|
String fromServer;
|
||||||
|
int i = 0;
|
||||||
|
String[] cmds = {"help", "toto", "test",
|
||||||
|
ConsoleTestApplication.EXIT};
|
||||||
|
while ((fromServer = in.readLine()) != null) {
|
||||||
|
// System.out.println("Server: \n" + ENCODER.decode(fromServer));
|
||||||
|
if (fromServer.equals("Bye.")) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
final String fromUser = cmds[i];
|
||||||
|
if (fromUser != null) {
|
||||||
|
// System.out.println("Client: " + fromUser);
|
||||||
|
out.println(fromUser);
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
assertEquals(4, i);
|
||||||
|
} catch (final IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
Thread.sleep(100);
|
||||||
|
} catch (InterruptedException e1) {
|
||||||
|
e1.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
try (Socket kkSocket = new Socket(hostName, portNumber);
|
||||||
|
PrintWriter out = new PrintWriter(kkSocket.getOutputStream(),
|
||||||
|
true);
|
||||||
|
BufferedReader in = new BufferedReader(
|
||||||
|
new InputStreamReader(kkSocket.getInputStream()));) {
|
||||||
|
|
||||||
|
String fromServer;
|
||||||
|
int i = 0;
|
||||||
|
String[] cmds = {"help", "toto", "test",
|
||||||
|
ConsoleTestApplication.EXIT};
|
||||||
|
while ((fromServer = in.readLine()) != null) {
|
||||||
|
// System.out.println("Server: \n" + ENCODER.decode(fromServer));
|
||||||
|
if (fromServer.equals("Bye.")) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
final String fromUser = cmds[i];
|
||||||
|
if (fromUser != null) {
|
||||||
|
// System.out.println("Client: " + fromUser);
|
||||||
|
out.println(fromUser);
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
assertEquals(4, i);
|
||||||
} catch (final IOException e) {
|
} catch (final IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@@ -108,31 +174,39 @@ public class SocketConsoleApplicationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try (Socket kkSocket = new Socket(hostName, portNumber);
|
try (Socket kkSocket = new Socket(hostName, portNumber);
|
||||||
PrintWriter out = new PrintWriter(kkSocket.getOutputStream(),
|
PrintWriter out = new PrintWriter(kkSocket.getOutputStream(),
|
||||||
true);
|
true);
|
||||||
BufferedReader in = new BufferedReader(
|
BufferedReader in = new BufferedReader(
|
||||||
new InputStreamReader(kkSocket.getInputStream()));) {
|
new InputStreamReader(kkSocket.getInputStream()));) {
|
||||||
|
|
||||||
String fromServer;
|
String fromServer;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
String[] cmds = {"help", "test", "close"};
|
String[] cmds = {"help", "test", "close"};
|
||||||
while ((fromServer = in.readLine()) != null) {
|
while ((fromServer = in.readLine()) != null) {
|
||||||
// System.out.println("Server: \n" + ENCODER.decode(fromServer));
|
assertTrue(i < 2);
|
||||||
|
System.out.println("Server: \n" + ENCODER.decode(fromServer));
|
||||||
if (fromServer.equals("Bye.")) {
|
if (fromServer.equals("Bye.")) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
final String fromUser = cmds[i];
|
final String fromUser = cmds[i];
|
||||||
if (fromUser != null) {
|
if (fromUser != null) {
|
||||||
// System.out.println("Client: " + fromUser);
|
System.out.println("Client: " + fromUser);
|
||||||
out.println(fromUser);
|
out.println(fromUser);
|
||||||
}
|
}
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
assertEquals(2, i);
|
||||||
} catch (final IOException e) {
|
} catch (final IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
Thread srv = TestServer.getServer();
|
||||||
TestServer.closeServer();
|
TestServer.closeServer();
|
||||||
|
try {
|
||||||
|
srv.join();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -85,6 +85,18 @@ public class TestServer {
|
|||||||
return getServer();
|
return getServer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Thread startServer(String closeConnection) {
|
||||||
|
if (SHELL == null) {
|
||||||
|
SHELL = new SocketConsoleApplicationShell(3300, closeConnection,
|
||||||
|
ConsoleTestApplication.EXIT, Charset.forName("UTF-8"));
|
||||||
|
final ConsoleTestApplication app = new ConsoleTestApplication(
|
||||||
|
SHELL.getConsoleManager());
|
||||||
|
SHELL.setApplication(app);
|
||||||
|
server = null;
|
||||||
|
}
|
||||||
|
return getServer();
|
||||||
|
}
|
||||||
|
|
||||||
public static void closeServer() {
|
public static void closeServer() {
|
||||||
SHELL.stop();
|
SHELL.stop();
|
||||||
SHELL = null;
|
SHELL = null;
|
||||||
|
|||||||
@@ -53,7 +53,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>fr.bigeon</groupId>
|
<groupId>fr.bigeon</groupId>
|
||||||
<artifactId>gclc</artifactId>
|
<artifactId>gclc</artifactId>
|
||||||
<version>1.2.3</version>
|
<version>1.2.6</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>fr.bigeon</groupId>
|
<groupId>fr.bigeon</groupId>
|
||||||
|
|||||||
@@ -40,13 +40,13 @@ package fr.bigeon.gclc.swt;
|
|||||||
|
|
||||||
import fr.bigeon.gclc.manager.ConsoleManager;
|
import fr.bigeon.gclc.manager.ConsoleManager;
|
||||||
|
|
||||||
/**
|
/** This class represents an object used to send commands to a console
|
||||||
|
* application.
|
||||||
* <p>
|
* <p>
|
||||||
* TODO
|
* The sending of command is done in two phases. Define the input through get
|
||||||
|
* and set, and then validate the input.
|
||||||
*
|
*
|
||||||
* @author Emmanuel Bigeon
|
* @author Emmanuel Bigeon */
|
||||||
*
|
|
||||||
*/
|
|
||||||
public interface ConsoleDelayIO extends ConsoleManager {
|
public interface ConsoleDelayIO extends ConsoleManager {
|
||||||
/** Actually send the input as the prompt next input. */
|
/** Actually send the input as the prompt next input. */
|
||||||
void validateInput();
|
void validateInput();
|
||||||
|
|||||||
@@ -23,7 +23,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>fr.bigeon</groupId>
|
<groupId>fr.bigeon</groupId>
|
||||||
<artifactId>gclc</artifactId>
|
<artifactId>gclc</artifactId>
|
||||||
<version>1.2.5</version>
|
<version>1.2.6</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<name>GCLC system command</name>
|
<name>GCLC system command</name>
|
||||||
|
|||||||
@@ -35,7 +35,7 @@
|
|||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>gclc</artifactId>
|
<artifactId>gclc</artifactId>
|
||||||
<version>1.2.6</version>
|
<version>1.3.0</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
<url>http://www.bigeon.fr/emmanuel</url>
|
<url>http://www.bigeon.fr/emmanuel</url>
|
||||||
<properties>
|
<properties>
|
||||||
@@ -83,6 +83,6 @@
|
|||||||
<scm>
|
<scm>
|
||||||
|
|
||||||
<developerConnection>scm:git:gogs@git.code.bigeon.net:emmanuel/gclc.git</developerConnection>
|
<developerConnection>scm:git:gogs@git.code.bigeon.net:emmanuel/gclc.git</developerConnection>
|
||||||
<tag>gclc-1.2.6</tag>
|
<tag>gclc-1.3.0</tag>
|
||||||
</scm>
|
</scm>
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@@ -37,14 +37,13 @@
|
|||||||
package fr.bigeon.gclc;
|
package fr.bigeon.gclc;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InterruptedIOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import fr.bigeon.gclc.command.ExitCommand;
|
|
||||||
import fr.bigeon.gclc.command.HelpExecutor;
|
|
||||||
import fr.bigeon.gclc.command.ICommand;
|
import fr.bigeon.gclc.command.ICommand;
|
||||||
import fr.bigeon.gclc.command.ICommandProvider;
|
import fr.bigeon.gclc.command.ICommandProvider;
|
||||||
import fr.bigeon.gclc.command.SubedCommand;
|
import fr.bigeon.gclc.command.SubedCommand;
|
||||||
@@ -54,7 +53,6 @@ import fr.bigeon.gclc.exception.CommandRunExceptionType;
|
|||||||
import fr.bigeon.gclc.exception.InvalidCommandName;
|
import fr.bigeon.gclc.exception.InvalidCommandName;
|
||||||
import fr.bigeon.gclc.i18n.Messages;
|
import fr.bigeon.gclc.i18n.Messages;
|
||||||
import fr.bigeon.gclc.manager.ConsoleManager;
|
import fr.bigeon.gclc.manager.ConsoleManager;
|
||||||
import fr.bigeon.gclc.manager.SystemConsoleManager;
|
|
||||||
|
|
||||||
/** <p>
|
/** <p>
|
||||||
* A {@link ConsoleApplication} is an application that require the user to input
|
* A {@link ConsoleApplication} is an application that require the user to input
|
||||||
@@ -63,7 +61,8 @@ import fr.bigeon.gclc.manager.SystemConsoleManager;
|
|||||||
* A typical use case is the following:
|
* A typical use case is the following:
|
||||||
*
|
*
|
||||||
* <pre>
|
* <pre>
|
||||||
* {@link ConsoleApplication} app = new {@link ConsoleApplication#ConsoleApplication(String, String, String) ConsoleApplication("exit", "welcome", "see you latter")};
|
* {@link ConsoleManager} manager = new {@link fr.bigeon.gclc.manager.SystemConsoleManager#SystemConsoleManager()}
|
||||||
|
* {@link ConsoleApplication} app = new {@link ConsoleApplication#ConsoleApplication(ConsoleManager, String, String) ConsoleApplication(manager, "welcome", "see you latter")};
|
||||||
* app.{@link ConsoleApplication#add(ICommand) add}("my_command", new {@link ICommand MyCommand()});
|
* app.{@link ConsoleApplication#add(ICommand) add}("my_command", new {@link ICommand MyCommand()});
|
||||||
* app.{@link ConsoleApplication#start start}();
|
* app.{@link ConsoleApplication#start start}();
|
||||||
* </pre>
|
* </pre>
|
||||||
@@ -103,66 +102,18 @@ public class ConsoleApplication implements ICommandProvider {
|
|||||||
root = new SubedCommand(""); //$NON-NLS-1$
|
root = new SubedCommand(""); //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @param manager the manager
|
|
||||||
* @param exit the keyword for the exit command of this application
|
|
||||||
* @param welcome the header message to display on launch of this
|
|
||||||
* application
|
|
||||||
* @param goodbye the message to display on exit
|
|
||||||
* @throws InvalidCommandName if the exit command name is invalid
|
|
||||||
* @deprecated since 1.2.0, use the {@link #add(ICommand)} method to add the
|
|
||||||
* exit command */
|
|
||||||
@Deprecated
|
|
||||||
public ConsoleApplication(ConsoleManager manager, String exit,
|
|
||||||
String welcome, String goodbye) throws InvalidCommandName {
|
|
||||||
this(manager, welcome, goodbye);
|
|
||||||
root.add(new ExitCommand(exit, this));
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @param exit the keyword for the exit command of this application
|
|
||||||
* @param welcome the header message to display on launch of this
|
|
||||||
* application
|
|
||||||
* @param goodbye the message to display on exit
|
|
||||||
* @throws InvalidCommandName if the exit command name is invalid
|
|
||||||
* @deprecated since 1.2.0, use the {@link #add(ICommand)} method to add the
|
|
||||||
* command and create this console using the
|
|
||||||
* {@link #ConsoleApplication(ConsoleManager, String, String)}
|
|
||||||
* method with a {@link SystemConsoleManager} as argument */
|
|
||||||
@Deprecated
|
|
||||||
public ConsoleApplication(String exit, String welcome,
|
|
||||||
String goodbye) throws InvalidCommandName {
|
|
||||||
this(new SystemConsoleManager(), welcome, goodbye);
|
|
||||||
root.add(new ExitCommand(exit, this));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final boolean add(ICommand cmd) throws InvalidCommandName {
|
public final boolean add(ICommand cmd) throws InvalidCommandName {
|
||||||
return root.add(cmd);
|
return root.add(cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Adds help command on the given key
|
/** @param listener the command listener */
|
||||||
*
|
|
||||||
* @param cmd the handle for help
|
|
||||||
* @return if the help command was added
|
|
||||||
* @throws InvalidCommandName if the help command was not valid
|
|
||||||
* @deprecated since 1.2.0 use the {@link #add(ICommand)} with a
|
|
||||||
* {@link HelpExecutor} instead */
|
|
||||||
@Deprecated
|
|
||||||
public final boolean addHelpCommand(String cmd) throws InvalidCommandName {
|
|
||||||
return root.add(new HelpExecutor(cmd, manager, root));
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Add a command request listener.
|
|
||||||
* <p>
|
|
||||||
* A listener can listen several times to the same application.
|
|
||||||
*
|
|
||||||
* @param listener the listener to add. */
|
|
||||||
public final void addListener(CommandRequestListener listener) {
|
public final void addListener(CommandRequestListener listener) {
|
||||||
listeners.add(listener);
|
listeners.add(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
* @see
|
* @see fr.bigeon.gclc.command.ICommandProvider#executeSub(java.lang.String,
|
||||||
* fr.bigeon.gclc.command.ICommandProvider#executeSub(java.lang.String,
|
|
||||||
* java.lang.String[]) */
|
* java.lang.String[]) */
|
||||||
@Override
|
@Override
|
||||||
public final void executeSub(String command,
|
public final void executeSub(String command,
|
||||||
@@ -170,10 +121,11 @@ public class ConsoleApplication implements ICommandProvider {
|
|||||||
root.executeSub(command, args);
|
root.executeSub(command, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Exit this running application before next command prompt */
|
/** Signify to the application that no command should be inputed anymore */
|
||||||
public final void exit() {
|
public final void exit() {
|
||||||
LOGGER.fine("Request exiting application..."); //$NON-NLS-1$
|
LOGGER.fine("Request exiting application..."); //$NON-NLS-1$
|
||||||
running = false;
|
running = false;
|
||||||
|
manager.interruptPrompt();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
@@ -188,8 +140,8 @@ public class ConsoleApplication implements ICommandProvider {
|
|||||||
return manager;
|
return manager;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @param cmd the command to interpret
|
/** @param cmd the command
|
||||||
* @throws IOException if the manager was closed */
|
* @throws IOException if the command could not be parsed */
|
||||||
public final void interpretCommand(String cmd) throws IOException {
|
public final void interpretCommand(String cmd) throws IOException {
|
||||||
List<String> args;
|
List<String> args;
|
||||||
try {
|
try {
|
||||||
@@ -215,7 +167,7 @@ public class ConsoleApplication implements ICommandProvider {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @param listener the listener to remove (once) */
|
/** @param listener the command listener to remove */
|
||||||
public final void removeListener(CommandRequestListener listener) {
|
public final void removeListener(CommandRequestListener listener) {
|
||||||
for (int i = 0; i < listeners.size(); i++) {
|
for (int i = 0; i < listeners.size(); i++) {
|
||||||
if (listeners.get(i) == listener) {
|
if (listeners.get(i) == listener) {
|
||||||
@@ -225,28 +177,57 @@ public class ConsoleApplication implements ICommandProvider {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Launches the prompting application */
|
/** Start the application */
|
||||||
public final void start() {
|
public final void start() {
|
||||||
try {
|
try {
|
||||||
running = true;
|
running = true;
|
||||||
if (header != null) {
|
if (header != null) {
|
||||||
manager.println(header);
|
manager.println(header);
|
||||||
}
|
}
|
||||||
do {
|
} catch (IOException e) {
|
||||||
final String cmd = manager.prompt();
|
// The manager was closed
|
||||||
if (cmd == null || cmd.isEmpty()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
for (final CommandRequestListener listener : listeners) {
|
|
||||||
listener.commandRequest(cmd);
|
|
||||||
}
|
|
||||||
interpretCommand(cmd);
|
|
||||||
} while (running);
|
|
||||||
if (footer != null) {
|
|
||||||
manager.println(footer);
|
|
||||||
}
|
|
||||||
running = false;
|
running = false;
|
||||||
LOGGER.fine("Exiting application."); //$NON-NLS-1$
|
LOGGER.log(Level.WARNING,
|
||||||
|
"The console manager was closed. Closing the application as no one can reach it.", //$NON-NLS-1$
|
||||||
|
e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
do {
|
||||||
|
runLoop();
|
||||||
|
} while (running);
|
||||||
|
if (footer != null) {
|
||||||
|
try {
|
||||||
|
manager.println(footer);
|
||||||
|
} catch (IOException e) {
|
||||||
|
// The manager was closed
|
||||||
|
running = false;
|
||||||
|
LOGGER.log(Level.WARNING,
|
||||||
|
"The console manager was closed.", //$NON-NLS-1$
|
||||||
|
e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
running = false;
|
||||||
|
LOGGER.fine("Exiting application."); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The running loop content.
|
||||||
|
* <p>
|
||||||
|
* This consisting in getting the command, executing it and exiting
|
||||||
|
* (restarting the loop). */
|
||||||
|
private void runLoop() {
|
||||||
|
try {
|
||||||
|
final String cmd = manager.prompt();
|
||||||
|
if (cmd == null || cmd.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (final CommandRequestListener listener : listeners) {
|
||||||
|
listener.commandRequest(cmd);
|
||||||
|
}
|
||||||
|
interpretCommand(cmd);
|
||||||
|
} catch (InterruptedIOException e) {
|
||||||
|
LOGGER.log(Level.INFO,
|
||||||
|
"Prompt interrupted. It is likely the application is closing.", //$NON-NLS-1$
|
||||||
|
e);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// The manager was closed
|
// The manager was closed
|
||||||
running = false;
|
running = false;
|
||||||
@@ -256,7 +237,7 @@ public class ConsoleApplication implements ICommandProvider {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @return if the application is running */
|
/** @return the running status */
|
||||||
public boolean isRunning() {
|
public boolean isRunning() {
|
||||||
return running;
|
return running;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -154,8 +154,9 @@ public abstract class ParametrizedCommand extends Command {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @param parameters the command parameters */
|
/** @param parameters the command parameters
|
||||||
protected abstract void doExecute(CommandParameters parameters);
|
* @throws CommandRunException if the command failed */
|
||||||
|
protected abstract void doExecute(CommandParameters parameters) throws CommandRunException;
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
* @see fr.bigeon.gclc.command.Command#execute(java.lang.String[]) */
|
* @see fr.bigeon.gclc.command.Command#execute(java.lang.String[]) */
|
||||||
|
|||||||
@@ -39,6 +39,7 @@
|
|||||||
package fr.bigeon.gclc.manager;
|
package fr.bigeon.gclc.manager;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InterruptedIOException;
|
||||||
|
|
||||||
/** <p>
|
/** <p>
|
||||||
* A console manager is in charge of the basic prompts and prints on a console.
|
* A console manager is in charge of the basic prompts and prints on a console.
|
||||||
@@ -69,14 +70,14 @@ public interface ConsoleManager {
|
|||||||
void println(String message) throws IOException;
|
void println(String message) throws IOException;
|
||||||
|
|
||||||
/** @return the user inputed string
|
/** @return the user inputed string
|
||||||
* @throws IOException if the manager is closed or could not read the
|
* @throws IOException if the manager is closed or could not read the prompt
|
||||||
* prompt */
|
* @throws InterruptedIOException if the prompt was interrupted */
|
||||||
String prompt() throws IOException;
|
String prompt() throws IOException;
|
||||||
|
|
||||||
/** @param message the message to prompt the user
|
/** @param message the message to prompt the user
|
||||||
* @return the user inputed string
|
* @return the user inputed string
|
||||||
* @throws IOException if the manager is closed or could not read the
|
* @throws IOException if the manager is closed or could not read the prompt
|
||||||
* prompt */
|
* @throws InterruptedIOException if the prompt was interrupted */
|
||||||
String prompt(String message) throws IOException;
|
String prompt(String message) throws IOException;
|
||||||
|
|
||||||
/** <p>
|
/** <p>
|
||||||
@@ -92,4 +93,8 @@ public interface ConsoleManager {
|
|||||||
|
|
||||||
/** @return if the manager is closed. */
|
/** @return if the manager is closed. */
|
||||||
boolean isClosed();
|
boolean isClosed();
|
||||||
|
|
||||||
|
/** Indicate to the manager that is should interrompt the prompting, if
|
||||||
|
* possible. */
|
||||||
|
void interruptPrompt();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,7 +36,7 @@
|
|||||||
* gclc-test:fr.bigeon.gclc.test.TestConsoleManager.java
|
* gclc-test:fr.bigeon.gclc.test.TestConsoleManager.java
|
||||||
* Created on: Nov 18, 2016
|
* Created on: Nov 18, 2016
|
||||||
*/
|
*/
|
||||||
package fr.bigeon.gclc.test.utils;
|
package fr.bigeon.gclc.manager;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -44,12 +44,8 @@ import java.io.InputStreamReader;
|
|||||||
import java.io.PipedInputStream;
|
import java.io.PipedInputStream;
|
||||||
import java.io.PipedOutputStream;
|
import java.io.PipedOutputStream;
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
import java.nio.ByteBuffer;
|
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
|
|
||||||
import fr.bigeon.gclc.manager.ConsoleManager;
|
|
||||||
import fr.bigeon.gclc.manager.SystemConsoleManager;
|
|
||||||
|
|
||||||
/** This console manager allows to enter commands and retrieve the output as an
|
/** This console manager allows to enter commands and retrieve the output as an
|
||||||
* input.
|
* input.
|
||||||
* <p>
|
* <p>
|
||||||
@@ -57,26 +53,45 @@ import fr.bigeon.gclc.manager.SystemConsoleManager;
|
|||||||
* used to test application behavior.
|
* used to test application behavior.
|
||||||
*
|
*
|
||||||
* @author Emmanuel Bigeon */
|
* @author Emmanuel Bigeon */
|
||||||
public class TestConsoleManager implements ConsoleManager, AutoCloseable {
|
public final class PipedConsoleManager
|
||||||
|
implements ConsoleManager, AutoCloseable {
|
||||||
|
|
||||||
|
/** The encoding between streams */
|
||||||
|
private static final String UTF_8 = "UTF-8"; //$NON-NLS-1$
|
||||||
|
/** THe inner manager */
|
||||||
private final SystemConsoleManager innerManager;
|
private final SystemConsoleManager innerManager;
|
||||||
|
/** The stream to pipe commands into */
|
||||||
private final PipedOutputStream commandInput;
|
private final PipedOutputStream commandInput;
|
||||||
|
/** The reader to get application return from */
|
||||||
private final BufferedReader commandBuffOutput;
|
private final BufferedReader commandBuffOutput;
|
||||||
|
/** The stream to get application return from */
|
||||||
private final PipedInputStream commandOutput;
|
private final PipedInputStream commandOutput;
|
||||||
|
/** The print writer for application to write return to */
|
||||||
private final PrintStream outPrint;
|
private final PrintStream outPrint;
|
||||||
|
/** The stream for the application to read commands from */
|
||||||
private final PipedInputStream in;
|
private final PipedInputStream in;
|
||||||
|
/** The writing thread */
|
||||||
|
private final WritingRunnable writing;
|
||||||
|
/** The reading thread */
|
||||||
|
private final ReadingRunnable reading;
|
||||||
|
|
||||||
/** @throws IOException if the piping failed for streams */
|
/** @throws IOException if the piping failed for streams */
|
||||||
public TestConsoleManager() throws IOException {
|
public PipedConsoleManager() throws IOException {
|
||||||
commandInput = new PipedOutputStream();
|
commandInput = new PipedOutputStream();
|
||||||
in = new PipedInputStream(commandInput);
|
in = new PipedInputStream(commandInput);
|
||||||
commandOutput = new PipedInputStream();
|
commandOutput = new PipedInputStream();
|
||||||
PipedOutputStream out = new PipedOutputStream(commandOutput);
|
PipedOutputStream out = new PipedOutputStream(commandOutput);
|
||||||
commandBuffOutput = new BufferedReader(
|
commandBuffOutput = new BufferedReader(
|
||||||
new InputStreamReader(commandOutput, Charset.defaultCharset()));
|
new InputStreamReader(commandOutput, Charset.forName(UTF_8)));
|
||||||
outPrint = new PrintStream(out);
|
outPrint = new PrintStream(out, true, UTF_8);
|
||||||
innerManager = new SystemConsoleManager(outPrint, in,
|
innerManager = new SystemConsoleManager(outPrint, in,
|
||||||
Charset.forName("UTF-8"));
|
Charset.forName(UTF_8));
|
||||||
|
writing = new WritingRunnable(commandInput, Charset.forName(UTF_8));
|
||||||
|
reading = new ReadingRunnable(commandBuffOutput);
|
||||||
|
Thread th = new Thread(writing, "write"); //$NON-NLS-1$
|
||||||
|
th.start();
|
||||||
|
th = new Thread(reading, "read"); //$NON-NLS-1$
|
||||||
|
th.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -118,6 +133,8 @@ public class TestConsoleManager implements ConsoleManager, AutoCloseable {
|
|||||||
@Override
|
@Override
|
||||||
public void close() throws IOException {
|
public void close() throws IOException {
|
||||||
innerManager.close();
|
innerManager.close();
|
||||||
|
reading.setRunning(false);
|
||||||
|
writing.setRunning(false);
|
||||||
outPrint.close();
|
outPrint.close();
|
||||||
commandBuffOutput.close();
|
commandBuffOutput.close();
|
||||||
commandOutput.close();
|
commandOutput.close();
|
||||||
@@ -130,15 +147,22 @@ public class TestConsoleManager implements ConsoleManager, AutoCloseable {
|
|||||||
return innerManager.isClosed();
|
return innerManager.isClosed();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @param content the content to type to the application
|
||||||
|
* @throws IOException if the typing failed */
|
||||||
public void type(String content) throws IOException {
|
public void type(String content) throws IOException {
|
||||||
ByteBuffer buff = Charset.defaultCharset()
|
writing.addMessage(content);
|
||||||
.encode(content + System.lineSeparator());
|
|
||||||
if (buff.hasArray()) {
|
|
||||||
commandInput.write(buff.array());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @return the content of the next line written by the application
|
||||||
|
* @throws IOException if the reading failed */
|
||||||
public String readNextLine() throws IOException {
|
public String readNextLine() throws IOException {
|
||||||
return commandBuffOutput.readLine();
|
return reading.getMessage();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see fr.bigeon.gclc.manager.ConsoleManager#interruptPrompt() */
|
||||||
|
@Override
|
||||||
|
public void interruptPrompt() {
|
||||||
|
innerManager.interruptPrompt();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
154
gclc/src/main/java/fr/bigeon/gclc/manager/ReadingRunnable.java
Normal file
154
gclc/src/main/java/fr/bigeon/gclc/manager/ReadingRunnable.java
Normal file
@@ -0,0 +1,154 @@
|
|||||||
|
/*
|
||||||
|
* 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.test.utils.WritingRunnable.java
|
||||||
|
* Created on: Nov 29, 2016
|
||||||
|
*/
|
||||||
|
package fr.bigeon.gclc.manager;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InterruptedIOException;
|
||||||
|
import java.util.ArrayDeque;
|
||||||
|
import java.util.Deque;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
/** A runnable to read the piped output.
|
||||||
|
*
|
||||||
|
* @author Emmanuel Bigeon */
|
||||||
|
public class ReadingRunnable implements Runnable {
|
||||||
|
|
||||||
|
/** Wait timeout */
|
||||||
|
private static final long TIMEOUT = 1000;
|
||||||
|
/** Class logger */
|
||||||
|
private static final Logger LOGGER = Logger
|
||||||
|
.getLogger(ReadingRunnable.class.getName());
|
||||||
|
/** Read messages */
|
||||||
|
private final Deque<String> messages = new ArrayDeque<>();
|
||||||
|
/** the reader */
|
||||||
|
private final BufferedReader reader;
|
||||||
|
/** the state of this runnable */
|
||||||
|
private boolean running = true;
|
||||||
|
|
||||||
|
/** Synchro object */
|
||||||
|
private final Object lock = new Object();
|
||||||
|
|
||||||
|
/** @param reader the input to read from */
|
||||||
|
public ReadingRunnable(BufferedReader reader) {
|
||||||
|
super();
|
||||||
|
this.reader = reader;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see java.lang.Runnable#run() */
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
|
||||||
|
while (running) {
|
||||||
|
try {
|
||||||
|
String line = reader.readLine();
|
||||||
|
if (line == null) {
|
||||||
|
// Buffer end
|
||||||
|
running = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
LOGGER.finer("Read: " + line); //$NON-NLS-1$
|
||||||
|
line = stripNull(line);
|
||||||
|
synchronized (lock) {
|
||||||
|
messages.add(line);
|
||||||
|
lock.notify();
|
||||||
|
}
|
||||||
|
} catch (InterruptedIOException e) {
|
||||||
|
LOGGER.log(Level.INFO, "Reading interrupted", e); //$NON-NLS-1$
|
||||||
|
} catch (IOException e) {
|
||||||
|
LOGGER.log(Level.SEVERE, "Unable to read from stream", e); //$NON-NLS-1$
|
||||||
|
running = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Strip the string from head NULL characters.
|
||||||
|
*
|
||||||
|
* @param line the line to strip the null character from
|
||||||
|
* @return the resulting string */
|
||||||
|
private static String stripNull(String line) {
|
||||||
|
String res = line;
|
||||||
|
while (res.length() > 0 && res.charAt(0) == 0) {
|
||||||
|
LOGGER.severe(
|
||||||
|
"NULL character heading the result of the read. This is a stream problem..."); //$NON-NLS-1$
|
||||||
|
res = res.substring(1);
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return the next read message
|
||||||
|
* @throws IOException if the pipe is closed */
|
||||||
|
public String getMessage() throws IOException {
|
||||||
|
synchronized (lock) {
|
||||||
|
if (!running) {
|
||||||
|
throw new IOException("Closed pipe"); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
while (messages.isEmpty()) {
|
||||||
|
try {
|
||||||
|
lock.wait(TIMEOUT);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
LOGGER.log(Level.SEVERE, "Thread interruption exception.", //$NON-NLS-1$
|
||||||
|
e);
|
||||||
|
}
|
||||||
|
if (messages.isEmpty() && !running) {
|
||||||
|
throw new IOException("Closed pipe"); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
}
|
||||||
|
LOGGER.finest("Polled: " + messages.peek()); //$NON-NLS-1$
|
||||||
|
return messages.poll();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @param running the running to set */
|
||||||
|
public void setRunning(boolean running) {
|
||||||
|
synchronized (lock) {
|
||||||
|
this.running = running;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return the running */
|
||||||
|
public boolean isRunning() {
|
||||||
|
synchronized (lock) {
|
||||||
|
return running;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -44,26 +44,17 @@ import java.io.InputStream;
|
|||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.util.logging.Level;
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
/** A console using the input stream and print stream.
|
/** A console using the input stream and print stream.
|
||||||
* <p>
|
* <p>
|
||||||
* The default constructor will use the system standart input and output.
|
* The default constructor will use the system standart input and output.
|
||||||
*
|
*
|
||||||
* @author Emmanuel BIGEON */
|
* @author Emmanuel BIGEON */
|
||||||
public class SystemConsoleManager implements ConsoleManager { // NOSONAR
|
public final class SystemConsoleManager implements ConsoleManager { // NOSONAR
|
||||||
|
|
||||||
/** The default prompt */
|
/** The default prompt */
|
||||||
public static final String DEFAULT_PROMPT = "> "; //$NON-NLS-1$
|
public static final String DEFAULT_PROMPT = "> "; //$NON-NLS-1$
|
||||||
|
|
||||||
/** The logger */
|
|
||||||
private static final Logger LOGGER = Logger
|
|
||||||
.getLogger(SystemConsoleManager.class.getName());
|
|
||||||
|
|
||||||
/** The empty string constant */
|
|
||||||
private static final String EMPTY = ""; //$NON-NLS-1$
|
|
||||||
|
|
||||||
/** The command prompt. It can be changed. */
|
/** The command prompt. It can be changed. */
|
||||||
private String prompt = DEFAULT_PROMPT;
|
private String prompt = DEFAULT_PROMPT;
|
||||||
|
|
||||||
@@ -75,6 +66,12 @@ public class SystemConsoleManager implements ConsoleManager { // NOSONAR
|
|||||||
/** If the manager is closed */
|
/** If the manager is closed */
|
||||||
private boolean closed = false;
|
private boolean closed = false;
|
||||||
|
|
||||||
|
/** The prompting thread */
|
||||||
|
private final Thread promptThread;
|
||||||
|
|
||||||
|
/** The reading runnable */
|
||||||
|
private final ReadingRunnable reading;
|
||||||
|
|
||||||
/** This default constructor relies on the system defined standart output
|
/** This default constructor relies on the system defined standart output
|
||||||
* and input stream. */
|
* and input stream. */
|
||||||
public SystemConsoleManager() {
|
public SystemConsoleManager() {
|
||||||
@@ -89,6 +86,9 @@ public class SystemConsoleManager implements ConsoleManager { // NOSONAR
|
|||||||
super();
|
super();
|
||||||
this.out = out;
|
this.out = out;
|
||||||
this.in = new BufferedReader(new InputStreamReader(in, charset));
|
this.in = new BufferedReader(new InputStreamReader(in, charset));
|
||||||
|
reading = new ReadingRunnable(this.in);
|
||||||
|
promptThread = new Thread(reading, "prompt"); //$NON-NLS-1$
|
||||||
|
promptThread.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @return the prompt */
|
/** @return the prompt */
|
||||||
@@ -140,18 +140,8 @@ public class SystemConsoleManager implements ConsoleManager { // NOSONAR
|
|||||||
@Override
|
@Override
|
||||||
public String prompt(String message) throws IOException {
|
public String prompt(String message) throws IOException {
|
||||||
checkOpen();
|
checkOpen();
|
||||||
String result = EMPTY;
|
|
||||||
out.print(message);
|
out.print(message);
|
||||||
try {
|
return reading.getMessage();
|
||||||
result = in.readLine();
|
|
||||||
while (result != null && result.length() > 0 &&
|
|
||||||
result.charAt(0) == 0) {
|
|
||||||
result = result.substring(1);
|
|
||||||
}
|
|
||||||
} catch (final IOException e) {
|
|
||||||
LOGGER.log(Level.SEVERE, "Unable to read prompt", e); //$NON-NLS-1$
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @param prompt the prompt to set */
|
/** @param prompt the prompt to set */
|
||||||
@@ -165,6 +155,7 @@ public class SystemConsoleManager implements ConsoleManager { // NOSONAR
|
|||||||
@Override
|
@Override
|
||||||
public void close() throws IOException {
|
public void close() throws IOException {
|
||||||
closed = true;
|
closed = true;
|
||||||
|
reading.setRunning(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
@@ -174,4 +165,12 @@ public class SystemConsoleManager implements ConsoleManager { // NOSONAR
|
|||||||
return closed;
|
return closed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Beware, in this implementation this is the same as closing the manager.
|
||||||
|
*
|
||||||
|
* @see fr.bigeon.gclc.manager.ConsoleManager#interruptPrompt() */
|
||||||
|
@Override
|
||||||
|
public void interruptPrompt() {
|
||||||
|
promptThread.interrupt();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
140
gclc/src/main/java/fr/bigeon/gclc/manager/WritingRunnable.java
Normal file
140
gclc/src/main/java/fr/bigeon/gclc/manager/WritingRunnable.java
Normal file
@@ -0,0 +1,140 @@
|
|||||||
|
/*
|
||||||
|
* 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.test.utils.WritingRunnable.java
|
||||||
|
* Created on: Nov 29, 2016
|
||||||
|
*/
|
||||||
|
package fr.bigeon.gclc.manager;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.PipedOutputStream;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.util.ArrayDeque;
|
||||||
|
import java.util.Deque;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
/** The runnable in charge of writing messages as they are read.
|
||||||
|
* <p>
|
||||||
|
* Messages are queued to be retrieved latter on.
|
||||||
|
*
|
||||||
|
* @author Emmanuel Bigeon */
|
||||||
|
public class WritingRunnable implements Runnable {
|
||||||
|
|
||||||
|
/** Wait timeout */
|
||||||
|
private static final long TIMEOUT = 1000;
|
||||||
|
/** Class logger */
|
||||||
|
private static final Logger LOGGER = Logger
|
||||||
|
.getLogger(WritingRunnable.class.getName());
|
||||||
|
/** Messages to write */
|
||||||
|
private final Deque<String> messages = new ArrayDeque<>();
|
||||||
|
/** Stream to write to */
|
||||||
|
private final PipedOutputStream outPrint;
|
||||||
|
/** The charset */
|
||||||
|
private final Charset charset;
|
||||||
|
/** Runnable state */
|
||||||
|
private boolean running = true;
|
||||||
|
|
||||||
|
/** Synchro object */
|
||||||
|
private final Object lock = new Object();
|
||||||
|
|
||||||
|
/** @param outPrint the output to print to
|
||||||
|
* @param charset the charset of the stream */
|
||||||
|
public WritingRunnable(PipedOutputStream outPrint, Charset charset) {
|
||||||
|
super();
|
||||||
|
this.outPrint = outPrint;
|
||||||
|
this.charset = charset;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see java.lang.Runnable#run() */
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
|
||||||
|
while (running) {
|
||||||
|
synchronized (lock) {
|
||||||
|
while (messages.isEmpty()) {
|
||||||
|
try {
|
||||||
|
lock.wait(TIMEOUT);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
LOGGER.log(Level.SEVERE,
|
||||||
|
"Thread interruption exception.", e); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
if (!running) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
String message = messages.poll();
|
||||||
|
ByteBuffer buff = charset
|
||||||
|
.encode(message + System.lineSeparator());
|
||||||
|
if (buff.hasArray()) {
|
||||||
|
try {
|
||||||
|
outPrint.write(buff.array());
|
||||||
|
} catch (IOException e) {
|
||||||
|
LOGGER.log(Level.SEVERE, "Unable to write to stream", //$NON-NLS-1$
|
||||||
|
e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @param message the message
|
||||||
|
* @throws IOException if the pipe is closed */
|
||||||
|
public void addMessage(String message) throws IOException {
|
||||||
|
synchronized (lock) {
|
||||||
|
if (!running) {
|
||||||
|
throw new IOException("Closed pipe"); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
messages.offer(message);
|
||||||
|
lock.notify();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @param running the running to set */
|
||||||
|
public void setRunning(boolean running) {
|
||||||
|
synchronized (lock) {
|
||||||
|
this.running = running;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return the running */
|
||||||
|
public boolean isRunning() {
|
||||||
|
synchronized (lock) {
|
||||||
|
return running;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -42,7 +42,7 @@ import java.io.IOException;
|
|||||||
|
|
||||||
import fr.bigeon.gclc.command.ICommand;
|
import fr.bigeon.gclc.command.ICommand;
|
||||||
import fr.bigeon.gclc.exception.InvalidCommandName;
|
import fr.bigeon.gclc.exception.InvalidCommandName;
|
||||||
import fr.bigeon.gclc.test.utils.TestConsoleManager;
|
import fr.bigeon.gclc.manager.PipedConsoleManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
@@ -56,11 +56,11 @@ public class CommandTestingApplication implements AutoCloseable {
|
|||||||
|
|
||||||
private final ConsoleTestApplication application;
|
private final ConsoleTestApplication application;
|
||||||
private final Thread th;
|
private final Thread th;
|
||||||
private final TestConsoleManager manager;
|
private final PipedConsoleManager manager;
|
||||||
|
|
||||||
/** @throws IOException if the streams cannot be build */
|
/** @throws IOException if the streams cannot be build */
|
||||||
public CommandTestingApplication() throws IOException {
|
public CommandTestingApplication() throws IOException {
|
||||||
manager = new TestConsoleManager();
|
manager = new PipedConsoleManager();
|
||||||
application = new ConsoleTestApplication(manager);
|
application = new ConsoleTestApplication(manager);
|
||||||
th = new Thread(new Runnable() {
|
th = new Thread(new Runnable() {
|
||||||
|
|
||||||
@@ -110,7 +110,7 @@ public class CommandTestingApplication implements AutoCloseable {
|
|||||||
|
|
||||||
/** @return the next written line, null if it is the prompt
|
/** @return the next written line, null if it is the prompt
|
||||||
* @throws IOException if the reading failed
|
* @throws IOException if the reading failed
|
||||||
* @see fr.bigeon.gclc.test.utils.TestConsoleManager#readNextLine() */
|
* @see fr.bigeon.gclc.manager.PipedConsoleManager#readNextLine() */
|
||||||
public String readNextLine() throws IOException {
|
public String readNextLine() throws IOException {
|
||||||
String ret = manager.readNextLine();
|
String ret = manager.readNextLine();
|
||||||
if (ret.equals(manager.getPrompt())) {
|
if (ret.equals(manager.getPrompt())) {
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ import fr.bigeon.gclc.exception.InvalidCommandName;
|
|||||||
import fr.bigeon.gclc.i18n.Messages;
|
import fr.bigeon.gclc.i18n.Messages;
|
||||||
import fr.bigeon.gclc.manager.ConsoleManager;
|
import fr.bigeon.gclc.manager.ConsoleManager;
|
||||||
import fr.bigeon.gclc.manager.SystemConsoleManager;
|
import fr.bigeon.gclc.manager.SystemConsoleManager;
|
||||||
import fr.bigeon.gclc.test.utils.TestConsoleManager;
|
import fr.bigeon.gclc.manager.PipedConsoleManager;
|
||||||
|
|
||||||
/** Test class for ConsoleApplication
|
/** Test class for ConsoleApplication
|
||||||
*
|
*
|
||||||
@@ -136,7 +136,7 @@ public class ConsoleApplicationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ConsoleApplication appli = null;
|
ConsoleApplication appli = null;
|
||||||
try (TestConsoleManager manager = new TestConsoleManager()) {
|
try (PipedConsoleManager manager = new PipedConsoleManager()) {
|
||||||
final ConsoleApplication app = new ConsoleApplication(manager, null,
|
final ConsoleApplication app = new ConsoleApplication(manager, null,
|
||||||
null);
|
null);
|
||||||
appli = app;
|
appli = app;
|
||||||
@@ -167,7 +167,7 @@ public class ConsoleApplicationTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void interpretCommandTest() {
|
public void interpretCommandTest() {
|
||||||
try (TestConsoleManager test = new TestConsoleManager()) {
|
try (PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
ConsoleApplication appl = new ConsoleApplication(test, "", "");
|
ConsoleApplication appl = new ConsoleApplication(test, "", "");
|
||||||
|
|
||||||
appl.interpretCommand("invalid cmd \"due to misplaced\"quote");
|
appl.interpretCommand("invalid cmd \"due to misplaced\"quote");
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ import java.io.IOException;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import fr.bigeon.gclc.exception.CommandRunException;
|
import fr.bigeon.gclc.exception.CommandRunException;
|
||||||
import fr.bigeon.gclc.test.utils.TestConsoleManager;
|
import fr.bigeon.gclc.manager.PipedConsoleManager;
|
||||||
|
|
||||||
/** <p>
|
/** <p>
|
||||||
* TODO
|
* TODO
|
||||||
@@ -55,7 +55,7 @@ public class CommandTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void test() {
|
public final void test() {
|
||||||
try (TestConsoleManager test = new TestConsoleManager()) {
|
try (PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
Command cmd;
|
Command cmd;
|
||||||
cmd = new Command("name") {
|
cmd = new Command("name") {
|
||||||
|
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ import static org.junit.Assert.fail;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import fr.bigeon.gclc.exception.CommandRunException;
|
import fr.bigeon.gclc.exception.CommandRunException;
|
||||||
import fr.bigeon.gclc.test.utils.TestConsoleManager;
|
import fr.bigeon.gclc.manager.PipedConsoleManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
@@ -68,7 +68,7 @@ public class HelpExecutorTest {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
assertNotNull(e);
|
assertNotNull(e);
|
||||||
}
|
}
|
||||||
try (TestConsoleManager test = new TestConsoleManager()) {
|
try (PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
help = new HelpExecutor("?", test, new MockCommand("mock"));
|
help = new HelpExecutor("?", test, new MockCommand("mock"));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
assertNull(e);
|
assertNull(e);
|
||||||
@@ -81,7 +81,7 @@ public class HelpExecutorTest {
|
|||||||
@Test
|
@Test
|
||||||
public final void testExecute(){
|
public final void testExecute(){
|
||||||
try {
|
try {
|
||||||
TestConsoleManager test = new TestConsoleManager();
|
PipedConsoleManager test = new PipedConsoleManager();
|
||||||
HelpExecutor help = new HelpExecutor("?", test,
|
HelpExecutor help = new HelpExecutor("?", test,
|
||||||
new Command("mock") {
|
new Command("mock") {
|
||||||
|
|
||||||
@@ -116,7 +116,7 @@ public class HelpExecutorTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public final void testTip(){
|
public final void testTip(){
|
||||||
try (TestConsoleManager test = new TestConsoleManager()) {
|
try (PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
HelpExecutor help = new HelpExecutor("?", test,
|
HelpExecutor help = new HelpExecutor("?", test,
|
||||||
new MockCommand("mock"));
|
new MockCommand("mock"));
|
||||||
help.tip();
|
help.tip();
|
||||||
@@ -124,7 +124,7 @@ public class HelpExecutorTest {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
assertNull(e);
|
assertNull(e);
|
||||||
}
|
}
|
||||||
try (TestConsoleManager test = new TestConsoleManager()) {
|
try (PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
HelpExecutor help = new HelpExecutor("?", test,
|
HelpExecutor help = new HelpExecutor("?", test,
|
||||||
new SubedCommand("sub", new MockCommand("mock")));
|
new SubedCommand("sub", new MockCommand("mock")));
|
||||||
help.tip();
|
help.tip();
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ import org.junit.Test;
|
|||||||
|
|
||||||
import fr.bigeon.gclc.exception.CommandRunException;
|
import fr.bigeon.gclc.exception.CommandRunException;
|
||||||
import fr.bigeon.gclc.exception.InvalidParameterException;
|
import fr.bigeon.gclc.exception.InvalidParameterException;
|
||||||
import fr.bigeon.gclc.test.utils.TestConsoleManager;
|
import fr.bigeon.gclc.manager.PipedConsoleManager;
|
||||||
|
|
||||||
/** <p>
|
/** <p>
|
||||||
* TODO
|
* TODO
|
||||||
@@ -63,7 +63,7 @@ public class ParametrizedCommandTest {
|
|||||||
* {@link fr.bigeon.gclc.command.ParametrizedCommand#ParametrizedCommand(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String)}. */
|
* {@link fr.bigeon.gclc.command.ParametrizedCommand#ParametrizedCommand(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String)}. */
|
||||||
@Test
|
@Test
|
||||||
public final void testParametrizedCommandConsoleManagerString() {
|
public final void testParametrizedCommandConsoleManagerString() {
|
||||||
try (TestConsoleManager test = new TestConsoleManager()) {
|
try (PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
ParametrizedCommand cmd = new ParametrizedCommand(test, "name") {
|
ParametrizedCommand cmd = new ParametrizedCommand(test, "name") {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -116,7 +116,7 @@ public class ParametrizedCommandTest {
|
|||||||
* {@link fr.bigeon.gclc.command.ParametrizedCommand#ParametrizedCommand(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String, boolean)}. */
|
* {@link fr.bigeon.gclc.command.ParametrizedCommand#ParametrizedCommand(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String, boolean)}. */
|
||||||
@Test
|
@Test
|
||||||
public final void testParametrizedCommandConsoleManagerStringBoolean() {
|
public final void testParametrizedCommandConsoleManagerStringBoolean() {
|
||||||
try (TestConsoleManager test = new TestConsoleManager()) {
|
try (PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
ParametrizedCommand cmd = new ParametrizedCommand(test, "name",
|
ParametrizedCommand cmd = new ParametrizedCommand(test, "name",
|
||||||
false) {
|
false) {
|
||||||
|
|
||||||
@@ -528,7 +528,7 @@ public class ParametrizedCommandTest {
|
|||||||
assertNotNull(e);
|
assertNotNull(e);
|
||||||
}
|
}
|
||||||
// TODO Test of interactive not providing and providing all needed
|
// TODO Test of interactive not providing and providing all needed
|
||||||
try (TestConsoleManager test = new TestConsoleManager()) {
|
try (PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
cmd = new ParametrizedCommand(test, "name", false) {
|
cmd = new ParametrizedCommand(test, "name", false) {
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
@@ -610,7 +610,7 @@ public class ParametrizedCommandTest {
|
|||||||
fail("unepected error");
|
fail("unepected error");
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
TestConsoleManager test = new TestConsoleManager();
|
PipedConsoleManager test = new PipedConsoleManager();
|
||||||
cmd = new ParametrizedCommand(test, "name") {
|
cmd = new ParametrizedCommand(test, "name") {
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ import org.junit.Test;
|
|||||||
import fr.bigeon.gclc.ConsoleTestApplication;
|
import fr.bigeon.gclc.ConsoleTestApplication;
|
||||||
import fr.bigeon.gclc.exception.CommandRunException;
|
import fr.bigeon.gclc.exception.CommandRunException;
|
||||||
import fr.bigeon.gclc.exception.CommandRunExceptionType;
|
import fr.bigeon.gclc.exception.CommandRunExceptionType;
|
||||||
import fr.bigeon.gclc.test.utils.TestConsoleManager;
|
import fr.bigeon.gclc.manager.PipedConsoleManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
@@ -67,9 +67,9 @@ public class ScriptExecutionTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testExecute() {
|
public void testExecute() {
|
||||||
TestConsoleManager test;
|
PipedConsoleManager test;
|
||||||
try {
|
try {
|
||||||
test = new TestConsoleManager();
|
test = new PipedConsoleManager();
|
||||||
} catch (IOException e2) {
|
} catch (IOException e2) {
|
||||||
fail("creation of console manager failed"); //$NON-NLS-1$
|
fail("creation of console manager failed"); //$NON-NLS-1$
|
||||||
assertNotNull(e2);
|
assertNotNull(e2);
|
||||||
@@ -165,7 +165,7 @@ public class ScriptExecutionTest {
|
|||||||
public void testHelp() {
|
public void testHelp() {
|
||||||
ScriptExecution exec = new ScriptExecution("script", null, "#", //$NON-NLS-1$ //$NON-NLS-2$
|
ScriptExecution exec = new ScriptExecution("script", null, "#", //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
Charset.forName("UTF-8"));
|
Charset.forName("UTF-8"));
|
||||||
try (TestConsoleManager test = new TestConsoleManager()) {
|
try (PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
exec.help(test);
|
exec.help(test);
|
||||||
exec.help(test, "ignored element");
|
exec.help(test, "ignored element");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ import org.junit.Test;
|
|||||||
import fr.bigeon.gclc.exception.CommandRunException;
|
import fr.bigeon.gclc.exception.CommandRunException;
|
||||||
import fr.bigeon.gclc.exception.InvalidCommandName;
|
import fr.bigeon.gclc.exception.InvalidCommandName;
|
||||||
import fr.bigeon.gclc.manager.ConsoleManager;
|
import fr.bigeon.gclc.manager.ConsoleManager;
|
||||||
import fr.bigeon.gclc.test.utils.TestConsoleManager;
|
import fr.bigeon.gclc.manager.PipedConsoleManager;
|
||||||
|
|
||||||
/** <p>
|
/** <p>
|
||||||
* TODO
|
* TODO
|
||||||
@@ -307,8 +307,8 @@ public class SubedCommandTest {
|
|||||||
assertNotNull(e);
|
assertNotNull(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
try (TestConsoleManager manager = new TestConsoleManager();
|
try (PipedConsoleManager manager = new PipedConsoleManager();
|
||||||
TestConsoleManager manager2 = new TestConsoleManager()) {
|
PipedConsoleManager manager2 = new PipedConsoleManager()) {
|
||||||
cmd.help(manager);
|
cmd.help(manager);
|
||||||
assertEquals("\tid", manager.readNextLine());
|
assertEquals("\tid", manager.readNextLine());
|
||||||
cmd.help(manager, "id");
|
cmd.help(manager, "id");
|
||||||
@@ -327,8 +327,8 @@ public class SubedCommandTest {
|
|||||||
assertNotNull(e);
|
assertNotNull(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
try (TestConsoleManager manager = new TestConsoleManager();
|
try (PipedConsoleManager manager = new PipedConsoleManager();
|
||||||
TestConsoleManager manager2 = new TestConsoleManager()) {
|
PipedConsoleManager manager2 = new PipedConsoleManager()) {
|
||||||
cmd.help(manager);
|
cmd.help(manager);
|
||||||
assertEquals("\tid", manager.readNextLine());
|
assertEquals("\tid", manager.readNextLine());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@@ -368,8 +368,8 @@ public class SubedCommandTest {
|
|||||||
assertNotNull(e);
|
assertNotNull(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
try (TestConsoleManager manager = new TestConsoleManager();
|
try (PipedConsoleManager manager = new PipedConsoleManager();
|
||||||
TestConsoleManager manager2 = new TestConsoleManager()) {
|
PipedConsoleManager manager2 = new PipedConsoleManager()) {
|
||||||
cmd.help(manager);
|
cmd.help(manager);
|
||||||
assertEquals("\ttip", manager.readNextLine());
|
assertEquals("\ttip", manager.readNextLine());
|
||||||
assertEquals("\tid: tip", manager.readNextLine());
|
assertEquals("\tid: tip", manager.readNextLine());
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ import org.junit.After;
|
|||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import fr.bigeon.gclc.test.utils.TestConsoleManager;
|
import fr.bigeon.gclc.manager.PipedConsoleManager;
|
||||||
|
|
||||||
/** <p>
|
/** <p>
|
||||||
* TODO
|
* TODO
|
||||||
@@ -77,7 +77,7 @@ public class CLIPrompterTest {
|
|||||||
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptBoolean(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String)}. */
|
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptBoolean(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String)}. */
|
||||||
@Test
|
@Test
|
||||||
public final void testPromptBoolean() {
|
public final void testPromptBoolean() {
|
||||||
try (final TestConsoleManager test = new TestConsoleManager()) {
|
try (final PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
Thread th = new Thread(new Runnable() {
|
Thread th = new Thread(new Runnable() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -121,7 +121,7 @@ public class CLIPrompterTest {
|
|||||||
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptChoice(fr.bigeon.gclc.manager.ConsoleManager, java.util.List, java.util.List, java.lang.String, java.lang.String)}. */
|
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptChoice(fr.bigeon.gclc.manager.ConsoleManager, java.util.List, java.util.List, java.lang.String, java.lang.String)}. */
|
||||||
@Test
|
@Test
|
||||||
public final void testPromptChoiceConsoleManagerListOfStringListOfUStringString() {
|
public final void testPromptChoiceConsoleManagerListOfStringListOfUStringString() {
|
||||||
try (final TestConsoleManager test = new TestConsoleManager()) {
|
try (final PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
final List<String> keys = new ArrayList<>();
|
final List<String> keys = new ArrayList<>();
|
||||||
final List<Object> choices = new ArrayList<>();
|
final List<Object> choices = new ArrayList<>();
|
||||||
keys.add("A choice"); //$NON-NLS-1$
|
keys.add("A choice"); //$NON-NLS-1$
|
||||||
@@ -201,7 +201,7 @@ public class CLIPrompterTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void testPromptChoiceConsoleManagerListOfUStringString() {
|
public final void testPromptChoiceConsoleManagerListOfUStringString() {
|
||||||
try (final TestConsoleManager test = new TestConsoleManager()) {
|
try (final PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
final List<Object> keys = new ArrayList<>();
|
final List<Object> keys = new ArrayList<>();
|
||||||
keys.add("A choice"); //$NON-NLS-1$
|
keys.add("A choice"); //$NON-NLS-1$
|
||||||
keys.add("An other"); //$NON-NLS-1$
|
keys.add("An other"); //$NON-NLS-1$
|
||||||
@@ -289,7 +289,7 @@ public class CLIPrompterTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void testPromptChoiceConsoleManagerListOfUMapOfUTStringString() {
|
public final void testPromptChoiceConsoleManagerListOfUMapOfUTStringString() {
|
||||||
try (final TestConsoleManager test = new TestConsoleManager()) {
|
try (final PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
final List<Object> keys = new ArrayList<>();
|
final List<Object> keys = new ArrayList<>();
|
||||||
final Map<Object, Object> choices = new HashMap<>();
|
final Map<Object, Object> choices = new HashMap<>();
|
||||||
keys.add("A choice"); //$NON-NLS-1$
|
keys.add("A choice"); //$NON-NLS-1$
|
||||||
@@ -373,7 +373,7 @@ public class CLIPrompterTest {
|
|||||||
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptChoice(fr.bigeon.gclc.manager.ConsoleManager, java.util.Map, java.lang.String, java.lang.String)}. */
|
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptChoice(fr.bigeon.gclc.manager.ConsoleManager, java.util.Map, java.lang.String, java.lang.String)}. */
|
||||||
@Test
|
@Test
|
||||||
public final void testPromptChoiceConsoleManagerMapOfUTStringString() {
|
public final void testPromptChoiceConsoleManagerMapOfUTStringString() {
|
||||||
try (final TestConsoleManager test = new TestConsoleManager()) {
|
try (final PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
final List<Object> keys = new ArrayList<>();
|
final List<Object> keys = new ArrayList<>();
|
||||||
final Map<Object, Object> choices = new HashMap<>();
|
final Map<Object, Object> choices = new HashMap<>();
|
||||||
keys.add("A choice"); //$NON-NLS-1$
|
keys.add("A choice"); //$NON-NLS-1$
|
||||||
@@ -455,7 +455,7 @@ public class CLIPrompterTest {
|
|||||||
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptInteger(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String)}. */
|
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptInteger(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String)}. */
|
||||||
@Test
|
@Test
|
||||||
public final void testPromptInteger() {
|
public final void testPromptInteger() {
|
||||||
try (final TestConsoleManager test = new TestConsoleManager()) {
|
try (final PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
Thread th = new Thread(new Runnable() {
|
Thread th = new Thread(new Runnable() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -492,7 +492,7 @@ public class CLIPrompterTest {
|
|||||||
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptList(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String)}. */
|
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptList(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String)}. */
|
||||||
@Test
|
@Test
|
||||||
public final void testPromptListConsoleManagerString() {
|
public final void testPromptListConsoleManagerString() {
|
||||||
try (final TestConsoleManager test = new TestConsoleManager()) {
|
try (final PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
final List<String> keys = new ArrayList<>();
|
final List<String> keys = new ArrayList<>();
|
||||||
keys.add("A choice"); //$NON-NLS-1$
|
keys.add("A choice"); //$NON-NLS-1$
|
||||||
keys.add("An other"); //$NON-NLS-1$
|
keys.add("An other"); //$NON-NLS-1$
|
||||||
@@ -549,7 +549,7 @@ public class CLIPrompterTest {
|
|||||||
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptList(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String, java.lang.String)}. */
|
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptList(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String, java.lang.String)}. */
|
||||||
@Test
|
@Test
|
||||||
public final void testPromptListConsoleManagerStringString() {
|
public final void testPromptListConsoleManagerStringString() {
|
||||||
try (final TestConsoleManager test = new TestConsoleManager()) {
|
try (final PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
final List<String> keys = new ArrayList<>();
|
final List<String> keys = new ArrayList<>();
|
||||||
keys.add("A choice"); //$NON-NLS-1$
|
keys.add("A choice"); //$NON-NLS-1$
|
||||||
keys.add("An other"); //$NON-NLS-1$
|
keys.add("An other"); //$NON-NLS-1$
|
||||||
@@ -603,7 +603,7 @@ public class CLIPrompterTest {
|
|||||||
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptLongText(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String)}. */
|
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptLongText(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String)}. */
|
||||||
@Test
|
@Test
|
||||||
public final void testPromptLongTextConsoleManagerString() {
|
public final void testPromptLongTextConsoleManagerString() {
|
||||||
try (final TestConsoleManager test = new TestConsoleManager()) {
|
try (final PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
final String message = "My message";
|
final String message = "My message";
|
||||||
final String longText = "Some text with" + System.lineSeparator() +
|
final String longText = "Some text with" + System.lineSeparator() +
|
||||||
"line feeds and other" + System.lineSeparator() +
|
"line feeds and other" + System.lineSeparator() +
|
||||||
@@ -663,7 +663,7 @@ public class CLIPrompterTest {
|
|||||||
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptLongText(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String, java.lang.String)}. */
|
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptLongText(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String, java.lang.String)}. */
|
||||||
@Test
|
@Test
|
||||||
public final void testPromptLongTextConsoleManagerStringString() {
|
public final void testPromptLongTextConsoleManagerStringString() {
|
||||||
try (final TestConsoleManager test = new TestConsoleManager()) {
|
try (final PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
final String message = "My message";
|
final String message = "My message";
|
||||||
final String ender = "\\quit";
|
final String ender = "\\quit";
|
||||||
final String[] text = new String[]{"Some text with" ,
|
final String[] text = new String[]{"Some text with" ,
|
||||||
@@ -676,7 +676,7 @@ public class CLIPrompterTest {
|
|||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
assertEquals("",
|
assertEquals("", //$NON-NLS-1$
|
||||||
CLIPrompter.promptLongText(test, message, ender));
|
CLIPrompter.promptLongText(test, message, ender));
|
||||||
assertEquals(longText,
|
assertEquals(longText,
|
||||||
CLIPrompter.promptLongText(test, message, ender));
|
CLIPrompter.promptLongText(test, message, ender));
|
||||||
@@ -720,7 +720,7 @@ public class CLIPrompterTest {
|
|||||||
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptMultiChoice(fr.bigeon.gclc.manager.ConsoleManager, java.util.List, java.util.List, java.lang.String)}. */
|
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptMultiChoice(fr.bigeon.gclc.manager.ConsoleManager, java.util.List, java.util.List, java.lang.String)}. */
|
||||||
@Test
|
@Test
|
||||||
public final void testPromptMultiChoiceConsoleManagerListOfStringListOfUString() {
|
public final void testPromptMultiChoiceConsoleManagerListOfStringListOfUString() {
|
||||||
try (final TestConsoleManager test = new TestConsoleManager()) {
|
try (final PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
final List<String> keys = new ArrayList<>();
|
final List<String> keys = new ArrayList<>();
|
||||||
final List<Object> choices = new ArrayList<>();
|
final List<Object> choices = new ArrayList<>();
|
||||||
keys.add("A choice"); //$NON-NLS-1$
|
keys.add("A choice"); //$NON-NLS-1$
|
||||||
@@ -803,7 +803,7 @@ public class CLIPrompterTest {
|
|||||||
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptMultiChoice(fr.bigeon.gclc.manager.ConsoleManager, java.util.List, java.util.Map, java.lang.String)}. */
|
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptMultiChoice(fr.bigeon.gclc.manager.ConsoleManager, java.util.List, java.util.Map, java.lang.String)}. */
|
||||||
@Test
|
@Test
|
||||||
public final void testPromptMultiChoiceConsoleManagerListOfUMapOfUTString() {
|
public final void testPromptMultiChoiceConsoleManagerListOfUMapOfUTString() {
|
||||||
try (final TestConsoleManager test = new TestConsoleManager()) {
|
try (final PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
final List<Object> keys = new ArrayList<>();
|
final List<Object> keys = new ArrayList<>();
|
||||||
final Map<Object, Object> choices = new HashMap<>();
|
final Map<Object, Object> choices = new HashMap<>();
|
||||||
keys.add("A choice"); //$NON-NLS-1$
|
keys.add("A choice"); //$NON-NLS-1$
|
||||||
@@ -886,7 +886,7 @@ public class CLIPrompterTest {
|
|||||||
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptMultiChoice(fr.bigeon.gclc.manager.ConsoleManager, java.util.List, java.lang.String)}. */
|
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptMultiChoice(fr.bigeon.gclc.manager.ConsoleManager, java.util.List, java.lang.String)}. */
|
||||||
@Test
|
@Test
|
||||||
public final void testPromptMultiChoiceConsoleManagerListOfUString() {
|
public final void testPromptMultiChoiceConsoleManagerListOfUString() {
|
||||||
try (final TestConsoleManager test = new TestConsoleManager()) {
|
try (final PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
final List<Object> keys = new ArrayList<>();
|
final List<Object> keys = new ArrayList<>();
|
||||||
keys.add("A choice"); //$NON-NLS-1$
|
keys.add("A choice"); //$NON-NLS-1$
|
||||||
keys.add("An other"); //$NON-NLS-1$
|
keys.add("An other"); //$NON-NLS-1$
|
||||||
@@ -966,7 +966,7 @@ public class CLIPrompterTest {
|
|||||||
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptMultiChoice(fr.bigeon.gclc.manager.ConsoleManager, java.util.Map, java.lang.String)}. */
|
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptMultiChoice(fr.bigeon.gclc.manager.ConsoleManager, java.util.Map, java.lang.String)}. */
|
||||||
@Test
|
@Test
|
||||||
public final void testPromptMultiChoiceConsoleManagerMapOfUTString() {
|
public final void testPromptMultiChoiceConsoleManagerMapOfUTString() {
|
||||||
try (final TestConsoleManager test = new TestConsoleManager()) {
|
try (final PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
final List<Object> keys = new ArrayList<>();
|
final List<Object> keys = new ArrayList<>();
|
||||||
final Map<Object, Object> choices = new HashMap<>();
|
final Map<Object, Object> choices = new HashMap<>();
|
||||||
keys.add("A choice"); //$NON-NLS-1$
|
keys.add("A choice"); //$NON-NLS-1$
|
||||||
@@ -1049,7 +1049,7 @@ public class CLIPrompterTest {
|
|||||||
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptNonEmpty(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String, java.lang.String)}. */
|
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptNonEmpty(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String, java.lang.String)}. */
|
||||||
@Test
|
@Test
|
||||||
public final void testPromptNonEmpty() {
|
public final void testPromptNonEmpty() {
|
||||||
try (final TestConsoleManager test = new TestConsoleManager()) {
|
try (final PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
final String res = "some content"; //$NON-NLS-1$
|
final String res = "some content"; //$NON-NLS-1$
|
||||||
Thread th = new Thread(new Runnable() {
|
Thread th = new Thread(new Runnable() {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user