From 72362936be35de7213784d2552a8f1f963c795ae Mon Sep 17 00:00:00 2001 From: Emmanuel Bigeon Date: Wed, 30 Nov 2016 09:49:16 -0500 Subject: [PATCH] Added piped stream manager Signed-off-by: Emmanuel Bigeon --- .../fr/bigeon/gclc/ConsoleApplication.java | 48 +++--- .../bigeon/gclc/manager/ConsoleManager.java | 7 +- .../gclc/manager/PipedConsoleManager.java} | 47 ++++-- .../fr/bigeon/gclc/manager/ReadRunnable.java | 54 ------ .../bigeon/gclc/manager/ReadingRunnable.java | 154 ++++++++++++++++++ .../gclc/manager/SystemConsoleManager.java | 45 ++--- .../bigeon/gclc/manager/WritingRunnable.java | 138 ++++++++++++++++ .../gclc/CommandTestingApplication.java | 8 +- .../bigeon/gclc/ConsoleApplicationTest.java | 6 +- .../fr/bigeon/gclc/command/CommandTest.java | 4 +- .../bigeon/gclc/command/HelpExecutorTest.java | 10 +- .../gclc/command/ParametrizedCommandTest.java | 10 +- .../gclc/command/ScriptExecutionTest.java | 8 +- .../bigeon/gclc/command/SubedCommandTest.java | 14 +- .../bigeon/gclc/prompt/CLIPrompterTest.java | 34 ++-- 15 files changed, 416 insertions(+), 171 deletions(-) rename gclc/src/{test/java/fr/bigeon/gclc/test/utils/TestConsoleManager.java => main/java/fr/bigeon/gclc/manager/PipedConsoleManager.java} (74%) delete mode 100644 gclc/src/main/java/fr/bigeon/gclc/manager/ReadRunnable.java create mode 100644 gclc/src/main/java/fr/bigeon/gclc/manager/ReadingRunnable.java create mode 100644 gclc/src/main/java/fr/bigeon/gclc/manager/WritingRunnable.java diff --git a/gclc/src/main/java/fr/bigeon/gclc/ConsoleApplication.java b/gclc/src/main/java/fr/bigeon/gclc/ConsoleApplication.java index c720d3d..638e5cd 100644 --- a/gclc/src/main/java/fr/bigeon/gclc/ConsoleApplication.java +++ b/gclc/src/main/java/fr/bigeon/gclc/ConsoleApplication.java @@ -193,26 +193,7 @@ public class ConsoleApplication implements ICommandProvider { return; } do { - try { - 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); - } + runLoop(); } while (running); if (footer != null) { try { @@ -229,6 +210,33 @@ public class ConsoleApplication implements ICommandProvider { LOGGER.fine("Exiting application."); //$NON-NLS-1$ } + /** The running loop content. + *

+ * 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 */ public boolean isRunning() { return running; diff --git a/gclc/src/main/java/fr/bigeon/gclc/manager/ConsoleManager.java b/gclc/src/main/java/fr/bigeon/gclc/manager/ConsoleManager.java index 154d1dc..18c4c8a 100644 --- a/gclc/src/main/java/fr/bigeon/gclc/manager/ConsoleManager.java +++ b/gclc/src/main/java/fr/bigeon/gclc/manager/ConsoleManager.java @@ -72,13 +72,13 @@ public interface ConsoleManager { /** @return the user inputed string * @throws IOException if the manager is closed or could not read the prompt * @throws InterruptedIOException if the prompt was interrupted */ - String prompt() throws IOException, InterruptedIOException; + String prompt() throws IOException; /** @param message the message to prompt the user * @return the user inputed string * @throws IOException if the manager is closed or could not read the prompt * @throws InterruptedIOException if the prompt was interrupted */ - String prompt(String message) throws IOException, InterruptedIOException; + String prompt(String message) throws IOException; /**

* Set a prompting prefix. @@ -94,6 +94,7 @@ public interface ConsoleManager { /** @return if the manager is closed. */ 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(); } diff --git a/gclc/src/test/java/fr/bigeon/gclc/test/utils/TestConsoleManager.java b/gclc/src/main/java/fr/bigeon/gclc/manager/PipedConsoleManager.java similarity index 74% rename from gclc/src/test/java/fr/bigeon/gclc/test/utils/TestConsoleManager.java rename to gclc/src/main/java/fr/bigeon/gclc/manager/PipedConsoleManager.java index e050b83..5e9c441 100644 --- a/gclc/src/test/java/fr/bigeon/gclc/test/utils/TestConsoleManager.java +++ b/gclc/src/main/java/fr/bigeon/gclc/manager/PipedConsoleManager.java @@ -36,7 +36,7 @@ * gclc-test:fr.bigeon.gclc.test.TestConsoleManager.java * Created on: Nov 18, 2016 */ -package fr.bigeon.gclc.test.utils; +package fr.bigeon.gclc.manager; import java.io.BufferedReader; import java.io.IOException; @@ -44,12 +44,8 @@ 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. *

@@ -57,26 +53,43 @@ import fr.bigeon.gclc.manager.SystemConsoleManager; * used to test application behavior. * * @author Emmanuel Bigeon */ -public class TestConsoleManager implements ConsoleManager, AutoCloseable { +public final class PipedConsoleManager + implements ConsoleManager, AutoCloseable { + /** THe inner manager */ private final SystemConsoleManager innerManager; + /** The stream to pipe commands into */ private final PipedOutputStream commandInput; + /** The reader to get application return from */ private final BufferedReader commandBuffOutput; + /** The stream to get application return from */ private final PipedInputStream commandOutput; + /** The print writer for application to write return to */ private final PrintStream outPrint; + /** The stream for the application to read commands from */ 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 */ - public TestConsoleManager() throws IOException { + public PipedConsoleManager() 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, true, Charset.defaultCharset().name()); + new InputStreamReader(commandOutput, Charset.forName("UTF-8"))); + outPrint = new PrintStream(out, true, "UTF-8"); 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 @@ -118,6 +131,8 @@ public class TestConsoleManager implements ConsoleManager, AutoCloseable { @Override public void close() throws IOException { innerManager.close(); + reading.setRunning(false); + writing.setRunning(false); outPrint.close(); commandBuffOutput.close(); commandOutput.close(); @@ -130,16 +145,16 @@ public class TestConsoleManager implements ConsoleManager, AutoCloseable { 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 { - ByteBuffer buff = Charset.defaultCharset() - .encode(content + System.lineSeparator()); - if (buff.hasArray()) { - commandInput.write(buff.array()); - } + writing.addMessage(content); } + /** @return the content of the next line written by the application + * @throws IOException if the reading failed */ public String readNextLine() throws IOException { - return commandBuffOutput.readLine(); + return reading.getMessage(); } /* (non-Javadoc) diff --git a/gclc/src/main/java/fr/bigeon/gclc/manager/ReadRunnable.java b/gclc/src/main/java/fr/bigeon/gclc/manager/ReadRunnable.java deleted file mode 100644 index 8515de3..0000000 --- a/gclc/src/main/java/fr/bigeon/gclc/manager/ReadRunnable.java +++ /dev/null @@ -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; - -/** - *

- * 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; - } -} diff --git a/gclc/src/main/java/fr/bigeon/gclc/manager/ReadingRunnable.java b/gclc/src/main/java/fr/bigeon/gclc/manager/ReadingRunnable.java new file mode 100644 index 0000000..0f1bf45 --- /dev/null +++ b/gclc/src/main/java/fr/bigeon/gclc/manager/ReadingRunnable.java @@ -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 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; + } + } +} diff --git a/gclc/src/main/java/fr/bigeon/gclc/manager/SystemConsoleManager.java b/gclc/src/main/java/fr/bigeon/gclc/manager/SystemConsoleManager.java index b785a1f..8333072 100644 --- a/gclc/src/main/java/fr/bigeon/gclc/manager/SystemConsoleManager.java +++ b/gclc/src/main/java/fr/bigeon/gclc/manager/SystemConsoleManager.java @@ -44,25 +44,17 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintStream; import java.nio.charset.Charset; -import java.util.logging.Logger; /** A console using the input stream and print stream. *

* The default constructor will use the system standart input and output. * * @author Emmanuel BIGEON */ -public class SystemConsoleManager implements ConsoleManager { // NOSONAR +public final class SystemConsoleManager implements ConsoleManager { // NOSONAR /** The default prompt */ 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. */ private String prompt = DEFAULT_PROMPT; @@ -74,7 +66,11 @@ public class SystemConsoleManager implements ConsoleManager { // NOSONAR /** If the manager is closed */ 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 * and input stream. */ @@ -90,6 +86,9 @@ public class SystemConsoleManager implements ConsoleManager { // NOSONAR super(); this.out = out; 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 */ @@ -142,23 +141,7 @@ public class SystemConsoleManager implements ConsoleManager { // NOSONAR public String prompt(String message) throws IOException { checkOpen(); out.print(message); -// ReadRunnable rr = new ReadRunnable(in); -// 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(); + return reading.getMessage(); } /** @param prompt the prompt to set */ @@ -172,6 +155,7 @@ public class SystemConsoleManager implements ConsoleManager { // NOSONAR @Override public void close() throws IOException { closed = true; + reading.setRunning(false); } /* (non-Javadoc) @@ -181,13 +165,12 @@ public class SystemConsoleManager implements ConsoleManager { // NOSONAR return closed; } - /* (non-Javadoc) + /** Beware, in this implementation this is the same as closing the manager. + * * @see fr.bigeon.gclc.manager.ConsoleManager#interruptPrompt() */ @Override public void interruptPrompt() { - if (reading != null) { - reading.interrupt(); - } + promptThread.interrupt(); } } diff --git a/gclc/src/main/java/fr/bigeon/gclc/manager/WritingRunnable.java b/gclc/src/main/java/fr/bigeon/gclc/manager/WritingRunnable.java new file mode 100644 index 0000000..3280a56 --- /dev/null +++ b/gclc/src/main/java/fr/bigeon/gclc/manager/WritingRunnable.java @@ -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; + +/**

+ * 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 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; + } + } +} diff --git a/gclc/src/test/java/fr/bigeon/gclc/CommandTestingApplication.java b/gclc/src/test/java/fr/bigeon/gclc/CommandTestingApplication.java index 67203a4..d832bd3 100644 --- a/gclc/src/test/java/fr/bigeon/gclc/CommandTestingApplication.java +++ b/gclc/src/test/java/fr/bigeon/gclc/CommandTestingApplication.java @@ -42,7 +42,7 @@ import java.io.IOException; import fr.bigeon.gclc.command.ICommand; import fr.bigeon.gclc.exception.InvalidCommandName; -import fr.bigeon.gclc.test.utils.TestConsoleManager; +import fr.bigeon.gclc.manager.PipedConsoleManager; /** *

@@ -56,11 +56,11 @@ public class CommandTestingApplication implements AutoCloseable { private final ConsoleTestApplication application; private final Thread th; - private final TestConsoleManager manager; + private final PipedConsoleManager manager; /** @throws IOException if the streams cannot be build */ public CommandTestingApplication() throws IOException { - manager = new TestConsoleManager(); + manager = new PipedConsoleManager(); application = new ConsoleTestApplication(manager); 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 * @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 { String ret = manager.readNextLine(); if (ret.equals(manager.getPrompt())) { diff --git a/gclc/src/test/java/fr/bigeon/gclc/ConsoleApplicationTest.java b/gclc/src/test/java/fr/bigeon/gclc/ConsoleApplicationTest.java index 017f188..5cdc62d 100644 --- a/gclc/src/test/java/fr/bigeon/gclc/ConsoleApplicationTest.java +++ b/gclc/src/test/java/fr/bigeon/gclc/ConsoleApplicationTest.java @@ -56,7 +56,7 @@ 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.test.utils.TestConsoleManager; +import fr.bigeon.gclc.manager.PipedConsoleManager; /** Test class for ConsoleApplication * @@ -136,7 +136,7 @@ public class ConsoleApplicationTest { } ConsoleApplication appli = null; - try (TestConsoleManager manager = new TestConsoleManager()) { + try (PipedConsoleManager manager = new PipedConsoleManager()) { final ConsoleApplication app = new ConsoleApplication(manager, null, null); appli = app; @@ -167,7 +167,7 @@ public class ConsoleApplicationTest { @Test public void interpretCommandTest() { - try (TestConsoleManager test = new TestConsoleManager()) { + try (PipedConsoleManager test = new PipedConsoleManager()) { ConsoleApplication appl = new ConsoleApplication(test, "", ""); appl.interpretCommand("invalid cmd \"due to misplaced\"quote"); diff --git a/gclc/src/test/java/fr/bigeon/gclc/command/CommandTest.java b/gclc/src/test/java/fr/bigeon/gclc/command/CommandTest.java index 8b74e4e..3b969b0 100644 --- a/gclc/src/test/java/fr/bigeon/gclc/command/CommandTest.java +++ b/gclc/src/test/java/fr/bigeon/gclc/command/CommandTest.java @@ -45,7 +45,7 @@ import java.io.IOException; import org.junit.Test; import fr.bigeon.gclc.exception.CommandRunException; -import fr.bigeon.gclc.test.utils.TestConsoleManager; +import fr.bigeon.gclc.manager.PipedConsoleManager; /**

* TODO @@ -55,7 +55,7 @@ public class CommandTest { @Test public final void test() { - try (TestConsoleManager test = new TestConsoleManager()) { + try (PipedConsoleManager test = new PipedConsoleManager()) { Command cmd; cmd = new Command("name") { diff --git a/gclc/src/test/java/fr/bigeon/gclc/command/HelpExecutorTest.java b/gclc/src/test/java/fr/bigeon/gclc/command/HelpExecutorTest.java index edb4568..4492cac 100644 --- a/gclc/src/test/java/fr/bigeon/gclc/command/HelpExecutorTest.java +++ b/gclc/src/test/java/fr/bigeon/gclc/command/HelpExecutorTest.java @@ -45,7 +45,7 @@ import static org.junit.Assert.fail; import org.junit.Test; import fr.bigeon.gclc.exception.CommandRunException; -import fr.bigeon.gclc.test.utils.TestConsoleManager; +import fr.bigeon.gclc.manager.PipedConsoleManager; /** *

@@ -68,7 +68,7 @@ public class HelpExecutorTest { } catch (Exception e) { assertNotNull(e); } - try (TestConsoleManager test = new TestConsoleManager()) { + try (PipedConsoleManager test = new PipedConsoleManager()) { help = new HelpExecutor("?", test, new MockCommand("mock")); } catch (Exception e) { assertNull(e); @@ -81,7 +81,7 @@ public class HelpExecutorTest { @Test public final void testExecute(){ try { - TestConsoleManager test = new TestConsoleManager(); + PipedConsoleManager test = new PipedConsoleManager(); HelpExecutor help = new HelpExecutor("?", test, new Command("mock") { @@ -116,7 +116,7 @@ public class HelpExecutorTest { */ @Test public final void testTip(){ - try (TestConsoleManager test = new TestConsoleManager()) { + try (PipedConsoleManager test = new PipedConsoleManager()) { HelpExecutor help = new HelpExecutor("?", test, new MockCommand("mock")); help.tip(); @@ -124,7 +124,7 @@ public class HelpExecutorTest { } catch (Exception e) { assertNull(e); } - try (TestConsoleManager test = new TestConsoleManager()) { + try (PipedConsoleManager test = new PipedConsoleManager()) { HelpExecutor help = new HelpExecutor("?", test, new SubedCommand("sub", new MockCommand("mock"))); help.tip(); diff --git a/gclc/src/test/java/fr/bigeon/gclc/command/ParametrizedCommandTest.java b/gclc/src/test/java/fr/bigeon/gclc/command/ParametrizedCommandTest.java index dc60e8a..20dbd23 100644 --- a/gclc/src/test/java/fr/bigeon/gclc/command/ParametrizedCommandTest.java +++ b/gclc/src/test/java/fr/bigeon/gclc/command/ParametrizedCommandTest.java @@ -51,7 +51,7 @@ import org.junit.Test; import fr.bigeon.gclc.exception.CommandRunException; import fr.bigeon.gclc.exception.InvalidParameterException; -import fr.bigeon.gclc.test.utils.TestConsoleManager; +import fr.bigeon.gclc.manager.PipedConsoleManager; /**

* TODO @@ -63,7 +63,7 @@ public class ParametrizedCommandTest { * {@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()) { + try (PipedConsoleManager test = new PipedConsoleManager()) { ParametrizedCommand cmd = new ParametrizedCommand(test, "name") { @Override @@ -116,7 +116,7 @@ public class ParametrizedCommandTest { * {@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()) { + try (PipedConsoleManager test = new PipedConsoleManager()) { ParametrizedCommand cmd = new ParametrizedCommand(test, "name", false) { @@ -528,7 +528,7 @@ public class ParametrizedCommandTest { assertNotNull(e); } // 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) { { try { @@ -610,7 +610,7 @@ public class ParametrizedCommandTest { fail("unepected error"); } try { - TestConsoleManager test = new TestConsoleManager(); + PipedConsoleManager test = new PipedConsoleManager(); cmd = new ParametrizedCommand(test, "name") { { try { diff --git a/gclc/src/test/java/fr/bigeon/gclc/command/ScriptExecutionTest.java b/gclc/src/test/java/fr/bigeon/gclc/command/ScriptExecutionTest.java index 4126dc9..5074197 100644 --- a/gclc/src/test/java/fr/bigeon/gclc/command/ScriptExecutionTest.java +++ b/gclc/src/test/java/fr/bigeon/gclc/command/ScriptExecutionTest.java @@ -50,7 +50,7 @@ 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; +import fr.bigeon.gclc.manager.PipedConsoleManager; /** *

@@ -67,9 +67,9 @@ public class ScriptExecutionTest { */ @Test public void testExecute() { - TestConsoleManager test; + PipedConsoleManager test; try { - test = new TestConsoleManager(); + test = new PipedConsoleManager(); } catch (IOException e2) { fail("creation of console manager failed"); //$NON-NLS-1$ assertNotNull(e2); @@ -165,7 +165,7 @@ public class ScriptExecutionTest { public void testHelp() { ScriptExecution exec = new ScriptExecution("script", null, "#", //$NON-NLS-1$ //$NON-NLS-2$ Charset.forName("UTF-8")); - try (TestConsoleManager test = new TestConsoleManager()) { + try (PipedConsoleManager test = new PipedConsoleManager()) { exec.help(test); exec.help(test, "ignored element"); } catch (IOException e) { diff --git a/gclc/src/test/java/fr/bigeon/gclc/command/SubedCommandTest.java b/gclc/src/test/java/fr/bigeon/gclc/command/SubedCommandTest.java index 00998ce..bba4620 100644 --- a/gclc/src/test/java/fr/bigeon/gclc/command/SubedCommandTest.java +++ b/gclc/src/test/java/fr/bigeon/gclc/command/SubedCommandTest.java @@ -50,7 +50,7 @@ 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; +import fr.bigeon.gclc.manager.PipedConsoleManager; /**

* TODO @@ -307,8 +307,8 @@ public class SubedCommandTest { assertNotNull(e); } - try (TestConsoleManager manager = new TestConsoleManager(); - TestConsoleManager manager2 = new TestConsoleManager()) { + try (PipedConsoleManager manager = new PipedConsoleManager(); + PipedConsoleManager manager2 = new PipedConsoleManager()) { cmd.help(manager); assertEquals("\tid", manager.readNextLine()); cmd.help(manager, "id"); @@ -327,8 +327,8 @@ public class SubedCommandTest { assertNotNull(e); } - try (TestConsoleManager manager = new TestConsoleManager(); - TestConsoleManager manager2 = new TestConsoleManager()) { + try (PipedConsoleManager manager = new PipedConsoleManager(); + PipedConsoleManager manager2 = new PipedConsoleManager()) { cmd.help(manager); assertEquals("\tid", manager.readNextLine()); } catch (IOException e) { @@ -368,8 +368,8 @@ public class SubedCommandTest { assertNotNull(e); } - try (TestConsoleManager manager = new TestConsoleManager(); - TestConsoleManager manager2 = new TestConsoleManager()) { + try (PipedConsoleManager manager = new PipedConsoleManager(); + PipedConsoleManager manager2 = new PipedConsoleManager()) { cmd.help(manager); assertEquals("\ttip", manager.readNextLine()); assertEquals("\tid: tip", manager.readNextLine()); diff --git a/gclc/src/test/java/fr/bigeon/gclc/prompt/CLIPrompterTest.java b/gclc/src/test/java/fr/bigeon/gclc/prompt/CLIPrompterTest.java index d522fe7..3370151 100644 --- a/gclc/src/test/java/fr/bigeon/gclc/prompt/CLIPrompterTest.java +++ b/gclc/src/test/java/fr/bigeon/gclc/prompt/CLIPrompterTest.java @@ -53,7 +53,7 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; -import fr.bigeon.gclc.test.utils.TestConsoleManager; +import fr.bigeon.gclc.manager.PipedConsoleManager; /**

* TODO @@ -77,7 +77,7 @@ public class CLIPrompterTest { * {@link fr.bigeon.gclc.prompt.CLIPrompter#promptBoolean(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String)}. */ @Test public final void testPromptBoolean() { - try (final TestConsoleManager test = new TestConsoleManager()) { + try (final PipedConsoleManager test = new PipedConsoleManager()) { Thread th = new Thread(new Runnable() { @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)}. */ @Test public final void testPromptChoiceConsoleManagerListOfStringListOfUStringString() { - try (final TestConsoleManager test = new TestConsoleManager()) { + try (final PipedConsoleManager test = new PipedConsoleManager()) { final List keys = new ArrayList<>(); final List choices = new ArrayList<>(); keys.add("A choice"); //$NON-NLS-1$ @@ -201,7 +201,7 @@ public class CLIPrompterTest { @Test public final void testPromptChoiceConsoleManagerListOfUStringString() { - try (final TestConsoleManager test = new TestConsoleManager()) { + try (final PipedConsoleManager test = new PipedConsoleManager()) { final List keys = new ArrayList<>(); keys.add("A choice"); //$NON-NLS-1$ keys.add("An other"); //$NON-NLS-1$ @@ -289,7 +289,7 @@ public class CLIPrompterTest { @Test public final void testPromptChoiceConsoleManagerListOfUMapOfUTStringString() { - try (final TestConsoleManager test = new TestConsoleManager()) { + try (final PipedConsoleManager test = new PipedConsoleManager()) { final List keys = new ArrayList<>(); final Map choices = new HashMap<>(); 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)}. */ @Test public final void testPromptChoiceConsoleManagerMapOfUTStringString() { - try (final TestConsoleManager test = new TestConsoleManager()) { + try (final PipedConsoleManager test = new PipedConsoleManager()) { final List keys = new ArrayList<>(); final Map choices = new HashMap<>(); 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)}. */ @Test public final void testPromptInteger() { - try (final TestConsoleManager test = new TestConsoleManager()) { + try (final PipedConsoleManager test = new PipedConsoleManager()) { Thread th = new Thread(new Runnable() { @Override @@ -492,7 +492,7 @@ public class CLIPrompterTest { * {@link fr.bigeon.gclc.prompt.CLIPrompter#promptList(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String)}. */ @Test public final void testPromptListConsoleManagerString() { - try (final TestConsoleManager test = new TestConsoleManager()) { + try (final PipedConsoleManager test = new PipedConsoleManager()) { final List keys = new ArrayList<>(); keys.add("A choice"); //$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)}. */ @Test public final void testPromptListConsoleManagerStringString() { - try (final TestConsoleManager test = new TestConsoleManager()) { + try (final PipedConsoleManager test = new PipedConsoleManager()) { final List keys = new ArrayList<>(); keys.add("A choice"); //$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)}. */ @Test public final void testPromptLongTextConsoleManagerString() { - try (final TestConsoleManager test = new TestConsoleManager()) { + try (final PipedConsoleManager test = new PipedConsoleManager()) { final String message = "My message"; final String longText = "Some text with" + 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)}. */ @Test public final void testPromptLongTextConsoleManagerStringString() { - try (final TestConsoleManager test = new TestConsoleManager()) { + try (final PipedConsoleManager test = new PipedConsoleManager()) { final String message = "My message"; final String ender = "\\quit"; final String[] text = new String[]{"Some text with" , @@ -676,7 +676,7 @@ public class CLIPrompterTest { @Override public void run() { try { - assertEquals("", + assertEquals("", //$NON-NLS-1$ CLIPrompter.promptLongText(test, message, ender)); assertEquals(longText, 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)}. */ @Test public final void testPromptMultiChoiceConsoleManagerListOfStringListOfUString() { - try (final TestConsoleManager test = new TestConsoleManager()) { + try (final PipedConsoleManager test = new PipedConsoleManager()) { final List keys = new ArrayList<>(); final List choices = new ArrayList<>(); 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)}. */ @Test public final void testPromptMultiChoiceConsoleManagerListOfUMapOfUTString() { - try (final TestConsoleManager test = new TestConsoleManager()) { + try (final PipedConsoleManager test = new PipedConsoleManager()) { final List keys = new ArrayList<>(); final Map choices = new HashMap<>(); 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)}. */ @Test public final void testPromptMultiChoiceConsoleManagerListOfUString() { - try (final TestConsoleManager test = new TestConsoleManager()) { + try (final PipedConsoleManager test = new PipedConsoleManager()) { final List keys = new ArrayList<>(); keys.add("A choice"); //$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)}. */ @Test public final void testPromptMultiChoiceConsoleManagerMapOfUTString() { - try (final TestConsoleManager test = new TestConsoleManager()) { + try (final PipedConsoleManager test = new PipedConsoleManager()) { final List keys = new ArrayList<>(); final Map choices = new HashMap<>(); 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)}. */ @Test public final void testPromptNonEmpty() { - try (final TestConsoleManager test = new TestConsoleManager()) { + try (final PipedConsoleManager test = new PipedConsoleManager()) { final String res = "some content"; //$NON-NLS-1$ Thread th = new Thread(new Runnable() {