Added piped stream manager
Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
parent
65b6be8283
commit
72362936be
@ -193,26 +193,7 @@ public class ConsoleApplication implements ICommandProvider {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
do {
|
do {
|
||||||
try {
|
runLoop();
|
||||||
final String cmd = manager.prompt();
|
|
||||||
if (cmd == null || cmd.isEmpty()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
for (final CommandRequestListener listener : listeners) {
|
|
||||||
listener.commandRequest(cmd);
|
|
||||||
}
|
|
||||||
interpretCommand(cmd);
|
|
||||||
} catch (InterruptedIOException e) {
|
|
||||||
LOGGER.log(Level.INFO,
|
|
||||||
"Prompt interrupted. It is likely the application is closing.", //$NON-NLS-1$
|
|
||||||
e);
|
|
||||||
} catch (IOException e) {
|
|
||||||
// The manager was closed
|
|
||||||
running = false;
|
|
||||||
LOGGER.log(Level.WARNING,
|
|
||||||
"The console manager was closed. Closing the application as no one can reach it.", //$NON-NLS-1$
|
|
||||||
e);
|
|
||||||
}
|
|
||||||
} while (running);
|
} while (running);
|
||||||
if (footer != null) {
|
if (footer != null) {
|
||||||
try {
|
try {
|
||||||
@ -229,6 +210,33 @@ public class ConsoleApplication implements ICommandProvider {
|
|||||||
LOGGER.fine("Exiting application."); //$NON-NLS-1$
|
LOGGER.fine("Exiting application."); //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** The running loop content.
|
||||||
|
* <p>
|
||||||
|
* This consisting in getting the command, executing it and exiting
|
||||||
|
* (restarting the loop). */
|
||||||
|
private void runLoop() {
|
||||||
|
try {
|
||||||
|
final String cmd = manager.prompt();
|
||||||
|
if (cmd == null || cmd.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (final CommandRequestListener listener : listeners) {
|
||||||
|
listener.commandRequest(cmd);
|
||||||
|
}
|
||||||
|
interpretCommand(cmd);
|
||||||
|
} catch (InterruptedIOException e) {
|
||||||
|
LOGGER.log(Level.INFO,
|
||||||
|
"Prompt interrupted. It is likely the application is closing.", //$NON-NLS-1$
|
||||||
|
e);
|
||||||
|
} catch (IOException e) {
|
||||||
|
// The manager was closed
|
||||||
|
running = false;
|
||||||
|
LOGGER.log(Level.WARNING,
|
||||||
|
"The console manager was closed. Closing the application as no one can reach it.", //$NON-NLS-1$
|
||||||
|
e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** @return the running status */
|
/** @return the running status */
|
||||||
public boolean isRunning() {
|
public boolean isRunning() {
|
||||||
return running;
|
return running;
|
||||||
|
@ -72,13 +72,13 @@ public interface ConsoleManager {
|
|||||||
/** @return the user inputed string
|
/** @return the user inputed string
|
||||||
* @throws IOException if the manager is closed or could not read the prompt
|
* @throws IOException if the manager is closed or could not read the prompt
|
||||||
* @throws InterruptedIOException if the prompt was interrupted */
|
* @throws InterruptedIOException if the prompt was interrupted */
|
||||||
String prompt() throws IOException, InterruptedIOException;
|
String prompt() throws IOException;
|
||||||
|
|
||||||
/** @param message the message to prompt the user
|
/** @param message the message to prompt the user
|
||||||
* @return the user inputed string
|
* @return the user inputed string
|
||||||
* @throws IOException if the manager is closed or could not read the prompt
|
* @throws IOException if the manager is closed or could not read the prompt
|
||||||
* @throws InterruptedIOException if the prompt was interrupted */
|
* @throws InterruptedIOException if the prompt was interrupted */
|
||||||
String prompt(String message) throws IOException, InterruptedIOException;
|
String prompt(String message) throws IOException;
|
||||||
|
|
||||||
/** <p>
|
/** <p>
|
||||||
* Set a prompting prefix.
|
* Set a prompting prefix.
|
||||||
@ -94,6 +94,7 @@ public interface ConsoleManager {
|
|||||||
/** @return if the manager is closed. */
|
/** @return if the manager is closed. */
|
||||||
boolean isClosed();
|
boolean isClosed();
|
||||||
|
|
||||||
/** Indicate to the manager that is should interrompt the prompting */
|
/** Indicate to the manager that is should interrompt the prompting, if
|
||||||
|
* possible. */
|
||||||
void interruptPrompt();
|
void interruptPrompt();
|
||||||
}
|
}
|
||||||
|
@ -36,7 +36,7 @@
|
|||||||
* gclc-test:fr.bigeon.gclc.test.TestConsoleManager.java
|
* gclc-test:fr.bigeon.gclc.test.TestConsoleManager.java
|
||||||
* Created on: Nov 18, 2016
|
* Created on: Nov 18, 2016
|
||||||
*/
|
*/
|
||||||
package fr.bigeon.gclc.test.utils;
|
package fr.bigeon.gclc.manager;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -44,12 +44,8 @@ import java.io.InputStreamReader;
|
|||||||
import java.io.PipedInputStream;
|
import java.io.PipedInputStream;
|
||||||
import java.io.PipedOutputStream;
|
import java.io.PipedOutputStream;
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
import java.nio.ByteBuffer;
|
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
|
|
||||||
import fr.bigeon.gclc.manager.ConsoleManager;
|
|
||||||
import fr.bigeon.gclc.manager.SystemConsoleManager;
|
|
||||||
|
|
||||||
/** This console manager allows to enter commands and retrieve the output as an
|
/** This console manager allows to enter commands and retrieve the output as an
|
||||||
* input.
|
* input.
|
||||||
* <p>
|
* <p>
|
||||||
@ -57,26 +53,43 @@ import fr.bigeon.gclc.manager.SystemConsoleManager;
|
|||||||
* used to test application behavior.
|
* used to test application behavior.
|
||||||
*
|
*
|
||||||
* @author Emmanuel Bigeon */
|
* @author Emmanuel Bigeon */
|
||||||
public class TestConsoleManager implements ConsoleManager, AutoCloseable {
|
public final class PipedConsoleManager
|
||||||
|
implements ConsoleManager, AutoCloseable {
|
||||||
|
|
||||||
|
/** THe inner manager */
|
||||||
private final SystemConsoleManager innerManager;
|
private final SystemConsoleManager innerManager;
|
||||||
|
/** The stream to pipe commands into */
|
||||||
private final PipedOutputStream commandInput;
|
private final PipedOutputStream commandInput;
|
||||||
|
/** The reader to get application return from */
|
||||||
private final BufferedReader commandBuffOutput;
|
private final BufferedReader commandBuffOutput;
|
||||||
|
/** The stream to get application return from */
|
||||||
private final PipedInputStream commandOutput;
|
private final PipedInputStream commandOutput;
|
||||||
|
/** The print writer for application to write return to */
|
||||||
private final PrintStream outPrint;
|
private final PrintStream outPrint;
|
||||||
|
/** The stream for the application to read commands from */
|
||||||
private final PipedInputStream in;
|
private final PipedInputStream in;
|
||||||
|
/** The writing thread */
|
||||||
|
private final WritingRunnable writing;
|
||||||
|
/** The reading thread */
|
||||||
|
private final ReadingRunnable reading;
|
||||||
|
|
||||||
/** @throws IOException if the piping failed for streams */
|
/** @throws IOException if the piping failed for streams */
|
||||||
public TestConsoleManager() throws IOException {
|
public PipedConsoleManager() throws IOException {
|
||||||
commandInput = new PipedOutputStream();
|
commandInput = new PipedOutputStream();
|
||||||
in = new PipedInputStream(commandInput);
|
in = new PipedInputStream(commandInput);
|
||||||
commandOutput = new PipedInputStream();
|
commandOutput = new PipedInputStream();
|
||||||
PipedOutputStream out = new PipedOutputStream(commandOutput);
|
PipedOutputStream out = new PipedOutputStream(commandOutput);
|
||||||
commandBuffOutput = new BufferedReader(
|
commandBuffOutput = new BufferedReader(
|
||||||
new InputStreamReader(commandOutput, Charset.defaultCharset()));
|
new InputStreamReader(commandOutput, Charset.forName("UTF-8")));
|
||||||
outPrint = new PrintStream(out, true, Charset.defaultCharset().name());
|
outPrint = new PrintStream(out, true, "UTF-8");
|
||||||
innerManager = new SystemConsoleManager(outPrint, in,
|
innerManager = new SystemConsoleManager(outPrint, in,
|
||||||
Charset.forName("UTF-8"));
|
Charset.forName("UTF-8")); //$NON-NLS-1$
|
||||||
|
writing = new WritingRunnable(commandInput, Charset.forName("UTF-8"));
|
||||||
|
reading = new ReadingRunnable(commandBuffOutput);
|
||||||
|
Thread th = new Thread(writing, "write"); //$NON-NLS-1$
|
||||||
|
th.start();
|
||||||
|
th = new Thread(reading, "read"); //$NON-NLS-1$
|
||||||
|
th.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -118,6 +131,8 @@ public class TestConsoleManager implements ConsoleManager, AutoCloseable {
|
|||||||
@Override
|
@Override
|
||||||
public void close() throws IOException {
|
public void close() throws IOException {
|
||||||
innerManager.close();
|
innerManager.close();
|
||||||
|
reading.setRunning(false);
|
||||||
|
writing.setRunning(false);
|
||||||
outPrint.close();
|
outPrint.close();
|
||||||
commandBuffOutput.close();
|
commandBuffOutput.close();
|
||||||
commandOutput.close();
|
commandOutput.close();
|
||||||
@ -130,16 +145,16 @@ public class TestConsoleManager implements ConsoleManager, AutoCloseable {
|
|||||||
return innerManager.isClosed();
|
return innerManager.isClosed();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @param content the content to type to the application
|
||||||
|
* @throws IOException if the typing failed */
|
||||||
public void type(String content) throws IOException {
|
public void type(String content) throws IOException {
|
||||||
ByteBuffer buff = Charset.defaultCharset()
|
writing.addMessage(content);
|
||||||
.encode(content + System.lineSeparator());
|
|
||||||
if (buff.hasArray()) {
|
|
||||||
commandInput.write(buff.array());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @return the content of the next line written by the application
|
||||||
|
* @throws IOException if the reading failed */
|
||||||
public String readNextLine() throws IOException {
|
public String readNextLine() throws IOException {
|
||||||
return commandBuffOutput.readLine();
|
return reading.getMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
@ -1,54 +0,0 @@
|
|||||||
/**
|
|
||||||
* gclc:fr.bigeon.gclc.manager.ReadRunnable.java
|
|
||||||
* Created on: Nov 21, 2016
|
|
||||||
*/
|
|
||||||
package fr.bigeon.gclc.manager;
|
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.logging.Level;
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <p>
|
|
||||||
* TODO
|
|
||||||
*
|
|
||||||
* @author Emmanuel Bigeon
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class ReadRunnable implements Runnable {
|
|
||||||
|
|
||||||
/** The logger */
|
|
||||||
private static final Logger LOGGER = Logger
|
|
||||||
.getLogger(ReadRunnable.class.getName());
|
|
||||||
/** The result */
|
|
||||||
private String result = "";
|
|
||||||
/** The input buffer */
|
|
||||||
private final BufferedReader in;
|
|
||||||
|
|
||||||
/** @param in the input buffer */
|
|
||||||
protected ReadRunnable(BufferedReader in) {
|
|
||||||
super();
|
|
||||||
this.in = in;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* (non-Javadoc)
|
|
||||||
* @see java.lang.Runnable#run() */
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
try {
|
|
||||||
result = in.readLine();
|
|
||||||
while (result != null && result.length() > 0 &&
|
|
||||||
result.charAt(0) == 0) {
|
|
||||||
result = result.substring(1);
|
|
||||||
}
|
|
||||||
} catch (final IOException e) {
|
|
||||||
LOGGER.log(Level.SEVERE, "Unable to read prompt", e); //$NON-NLS-1$
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @return the result */
|
|
||||||
public String getResult() {
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
154
gclc/src/main/java/fr/bigeon/gclc/manager/ReadingRunnable.java
Normal file
154
gclc/src/main/java/fr/bigeon/gclc/manager/ReadingRunnable.java
Normal file
@ -0,0 +1,154 @@
|
|||||||
|
/*
|
||||||
|
* Copyright Bigeon Emmanuel (2014)
|
||||||
|
*
|
||||||
|
* emmanuel@bigeon.fr
|
||||||
|
*
|
||||||
|
* This software is a computer program whose purpose is to
|
||||||
|
* provide a generic framework for console applications.
|
||||||
|
*
|
||||||
|
* This software is governed by the CeCILL license under French law and
|
||||||
|
* abiding by the rules of distribution of free software. You can use,
|
||||||
|
* modify and/or redistribute the software under the terms of the CeCILL
|
||||||
|
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||||
|
* "http://www.cecill.info".
|
||||||
|
*
|
||||||
|
* As a counterpart to the access to the source code and rights to copy,
|
||||||
|
* modify and redistribute granted by the license, users are provided only
|
||||||
|
* with a limited warranty and the software's author, the holder of the
|
||||||
|
* economic rights, and the successive licensors have only limited
|
||||||
|
* liability.
|
||||||
|
*
|
||||||
|
* In this respect, the user's attention is drawn to the risks associated
|
||||||
|
* with loading, using, modifying and/or developing or reproducing the
|
||||||
|
* software by the user in light of its specific status of free software,
|
||||||
|
* that may mean that it is complicated to manipulate, and that also
|
||||||
|
* therefore means that it is reserved for developers and experienced
|
||||||
|
* professionals having in-depth computer knowledge. Users are therefore
|
||||||
|
* encouraged to load and test the software's suitability as regards their
|
||||||
|
* requirements in conditions enabling the security of their systems and/or
|
||||||
|
* data to be ensured and, more generally, to use and operate it in the
|
||||||
|
* same conditions as regards security.
|
||||||
|
*
|
||||||
|
* The fact that you are presently reading this means that you have had
|
||||||
|
* knowledge of the CeCILL license and that you accept its terms.
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* gclc:fr.bigeon.gclc.test.utils.WritingRunnable.java
|
||||||
|
* Created on: Nov 29, 2016
|
||||||
|
*/
|
||||||
|
package fr.bigeon.gclc.manager;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InterruptedIOException;
|
||||||
|
import java.util.ArrayDeque;
|
||||||
|
import java.util.Deque;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
/** A runnable to read the piped output.
|
||||||
|
*
|
||||||
|
* @author Emmanuel Bigeon */
|
||||||
|
public class ReadingRunnable implements Runnable {
|
||||||
|
|
||||||
|
/** Wait timeout */
|
||||||
|
private static final long TIMEOUT = 1000;
|
||||||
|
/** Class logger */
|
||||||
|
private static final Logger LOGGER = Logger
|
||||||
|
.getLogger(ReadingRunnable.class.getName());
|
||||||
|
/** Read messages */
|
||||||
|
private final Deque<String> messages = new ArrayDeque<>();
|
||||||
|
/** the reader */
|
||||||
|
private final BufferedReader reader;
|
||||||
|
/** the state of this runnable */
|
||||||
|
private boolean running = true;
|
||||||
|
|
||||||
|
/** Synchro object */
|
||||||
|
private final Object lock = new Object();
|
||||||
|
|
||||||
|
/** @param reader the input to read from */
|
||||||
|
public ReadingRunnable(BufferedReader reader) {
|
||||||
|
super();
|
||||||
|
this.reader = reader;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see java.lang.Runnable#run() */
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
|
||||||
|
while (running) {
|
||||||
|
try {
|
||||||
|
String line = reader.readLine();
|
||||||
|
if (line == null) {
|
||||||
|
// Buffer end
|
||||||
|
running = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
LOGGER.finer("Read: " + line); //$NON-NLS-1$
|
||||||
|
line = stripNull(line);
|
||||||
|
synchronized (lock) {
|
||||||
|
messages.add(line);
|
||||||
|
lock.notify();
|
||||||
|
}
|
||||||
|
} catch (InterruptedIOException e) {
|
||||||
|
LOGGER.log(Level.INFO, "Reading interrupted", e); //$NON-NLS-1$
|
||||||
|
} catch (IOException e) {
|
||||||
|
LOGGER.log(Level.SEVERE, "Unable to read from stream", e); //$NON-NLS-1$
|
||||||
|
running = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Strip the string from head NULL characters.
|
||||||
|
*
|
||||||
|
* @param line the line to strip the null character from
|
||||||
|
* @return the resulting string */
|
||||||
|
private String stripNull(String line) {
|
||||||
|
String res = line;
|
||||||
|
while (res.length() > 0 && res.charAt(0) == 0) {
|
||||||
|
LOGGER.severe(
|
||||||
|
"NULL character heading the result of the read. This is a stream problem...");
|
||||||
|
res = res.substring(1);
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return the next read message
|
||||||
|
* @throws IOException if the pipe is closed */
|
||||||
|
public String getMessage() throws IOException {
|
||||||
|
synchronized (lock) {
|
||||||
|
if (!running) {
|
||||||
|
throw new IOException("Closed pipe"); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
while (messages.isEmpty()) {
|
||||||
|
try {
|
||||||
|
lock.wait(TIMEOUT);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
LOGGER.log(Level.SEVERE, "Thread interruption exception.", //$NON-NLS-1$
|
||||||
|
e);
|
||||||
|
}
|
||||||
|
if (messages.isEmpty() && !running) {
|
||||||
|
throw new IOException("Closed pipe"); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
}
|
||||||
|
LOGGER.finest("Polled: " + messages.peek()); //$NON-NLS-1$
|
||||||
|
return messages.poll();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @param running the running to set */
|
||||||
|
public void setRunning(boolean running) {
|
||||||
|
synchronized (lock) {
|
||||||
|
this.running = running;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return the running */
|
||||||
|
public boolean isRunning() {
|
||||||
|
synchronized (lock) {
|
||||||
|
return running;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -44,25 +44,17 @@ import java.io.InputStream;
|
|||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
/** A console using the input stream and print stream.
|
/** A console using the input stream and print stream.
|
||||||
* <p>
|
* <p>
|
||||||
* The default constructor will use the system standart input and output.
|
* The default constructor will use the system standart input and output.
|
||||||
*
|
*
|
||||||
* @author Emmanuel BIGEON */
|
* @author Emmanuel BIGEON */
|
||||||
public class SystemConsoleManager implements ConsoleManager { // NOSONAR
|
public final class SystemConsoleManager implements ConsoleManager { // NOSONAR
|
||||||
|
|
||||||
/** The default prompt */
|
/** The default prompt */
|
||||||
public static final String DEFAULT_PROMPT = "> "; //$NON-NLS-1$
|
public static final String DEFAULT_PROMPT = "> "; //$NON-NLS-1$
|
||||||
|
|
||||||
/** The logger */
|
|
||||||
private static final Logger LOGGER = Logger
|
|
||||||
.getLogger(SystemConsoleManager.class.getName());
|
|
||||||
|
|
||||||
/** The empty string constant */
|
|
||||||
private static final String EMPTY = ""; //$NON-NLS-1$
|
|
||||||
|
|
||||||
/** The command prompt. It can be changed. */
|
/** The command prompt. It can be changed. */
|
||||||
private String prompt = DEFAULT_PROMPT;
|
private String prompt = DEFAULT_PROMPT;
|
||||||
|
|
||||||
@ -74,7 +66,11 @@ public class SystemConsoleManager implements ConsoleManager { // NOSONAR
|
|||||||
/** If the manager is closed */
|
/** If the manager is closed */
|
||||||
private boolean closed = false;
|
private boolean closed = false;
|
||||||
|
|
||||||
private Thread reading;
|
/** The prompting thread */
|
||||||
|
private final Thread promptThread;
|
||||||
|
|
||||||
|
/** The reading runnable */
|
||||||
|
private final ReadingRunnable reading;
|
||||||
|
|
||||||
/** This default constructor relies on the system defined standart output
|
/** This default constructor relies on the system defined standart output
|
||||||
* and input stream. */
|
* and input stream. */
|
||||||
@ -90,6 +86,9 @@ public class SystemConsoleManager implements ConsoleManager { // NOSONAR
|
|||||||
super();
|
super();
|
||||||
this.out = out;
|
this.out = out;
|
||||||
this.in = new BufferedReader(new InputStreamReader(in, charset));
|
this.in = new BufferedReader(new InputStreamReader(in, charset));
|
||||||
|
reading = new ReadingRunnable(this.in);
|
||||||
|
promptThread = new Thread(reading, "prompt"); //$NON-NLS-1$
|
||||||
|
promptThread.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @return the prompt */
|
/** @return the prompt */
|
||||||
@ -142,23 +141,7 @@ public class SystemConsoleManager implements ConsoleManager { // NOSONAR
|
|||||||
public String prompt(String message) throws IOException {
|
public String prompt(String message) throws IOException {
|
||||||
checkOpen();
|
checkOpen();
|
||||||
out.print(message);
|
out.print(message);
|
||||||
// ReadRunnable rr = new ReadRunnable(in);
|
return reading.getMessage();
|
||||||
// reading = new Thread(rr, "prompt"); //$NON-NLS-1$
|
|
||||||
// reading.start();
|
|
||||||
// try {
|
|
||||||
// reading.join();
|
|
||||||
// } catch (final InterruptedException e) {
|
|
||||||
// LOGGER.log(Level.SEVERE, "Prompt reading interrupted", e); //$NON-NLS-1$
|
|
||||||
// throw new InterruptedIOException("Prompt interruption"); //$NON-NLS-1$
|
|
||||||
// }
|
|
||||||
String result = EMPTY;
|
|
||||||
result = in.readLine();
|
|
||||||
while (result != null && result.length() > 0 &&
|
|
||||||
result.charAt(0) == 0) {
|
|
||||||
result = result.substring(1);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
// return rr.getResult();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @param prompt the prompt to set */
|
/** @param prompt the prompt to set */
|
||||||
@ -172,6 +155,7 @@ public class SystemConsoleManager implements ConsoleManager { // NOSONAR
|
|||||||
@Override
|
@Override
|
||||||
public void close() throws IOException {
|
public void close() throws IOException {
|
||||||
closed = true;
|
closed = true;
|
||||||
|
reading.setRunning(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
@ -181,13 +165,12 @@ public class SystemConsoleManager implements ConsoleManager { // NOSONAR
|
|||||||
return closed;
|
return closed;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/** Beware, in this implementation this is the same as closing the manager.
|
||||||
|
*
|
||||||
* @see fr.bigeon.gclc.manager.ConsoleManager#interruptPrompt() */
|
* @see fr.bigeon.gclc.manager.ConsoleManager#interruptPrompt() */
|
||||||
@Override
|
@Override
|
||||||
public void interruptPrompt() {
|
public void interruptPrompt() {
|
||||||
if (reading != null) {
|
promptThread.interrupt();
|
||||||
reading.interrupt();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
138
gclc/src/main/java/fr/bigeon/gclc/manager/WritingRunnable.java
Normal file
138
gclc/src/main/java/fr/bigeon/gclc/manager/WritingRunnable.java
Normal file
@ -0,0 +1,138 @@
|
|||||||
|
/*
|
||||||
|
* Copyright Bigeon Emmanuel (2014)
|
||||||
|
*
|
||||||
|
* emmanuel@bigeon.fr
|
||||||
|
*
|
||||||
|
* This software is a computer program whose purpose is to
|
||||||
|
* provide a generic framework for console applications.
|
||||||
|
*
|
||||||
|
* This software is governed by the CeCILL license under French law and
|
||||||
|
* abiding by the rules of distribution of free software. You can use,
|
||||||
|
* modify and/or redistribute the software under the terms of the CeCILL
|
||||||
|
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||||
|
* "http://www.cecill.info".
|
||||||
|
*
|
||||||
|
* As a counterpart to the access to the source code and rights to copy,
|
||||||
|
* modify and redistribute granted by the license, users are provided only
|
||||||
|
* with a limited warranty and the software's author, the holder of the
|
||||||
|
* economic rights, and the successive licensors have only limited
|
||||||
|
* liability.
|
||||||
|
*
|
||||||
|
* In this respect, the user's attention is drawn to the risks associated
|
||||||
|
* with loading, using, modifying and/or developing or reproducing the
|
||||||
|
* software by the user in light of its specific status of free software,
|
||||||
|
* that may mean that it is complicated to manipulate, and that also
|
||||||
|
* therefore means that it is reserved for developers and experienced
|
||||||
|
* professionals having in-depth computer knowledge. Users are therefore
|
||||||
|
* encouraged to load and test the software's suitability as regards their
|
||||||
|
* requirements in conditions enabling the security of their systems and/or
|
||||||
|
* data to be ensured and, more generally, to use and operate it in the
|
||||||
|
* same conditions as regards security.
|
||||||
|
*
|
||||||
|
* The fact that you are presently reading this means that you have had
|
||||||
|
* knowledge of the CeCILL license and that you accept its terms.
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* gclc:fr.bigeon.gclc.test.utils.WritingRunnable.java
|
||||||
|
* Created on: Nov 29, 2016
|
||||||
|
*/
|
||||||
|
package fr.bigeon.gclc.manager;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.PipedOutputStream;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.util.ArrayDeque;
|
||||||
|
import java.util.Deque;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
/** <p>
|
||||||
|
* TODO
|
||||||
|
*
|
||||||
|
* @author Emmanuel Bigeon */
|
||||||
|
public class WritingRunnable implements Runnable {
|
||||||
|
|
||||||
|
/** Wait timeout */
|
||||||
|
private static final long TIMEOUT = 1000;
|
||||||
|
/** Class logger */
|
||||||
|
private static final Logger LOGGER = Logger
|
||||||
|
.getLogger(WritingRunnable.class.getName());
|
||||||
|
/** Messages to write */
|
||||||
|
private final Deque<String> messages = new ArrayDeque<>();
|
||||||
|
/** Stream to write to */
|
||||||
|
private final PipedOutputStream outPrint;
|
||||||
|
/** The charset */
|
||||||
|
private final Charset charset;
|
||||||
|
/** Runnable state */
|
||||||
|
private boolean running = true;
|
||||||
|
|
||||||
|
/** Synchro object */
|
||||||
|
private final Object lock = new Object();
|
||||||
|
|
||||||
|
/** @param outPrint the output to print to */
|
||||||
|
public WritingRunnable(PipedOutputStream outPrint, Charset charset) {
|
||||||
|
super();
|
||||||
|
this.outPrint = outPrint;
|
||||||
|
this.charset = charset;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see java.lang.Runnable#run() */
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
|
||||||
|
while (running) {
|
||||||
|
synchronized (lock) {
|
||||||
|
while (messages.isEmpty()) {
|
||||||
|
try {
|
||||||
|
lock.wait(TIMEOUT);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
LOGGER.log(Level.SEVERE,
|
||||||
|
"Thread interruption exception.", e); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
if (!running) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
String message = messages.poll();
|
||||||
|
ByteBuffer buff = charset
|
||||||
|
.encode(message + System.lineSeparator());
|
||||||
|
if (buff.hasArray()) {
|
||||||
|
try {
|
||||||
|
outPrint.write(buff.array());
|
||||||
|
} catch (IOException e) {
|
||||||
|
LOGGER.log(Level.SEVERE, "Unable to write to stream", //$NON-NLS-1$
|
||||||
|
e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @param message the message
|
||||||
|
* @throws IOException if the pipe is closed */
|
||||||
|
public void addMessage(String message) throws IOException {
|
||||||
|
synchronized (lock) {
|
||||||
|
if (!running) {
|
||||||
|
throw new IOException("Closed pipe"); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
messages.offer(message);
|
||||||
|
lock.notify();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @param running the running to set */
|
||||||
|
public void setRunning(boolean running) {
|
||||||
|
synchronized (lock) {
|
||||||
|
this.running = running;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return the running */
|
||||||
|
public boolean isRunning() {
|
||||||
|
synchronized (lock) {
|
||||||
|
return running;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -42,7 +42,7 @@ import java.io.IOException;
|
|||||||
|
|
||||||
import fr.bigeon.gclc.command.ICommand;
|
import fr.bigeon.gclc.command.ICommand;
|
||||||
import fr.bigeon.gclc.exception.InvalidCommandName;
|
import fr.bigeon.gclc.exception.InvalidCommandName;
|
||||||
import fr.bigeon.gclc.test.utils.TestConsoleManager;
|
import fr.bigeon.gclc.manager.PipedConsoleManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
@ -56,11 +56,11 @@ public class CommandTestingApplication implements AutoCloseable {
|
|||||||
|
|
||||||
private final ConsoleTestApplication application;
|
private final ConsoleTestApplication application;
|
||||||
private final Thread th;
|
private final Thread th;
|
||||||
private final TestConsoleManager manager;
|
private final PipedConsoleManager manager;
|
||||||
|
|
||||||
/** @throws IOException if the streams cannot be build */
|
/** @throws IOException if the streams cannot be build */
|
||||||
public CommandTestingApplication() throws IOException {
|
public CommandTestingApplication() throws IOException {
|
||||||
manager = new TestConsoleManager();
|
manager = new PipedConsoleManager();
|
||||||
application = new ConsoleTestApplication(manager);
|
application = new ConsoleTestApplication(manager);
|
||||||
th = new Thread(new Runnable() {
|
th = new Thread(new Runnable() {
|
||||||
|
|
||||||
@ -110,7 +110,7 @@ public class CommandTestingApplication implements AutoCloseable {
|
|||||||
|
|
||||||
/** @return the next written line, null if it is the prompt
|
/** @return the next written line, null if it is the prompt
|
||||||
* @throws IOException if the reading failed
|
* @throws IOException if the reading failed
|
||||||
* @see fr.bigeon.gclc.test.utils.TestConsoleManager#readNextLine() */
|
* @see fr.bigeon.gclc.manager.PipedConsoleManager#readNextLine() */
|
||||||
public String readNextLine() throws IOException {
|
public String readNextLine() throws IOException {
|
||||||
String ret = manager.readNextLine();
|
String ret = manager.readNextLine();
|
||||||
if (ret.equals(manager.getPrompt())) {
|
if (ret.equals(manager.getPrompt())) {
|
||||||
|
@ -56,7 +56,7 @@ import fr.bigeon.gclc.exception.InvalidCommandName;
|
|||||||
import fr.bigeon.gclc.i18n.Messages;
|
import fr.bigeon.gclc.i18n.Messages;
|
||||||
import fr.bigeon.gclc.manager.ConsoleManager;
|
import fr.bigeon.gclc.manager.ConsoleManager;
|
||||||
import fr.bigeon.gclc.manager.SystemConsoleManager;
|
import fr.bigeon.gclc.manager.SystemConsoleManager;
|
||||||
import fr.bigeon.gclc.test.utils.TestConsoleManager;
|
import fr.bigeon.gclc.manager.PipedConsoleManager;
|
||||||
|
|
||||||
/** Test class for ConsoleApplication
|
/** Test class for ConsoleApplication
|
||||||
*
|
*
|
||||||
@ -136,7 +136,7 @@ public class ConsoleApplicationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ConsoleApplication appli = null;
|
ConsoleApplication appli = null;
|
||||||
try (TestConsoleManager manager = new TestConsoleManager()) {
|
try (PipedConsoleManager manager = new PipedConsoleManager()) {
|
||||||
final ConsoleApplication app = new ConsoleApplication(manager, null,
|
final ConsoleApplication app = new ConsoleApplication(manager, null,
|
||||||
null);
|
null);
|
||||||
appli = app;
|
appli = app;
|
||||||
@ -167,7 +167,7 @@ public class ConsoleApplicationTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void interpretCommandTest() {
|
public void interpretCommandTest() {
|
||||||
try (TestConsoleManager test = new TestConsoleManager()) {
|
try (PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
ConsoleApplication appl = new ConsoleApplication(test, "", "");
|
ConsoleApplication appl = new ConsoleApplication(test, "", "");
|
||||||
|
|
||||||
appl.interpretCommand("invalid cmd \"due to misplaced\"quote");
|
appl.interpretCommand("invalid cmd \"due to misplaced\"quote");
|
||||||
|
@ -45,7 +45,7 @@ import java.io.IOException;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import fr.bigeon.gclc.exception.CommandRunException;
|
import fr.bigeon.gclc.exception.CommandRunException;
|
||||||
import fr.bigeon.gclc.test.utils.TestConsoleManager;
|
import fr.bigeon.gclc.manager.PipedConsoleManager;
|
||||||
|
|
||||||
/** <p>
|
/** <p>
|
||||||
* TODO
|
* TODO
|
||||||
@ -55,7 +55,7 @@ public class CommandTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void test() {
|
public final void test() {
|
||||||
try (TestConsoleManager test = new TestConsoleManager()) {
|
try (PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
Command cmd;
|
Command cmd;
|
||||||
cmd = new Command("name") {
|
cmd = new Command("name") {
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ import static org.junit.Assert.fail;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import fr.bigeon.gclc.exception.CommandRunException;
|
import fr.bigeon.gclc.exception.CommandRunException;
|
||||||
import fr.bigeon.gclc.test.utils.TestConsoleManager;
|
import fr.bigeon.gclc.manager.PipedConsoleManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
@ -68,7 +68,7 @@ public class HelpExecutorTest {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
assertNotNull(e);
|
assertNotNull(e);
|
||||||
}
|
}
|
||||||
try (TestConsoleManager test = new TestConsoleManager()) {
|
try (PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
help = new HelpExecutor("?", test, new MockCommand("mock"));
|
help = new HelpExecutor("?", test, new MockCommand("mock"));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
assertNull(e);
|
assertNull(e);
|
||||||
@ -81,7 +81,7 @@ public class HelpExecutorTest {
|
|||||||
@Test
|
@Test
|
||||||
public final void testExecute(){
|
public final void testExecute(){
|
||||||
try {
|
try {
|
||||||
TestConsoleManager test = new TestConsoleManager();
|
PipedConsoleManager test = new PipedConsoleManager();
|
||||||
HelpExecutor help = new HelpExecutor("?", test,
|
HelpExecutor help = new HelpExecutor("?", test,
|
||||||
new Command("mock") {
|
new Command("mock") {
|
||||||
|
|
||||||
@ -116,7 +116,7 @@ public class HelpExecutorTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public final void testTip(){
|
public final void testTip(){
|
||||||
try (TestConsoleManager test = new TestConsoleManager()) {
|
try (PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
HelpExecutor help = new HelpExecutor("?", test,
|
HelpExecutor help = new HelpExecutor("?", test,
|
||||||
new MockCommand("mock"));
|
new MockCommand("mock"));
|
||||||
help.tip();
|
help.tip();
|
||||||
@ -124,7 +124,7 @@ public class HelpExecutorTest {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
assertNull(e);
|
assertNull(e);
|
||||||
}
|
}
|
||||||
try (TestConsoleManager test = new TestConsoleManager()) {
|
try (PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
HelpExecutor help = new HelpExecutor("?", test,
|
HelpExecutor help = new HelpExecutor("?", test,
|
||||||
new SubedCommand("sub", new MockCommand("mock")));
|
new SubedCommand("sub", new MockCommand("mock")));
|
||||||
help.tip();
|
help.tip();
|
||||||
|
@ -51,7 +51,7 @@ import org.junit.Test;
|
|||||||
|
|
||||||
import fr.bigeon.gclc.exception.CommandRunException;
|
import fr.bigeon.gclc.exception.CommandRunException;
|
||||||
import fr.bigeon.gclc.exception.InvalidParameterException;
|
import fr.bigeon.gclc.exception.InvalidParameterException;
|
||||||
import fr.bigeon.gclc.test.utils.TestConsoleManager;
|
import fr.bigeon.gclc.manager.PipedConsoleManager;
|
||||||
|
|
||||||
/** <p>
|
/** <p>
|
||||||
* TODO
|
* TODO
|
||||||
@ -63,7 +63,7 @@ public class ParametrizedCommandTest {
|
|||||||
* {@link fr.bigeon.gclc.command.ParametrizedCommand#ParametrizedCommand(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String)}. */
|
* {@link fr.bigeon.gclc.command.ParametrizedCommand#ParametrizedCommand(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String)}. */
|
||||||
@Test
|
@Test
|
||||||
public final void testParametrizedCommandConsoleManagerString() {
|
public final void testParametrizedCommandConsoleManagerString() {
|
||||||
try (TestConsoleManager test = new TestConsoleManager()) {
|
try (PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
ParametrizedCommand cmd = new ParametrizedCommand(test, "name") {
|
ParametrizedCommand cmd = new ParametrizedCommand(test, "name") {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -116,7 +116,7 @@ public class ParametrizedCommandTest {
|
|||||||
* {@link fr.bigeon.gclc.command.ParametrizedCommand#ParametrizedCommand(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String, boolean)}. */
|
* {@link fr.bigeon.gclc.command.ParametrizedCommand#ParametrizedCommand(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String, boolean)}. */
|
||||||
@Test
|
@Test
|
||||||
public final void testParametrizedCommandConsoleManagerStringBoolean() {
|
public final void testParametrizedCommandConsoleManagerStringBoolean() {
|
||||||
try (TestConsoleManager test = new TestConsoleManager()) {
|
try (PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
ParametrizedCommand cmd = new ParametrizedCommand(test, "name",
|
ParametrizedCommand cmd = new ParametrizedCommand(test, "name",
|
||||||
false) {
|
false) {
|
||||||
|
|
||||||
@ -528,7 +528,7 @@ public class ParametrizedCommandTest {
|
|||||||
assertNotNull(e);
|
assertNotNull(e);
|
||||||
}
|
}
|
||||||
// TODO Test of interactive not providing and providing all needed
|
// TODO Test of interactive not providing and providing all needed
|
||||||
try (TestConsoleManager test = new TestConsoleManager()) {
|
try (PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
cmd = new ParametrizedCommand(test, "name", false) {
|
cmd = new ParametrizedCommand(test, "name", false) {
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
@ -610,7 +610,7 @@ public class ParametrizedCommandTest {
|
|||||||
fail("unepected error");
|
fail("unepected error");
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
TestConsoleManager test = new TestConsoleManager();
|
PipedConsoleManager test = new PipedConsoleManager();
|
||||||
cmd = new ParametrizedCommand(test, "name") {
|
cmd = new ParametrizedCommand(test, "name") {
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
|
@ -50,7 +50,7 @@ import org.junit.Test;
|
|||||||
import fr.bigeon.gclc.ConsoleTestApplication;
|
import fr.bigeon.gclc.ConsoleTestApplication;
|
||||||
import fr.bigeon.gclc.exception.CommandRunException;
|
import fr.bigeon.gclc.exception.CommandRunException;
|
||||||
import fr.bigeon.gclc.exception.CommandRunExceptionType;
|
import fr.bigeon.gclc.exception.CommandRunExceptionType;
|
||||||
import fr.bigeon.gclc.test.utils.TestConsoleManager;
|
import fr.bigeon.gclc.manager.PipedConsoleManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
@ -67,9 +67,9 @@ public class ScriptExecutionTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testExecute() {
|
public void testExecute() {
|
||||||
TestConsoleManager test;
|
PipedConsoleManager test;
|
||||||
try {
|
try {
|
||||||
test = new TestConsoleManager();
|
test = new PipedConsoleManager();
|
||||||
} catch (IOException e2) {
|
} catch (IOException e2) {
|
||||||
fail("creation of console manager failed"); //$NON-NLS-1$
|
fail("creation of console manager failed"); //$NON-NLS-1$
|
||||||
assertNotNull(e2);
|
assertNotNull(e2);
|
||||||
@ -165,7 +165,7 @@ public class ScriptExecutionTest {
|
|||||||
public void testHelp() {
|
public void testHelp() {
|
||||||
ScriptExecution exec = new ScriptExecution("script", null, "#", //$NON-NLS-1$ //$NON-NLS-2$
|
ScriptExecution exec = new ScriptExecution("script", null, "#", //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
Charset.forName("UTF-8"));
|
Charset.forName("UTF-8"));
|
||||||
try (TestConsoleManager test = new TestConsoleManager()) {
|
try (PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
exec.help(test);
|
exec.help(test);
|
||||||
exec.help(test, "ignored element");
|
exec.help(test, "ignored element");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
@ -50,7 +50,7 @@ import org.junit.Test;
|
|||||||
import fr.bigeon.gclc.exception.CommandRunException;
|
import fr.bigeon.gclc.exception.CommandRunException;
|
||||||
import fr.bigeon.gclc.exception.InvalidCommandName;
|
import fr.bigeon.gclc.exception.InvalidCommandName;
|
||||||
import fr.bigeon.gclc.manager.ConsoleManager;
|
import fr.bigeon.gclc.manager.ConsoleManager;
|
||||||
import fr.bigeon.gclc.test.utils.TestConsoleManager;
|
import fr.bigeon.gclc.manager.PipedConsoleManager;
|
||||||
|
|
||||||
/** <p>
|
/** <p>
|
||||||
* TODO
|
* TODO
|
||||||
@ -307,8 +307,8 @@ public class SubedCommandTest {
|
|||||||
assertNotNull(e);
|
assertNotNull(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
try (TestConsoleManager manager = new TestConsoleManager();
|
try (PipedConsoleManager manager = new PipedConsoleManager();
|
||||||
TestConsoleManager manager2 = new TestConsoleManager()) {
|
PipedConsoleManager manager2 = new PipedConsoleManager()) {
|
||||||
cmd.help(manager);
|
cmd.help(manager);
|
||||||
assertEquals("\tid", manager.readNextLine());
|
assertEquals("\tid", manager.readNextLine());
|
||||||
cmd.help(manager, "id");
|
cmd.help(manager, "id");
|
||||||
@ -327,8 +327,8 @@ public class SubedCommandTest {
|
|||||||
assertNotNull(e);
|
assertNotNull(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
try (TestConsoleManager manager = new TestConsoleManager();
|
try (PipedConsoleManager manager = new PipedConsoleManager();
|
||||||
TestConsoleManager manager2 = new TestConsoleManager()) {
|
PipedConsoleManager manager2 = new PipedConsoleManager()) {
|
||||||
cmd.help(manager);
|
cmd.help(manager);
|
||||||
assertEquals("\tid", manager.readNextLine());
|
assertEquals("\tid", manager.readNextLine());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@ -368,8 +368,8 @@ public class SubedCommandTest {
|
|||||||
assertNotNull(e);
|
assertNotNull(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
try (TestConsoleManager manager = new TestConsoleManager();
|
try (PipedConsoleManager manager = new PipedConsoleManager();
|
||||||
TestConsoleManager manager2 = new TestConsoleManager()) {
|
PipedConsoleManager manager2 = new PipedConsoleManager()) {
|
||||||
cmd.help(manager);
|
cmd.help(manager);
|
||||||
assertEquals("\ttip", manager.readNextLine());
|
assertEquals("\ttip", manager.readNextLine());
|
||||||
assertEquals("\tid: tip", manager.readNextLine());
|
assertEquals("\tid: tip", manager.readNextLine());
|
||||||
|
@ -53,7 +53,7 @@ import org.junit.After;
|
|||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import fr.bigeon.gclc.test.utils.TestConsoleManager;
|
import fr.bigeon.gclc.manager.PipedConsoleManager;
|
||||||
|
|
||||||
/** <p>
|
/** <p>
|
||||||
* TODO
|
* TODO
|
||||||
@ -77,7 +77,7 @@ public class CLIPrompterTest {
|
|||||||
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptBoolean(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String)}. */
|
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptBoolean(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String)}. */
|
||||||
@Test
|
@Test
|
||||||
public final void testPromptBoolean() {
|
public final void testPromptBoolean() {
|
||||||
try (final TestConsoleManager test = new TestConsoleManager()) {
|
try (final PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
Thread th = new Thread(new Runnable() {
|
Thread th = new Thread(new Runnable() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -121,7 +121,7 @@ public class CLIPrompterTest {
|
|||||||
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptChoice(fr.bigeon.gclc.manager.ConsoleManager, java.util.List, java.util.List, java.lang.String, java.lang.String)}. */
|
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptChoice(fr.bigeon.gclc.manager.ConsoleManager, java.util.List, java.util.List, java.lang.String, java.lang.String)}. */
|
||||||
@Test
|
@Test
|
||||||
public final void testPromptChoiceConsoleManagerListOfStringListOfUStringString() {
|
public final void testPromptChoiceConsoleManagerListOfStringListOfUStringString() {
|
||||||
try (final TestConsoleManager test = new TestConsoleManager()) {
|
try (final PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
final List<String> keys = new ArrayList<>();
|
final List<String> keys = new ArrayList<>();
|
||||||
final List<Object> choices = new ArrayList<>();
|
final List<Object> choices = new ArrayList<>();
|
||||||
keys.add("A choice"); //$NON-NLS-1$
|
keys.add("A choice"); //$NON-NLS-1$
|
||||||
@ -201,7 +201,7 @@ public class CLIPrompterTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void testPromptChoiceConsoleManagerListOfUStringString() {
|
public final void testPromptChoiceConsoleManagerListOfUStringString() {
|
||||||
try (final TestConsoleManager test = new TestConsoleManager()) {
|
try (final PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
final List<Object> keys = new ArrayList<>();
|
final List<Object> keys = new ArrayList<>();
|
||||||
keys.add("A choice"); //$NON-NLS-1$
|
keys.add("A choice"); //$NON-NLS-1$
|
||||||
keys.add("An other"); //$NON-NLS-1$
|
keys.add("An other"); //$NON-NLS-1$
|
||||||
@ -289,7 +289,7 @@ public class CLIPrompterTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void testPromptChoiceConsoleManagerListOfUMapOfUTStringString() {
|
public final void testPromptChoiceConsoleManagerListOfUMapOfUTStringString() {
|
||||||
try (final TestConsoleManager test = new TestConsoleManager()) {
|
try (final PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
final List<Object> keys = new ArrayList<>();
|
final List<Object> keys = new ArrayList<>();
|
||||||
final Map<Object, Object> choices = new HashMap<>();
|
final Map<Object, Object> choices = new HashMap<>();
|
||||||
keys.add("A choice"); //$NON-NLS-1$
|
keys.add("A choice"); //$NON-NLS-1$
|
||||||
@ -373,7 +373,7 @@ public class CLIPrompterTest {
|
|||||||
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptChoice(fr.bigeon.gclc.manager.ConsoleManager, java.util.Map, java.lang.String, java.lang.String)}. */
|
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptChoice(fr.bigeon.gclc.manager.ConsoleManager, java.util.Map, java.lang.String, java.lang.String)}. */
|
||||||
@Test
|
@Test
|
||||||
public final void testPromptChoiceConsoleManagerMapOfUTStringString() {
|
public final void testPromptChoiceConsoleManagerMapOfUTStringString() {
|
||||||
try (final TestConsoleManager test = new TestConsoleManager()) {
|
try (final PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
final List<Object> keys = new ArrayList<>();
|
final List<Object> keys = new ArrayList<>();
|
||||||
final Map<Object, Object> choices = new HashMap<>();
|
final Map<Object, Object> choices = new HashMap<>();
|
||||||
keys.add("A choice"); //$NON-NLS-1$
|
keys.add("A choice"); //$NON-NLS-1$
|
||||||
@ -455,7 +455,7 @@ public class CLIPrompterTest {
|
|||||||
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptInteger(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String)}. */
|
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptInteger(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String)}. */
|
||||||
@Test
|
@Test
|
||||||
public final void testPromptInteger() {
|
public final void testPromptInteger() {
|
||||||
try (final TestConsoleManager test = new TestConsoleManager()) {
|
try (final PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
Thread th = new Thread(new Runnable() {
|
Thread th = new Thread(new Runnable() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -492,7 +492,7 @@ public class CLIPrompterTest {
|
|||||||
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptList(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String)}. */
|
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptList(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String)}. */
|
||||||
@Test
|
@Test
|
||||||
public final void testPromptListConsoleManagerString() {
|
public final void testPromptListConsoleManagerString() {
|
||||||
try (final TestConsoleManager test = new TestConsoleManager()) {
|
try (final PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
final List<String> keys = new ArrayList<>();
|
final List<String> keys = new ArrayList<>();
|
||||||
keys.add("A choice"); //$NON-NLS-1$
|
keys.add("A choice"); //$NON-NLS-1$
|
||||||
keys.add("An other"); //$NON-NLS-1$
|
keys.add("An other"); //$NON-NLS-1$
|
||||||
@ -549,7 +549,7 @@ public class CLIPrompterTest {
|
|||||||
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptList(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String, java.lang.String)}. */
|
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptList(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String, java.lang.String)}. */
|
||||||
@Test
|
@Test
|
||||||
public final void testPromptListConsoleManagerStringString() {
|
public final void testPromptListConsoleManagerStringString() {
|
||||||
try (final TestConsoleManager test = new TestConsoleManager()) {
|
try (final PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
final List<String> keys = new ArrayList<>();
|
final List<String> keys = new ArrayList<>();
|
||||||
keys.add("A choice"); //$NON-NLS-1$
|
keys.add("A choice"); //$NON-NLS-1$
|
||||||
keys.add("An other"); //$NON-NLS-1$
|
keys.add("An other"); //$NON-NLS-1$
|
||||||
@ -603,7 +603,7 @@ public class CLIPrompterTest {
|
|||||||
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptLongText(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String)}. */
|
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptLongText(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String)}. */
|
||||||
@Test
|
@Test
|
||||||
public final void testPromptLongTextConsoleManagerString() {
|
public final void testPromptLongTextConsoleManagerString() {
|
||||||
try (final TestConsoleManager test = new TestConsoleManager()) {
|
try (final PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
final String message = "My message";
|
final String message = "My message";
|
||||||
final String longText = "Some text with" + System.lineSeparator() +
|
final String longText = "Some text with" + System.lineSeparator() +
|
||||||
"line feeds and other" + System.lineSeparator() +
|
"line feeds and other" + System.lineSeparator() +
|
||||||
@ -663,7 +663,7 @@ public class CLIPrompterTest {
|
|||||||
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptLongText(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String, java.lang.String)}. */
|
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptLongText(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String, java.lang.String)}. */
|
||||||
@Test
|
@Test
|
||||||
public final void testPromptLongTextConsoleManagerStringString() {
|
public final void testPromptLongTextConsoleManagerStringString() {
|
||||||
try (final TestConsoleManager test = new TestConsoleManager()) {
|
try (final PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
final String message = "My message";
|
final String message = "My message";
|
||||||
final String ender = "\\quit";
|
final String ender = "\\quit";
|
||||||
final String[] text = new String[]{"Some text with" ,
|
final String[] text = new String[]{"Some text with" ,
|
||||||
@ -676,7 +676,7 @@ public class CLIPrompterTest {
|
|||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
assertEquals("",
|
assertEquals("", //$NON-NLS-1$
|
||||||
CLIPrompter.promptLongText(test, message, ender));
|
CLIPrompter.promptLongText(test, message, ender));
|
||||||
assertEquals(longText,
|
assertEquals(longText,
|
||||||
CLIPrompter.promptLongText(test, message, ender));
|
CLIPrompter.promptLongText(test, message, ender));
|
||||||
@ -720,7 +720,7 @@ public class CLIPrompterTest {
|
|||||||
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptMultiChoice(fr.bigeon.gclc.manager.ConsoleManager, java.util.List, java.util.List, java.lang.String)}. */
|
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptMultiChoice(fr.bigeon.gclc.manager.ConsoleManager, java.util.List, java.util.List, java.lang.String)}. */
|
||||||
@Test
|
@Test
|
||||||
public final void testPromptMultiChoiceConsoleManagerListOfStringListOfUString() {
|
public final void testPromptMultiChoiceConsoleManagerListOfStringListOfUString() {
|
||||||
try (final TestConsoleManager test = new TestConsoleManager()) {
|
try (final PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
final List<String> keys = new ArrayList<>();
|
final List<String> keys = new ArrayList<>();
|
||||||
final List<Object> choices = new ArrayList<>();
|
final List<Object> choices = new ArrayList<>();
|
||||||
keys.add("A choice"); //$NON-NLS-1$
|
keys.add("A choice"); //$NON-NLS-1$
|
||||||
@ -803,7 +803,7 @@ public class CLIPrompterTest {
|
|||||||
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptMultiChoice(fr.bigeon.gclc.manager.ConsoleManager, java.util.List, java.util.Map, java.lang.String)}. */
|
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptMultiChoice(fr.bigeon.gclc.manager.ConsoleManager, java.util.List, java.util.Map, java.lang.String)}. */
|
||||||
@Test
|
@Test
|
||||||
public final void testPromptMultiChoiceConsoleManagerListOfUMapOfUTString() {
|
public final void testPromptMultiChoiceConsoleManagerListOfUMapOfUTString() {
|
||||||
try (final TestConsoleManager test = new TestConsoleManager()) {
|
try (final PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
final List<Object> keys = new ArrayList<>();
|
final List<Object> keys = new ArrayList<>();
|
||||||
final Map<Object, Object> choices = new HashMap<>();
|
final Map<Object, Object> choices = new HashMap<>();
|
||||||
keys.add("A choice"); //$NON-NLS-1$
|
keys.add("A choice"); //$NON-NLS-1$
|
||||||
@ -886,7 +886,7 @@ public class CLIPrompterTest {
|
|||||||
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptMultiChoice(fr.bigeon.gclc.manager.ConsoleManager, java.util.List, java.lang.String)}. */
|
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptMultiChoice(fr.bigeon.gclc.manager.ConsoleManager, java.util.List, java.lang.String)}. */
|
||||||
@Test
|
@Test
|
||||||
public final void testPromptMultiChoiceConsoleManagerListOfUString() {
|
public final void testPromptMultiChoiceConsoleManagerListOfUString() {
|
||||||
try (final TestConsoleManager test = new TestConsoleManager()) {
|
try (final PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
final List<Object> keys = new ArrayList<>();
|
final List<Object> keys = new ArrayList<>();
|
||||||
keys.add("A choice"); //$NON-NLS-1$
|
keys.add("A choice"); //$NON-NLS-1$
|
||||||
keys.add("An other"); //$NON-NLS-1$
|
keys.add("An other"); //$NON-NLS-1$
|
||||||
@ -966,7 +966,7 @@ public class CLIPrompterTest {
|
|||||||
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptMultiChoice(fr.bigeon.gclc.manager.ConsoleManager, java.util.Map, java.lang.String)}. */
|
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptMultiChoice(fr.bigeon.gclc.manager.ConsoleManager, java.util.Map, java.lang.String)}. */
|
||||||
@Test
|
@Test
|
||||||
public final void testPromptMultiChoiceConsoleManagerMapOfUTString() {
|
public final void testPromptMultiChoiceConsoleManagerMapOfUTString() {
|
||||||
try (final TestConsoleManager test = new TestConsoleManager()) {
|
try (final PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
final List<Object> keys = new ArrayList<>();
|
final List<Object> keys = new ArrayList<>();
|
||||||
final Map<Object, Object> choices = new HashMap<>();
|
final Map<Object, Object> choices = new HashMap<>();
|
||||||
keys.add("A choice"); //$NON-NLS-1$
|
keys.add("A choice"); //$NON-NLS-1$
|
||||||
@ -1049,7 +1049,7 @@ public class CLIPrompterTest {
|
|||||||
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptNonEmpty(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String, java.lang.String)}. */
|
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptNonEmpty(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String, java.lang.String)}. */
|
||||||
@Test
|
@Test
|
||||||
public final void testPromptNonEmpty() {
|
public final void testPromptNonEmpty() {
|
||||||
try (final TestConsoleManager test = new TestConsoleManager()) {
|
try (final PipedConsoleManager test = new PipedConsoleManager()) {
|
||||||
final String res = "some content"; //$NON-NLS-1$
|
final String res = "some content"; //$NON-NLS-1$
|
||||||
Thread th = new Thread(new Runnable() {
|
Thread th = new Thread(new Runnable() {
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user