Compare commits
33 Commits
gclc-1.2.1
...
gclc-1.2.6
| Author | SHA1 | Date | |
|---|---|---|---|
| 9e5d8c4ca4 | |||
| a70be63894 | |||
| c2fa0f2c0b | |||
| 5280ee98bd | |||
| 1a207c8100 | |||
| 80d10d7d85 | |||
| ecc10994ca | |||
| 7289fc12ad | |||
| f44366ff05 | |||
| 70a71c06a7 | |||
| a0202de532 | |||
| caa00f2a61 | |||
| eed6f43aea | |||
| 59ab689a36 | |||
| 2bf1fa7c80 | |||
| 32e5f777fe | |||
| f8aeef14e3 | |||
| 763b7361ec | |||
| c151107207 | |||
| f95d2f114a | |||
| e04e7ceaa5 | |||
| 3c1c8e85c8 | |||
| 095a363520 | |||
| 932780e9d9 | |||
| d404d0ab69 | |||
| 8038db26f3 | |||
| a29fe4b458 | |||
| 4ddce780e0 | |||
| 12326b7cba | |||
| 04fcb5bedd | |||
| fef08dd29a | |||
| 801189fdeb | |||
| 04ca80f1b5 |
@@ -67,15 +67,15 @@ of Emmanuel Bigeon. -->
|
|||||||
<!-- The fact that you are presently reading this means that you have had -->
|
<!-- The fact that you are presently reading this means that you have had -->
|
||||||
<!-- knowledge of the CeCILL license and that you accept its terms. -->
|
<!-- knowledge of the CeCILL license and that you accept its terms. -->
|
||||||
|
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<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">
|
||||||
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-socket</artifactId>
|
<artifactId>gclc-socket</artifactId>
|
||||||
<version>1.0.4-SNAPSHOT</version>
|
<version>1.0.7-SNAPSHOT</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
<url>http://www.bigeon.fr/emmanuel</url>
|
<url>http://www.bigeon.fr/emmanuel</url>
|
||||||
<properties>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<project.scm.id>git.bigeon.net</project.scm.id>
|
||||||
</properties>
|
</properties>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
@@ -87,19 +87,23 @@ of Emmanuel Bigeon. -->
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>fr.bigeon</groupId>
|
<groupId>fr.bigeon</groupId>
|
||||||
<artifactId>gclc</artifactId>
|
<artifactId>gclc</artifactId>
|
||||||
<version>1.2.0</version>
|
<version>1.2.5</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>fr.bigeon</groupId>
|
<groupId>fr.bigeon</groupId>
|
||||||
<artifactId>smu</artifactId>
|
<artifactId>smu</artifactId>
|
||||||
<version>0.0.4</version>
|
<version>0.0.5</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>fr.bigeon</groupId>
|
<groupId>fr.bigeon</groupId>
|
||||||
<artifactId>ebigeon-config</artifactId>
|
<artifactId>ebigeon-config</artifactId>
|
||||||
<version>1.6.0</version>
|
<version>1.7.1</version>
|
||||||
</parent>
|
</parent>
|
||||||
<name>GCLC Socket</name>
|
<name>GCLC Socket</name>
|
||||||
<description>Socket implementation of GCLC</description>
|
<description>Socket implementation of GCLC</description>
|
||||||
|
<scm>
|
||||||
|
<developerConnection>scm:git:gogs@git.code.bigeon.net:emmanuel/gclc.git</developerConnection>
|
||||||
|
<tag>HEAD</tag>
|
||||||
|
</scm>
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@@ -45,6 +45,7 @@ import java.io.PrintWriter;
|
|||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.net.SocketException;
|
import java.net.SocketException;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
@@ -108,19 +109,23 @@ public class SocketConsoleApplicationShell implements Runnable {
|
|||||||
private ServerSocket serverSocket;
|
private ServerSocket serverSocket;
|
||||||
/** The application shutdown string */
|
/** The application shutdown string */
|
||||||
private final String applicationShutdown;
|
private final String applicationShutdown;
|
||||||
|
/** The charset for the communication. */
|
||||||
|
private 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
|
||||||
*
|
*
|
||||||
* @param port the port to listen to
|
* @param port the port to listen to
|
||||||
* @param close the session closing command
|
* @param close the session closing command
|
||||||
* @param applicationShutdown the appication shut down command */
|
* @param applicationShutdown the appication shut down command
|
||||||
|
* @param charset the charset for communication */
|
||||||
public SocketConsoleApplicationShell(int port, String close,
|
public SocketConsoleApplicationShell(int port, String close,
|
||||||
String applicationShutdown) {
|
String applicationShutdown, Charset charset) {
|
||||||
this.port = port;
|
this.port = port;
|
||||||
this.close = close;
|
this.close = close;
|
||||||
this.applicationShutdown = applicationShutdown;
|
this.applicationShutdown = applicationShutdown;
|
||||||
this.autoClose = false;
|
this.autoClose = false;
|
||||||
|
this.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
|
||||||
@@ -129,47 +134,52 @@ public class SocketConsoleApplicationShell implements Runnable {
|
|||||||
* @param port the port to listen to
|
* @param port the port to listen to
|
||||||
* @param autoClose if the session must be closed once the request has been
|
* @param autoClose if the session must be closed once the request has been
|
||||||
* sent
|
* sent
|
||||||
* @param applicationShutdown the application shutdown command */
|
* @param applicationShutdown the appication shut down command
|
||||||
|
* @param charset the charset for communication */
|
||||||
public SocketConsoleApplicationShell(int port, boolean autoClose,
|
public SocketConsoleApplicationShell(int port, boolean autoClose,
|
||||||
String applicationShutdown) {
|
String applicationShutdown, Charset charset) {
|
||||||
this.port = port;
|
this.port = port;
|
||||||
this.autoClose = autoClose;
|
this.autoClose = autoClose;
|
||||||
this.applicationShutdown = applicationShutdown;
|
this.applicationShutdown = applicationShutdown;
|
||||||
this.close = autoClose ? null : "close"; //$NON-NLS-1$
|
this.close = autoClose ? null : "close"; //$NON-NLS-1$
|
||||||
|
this.charset = charset;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
* @see java.lang.Runnable#run() */
|
* @see java.lang.Runnable#run() */
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
// Create the server
|
||||||
try (ServerSocket actualServerSocket = new ServerSocket(port)) {
|
try (ServerSocket actualServerSocket = new ServerSocket(port)) {
|
||||||
this.serverSocket = actualServerSocket;
|
this.serverSocket = actualServerSocket;
|
||||||
running = true;
|
running = true;
|
||||||
|
// 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));
|
||||||
|
InputStreamReader isr = new InputStreamReader(consoleInput,
|
||||||
|
charset);
|
||||||
|
BufferedReader inBuf = new BufferedReader(isr)) {
|
||||||
consoleInput.connect(outStream);
|
consoleInput.connect(outStream);
|
||||||
try (InputStreamReader isr = new InputStreamReader(
|
consoleManager.setInput(inBuf);
|
||||||
consoleInput);
|
runSokectServer(writer);
|
||||||
BufferedReader inBuf = new BufferedReader(isr);) {
|
// Close the application
|
||||||
consoleManager.setInput(inBuf);
|
// Pass command to application
|
||||||
runSokectServer(writer);
|
if (app.isRunning()) {
|
||||||
// Close the application
|
writer.write(applicationShutdown + EOL);
|
||||||
// Pass command to application
|
writer.flush();
|
||||||
if (app.isRunning()) {
|
|
||||||
writer.write(applicationShutdown + EOL);
|
|
||||||
writer.flush();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (final IOException e) {
|
} catch (
|
||||||
|
|
||||||
|
final IOException e) {
|
||||||
LOGGER.log(Level.SEVERE,
|
LOGGER.log(Level.SEVERE,
|
||||||
"Communication error between client and server", e); //$NON-NLS-1$
|
"Communication error between client and server", e); //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @param appTh the application thread
|
/** @param writer the writer to the application
|
||||||
* @param writer the writer to the application
|
|
||||||
* @throws IOException if the communication with the client failed */
|
* @throws IOException if the communication with the client failed */
|
||||||
private void runSokectServer(BufferedWriter writer) throws IOException {
|
private void runSokectServer(BufferedWriter writer) throws IOException {
|
||||||
final ConsoleRunnable runnable = new ConsoleRunnable(app,
|
final ConsoleRunnable runnable = new ConsoleRunnable(app,
|
||||||
@@ -179,9 +189,12 @@ public class SocketConsoleApplicationShell implements Runnable {
|
|||||||
while (running) {
|
while (running) {
|
||||||
try (Socket clientSocket = serverSocket.accept();
|
try (Socket clientSocket = serverSocket.accept();
|
||||||
PrintWriter out = new PrintWriter(
|
PrintWriter out = new PrintWriter(
|
||||||
clientSocket.getOutputStream(), true);
|
new OutputStreamWriter(clientSocket.getOutputStream(),
|
||||||
|
charset),
|
||||||
|
true);
|
||||||
InputStreamReader isr = new InputStreamReader(
|
InputStreamReader isr = new InputStreamReader(
|
||||||
clientSocket.getInputStream());
|
clientSocket.getInputStream(),
|
||||||
|
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
|
||||||
@@ -197,7 +210,10 @@ public class SocketConsoleApplicationShell implements Runnable {
|
|||||||
}
|
}
|
||||||
communicate(writer, in);
|
communicate(writer, in);
|
||||||
} catch (SocketException e) {
|
} catch (SocketException e) {
|
||||||
LOGGER.log(Level.INFO, "Socket closed", e); //$NON-NLS-1$
|
LOGGER.log(Level.INFO, "Socket closed"); //$NON-NLS-1$
|
||||||
|
LOGGER.log(Level.FINE,
|
||||||
|
"Socket closed with exception (probably due to server interruption)", //$NON-NLS-1$
|
||||||
|
e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,8 +57,10 @@ public class ThreadedServerConsoleManager implements ConsoleManager {
|
|||||||
/** The class logger */
|
/** The class logger */
|
||||||
private static final Logger LOGGER = Logger
|
private static final Logger LOGGER = Logger
|
||||||
.getLogger(ThreadedServerConsoleManager.class.getName());
|
.getLogger(ThreadedServerConsoleManager.class.getName());
|
||||||
|
/** The empty string constant */
|
||||||
|
private static final String EMPTY = ""; //$NON-NLS-1$
|
||||||
/** The prompting sequence */
|
/** The prompting sequence */
|
||||||
private String prompt = new String();
|
private String prompt = EMPTY;
|
||||||
/** The buffer of data to send to the user */
|
/** The buffer of data to send to the user */
|
||||||
private StringBuilder buffer = new StringBuilder();
|
private StringBuilder buffer = new StringBuilder();
|
||||||
/** The synchronized object */
|
/** The synchronized object */
|
||||||
@@ -105,7 +107,7 @@ public class ThreadedServerConsoleManager implements ConsoleManager {
|
|||||||
@Override
|
@Override
|
||||||
public String prompt(String message) {
|
public String prompt(String message) {
|
||||||
buffer.append(message);
|
buffer.append(message);
|
||||||
String userInput = new String();
|
String userInput = EMPTY;
|
||||||
boolean prompting = true;
|
boolean prompting = true;
|
||||||
while (prompting) {
|
while (prompting) {
|
||||||
// Send buffer content
|
// Send buffer content
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ public class ConsoleRunnableTest {
|
|||||||
int i = 0;
|
int i = 0;
|
||||||
String[] cmds;
|
String[] cmds;
|
||||||
|
|
||||||
/** @param cmds */
|
/** @param cmds the commands to run */
|
||||||
public ConsoleManagerTestImplementation(String[] cmds) {
|
public ConsoleManagerTestImplementation(String[] cmds) {
|
||||||
super();
|
super();
|
||||||
this.cmds = cmds;
|
this.cmds = cmds;
|
||||||
|
|||||||
@@ -72,7 +72,8 @@ public class ConsoleTestApplication extends ConsoleApplication {
|
|||||||
try {
|
try {
|
||||||
manager.println("Test command ran fine");
|
manager.println("Test command ran fine");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new CommandRunException("manager closed", e);
|
throw new CommandRunException("manager closed", e,
|
||||||
|
this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -89,7 +90,8 @@ public class ConsoleTestApplication extends ConsoleApplication {
|
|||||||
Thread.sleep(2000);
|
Thread.sleep(2000);
|
||||||
manager.println("Test command ran fine");
|
manager.println("Test command ran fine");
|
||||||
} catch (IOException | InterruptedException e) {
|
} catch (IOException | InterruptedException e) {
|
||||||
throw new CommandRunException("manager closed", e);
|
throw new CommandRunException("manager closed", e,
|
||||||
|
this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -34,6 +34,8 @@
|
|||||||
*/
|
*/
|
||||||
package fr.bigeon.gclc.socket;
|
package fr.bigeon.gclc.socket;
|
||||||
|
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
|
||||||
/** A test server
|
/** A test server
|
||||||
*
|
*
|
||||||
* @author Emmanuel Bigeon */
|
* @author Emmanuel Bigeon */
|
||||||
@@ -60,11 +62,10 @@ public class TestServer {
|
|||||||
return server;
|
return server;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @return */
|
|
||||||
private static SocketConsoleApplicationShell getShell() {
|
private static SocketConsoleApplicationShell getShell() {
|
||||||
if (SHELL == null) {
|
if (SHELL == null) {
|
||||||
SHELL = new SocketConsoleApplicationShell(3300, "close",
|
SHELL = new SocketConsoleApplicationShell(3300, "close",
|
||||||
ConsoleTestApplication.EXIT);
|
ConsoleTestApplication.EXIT, Charset.forName("UTF-8"));
|
||||||
final ConsoleTestApplication app = new ConsoleTestApplication(
|
final ConsoleTestApplication app = new ConsoleTestApplication(
|
||||||
SHELL.getConsoleManager());
|
SHELL.getConsoleManager());
|
||||||
SHELL.setApplication(app);
|
SHELL.setApplication(app);
|
||||||
@@ -75,7 +76,7 @@ public class TestServer {
|
|||||||
public static Thread startServer(boolean autoClose) {
|
public static Thread startServer(boolean autoClose) {
|
||||||
if (SHELL == null) {
|
if (SHELL == null) {
|
||||||
SHELL = new SocketConsoleApplicationShell(3300, autoClose,
|
SHELL = new SocketConsoleApplicationShell(3300, autoClose,
|
||||||
ConsoleTestApplication.EXIT);
|
ConsoleTestApplication.EXIT, Charset.forName("UTF-8"));
|
||||||
final ConsoleTestApplication app = new ConsoleTestApplication(
|
final ConsoleTestApplication app = new ConsoleTestApplication(
|
||||||
SHELL.getConsoleManager());
|
SHELL.getConsoleManager());
|
||||||
SHELL.setApplication(app);
|
SHELL.setApplication(app);
|
||||||
|
|||||||
@@ -32,10 +32,12 @@
|
|||||||
<!-- The fact that you are presently reading this means that you have had -->
|
<!-- The fact that you are presently reading this means that you have had -->
|
||||||
<!-- knowledge of the CeCILL license and that you accept its terms. -->
|
<!-- knowledge of the CeCILL license and that you accept its terms. -->
|
||||||
|
|
||||||
<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-swt</artifactId>
|
<artifactId>gclc-swt</artifactId>
|
||||||
<version>1.0.3-SNAPSHOT</version>
|
<version>1.0.5-SNAPSHOT</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
<url>http://www.bigeon.fr/emmanuel</url>
|
<url>http://www.bigeon.fr/emmanuel</url>
|
||||||
<properties>
|
<properties>
|
||||||
@@ -45,23 +47,13 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>fr.bigeon</groupId>
|
<groupId>fr.bigeon</groupId>
|
||||||
<artifactId>ebigeon-config</artifactId>
|
<artifactId>ebigeon-config</artifactId>
|
||||||
<version>1.6.0</version>
|
<version>1.7.1</version>
|
||||||
</parent>
|
</parent>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>fr.bigeon</groupId>
|
<groupId>fr.bigeon</groupId>
|
||||||
<artifactId>gclc</artifactId>
|
<artifactId>gclc</artifactId>
|
||||||
<version>1.2.0</version>
|
<version>1.2.3</version>
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.eclipse.swt</groupId>
|
|
||||||
<artifactId>org.eclipse.swt.gtk.linux.x86_64</artifactId>
|
|
||||||
<version>4.3</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.eclipse.swt</groupId>
|
|
||||||
<artifactId>org.eclipse.swt.win32.win32.x86_64</artifactId>
|
|
||||||
<version>4.3</version>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>fr.bigeon</groupId>
|
<groupId>fr.bigeon</groupId>
|
||||||
@@ -76,4 +68,36 @@
|
|||||||
<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>HEAD</tag>
|
<tag>HEAD</tag>
|
||||||
</scm>
|
</scm>
|
||||||
|
<profiles>
|
||||||
|
<profile>
|
||||||
|
<id>linux-deps</id>
|
||||||
|
<activation>
|
||||||
|
<os>
|
||||||
|
<family>Linux</family>
|
||||||
|
</os>
|
||||||
|
</activation>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.swt</groupId>
|
||||||
|
<artifactId>org.eclipse.swt.gtk.linux.x86_64</artifactId>
|
||||||
|
<version>4.3</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</profile>
|
||||||
|
<profile>
|
||||||
|
<id>windows-deps</id>
|
||||||
|
<activation>
|
||||||
|
<os>
|
||||||
|
<family>Windows</family>
|
||||||
|
</os>
|
||||||
|
</activation>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.swt</groupId>
|
||||||
|
<artifactId>org.eclipse.swt.win32.win32.x86_64</artifactId>
|
||||||
|
<version>4.3</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</profile>
|
||||||
|
</profiles>
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@@ -0,0 +1,59 @@
|
|||||||
|
/*
|
||||||
|
* Copyright E. Bigeon (2015)
|
||||||
|
*
|
||||||
|
* emmanuel@bigeon.fr
|
||||||
|
*
|
||||||
|
* This software is a computer program whose purpose is to
|
||||||
|
* provide a swt window 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-swt:fr.bigeon.gclc.swt.ConsoleDelayIO.java
|
||||||
|
* Created on: Nov 19, 2016
|
||||||
|
*/
|
||||||
|
package fr.bigeon.gclc.swt;
|
||||||
|
|
||||||
|
import fr.bigeon.gclc.manager.ConsoleManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* TODO
|
||||||
|
*
|
||||||
|
* @author Emmanuel Bigeon
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public interface ConsoleDelayIO extends ConsoleManager {
|
||||||
|
/** Actually send the input as the prompt next input. */
|
||||||
|
void validateInput();
|
||||||
|
|
||||||
|
/** @param input the input to set */
|
||||||
|
void setInput(String input);
|
||||||
|
|
||||||
|
/** @return the non validated input */
|
||||||
|
String getInput();
|
||||||
|
}
|
||||||
@@ -0,0 +1,109 @@
|
|||||||
|
/*
|
||||||
|
* Copyright E. Bigeon (2015)
|
||||||
|
*
|
||||||
|
* emmanuel@bigeon.fr
|
||||||
|
*
|
||||||
|
* This software is a computer program whose purpose is to
|
||||||
|
* provide a swt window 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-swt:fr.bigeon.gclc.swt.HistoryTextKeyListener.java
|
||||||
|
* Created on: Jun 9, 2016
|
||||||
|
*/
|
||||||
|
package fr.bigeon.gclc.swt;
|
||||||
|
|
||||||
|
import org.eclipse.swt.SWT;
|
||||||
|
import org.eclipse.swt.events.KeyAdapter;
|
||||||
|
import org.eclipse.swt.events.KeyEvent;
|
||||||
|
|
||||||
|
import fr.bigeon.collections.ArrayRibbon;
|
||||||
|
import fr.bigeon.collections.Ribbon;
|
||||||
|
|
||||||
|
/** A key listener to validate commands and manage the history of commands
|
||||||
|
*
|
||||||
|
* @author Emmanuel Bigeon */
|
||||||
|
public final class HistoryTextKeyListener extends KeyAdapter {
|
||||||
|
|
||||||
|
/** The size of commands history */
|
||||||
|
private static final int DEFAULT_HISTORY_SIZE = 10;
|
||||||
|
/** The empty string constant */
|
||||||
|
private static final String EMPTY = ""; //$NON-NLS-1$
|
||||||
|
/** The history ribbon */
|
||||||
|
private final Ribbon<String> commands;
|
||||||
|
/** The current index in history search */
|
||||||
|
private int currentIndex = 0;
|
||||||
|
/** The console to notify of command validation */
|
||||||
|
private final ConsoleDelayIO console;
|
||||||
|
|
||||||
|
/** @param console the console delayed */
|
||||||
|
public HistoryTextKeyListener(ConsoleDelayIO console) {
|
||||||
|
super();
|
||||||
|
this.console = console;
|
||||||
|
this.commands = new ArrayRibbon<>(DEFAULT_HISTORY_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void keyPressed(KeyEvent e) {
|
||||||
|
|
||||||
|
pressedKeyCode(e.keyCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @param keyCode the pressed key code */
|
||||||
|
public void pressedKeyCode(int keyCode) {
|
||||||
|
// Enter validates the command if prompting
|
||||||
|
if (keyCode == '\r') {
|
||||||
|
String input = console.getInput();
|
||||||
|
if (!input.isEmpty()) {
|
||||||
|
commands.add(input);
|
||||||
|
}
|
||||||
|
console.validateInput();
|
||||||
|
currentIndex = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Upper arrow retrieves previous commands
|
||||||
|
if (keyCode == SWT.ARROW_UP &&
|
||||||
|
currentIndex < commands.size() - 1) {
|
||||||
|
currentIndex++;
|
||||||
|
String cmd = commands.get(commands.size() - currentIndex - 1);
|
||||||
|
console.setInput(cmd);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lower arrow retrieves next commands
|
||||||
|
if (keyCode == SWT.ARROW_DOWN) {
|
||||||
|
if (currentIndex == 0) {
|
||||||
|
currentIndex--;
|
||||||
|
console.setInput(EMPTY);
|
||||||
|
} else if (currentIndex > 0) {
|
||||||
|
String cmd = commands
|
||||||
|
.get(commands.size() - (--currentIndex) - 1);
|
||||||
|
console.setInput(cmd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -39,12 +39,12 @@
|
|||||||
package fr.bigeon.gclc.swt;
|
package fr.bigeon.gclc.swt;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import org.eclipse.swt.SWT;
|
import org.eclipse.swt.SWT;
|
||||||
import org.eclipse.swt.events.FocusAdapter;
|
import org.eclipse.swt.events.FocusAdapter;
|
||||||
import org.eclipse.swt.events.FocusEvent;
|
import org.eclipse.swt.events.FocusEvent;
|
||||||
import org.eclipse.swt.events.KeyAdapter;
|
|
||||||
import org.eclipse.swt.events.KeyEvent;
|
|
||||||
import org.eclipse.swt.layout.GridData;
|
import org.eclipse.swt.layout.GridData;
|
||||||
import org.eclipse.swt.layout.GridLayout;
|
import org.eclipse.swt.layout.GridLayout;
|
||||||
import org.eclipse.swt.widgets.Composite;
|
import org.eclipse.swt.widgets.Composite;
|
||||||
@@ -52,80 +52,24 @@ import org.eclipse.swt.widgets.Display;
|
|||||||
import org.eclipse.swt.widgets.Label;
|
import org.eclipse.swt.widgets.Label;
|
||||||
import org.eclipse.swt.widgets.Text;
|
import org.eclipse.swt.widgets.Text;
|
||||||
|
|
||||||
import fr.bigeon.collections.ArrayRibbon;
|
|
||||||
import fr.bigeon.collections.Ribbon;
|
|
||||||
import fr.bigeon.gclc.ConsoleApplication;
|
import fr.bigeon.gclc.ConsoleApplication;
|
||||||
import fr.bigeon.gclc.manager.ConsoleManager;
|
|
||||||
|
|
||||||
/** A SWT component to connect to gclc {@link ConsoleApplication}
|
/** A SWT component to connect to gclc {@link ConsoleApplication}
|
||||||
* <p>
|
* <p>
|
||||||
*
|
*
|
||||||
* @author Emmanuel Bigeon */
|
* @author Emmanuel Bigeon */
|
||||||
public class SWTConsole extends Composite implements ConsoleManager {
|
public class SWTConsole extends Composite implements ConsoleDelayIO {
|
||||||
/** A key listener to validate commands and manage the history of commands
|
|
||||||
*
|
|
||||||
* @author Emmanuel Bigeon */
|
|
||||||
public static final class HistoryTextKeyListener extends KeyAdapter {
|
|
||||||
|
|
||||||
/** The size of commands history */
|
|
||||||
private static final int DEFAULT_HISTORY_SIZE = 10;
|
|
||||||
/** The history ribbon */
|
|
||||||
private final Ribbon<String> commands;
|
|
||||||
/** The current index in history search */
|
|
||||||
private int currentIndex = 0;
|
|
||||||
/** The console to write the commands in */
|
|
||||||
private final Text consoleInput;
|
|
||||||
/** The console to notify of command validation */
|
|
||||||
private final SWTConsole console;
|
|
||||||
|
|
||||||
/** @param console the console
|
|
||||||
* @param consoleInput the text to write commands in */
|
|
||||||
public HistoryTextKeyListener(SWTConsole console, Text consoleInput) {
|
|
||||||
super();
|
|
||||||
this.console = console;
|
|
||||||
this.consoleInput = consoleInput;
|
|
||||||
this.commands = new ArrayRibbon<>(DEFAULT_HISTORY_SIZE);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void keyPressed(KeyEvent e) {
|
|
||||||
// Enter validates the command if prompting
|
|
||||||
if (e.keyCode == '\r') {
|
|
||||||
commands.add(consoleInput.getText());
|
|
||||||
console.validateInput();
|
|
||||||
currentIndex = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Upper arrow retrieves previous commands
|
|
||||||
if (e.keyCode == SWT.ARROW_UP &&
|
|
||||||
currentIndex < commands.size() - 1) {
|
|
||||||
currentIndex++;
|
|
||||||
String cmd = commands.get(commands.size() - currentIndex - 1);
|
|
||||||
consoleInput.setText(cmd);
|
|
||||||
consoleInput.setSelection(cmd.length());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Lower arrow retrieves next commands
|
|
||||||
if (e.keyCode == SWT.ARROW_DOWN) {
|
|
||||||
if (currentIndex == 0) {
|
|
||||||
currentIndex--;
|
|
||||||
consoleInput.setText(new String());
|
|
||||||
} else if (currentIndex > 0) {
|
|
||||||
String cmd = commands
|
|
||||||
.get(commands.size() - (--currentIndex) - 1);
|
|
||||||
consoleInput.setText(cmd);
|
|
||||||
consoleInput.setSelection(cmd.length());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
private static final int LAYOUT_NB_COLUMNS = 2;
|
private static final int LAYOUT_NB_COLUMNS = 2;
|
||||||
/** The cmd prefix in the output console */
|
/** The cmd prefix in the output console */
|
||||||
private static final String CMD_PREFIX = "[CMD] "; //$NON-NLS-1$
|
private static final String CMD_PREFIX = "[CMD] "; //$NON-NLS-1$
|
||||||
|
/** The class logger */
|
||||||
|
private static final Logger LOGGER = Logger
|
||||||
|
.getLogger(SWTConsole.class.getName());
|
||||||
|
/** The empty string constant */
|
||||||
|
private static final String EMPTY = ""; //$NON-NLS-1$
|
||||||
/** The console output text field */
|
/** The console output text field */
|
||||||
private final Text consoleOutput;
|
private final Text consoleOutput;
|
||||||
/** The console input text field */
|
/** The console input text field */
|
||||||
@@ -169,25 +113,43 @@ public class SWTConsole extends Composite implements ConsoleManager {
|
|||||||
consoleInput = new Text(this, SWT.BORDER);
|
consoleInput = new Text(this, SWT.BORDER);
|
||||||
consoleInput.setLayoutData(
|
consoleInput.setLayoutData(
|
||||||
new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
|
new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
|
||||||
consoleInput.addKeyListener(
|
consoleInput.addKeyListener(new HistoryTextKeyListener(this));
|
||||||
new HistoryTextKeyListener(this, consoleInput));
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
protected void validateInput() {
|
@Override
|
||||||
if (prompting) {
|
public void validateInput() {
|
||||||
synchronized (promptLock) {
|
Display.getDefault().syncExec(new Runnable() {
|
||||||
command = consoleInput.getText();
|
@SuppressWarnings("synthetic-access")
|
||||||
prompting = false;
|
@Override
|
||||||
consoleInput.setText(new String());
|
public void run() {
|
||||||
consoleInput.setEnabled(false);
|
consoleInput.setEnabled(false);
|
||||||
consoleOutput
|
|
||||||
.append(CMD_PREFIX + command + System.lineSeparator());
|
|
||||||
promptLock.notifyAll();
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
synchronized (promptLock) {
|
||||||
|
while (!prompting) {
|
||||||
|
try {
|
||||||
|
promptLock.wait();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
LOGGER.log(Level.SEVERE,
|
||||||
|
"Interruption while waiting prompt", e); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Display.getDefault().syncExec(new Runnable() {
|
||||||
|
@SuppressWarnings("synthetic-access")
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
command = consoleInput.getText();
|
||||||
|
prompting = false;
|
||||||
|
consoleInput.setText(EMPTY);
|
||||||
|
consoleOutput.append(
|
||||||
|
CMD_PREFIX + command + System.lineSeparator());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
promptLock.notifyAll();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -261,11 +223,17 @@ public class SWTConsole extends Composite implements ConsoleManager {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
prompting = true;
|
prompting = true;
|
||||||
|
promptLock.notifyAll();
|
||||||
promptLock.wait();
|
promptLock.wait();
|
||||||
} catch (final InterruptedException e) {
|
} catch (final InterruptedException e) {
|
||||||
|
LOGGER.log(Level.WARNING,
|
||||||
|
"Error in synchronization of prompting", e); //$NON-NLS-1$
|
||||||
command = null;
|
command = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (isDisposed()) {
|
||||||
|
throw new IOException("Input closed"); //$NON-NLS-1$
|
||||||
|
}
|
||||||
return command;
|
return command;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -295,6 +263,8 @@ public class SWTConsole extends Composite implements ConsoleManager {
|
|||||||
throw new IOException();
|
throw new IOException();
|
||||||
}
|
}
|
||||||
} catch (final InterruptedException e) {
|
} catch (final InterruptedException e) {
|
||||||
|
LOGGER.log(Level.WARNING,
|
||||||
|
"Error in synchronization of prompting", e); //$NON-NLS-1$
|
||||||
command = null;
|
command = null;
|
||||||
} finally {
|
} finally {
|
||||||
Display.getDefault().syncExec(new Runnable() {
|
Display.getDefault().syncExec(new Runnable() {
|
||||||
@@ -355,4 +325,31 @@ public class SWTConsole extends Composite implements ConsoleManager {
|
|||||||
return isDisposed();
|
return isDisposed();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @param string the text */
|
||||||
|
public void setText(String string) {
|
||||||
|
consoleInput.setText(string);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public void validateCommand() {
|
||||||
|
validateInput();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see fr.bigeon.gclc.swt.ConsoleDelayIO#setInput(java.lang.String) */
|
||||||
|
@Override
|
||||||
|
public void setInput(String input) {
|
||||||
|
consoleInput.setText(input);
|
||||||
|
consoleInput.setSelection(input.length());
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see fr.bigeon.gclc.swt.ConsoleDelayIO#getInput() */
|
||||||
|
@Override
|
||||||
|
public String getInput() {
|
||||||
|
return consoleInput.getText();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,158 @@
|
|||||||
|
/*
|
||||||
|
* Copyright E. Bigeon (2015)
|
||||||
|
*
|
||||||
|
* emmanuel@bigeon.fr
|
||||||
|
*
|
||||||
|
* This software is a computer program whose purpose is to
|
||||||
|
* provide a swt window 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-swt:fr.bigeon.gclc.swt.HistoryTextKeyListenerTest.java
|
||||||
|
* Created on: Nov 19, 2016
|
||||||
|
*/
|
||||||
|
package fr.bigeon.gclc.swt;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.eclipse.swt.SWT;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/** <p>
|
||||||
|
* TODO
|
||||||
|
*
|
||||||
|
* @author Emmanuel Bigeon */
|
||||||
|
public class HistoryTextKeyListenerTest {
|
||||||
|
|
||||||
|
/** Test method for
|
||||||
|
* {@link fr.bigeon.gclc.swt.HistoryTextKeyListener#keyPressed(org.eclipse.swt.events.KeyEvent)}. */
|
||||||
|
@Test
|
||||||
|
public final void testKeyPressedKeyEvent() {
|
||||||
|
ConsoleDelayIO io = new ConsoleDelayIO() {
|
||||||
|
private String input = "";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setPrompt(String prompt) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String prompt(String message) throws IOException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String prompt() throws IOException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void println(String message) throws IOException {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void println() throws IOException {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void print(String text) throws IOException {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isClosed() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getPrompt() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() throws IOException {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void validateInput() {
|
||||||
|
input = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setInput(String input) {
|
||||||
|
this.input = input;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getInput() {
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
HistoryTextKeyListener listener = new HistoryTextKeyListener(io);
|
||||||
|
|
||||||
|
// no effects
|
||||||
|
assertEquals("", io.getInput());
|
||||||
|
listener.pressedKeyCode(SWT.ARROW_UP);
|
||||||
|
assertEquals("", io.getInput());
|
||||||
|
listener.pressedKeyCode(SWT.ARROW_DOWN);
|
||||||
|
assertEquals("", io.getInput());
|
||||||
|
listener.pressedKeyCode('\r');
|
||||||
|
assertEquals("", io.getInput());
|
||||||
|
|
||||||
|
io.setInput("cmd arg");
|
||||||
|
listener.pressedKeyCode(SWT.ARROW_UP);
|
||||||
|
assertEquals("cmd arg", io.getInput());
|
||||||
|
listener.pressedKeyCode(SWT.ARROW_DOWN);
|
||||||
|
assertEquals("cmd arg", io.getInput());
|
||||||
|
listener.pressedKeyCode('\r');
|
||||||
|
assertEquals("", io.getInput());
|
||||||
|
|
||||||
|
io.setInput("cmd arg2");
|
||||||
|
listener.pressedKeyCode(SWT.ARROW_UP);
|
||||||
|
assertEquals("cmd arg", io.getInput());
|
||||||
|
listener.pressedKeyCode(SWT.ARROW_DOWN);
|
||||||
|
assertEquals("", io.getInput());
|
||||||
|
io.setInput("cmd arg2");
|
||||||
|
listener.pressedKeyCode('\r');
|
||||||
|
assertEquals("", io.getInput());
|
||||||
|
listener.pressedKeyCode(SWT.ARROW_UP);
|
||||||
|
assertEquals("cmd arg2", io.getInput());
|
||||||
|
listener.pressedKeyCode(SWT.ARROW_UP);
|
||||||
|
assertEquals("cmd arg", io.getInput());
|
||||||
|
listener.pressedKeyCode(SWT.ARROW_UP);
|
||||||
|
assertEquals("cmd arg", io.getInput());
|
||||||
|
listener.pressedKeyCode(SWT.ARROW_DOWN);
|
||||||
|
assertEquals("cmd arg2", io.getInput());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -41,30 +41,160 @@ package fr.bigeon.gclc.swt;
|
|||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.eclipse.swt.widgets.Display;
|
import org.eclipse.swt.widgets.Display;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import fr.bigeon.gclc.ConsoleApplication;
|
import fr.bigeon.gclc.ConsoleApplication;
|
||||||
import fr.bigeon.gclc.command.Command;
|
import fr.bigeon.gclc.command.Command;
|
||||||
|
import fr.bigeon.gclc.exception.CommandRunException;
|
||||||
import fr.bigeon.gclc.exception.InvalidCommandName;
|
import fr.bigeon.gclc.exception.InvalidCommandName;
|
||||||
import fr.bigeon.gclc.manager.ConsoleManager;
|
|
||||||
|
|
||||||
/**
|
/** <p>
|
||||||
* <p>
|
|
||||||
* TODO
|
* TODO
|
||||||
*
|
*
|
||||||
* @author Emmanuel Bigeon
|
* @author Emmanuel Bigeon */
|
||||||
*
|
@SuppressWarnings({"javadoc", "static-method", "nls", "deprecation"})
|
||||||
*/
|
|
||||||
public class SWTConsoleShellTest {
|
public class SWTConsoleShellTest {
|
||||||
|
|
||||||
protected static final long TWO_SECONDS = 2000;
|
protected static final long TWO_SECONDS = 2000;
|
||||||
|
private static final Display DISPLAY = new Display();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConsoleClose() {
|
||||||
|
final SWTConsoleShell shell = new SWTConsoleShell(DISPLAY);
|
||||||
|
final SWTConsole swtConsole = (SWTConsole) shell.getManager();
|
||||||
|
swtConsole.close();
|
||||||
|
swtConsole.setPrompt(":");
|
||||||
|
try {
|
||||||
|
final ConsoleApplication appl = new ConsoleApplication(swtConsole,
|
||||||
|
"exit", "Hello", "See you");
|
||||||
|
appl.add(new Command("long") {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String tip() {
|
||||||
|
return "a long running command";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute(String... args) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(TWO_SECONDS);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
appl.add(new Command("test") {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String tip() {
|
||||||
|
return "a prompting running command";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute(String... args) throws CommandRunException {
|
||||||
|
try {
|
||||||
|
appl.getManager().prompt("Test");
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new CommandRunException("No input", e, this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// shell.pack();
|
||||||
|
shell.open();
|
||||||
|
Thread applThread = new Thread(new Runnable() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
appl.start();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
Thread testThread = new Thread(new Runnable() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
Thread.sleep(TWO_SECONDS);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
Display.getDefault().syncExec(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
swtConsole.setText("test"); //$NON-NLS-1$
|
||||||
|
swtConsole.validateCommand();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
Display.getDefault().syncExec(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
swtConsole.setText("ok"); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
});
|
||||||
|
swtConsole.validateCommand();
|
||||||
|
Display.getDefault().syncExec(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
swtConsole.setText("long"); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
});
|
||||||
|
swtConsole.validateCommand();
|
||||||
|
Display.getDefault().syncExec(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
swtConsole.setText("test"); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
});
|
||||||
|
swtConsole.validateCommand();
|
||||||
|
Display.getDefault().syncExec(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
swtConsole.setText("test"); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
});
|
||||||
|
swtConsole.validateCommand();
|
||||||
|
try {
|
||||||
|
Thread.sleep(TWO_SECONDS);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
Display.getDefault().syncExec(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
shell.dispose();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
applThread.start();
|
||||||
|
testThread.start();
|
||||||
|
while (!shell.isDisposed()) {
|
||||||
|
if (!DISPLAY.readAndDispatch()) {
|
||||||
|
DISPLAY.sleep();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// DISPLAY.dispose();
|
||||||
|
assertTrue(appl.getManager().isClosed());
|
||||||
|
Thread.sleep(TWO_SECONDS);
|
||||||
|
assertFalse(appl.isRunning());
|
||||||
|
} catch (InvalidCommandName e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test() {
|
public void test() {
|
||||||
Display display = new Display();
|
final SWTConsoleShell shell = new SWTConsoleShell(DISPLAY);
|
||||||
final SWTConsoleShell shell = new SWTConsoleShell(display);
|
final SWTConsole swtConsole = (SWTConsole) shell.getManager();
|
||||||
ConsoleManager swtConsole = shell.getManager();
|
|
||||||
try {
|
try {
|
||||||
final ConsoleApplication appl = new ConsoleApplication(swtConsole,
|
final ConsoleApplication appl = new ConsoleApplication(swtConsole,
|
||||||
"exit", "Hello", "See you");
|
"exit", "Hello", "See you");
|
||||||
@@ -98,6 +228,19 @@ public class SWTConsoleShellTest {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
try {
|
||||||
|
Thread.sleep(TWO_SECONDS);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
Display.getDefault().syncExec(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
swtConsole.setText("test"); //$NON-NLS-1$
|
||||||
|
swtConsole.validateCommand();
|
||||||
|
}
|
||||||
|
});
|
||||||
try {
|
try {
|
||||||
Thread.sleep(TWO_SECONDS);
|
Thread.sleep(TWO_SECONDS);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
@@ -115,11 +258,11 @@ public class SWTConsoleShellTest {
|
|||||||
applThread.start();
|
applThread.start();
|
||||||
testThread.start();
|
testThread.start();
|
||||||
while (!shell.isDisposed()) {
|
while (!shell.isDisposed()) {
|
||||||
if (!display.readAndDispatch()) {
|
if (!DISPLAY.readAndDispatch()) {
|
||||||
display.sleep();
|
DISPLAY.sleep();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// display.dispose();
|
// DISPLAY.dispose();
|
||||||
assertTrue(appl.getManager().isClosed());
|
assertTrue(appl.getManager().isClosed());
|
||||||
Thread.sleep(TWO_SECONDS);
|
Thread.sleep(TWO_SECONDS);
|
||||||
assertFalse(appl.isRunning());
|
assertFalse(appl.isRunning());
|
||||||
|
|||||||
4
gclc.system/.gitignore
vendored
Normal file
4
gclc.system/.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
/target/
|
||||||
|
/.classpath
|
||||||
|
/.project
|
||||||
|
/.settings/
|
||||||
41
gclc.system/pom.xml
Normal file
41
gclc.system/pom.xml
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
<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>
|
||||||
|
<groupId>net.bigeon</groupId>
|
||||||
|
<artifactId>gclc.system</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<url>http://www.bigeon.net</url>
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.11</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>fr.bigeon</groupId>
|
||||||
|
<artifactId>gclc</artifactId>
|
||||||
|
<version>1.2.5</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<name>GCLC system command</name>
|
||||||
|
<description>Provide an exec command to execute system commands</description>
|
||||||
|
<inceptionYear>2016</inceptionYear>
|
||||||
|
<scm>
|
||||||
|
<developerConnection>scm:git:gogs@git.code.bigeon.net:emmanuel/gclc.git</developerConnection>
|
||||||
|
<tag>HEAD</tag>
|
||||||
|
</scm>
|
||||||
|
<parent>
|
||||||
|
<groupId>fr.bigeon</groupId>
|
||||||
|
<artifactId>ebigeon-config</artifactId>
|
||||||
|
<version>1.7.1</version>
|
||||||
|
</parent>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,150 @@
|
|||||||
|
/**
|
||||||
|
* gclc.system:net.bigeon.gclc.system.ExecSystemCommand.java
|
||||||
|
* Created on: Jun 20, 2016
|
||||||
|
*/
|
||||||
|
package net.bigeon.gclc.system;
|
||||||
|
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.io.OutputStreamWriter;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import fr.bigeon.gclc.command.Command;
|
||||||
|
import fr.bigeon.gclc.exception.CommandRunException;
|
||||||
|
import fr.bigeon.gclc.exception.CommandRunExceptionType;
|
||||||
|
import fr.bigeon.gclc.manager.ConsoleManager;
|
||||||
|
|
||||||
|
/** <p>
|
||||||
|
* TODO
|
||||||
|
*
|
||||||
|
* @author Emmanuel Bigeon */
|
||||||
|
public class ExecSystemCommand extends Command {
|
||||||
|
|
||||||
|
/** The command default name */
|
||||||
|
private static final String COMMAND_DEFAULT_NAME = "exec"; //$NON-NLS-1$
|
||||||
|
/** The end of line separator */
|
||||||
|
private static final String EOL = System.lineSeparator();
|
||||||
|
/** The class logger */
|
||||||
|
private static final Logger LOGGER = Logger
|
||||||
|
.getLogger(ExecSystemCommand.class.getName());
|
||||||
|
/** The manager for the application's user interface */
|
||||||
|
private final ConsoleManager manager;
|
||||||
|
|
||||||
|
/** @param name the name of the command (the input from the manager that
|
||||||
|
* should trigger this command)
|
||||||
|
* @param manager the console manager for input and outputs */
|
||||||
|
public ExecSystemCommand(String name, ConsoleManager manager) {
|
||||||
|
super(name);
|
||||||
|
this.manager = manager;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @param manager the console manager for input and outputs */
|
||||||
|
public ExecSystemCommand(ConsoleManager manager) {
|
||||||
|
super(COMMAND_DEFAULT_NAME);
|
||||||
|
this.manager = manager;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see fr.bigeon.gclc.command.ICommand#execute(java.lang.String[]) */
|
||||||
|
@SuppressWarnings("resource")
|
||||||
|
@Override
|
||||||
|
public void execute(String... args) throws CommandRunException {
|
||||||
|
Process proc;
|
||||||
|
try {
|
||||||
|
proc = Runtime.getRuntime().exec(args);
|
||||||
|
} catch (IOException e2) {
|
||||||
|
LOGGER.log(Level.SEVERE, "Unable to run process", e2); //$NON-NLS-1$
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final InputStream is = proc
|
||||||
|
.getInputStream();
|
||||||
|
Thread th = new Thread(new Runnable() {
|
||||||
|
|
||||||
|
@SuppressWarnings("synthetic-access")
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
readToEnd(is);
|
||||||
|
is.close();
|
||||||
|
} catch (CommandRunException e) {
|
||||||
|
LOGGER.log(Level.WARNING,
|
||||||
|
"Manager was closed in the meantime...", e); //$NON-NLS-1$
|
||||||
|
} catch (IOException e) {
|
||||||
|
LOGGER.log(Level.WARNING, "Input stream was closed...", e); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
th.start();
|
||||||
|
manager.setPrompt("");
|
||||||
|
final OutputStream os = proc.getOutputStream();
|
||||||
|
try (BufferedWriter writer = new BufferedWriter(
|
||||||
|
new OutputStreamWriter(os))) {
|
||||||
|
while (th.isAlive()) {
|
||||||
|
String user;
|
||||||
|
try {
|
||||||
|
user = manager.prompt();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new CommandRunException(
|
||||||
|
CommandRunExceptionType.INTERACTION,
|
||||||
|
"manager was closed", e, this); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
writer.write(user + EOL);
|
||||||
|
}
|
||||||
|
} catch (IOException e1) {
|
||||||
|
throw new CommandRunException(CommandRunExceptionType.INTERACTION,
|
||||||
|
"manager was closed", e1, this); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @param is the input stream
|
||||||
|
* @throws CommandRunException if the manager was closed while writing the
|
||||||
|
* stream */
|
||||||
|
protected void readToEnd(InputStream is) throws CommandRunException {
|
||||||
|
int c;
|
||||||
|
try {
|
||||||
|
while ((c = is.read()) != -1) {
|
||||||
|
try {
|
||||||
|
manager.print(Character.valueOf((char) c).toString());
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new CommandRunException(
|
||||||
|
CommandRunExceptionType.INTERACTION,
|
||||||
|
"manager was closed", e, this); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
LOGGER.log(Level.INFO, "input stream reading failed", e); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see fr.bigeon.gclc.command.Command#usageDetail() */
|
||||||
|
@Override
|
||||||
|
protected String usageDetail() {
|
||||||
|
return " The system command is a system dependend command like sh on linux or" + //$NON-NLS-1$
|
||||||
|
System.lineSeparator() + "powershell on windows." + //$NON-NLS-1$
|
||||||
|
System.lineSeparator() + System.lineSeparator() +
|
||||||
|
" As an example if you give \"cat /etc/hostname\" as argument, on a linux" + //$NON-NLS-1$
|
||||||
|
System.lineSeparator() +
|
||||||
|
"system, you would get the computer name." + //$NON-NLS-1$
|
||||||
|
System.lineSeparator();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see fr.bigeon.gclc.command.Command#usagePattern() */
|
||||||
|
@Override
|
||||||
|
protected String usagePattern() {
|
||||||
|
return " CMD <system command>"; //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see fr.bigeon.gclc.command.ICommand#tip() */
|
||||||
|
@Override
|
||||||
|
public String tip() {
|
||||||
|
return "Execute a system command"; //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,3 +1,11 @@
|
|||||||
|
## Version 1.2.6
|
||||||
|
|
||||||
|
* in script command failure report improved
|
||||||
|
|
||||||
|
### Bug fixes
|
||||||
|
|
||||||
|
* command line reading now succeed to parse last argument if it is a string
|
||||||
|
|
||||||
## Version 1.1.0
|
## Version 1.1.0
|
||||||
|
|
||||||
* Parsing quoted string as one argument
|
* Parsing quoted string as one argument
|
||||||
|
|||||||
@@ -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.1-SNAPSHOT</version>
|
<version>1.2.6</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
<url>http://www.bigeon.fr/emmanuel</url>
|
<url>http://www.bigeon.fr/emmanuel</url>
|
||||||
<properties>
|
<properties>
|
||||||
@@ -53,7 +53,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>fr.bigeon</groupId>
|
<groupId>fr.bigeon</groupId>
|
||||||
<artifactId>ebigeon-config</artifactId>
|
<artifactId>ebigeon-config</artifactId>
|
||||||
<version>1.6.0</version>
|
<version>1.7.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
@@ -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>HEAD</tag>
|
<tag>gclc-1.2.6</tag>
|
||||||
</scm>
|
</scm>
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@@ -48,9 +48,9 @@ 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;
|
||||||
import fr.bigeon.gclc.command.UnrecognizedCommand;
|
|
||||||
import fr.bigeon.gclc.exception.CommandParsingException;
|
import fr.bigeon.gclc.exception.CommandParsingException;
|
||||||
import fr.bigeon.gclc.exception.CommandRunException;
|
import fr.bigeon.gclc.exception.CommandRunException;
|
||||||
|
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;
|
||||||
@@ -100,7 +100,7 @@ public class ConsoleApplication implements ICommandProvider {
|
|||||||
this.header = welcome;
|
this.header = welcome;
|
||||||
this.footer = goodbye;
|
this.footer = goodbye;
|
||||||
this.manager = manager;
|
this.manager = manager;
|
||||||
root = new SubedCommand(new String(), new UnrecognizedCommand(manager));
|
root = new SubedCommand(""); //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @param manager the manager
|
/** @param manager the manager
|
||||||
@@ -208,6 +208,9 @@ public class ConsoleApplication implements ICommandProvider {
|
|||||||
manager.println(Messages
|
manager.println(Messages
|
||||||
.getString("ConsoleApplication.cmd.failed", cmd)); //$NON-NLS-1$
|
.getString("ConsoleApplication.cmd.failed", cmd)); //$NON-NLS-1$
|
||||||
manager.println(e.getLocalizedMessage());
|
manager.println(e.getLocalizedMessage());
|
||||||
|
if (e.getType() == CommandRunExceptionType.USAGE) {
|
||||||
|
e.getSource().help(getManager());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -242,6 +245,7 @@ public class ConsoleApplication implements ICommandProvider {
|
|||||||
if (footer != null) {
|
if (footer != null) {
|
||||||
manager.println(footer);
|
manager.println(footer);
|
||||||
}
|
}
|
||||||
|
running = false;
|
||||||
LOGGER.fine("Exiting application."); //$NON-NLS-1$
|
LOGGER.fine("Exiting application."); //$NON-NLS-1$
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// The manager was closed
|
// The manager was closed
|
||||||
@@ -261,4 +265,14 @@ public class ConsoleApplication implements ICommandProvider {
|
|||||||
public SubedCommand getRoot() {
|
public SubedCommand getRoot() {
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @return the header */
|
||||||
|
public String getHeader() {
|
||||||
|
return header;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return the footer */
|
||||||
|
public String getFooter() {
|
||||||
|
return footer;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,11 +51,19 @@ import fr.bigeon.gclc.exception.CommandParsingException;
|
|||||||
* @author Emmanuel Bigeon */
|
* @author Emmanuel Bigeon */
|
||||||
public class GCLCConstants {
|
public class GCLCConstants {
|
||||||
|
|
||||||
|
/** The escaping character */
|
||||||
|
private static final char ESCAPING_CHAR = getSystemEscapingChar();
|
||||||
|
|
||||||
/** Hide utility class constructor */
|
/** Hide utility class constructor */
|
||||||
private GCLCConstants() {
|
private GCLCConstants() {
|
||||||
// utility class
|
// utility class
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @return the escaping character */
|
||||||
|
private static char getSystemEscapingChar() {
|
||||||
|
return '\\';
|
||||||
|
}
|
||||||
|
|
||||||
/** Splits a command in the diferrent arguments
|
/** Splits a command in the diferrent arguments
|
||||||
*
|
*
|
||||||
* @param cmd the command to split in its parts
|
* @param cmd the command to split in its parts
|
||||||
@@ -71,7 +79,7 @@ public class GCLCConstants {
|
|||||||
while (index < cmd.length()) {
|
while (index < cmd.length()) {
|
||||||
char c = cmd.charAt(index);
|
char c = cmd.charAt(index);
|
||||||
index++;
|
index++;
|
||||||
if (escaped || c == '\\') {
|
if (escaped || c == ESCAPING_CHAR) {
|
||||||
escaped = !escaped;
|
escaped = !escaped;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -90,8 +98,8 @@ public class GCLCConstants {
|
|||||||
inString = startIndex == index - 1;
|
inString = startIndex == index - 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
final String arg = cmd.substring(startIndex, cmd.length());
|
if (startIndex < cmd.length()) {
|
||||||
if (!arg.isEmpty()) {
|
final String arg = cmd.substring(startIndex, cmd.length());
|
||||||
args.add(arg);
|
args.add(arg);
|
||||||
}
|
}
|
||||||
return args;
|
return args;
|
||||||
@@ -102,11 +110,11 @@ public class GCLCConstants {
|
|||||||
private static String removeEscaped(String arg) {
|
private static String removeEscaped(String arg) {
|
||||||
StringBuilder builder = new StringBuilder();
|
StringBuilder builder = new StringBuilder();
|
||||||
int index = 0;
|
int index = 0;
|
||||||
int endIndex = arg.indexOf('\\');
|
int endIndex = arg.indexOf(ESCAPING_CHAR);
|
||||||
while (endIndex != -1) {
|
while (endIndex != -1) {
|
||||||
builder.append(arg.subSequence(index, endIndex));
|
builder.append(arg.subSequence(index, endIndex));
|
||||||
index = endIndex + 1;
|
index = endIndex + 1;
|
||||||
endIndex = arg.indexOf('\\', index + 1);
|
endIndex = arg.indexOf(ESCAPING_CHAR, index + 1);
|
||||||
}
|
}
|
||||||
builder.append(arg.substring(index));
|
builder.append(arg.substring(index));
|
||||||
return builder.toString();
|
return builder.toString();
|
||||||
|
|||||||
@@ -69,6 +69,8 @@ public abstract class Command implements ICommand {
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
private static final String EOL_LINUX = "\n"; //$NON-NLS-1$
|
private static final String EOL_LINUX = "\n"; //$NON-NLS-1$
|
||||||
|
/** The empty string constant */
|
||||||
|
private static final String EMPTY = ""; //$NON-NLS-1$
|
||||||
/** The name of the command */
|
/** The name of the command */
|
||||||
protected final String name;
|
protected final String name;
|
||||||
|
|
||||||
@@ -119,7 +121,7 @@ public abstract class Command implements ICommand {
|
|||||||
* @return the detailed help (should end with end of line or be empty) */
|
* @return the detailed help (should end with end of line or be empty) */
|
||||||
@SuppressWarnings("static-method")
|
@SuppressWarnings("static-method")
|
||||||
protected String usageDetail() {
|
protected String usageDetail() {
|
||||||
return new String();
|
return EMPTY;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** <p>
|
/** <p>
|
||||||
|
|||||||
@@ -56,9 +56,9 @@ public class CommandParameters {
|
|||||||
*/
|
*/
|
||||||
private static final int STRINGARG_NUMBER_OF_ELEMENTS = 2;
|
private static final int STRINGARG_NUMBER_OF_ELEMENTS = 2;
|
||||||
/** Boolean arguments */
|
/** Boolean arguments */
|
||||||
private final Map<String, Boolean> boolArgs = new HashMap<>();
|
private final Map<String, Boolean> booleanArguments = new HashMap<>();
|
||||||
/** String arguments */
|
/** String arguments */
|
||||||
private final Map<String, String> stringArgs = new HashMap<>();
|
private final Map<String, String> stringArguments = new HashMap<>();
|
||||||
/** Arguments restriction on the named ones */
|
/** Arguments restriction on the named ones */
|
||||||
private final boolean strict;
|
private final boolean strict;
|
||||||
/** additional (unnamed) parameters */
|
/** additional (unnamed) parameters */
|
||||||
@@ -71,10 +71,10 @@ public class CommandParameters {
|
|||||||
public CommandParameters(Set<String> bools, Set<String> strings,
|
public CommandParameters(Set<String> bools, Set<String> strings,
|
||||||
boolean strict) {
|
boolean strict) {
|
||||||
for (final String string : bools) {
|
for (final String string : bools) {
|
||||||
boolArgs.put(string, false);
|
booleanArguments.put(string, false);
|
||||||
}
|
}
|
||||||
for (final String string : strings) {
|
for (final String string : strings) {
|
||||||
stringArgs.put(string, null);
|
stringArguments.put(string, null);
|
||||||
}
|
}
|
||||||
this.strict = strict;
|
this.strict = strict;
|
||||||
}
|
}
|
||||||
@@ -82,7 +82,7 @@ public class CommandParameters {
|
|||||||
/** @param key the key
|
/** @param key the key
|
||||||
* @return the associated value, null if it was not specified */
|
* @return the associated value, null if it was not specified */
|
||||||
public String get(String key) {
|
public String get(String key) {
|
||||||
return stringArgs.get(key);
|
return stringArguments.get(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @return additional non parsed parameters */
|
/** @return additional non parsed parameters */
|
||||||
@@ -92,9 +92,9 @@ public class CommandParameters {
|
|||||||
|
|
||||||
/** @param key the key
|
/** @param key the key
|
||||||
* @return if the key was specified */
|
* @return if the key was specified */
|
||||||
@SuppressWarnings("boxing")
|
|
||||||
public boolean getBool(String key) {
|
public boolean getBool(String key) {
|
||||||
return boolArgs.get(key);
|
return booleanArguments.containsKey(key) &&
|
||||||
|
booleanArguments.get(key).booleanValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @param args the arguments to parse
|
/** @param args the arguments to parse
|
||||||
@@ -129,11 +129,11 @@ public class CommandParameters {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
String name = arg.substring(1);
|
String name = arg.substring(1);
|
||||||
if (boolArgs.containsKey(name)) {
|
if (booleanArguments.containsKey(name)) {
|
||||||
boolArgs.put(name, Boolean.TRUE);
|
booleanArguments.put(name, Boolean.TRUE);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
if (stringArgs.containsKey(name)) {
|
if (stringArguments.containsKey(name)) {
|
||||||
return parseStringArg(name, next);
|
return parseStringArg(name, next);
|
||||||
}
|
}
|
||||||
if (strict) {
|
if (strict) {
|
||||||
@@ -152,7 +152,7 @@ public class CommandParameters {
|
|||||||
if (next == null) {
|
if (next == null) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
stringArgs.put(name, next);
|
stringArguments.put(name, next);
|
||||||
return STRINGARG_NUMBER_OF_ELEMENTS;
|
return STRINGARG_NUMBER_OF_ELEMENTS;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -160,16 +160,26 @@ public class CommandParameters {
|
|||||||
* @param value the value */
|
* @param value the value */
|
||||||
@SuppressWarnings("boxing")
|
@SuppressWarnings("boxing")
|
||||||
public void set(String string, boolean value) {
|
public void set(String string, boolean value) {
|
||||||
if (boolArgs.containsKey(string)) {
|
if (booleanArguments.containsKey(string)) {
|
||||||
boolArgs.put(string, value);
|
booleanArguments.put(string, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @param string the key
|
/** @param string the key
|
||||||
* @param value the value */
|
* @param value the value */
|
||||||
public void set(String string, String value) {
|
public void set(String string, String value) {
|
||||||
if (stringArgs.containsKey(string)) {
|
if (stringArguments.containsKey(string)) {
|
||||||
stringArgs.put(string, value);
|
stringArguments.put(string, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @return the boolean arguments */
|
||||||
|
public Set<String> getBooleanArgumentKeys() {
|
||||||
|
return booleanArguments.keySet();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return the boolean arguments */
|
||||||
|
public Set<String> getStringArgumentKeys() {
|
||||||
|
return stringArguments.keySet();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,8 +36,8 @@
|
|||||||
* Created on: Aug 6, 2014 */
|
* Created on: Aug 6, 2014 */
|
||||||
package fr.bigeon.gclc.command;
|
package fr.bigeon.gclc.command;
|
||||||
|
|
||||||
import java.util.HashSet;
|
import java.util.ArrayList;
|
||||||
import java.util.Set;
|
import java.util.List;
|
||||||
|
|
||||||
import fr.bigeon.gclc.exception.CommandRunException;
|
import fr.bigeon.gclc.exception.CommandRunException;
|
||||||
import fr.bigeon.gclc.exception.InvalidCommandName;
|
import fr.bigeon.gclc.exception.InvalidCommandName;
|
||||||
@@ -53,12 +53,12 @@ public class CommandProvider implements ICommandProvider {
|
|||||||
/** The space character */
|
/** The space character */
|
||||||
private static final String SPACE = " "; //$NON-NLS-1$
|
private static final String SPACE = " "; //$NON-NLS-1$
|
||||||
/** The commands map */
|
/** The commands map */
|
||||||
protected final Set<ICommand> commands;
|
protected final List<ICommand> commands;
|
||||||
|
|
||||||
/** Create a command provider */
|
/** Create a command provider */
|
||||||
public CommandProvider() {
|
public CommandProvider() {
|
||||||
super();
|
super();
|
||||||
commands = new HashSet<>();
|
commands = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
@@ -67,12 +67,28 @@ public class CommandProvider implements ICommandProvider {
|
|||||||
@Override
|
@Override
|
||||||
public boolean add(ICommand value) throws InvalidCommandName {
|
public boolean add(ICommand value) throws InvalidCommandName {
|
||||||
final String name = value.getCommandName();
|
final String name = value.getCommandName();
|
||||||
if (name == null || name.startsWith(MINUS) || name.contains(SPACE)) {
|
testCommandName(name);
|
||||||
throw new InvalidCommandName();
|
if (commands.contains(value)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
for (ICommand iCommand : commands) {
|
||||||
|
if (iCommand.getCommandName().equals(value.getCommandName())) {
|
||||||
|
throw new InvalidCommandName(
|
||||||
|
"Name already used: " + value.getCommandName()); //$NON-NLS-1$
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return commands.add(value);
|
return commands.add(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @param name the command name
|
||||||
|
* @throws InvalidCommandName if the name is invalid */
|
||||||
|
private static void testCommandName(String name) throws InvalidCommandName {
|
||||||
|
if (name == null || name.isEmpty() || name.startsWith(MINUS) ||
|
||||||
|
name.contains(SPACE)) {
|
||||||
|
throw new InvalidCommandName();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void executeSub(String cmd,
|
public void executeSub(String cmd,
|
||||||
String... args) throws CommandRunException {
|
String... args) throws CommandRunException {
|
||||||
@@ -83,7 +99,7 @@ public class CommandProvider implements ICommandProvider {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw new CommandRunException(
|
throw new CommandRunException(
|
||||||
Messages.getString("CommandProvider.unrecognized", cmd)); //$NON-NLS-1$
|
Messages.getString("CommandProvider.unrecognized", cmd), null); //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ package fr.bigeon.gclc.command;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import fr.bigeon.gclc.exception.CommandRunException;
|
import fr.bigeon.gclc.exception.CommandRunException;
|
||||||
|
import fr.bigeon.gclc.exception.CommandRunExceptionType;
|
||||||
import fr.bigeon.gclc.manager.ConsoleManager;
|
import fr.bigeon.gclc.manager.ConsoleManager;
|
||||||
import fr.bigeon.gclc.prompt.CLIPrompterMessages;
|
import fr.bigeon.gclc.prompt.CLIPrompterMessages;
|
||||||
|
|
||||||
@@ -77,7 +78,8 @@ public class HelpExecutor extends Command {
|
|||||||
try {
|
try {
|
||||||
cmd.help(consoleManager, args);
|
cmd.help(consoleManager, args);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new CommandRunException("Console manager closed", e); //$NON-NLS-1$
|
throw new CommandRunException(CommandRunExceptionType.INTERACTION,
|
||||||
|
"Console manager closed", e, this); //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,14 +87,20 @@ public class HelpExecutor extends Command {
|
|||||||
* @see fr.bigeon.gclc.command.Command#brief() */
|
* @see fr.bigeon.gclc.command.Command#brief() */
|
||||||
@Override
|
@Override
|
||||||
protected String brief() {
|
protected String brief() {
|
||||||
return " A command to get help for other commands"; //$NON-NLS-1$
|
if (cmd instanceof SubedCommand) {
|
||||||
|
return " A command to get help for other commands"; //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
return " A command to retrieve help for " + cmd.getCommandName(); //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
* @see fr.bigeon.gclc.command.Command#usagePattern() */
|
* @see fr.bigeon.gclc.command.Command#usagePattern() */
|
||||||
@Override
|
@Override
|
||||||
protected String usagePattern() {
|
protected String usagePattern() {
|
||||||
return getCommandName() + " <otherCommand>"; //$NON-NLS-1$
|
if (cmd instanceof SubedCommand) {
|
||||||
|
return getCommandName() + " <otherCommand>"; //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
return getCommandName();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
|
|||||||
@@ -33,8 +33,8 @@
|
|||||||
* knowledge of the CeCILL license and that you accept its terms.
|
* knowledge of the CeCILL license and that you accept its terms.
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
* gclc:fr.bigeon.gclc.UnrecognizedCommand.java
|
* gclc:fr.bigeon.gclc.command.MockCommand.java
|
||||||
* Created on: Dec 23, 2014
|
* Created on: Nov 18, 2016
|
||||||
*/
|
*/
|
||||||
package fr.bigeon.gclc.command;
|
package fr.bigeon.gclc.command;
|
||||||
|
|
||||||
@@ -42,59 +42,50 @@ import java.io.IOException;
|
|||||||
|
|
||||||
import fr.bigeon.gclc.exception.CommandRunException;
|
import fr.bigeon.gclc.exception.CommandRunException;
|
||||||
import fr.bigeon.gclc.manager.ConsoleManager;
|
import fr.bigeon.gclc.manager.ConsoleManager;
|
||||||
import fr.bigeon.gclc.prompt.CLIPrompterMessages;
|
|
||||||
|
|
||||||
/** <p>
|
/** This implement a command that does nothing.
|
||||||
* The error message for unrecognized commands
|
* <p>
|
||||||
|
* This class is intended for testing purpose only.
|
||||||
*
|
*
|
||||||
* @author Emmanuel BIGEON */
|
* @author Emmanuel Bigeon */
|
||||||
public final class UnrecognizedCommand implements ICommand {
|
public final class MockCommand implements ICommand {
|
||||||
/** The unrecognized command key */
|
|
||||||
private static final String UNRECOGNIZED_CMD = "unrecognized.cmd"; //$NON-NLS-1$
|
|
||||||
/** The unrecognized command key */
|
|
||||||
private static final String EXPECTED_CMD = "expected.cmd"; //$NON-NLS-1$
|
|
||||||
/** The manager */
|
|
||||||
private final ConsoleManager manager;
|
|
||||||
|
|
||||||
/** @param manager the console manager to use */
|
/** The command name */
|
||||||
public UnrecognizedCommand(ConsoleManager manager) {
|
private final String name;
|
||||||
if (manager == null) {
|
|
||||||
throw new NullPointerException("The argument cannot be null"); //$NON-NLS-1$
|
/** @param name the command name */
|
||||||
}
|
public MockCommand(String name) {
|
||||||
this.manager = manager;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see fr.bigeon.gclc.command.ICommand#execute(java.lang.String[]) */
|
||||||
@Override
|
@Override
|
||||||
public void execute(String... args) throws CommandRunException {
|
public void execute(String... args) throws CommandRunException {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
/* (non-Javadoc)
|
||||||
if (args.length > 0) {
|
* @see fr.bigeon.gclc.command.ICommand#tip() */
|
||||||
manager.println(CLIPrompterMessages.getString(UNRECOGNIZED_CMD,
|
@Override
|
||||||
(Object[]) args));
|
public String tip() {
|
||||||
} else {
|
return null;
|
||||||
manager.println(CLIPrompterMessages.getString(EXPECTED_CMD));
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new CommandRunException("Manager closed", e); //$NON-NLS-1$
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
* @see fr.bigeon.gclc.command.ICommand#getCommandName() */
|
* @see fr.bigeon.gclc.command.ICommand#getCommandName() */
|
||||||
@Override
|
@Override
|
||||||
public String getCommandName() {
|
public String getCommandName() {
|
||||||
return ""; //$NON-NLS-1$
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see fr.bigeon.gclc.command.ICommand#help(fr.bigeon.gclc.manager.
|
||||||
|
* ConsoleManager, java.lang.String[]) */
|
||||||
@Override
|
@Override
|
||||||
public void help(@SuppressWarnings("hiding") ConsoleManager manager,
|
public void help(ConsoleManager manager,
|
||||||
String... args) {
|
String... args) throws IOException {
|
||||||
// Nothing to do (no help provided as this is not a user command
|
//
|
||||||
// (usually)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
}
|
||||||
public String tip() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -40,11 +40,17 @@ package fr.bigeon.gclc.command;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import fr.bigeon.gclc.exception.CommandRunException;
|
import fr.bigeon.gclc.exception.CommandRunException;
|
||||||
|
import fr.bigeon.gclc.exception.CommandRunExceptionType;
|
||||||
|
import fr.bigeon.gclc.exception.InvalidParameterException;
|
||||||
import fr.bigeon.gclc.manager.ConsoleManager;
|
import fr.bigeon.gclc.manager.ConsoleManager;
|
||||||
|
|
||||||
/** <p>
|
/** <p>
|
||||||
@@ -60,7 +66,7 @@ public abstract class ParametrizedCommand extends Command {
|
|||||||
/** The manager */
|
/** The manager */
|
||||||
protected final ConsoleManager manager;
|
protected final ConsoleManager manager;
|
||||||
/** The boolean parameters mandatory status */
|
/** The boolean parameters mandatory status */
|
||||||
private final Map<String, Boolean> boolParams = new HashMap<>();
|
private final Set<String> boolParams = new HashSet<>();
|
||||||
/** The string parameters mandatory status */
|
/** The string parameters mandatory status */
|
||||||
private final Map<String, Boolean> stringParams = new HashMap<>();
|
private final Map<String, Boolean> stringParams = new HashMap<>();
|
||||||
/** The parameters mandatory status */
|
/** The parameters mandatory status */
|
||||||
@@ -76,6 +82,11 @@ public abstract class ParametrizedCommand extends Command {
|
|||||||
this(manager, name, true);
|
this(manager, name, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @param name the name */
|
||||||
|
public ParametrizedCommand(String name) {
|
||||||
|
this(null, name, true);
|
||||||
|
}
|
||||||
|
|
||||||
/** @param manager the manager
|
/** @param manager the manager
|
||||||
* @param name the name
|
* @param name the name
|
||||||
* @param strict if the arguments are restricted to the declared ones */
|
* @param strict if the arguments are restricted to the declared ones */
|
||||||
@@ -87,26 +98,59 @@ public abstract class ParametrizedCommand extends Command {
|
|||||||
this.strict = strict;
|
this.strict = strict;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @param name the name
|
||||||
|
* @param strict if the arguments are restricted to the declared ones */
|
||||||
|
public ParametrizedCommand(String name, boolean strict) {
|
||||||
|
this(null, name, strict);
|
||||||
|
}
|
||||||
|
|
||||||
/** <p>
|
/** <p>
|
||||||
* Add a parameter to the defined parameters
|
* Add a parameter to the defined parameters
|
||||||
*
|
*
|
||||||
* @param param the parameter identification
|
* @param param the parameter identification
|
||||||
* @param stringOrBool if the parameter is a parameter with an argument
|
* @param stringParameter if the parameter is a parameter with an argument
|
||||||
* @param needed if the parameter is required */
|
* @param needed if the parameter is required
|
||||||
@SuppressWarnings("boxing")
|
* @throws InvalidParameterException if the parameter was invalid */
|
||||||
protected void addParameter(String param, boolean stringOrBool,
|
protected void addParameter(String param, boolean stringParameter,
|
||||||
boolean needed) {
|
boolean needed) throws InvalidParameterException {
|
||||||
if (params.containsKey(param)) {
|
if (params.containsKey(param)) {
|
||||||
|
checkParam(param, stringParameter, needed);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
params.put(param, needed);
|
if (stringParameter) {
|
||||||
if (stringOrBool) {
|
stringParams.put(param, Boolean.valueOf(needed));
|
||||||
stringParams.put(param, needed);
|
params.put(param, Boolean.valueOf(needed));
|
||||||
} else {
|
} else {
|
||||||
if (needed) {
|
if (needed) {
|
||||||
// ERROR the boolean parameters cannot be needed
|
// ERROR the boolean parameters cannot be needed
|
||||||
|
throw new InvalidParameterException(
|
||||||
|
"Boolean parameter are present by their very nature. They should not be defined as needed"); //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
boolParams.put(param, needed);
|
boolParams.add(param);
|
||||||
|
params.put(param, Boolean.valueOf(false));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @param param the parameter
|
||||||
|
* @param stringParameter the string parameter type
|
||||||
|
* @param needed if the parameter is needed
|
||||||
|
* @throws InvalidParameterException if the new definition is invalid */
|
||||||
|
private void checkParam(String param, boolean stringParameter,
|
||||||
|
boolean needed) throws InvalidParameterException {
|
||||||
|
if (stringParameter) {
|
||||||
|
if (stringParams.containsKey(param)) {
|
||||||
|
Boolean need = Boolean.valueOf(
|
||||||
|
needed || stringParams.get(param).booleanValue());
|
||||||
|
stringParams.put(param, need);
|
||||||
|
params.put(param, need);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
throw new InvalidParameterException(
|
||||||
|
"Parameter is already defined as boolean"); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
if (stringParams.containsKey(param) || needed) {
|
||||||
|
throw new InvalidParameterException(
|
||||||
|
"Parameter is already defined as string"); //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -119,18 +163,22 @@ public abstract class ParametrizedCommand extends Command {
|
|||||||
@Override
|
@Override
|
||||||
public final void execute(String... args) throws CommandRunException {
|
public final void execute(String... args) throws CommandRunException {
|
||||||
final CommandParameters parameters = new CommandParameters(
|
final CommandParameters parameters = new CommandParameters(
|
||||||
boolParams.keySet(), stringParams.keySet(), strict);
|
boolParams, stringParams.keySet(), strict);
|
||||||
if (!parameters.parseArgs(args)) {
|
if (!parameters.parseArgs(args)) {
|
||||||
// ERROR the parameters could not be correctly parsed
|
// the parameters could not be correctly parsed
|
||||||
throw new CommandRunException("Unable to read arguments"); //$NON-NLS-1$
|
throw new CommandRunException(CommandRunExceptionType.USAGE,
|
||||||
|
"Unable to read arguments", this); //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
final List<String> toProvide = new ArrayList<>();
|
final List<String> toProvide = new ArrayList<>();
|
||||||
for (final String string : params.keySet()) {
|
for (final Entry<String, Boolean> string : params.entrySet()) {
|
||||||
if (params.get(string) && parameters.get(string) == null) {
|
if (string.getValue() && parameters.get(string.getKey()) == null) {
|
||||||
if (!interactive) {
|
if (!interactive) {
|
||||||
return;
|
throw new CommandRunException(
|
||||||
|
CommandRunExceptionType.INTERACTION,
|
||||||
|
"Missing required parameter " + string.getKey(), //$NON-NLS-1$
|
||||||
|
this);
|
||||||
}
|
}
|
||||||
toProvide.add(string);
|
toProvide.add(string.getKey());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// for each needed parameters that is missing, prompt the user.
|
// for each needed parameters that is missing, prompt the user.
|
||||||
@@ -152,9 +200,41 @@ public abstract class ParametrizedCommand extends Command {
|
|||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new CommandRunException(
|
throw new CommandRunException(
|
||||||
"Interactive command but manager closed...", e); //$NON-NLS-1$
|
CommandRunExceptionType.INTERACTION,
|
||||||
|
"Interactive command but manager closed...", e, this); //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
parameters.set(string, value);
|
parameters.set(string, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @return the set of boolean parameters */
|
||||||
|
public Set<String> getBooleanParameters() {
|
||||||
|
return Collections.unmodifiableSet(boolParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return the stringParams */
|
||||||
|
public Set<String> getStringParameters() {
|
||||||
|
return stringParams.keySet();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return the stringParams */
|
||||||
|
public Set<String> getParameters() {
|
||||||
|
return params.keySet();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @param param the parameter name
|
||||||
|
* @return if the parameter is needed */
|
||||||
|
public boolean isNeeded(String param) {
|
||||||
|
return params.containsKey(param) && params.get(param).booleanValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return the strict */
|
||||||
|
public boolean isStrict() {
|
||||||
|
return strict;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return the interactive */
|
||||||
|
public boolean isInteractive() {
|
||||||
|
return interactive;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
196
gclc/src/main/java/fr/bigeon/gclc/command/ScriptExecution.java
Normal file
196
gclc/src/main/java/fr/bigeon/gclc/command/ScriptExecution.java
Normal file
@@ -0,0 +1,196 @@
|
|||||||
|
/*
|
||||||
|
* 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.ScriptExecution.java
|
||||||
|
* Created on: Jun 12, 2016
|
||||||
|
*/
|
||||||
|
package fr.bigeon.gclc.command;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.text.MessageFormat;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import fr.bigeon.gclc.ConsoleApplication;
|
||||||
|
import fr.bigeon.gclc.GCLCConstants;
|
||||||
|
import fr.bigeon.gclc.exception.CommandParsingException;
|
||||||
|
import fr.bigeon.gclc.exception.CommandRunException;
|
||||||
|
import fr.bigeon.gclc.exception.CommandRunExceptionType;
|
||||||
|
|
||||||
|
/** A command that will laucnh a series of command from a file
|
||||||
|
* <p>
|
||||||
|
* This command will read a file and execute each non empty non commented line
|
||||||
|
* as a command of the application.
|
||||||
|
*
|
||||||
|
* @author Emmanuel Bigeon */
|
||||||
|
public class ScriptExecution extends Command {
|
||||||
|
|
||||||
|
/** The tab character */
|
||||||
|
private static final String TAB = "\t"; //$NON-NLS-1$
|
||||||
|
/** the space character */
|
||||||
|
private static final String SPACE = " "; //$NON-NLS-1$
|
||||||
|
/** The application */
|
||||||
|
private final ConsoleApplication application;
|
||||||
|
/** The commenting prefix */
|
||||||
|
private final String commentPrefix;
|
||||||
|
/** The charset for files */
|
||||||
|
private final Charset charset;
|
||||||
|
|
||||||
|
/** @param name the name of the command
|
||||||
|
* @param application the application
|
||||||
|
* @param commentPrefix the comment prefix in the script files
|
||||||
|
* @param charset the charset to use for files */
|
||||||
|
public ScriptExecution(String name, ConsoleApplication application,
|
||||||
|
String commentPrefix, Charset charset) {
|
||||||
|
super(name);
|
||||||
|
this.application = application;
|
||||||
|
this.commentPrefix = commentPrefix;
|
||||||
|
this.charset = charset;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see fr.bigeon.gclc.command.ICommand#execute(java.lang.String[]) */
|
||||||
|
@Override
|
||||||
|
public void execute(String... args) throws CommandRunException {
|
||||||
|
checkArgs(args);
|
||||||
|
String scriptFile = args[0];
|
||||||
|
String[] params = Arrays.copyOfRange(args, 1, args.length);
|
||||||
|
String cmd;
|
||||||
|
int lineNo = -1;
|
||||||
|
try (InputStreamReader fReader = new InputStreamReader(
|
||||||
|
new FileInputStream(scriptFile), charset);
|
||||||
|
BufferedReader reader = new BufferedReader(fReader)) {
|
||||||
|
while ((cmd = reader.readLine()) != null) {
|
||||||
|
lineNo++;
|
||||||
|
String cmdLine = readCommandLine(cmd, params);
|
||||||
|
if (cmdLine == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
List<String> ps = GCLCConstants.splitCommand(cmdLine);
|
||||||
|
String command = ps.remove(0);
|
||||||
|
application.executeSub(command, ps.toArray(new String[0]));
|
||||||
|
}
|
||||||
|
} catch (CommandParsingException e) {
|
||||||
|
throw new CommandRunException("Invalid command in script (" + //$NON-NLS-1$
|
||||||
|
e.getLocalizedMessage() + ")", //$NON-NLS-1$
|
||||||
|
e, this);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new CommandRunException("Unable to read script", //$NON-NLS-1$
|
||||||
|
e, this);
|
||||||
|
} catch (CommandRunException e) {
|
||||||
|
throw manageRunException(e, lineNo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @param args the arguments
|
||||||
|
* @throws CommandRunException if the arguments were not the ones
|
||||||
|
* expected */
|
||||||
|
private void checkArgs(String[] args) throws CommandRunException {
|
||||||
|
if (args.length == 0) {
|
||||||
|
throw new CommandRunException(CommandRunExceptionType.USAGE,
|
||||||
|
"Expecting a file", this); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** This method will create the correct exception. The exception source must
|
||||||
|
* be this command.
|
||||||
|
*
|
||||||
|
* @param e the exception
|
||||||
|
* @param lineNo the line nu;ber
|
||||||
|
* @return the exception to actually throw */
|
||||||
|
private CommandRunException manageRunException(CommandRunException e,
|
||||||
|
int lineNo) {
|
||||||
|
if (e.getSource() == this) {
|
||||||
|
// ensure closing?
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
return new CommandRunException(CommandRunExceptionType.EXECUTION,
|
||||||
|
MessageFormat.format(
|
||||||
|
"The script could not complete due to command failure at line {0} ({1})", //$NON-NLS-1$
|
||||||
|
Integer.valueOf(lineNo), e.getLocalizedMessage()),
|
||||||
|
e, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @param cmd the line
|
||||||
|
* @param params the formatting parameters
|
||||||
|
* @return the command if it is indeed one, null otherwise
|
||||||
|
* @throws CommandRunException if the line stqrted with a space character */
|
||||||
|
private String readCommandLine(String cmd,
|
||||||
|
Object[] params) throws CommandRunException {
|
||||||
|
if (cmd.startsWith(SPACE) || cmd.startsWith(TAB)) {
|
||||||
|
throw new CommandRunException(
|
||||||
|
"Invalid command in script (line starts with space character)", //$NON-NLS-1$
|
||||||
|
this);
|
||||||
|
}
|
||||||
|
if (cmd.isEmpty() || cmd.startsWith(commentPrefix)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return MessageFormat.format(cmd, params);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see fr.bigeon.gclc.command.Command#usageDetail() */
|
||||||
|
@Override
|
||||||
|
protected String usageDetail() {
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
builder.append(
|
||||||
|
" scriptfile: path to the file containing the script to execute."); //$NON-NLS-1$
|
||||||
|
builder.append(System.lineSeparator());
|
||||||
|
builder.append(System.lineSeparator());
|
||||||
|
builder.append(
|
||||||
|
" The script file must contain one line commands. The lines must never"); //$NON-NLS-1$
|
||||||
|
builder.append(System.lineSeparator());
|
||||||
|
builder.append(
|
||||||
|
"start with whitespace characters. The lines starting with"); //$NON-NLS-1$
|
||||||
|
builder.append(System.lineSeparator());
|
||||||
|
builder.append("\"" + commentPrefix + //$NON-NLS-1$
|
||||||
|
"\" will be ignored as well as empty lines."); //$NON-NLS-1$
|
||||||
|
builder.append(System.lineSeparator());
|
||||||
|
|
||||||
|
return builder.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see fr.bigeon.gclc.command.ICommand#tip() */
|
||||||
|
@Override
|
||||||
|
public String tip() {
|
||||||
|
return "Execute a script"; //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -40,6 +40,7 @@ import java.io.IOException;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
import fr.bigeon.gclc.exception.CommandRunException;
|
import fr.bigeon.gclc.exception.CommandRunException;
|
||||||
|
import fr.bigeon.gclc.exception.CommandRunExceptionType;
|
||||||
import fr.bigeon.gclc.exception.InvalidCommandName;
|
import fr.bigeon.gclc.exception.InvalidCommandName;
|
||||||
import fr.bigeon.gclc.manager.ConsoleManager;
|
import fr.bigeon.gclc.manager.ConsoleManager;
|
||||||
|
|
||||||
@@ -114,10 +115,19 @@ public class SubedCommand implements ICommandProvider, ICommand {
|
|||||||
if (noArgCommand != null) {
|
if (noArgCommand != null) {
|
||||||
noArgCommand.execute(args);
|
noArgCommand.execute(args);
|
||||||
} else {
|
} else {
|
||||||
throw new CommandRunException("Unrecognized command"); //$NON-NLS-1$
|
throw new CommandRunException("Unrecognized command", this); //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
executeSub(args[0], Arrays.copyOfRange(args, 1, args.length));
|
|
||||||
|
try {
|
||||||
|
executeSub(args[0], Arrays.copyOfRange(args, 1, args.length));
|
||||||
|
} catch (CommandRunException e) {
|
||||||
|
if (e.getSource() != null) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
throw new CommandRunException(CommandRunExceptionType.USAGE,
|
||||||
|
e.getLocalizedMessage(), e, this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -44,8 +44,6 @@
|
|||||||
* {@link fr.bigeon.gclc.command.SubedCommand} for a command that is declined in
|
* {@link fr.bigeon.gclc.command.SubedCommand} for a command that is declined in
|
||||||
* a set of sub commands, the {@link fr.bigeon.gclc.command.HelpExecutor} for
|
* a set of sub commands, the {@link fr.bigeon.gclc.command.HelpExecutor} for
|
||||||
* help display of other commands and the
|
* help display of other commands and the
|
||||||
* {@link fr.bigeon.gclc.command.UnrecognizedCommand} that should not be
|
|
||||||
* directly accessible to the user, but will be used in case of error in typing.
|
|
||||||
*
|
*
|
||||||
* @author Emmanuel BIGEON */
|
* @author Emmanuel BIGEON */
|
||||||
package fr.bigeon.gclc.command;
|
package fr.bigeon.gclc.command;
|
||||||
@@ -38,6 +38,8 @@
|
|||||||
*/
|
*/
|
||||||
package fr.bigeon.gclc.exception;
|
package fr.bigeon.gclc.exception;
|
||||||
|
|
||||||
|
import fr.bigeon.gclc.command.ICommand;
|
||||||
|
|
||||||
/** <p>
|
/** <p>
|
||||||
* An exception thrown when a command failed to run correctly.
|
* An exception thrown when a command failed to run correctly.
|
||||||
*
|
*
|
||||||
@@ -49,15 +51,48 @@ public class CommandRunException extends Exception {
|
|||||||
*/
|
*/
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
/** @param message a message */
|
/** The type of run exception */
|
||||||
public CommandRunException(String message) {
|
private final CommandRunExceptionType type;
|
||||||
|
/** The command that caused the error */
|
||||||
|
private final ICommand source;
|
||||||
|
|
||||||
|
/** @param message a message
|
||||||
|
* @param source the source */
|
||||||
|
public CommandRunException(String message, ICommand source) {
|
||||||
super(message);
|
super(message);
|
||||||
|
type = CommandRunExceptionType.EXECUTION;
|
||||||
|
this.source = source;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @param message a message
|
/** @param message a message
|
||||||
* @param cause the cause */
|
* @param cause the cause
|
||||||
public CommandRunException(String message, Throwable cause) {
|
* @param source the source */
|
||||||
|
public CommandRunException(String message, Throwable cause,
|
||||||
|
ICommand source) {
|
||||||
super(message, cause);
|
super(message, cause);
|
||||||
|
type = CommandRunExceptionType.EXECUTION;
|
||||||
|
this.source = source;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @param type the type of exception
|
||||||
|
* @param message the message
|
||||||
|
* @param source the source */
|
||||||
|
public CommandRunException(CommandRunExceptionType type, String message,
|
||||||
|
ICommand source) {
|
||||||
|
super(message);
|
||||||
|
this.type = type;
|
||||||
|
this.source = source;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @param type the type of exception
|
||||||
|
* @param message a message
|
||||||
|
* @param cause the cause
|
||||||
|
* @param source the source */
|
||||||
|
public CommandRunException(CommandRunExceptionType type, String message,
|
||||||
|
Throwable cause, ICommand source) {
|
||||||
|
super(message, cause);
|
||||||
|
this.type = type;
|
||||||
|
this.source = source;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
@@ -71,4 +106,13 @@ public class CommandRunException extends Exception {
|
|||||||
return super.getLocalizedMessage();
|
return super.getLocalizedMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @return the source */
|
||||||
|
public ICommand getSource() {
|
||||||
|
return source;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return the type */
|
||||||
|
public CommandRunExceptionType getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,51 @@
|
|||||||
|
/*
|
||||||
|
* 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.CommandRunExceptionType.java
|
||||||
|
* Created on: Jun 12, 2016
|
||||||
|
*/
|
||||||
|
package fr.bigeon.gclc.exception;
|
||||||
|
|
||||||
|
/** The command run exception possible types
|
||||||
|
*
|
||||||
|
* @author Emmanuel Bigeon */
|
||||||
|
public enum CommandRunExceptionType {
|
||||||
|
/** Type of exception due to a wrong usage */
|
||||||
|
USAGE,
|
||||||
|
/** Type of exception due to a problem in execution */
|
||||||
|
EXECUTION,
|
||||||
|
/** Type of exception due to the impossibility to interact with user */
|
||||||
|
INTERACTION;
|
||||||
|
}
|
||||||
@@ -40,7 +40,7 @@ package fr.bigeon.gclc.exception;
|
|||||||
|
|
||||||
/** <p>
|
/** <p>
|
||||||
* Exception sent from the application when a command is added but the name of
|
* Exception sent from the application when a command is added but the name of
|
||||||
* the command
|
* the command is already used
|
||||||
*
|
*
|
||||||
* @author Emmanuel BIGEON */
|
* @author Emmanuel BIGEON */
|
||||||
public class InvalidCommandName extends Exception {
|
public class InvalidCommandName extends Exception {
|
||||||
@@ -50,4 +50,25 @@ public class InvalidCommandName extends Exception {
|
|||||||
*/
|
*/
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
/** Default constructor */
|
||||||
|
public InvalidCommandName() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @param message the message
|
||||||
|
* @param cause the cause */
|
||||||
|
public InvalidCommandName(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @param message the message */
|
||||||
|
public InvalidCommandName(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @param cause the cause */
|
||||||
|
public InvalidCommandName(Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,71 @@
|
|||||||
|
/*
|
||||||
|
* Copyright Bigeon Emmanuel (2014)
|
||||||
|
*
|
||||||
|
* emmanuel@bigeon.fr
|
||||||
|
*
|
||||||
|
* This software is a computer program whose purpose is to
|
||||||
|
* provide a generic framework for console applications.
|
||||||
|
*
|
||||||
|
* This software is governed by the CeCILL license under French law and
|
||||||
|
* abiding by the rules of distribution of free software. You can use,
|
||||||
|
* modify and/or redistribute the software under the terms of the CeCILL
|
||||||
|
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||||
|
* "http://www.cecill.info".
|
||||||
|
*
|
||||||
|
* As a counterpart to the access to the source code and rights to copy,
|
||||||
|
* modify and redistribute granted by the license, users are provided only
|
||||||
|
* with a limited warranty and the software's author, the holder of the
|
||||||
|
* economic rights, and the successive licensors have only limited
|
||||||
|
* liability.
|
||||||
|
*
|
||||||
|
* In this respect, the user's attention is drawn to the risks associated
|
||||||
|
* with loading, using, modifying and/or developing or reproducing the
|
||||||
|
* software by the user in light of its specific status of free software,
|
||||||
|
* that may mean that it is complicated to manipulate, and that also
|
||||||
|
* therefore means that it is reserved for developers and experienced
|
||||||
|
* professionals having in-depth computer knowledge. Users are therefore
|
||||||
|
* encouraged to load and test the software's suitability as regards their
|
||||||
|
* requirements in conditions enabling the security of their systems and/or
|
||||||
|
* data to be ensured and, more generally, to use and operate it in the
|
||||||
|
* same conditions as regards security.
|
||||||
|
*
|
||||||
|
* The fact that you are presently reading this means that you have had
|
||||||
|
* knowledge of the CeCILL license and that you accept its terms.
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* gclc:fr.bigeon.gclc.exception.InvalidParameterException.java
|
||||||
|
* Created on: Nov 19, 2016
|
||||||
|
*/
|
||||||
|
package fr.bigeon.gclc.exception;
|
||||||
|
|
||||||
|
/** This exception is thrown during command definitions to indicate a wrong
|
||||||
|
* parameter definition.
|
||||||
|
* <p>
|
||||||
|
* This class is particularly used by
|
||||||
|
* {@link fr.bigeon.gclc.command.ParametrizedCommand parameterized commands}.
|
||||||
|
*
|
||||||
|
* @author Emmanuel Bigeon */
|
||||||
|
public class InvalidParameterException extends Exception {
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
/** @param message the message
|
||||||
|
* @param cause the cause */
|
||||||
|
public InvalidParameterException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @param message the message */
|
||||||
|
public InvalidParameterException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @param cause the cause */
|
||||||
|
public InvalidParameterException(Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -43,6 +43,7 @@ import java.io.IOException;
|
|||||||
import java.io.InputStream;
|
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.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
@@ -54,12 +55,15 @@ import java.util.logging.Logger;
|
|||||||
public class SystemConsoleManager implements ConsoleManager { // NOSONAR
|
public 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 */
|
/** The logger */
|
||||||
private static final Logger LOGGER = Logger
|
private static final Logger LOGGER = Logger
|
||||||
.getLogger(SystemConsoleManager.class.getName());
|
.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;
|
||||||
|
|
||||||
@@ -74,15 +78,17 @@ public class SystemConsoleManager implements ConsoleManager { // NOSONAR
|
|||||||
/** 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() {
|
||||||
this(System.out, System.in);
|
this(System.out, System.in, Charset.defaultCharset());
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @param out the output stream
|
/** @param out the output stream
|
||||||
* @param in the input stream */
|
* @param in the input stream
|
||||||
public SystemConsoleManager(PrintStream out, InputStream in) {
|
* @param charset the charset for the input */
|
||||||
|
public SystemConsoleManager(PrintStream out, InputStream in,
|
||||||
|
Charset charset) {
|
||||||
super();
|
super();
|
||||||
this.out = out;
|
this.out = out;
|
||||||
this.in = new BufferedReader(new InputStreamReader(in));
|
this.in = new BufferedReader(new InputStreamReader(in, charset));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @return the prompt */
|
/** @return the prompt */
|
||||||
@@ -95,19 +101,22 @@ public class SystemConsoleManager implements ConsoleManager { // NOSONAR
|
|||||||
* @see fr.bigeon.gclc.ConsoleManager#print(java.lang.Object) */
|
* @see fr.bigeon.gclc.ConsoleManager#print(java.lang.Object) */
|
||||||
@Override
|
@Override
|
||||||
public void print(String object) throws IOException {
|
public void print(String object) throws IOException {
|
||||||
|
checkOpen();
|
||||||
|
out.print(object);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @throws IOException if the stream was closed */
|
||||||
|
private void checkOpen() throws IOException {
|
||||||
if (closed) {
|
if (closed) {
|
||||||
throw new IOException();
|
throw new IOException();
|
||||||
}
|
}
|
||||||
out.print(object);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
* @see fr.bigeon.gclc.ConsoleManager#println() */
|
* @see fr.bigeon.gclc.ConsoleManager#println() */
|
||||||
@Override
|
@Override
|
||||||
public void println() throws IOException {
|
public void println() throws IOException {
|
||||||
if (closed) {
|
checkOpen();
|
||||||
throw new IOException();
|
|
||||||
}
|
|
||||||
out.println();
|
out.println();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,9 +124,7 @@ public class SystemConsoleManager implements ConsoleManager { // NOSONAR
|
|||||||
* @see fr.bigeon.gclc.ConsoleManager#println(java.lang.Object) */
|
* @see fr.bigeon.gclc.ConsoleManager#println(java.lang.Object) */
|
||||||
@Override
|
@Override
|
||||||
public void println(String object) throws IOException {
|
public void println(String object) throws IOException {
|
||||||
if (closed) {
|
checkOpen();
|
||||||
throw new IOException();
|
|
||||||
}
|
|
||||||
out.println(object);
|
out.println(object);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,29 +132,22 @@ public class SystemConsoleManager implements ConsoleManager { // NOSONAR
|
|||||||
* @see fr.bigeon.gclc.ConsoleManager#prompt() */
|
* @see fr.bigeon.gclc.ConsoleManager#prompt() */
|
||||||
@Override
|
@Override
|
||||||
public String prompt() throws IOException {
|
public String prompt() throws IOException {
|
||||||
return prompt(new String() + prompt);
|
return prompt(prompt);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
* @see fr.bigeon.gclc.ConsoleManager#prompt(java.lang.String) */
|
* @see fr.bigeon.gclc.ConsoleManager#prompt(java.lang.String) */
|
||||||
@Override
|
@Override
|
||||||
public String prompt(String message) throws IOException {
|
public String prompt(String message) throws IOException {
|
||||||
if (closed) {
|
checkOpen();
|
||||||
throw new IOException();
|
String result = EMPTY;
|
||||||
}
|
out.print(message);
|
||||||
String result = new String();
|
|
||||||
out.print(message + ' ');
|
|
||||||
char c;
|
|
||||||
try {
|
try {
|
||||||
result = in.readLine();
|
result = in.readLine();
|
||||||
// c = (char) in.read();
|
while (result != null && result.length() > 0 &&
|
||||||
// while (c != System.lineSeparator().charAt(0)) {
|
result.charAt(0) == 0) {
|
||||||
// result += c;
|
result = result.substring(1);
|
||||||
// c = (char) in.read();
|
}
|
||||||
// }
|
|
||||||
// while (in.available() != 0) {
|
|
||||||
// in.read();
|
|
||||||
// }
|
|
||||||
} catch (final IOException e) {
|
} catch (final IOException e) {
|
||||||
LOGGER.log(Level.SEVERE, "Unable to read prompt", e); //$NON-NLS-1$
|
LOGGER.log(Level.SEVERE, "Unable to read prompt", e); //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -90,9 +90,9 @@ public class CLIPrompter {
|
|||||||
manager.println(index++ + ") " + u); //$NON-NLS-1$
|
manager.println(index++ + ") " + u); //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
if (cancel != null) {
|
if (cancel != null) {
|
||||||
manager.println(index + ") " + cancel); //$NON-NLS-1$
|
manager.println(index++ + ") " + cancel); //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
return index;
|
return index - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @param manager the manager
|
/** @param manager the manager
|
||||||
@@ -162,8 +162,11 @@ public class CLIPrompter {
|
|||||||
public static <U, T> T promptChoice(ConsoleManager manager, List<U> choices,
|
public static <U, T> T promptChoice(ConsoleManager manager, List<U> choices,
|
||||||
Map<U, T> choicesMap, String message,
|
Map<U, T> choicesMap, String message,
|
||||||
String cancel) throws IOException {
|
String cancel) throws IOException {
|
||||||
return choicesMap.get(choices.get(
|
Integer res = promptChoice(manager, choices, message, cancel);
|
||||||
promptChoice(manager, choices, message, cancel).intValue()));
|
if (res == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return choicesMap.get(choices.get(res.intValue()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @param manager the manager
|
/** @param manager the manager
|
||||||
@@ -195,6 +198,8 @@ public class CLIPrompter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
} catch (final NumberFormatException e) {
|
} catch (final NumberFormatException e) {
|
||||||
|
LOGGER.log(Level.FINER,
|
||||||
|
"Unrecognized number. Prompting user again.", e); //$NON-NLS-1$
|
||||||
keepOn = true;
|
keepOn = true;
|
||||||
manager.println(CLIPrompterMessages
|
manager.println(CLIPrompterMessages
|
||||||
.getString(PROMPTCHOICE_FORMATERR, 0, index));
|
.getString(PROMPTCHOICE_FORMATERR, 0, index));
|
||||||
@@ -306,10 +311,10 @@ public class CLIPrompter {
|
|||||||
String ender) throws IOException {
|
String ender) throws IOException {
|
||||||
manager.println(message + CLIPrompterMessages
|
manager.println(message + CLIPrompterMessages
|
||||||
.getString("promptlongtext.exit.dispkey", ender)); //$NON-NLS-1$
|
.getString("promptlongtext.exit.dispkey", ender)); //$NON-NLS-1$
|
||||||
String res = manager.prompt(PROMPT);
|
String res = manager.prompt(CLIPrompterMessages.getString(PROMPT));
|
||||||
String line = res;
|
String line = res;
|
||||||
while (!line.equals(ender)) {
|
while (!line.equals(ender)) {
|
||||||
line = manager.prompt(PROMPT);
|
line = manager.prompt(CLIPrompterMessages.getString(PROMPT));
|
||||||
if (!line.equals(ender)) {
|
if (!line.equals(ender)) {
|
||||||
res += System.lineSeparator() + line;
|
res += System.lineSeparator() + line;
|
||||||
}
|
}
|
||||||
@@ -351,7 +356,7 @@ public class CLIPrompter {
|
|||||||
final List<Integer> chs = promptMultiChoice(manager, choices, message);
|
final List<Integer> chs = promptMultiChoice(manager, choices, message);
|
||||||
final List<T> userChoices = new ArrayList<>();
|
final List<T> userChoices = new ArrayList<>();
|
||||||
for (final Integer integer : chs) {
|
for (final Integer integer : chs) {
|
||||||
userChoices.add(choicesMap.get(integer));
|
userChoices.add(choicesMap.get(choices.get(integer.intValue())));
|
||||||
}
|
}
|
||||||
return userChoices;
|
return userChoices;
|
||||||
}
|
}
|
||||||
@@ -381,6 +386,8 @@ public class CLIPrompter {
|
|||||||
try {
|
try {
|
||||||
added = addUserChoice(val, chs, index);
|
added = addUserChoice(val, chs, index);
|
||||||
} catch (final NumberFormatException e) {
|
} catch (final NumberFormatException e) {
|
||||||
|
LOGGER.log(Level.FINER,
|
||||||
|
"Unrecognized number. Prompting user again.", e); //$NON-NLS-1$
|
||||||
keepOn = true;
|
keepOn = true;
|
||||||
manager.println(CLIPrompterMessages
|
manager.println(CLIPrompterMessages
|
||||||
.getString(PROMPTCHOICE_FORMATERR, 0, index));
|
.getString(PROMPTCHOICE_FORMATERR, 0, index));
|
||||||
@@ -409,7 +416,7 @@ public class CLIPrompter {
|
|||||||
}
|
}
|
||||||
final int r;
|
final int r;
|
||||||
r = Integer.parseInt(val);
|
r = Integer.parseInt(val);
|
||||||
if (r >= 0 && r < index) {
|
if (r >= 0 && r <= index) {
|
||||||
chs.add(Integer.valueOf(r));
|
chs.add(Integer.valueOf(r));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,6 +50,8 @@ public class PrintUtils {
|
|||||||
private static final String CONT_DOT = "..."; //$NON-NLS-1$
|
private static final String CONT_DOT = "..."; //$NON-NLS-1$
|
||||||
/** The continuation dot string length */
|
/** The continuation dot string length */
|
||||||
private static final int CONT_DOT_LENGTH = CONT_DOT.length();
|
private static final int CONT_DOT_LENGTH = CONT_DOT.length();
|
||||||
|
/** The empty string constant */
|
||||||
|
private static final String EMPTY = ""; //$NON-NLS-1$
|
||||||
|
|
||||||
/** Utility class */
|
/** Utility class */
|
||||||
private PrintUtils() {
|
private PrintUtils() {
|
||||||
@@ -101,8 +103,9 @@ public class PrintUtils {
|
|||||||
toCut = toCut.substring(index + 1);
|
toCut = toCut.substring(index + 1);
|
||||||
}
|
}
|
||||||
result.add(toCut);
|
result.add(toCut);
|
||||||
result.add(new String());
|
result.add(EMPTY);
|
||||||
}
|
}
|
||||||
|
result.remove(result.size() - 1);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
exit.tip=Exit the application
|
exit.tip=Exit the application
|
||||||
help.cmd.tip=Display an help tip
|
help.cmd.tip=Display an help tip
|
||||||
|
|
||||||
prompt.lineprompt=>
|
prompt.lineprompt=>\
|
||||||
|
|
||||||
promptlist.exit.defaultkey=\\q
|
promptlist.exit.defaultkey=\\q
|
||||||
promptlist.exit.dispkey=\ (exit with a new line made of "{0}")
|
promptlist.exit.dispkey=\ (exit with a new line made of "{0}")
|
||||||
promptlist.prompt=>\
|
promptlist.prompt=>\
|
||||||
|
promptlist.multi.sepkey=\
|
||||||
|
|
||||||
promptbool.choices=\ (Y/N)
|
promptbool.choices=\ (Y/N)
|
||||||
promptbool.choices.invalid=Invalid input. Please input one of {0}.
|
promptbool.choices.invalid=Invalid input. Please input one of {0}.
|
||||||
|
|||||||
122
gclc/src/test/java/fr/bigeon/gclc/CommandTestingApplication.java
Normal file
122
gclc/src/test/java/fr/bigeon/gclc/CommandTestingApplication.java
Normal file
@@ -0,0 +1,122 @@
|
|||||||
|
/*
|
||||||
|
* 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.test.utils.TestConsoleManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* TODO
|
||||||
|
*
|
||||||
|
* @author Emmanuel Bigeon
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("javadoc")
|
||||||
|
public class CommandTestingApplication implements AutoCloseable {
|
||||||
|
|
||||||
|
private final ConsoleTestApplication application;
|
||||||
|
private final Thread th;
|
||||||
|
private final TestConsoleManager manager;
|
||||||
|
|
||||||
|
/** @throws IOException if the streams cannot be build */
|
||||||
|
public CommandTestingApplication() throws IOException {
|
||||||
|
manager = new TestConsoleManager();
|
||||||
|
application = new ConsoleTestApplication(manager);
|
||||||
|
th = new Thread(new Runnable() {
|
||||||
|
|
||||||
|
@SuppressWarnings("synthetic-access")
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
application.start();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
th.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() throws IOException {
|
||||||
|
application.exit();
|
||||||
|
manager.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendCommand(String cmd) throws IOException {
|
||||||
|
manager.type(cmd);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @param cmd the command
|
||||||
|
* @return if the command was added
|
||||||
|
* @throws InvalidCommandName if the command name is invalid
|
||||||
|
* @see fr.bigeon.gclc.ConsoleApplication#add(fr.bigeon.gclc.command.ICommand) */
|
||||||
|
public final boolean add(ICommand cmd) throws InvalidCommandName {
|
||||||
|
return application.add(cmd);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return the application */
|
||||||
|
public ConsoleTestApplication getApplication() {
|
||||||
|
return application;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @throws IOException if the writing failed */
|
||||||
|
public void waitAndStop() throws IOException {
|
||||||
|
manager.type(ConsoleTestApplication.EXIT);
|
||||||
|
try {
|
||||||
|
th.join();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return the next written line, null if it is the prompt
|
||||||
|
* @throws IOException if the reading failed
|
||||||
|
* @see fr.bigeon.gclc.test.utils.TestConsoleManager#readNextLine() */
|
||||||
|
public String readNextLine() throws IOException {
|
||||||
|
String ret = manager.readNextLine();
|
||||||
|
if (ret.equals(manager.getPrompt())) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -38,25 +38,33 @@
|
|||||||
*/
|
*/
|
||||||
package fr.bigeon.gclc;
|
package fr.bigeon.gclc;
|
||||||
|
|
||||||
import static org.junit.Assert.fail;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.PipedInputStream;
|
|
||||||
import java.io.PipedOutputStream;
|
|
||||||
import java.io.PrintStream;
|
|
||||||
import java.io.PrintWriter;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import fr.bigeon.gclc.command.ExitCommand;
|
||||||
|
import fr.bigeon.gclc.command.ICommand;
|
||||||
|
import fr.bigeon.gclc.exception.CommandRunException;
|
||||||
|
import fr.bigeon.gclc.exception.CommandRunExceptionType;
|
||||||
|
import fr.bigeon.gclc.exception.InvalidCommandName;
|
||||||
|
import fr.bigeon.gclc.i18n.Messages;
|
||||||
|
import fr.bigeon.gclc.manager.ConsoleManager;
|
||||||
import fr.bigeon.gclc.manager.SystemConsoleManager;
|
import fr.bigeon.gclc.manager.SystemConsoleManager;
|
||||||
|
import fr.bigeon.gclc.test.utils.TestConsoleManager;
|
||||||
|
|
||||||
/** Test class for ConsoleApplication
|
/** Test class for ConsoleApplication
|
||||||
*
|
*
|
||||||
* @author Emmanuel Bigeon */
|
* @author Emmanuel Bigeon */
|
||||||
@SuppressWarnings("static-method")
|
@SuppressWarnings({"javadoc", "nls", "static-method"})
|
||||||
public class ConsoleApplicationTest {
|
public class ConsoleApplicationTest {
|
||||||
|
|
||||||
|
/** 3 seconds in milliseconds */
|
||||||
protected static final long THREE_SECONDS = 3000;
|
protected static final long THREE_SECONDS = 3000;
|
||||||
|
|
||||||
/** Test the base of a console application */
|
/** Test the base of a console application */
|
||||||
@@ -69,16 +77,74 @@ public class ConsoleApplicationTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void executionTest() {
|
public void executionTest() {
|
||||||
try {
|
try (CommandTestingApplication application = new CommandTestingApplication()) {
|
||||||
final PipedOutputStream src = new PipedOutputStream();
|
|
||||||
InputStream in = new PipedInputStream(src);
|
// remove welcome
|
||||||
|
assertEquals(application.getApplication().getHeader(),
|
||||||
|
application.readNextLine());
|
||||||
|
// Remove first prompt
|
||||||
|
assertNull(application.readNextLine());
|
||||||
|
application.sendCommand("");
|
||||||
|
assertNull(application.readNextLine());
|
||||||
|
application.sendCommand("test");
|
||||||
|
assertEquals("Test command ran fine", application.readNextLine());
|
||||||
|
assertNull(application.readNextLine());
|
||||||
|
application.sendCommand("toto");
|
||||||
|
assertEquals(
|
||||||
|
Messages.getString("ConsoleApplication.cmd.failed", "toto"),
|
||||||
|
application.readNextLine());
|
||||||
|
assertEquals(
|
||||||
|
Messages.getString("CommandProvider.unrecognized", "toto"),
|
||||||
|
application.readNextLine());
|
||||||
|
assertNull(application.readNextLine());
|
||||||
|
application.sendCommand("long");
|
||||||
|
assertEquals("Waita minute", application.readNextLine());
|
||||||
|
assertEquals("done!", application.readNextLine());
|
||||||
|
|
||||||
|
CommandRequestListener crl = new CommandRequestListener() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void commandRequest(String command) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
};
|
||||||
|
CommandRequestListener crl2 = new CommandRequestListener() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void commandRequest(String command) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
};
|
||||||
|
application.getApplication().addListener(crl);
|
||||||
|
application.getApplication().addListener(crl2);
|
||||||
|
assertNull(application.readNextLine());
|
||||||
|
application.sendCommand("test");
|
||||||
|
assertEquals("Test command ran fine", application.readNextLine());
|
||||||
|
application.getApplication().removeListener(crl2);
|
||||||
|
application.getApplication().removeListener(crl);
|
||||||
|
application.getApplication().removeListener(crl);
|
||||||
|
|
||||||
|
assertTrue(application.getApplication().isRunning());
|
||||||
|
|
||||||
|
assertNull(application.readNextLine());
|
||||||
|
application.sendCommand("exit");
|
||||||
|
assertEquals(application.getApplication().getFooter(),
|
||||||
|
application.readNextLine());
|
||||||
|
assertFalse(application.getApplication().isRunning());
|
||||||
|
} catch (IOException e1) {
|
||||||
|
assertNull(e1);
|
||||||
|
}
|
||||||
|
|
||||||
|
ConsoleApplication appli = null;
|
||||||
|
try (TestConsoleManager manager = new TestConsoleManager()) {
|
||||||
|
final ConsoleApplication app = new ConsoleApplication(manager, null,
|
||||||
|
null);
|
||||||
|
appli = app;
|
||||||
|
app.add(new ExitCommand("exit", app));
|
||||||
|
|
||||||
final PipedInputStream snk = new PipedInputStream();
|
|
||||||
PrintStream out = new PrintStream(new PipedOutputStream(snk));
|
|
||||||
final ConsoleTestApplication app = new ConsoleTestApplication(
|
|
||||||
new SystemConsoleManager(System.out, in));
|
|
||||||
Thread th = new Thread(new Runnable() {
|
Thread th = new Thread(new Runnable() {
|
||||||
|
|
||||||
|
@SuppressWarnings("synthetic-access")
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
app.start();
|
app.start();
|
||||||
@@ -87,33 +153,67 @@ public class ConsoleApplicationTest {
|
|||||||
|
|
||||||
th.start();
|
th.start();
|
||||||
|
|
||||||
Thread test = new Thread(new Runnable() {
|
manager.type("exit");
|
||||||
|
th.join();
|
||||||
|
|
||||||
@Override
|
} catch (IOException | InvalidCommandName | InterruptedException e) {
|
||||||
public void run() {
|
assertNull(e);
|
||||||
try (PrintWriter writer = new PrintWriter(src, true)) {
|
}
|
||||||
writer.println("test");
|
|
||||||
writer.println("toto");
|
assertNotNull(appli);
|
||||||
writer.println("long");
|
appli.start();
|
||||||
writer.println("exit");
|
assertFalse(appli.isRunning());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void interpretCommandTest() {
|
||||||
|
try (TestConsoleManager test = new TestConsoleManager()) {
|
||||||
|
ConsoleApplication appl = new ConsoleApplication(test, "", "");
|
||||||
|
|
||||||
|
appl.interpretCommand("invalid cmd \"due to misplaced\"quote");
|
||||||
|
assertEquals("Command line cannot be parsed", test.readNextLine());
|
||||||
|
|
||||||
|
appl.interpretCommand("");
|
||||||
|
|
||||||
|
final String message = "message";
|
||||||
|
try {
|
||||||
|
appl.add(new ICommand() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute(String... args) throws CommandRunException {
|
||||||
|
throw new CommandRunException(
|
||||||
|
CommandRunExceptionType.USAGE, message, this);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
});
|
@Override
|
||||||
test.start();
|
public String getCommandName() {
|
||||||
try {
|
return "fail";
|
||||||
th.join();
|
}
|
||||||
} catch (InterruptedException e) {
|
|
||||||
// TODO Auto-generated catch block
|
@Override
|
||||||
e.printStackTrace();
|
public void help(ConsoleManager manager,
|
||||||
}
|
String... args) throws IOException {
|
||||||
try {
|
manager.println(message);
|
||||||
test.join();
|
}
|
||||||
} catch (InterruptedException e) {
|
|
||||||
// TODO Auto-generated catch block
|
@Override
|
||||||
e.printStackTrace();
|
public String tip() {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
} catch (InvalidCommandName e) {
|
||||||
|
assertNull(e);
|
||||||
}
|
}
|
||||||
|
appl.interpretCommand("fail");
|
||||||
|
assertEquals(
|
||||||
|
Messages.getString("ConsoleApplication.cmd.failed", "fail"),
|
||||||
|
test.readNextLine());
|
||||||
|
assertEquals(message, test.readNextLine());
|
||||||
|
assertEquals(message, test.readNextLine());
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
fail("pipe creation"); //$NON-NLS-1$
|
assertNull(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,7 +36,6 @@ package fr.bigeon.gclc;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import fr.bigeon.gclc.ConsoleApplication;
|
|
||||||
import fr.bigeon.gclc.command.Command;
|
import fr.bigeon.gclc.command.Command;
|
||||||
import fr.bigeon.gclc.command.ExitCommand;
|
import fr.bigeon.gclc.command.ExitCommand;
|
||||||
import fr.bigeon.gclc.command.HelpExecutor;
|
import fr.bigeon.gclc.command.HelpExecutor;
|
||||||
@@ -74,7 +73,8 @@ public class ConsoleTestApplication extends ConsoleApplication {
|
|||||||
try {
|
try {
|
||||||
manager.println("Test command ran fine");
|
manager.println("Test command ran fine");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new CommandRunException("manager closed", e);
|
throw new CommandRunException("manager closed", e,
|
||||||
|
this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -92,12 +92,26 @@ public class ConsoleTestApplication extends ConsoleApplication {
|
|||||||
Thread.sleep(TWO_SECONDS);
|
Thread.sleep(TWO_SECONDS);
|
||||||
manager.println("done!");
|
manager.println("done!");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new CommandRunException("manager closed", e);
|
throw new CommandRunException("manager closed", e,
|
||||||
|
this);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
throw new CommandRunException("wait interrupted", e);
|
throw new CommandRunException("wait interrupted", e,
|
||||||
|
this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
add(new Command("failingCmd") {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String tip() {
|
||||||
|
return "A long execution command";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute(String... args) throws CommandRunException {
|
||||||
|
throw new CommandRunException("Failing command", this);
|
||||||
|
}
|
||||||
|
});
|
||||||
} catch (final InvalidCommandName e) {
|
} catch (final InvalidCommandName e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ public class GCLCConstantsTest {
|
|||||||
try {
|
try {
|
||||||
res = GCLCConstants.splitCommand("aCommand");
|
res = GCLCConstants.splitCommand("aCommand");
|
||||||
} catch (CommandParsingException e) {
|
} catch (CommandParsingException e) {
|
||||||
fail("Unable to parse simple command"); //$NON-NLS-1$
|
fail("Unable to parse simple command " + e.getLocalizedMessage()); //$NON-NLS-1$
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
assertTrue(res.size() == 1);
|
assertTrue(res.size() == 1);
|
||||||
@@ -71,7 +71,8 @@ public class GCLCConstantsTest {
|
|||||||
try {
|
try {
|
||||||
res = GCLCConstants.splitCommand("aCommand with some arguments");
|
res = GCLCConstants.splitCommand("aCommand with some arguments");
|
||||||
} catch (CommandParsingException e) {
|
} catch (CommandParsingException e) {
|
||||||
fail("Unable to parse command with arguments"); //$NON-NLS-1$
|
fail("Unable to parse command with arguments " + //$NON-NLS-1$
|
||||||
|
e.getLocalizedMessage());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
assertTrue(res.size() == 4);
|
assertTrue(res.size() == 4);
|
||||||
@@ -82,7 +83,8 @@ public class GCLCConstantsTest {
|
|||||||
try {
|
try {
|
||||||
res = GCLCConstants.splitCommand("aCommand with some arguments");
|
res = GCLCConstants.splitCommand("aCommand with some arguments");
|
||||||
} catch (CommandParsingException e) {
|
} catch (CommandParsingException e) {
|
||||||
fail("Unable to parse command with arguments and double whitspaces"); //$NON-NLS-1$
|
fail("Unable to parse command with arguments and double whitspaces " + //$NON-NLS-1$
|
||||||
|
e.getLocalizedMessage());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
assertTrue(res.size() == 4);
|
assertTrue(res.size() == 4);
|
||||||
@@ -94,7 +96,8 @@ public class GCLCConstantsTest {
|
|||||||
res = GCLCConstants
|
res = GCLCConstants
|
||||||
.splitCommand("aCommand \"with some\" arguments");
|
.splitCommand("aCommand \"with some\" arguments");
|
||||||
} catch (CommandParsingException e) {
|
} catch (CommandParsingException e) {
|
||||||
fail("Unable to parse command with string argument"); //$NON-NLS-1$
|
fail("Unable to parse command with string argument " + //$NON-NLS-1$
|
||||||
|
e.getLocalizedMessage());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
assertTrue(res.size() == 3);
|
assertTrue(res.size() == 3);
|
||||||
@@ -104,7 +107,8 @@ public class GCLCConstantsTest {
|
|||||||
try {
|
try {
|
||||||
res = GCLCConstants.splitCommand("aCommand with\\ some arguments");
|
res = GCLCConstants.splitCommand("aCommand with\\ some arguments");
|
||||||
} catch (CommandParsingException e) {
|
} catch (CommandParsingException e) {
|
||||||
fail("Unable to parse command with arguments with escaped whitspaces"); //$NON-NLS-1$
|
fail("Unable to parse command with arguments with escaped whitspaces " + //$NON-NLS-1$
|
||||||
|
e.getLocalizedMessage());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
assertTrue(res.size() == 3);
|
assertTrue(res.size() == 3);
|
||||||
@@ -115,7 +119,8 @@ public class GCLCConstantsTest {
|
|||||||
res = GCLCConstants
|
res = GCLCConstants
|
||||||
.splitCommand("aCommand wi\\\"th some arguments");
|
.splitCommand("aCommand wi\\\"th some arguments");
|
||||||
} catch (CommandParsingException e) {
|
} catch (CommandParsingException e) {
|
||||||
fail("Unable to parse command with string argument"); //$NON-NLS-1$
|
fail("Unable to parse command with string argument " + //$NON-NLS-1$
|
||||||
|
e.getLocalizedMessage());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
assertTrue(res.size() == 4);
|
assertTrue(res.size() == 4);
|
||||||
@@ -124,6 +129,19 @@ public class GCLCConstantsTest {
|
|||||||
assertTrue(res.get(2).equals("some"));
|
assertTrue(res.get(2).equals("some"));
|
||||||
assertTrue(res.get(3).equals("arguments"));
|
assertTrue(res.get(3).equals("arguments"));
|
||||||
|
|
||||||
|
try {
|
||||||
|
res = GCLCConstants
|
||||||
|
.splitCommand("aCommand with \"some arguments\"");
|
||||||
|
} catch (CommandParsingException e) {
|
||||||
|
fail("Unable to parse command ending with string argument " + //$NON-NLS-1$
|
||||||
|
e.getLocalizedMessage());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
assertTrue(res.size() == 3);
|
||||||
|
assertTrue(res.get(0).equals("aCommand"));
|
||||||
|
assertTrue(res.get(1).equals("with"));
|
||||||
|
assertTrue(res.get(2).equals("some arguments"));
|
||||||
|
|
||||||
// Wrong lines?
|
// Wrong lines?
|
||||||
try {
|
try {
|
||||||
res = GCLCConstants
|
res = GCLCConstants
|
||||||
@@ -131,6 +149,7 @@ public class GCLCConstantsTest {
|
|||||||
fail("Parsing argument with string cut");
|
fail("Parsing argument with string cut");
|
||||||
} catch (CommandParsingException e) {
|
} catch (CommandParsingException e) {
|
||||||
// OK
|
// OK
|
||||||
|
assertTrue(e.getLocalizedMessage(), true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,283 @@
|
|||||||
|
/*
|
||||||
|
* Copyright Bigeon Emmanuel (2014)
|
||||||
|
*
|
||||||
|
* emmanuel@bigeon.fr
|
||||||
|
*
|
||||||
|
* This software is a computer program whose purpose is to
|
||||||
|
* provide a generic framework for console applications.
|
||||||
|
*
|
||||||
|
* This software is governed by the CeCILL license under French law and
|
||||||
|
* abiding by the rules of distribution of free software. You can use,
|
||||||
|
* modify and/or redistribute the software under the terms of the CeCILL
|
||||||
|
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||||
|
* "http://www.cecill.info".
|
||||||
|
*
|
||||||
|
* As a counterpart to the access to the source code and rights to copy,
|
||||||
|
* modify and redistribute granted by the license, users are provided only
|
||||||
|
* with a limited warranty and the software's author, the holder of the
|
||||||
|
* economic rights, and the successive licensors have only limited
|
||||||
|
* liability.
|
||||||
|
*
|
||||||
|
* In this respect, the user's attention is drawn to the risks associated
|
||||||
|
* with loading, using, modifying and/or developing or reproducing the
|
||||||
|
* software by the user in light of its specific status of free software,
|
||||||
|
* that may mean that it is complicated to manipulate, and that also
|
||||||
|
* therefore means that it is reserved for developers and experienced
|
||||||
|
* professionals having in-depth computer knowledge. Users are therefore
|
||||||
|
* encouraged to load and test the software's suitability as regards their
|
||||||
|
* requirements in conditions enabling the security of their systems and/or
|
||||||
|
* data to be ensured and, more generally, to use and operate it in the
|
||||||
|
* same conditions as regards security.
|
||||||
|
*
|
||||||
|
* The fact that you are presently reading this means that you have had
|
||||||
|
* knowledge of the CeCILL license and that you accept its terms.
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* gclc:fr.bigeon.gclc.command.CommandParametersTest.java
|
||||||
|
* Created on: Nov 18, 2016
|
||||||
|
*/
|
||||||
|
package fr.bigeon.gclc.command;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* TODO
|
||||||
|
*
|
||||||
|
* @author Emmanuel Bigeon
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class CommandParametersTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test method for {@link fr.bigeon.gclc.command.CommandParameters#CommandParameters(java.util.Set, java.util.Set, boolean)}.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public final void testCommandParameters(){
|
||||||
|
Set<String> strings = new HashSet<>();
|
||||||
|
Set<String> bools = new HashSet<>();
|
||||||
|
CommandParameters parameters = new CommandParameters(bools, strings,
|
||||||
|
true);
|
||||||
|
|
||||||
|
assertFalse(parameters.parseArgs("-ungivenFlag"));
|
||||||
|
parameters = new CommandParameters(bools, strings, false);
|
||||||
|
|
||||||
|
assertTrue(parameters.parseArgs("-ungivenFlag"));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test method for {@link fr.bigeon.gclc.command.CommandParameters#get(java.lang.String)}.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public final void testGet(){
|
||||||
|
Set<String> strings = new HashSet<>();
|
||||||
|
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.parseArgs("-ungiven", "val");
|
||||||
|
assertNull(parameters.get("ungiven"));
|
||||||
|
assertNull(parameters.get("str"));
|
||||||
|
|
||||||
|
parameters.parseArgs("-str", "val");
|
||||||
|
assertNull(parameters.get("ungiven"));
|
||||||
|
assertEquals("val", parameters.get("str"));
|
||||||
|
|
||||||
|
parameters.parseArgs("-ungiven");
|
||||||
|
assertNull(parameters.get("ungiven"));
|
||||||
|
assertEquals("val", parameters.get("str"));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test method for {@link fr.bigeon.gclc.command.CommandParameters#getAdditionals()}.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public final void testGetAdditionals(){
|
||||||
|
Set<String> strings = new HashSet<>();
|
||||||
|
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());
|
||||||
|
|
||||||
|
parameters.parseArgs("-ungiven");
|
||||||
|
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)}.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public final void testGetBool(){
|
||||||
|
Set<String> strings = new HashSet<>();
|
||||||
|
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"));
|
||||||
|
|
||||||
|
parameters.parseArgs("-ungiven");
|
||||||
|
assertFalse(parameters.getBool("ungiven"));
|
||||||
|
assertTrue(parameters.getBool("boolFlag"));
|
||||||
|
|
||||||
|
parameters = new CommandParameters(bools, strings, false);
|
||||||
|
|
||||||
|
assertFalse(parameters.getBool("ungiven"));
|
||||||
|
assertFalse(parameters.getBool("boolFlag"));
|
||||||
|
|
||||||
|
parameters.parseArgs("-boolFlag");
|
||||||
|
assertTrue(parameters.getBool("boolFlag"));
|
||||||
|
assertFalse(parameters.getBool("ungiven"));
|
||||||
|
|
||||||
|
parameters.parseArgs("-ungiven");
|
||||||
|
assertFalse(parameters.getBool("ungiven"));
|
||||||
|
assertTrue(parameters.getBool("boolFlag"));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test method for {@link fr.bigeon.gclc.command.CommandParameters#parseArgs(java.lang.String[])}.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public final void testParseArgs(){
|
||||||
|
Set<String> strings = new HashSet<>();
|
||||||
|
Set<String> bools = new HashSet<>();
|
||||||
|
|
||||||
|
bools.add("boolFlag");
|
||||||
|
strings.add("str");
|
||||||
|
|
||||||
|
CommandParameters parameters = new CommandParameters(bools, strings,
|
||||||
|
true);
|
||||||
|
|
||||||
|
assertFalse(parameters.parseArgs("-ungivenFlag"));
|
||||||
|
assertFalse(parameters.parseArgs("-str"));
|
||||||
|
assertTrue(parameters.parseArgs("-boolFlag"));
|
||||||
|
assertTrue(parameters.parseArgs("-str", "-boolFlag"));
|
||||||
|
assertTrue(parameters.parseArgs("-boolFlag", "-str", "val"));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test method for {@link fr.bigeon.gclc.command.CommandParameters#set(java.lang.String, boolean)}.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public final void testSetStringBoolean(){
|
||||||
|
Set<String> strings = new HashSet<>();
|
||||||
|
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(){
|
||||||
|
Set<String> strings = new HashSet<>();
|
||||||
|
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"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,105 @@
|
|||||||
|
/*
|
||||||
|
* 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.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
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)}. */
|
||||||
|
@Test
|
||||||
|
public final void testAdd() {
|
||||||
|
CommandProvider provider = new CommandProvider();
|
||||||
|
try {
|
||||||
|
provider.add(new MockCommand(null));
|
||||||
|
fail("null name for command should be rejected");
|
||||||
|
} catch (InvalidCommandName e) {
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
provider.add(new MockCommand(""));
|
||||||
|
fail("null name for command should be rejected");
|
||||||
|
} catch (InvalidCommandName e) {
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
provider.add(new MockCommand("-name"));
|
||||||
|
fail("name with minus as starting character for command should be rejected");
|
||||||
|
} catch (InvalidCommandName e) {
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
provider.add(new MockCommand("name command"));
|
||||||
|
fail("name with space for command should be rejected");
|
||||||
|
} catch (InvalidCommandName e) {
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
ICommand mock = new MockCommand("name");
|
||||||
|
try {
|
||||||
|
provider.add(mock);
|
||||||
|
} catch (InvalidCommandName e) {
|
||||||
|
assertNull(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
provider.add(new MockCommand(mock.getCommandName()));
|
||||||
|
fail("already existing command name should be rejected");
|
||||||
|
} catch (InvalidCommandName e) {
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
provider.add(mock);
|
||||||
|
} catch (InvalidCommandName e) {
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
217
gclc/src/test/java/fr/bigeon/gclc/command/CommandTest.java
Normal file
217
gclc/src/test/java/fr/bigeon/gclc/command/CommandTest.java
Normal file
@@ -0,0 +1,217 @@
|
|||||||
|
/*
|
||||||
|
* Copyright Bigeon Emmanuel (2014)
|
||||||
|
*
|
||||||
|
* emmanuel@bigeon.fr
|
||||||
|
*
|
||||||
|
* This software is a computer program whose purpose is to
|
||||||
|
* provide a generic framework for console applications.
|
||||||
|
*
|
||||||
|
* This software is governed by the CeCILL license under French law and
|
||||||
|
* abiding by the rules of distribution of free software. You can use,
|
||||||
|
* modify and/or redistribute the software under the terms of the CeCILL
|
||||||
|
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||||
|
* "http://www.cecill.info".
|
||||||
|
*
|
||||||
|
* As a counterpart to the access to the source code and rights to copy,
|
||||||
|
* modify and redistribute granted by the license, users are provided only
|
||||||
|
* with a limited warranty and the software's author, the holder of the
|
||||||
|
* economic rights, and the successive licensors have only limited
|
||||||
|
* liability.
|
||||||
|
*
|
||||||
|
* In this respect, the user's attention is drawn to the risks associated
|
||||||
|
* with loading, using, modifying and/or developing or reproducing the
|
||||||
|
* software by the user in light of its specific status of free software,
|
||||||
|
* that may mean that it is complicated to manipulate, and that also
|
||||||
|
* therefore means that it is reserved for developers and experienced
|
||||||
|
* professionals having in-depth computer knowledge. Users are therefore
|
||||||
|
* encouraged to load and test the software's suitability as regards their
|
||||||
|
* requirements in conditions enabling the security of their systems and/or
|
||||||
|
* data to be ensured and, more generally, to use and operate it in the
|
||||||
|
* same conditions as regards security.
|
||||||
|
*
|
||||||
|
* The fact that you are presently reading this means that you have had
|
||||||
|
* knowledge of the CeCILL license and that you accept its terms.
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* gclc:fr.bigeon.gclc.command.CommandTest.java
|
||||||
|
* Created on: Nov 19, 2016
|
||||||
|
*/
|
||||||
|
package fr.bigeon.gclc.command;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import fr.bigeon.gclc.exception.CommandRunException;
|
||||||
|
import fr.bigeon.gclc.test.utils.TestConsoleManager;
|
||||||
|
|
||||||
|
/** <p>
|
||||||
|
* TODO
|
||||||
|
*
|
||||||
|
* @author Emmanuel Bigeon */
|
||||||
|
public class CommandTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void test() {
|
||||||
|
try (TestConsoleManager test = new TestConsoleManager()) {
|
||||||
|
Command cmd;
|
||||||
|
cmd = new Command("name") {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String tip() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute(String... args) throws CommandRunException {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
};
|
||||||
|
cmd.help(test);
|
||||||
|
|
||||||
|
cmd = new Command("name") {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String tip() {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute(String... args) throws CommandRunException {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
};
|
||||||
|
cmd.help(test);
|
||||||
|
|
||||||
|
cmd = new Command("name") {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String tip() {
|
||||||
|
return "tip";
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see fr.bigeon.gclc.command.Command#brief() */
|
||||||
|
@Override
|
||||||
|
protected String brief() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute(String... args) throws CommandRunException {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
};
|
||||||
|
cmd.help(test);
|
||||||
|
|
||||||
|
cmd = new Command("name") {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String tip() {
|
||||||
|
return "tip";
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see fr.bigeon.gclc.command.Command#brief() */
|
||||||
|
@Override
|
||||||
|
protected String brief() {
|
||||||
|
return "brief";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute(String... args) throws CommandRunException {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
};
|
||||||
|
cmd.help(test);
|
||||||
|
|
||||||
|
cmd = new Command("name") {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String tip() {
|
||||||
|
return "tip";
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see fr.bigeon.gclc.command.Command#usageDetail() */
|
||||||
|
@Override
|
||||||
|
protected String usageDetail() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute(String... args) throws CommandRunException {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
};
|
||||||
|
cmd.help(test);
|
||||||
|
|
||||||
|
cmd = new Command("name") {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String tip() {
|
||||||
|
return "tip";
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see fr.bigeon.gclc.command.Command#usageDetail() */
|
||||||
|
@Override
|
||||||
|
protected String usageDetail() {
|
||||||
|
return "details";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute(String... args) throws CommandRunException {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
};
|
||||||
|
cmd.help(test);
|
||||||
|
cmd = new Command("name") {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String tip() {
|
||||||
|
return "tip";
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see fr.bigeon.gclc.command.Command#usageDetail() */
|
||||||
|
@Override
|
||||||
|
protected String usageDetail() {
|
||||||
|
return "details" + System.lineSeparator();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute(String... args) throws CommandRunException {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
};
|
||||||
|
cmd.help(test);
|
||||||
|
|
||||||
|
cmd = new Command("name") {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String tip() {
|
||||||
|
return "tip";
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see fr.bigeon.gclc.command.Command#usageDetail() */
|
||||||
|
@Override
|
||||||
|
protected String usageDetail() {
|
||||||
|
return "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute(String... args) throws CommandRunException {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
};
|
||||||
|
cmd.help(test);
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
assertNull(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
137
gclc/src/test/java/fr/bigeon/gclc/command/HelpExecutorTest.java
Normal file
137
gclc/src/test/java/fr/bigeon/gclc/command/HelpExecutorTest.java
Normal file
@@ -0,0 +1,137 @@
|
|||||||
|
/*
|
||||||
|
* Copyright Bigeon Emmanuel (2014)
|
||||||
|
*
|
||||||
|
* emmanuel@bigeon.fr
|
||||||
|
*
|
||||||
|
* This software is a computer program whose purpose is to
|
||||||
|
* provide a generic framework for console applications.
|
||||||
|
*
|
||||||
|
* This software is governed by the CeCILL license under French law and
|
||||||
|
* abiding by the rules of distribution of free software. You can use,
|
||||||
|
* modify and/or redistribute the software under the terms of the CeCILL
|
||||||
|
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||||
|
* "http://www.cecill.info".
|
||||||
|
*
|
||||||
|
* As a counterpart to the access to the source code and rights to copy,
|
||||||
|
* modify and redistribute granted by the license, users are provided only
|
||||||
|
* with a limited warranty and the software's author, the holder of the
|
||||||
|
* economic rights, and the successive licensors have only limited
|
||||||
|
* liability.
|
||||||
|
*
|
||||||
|
* In this respect, the user's attention is drawn to the risks associated
|
||||||
|
* with loading, using, modifying and/or developing or reproducing the
|
||||||
|
* software by the user in light of its specific status of free software,
|
||||||
|
* that may mean that it is complicated to manipulate, and that also
|
||||||
|
* therefore means that it is reserved for developers and experienced
|
||||||
|
* professionals having in-depth computer knowledge. Users are therefore
|
||||||
|
* encouraged to load and test the software's suitability as regards their
|
||||||
|
* requirements in conditions enabling the security of their systems and/or
|
||||||
|
* data to be ensured and, more generally, to use and operate it in the
|
||||||
|
* same conditions as regards security.
|
||||||
|
*
|
||||||
|
* The fact that you are presently reading this means that you have had
|
||||||
|
* knowledge of the CeCILL license and that you accept its terms.
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* gclc:fr.bigeon.gclc.command.HelpExecutorTest.java
|
||||||
|
* Created on: Nov 19, 2016
|
||||||
|
*/
|
||||||
|
package fr.bigeon.gclc.command;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import fr.bigeon.gclc.exception.CommandRunException;
|
||||||
|
import fr.bigeon.gclc.test.utils.TestConsoleManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* TODO
|
||||||
|
*
|
||||||
|
* @author Emmanuel Bigeon
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class HelpExecutorTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test method for {@link fr.bigeon.gclc.command.HelpExecutor#HelpExecutor(java.lang.String, fr.bigeon.gclc.manager.ConsoleManager, fr.bigeon.gclc.command.ICommand)}.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public final void testHelpExecutor(){
|
||||||
|
HelpExecutor help;
|
||||||
|
try {
|
||||||
|
help = new HelpExecutor("?", null, new MockCommand("mock"));
|
||||||
|
fail("help is an interactive command, should need console manager");
|
||||||
|
} catch (Exception e) {
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
try (TestConsoleManager test = new TestConsoleManager()) {
|
||||||
|
help = new HelpExecutor("?", test, new MockCommand("mock"));
|
||||||
|
} catch (Exception e) {
|
||||||
|
assertNull(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test method for {@link fr.bigeon.gclc.command.HelpExecutor#execute(java.lang.String[])}.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public final void testExecute(){
|
||||||
|
try {
|
||||||
|
TestConsoleManager test = new TestConsoleManager();
|
||||||
|
HelpExecutor help = new HelpExecutor("?", test,
|
||||||
|
new Command("mock") {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute(String... args) throws CommandRunException {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String tip() {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
help.execute();
|
||||||
|
test.close();
|
||||||
|
|
||||||
|
try {
|
||||||
|
help.execute();
|
||||||
|
fail("manager closed shall provoke failure of help command execution");
|
||||||
|
} catch (Exception e) {
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
assertNull(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test method for {@link fr.bigeon.gclc.command.HelpExecutor#tip()}.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public final void testTip(){
|
||||||
|
try (TestConsoleManager test = new TestConsoleManager()) {
|
||||||
|
HelpExecutor help = new HelpExecutor("?", test,
|
||||||
|
new MockCommand("mock"));
|
||||||
|
help.tip();
|
||||||
|
help.help(test);
|
||||||
|
} catch (Exception e) {
|
||||||
|
assertNull(e);
|
||||||
|
}
|
||||||
|
try (TestConsoleManager test = new TestConsoleManager()) {
|
||||||
|
HelpExecutor help = new HelpExecutor("?", test,
|
||||||
|
new SubedCommand("sub", new MockCommand("mock")));
|
||||||
|
help.tip();
|
||||||
|
help.help(test);
|
||||||
|
} catch (Exception e) {
|
||||||
|
assertNull(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,649 @@
|
|||||||
|
/*
|
||||||
|
* Copyright Bigeon Emmanuel (2014)
|
||||||
|
*
|
||||||
|
* emmanuel@bigeon.fr
|
||||||
|
*
|
||||||
|
* This software is a computer program whose purpose is to
|
||||||
|
* provide a generic framework for console applications.
|
||||||
|
*
|
||||||
|
* This software is governed by the CeCILL license under French law and
|
||||||
|
* abiding by the rules of distribution of free software. You can use,
|
||||||
|
* modify and/or redistribute the software under the terms of the CeCILL
|
||||||
|
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||||
|
* "http://www.cecill.info".
|
||||||
|
*
|
||||||
|
* As a counterpart to the access to the source code and rights to copy,
|
||||||
|
* modify and redistribute granted by the license, users are provided only
|
||||||
|
* with a limited warranty and the software's author, the holder of the
|
||||||
|
* economic rights, and the successive licensors have only limited
|
||||||
|
* liability.
|
||||||
|
*
|
||||||
|
* In this respect, the user's attention is drawn to the risks associated
|
||||||
|
* with loading, using, modifying and/or developing or reproducing the
|
||||||
|
* software by the user in light of its specific status of free software,
|
||||||
|
* that may mean that it is complicated to manipulate, and that also
|
||||||
|
* therefore means that it is reserved for developers and experienced
|
||||||
|
* professionals having in-depth computer knowledge. Users are therefore
|
||||||
|
* encouraged to load and test the software's suitability as regards their
|
||||||
|
* requirements in conditions enabling the security of their systems and/or
|
||||||
|
* data to be ensured and, more generally, to use and operate it in the
|
||||||
|
* same conditions as regards security.
|
||||||
|
*
|
||||||
|
* The fact that you are presently reading this means that you have had
|
||||||
|
* knowledge of the CeCILL license and that you accept its terms.
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* gclc:fr.bigeon.gclc.command.ParametrizedCommandTest.java
|
||||||
|
* Created on: Nov 18, 2016
|
||||||
|
*/
|
||||||
|
package fr.bigeon.gclc.command;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import fr.bigeon.gclc.exception.CommandRunException;
|
||||||
|
import fr.bigeon.gclc.exception.InvalidParameterException;
|
||||||
|
import fr.bigeon.gclc.test.utils.TestConsoleManager;
|
||||||
|
|
||||||
|
/** <p>
|
||||||
|
* TODO
|
||||||
|
*
|
||||||
|
* @author Emmanuel Bigeon */
|
||||||
|
public class ParametrizedCommandTest {
|
||||||
|
|
||||||
|
/** Test method for
|
||||||
|
* {@link fr.bigeon.gclc.command.ParametrizedCommand#ParametrizedCommand(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String)}. */
|
||||||
|
@Test
|
||||||
|
public final void testParametrizedCommandConsoleManagerString() {
|
||||||
|
try (TestConsoleManager test = new TestConsoleManager()) {
|
||||||
|
ParametrizedCommand cmd = new ParametrizedCommand(test, "name") {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String tip() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doExecute(CommandParameters parameters) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
};
|
||||||
|
assertTrue(cmd.isStrict());
|
||||||
|
assertTrue(cmd.isInteractive());
|
||||||
|
} catch (IOException e) {
|
||||||
|
fail("Unexpected exception in creation");
|
||||||
|
assertNull(e);
|
||||||
|
}
|
||||||
|
ParametrizedCommand cmd = new ParametrizedCommand(null, "name") {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String tip() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doExecute(CommandParameters parameters) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
};
|
||||||
|
assertTrue(cmd.isStrict());
|
||||||
|
assertFalse(cmd.isInteractive());
|
||||||
|
cmd = new ParametrizedCommand("name") {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String tip() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doExecute(CommandParameters parameters) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
};
|
||||||
|
assertTrue(cmd.isStrict());
|
||||||
|
assertFalse(cmd.isInteractive());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Test method for
|
||||||
|
* {@link fr.bigeon.gclc.command.ParametrizedCommand#ParametrizedCommand(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String, boolean)}. */
|
||||||
|
@Test
|
||||||
|
public final void testParametrizedCommandConsoleManagerStringBoolean() {
|
||||||
|
try (TestConsoleManager test = new TestConsoleManager()) {
|
||||||
|
ParametrizedCommand cmd = new ParametrizedCommand(test, "name",
|
||||||
|
false) {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String tip() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doExecute(CommandParameters parameters) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
};
|
||||||
|
assertFalse(cmd.isStrict());
|
||||||
|
assertTrue(cmd.isInteractive());
|
||||||
|
} catch (IOException e) {
|
||||||
|
fail("Unexpected exception in creation");
|
||||||
|
assertNull(e);
|
||||||
|
}
|
||||||
|
ParametrizedCommand cmd = new ParametrizedCommand(null, "name", false) {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String tip() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doExecute(CommandParameters parameters) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
};
|
||||||
|
assertFalse(cmd.isStrict());
|
||||||
|
assertFalse(cmd.isInteractive());
|
||||||
|
cmd = new ParametrizedCommand("name", false) {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String tip() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doExecute(CommandParameters parameters) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
};
|
||||||
|
assertFalse(cmd.isStrict());
|
||||||
|
assertFalse(cmd.isInteractive());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Test method for
|
||||||
|
* {@link fr.bigeon.gclc.command.ParametrizedCommand#addParameter(java.lang.String, boolean, boolean)}. */
|
||||||
|
@Test
|
||||||
|
public final void testAddParameter() {
|
||||||
|
ParametrizedCommand cmd = new ParametrizedCommand(null, "name") {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String tip() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doExecute(CommandParameters parameters) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
};
|
||||||
|
cmd = new ParametrizedCommand(null, "name", true) {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String tip() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doExecute(CommandParameters parameters) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// XXX Boolean flag should not be specified mandatory! They are by
|
||||||
|
// nature qualified
|
||||||
|
try {
|
||||||
|
cmd.addParameter("boolFlag", false, true);
|
||||||
|
fail("Boolean parameters should never be needed specified");
|
||||||
|
} catch (InvalidParameterException e) {
|
||||||
|
// OK
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
String str = "str";
|
||||||
|
try {
|
||||||
|
assertTrue(cmd.getBooleanParameters().isEmpty());
|
||||||
|
assertTrue(cmd.getStringParameters().isEmpty());
|
||||||
|
cmd.addParameter("boolFlag", false, false);
|
||||||
|
assertEquals(1, cmd.getBooleanParameters().size());
|
||||||
|
assertTrue(cmd.getStringParameters().isEmpty());
|
||||||
|
cmd.addParameter(str, true, false);
|
||||||
|
assertEquals(1, cmd.getBooleanParameters().size());
|
||||||
|
assertEquals(1, cmd.getStringParameters().size());
|
||||||
|
assertFalse(cmd.isNeeded(str));
|
||||||
|
cmd.addParameter("boolFlag", false, false);
|
||||||
|
assertEquals(1, cmd.getBooleanParameters().size());
|
||||||
|
assertEquals(1, cmd.getStringParameters().size());
|
||||||
|
cmd.addParameter(str, true, true);
|
||||||
|
assertEquals(1, cmd.getBooleanParameters().size());
|
||||||
|
assertEquals(1, cmd.getStringParameters().size());
|
||||||
|
assertTrue(cmd.isNeeded(str));
|
||||||
|
cmd.addParameter(str, true, false);
|
||||||
|
assertEquals(1, cmd.getBooleanParameters().size());
|
||||||
|
assertEquals(1, cmd.getStringParameters().size());
|
||||||
|
assertTrue(cmd.isNeeded(str));
|
||||||
|
} catch (InvalidParameterException e) {
|
||||||
|
fail("Unexpected error in addition of legitimate parameter");
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
cmd.addParameter(str, false, false);
|
||||||
|
fail("parameter type conversion shall fail");
|
||||||
|
} catch (InvalidParameterException e) {
|
||||||
|
// OK
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
cmd.addParameter("boolFlag", true, false);
|
||||||
|
fail("parameter type conversion shall fail");
|
||||||
|
} catch (InvalidParameterException e) {
|
||||||
|
// OK
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
cmd.addParameter("boolFlag", false, true);
|
||||||
|
fail("parameter type conversion shall fail");
|
||||||
|
} catch (InvalidParameterException e) {
|
||||||
|
// OK
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
cmd.addParameter("boolFlag", true, true);
|
||||||
|
fail("parameter type conversion shall fail");
|
||||||
|
} catch (InvalidParameterException e) {
|
||||||
|
// OK
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Test method for
|
||||||
|
* {@link fr.bigeon.gclc.command.ParametrizedCommand#execute(java.lang.String[])}. */
|
||||||
|
@Test
|
||||||
|
public final void testExecute() {
|
||||||
|
final String addParam = "additional";
|
||||||
|
final String str1 = "str1";
|
||||||
|
final String str2 = "str2";
|
||||||
|
final String bool1 = "bool1";
|
||||||
|
final String bool2 = "bool2";
|
||||||
|
|
||||||
|
// Test on command with no needed elements
|
||||||
|
ParametrizedCommand cmd = new ParametrizedCommand("name", false) {
|
||||||
|
private boolean evenCall = true;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String tip() {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doExecute(CommandParameters parameters) {
|
||||||
|
assertTrue(parameters.getBooleanArgumentKeys().isEmpty());
|
||||||
|
assertTrue(parameters.getStringArgumentKeys().isEmpty());
|
||||||
|
if (evenCall) {
|
||||||
|
assertTrue(parameters.getAdditionals().isEmpty());
|
||||||
|
evenCall = false;
|
||||||
|
} else {
|
||||||
|
assertEquals(1, parameters.getAdditionals().size());
|
||||||
|
assertTrue(parameters.getAdditionals().contains(addParam));
|
||||||
|
evenCall = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
try {
|
||||||
|
cmd.execute();
|
||||||
|
cmd.execute("-" + addParam);
|
||||||
|
cmd.execute(addParam);
|
||||||
|
cmd.execute("-" + addParam, addParam);
|
||||||
|
} catch (CommandRunException e) {
|
||||||
|
assertNull(e);
|
||||||
|
fail("unepected error");
|
||||||
|
}
|
||||||
|
cmd = new ParametrizedCommand("name", false) {
|
||||||
|
private int call = 0;
|
||||||
|
{
|
||||||
|
|
||||||
|
try {
|
||||||
|
addParameter(str1, true, false);
|
||||||
|
addParameter(str2, true, false);
|
||||||
|
addParameter(bool1, false, false);
|
||||||
|
addParameter(bool2, false, false);
|
||||||
|
} catch (InvalidParameterException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String tip() {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doExecute(CommandParameters parameters) {
|
||||||
|
assertEquals(2, parameters.getBooleanArgumentKeys().size());
|
||||||
|
assertEquals(2, parameters.getStringArgumentKeys().size());
|
||||||
|
switch (call) {
|
||||||
|
case 0:
|
||||||
|
case 1:
|
||||||
|
case 2:
|
||||||
|
case 3:
|
||||||
|
assertNull(parameters.get(str1));
|
||||||
|
assertNull(parameters.get(str2));
|
||||||
|
assertFalse(parameters.getBool(bool1));
|
||||||
|
assertFalse(parameters.getBool(bool2));
|
||||||
|
call++;
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
assertEquals(str2, parameters.get(str1));
|
||||||
|
assertNull(parameters.get(str2));
|
||||||
|
assertFalse(parameters.getBool(bool1));
|
||||||
|
assertFalse(parameters.getBool(bool2));
|
||||||
|
call++;
|
||||||
|
break;
|
||||||
|
case 5:
|
||||||
|
assertEquals(str2, parameters.get(str1));
|
||||||
|
assertNull(parameters.get(str2));
|
||||||
|
assertTrue(parameters.getBool(bool1));
|
||||||
|
assertFalse(parameters.getBool(bool2));
|
||||||
|
call = 0;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
try {
|
||||||
|
cmd.execute();
|
||||||
|
cmd.execute("-" + addParam);
|
||||||
|
cmd.execute(addParam);
|
||||||
|
cmd.execute("-" + addParam, addParam);
|
||||||
|
cmd.execute("-" + str1, str2);
|
||||||
|
cmd.execute("-" + str1, str2, "-" + bool1);
|
||||||
|
} catch (CommandRunException e) {
|
||||||
|
assertNull(e);
|
||||||
|
fail("unepected error");
|
||||||
|
}
|
||||||
|
cmd = new ParametrizedCommand("name", true) {
|
||||||
|
private int call = 0;
|
||||||
|
{
|
||||||
|
|
||||||
|
try {
|
||||||
|
addParameter(str1, true, false);
|
||||||
|
addParameter(str2, true, false);
|
||||||
|
addParameter(bool1, false, false);
|
||||||
|
addParameter(bool2, false, false);
|
||||||
|
} catch (InvalidParameterException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String tip() {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doExecute(CommandParameters parameters) {
|
||||||
|
assertEquals(2, parameters.getBooleanArgumentKeys().size());
|
||||||
|
assertEquals(2, parameters.getStringArgumentKeys().size());
|
||||||
|
switch (call) {
|
||||||
|
case 0:
|
||||||
|
case 1:
|
||||||
|
assertNull(parameters.get(str1));
|
||||||
|
assertNull(parameters.get(str2));
|
||||||
|
assertFalse(parameters.getBool(bool1));
|
||||||
|
assertFalse(parameters.getBool(bool2));
|
||||||
|
call++;
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
assertEquals(str2, parameters.get(str1));
|
||||||
|
assertNull(parameters.get(str2));
|
||||||
|
assertFalse(parameters.getBool(bool1));
|
||||||
|
assertFalse(parameters.getBool(bool2));
|
||||||
|
call++;
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
assertEquals(str2, parameters.get(str1));
|
||||||
|
assertNull(parameters.get(str2));
|
||||||
|
assertTrue(parameters.getBool(bool1));
|
||||||
|
assertFalse(parameters.getBool(bool2));
|
||||||
|
call = 0;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
try {
|
||||||
|
cmd.execute();
|
||||||
|
cmd.execute(addParam);
|
||||||
|
cmd.execute("-" + str1, str2);
|
||||||
|
cmd.execute("-" + str1, str2, "-" + bool1);
|
||||||
|
} catch (CommandRunException e) {
|
||||||
|
assertNull(e);
|
||||||
|
fail("unepected error");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
cmd.execute("-" + addParam);
|
||||||
|
fail("Strict should fail with unexpected argument");
|
||||||
|
} catch (CommandRunException e) {
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
// Test on command with missing needed elements
|
||||||
|
cmd = new ParametrizedCommand("name", false) {
|
||||||
|
private final int call = 0;
|
||||||
|
{
|
||||||
|
|
||||||
|
try {
|
||||||
|
addParameter(str1, true, true);
|
||||||
|
addParameter(str2, true, false);
|
||||||
|
addParameter(bool1, false, false);
|
||||||
|
addParameter(bool2, false, false);
|
||||||
|
} catch (InvalidParameterException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String tip() {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doExecute(CommandParameters parameters) {
|
||||||
|
assertEquals(str2, parameters.get(str1));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
try {
|
||||||
|
cmd.execute("-" + str1, str2);
|
||||||
|
cmd.execute("-" + str1, str2, "-" + bool1);
|
||||||
|
cmd.execute("-" + str1, str2, "-" + addParam);
|
||||||
|
cmd.execute("-" + str1, str2, addParam);
|
||||||
|
cmd.execute("-" + str1, str2, "-" + addParam, addParam);
|
||||||
|
} catch (CommandRunException e) {
|
||||||
|
assertNull(e);
|
||||||
|
fail("unepected error");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
cmd.execute();
|
||||||
|
fail("needed " + str1 + " not provided shall fail");
|
||||||
|
} catch (CommandRunException e) {
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
cmd = new ParametrizedCommand("name", true) {
|
||||||
|
private final int call = 0;
|
||||||
|
{
|
||||||
|
|
||||||
|
try {
|
||||||
|
addParameter(str1, true, true);
|
||||||
|
addParameter(str2, true, false);
|
||||||
|
addParameter(bool1, false, false);
|
||||||
|
addParameter(bool2, false, false);
|
||||||
|
} catch (InvalidParameterException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String tip() {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doExecute(CommandParameters parameters) {
|
||||||
|
//
|
||||||
|
assertEquals(str2, parameters.get(str1));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
try {
|
||||||
|
cmd.execute("-" + str1, str2);
|
||||||
|
cmd.execute("-" + str1, str2, "-" + bool1);
|
||||||
|
cmd.execute("-" + str1, str2, addParam);
|
||||||
|
} catch (CommandRunException e) {
|
||||||
|
assertNull(e);
|
||||||
|
fail("unepected error");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
cmd.execute();
|
||||||
|
fail("needed " + str1 + " not provided shall fail");
|
||||||
|
} catch (CommandRunException e) {
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
cmd.execute("-" + str1, str2, "-" + addParam);
|
||||||
|
fail("unepected error");
|
||||||
|
} catch (CommandRunException e) {
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
cmd.execute("-" + str1, str2, "-" + addParam, addParam);
|
||||||
|
fail("unepected error");
|
||||||
|
} catch (CommandRunException e) {
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
// TODO Test of interactive not providing and providing all needed
|
||||||
|
try (TestConsoleManager test = new TestConsoleManager()) {
|
||||||
|
cmd = new ParametrizedCommand(test, "name", false) {
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
addParameter(str1, true, true);
|
||||||
|
addParameter(str2, true, false);
|
||||||
|
addParameter(bool1, false, false);
|
||||||
|
addParameter(bool2, false, false);
|
||||||
|
} catch (InvalidParameterException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String tip() {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doExecute(CommandParameters parameters) {
|
||||||
|
assertEquals(str2, parameters.get(str1));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
try {
|
||||||
|
cmd.execute("-" + str1, str2);
|
||||||
|
cmd.execute("-" + str1, str2, "-" + bool1);
|
||||||
|
cmd.execute("-" + str1, str2, addParam);
|
||||||
|
cmd.execute("-" + str1, str2, "-" + addParam);
|
||||||
|
cmd.execute("-" + str1, str2, "-" + addParam, addParam);
|
||||||
|
} catch (CommandRunException e) {
|
||||||
|
assertNull(e);
|
||||||
|
fail("unepected error");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
|
||||||
|
Thread th = new Thread(new Runnable() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
assertEquals(str1, test.readNextLine());
|
||||||
|
test.type("");
|
||||||
|
assertEquals(str1, test.readNextLine());
|
||||||
|
test.type(str2);
|
||||||
|
} catch (IOException e) {
|
||||||
|
assertNull(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
th.start();
|
||||||
|
|
||||||
|
cmd.execute();
|
||||||
|
|
||||||
|
th.join();
|
||||||
|
|
||||||
|
th = new Thread(new Runnable() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
assertEquals(str1, test.readNextLine());
|
||||||
|
test.type(str2);
|
||||||
|
} catch (IOException e) {
|
||||||
|
assertNull(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
th.start();
|
||||||
|
|
||||||
|
cmd.execute("-" + addParam);
|
||||||
|
|
||||||
|
th.join();
|
||||||
|
} catch (CommandRunException | InterruptedException e) {
|
||||||
|
assertNull(e);
|
||||||
|
fail("unepected error");
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
assertNull(e);
|
||||||
|
fail("unepected error");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
TestConsoleManager test = new TestConsoleManager();
|
||||||
|
cmd = new ParametrizedCommand(test, "name") {
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
addParameter(str1, true, true);
|
||||||
|
addParameter(str2, true, false);
|
||||||
|
addParameter(bool1, false, false);
|
||||||
|
addParameter(bool2, false, false);
|
||||||
|
} catch (InvalidParameterException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String tip() {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doExecute(CommandParameters parameters) {
|
||||||
|
assertEquals(str2, parameters.get(str1));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
test.close();
|
||||||
|
cmd.execute("-" + str1, str2);
|
||||||
|
cmd.execute("-" + addParam);
|
||||||
|
fail("Closed manager shall cause error");
|
||||||
|
} catch (IOException e) {
|
||||||
|
assertNull(e);
|
||||||
|
} catch (CommandRunException e) {
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,176 @@
|
|||||||
|
/*
|
||||||
|
* 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.ConsoleTestApplication;
|
||||||
|
import fr.bigeon.gclc.exception.CommandRunException;
|
||||||
|
import fr.bigeon.gclc.exception.CommandRunExceptionType;
|
||||||
|
import fr.bigeon.gclc.test.utils.TestConsoleManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* TODO
|
||||||
|
*
|
||||||
|
* @author Emmanuel Bigeon
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("static-method")
|
||||||
|
public class ScriptExecutionTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test method for {@link fr.bigeon.gclc.command.ScriptExecution#execute(java.lang.String[])}.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testExecute() {
|
||||||
|
TestConsoleManager test;
|
||||||
|
try {
|
||||||
|
test = new TestConsoleManager();
|
||||||
|
} catch (IOException e2) {
|
||||||
|
fail("creation of console manager failed"); //$NON-NLS-1$
|
||||||
|
assertNotNull(e2);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
ConsoleTestApplication app = new ConsoleTestApplication(
|
||||||
|
test);
|
||||||
|
ScriptExecution exec = new ScriptExecution("script", app, "#", //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
|
Charset.forName("UTF-8"));
|
||||||
|
try {
|
||||||
|
exec.execute();
|
||||||
|
fail("execution of script command with no file should fail"); //$NON-NLS-1$
|
||||||
|
} catch (CommandRunException e1) {
|
||||||
|
// ok
|
||||||
|
assertEquals(exec, e1.getSource());
|
||||||
|
assertEquals(CommandRunExceptionType.USAGE, e1.getType());
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
exec.execute("src/test/resources/scripts/withprependSpace.txt"); //$NON-NLS-1$
|
||||||
|
fail("execution of script with lines begining with space should fail"); //$NON-NLS-1$
|
||||||
|
} catch (CommandRunException e1) {
|
||||||
|
// ok
|
||||||
|
assertEquals(exec, e1.getSource());
|
||||||
|
assertEquals(CommandRunExceptionType.EXECUTION, e1.getType());
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
exec.execute("src/test/resources/scripts/invalidCmdParse.txt"); //$NON-NLS-1$
|
||||||
|
fail("execution of script with invalid command line should fail"); //$NON-NLS-1$
|
||||||
|
} catch (CommandRunException e1) {
|
||||||
|
// ok
|
||||||
|
assertEquals(exec, e1.getSource());
|
||||||
|
assertEquals(CommandRunExceptionType.EXECUTION, e1.getType());
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
exec.execute("src/test/resources/scripts/invalidCmd.txt"); //$NON-NLS-1$
|
||||||
|
fail("execution of script with invalid command should fail"); //$NON-NLS-1$
|
||||||
|
} catch (CommandRunException e1) {
|
||||||
|
// ok
|
||||||
|
assertEquals(exec, e1.getSource());
|
||||||
|
assertEquals(CommandRunExceptionType.EXECUTION, e1.getType());
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
exec.execute("src/test/resources/scripts/failingCmdInvoc.txt"); //$NON-NLS-1$
|
||||||
|
fail("execution of script with failing command should fail"); //$NON-NLS-1$
|
||||||
|
} catch (CommandRunException e1) {
|
||||||
|
// ok
|
||||||
|
assertEquals(exec, e1.getSource());
|
||||||
|
assertEquals(CommandRunExceptionType.EXECUTION, e1.getType());
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
exec.execute("src/test/resources/scripts/someNonExisting.file"); //$NON-NLS-1$
|
||||||
|
fail("execution of script with unexisting file should fail"); //$NON-NLS-1$
|
||||||
|
} catch (CommandRunException e1) {
|
||||||
|
// ok
|
||||||
|
assertEquals(exec, e1.getSource());
|
||||||
|
assertEquals(CommandRunExceptionType.EXECUTION, e1.getType());
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
exec.execute("src/test/resources/script1.txt"); //$NON-NLS-1$
|
||||||
|
exec.execute("src/test/resources/script2.txt"); //$NON-NLS-1$
|
||||||
|
exec.execute("src/test/resources/script3.txt"); //$NON-NLS-1$
|
||||||
|
exec.execute("src/test/resources/script4.txt"); //$NON-NLS-1$
|
||||||
|
exec.execute("src/test/resources/script5.txt", "test"); //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
|
} catch (CommandRunException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
fail("execution of wellformed script should not fail"); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
test.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Test method for {@link fr.bigeon.gclc.command.ScriptExecution#tip()}. */
|
||||||
|
@Test
|
||||||
|
public void testTip() {
|
||||||
|
ScriptExecution exec = new ScriptExecution("script", null, "#", //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
|
Charset.forName("UTF-8"));
|
||||||
|
exec.tip();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Test method for
|
||||||
|
* {@link fr.bigeon.gclc.command.ScriptExecution#help(fr.bigeon.gclc.manager.ConsoleManager, String...)}. */
|
||||||
|
@Test
|
||||||
|
public void testHelp() {
|
||||||
|
ScriptExecution exec = new ScriptExecution("script", null, "#", //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
|
Charset.forName("UTF-8"));
|
||||||
|
try (TestConsoleManager test = new TestConsoleManager()) {
|
||||||
|
exec.help(test);
|
||||||
|
exec.help(test, "ignored element");
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
fail("unexpected error in help invocation"); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
390
gclc/src/test/java/fr/bigeon/gclc/command/SubedCommandTest.java
Normal file
390
gclc/src/test/java/fr/bigeon/gclc/command/SubedCommandTest.java
Normal file
@@ -0,0 +1,390 @@
|
|||||||
|
/*
|
||||||
|
* Copyright Bigeon Emmanuel (2014)
|
||||||
|
*
|
||||||
|
* emmanuel@bigeon.fr
|
||||||
|
*
|
||||||
|
* This software is a computer program whose purpose is to
|
||||||
|
* provide a generic framework for console applications.
|
||||||
|
*
|
||||||
|
* This software is governed by the CeCILL license under French law and
|
||||||
|
* abiding by the rules of distribution of free software. You can use,
|
||||||
|
* modify and/or redistribute the software under the terms of the CeCILL
|
||||||
|
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||||
|
* "http://www.cecill.info".
|
||||||
|
*
|
||||||
|
* As a counterpart to the access to the source code and rights to copy,
|
||||||
|
* modify and redistribute granted by the license, users are provided only
|
||||||
|
* with a limited warranty and the software's author, the holder of the
|
||||||
|
* economic rights, and the successive licensors have only limited
|
||||||
|
* liability.
|
||||||
|
*
|
||||||
|
* In this respect, the user's attention is drawn to the risks associated
|
||||||
|
* with loading, using, modifying and/or developing or reproducing the
|
||||||
|
* software by the user in light of its specific status of free software,
|
||||||
|
* that may mean that it is complicated to manipulate, and that also
|
||||||
|
* therefore means that it is reserved for developers and experienced
|
||||||
|
* professionals having in-depth computer knowledge. Users are therefore
|
||||||
|
* encouraged to load and test the software's suitability as regards their
|
||||||
|
* requirements in conditions enabling the security of their systems and/or
|
||||||
|
* data to be ensured and, more generally, to use and operate it in the
|
||||||
|
* same conditions as regards security.
|
||||||
|
*
|
||||||
|
* The fact that you are presently reading this means that you have had
|
||||||
|
* knowledge of the CeCILL license and that you accept its terms.
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* gclc:fr.bigeon.gclc.command.SubedCommandTest.java
|
||||||
|
* Created on: Nov 18, 2016
|
||||||
|
*/
|
||||||
|
package fr.bigeon.gclc.command;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import fr.bigeon.gclc.exception.CommandRunException;
|
||||||
|
import fr.bigeon.gclc.exception.InvalidCommandName;
|
||||||
|
import fr.bigeon.gclc.manager.ConsoleManager;
|
||||||
|
import fr.bigeon.gclc.test.utils.TestConsoleManager;
|
||||||
|
|
||||||
|
/** <p>
|
||||||
|
* TODO
|
||||||
|
*
|
||||||
|
* @author Emmanuel Bigeon */
|
||||||
|
@SuppressWarnings("all")
|
||||||
|
public class SubedCommandTest {
|
||||||
|
|
||||||
|
/** Test method for
|
||||||
|
* {@link fr.bigeon.gclc.command.SubedCommand#SubedCommand(java.lang.String)}. */
|
||||||
|
@Test
|
||||||
|
public final void testSubedCommandString() {
|
||||||
|
SubedCommand cmd = new SubedCommand("name");
|
||||||
|
assertEquals("name", cmd.getCommandName());
|
||||||
|
cmd = new SubedCommand("name with spaces");
|
||||||
|
assertNull(cmd.tip());
|
||||||
|
assertEquals("name with spaces", cmd.getCommandName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Test method for
|
||||||
|
* {@link fr.bigeon.gclc.command.SubedCommand#SubedCommand(java.lang.String, fr.bigeon.gclc.command.ICommand)}. */
|
||||||
|
@Test
|
||||||
|
public final void testSubedCommandStringICommand() {
|
||||||
|
SubedCommand cmd = new SubedCommand("name", new MockCommand(""));
|
||||||
|
assertEquals("name", cmd.getCommandName());
|
||||||
|
cmd = new SubedCommand("name with spaces", new MockCommand(""));
|
||||||
|
assertNull(cmd.tip());
|
||||||
|
assertEquals("name with spaces", cmd.getCommandName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Test method for
|
||||||
|
* {@link fr.bigeon.gclc.command.SubedCommand#SubedCommand(java.lang.String, fr.bigeon.gclc.command.ICommand, java.lang.String)}. */
|
||||||
|
@Test
|
||||||
|
public final void testSubedCommandStringICommandString() {
|
||||||
|
SubedCommand cmd = new SubedCommand("name", new MockCommand(""), "tip");
|
||||||
|
assertEquals("name", cmd.getCommandName());
|
||||||
|
cmd = new SubedCommand("name with spaces", new MockCommand(""), "tip");
|
||||||
|
assertEquals("name with spaces", cmd.getCommandName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Test method for
|
||||||
|
* {@link fr.bigeon.gclc.command.SubedCommand#SubedCommand(java.lang.String, java.lang.String)}. */
|
||||||
|
@Test
|
||||||
|
public final void testSubedCommandStringString() {
|
||||||
|
SubedCommand cmd = new SubedCommand("name", "tip");
|
||||||
|
assertEquals("name", cmd.getCommandName());
|
||||||
|
cmd = new SubedCommand("name with spaces", "tip");
|
||||||
|
assertEquals("name with spaces", cmd.getCommandName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Test method for
|
||||||
|
* {@link fr.bigeon.gclc.command.SubedCommand#add(fr.bigeon.gclc.command.ICommand)}. */
|
||||||
|
@Test
|
||||||
|
public final void testAdd() {
|
||||||
|
SubedCommand cmd = new SubedCommand("name");
|
||||||
|
|
||||||
|
try {
|
||||||
|
cmd.add(new MockCommand("id"));
|
||||||
|
} catch (InvalidCommandName e) {
|
||||||
|
fail("addition of command with valid id failed");
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
cmd.add(new MockCommand("id"));
|
||||||
|
fail("addition of command with already used id succeeded");
|
||||||
|
} catch (InvalidCommandName e) {
|
||||||
|
//
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
cmd.add(new MockCommand(""));
|
||||||
|
fail("addition of command with invalid id succeeded");
|
||||||
|
} catch (InvalidCommandName e) {
|
||||||
|
//
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Test method for
|
||||||
|
* {@link fr.bigeon.gclc.command.SubedCommand#execute(java.lang.String[])}. */
|
||||||
|
@Test
|
||||||
|
public final void testExecute() {
|
||||||
|
SubedCommand cmd = new SubedCommand("name");
|
||||||
|
|
||||||
|
MockCommand mock = new MockCommand("id");
|
||||||
|
try {
|
||||||
|
cmd.add(mock);
|
||||||
|
cmd.add(new Command("fail") {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String tip() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute(String... args) throws CommandRunException {
|
||||||
|
throw new CommandRunException("Failing command", null);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (InvalidCommandName e) {
|
||||||
|
fail("addition of command with valid id failed");
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
cmd.execute("id");
|
||||||
|
} catch (CommandRunException e) {
|
||||||
|
fail("Unexpected exception when running mock command");
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
cmd.execute("fail");
|
||||||
|
fail("Fail command error should be re thrown");
|
||||||
|
} catch (CommandRunException e) {
|
||||||
|
assertNotNull(e);
|
||||||
|
assertEquals(cmd, e.getSource());
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
cmd.execute();
|
||||||
|
fail("Request for inexistent default command should fail");
|
||||||
|
} catch (CommandRunException e) {
|
||||||
|
assertNotNull(e);
|
||||||
|
assertEquals(cmd, e.getSource());
|
||||||
|
}
|
||||||
|
cmd = new SubedCommand("name", mock);
|
||||||
|
|
||||||
|
try {
|
||||||
|
cmd.add(mock);
|
||||||
|
cmd.add(new Command("fail") {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String tip() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute(String... args) throws CommandRunException {
|
||||||
|
throw new CommandRunException("Failing command", this);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (InvalidCommandName e) {
|
||||||
|
fail("addition of command with valid id failed");
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
cmd.execute("id");
|
||||||
|
} catch (CommandRunException e) {
|
||||||
|
fail("Unexpected exception when running mock command");
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
cmd.execute("fail");
|
||||||
|
fail("Fail command error should be re thrown");
|
||||||
|
} catch (CommandRunException e) {
|
||||||
|
assertNotNull(e);
|
||||||
|
assertEquals(cmd.get("fail"), e.getSource());
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
cmd.execute();
|
||||||
|
} catch (CommandRunException e) {
|
||||||
|
fail("Request for default command should execute default command");
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Test method for
|
||||||
|
* {@link fr.bigeon.gclc.command.SubedCommand#executeSub(java.lang.String, java.lang.String[])}. */
|
||||||
|
@Test
|
||||||
|
public final void testExecuteSub() {
|
||||||
|
SubedCommand cmd = new SubedCommand("name");
|
||||||
|
|
||||||
|
MockCommand mock = new MockCommand("id");
|
||||||
|
try {
|
||||||
|
cmd.add(mock);
|
||||||
|
cmd.add(new Command("fail") {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String tip() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute(String... args) throws CommandRunException {
|
||||||
|
throw new CommandRunException("Failing command", this);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (InvalidCommandName e) {
|
||||||
|
fail("addition of command with valid id failed");
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
cmd.executeSub("id");
|
||||||
|
} catch (CommandRunException e) {
|
||||||
|
fail("Unexpected exception when running mock command");
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
cmd.executeSub("fail");
|
||||||
|
fail("Fail command error should be re thrown");
|
||||||
|
} catch (CommandRunException e) {
|
||||||
|
assertNotNull(e);
|
||||||
|
assertEquals(cmd.get("fail"), e.getSource());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Test method for
|
||||||
|
* {@link fr.bigeon.gclc.command.SubedCommand#get(java.lang.String)}. */
|
||||||
|
@Test
|
||||||
|
public final void testGet() {
|
||||||
|
SubedCommand cmd = new SubedCommand("name");
|
||||||
|
|
||||||
|
assertNull(cmd.get("id"));
|
||||||
|
|
||||||
|
MockCommand mock = new MockCommand("id");
|
||||||
|
try {
|
||||||
|
cmd.add(mock);
|
||||||
|
} catch (InvalidCommandName e) {
|
||||||
|
fail("addition of command with valid id failed");
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
assertEquals(mock, cmd.get("id"));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Test method for
|
||||||
|
* {@link fr.bigeon.gclc.command.SubedCommand#getCommandName()}. */
|
||||||
|
@Test
|
||||||
|
public final void testGetCommandName() {
|
||||||
|
SubedCommand cmd = new SubedCommand("name");
|
||||||
|
assertEquals("name", cmd.getCommandName());
|
||||||
|
cmd = new SubedCommand("name with spaces");
|
||||||
|
assertEquals("name with spaces", cmd.getCommandName());
|
||||||
|
cmd = new SubedCommand("name", "some tip");
|
||||||
|
assertEquals("name", cmd.getCommandName());
|
||||||
|
cmd = new SubedCommand("name", new MockCommand(""));
|
||||||
|
assertEquals("name", cmd.getCommandName());
|
||||||
|
cmd = new SubedCommand("name", new MockCommand(""), "some tip");
|
||||||
|
assertEquals("name", cmd.getCommandName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Test method for
|
||||||
|
* {@link fr.bigeon.gclc.command.SubedCommand#help(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String[])}. */
|
||||||
|
@Test
|
||||||
|
public final void testHelp() {
|
||||||
|
SubedCommand cmd = new SubedCommand("name");
|
||||||
|
|
||||||
|
ICommand mock = new MockCommand("id");
|
||||||
|
try {
|
||||||
|
cmd.add(mock);
|
||||||
|
} catch (InvalidCommandName e) {
|
||||||
|
fail("addition of command with valid id failed");
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
try (TestConsoleManager manager = new TestConsoleManager();
|
||||||
|
TestConsoleManager manager2 = new TestConsoleManager()) {
|
||||||
|
cmd.help(manager);
|
||||||
|
assertEquals("\tid", manager.readNextLine());
|
||||||
|
cmd.help(manager, "id");
|
||||||
|
cmd.help(manager, "inexistent");
|
||||||
|
} catch (IOException e) {
|
||||||
|
fail("Unexpected exception when running help");
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd = new SubedCommand("name", mock);
|
||||||
|
|
||||||
|
try {
|
||||||
|
cmd.add(mock);
|
||||||
|
} catch (InvalidCommandName e) {
|
||||||
|
fail("addition of command with valid id failed");
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
try (TestConsoleManager manager = new TestConsoleManager();
|
||||||
|
TestConsoleManager manager2 = new TestConsoleManager()) {
|
||||||
|
cmd.help(manager);
|
||||||
|
assertEquals("\tid", manager.readNextLine());
|
||||||
|
} catch (IOException e) {
|
||||||
|
fail("Unexpected exception when running help");
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
mock = new ICommand() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String tip() {
|
||||||
|
return "tip";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void help(ConsoleManager manager,
|
||||||
|
String... args) throws IOException {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getCommandName() {
|
||||||
|
return "id";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute(String... args) throws CommandRunException {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
};
|
||||||
|
cmd = new SubedCommand("name", mock);
|
||||||
|
|
||||||
|
try {
|
||||||
|
cmd.add(mock);
|
||||||
|
} catch (InvalidCommandName e) {
|
||||||
|
fail("addition of command with valid id failed");
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
try (TestConsoleManager manager = new TestConsoleManager();
|
||||||
|
TestConsoleManager manager2 = new TestConsoleManager()) {
|
||||||
|
cmd.help(manager);
|
||||||
|
assertEquals("\ttip", manager.readNextLine());
|
||||||
|
assertEquals("\tid: tip", manager.readNextLine());
|
||||||
|
} catch (IOException e) {
|
||||||
|
fail("Unexpected exception when running help");
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Test method for {@link fr.bigeon.gclc.command.SubedCommand#tip()}. */
|
||||||
|
@Test
|
||||||
|
public final void testTip() {
|
||||||
|
SubedCommand cmd = new SubedCommand("name");
|
||||||
|
assertNull(cmd.tip());
|
||||||
|
cmd = new SubedCommand("name with spaces", new MockCommand(""), "tip");
|
||||||
|
assertEquals("tip", cmd.tip());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,75 @@
|
|||||||
|
/*
|
||||||
|
* Copyright Bigeon Emmanuel (2014)
|
||||||
|
*
|
||||||
|
* emmanuel@bigeon.fr
|
||||||
|
*
|
||||||
|
* This software is a computer program whose purpose is to
|
||||||
|
* provide a generic framework for console applications.
|
||||||
|
*
|
||||||
|
* This software is governed by the CeCILL license under French law and
|
||||||
|
* abiding by the rules of distribution of free software. You can use,
|
||||||
|
* modify and/or redistribute the software under the terms of the CeCILL
|
||||||
|
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||||
|
* "http://www.cecill.info".
|
||||||
|
*
|
||||||
|
* As a counterpart to the access to the source code and rights to copy,
|
||||||
|
* modify and redistribute granted by the license, users are provided only
|
||||||
|
* with a limited warranty and the software's author, the holder of the
|
||||||
|
* economic rights, and the successive licensors have only limited
|
||||||
|
* liability.
|
||||||
|
*
|
||||||
|
* In this respect, the user's attention is drawn to the risks associated
|
||||||
|
* with loading, using, modifying and/or developing or reproducing the
|
||||||
|
* software by the user in light of its specific status of free software,
|
||||||
|
* that may mean that it is complicated to manipulate, and that also
|
||||||
|
* therefore means that it is reserved for developers and experienced
|
||||||
|
* professionals having in-depth computer knowledge. Users are therefore
|
||||||
|
* encouraged to load and test the software's suitability as regards their
|
||||||
|
* requirements in conditions enabling the security of their systems and/or
|
||||||
|
* data to be ensured and, more generally, to use and operate it in the
|
||||||
|
* same conditions as regards security.
|
||||||
|
*
|
||||||
|
* The fact that you are presently reading this means that you have had
|
||||||
|
* knowledge of the CeCILL license and that you accept its terms.
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* gclc:fr.bigeon.gclc.exception.CommandRunExceptionTest.java
|
||||||
|
* Created on: Nov 19, 2016
|
||||||
|
*/
|
||||||
|
package fr.bigeon.gclc.exception;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import fr.bigeon.gclc.command.ICommand;
|
||||||
|
import fr.bigeon.gclc.command.MockCommand;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <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());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,148 @@
|
|||||||
|
/*
|
||||||
|
* Copyright Bigeon Emmanuel (2014)
|
||||||
|
*
|
||||||
|
* emmanuel@bigeon.fr
|
||||||
|
*
|
||||||
|
* This software is a computer program whose purpose is to
|
||||||
|
* provide a generic framework for console applications.
|
||||||
|
*
|
||||||
|
* This software is governed by the CeCILL license under French law and
|
||||||
|
* abiding by the rules of distribution of free software. You can use,
|
||||||
|
* modify and/or redistribute the software under the terms of the CeCILL
|
||||||
|
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||||
|
* "http://www.cecill.info".
|
||||||
|
*
|
||||||
|
* As a counterpart to the access to the source code and rights to copy,
|
||||||
|
* modify and redistribute granted by the license, users are provided only
|
||||||
|
* with a limited warranty and the software's author, the holder of the
|
||||||
|
* economic rights, and the successive licensors have only limited
|
||||||
|
* liability.
|
||||||
|
*
|
||||||
|
* In this respect, the user's attention is drawn to the risks associated
|
||||||
|
* with loading, using, modifying and/or developing or reproducing the
|
||||||
|
* software by the user in light of its specific status of free software,
|
||||||
|
* that may mean that it is complicated to manipulate, and that also
|
||||||
|
* therefore means that it is reserved for developers and experienced
|
||||||
|
* professionals having in-depth computer knowledge. Users are therefore
|
||||||
|
* encouraged to load and test the software's suitability as regards their
|
||||||
|
* requirements in conditions enabling the security of their systems and/or
|
||||||
|
* data to be ensured and, more generally, to use and operate it in the
|
||||||
|
* same conditions as regards security.
|
||||||
|
*
|
||||||
|
* The fact that you are presently reading this means that you have had
|
||||||
|
* knowledge of the CeCILL license and that you accept its terms.
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* gclc:fr.bigeon.gclc.manager.SystemConsoleManagerTest.java
|
||||||
|
* Created on: Nov 19, 2016
|
||||||
|
*/
|
||||||
|
package fr.bigeon.gclc.manager;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.PipedInputStream;
|
||||||
|
import java.io.PipedOutputStream;
|
||||||
|
import java.io.PrintStream;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/** <p>
|
||||||
|
* TODO
|
||||||
|
*
|
||||||
|
* @author Emmanuel Bigeon */
|
||||||
|
public class SystemConsoleManagerTest {
|
||||||
|
|
||||||
|
/** Test method for
|
||||||
|
* {@link fr.bigeon.gclc.manager.SystemConsoleManager#prompt()}. */
|
||||||
|
@Test
|
||||||
|
public final void testPrompt() {
|
||||||
|
|
||||||
|
try {
|
||||||
|
PipedOutputStream outStream = new PipedOutputStream();
|
||||||
|
InputStream in = new PipedInputStream(outStream);
|
||||||
|
final PrintStream out = new PrintStream(outStream);
|
||||||
|
final String test = "test";
|
||||||
|
SystemConsoleManager manager = new SystemConsoleManager(System.out,
|
||||||
|
in, Charset.forName("UTF-8"));
|
||||||
|
|
||||||
|
Thread th = new Thread(new Runnable() {
|
||||||
|
|
||||||
|
@SuppressWarnings("synthetic-access")
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
out.println(test);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
th.start();
|
||||||
|
assertEquals(test, manager.prompt());
|
||||||
|
|
||||||
|
th.join();
|
||||||
|
} catch (IOException | InterruptedException e) {
|
||||||
|
assertNull(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Test method for
|
||||||
|
* {@link fr.bigeon.gclc.manager.SystemConsoleManager#setPrompt(java.lang.String)}. */
|
||||||
|
@Test
|
||||||
|
public final void testSetPrompt() {
|
||||||
|
SystemConsoleManager manager = new SystemConsoleManager();
|
||||||
|
String prt = "++";
|
||||||
|
manager.setPrompt(prt);
|
||||||
|
assertEquals(prt, manager.getPrompt());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Test method for
|
||||||
|
* {@link fr.bigeon.gclc.manager.SystemConsoleManager#isClosed()}. */
|
||||||
|
@Test
|
||||||
|
public final void testIsClosed() {
|
||||||
|
try {
|
||||||
|
PipedOutputStream outStream = new PipedOutputStream();
|
||||||
|
InputStream in = new PipedInputStream(outStream);
|
||||||
|
final PrintStream out = new PrintStream(outStream);
|
||||||
|
final String test = "test";
|
||||||
|
SystemConsoleManager manager = new SystemConsoleManager(System.out,
|
||||||
|
in, Charset.forName("UTF-8"));
|
||||||
|
|
||||||
|
Thread th = new Thread(new Runnable() {
|
||||||
|
|
||||||
|
@SuppressWarnings("synthetic-access")
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
out.println(test);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
th.start();
|
||||||
|
assertEquals(test, manager.prompt());
|
||||||
|
assertFalse(manager.isClosed());
|
||||||
|
manager.close();
|
||||||
|
assertTrue(manager.isClosed());
|
||||||
|
try {
|
||||||
|
manager.prompt();
|
||||||
|
fail("prompt on closed manager");
|
||||||
|
} catch (IOException e) {
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
manager.println(test);
|
||||||
|
fail("prompt on closed manager");
|
||||||
|
} catch (IOException e) {
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
th.join();
|
||||||
|
} catch (IOException | InterruptedException e) {
|
||||||
|
assertNull(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
/*
|
||||||
|
* Copyright Bigeon Emmanuel (2014)
|
||||||
|
*
|
||||||
|
* emmanuel@bigeon.fr
|
||||||
|
*
|
||||||
|
* This software is a computer program whose purpose is to
|
||||||
|
* provide a generic framework for console applications.
|
||||||
|
*
|
||||||
|
* This software is governed by the CeCILL license under French law and
|
||||||
|
* abiding by the rules of distribution of free software. You can use,
|
||||||
|
* modify and/or redistribute the software under the terms of the CeCILL
|
||||||
|
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||||
|
* "http://www.cecill.info".
|
||||||
|
*
|
||||||
|
* As a counterpart to the access to the source code and rights to copy,
|
||||||
|
* modify and redistribute granted by the license, users are provided only
|
||||||
|
* with a limited warranty and the software's author, the holder of the
|
||||||
|
* economic rights, and the successive licensors have only limited
|
||||||
|
* liability.
|
||||||
|
*
|
||||||
|
* In this respect, the user's attention is drawn to the risks associated
|
||||||
|
* with loading, using, modifying and/or developing or reproducing the
|
||||||
|
* software by the user in light of its specific status of free software,
|
||||||
|
* that may mean that it is complicated to manipulate, and that also
|
||||||
|
* therefore means that it is reserved for developers and experienced
|
||||||
|
* professionals having in-depth computer knowledge. Users are therefore
|
||||||
|
* encouraged to load and test the software's suitability as regards their
|
||||||
|
* requirements in conditions enabling the security of their systems and/or
|
||||||
|
* data to be ensured and, more generally, to use and operate it in the
|
||||||
|
* same conditions as regards security.
|
||||||
|
*
|
||||||
|
* The fact that you are presently reading this means that you have had
|
||||||
|
* knowledge of the CeCILL license and that you accept its terms.
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* gclc:fr.bigeon.gclc.prompt.CLIPrompterMessagesTest.java
|
||||||
|
* Created on: Nov 19, 2016
|
||||||
|
*/
|
||||||
|
package fr.bigeon.gclc.prompt;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <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[] {}));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
1080
gclc/src/test/java/fr/bigeon/gclc/prompt/CLIPrompterTest.java
Normal file
1080
gclc/src/test/java/fr/bigeon/gclc/prompt/CLIPrompterTest.java
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,144 @@
|
|||||||
|
/*
|
||||||
|
* 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-test:fr.bigeon.gclc.test.TestConsoleManager.java
|
||||||
|
* Created on: Nov 18, 2016
|
||||||
|
*/
|
||||||
|
package fr.bigeon.gclc.test.utils;
|
||||||
|
|
||||||
|
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.ByteBuffer;
|
||||||
|
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
|
||||||
|
* input.
|
||||||
|
* <p>
|
||||||
|
* This console manager is used to internally pilot an application. This can be
|
||||||
|
* used to test application behavior.
|
||||||
|
*
|
||||||
|
* @author Emmanuel Bigeon */
|
||||||
|
public class TestConsoleManager implements ConsoleManager, AutoCloseable {
|
||||||
|
|
||||||
|
private final SystemConsoleManager innerManager;
|
||||||
|
private final PipedOutputStream commandInput;
|
||||||
|
private final BufferedReader commandBuffOutput;
|
||||||
|
private final PipedInputStream commandOutput;
|
||||||
|
private final PrintStream outPrint;
|
||||||
|
private final PipedInputStream in;
|
||||||
|
|
||||||
|
/** @throws IOException if the piping failed for streams */
|
||||||
|
public TestConsoleManager() throws IOException {
|
||||||
|
commandInput = new PipedOutputStream();
|
||||||
|
in = new PipedInputStream(commandInput);
|
||||||
|
commandOutput = new PipedInputStream();
|
||||||
|
PipedOutputStream out = new PipedOutputStream(commandOutput);
|
||||||
|
commandBuffOutput = new BufferedReader(
|
||||||
|
new InputStreamReader(commandOutput, Charset.defaultCharset()));
|
||||||
|
outPrint = new PrintStream(out);
|
||||||
|
innerManager = new SystemConsoleManager(outPrint, in,
|
||||||
|
Charset.forName("UTF-8"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getPrompt() {
|
||||||
|
return innerManager.getPrompt();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void print(String object) throws IOException {
|
||||||
|
innerManager.print(object);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void println() throws IOException {
|
||||||
|
innerManager.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void println(String object) throws IOException {
|
||||||
|
innerManager.println(object);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String prompt() throws IOException {
|
||||||
|
return innerManager
|
||||||
|
.prompt(innerManager.getPrompt() + System.lineSeparator());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String prompt(String message) throws IOException {
|
||||||
|
return innerManager.prompt(message + System.lineSeparator());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setPrompt(String prompt) {
|
||||||
|
innerManager.setPrompt(prompt);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() throws IOException {
|
||||||
|
innerManager.close();
|
||||||
|
outPrint.close();
|
||||||
|
commandBuffOutput.close();
|
||||||
|
commandOutput.close();
|
||||||
|
in.close();
|
||||||
|
commandInput.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isClosed() {
|
||||||
|
return innerManager.isClosed();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void type(String content) throws IOException {
|
||||||
|
ByteBuffer buff = Charset.defaultCharset()
|
||||||
|
.encode(content + System.lineSeparator());
|
||||||
|
if (buff.hasArray()) {
|
||||||
|
commandInput.write(buff.array());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String readNextLine() throws IOException {
|
||||||
|
return commandBuffOutput.readLine();
|
||||||
|
}
|
||||||
|
}
|
||||||
94
gclc/src/test/java/fr/bigeon/gclc/tools/PrintUtilsTest.java
Normal file
94
gclc/src/test/java/fr/bigeon/gclc/tools/PrintUtilsTest.java
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
/*
|
||||||
|
* 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()));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
0
gclc/src/test/resources/script1.txt
Normal file
0
gclc/src/test/resources/script1.txt
Normal file
1
gclc/src/test/resources/script2.txt
Normal file
1
gclc/src/test/resources/script2.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
# test only comment
|
||||||
1
gclc/src/test/resources/script3.txt
Normal file
1
gclc/src/test/resources/script3.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
test
|
||||||
7
gclc/src/test/resources/script4.txt
Normal file
7
gclc/src/test/resources/script4.txt
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# Test file with commands
|
||||||
|
test
|
||||||
|
# and comments
|
||||||
|
long
|
||||||
|
# interwined
|
||||||
|
|
||||||
|
# with empty line too
|
||||||
2
gclc/src/test/resources/script5.txt
Normal file
2
gclc/src/test/resources/script5.txt
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
# A script with arguments
|
||||||
|
{0}
|
||||||
2
gclc/src/test/resources/scripts/failingCmdInvoc.txt
Normal file
2
gclc/src/test/resources/scripts/failingCmdInvoc.txt
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
# a script invoking a failing command
|
||||||
|
failingCmd
|
||||||
2
gclc/src/test/resources/scripts/invalidCmd.txt
Normal file
2
gclc/src/test/resources/scripts/invalidCmd.txt
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
# a script with invalid commands
|
||||||
|
invalid
|
||||||
2
gclc/src/test/resources/scripts/invalidCmdParse.txt
Normal file
2
gclc/src/test/resources/scripts/invalidCmdParse.txt
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
# a script with invalid commands
|
||||||
|
test "po"m
|
||||||
3
gclc/src/test/resources/scripts/withprependSpace.txt
Normal file
3
gclc/src/test/resources/scripts/withprependSpace.txt
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
# a script with space in begining
|
||||||
|
test
|
||||||
|
should never be reached
|
||||||
Reference in New Issue
Block a user