diff --git a/gclc/src/main/java/fr/bigeon/gclc/manager/PipedConsoleInput.java b/gclc/src/main/java/fr/bigeon/gclc/manager/PipedConsoleInput.java
index f3ff38c..8486b65 100644
--- a/gclc/src/main/java/fr/bigeon/gclc/manager/PipedConsoleInput.java
+++ b/gclc/src/main/java/fr/bigeon/gclc/manager/PipedConsoleInput.java
@@ -38,9 +38,7 @@
*/
package fr.bigeon.gclc.manager;
-import java.io.BufferedReader;
import java.io.IOException;
-import java.io.InputStreamReader;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;
@@ -56,67 +54,34 @@ import java.nio.charset.StandardCharsets;
public final class PipedConsoleInput
implements ConsoleInput {
- /** The encoding between streams. */
- private static final String UTF_8 = "UTF-8"; //$NON-NLS-1$
/** THe inner manager. */
private final StreamConsoleInput 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;
/** Create a manager that will write and read through piped stream.
- *
+ *
+ * @param outPrint the stream to write the prompting messages to
* @throws IOException if the piping failed for streams */
- public PipedConsoleInput() throws IOException {
+ public PipedConsoleInput(final PrintStream outPrint) throws IOException {
commandInput = new PipedOutputStream();
in = new PipedInputStream(commandInput);
- commandOutput = new PipedInputStream();
- final PipedOutputStream out = new PipedOutputStream(commandOutput);
- commandBuffOutput = new BufferedReader(
- new InputStreamReader(commandOutput, StandardCharsets.UTF_8));
- outPrint = new PrintStream(out, true, UTF_8);
innerManager = new StreamConsoleInput(outPrint, in,
StandardCharsets.UTF_8);
writing = new WritingRunnable(commandInput, StandardCharsets.UTF_8);
- reading = new ReadingRunnable(commandBuffOutput);
- Thread th = new Thread(writing, "write"); //$NON-NLS-1$
+ final Thread th = new Thread(writing, "write"); //$NON-NLS-1$
th.start();
- th = new Thread(reading, "read"); //$NON-NLS-1$
- th.setDaemon(true);
- th.start();
- }
-
- /** Test if a content is available on the reading head.
- *
- * If this method returns true, the next {@link #prompt()} operation should
- * return immediatly.
- *
- * @return the content of the next line written by the application
- * @throws IOException if the reading failed */
- public boolean available() throws IOException {
- return reading.hasMessage();
}
@Override
public void close() throws IOException {
- reading.setRunning(false);
writing.setRunning(false);
in.close();
innerManager.close();
- outPrint.close();
- commandBuffOutput.close();
- commandOutput.close();
commandInput.close();
}
@@ -125,18 +90,6 @@ public final class PipedConsoleInput
return innerManager.getPrompt();
}
- /** Wait for a specific message to arrive.
- *
- * When this method returns, the message was appended to the data, it
- * may or may not be the next line of data.
- *
- * @param message the message
- * @return the thread to join to wait for message delivery
- * @see fr.bigeon.gclc.manager.ReadingRunnable#getWaitForDelivery(java.lang.String) */
- public Thread getWaitForDelivery(final String message) {
- return reading.getWaitForDelivery(message);
- }
-
/* (non-Javadoc)
* @see fr.bigeon.gclc.manager.ConsoleManager#interruptPrompt() */
@Override
@@ -181,16 +134,6 @@ public final class PipedConsoleInput
return innerManager.prompt(message + System.lineSeparator(), timeout);
}
- /** Read the next line in the input printed content.
- *
- * This corresponds to the {@link #prompt(String)} messages.
- *
- * @return the content of the next line written by the application
- * @throws IOException if the reading failed */
- public String readNextLine() throws IOException {
- return reading.getMessage();
- }
-
/* (non-Javadoc)
* @see fr.bigeon.gclc.manager.ConsoleInput#setPrompt(java.lang.String) */
@Override
diff --git a/gclc/src/main/java/fr/bigeon/gclc/manager/StreamConsoleInput.java b/gclc/src/main/java/fr/bigeon/gclc/manager/StreamConsoleInput.java
index d544586..062903c 100644
--- a/gclc/src/main/java/fr/bigeon/gclc/manager/StreamConsoleInput.java
+++ b/gclc/src/main/java/fr/bigeon/gclc/manager/StreamConsoleInput.java
@@ -154,7 +154,10 @@ public final class StreamConsoleInput implements ConsoleInput {
@Override
public String prompt(final String message) throws IOException {
checkOpen();
- out.print(message);
+ if (out != null) {
+ out.print(message);
+ out.flush();
+ }
return reading.getMessage();
}
@@ -164,7 +167,10 @@ public final class StreamConsoleInput implements ConsoleInput {
public String prompt(final String message,
final long timeout) throws IOException {
checkOpen();
- out.print(message);
+ if (out != null) {
+ out.print(message);
+ out.flush();
+ }
return reading.getNextMessage(timeout);
}
diff --git a/gclc/src/test/java/fr/bigeon/gclc/CommandTestingApplication.java b/gclc/src/test/java/fr/bigeon/gclc/CommandTestingApplication.java
index cd4233d..09ceda8 100644
--- a/gclc/src/test/java/fr/bigeon/gclc/CommandTestingApplication.java
+++ b/gclc/src/test/java/fr/bigeon/gclc/CommandTestingApplication.java
@@ -63,7 +63,7 @@ public class CommandTestingApplication implements AutoCloseable {
/** @throws IOException if the streams cannot be build */
public CommandTestingApplication() throws IOException {
out = new PipedConsoleOutput();
- in = new PipedConsoleInput();
+ in = new PipedConsoleInput(null);
application = new ConsoleApplication(out, in, "", "");
new ConsoleTestApplication().attach(application);
th = new Thread(new Runnable() {
diff --git a/gclc/src/test/java/fr/bigeon/gclc/ConsoleApplicationTest.java b/gclc/src/test/java/fr/bigeon/gclc/ConsoleApplicationTest.java
index d9179a8..008d2ca 100644
--- a/gclc/src/test/java/fr/bigeon/gclc/ConsoleApplicationTest.java
+++ b/gclc/src/test/java/fr/bigeon/gclc/ConsoleApplicationTest.java
@@ -44,7 +44,13 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
+import java.io.BufferedReader;
import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.PipedInputStream;
+import java.io.PipedOutputStream;
+import java.io.PrintStream;
+import java.nio.charset.StandardCharsets;
import org.junit.Test;
@@ -72,8 +78,13 @@ public class ConsoleApplicationTest {
@Test
public void testConsoleApplication() {
- try (PipedConsoleInput manager = new PipedConsoleInput()) {
- final ConsoleApplication app = new ConsoleApplication(null, manager,
+ try (PipedOutputStream pout = new PipedOutputStream();
+ PipedInputStream pis = new PipedInputStream(pout);
+ BufferedReader buf = new BufferedReader(
+ new InputStreamReader(pis, StandardCharsets.UTF_8));
+ PipedConsoleInput in = new PipedConsoleInput(
+ new PrintStream(pout))) {
+ final ConsoleApplication app = new ConsoleApplication(null, in,
"", "");
app.exit();
} catch (final IOException e) {
@@ -146,7 +157,12 @@ public class ConsoleApplicationTest {
ConsoleApplication appli = null;
try (PipedConsoleOutput manager = new PipedConsoleOutput();
- PipedConsoleInput in = new PipedConsoleInput()) {
+ PipedOutputStream pout = new PipedOutputStream();
+ PipedInputStream pis = new PipedInputStream(pout);
+ BufferedReader buf = new BufferedReader(
+ new InputStreamReader(pis, StandardCharsets.UTF_8));
+ PipedConsoleInput in = new PipedConsoleInput(
+ new PrintStream(pout))) {
final ConsoleApplication app = new ConsoleApplication(manager, in,
null, null);
appli = app;
@@ -176,7 +192,7 @@ public class ConsoleApplicationTest {
@Test
public void testInterpretCommand() throws InvalidCommandName, IOException {
- try (PipedConsoleInput test = new PipedConsoleInput();
+ try (PipedConsoleInput test = new PipedConsoleInput(null);
PipedConsoleOutput out = new PipedConsoleOutput()) {
final ConsoleApplication appl = new ConsoleApplication(out, test,
"", "");
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 2a0db01..b23cc8a 100644
--- a/gclc/src/test/java/fr/bigeon/gclc/command/ParametrizedCommandTest.java
+++ b/gclc/src/test/java/fr/bigeon/gclc/command/ParametrizedCommandTest.java
@@ -44,7 +44,13 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
+import java.io.BufferedReader;
import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.PipedInputStream;
+import java.io.PipedOutputStream;
+import java.io.PrintStream;
+import java.nio.charset.StandardCharsets;
import org.junit.Test;
@@ -425,7 +431,12 @@ public class ParametrizedCommandTest {
}
// TODO Test of interactive not providing and providing all needed
try (PipedConsoleOutput out = new PipedConsoleOutput();
- PipedConsoleInput in = new PipedConsoleInput()) {
+ PipedOutputStream pout = new PipedOutputStream();
+ PipedInputStream pis = new PipedInputStream(pout);
+ BufferedReader buf = new BufferedReader(
+ new InputStreamReader(pis, StandardCharsets.UTF_8));
+ PipedConsoleInput in = new PipedConsoleInput(
+ new PrintStream(pout))) {
cmd = new ParametrizedCommand("name", false) {
{
try {
@@ -468,15 +479,15 @@ public class ParametrizedCommandTest {
public void run() {
try {
assertEquals("value of " + str1 + "? ",
- in.readNextLine());
+ buf.readLine());
in.type("");
assertEquals(
"value of " + str1 + "? (cannot be empty) ",
- in.readNextLine());
+ buf.readLine());
in.type("");
assertEquals(
"value of " + str1 + "? (cannot be empty) ",
- in.readNextLine());
+ buf.readLine());
in.type(str2);
} catch (final IOException e) {
assertNull(e);
@@ -495,7 +506,7 @@ public class ParametrizedCommandTest {
public void run() {
try {
assertEquals("value of " + str1 + "? ",
- in.readNextLine());
+ buf.readLine());
in.type(str2);
} catch (final IOException e) {
assertNull(e);
@@ -510,7 +521,7 @@ public class ParametrizedCommandTest {
}
try {
final PipedConsoleOutput out = new PipedConsoleOutput();
- final PipedConsoleInput test = new PipedConsoleInput();
+ final PipedConsoleInput test = new PipedConsoleInput(null);
cmd = new ParametrizedCommand("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 fe506c3..7a43534 100644
--- a/gclc/src/test/java/fr/bigeon/gclc/command/ScriptExecutionTest.java
+++ b/gclc/src/test/java/fr/bigeon/gclc/command/ScriptExecutionTest.java
@@ -69,7 +69,7 @@ public class ScriptExecutionTest {
PipedConsoleOutput test;
PipedConsoleInput in;
try {
- in = new PipedConsoleInput();
+ in = new PipedConsoleInput(null);
test = new PipedConsoleOutput();
} catch (final IOException e2) {
fail("creation of console manager failed"); //$NON-NLS-1$
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 129f432..30dfd4d 100644
--- a/gclc/src/test/java/fr/bigeon/gclc/prompt/CLIPrompterTest.java
+++ b/gclc/src/test/java/fr/bigeon/gclc/prompt/CLIPrompterTest.java
@@ -43,7 +43,13 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
+import java.io.BufferedReader;
import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.PipedInputStream;
+import java.io.PipedOutputStream;
+import java.io.PrintStream;
+import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -80,7 +86,12 @@ public class CLIPrompterTest {
@Test
public final void testPromptBoolean() {
try (final PipedConsoleOutput out = new PipedConsoleOutput();
- PipedConsoleInput in = new PipedConsoleInput()) {
+ PipedOutputStream pout = new PipedOutputStream();
+ PipedInputStream pis = new PipedInputStream(pout);
+ BufferedReader buf = new BufferedReader(
+ new InputStreamReader(pis, StandardCharsets.UTF_8));
+ PipedConsoleInput in = new PipedConsoleInput(
+ new PrintStream(pout))) {
final Thread th = new Thread(new Runnable() {
@Override
@@ -101,16 +112,16 @@ public class CLIPrompterTest {
}
});
th.start();
- assertTrue(in.readNextLine().startsWith("My message")); //$NON-NLS-1$
+ assertTrue(buf.readLine().startsWith("My message")); //$NON-NLS-1$
in.type(""); //$NON-NLS-1$
out.readNextLine();
- assertTrue(in.readNextLine().startsWith("My message")); //$NON-NLS-1$
+ assertTrue(buf.readLine().startsWith("My message")); //$NON-NLS-1$
in.type("Y"); //$NON-NLS-1$
- assertTrue(in.readNextLine().startsWith("My message")); //$NON-NLS-1$
+ assertTrue(buf.readLine().startsWith("My message")); //$NON-NLS-1$
in.type("yes"); //$NON-NLS-1$
- assertTrue(in.readNextLine().startsWith("My message")); //$NON-NLS-1$
+ assertTrue(buf.readLine().startsWith("My message")); //$NON-NLS-1$
in.type("N"); //$NON-NLS-1$
- assertTrue(in.readNextLine().startsWith("My message")); //$NON-NLS-1$
+ assertTrue(buf.readLine().startsWith("My message")); //$NON-NLS-1$
in.type("nO"); //$NON-NLS-1$
th.join();
} catch (IOException | InterruptedException e) {
@@ -125,7 +136,12 @@ public class CLIPrompterTest {
@Test
public final void testPromptChoiceConsoleManagerListOfStringListOfUStringString() {
try (final PipedConsoleOutput out = new PipedConsoleOutput();
- PipedConsoleInput in = new PipedConsoleInput()) {
+ PipedOutputStream pout = new PipedOutputStream();
+ PipedInputStream pis = new PipedInputStream(pout);
+ BufferedReader buf = new BufferedReader(
+ new InputStreamReader(pis, StandardCharsets.UTF_8));
+ PipedConsoleInput in = new PipedConsoleInput(
+ new PrintStream(pout))) {
final List keys = new ArrayList<>();
final List