Fixed tests

Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
Emmanuel Bigeon 2017-11-18 08:13:51 -05:00
parent e5d5edcf63
commit d32ea6b4b0
7 changed files with 317 additions and 229 deletions

View File

@ -38,9 +38,7 @@
*/ */
package fr.bigeon.gclc.manager; package fr.bigeon.gclc.manager;
import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
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;
@ -56,67 +54,34 @@ import java.nio.charset.StandardCharsets;
public final class PipedConsoleInput public final class PipedConsoleInput
implements ConsoleInput { implements ConsoleInput {
/** The encoding between streams. */
private static final String UTF_8 = "UTF-8"; //$NON-NLS-1$
/** THe inner manager. */ /** THe inner manager. */
private final StreamConsoleInput innerManager; private final StreamConsoleInput innerManager;
/** The stream to pipe commands into. */ /** 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;
/** 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. */ /** The stream for the application to read commands from. */
private final PipedInputStream in; private final PipedInputStream in;
/** The writing thread. */ /** The writing thread. */
private final WritingRunnable writing; private final WritingRunnable writing;
/** The reading thread. */
private final ReadingRunnable reading;
/** Create a manager that will write and read through piped stream. /** 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 */ * @throws IOException if the piping failed for streams */
public PipedConsoleInput() throws IOException { public PipedConsoleInput(final PrintStream outPrint) throws IOException {
commandInput = new PipedOutputStream(); commandInput = new PipedOutputStream();
in = new PipedInputStream(commandInput); 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, innerManager = new StreamConsoleInput(outPrint, in,
StandardCharsets.UTF_8); StandardCharsets.UTF_8);
writing = new WritingRunnable(commandInput, StandardCharsets.UTF_8); writing = new WritingRunnable(commandInput, StandardCharsets.UTF_8);
reading = new ReadingRunnable(commandBuffOutput); final Thread th = new Thread(writing, "write"); //$NON-NLS-1$
Thread th = new Thread(writing, "write"); //$NON-NLS-1$
th.start(); 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.
* <p>
* 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 @Override
public void close() throws IOException { public void close() throws IOException {
reading.setRunning(false);
writing.setRunning(false); writing.setRunning(false);
in.close(); in.close();
innerManager.close(); innerManager.close();
outPrint.close();
commandBuffOutput.close();
commandOutput.close();
commandInput.close(); commandInput.close();
} }
@ -125,18 +90,6 @@ public final class PipedConsoleInput
return innerManager.getPrompt(); return innerManager.getPrompt();
} }
/** Wait for a specific message to arrive.
* <p>
* When this method returns, the message was appended to the data, it
* <em>may or may not</em> 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) /* (non-Javadoc)
* @see fr.bigeon.gclc.manager.ConsoleManager#interruptPrompt() */ * @see fr.bigeon.gclc.manager.ConsoleManager#interruptPrompt() */
@Override @Override
@ -181,16 +134,6 @@ public final class PipedConsoleInput
return innerManager.prompt(message + System.lineSeparator(), timeout); return innerManager.prompt(message + System.lineSeparator(), timeout);
} }
/** Read the next line in the input printed content.
* <p>
* 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) /* (non-Javadoc)
* @see fr.bigeon.gclc.manager.ConsoleInput#setPrompt(java.lang.String) */ * @see fr.bigeon.gclc.manager.ConsoleInput#setPrompt(java.lang.String) */
@Override @Override

View File

@ -154,7 +154,10 @@ public final class StreamConsoleInput implements ConsoleInput {
@Override @Override
public String prompt(final String message) throws IOException { public String prompt(final String message) throws IOException {
checkOpen(); checkOpen();
out.print(message); if (out != null) {
out.print(message);
out.flush();
}
return reading.getMessage(); return reading.getMessage();
} }
@ -164,7 +167,10 @@ public final class StreamConsoleInput implements ConsoleInput {
public String prompt(final String message, public String prompt(final String message,
final long timeout) throws IOException { final long timeout) throws IOException {
checkOpen(); checkOpen();
out.print(message); if (out != null) {
out.print(message);
out.flush();
}
return reading.getNextMessage(timeout); return reading.getNextMessage(timeout);
} }

View File

@ -63,7 +63,7 @@ public class CommandTestingApplication implements AutoCloseable {
/** @throws IOException if the streams cannot be build */ /** @throws IOException if the streams cannot be build */
public CommandTestingApplication() throws IOException { public CommandTestingApplication() throws IOException {
out = new PipedConsoleOutput(); out = new PipedConsoleOutput();
in = new PipedConsoleInput(); in = new PipedConsoleInput(null);
application = new ConsoleApplication(out, in, "", ""); application = new ConsoleApplication(out, in, "", "");
new ConsoleTestApplication().attach(application); new ConsoleTestApplication().attach(application);
th = new Thread(new Runnable() { th = new Thread(new Runnable() {

View File

@ -44,7 +44,13 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import java.io.BufferedReader;
import java.io.IOException; 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; import org.junit.Test;
@ -72,8 +78,13 @@ public class ConsoleApplicationTest {
@Test @Test
public void testConsoleApplication() { public void testConsoleApplication() {
try (PipedConsoleInput manager = new PipedConsoleInput()) { try (PipedOutputStream pout = new PipedOutputStream();
final ConsoleApplication app = new ConsoleApplication(null, manager, 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(); app.exit();
} catch (final IOException e) { } catch (final IOException e) {
@ -146,7 +157,12 @@ public class ConsoleApplicationTest {
ConsoleApplication appli = null; ConsoleApplication appli = null;
try (PipedConsoleOutput manager = new PipedConsoleOutput(); 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, final ConsoleApplication app = new ConsoleApplication(manager, in,
null, null); null, null);
appli = app; appli = app;
@ -176,7 +192,7 @@ public class ConsoleApplicationTest {
@Test @Test
public void testInterpretCommand() throws InvalidCommandName, IOException { public void testInterpretCommand() throws InvalidCommandName, IOException {
try (PipedConsoleInput test = new PipedConsoleInput(); try (PipedConsoleInput test = new PipedConsoleInput(null);
PipedConsoleOutput out = new PipedConsoleOutput()) { PipedConsoleOutput out = new PipedConsoleOutput()) {
final ConsoleApplication appl = new ConsoleApplication(out, test, final ConsoleApplication appl = new ConsoleApplication(out, test,
"", ""); "", "");

View File

@ -44,7 +44,13 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import java.io.BufferedReader;
import java.io.IOException; 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; import org.junit.Test;
@ -425,7 +431,12 @@ public class ParametrizedCommandTest {
} }
// TODO Test of interactive not providing and providing all needed // TODO Test of interactive not providing and providing all needed
try (PipedConsoleOutput out = new PipedConsoleOutput(); 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) { cmd = new ParametrizedCommand("name", false) {
{ {
try { try {
@ -468,15 +479,15 @@ public class ParametrizedCommandTest {
public void run() { public void run() {
try { try {
assertEquals("value of " + str1 + "? ", assertEquals("value of " + str1 + "? ",
in.readNextLine()); buf.readLine());
in.type(""); in.type("");
assertEquals( assertEquals(
"value of " + str1 + "? (cannot be empty) ", "value of " + str1 + "? (cannot be empty) ",
in.readNextLine()); buf.readLine());
in.type(""); in.type("");
assertEquals( assertEquals(
"value of " + str1 + "? (cannot be empty) ", "value of " + str1 + "? (cannot be empty) ",
in.readNextLine()); buf.readLine());
in.type(str2); in.type(str2);
} catch (final IOException e) { } catch (final IOException e) {
assertNull(e); assertNull(e);
@ -495,7 +506,7 @@ public class ParametrizedCommandTest {
public void run() { public void run() {
try { try {
assertEquals("value of " + str1 + "? ", assertEquals("value of " + str1 + "? ",
in.readNextLine()); buf.readLine());
in.type(str2); in.type(str2);
} catch (final IOException e) { } catch (final IOException e) {
assertNull(e); assertNull(e);
@ -510,7 +521,7 @@ public class ParametrizedCommandTest {
} }
try { try {
final PipedConsoleOutput out = new PipedConsoleOutput(); final PipedConsoleOutput out = new PipedConsoleOutput();
final PipedConsoleInput test = new PipedConsoleInput(); final PipedConsoleInput test = new PipedConsoleInput(null);
cmd = new ParametrizedCommand("name") { cmd = new ParametrizedCommand("name") {
{ {
try { try {

View File

@ -69,7 +69,7 @@ public class ScriptExecutionTest {
PipedConsoleOutput test; PipedConsoleOutput test;
PipedConsoleInput in; PipedConsoleInput in;
try { try {
in = new PipedConsoleInput(); in = new PipedConsoleInput(null);
test = new PipedConsoleOutput(); test = new PipedConsoleOutput();
} catch (final IOException e2) { } catch (final IOException e2) {
fail("creation of console manager failed"); //$NON-NLS-1$ fail("creation of console manager failed"); //$NON-NLS-1$

View File

@ -43,7 +43,13 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import java.io.BufferedReader;
import java.io.IOException; 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.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -80,7 +86,12 @@ public class CLIPrompterTest {
@Test @Test
public final void testPromptBoolean() { public final void testPromptBoolean() {
try (final PipedConsoleOutput out = new PipedConsoleOutput(); 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() { final Thread th = new Thread(new Runnable() {
@Override @Override
@ -101,16 +112,16 @@ public class CLIPrompterTest {
} }
}); });
th.start(); 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$ in.type(""); //$NON-NLS-1$
out.readNextLine(); 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$ 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$ 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$ 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$ in.type("nO"); //$NON-NLS-1$
th.join(); th.join();
} catch (IOException | InterruptedException e) { } catch (IOException | InterruptedException e) {
@ -125,7 +136,12 @@ public class CLIPrompterTest {
@Test @Test
public final void testPromptChoiceConsoleManagerListOfStringListOfUStringString() { public final void testPromptChoiceConsoleManagerListOfStringListOfUStringString() {
try (final PipedConsoleOutput out = new PipedConsoleOutput(); 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<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$
@ -140,12 +156,15 @@ public class CLIPrompterTest {
@Override @Override
public void run() { public void run() {
try { try {
assertEquals(choices.get(0), CLIPrompter.promptChoice( assertEquals("Asserted provided value to be retrieved",
out, in, keys, choices, message, cancel)); choices.get(0), CLIPrompter.promptChoice(out,
assertEquals(choices.get(0), CLIPrompter.promptChoice( in, keys, choices, message, cancel));
out, in, keys, choices, message, null)); assertEquals("Asserted provided value to be retrieved",
assertEquals(null, CLIPrompter.promptChoice(out, in, choices.get(0), CLIPrompter.promptChoice(out,
keys, choices, message, cancel)); in, keys, choices, message, null));
assertEquals("Asserted provided value to be retrieved",
null, CLIPrompter.promptChoice(out, in, keys,
choices, message, cancel));
} catch (final IOException e) { } catch (final IOException e) {
fail("Unexpected io excpetion"); //$NON-NLS-1$ fail("Unexpected io excpetion"); //$NON-NLS-1$
e.printStackTrace(); e.printStackTrace();
@ -158,7 +177,7 @@ public class CLIPrompterTest {
assertTrue(out.readNextLine().contains(keys.get(1))); assertTrue(out.readNextLine().contains(keys.get(1)));
assertTrue(out.readNextLine().contains(cancel)); assertTrue(out.readNextLine().contains(cancel));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
in.type("yoyo"); //$NON-NLS-1$ in.type("yoyo"); //$NON-NLS-1$
// fail, reprompt // fail, reprompt
final String msg = CLIPrompterMessages.getString( final String msg = CLIPrompterMessages.getString(
@ -171,14 +190,14 @@ public class CLIPrompterTest {
assertTrue(out.readNextLine().contains(keys.get(1))); assertTrue(out.readNextLine().contains(keys.get(1)));
assertTrue(out.readNextLine().contains(cancel)); assertTrue(out.readNextLine().contains(cancel));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
in.type("0"); //$NON-NLS-1$ in.type("0"); //$NON-NLS-1$
// Sucess, reprompt without cancel // Sucess, reprompt without cancel
assertTrue(out.readNextLine().startsWith(message)); assertTrue(out.readNextLine().startsWith(message));
assertTrue(out.readNextLine().contains(keys.get(0))); assertTrue(out.readNextLine().contains(keys.get(0)));
assertTrue(out.readNextLine().contains(keys.get(1))); assertTrue(out.readNextLine().contains(keys.get(1)));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
in.type("2"); //$NON-NLS-1$ in.type("2"); //$NON-NLS-1$
// fail, reprompt // fail, reprompt
assertEquals( assertEquals(
@ -188,7 +207,7 @@ public class CLIPrompterTest {
assertTrue(out.readNextLine().contains(keys.get(0))); assertTrue(out.readNextLine().contains(keys.get(0)));
assertTrue(out.readNextLine().contains(keys.get(1))); assertTrue(out.readNextLine().contains(keys.get(1)));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
in.type("0"); //$NON-NLS-1$ in.type("0"); //$NON-NLS-1$
// Sucess, prompt with cancel // Sucess, prompt with cancel
assertTrue(out.readNextLine().startsWith(message)); assertTrue(out.readNextLine().startsWith(message));
@ -196,7 +215,7 @@ public class CLIPrompterTest {
assertTrue(out.readNextLine().contains(keys.get(1))); assertTrue(out.readNextLine().contains(keys.get(1)));
assertTrue(out.readNextLine().contains(cancel)); assertTrue(out.readNextLine().contains(cancel));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
in.type("2"); //$NON-NLS-1$ in.type("2"); //$NON-NLS-1$
th.join(); th.join();
} catch (IOException | InterruptedException e) { } catch (IOException | InterruptedException e) {
@ -208,7 +227,12 @@ public class CLIPrompterTest {
@Test @Test
public final void testPromptChoiceConsoleManagerListOfUMapOfUTStringString() { public final void testPromptChoiceConsoleManagerListOfUMapOfUTStringString() {
try (final PipedConsoleOutput out = new PipedConsoleOutput(); 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<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$
@ -223,14 +247,17 @@ public class CLIPrompterTest {
@Override @Override
public void run() { public void run() {
try { try {
assertEquals(choices.get(keys.get(0)), assertEquals("Asserted provided value to be retrieved",
CLIPrompter.promptChoice(out, in, keys, choices.get(keys.get(0)),
CLIPrompter.promptChoice(out, in, keys, choices,
message, cancel));
assertEquals("Asserted provided value to be retrieved",
choices.get(keys.get(0)),
CLIPrompter.promptChoice(out, in, keys, choices,
message, null));
assertEquals("Asserted provided value to be retrieved",
null, CLIPrompter.promptChoice(out, in, keys,
choices, message, cancel)); choices, message, cancel));
assertEquals(choices.get(keys.get(0)),
CLIPrompter.promptChoice(out, in, keys,
choices, message, null));
assertEquals(null, CLIPrompter.promptChoice(out, in,
keys, choices, message, cancel));
} catch (final IOException e) { } catch (final IOException e) {
fail("Unexpected io excpetion"); //$NON-NLS-1$ fail("Unexpected io excpetion"); //$NON-NLS-1$
e.printStackTrace(); e.printStackTrace();
@ -243,7 +270,7 @@ public class CLIPrompterTest {
assertTrue(out.readNextLine().contains(keys.get(1).toString())); assertTrue(out.readNextLine().contains(keys.get(1).toString()));
assertTrue(out.readNextLine().contains(cancel)); assertTrue(out.readNextLine().contains(cancel));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
in.type("yoyo"); //$NON-NLS-1$ in.type("yoyo"); //$NON-NLS-1$
// fail, reprompt // fail, reprompt
final String msg = CLIPrompterMessages.getString( final String msg = CLIPrompterMessages.getString(
@ -256,14 +283,14 @@ public class CLIPrompterTest {
assertTrue(out.readNextLine().contains(keys.get(1).toString())); assertTrue(out.readNextLine().contains(keys.get(1).toString()));
assertTrue(out.readNextLine().contains(cancel)); assertTrue(out.readNextLine().contains(cancel));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
in.type("0"); //$NON-NLS-1$ in.type("0"); //$NON-NLS-1$
// Sucess, reprompt without cancel // Sucess, reprompt without cancel
assertTrue(out.readNextLine().startsWith(message)); assertTrue(out.readNextLine().startsWith(message));
assertTrue(out.readNextLine().contains(keys.get(0).toString())); assertTrue(out.readNextLine().contains(keys.get(0).toString()));
assertTrue(out.readNextLine().contains(keys.get(1).toString())); assertTrue(out.readNextLine().contains(keys.get(1).toString()));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
in.type("2"); //$NON-NLS-1$ in.type("2"); //$NON-NLS-1$
// fail, reprompt // fail, reprompt
assertEquals( assertEquals(
@ -273,7 +300,7 @@ public class CLIPrompterTest {
assertTrue(out.readNextLine().contains(keys.get(0).toString())); assertTrue(out.readNextLine().contains(keys.get(0).toString()));
assertTrue(out.readNextLine().contains(keys.get(1).toString())); assertTrue(out.readNextLine().contains(keys.get(1).toString()));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
in.type("0"); //$NON-NLS-1$ in.type("0"); //$NON-NLS-1$
// Sucess, prompt with cancel // Sucess, prompt with cancel
assertTrue(out.readNextLine().startsWith(message)); assertTrue(out.readNextLine().startsWith(message));
@ -281,7 +308,7 @@ public class CLIPrompterTest {
assertTrue(out.readNextLine().contains(keys.get(1).toString())); assertTrue(out.readNextLine().contains(keys.get(1).toString()));
assertTrue(out.readNextLine().contains(cancel)); assertTrue(out.readNextLine().contains(cancel));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
in.type("2"); //$NON-NLS-1$ in.type("2"); //$NON-NLS-1$
th.join(); th.join();
} catch (IOException | InterruptedException e) { } catch (IOException | InterruptedException e) {
@ -293,7 +320,12 @@ public class CLIPrompterTest {
@Test @Test
public final void testPromptChoiceConsoleManagerListOfUStringString() { public final void testPromptChoiceConsoleManagerListOfUStringString() {
try (final PipedConsoleOutput out = new PipedConsoleOutput(); 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<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$
@ -305,15 +337,18 @@ public class CLIPrompterTest {
@Override @Override
public void run() { public void run() {
try { try {
assertEquals(Integer.valueOf(0), assertEquals("Asserted provided value to be retrieved",
CLIPrompter.promptChoice(out, in, keys, Integer.valueOf(0), CLIPrompter.promptChoice(
out, in, keys, message, cancel));
assertEquals("Asserted provided value to be retrieved",
Integer.valueOf(0), CLIPrompter.promptChoice(
out, in, keys, message, null));
assertEquals("Asserted provided value to be retrieved",
Integer.valueOf(1), CLIPrompter.promptChoice(
out, in, keys, message, null));
assertEquals("Asserted provided value to be retrieved",
null, CLIPrompter.promptChoice(out, in, keys,
message, cancel)); message, cancel));
assertEquals(Integer.valueOf(0), CLIPrompter
.promptChoice(out, in, keys, message, null));
assertEquals(Integer.valueOf(1), CLIPrompter
.promptChoice(out, in, keys, message, null));
assertEquals(null, CLIPrompter.promptChoice(out, in,
keys, message, cancel));
} catch (final IOException e) { } catch (final IOException e) {
fail("Unexpected io excpetion"); //$NON-NLS-1$ fail("Unexpected io excpetion"); //$NON-NLS-1$
e.printStackTrace(); e.printStackTrace();
@ -326,7 +361,7 @@ public class CLIPrompterTest {
assertTrue(out.readNextLine().contains(keys.get(1).toString())); assertTrue(out.readNextLine().contains(keys.get(1).toString()));
assertTrue(out.readNextLine().contains(cancel)); assertTrue(out.readNextLine().contains(cancel));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
in.type("yoyo"); //$NON-NLS-1$ in.type("yoyo"); //$NON-NLS-1$
// fail, reprompt // fail, reprompt
final String msg = CLIPrompterMessages.getString( final String msg = CLIPrompterMessages.getString(
@ -339,14 +374,14 @@ public class CLIPrompterTest {
assertTrue(out.readNextLine().contains(keys.get(1).toString())); assertTrue(out.readNextLine().contains(keys.get(1).toString()));
assertTrue(out.readNextLine().contains(cancel)); assertTrue(out.readNextLine().contains(cancel));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
in.type("0"); //$NON-NLS-1$ in.type("0"); //$NON-NLS-1$
// Sucess, reprompt without cancel // Sucess, reprompt without cancel
assertTrue(out.readNextLine().startsWith(message)); assertTrue(out.readNextLine().startsWith(message));
assertTrue(out.readNextLine().contains(keys.get(0).toString())); assertTrue(out.readNextLine().contains(keys.get(0).toString()));
assertTrue(out.readNextLine().contains(keys.get(1).toString())); assertTrue(out.readNextLine().contains(keys.get(1).toString()));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
in.type("2"); //$NON-NLS-1$ in.type("2"); //$NON-NLS-1$
// fail, reprompt // fail, reprompt
assertEquals( assertEquals(
@ -356,14 +391,14 @@ public class CLIPrompterTest {
assertTrue(out.readNextLine().contains(keys.get(0).toString())); assertTrue(out.readNextLine().contains(keys.get(0).toString()));
assertTrue(out.readNextLine().contains(keys.get(1).toString())); assertTrue(out.readNextLine().contains(keys.get(1).toString()));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
in.type("0"); //$NON-NLS-1$ in.type("0"); //$NON-NLS-1$
// Success do it again // Success do it again
assertTrue(out.readNextLine().startsWith(message)); assertTrue(out.readNextLine().startsWith(message));
assertTrue(out.readNextLine().contains(keys.get(0).toString())); assertTrue(out.readNextLine().contains(keys.get(0).toString()));
assertTrue(out.readNextLine().contains(keys.get(1).toString())); assertTrue(out.readNextLine().contains(keys.get(1).toString()));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
in.type("1"); //$NON-NLS-1$ in.type("1"); //$NON-NLS-1$
// Sucess, prompt with cancel // Sucess, prompt with cancel
assertTrue(out.readNextLine().startsWith(message)); assertTrue(out.readNextLine().startsWith(message));
@ -371,7 +406,7 @@ public class CLIPrompterTest {
assertTrue(out.readNextLine().contains(keys.get(1).toString())); assertTrue(out.readNextLine().contains(keys.get(1).toString()));
assertTrue(out.readNextLine().contains(cancel)); assertTrue(out.readNextLine().contains(cancel));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
in.type("2"); //$NON-NLS-1$ in.type("2"); //$NON-NLS-1$
th.join(); th.join();
} catch (IOException | InterruptedException e) { } catch (IOException | InterruptedException e) {
@ -385,7 +420,12 @@ public class CLIPrompterTest {
@Test @Test
public final void testPromptChoiceConsoleManagerMapOfUTStringString() { public final void testPromptChoiceConsoleManagerMapOfUTStringString() {
try (final PipedConsoleOutput out = new PipedConsoleOutput(); 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<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$
@ -400,14 +440,17 @@ public class CLIPrompterTest {
@Override @Override
public void run() { public void run() {
try { try {
assertEquals(choices.get(keys.get(0)), assertEquals("Asserted provided value to be retrieved",
CLIPrompter.promptChoice(out, in, keys, choices.get(keys.get(0)),
CLIPrompter.promptChoice(out, in, keys, choices,
message, cancel));
assertEquals("Asserted provided value to be retrieved",
choices.get(keys.get(0)),
CLIPrompter.promptChoice(out, in, keys, choices,
message, null));
assertEquals("Asserted provided value to be retrieved",
null, CLIPrompter.promptChoice(out, in, keys,
choices, message, cancel)); choices, message, cancel));
assertEquals(choices.get(keys.get(0)),
CLIPrompter.promptChoice(out, in, keys,
choices, message, null));
assertEquals(null, CLIPrompter.promptChoice(out, in,
keys, choices, message, cancel));
} catch (final IOException e) { } catch (final IOException e) {
fail("Unexpected io excpetion"); //$NON-NLS-1$ fail("Unexpected io excpetion"); //$NON-NLS-1$
e.printStackTrace(); e.printStackTrace();
@ -422,7 +465,7 @@ public class CLIPrompterTest {
assertTrue(out.readNextLine().contains(keys.get(1).toString())); assertTrue(out.readNextLine().contains(keys.get(1).toString()));
assertTrue(out.readNextLine().contains(cancel)); assertTrue(out.readNextLine().contains(cancel));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
in.type("yoyo"); //$NON-NLS-1$ in.type("yoyo"); //$NON-NLS-1$
// fail, reprompt // fail, reprompt
final String msg = CLIPrompterMessages.getString( final String msg = CLIPrompterMessages.getString(
@ -435,14 +478,14 @@ public class CLIPrompterTest {
assertTrue(out.readNextLine().contains(keys.get(1).toString())); assertTrue(out.readNextLine().contains(keys.get(1).toString()));
assertTrue(out.readNextLine().contains(cancel)); assertTrue(out.readNextLine().contains(cancel));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
in.type("0"); //$NON-NLS-1$ in.type("0"); //$NON-NLS-1$
// Sucess, reprompt without cancel // Sucess, reprompt without cancel
assertTrue(out.readNextLine().startsWith(message)); assertTrue(out.readNextLine().startsWith(message));
assertTrue(out.readNextLine().contains(keys.get(0).toString())); assertTrue(out.readNextLine().contains(keys.get(0).toString()));
assertTrue(out.readNextLine().contains(keys.get(1).toString())); assertTrue(out.readNextLine().contains(keys.get(1).toString()));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
in.type("2"); //$NON-NLS-1$ in.type("2"); //$NON-NLS-1$
// fail, reprompt // fail, reprompt
assertEquals( assertEquals(
@ -452,7 +495,7 @@ public class CLIPrompterTest {
assertTrue(out.readNextLine().contains(keys.get(0).toString())); assertTrue(out.readNextLine().contains(keys.get(0).toString()));
assertTrue(out.readNextLine().contains(keys.get(1).toString())); assertTrue(out.readNextLine().contains(keys.get(1).toString()));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
in.type("0"); //$NON-NLS-1$ in.type("0"); //$NON-NLS-1$
// Sucess, prompt with cancel // Sucess, prompt with cancel
assertTrue(out.readNextLine().startsWith(message)); assertTrue(out.readNextLine().startsWith(message));
@ -460,7 +503,7 @@ public class CLIPrompterTest {
assertTrue(out.readNextLine().contains(keys.get(1).toString())); assertTrue(out.readNextLine().contains(keys.get(1).toString()));
assertTrue(out.readNextLine().contains(cancel)); assertTrue(out.readNextLine().contains(cancel));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
in.type("2"); //$NON-NLS-1$ in.type("2"); //$NON-NLS-1$
th.join(); th.join();
} catch (IOException | InterruptedException e) { } catch (IOException | InterruptedException e) {
@ -473,16 +516,23 @@ 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 PipedConsoleInput test = new PipedConsoleInput()) { 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 Thread th = new Thread(new Runnable() { final Thread th = new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
assertEquals(10, assertEquals("Asserted provided value to be retrieved",
CLIPrompter.promptInteger(test, "My message")); //$NON-NLS-1$ 10,
assertEquals(-15, CLIPrompter.promptInteger(in, "My message")); //$NON-NLS-1$
CLIPrompter.promptInteger(test, "My message")); //$NON-NLS-1$ assertEquals("Asserted provided value to be retrieved",
-15,
CLIPrompter.promptInteger(in, "My message")); //$NON-NLS-1$
} catch (final IOException e) { } catch (final IOException e) {
fail("Unexpected io excpetion"); //$NON-NLS-1$ fail("Unexpected io excpetion"); //$NON-NLS-1$
e.printStackTrace(); e.printStackTrace();
@ -490,14 +540,14 @@ public class CLIPrompterTest {
} }
}); });
th.start(); th.start();
assertTrue(test.readNextLine().startsWith("My message")); //$NON-NLS-1$ assertTrue(buf.readLine().startsWith("My message")); //$NON-NLS-1$
test.type(""); //$NON-NLS-1$ in.type(""); //$NON-NLS-1$
assertTrue(test.readNextLine().startsWith("My message")); //$NON-NLS-1$ assertTrue(buf.readLine().startsWith("My message")); //$NON-NLS-1$
test.type("Y"); //$NON-NLS-1$ in.type("Y"); //$NON-NLS-1$
assertTrue(test.readNextLine().startsWith("My message")); //$NON-NLS-1$ assertTrue(buf.readLine().startsWith("My message")); //$NON-NLS-1$
test.type("10"); //$NON-NLS-1$ in.type("10"); //$NON-NLS-1$
assertTrue(test.readNextLine().startsWith("My message")); //$NON-NLS-1$ assertTrue(buf.readLine().startsWith("My message")); //$NON-NLS-1$
test.type("-15"); //$NON-NLS-1$ in.type("-15"); //$NON-NLS-1$
th.join(); th.join();
} catch (IOException | InterruptedException e) { } catch (IOException | InterruptedException e) {
@ -511,7 +561,12 @@ public class CLIPrompterTest {
@Test @Test
public final void testPromptListConsoleManagerString() { public final void testPromptListConsoleManagerString() {
try (final PipedConsoleOutput out = new PipedConsoleOutput(); 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<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$
@ -522,10 +577,11 @@ public class CLIPrompterTest {
@Override @Override
public void run() { public void run() {
try { try {
assertEquals(new ArrayList<String>(), assertEquals("Asserted provided value to be retrieved",
CLIPrompter.promptList(out, in, message)); new ArrayList<String>(),
assertEquals(keys,
CLIPrompter.promptList(out, in, message)); CLIPrompter.promptList(out, in, message));
assertEquals("Asserted provided value to be retrieved",
keys, CLIPrompter.promptList(out, in, message));
} catch (final IOException e) { } catch (final IOException e) {
fail("Unexpected io excpetion"); //$NON-NLS-1$ fail("Unexpected io excpetion"); //$NON-NLS-1$
e.printStackTrace(); e.printStackTrace();
@ -539,7 +595,7 @@ public class CLIPrompterTest {
.getString("promptlist.exit.dispkey", CLIPrompterMessages //$NON-NLS-1$ .getString("promptlist.exit.dispkey", CLIPrompterMessages //$NON-NLS-1$
.getString("promptlist.exit.defaultkey")))); //$NON-NLS-1$ .getString("promptlist.exit.defaultkey")))); //$NON-NLS-1$
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
in.type(CLIPrompterMessages in.type(CLIPrompterMessages
.getString("promptlist.exit.defaultkey")); //$NON-NLS-1$ .getString("promptlist.exit.defaultkey")); //$NON-NLS-1$
// enter keys list // enter keys list
@ -549,11 +605,11 @@ public class CLIPrompterTest {
.getString("promptlist.exit.dispkey", CLIPrompterMessages //$NON-NLS-1$ .getString("promptlist.exit.dispkey", CLIPrompterMessages //$NON-NLS-1$
.getString("promptlist.exit.defaultkey")))); //$NON-NLS-1$ .getString("promptlist.exit.defaultkey")))); //$NON-NLS-1$
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
for (int i = 0; i < keys.size(); i++) { for (int i = 0; i < keys.size(); i++) {
in.type(keys.get(i)); in.type(keys.get(i));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
} }
in.type(CLIPrompterMessages in.type(CLIPrompterMessages
.getString("promptlist.exit.defaultkey")); //$NON-NLS-1$ .getString("promptlist.exit.defaultkey")); //$NON-NLS-1$
@ -569,7 +625,12 @@ public class CLIPrompterTest {
@Test @Test
public final void testPromptListConsoleManagerStringString() { public final void testPromptListConsoleManagerStringString() {
try (final PipedConsoleOutput out = new PipedConsoleOutput(); 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<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$
@ -581,10 +642,12 @@ public class CLIPrompterTest {
@Override @Override
public void run() { public void run() {
try { try {
assertEquals(new ArrayList<String>(), CLIPrompter assertEquals("Asserted provided value to be retrieved",
.promptList(out, in, message, ender)); new ArrayList<String>(), CLIPrompter
assertEquals(keys, CLIPrompter.promptList(out, in, .promptList(out, in, message, ender));
message, ender)); assertEquals("Asserted provided value to be retrieved",
keys, CLIPrompter.promptList(out, in, message,
ender));
} catch (final IOException e) { } catch (final IOException e) {
fail("Unexpected io excpetion"); //$NON-NLS-1$ fail("Unexpected io excpetion"); //$NON-NLS-1$
e.printStackTrace(); e.printStackTrace();
@ -597,7 +660,7 @@ public class CLIPrompterTest {
assertTrue(nLine.endsWith(CLIPrompterMessages assertTrue(nLine.endsWith(CLIPrompterMessages
.getString("promptlist.exit.dispkey", ender))); //$NON-NLS-1$ .getString("promptlist.exit.dispkey", ender))); //$NON-NLS-1$
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
in.type(ender); in.type(ender);
// enter keys list // enter keys list
nLine = out.readNextLine(); nLine = out.readNextLine();
@ -605,11 +668,11 @@ public class CLIPrompterTest {
assertTrue(nLine.endsWith(CLIPrompterMessages assertTrue(nLine.endsWith(CLIPrompterMessages
.getString("promptlist.exit.dispkey", ender))); //$NON-NLS-1$ .getString("promptlist.exit.dispkey", ender))); //$NON-NLS-1$
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
for (int i = 0; i < keys.size(); i++) { for (int i = 0; i < keys.size(); i++) {
in.type(keys.get(i)); in.type(keys.get(i));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
} }
in.type(ender); in.type(ender);
th.join(); th.join();
@ -624,7 +687,12 @@ public class CLIPrompterTest {
@Test @Test
public final void testPromptLongTextConsoleManagerString() { public final void testPromptLongTextConsoleManagerString() {
try (final PipedConsoleOutput out = new PipedConsoleOutput(); 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 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" + "line feeds and other" +
@ -637,11 +705,12 @@ public class CLIPrompterTest {
@Override @Override
public void run() { public void run() {
try { try {
assertEquals("", CLIPrompter.promptLongText(out, in, assertEquals("Asserted provided value to be retrieved",
message)); "",
assertEquals(longText + System.lineSeparator(), CLIPrompter.promptLongText(out, in, message));
CLIPrompter.promptLongText(out, in, assertEquals("Asserted provided value to be retrieved",
message)); longText + System.lineSeparator(),
CLIPrompter.promptLongText(out, in, message));
} catch (final IOException e) { } catch (final IOException e) {
fail("Unexpected io excpetion"); //$NON-NLS-1$ fail("Unexpected io excpetion"); //$NON-NLS-1$
e.printStackTrace(); e.printStackTrace();
@ -655,7 +724,7 @@ public class CLIPrompterTest {
"promptlongtext.exit.dispkey", CLIPrompterMessages "promptlongtext.exit.dispkey", CLIPrompterMessages
.getString("promptlongtext.exit.defaultkey")))); .getString("promptlongtext.exit.defaultkey"))));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"),
in.readNextLine()); buf.readLine());
in.type(CLIPrompterMessages in.type(CLIPrompterMessages
.getString("promptlongtext.exit.defaultkey")); .getString("promptlongtext.exit.defaultkey"));
// enter long text // enter long text
@ -665,12 +734,12 @@ public class CLIPrompterTest {
"promptlongtext.exit.dispkey", CLIPrompterMessages "promptlongtext.exit.dispkey", CLIPrompterMessages
.getString("promptlongtext.exit.defaultkey")))); .getString("promptlongtext.exit.defaultkey"))));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"),
in.readNextLine()); buf.readLine());
final String[] text = longText.split(System.lineSeparator()); final String[] text = longText.split(System.lineSeparator());
for (final String element : text) { for (final String element : text) {
in.type(element); in.type(element);
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"),
in.readNextLine()); buf.readLine());
} }
in.type(CLIPrompterMessages in.type(CLIPrompterMessages
.getString("promptlongtext.exit.defaultkey")); .getString("promptlongtext.exit.defaultkey"));
@ -686,7 +755,12 @@ public class CLIPrompterTest {
@Test @Test
public final void testPromptLongTextConsoleManagerStringString() { public final void testPromptLongTextConsoleManagerStringString() {
try (final PipedConsoleOutput out = new PipedConsoleOutput(); 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 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",
@ -699,10 +773,11 @@ public class CLIPrompterTest {
@Override @Override
public void run() { public void run() {
try { try {
assertEquals("", //$NON-NLS-1$ assertEquals("Asserted provided value to be retrieved", //$NON-NLS-1$
CLIPrompter.promptLongText(out, in, message, "", CLIPrompter.promptLongText(out, in, message,
ender)); ender));
assertEquals(longText + System.lineSeparator(), assertEquals("Asserted provided value to be retrieved",
longText + System.lineSeparator(),
CLIPrompter.promptLongText(out, in, message, CLIPrompter.promptLongText(out, in, message,
ender)); ender));
} catch (final IOException e) { } catch (final IOException e) {
@ -717,7 +792,7 @@ public class CLIPrompterTest {
assertTrue(nLine.endsWith(CLIPrompterMessages assertTrue(nLine.endsWith(CLIPrompterMessages
.getString("promptlongtext.exit.dispkey", ender))); .getString("promptlongtext.exit.dispkey", ender)));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"),
in.readNextLine()); buf.readLine());
in.type(ender); in.type(ender);
// enter long text // enter long text
nLine = out.readNextLine(); nLine = out.readNextLine();
@ -725,11 +800,11 @@ public class CLIPrompterTest {
assertTrue(nLine.endsWith(CLIPrompterMessages assertTrue(nLine.endsWith(CLIPrompterMessages
.getString("promptlongtext.exit.dispkey", ender))); .getString("promptlongtext.exit.dispkey", ender)));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"),
in.readNextLine()); buf.readLine());
for (final String element : text) { for (final String element : text) {
in.type(element); in.type(element);
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"),
in.readNextLine()); buf.readLine());
} }
in.type(ender); in.type(ender);
th.join(); th.join();
@ -744,7 +819,12 @@ public class CLIPrompterTest {
@Test @Test
public final void testPromptMultiChoiceConsoleManagerListOfStringListOfUString() { public final void testPromptMultiChoiceConsoleManagerListOfStringListOfUString() {
try (final PipedConsoleOutput out = new PipedConsoleOutput(); 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<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$
@ -758,15 +838,18 @@ public class CLIPrompterTest {
@Override @Override
public void run() { public void run() {
try { try {
assertTrue(CLIPrompter.promptMultiChoice(out, in, assertTrue("Asserted provided value to be retrieved",
keys, choices, message).isEmpty()); CLIPrompter.promptMultiChoice(out, in, keys,
choices, message).isEmpty());
final ArrayList<Object> l = new ArrayList<>(); final ArrayList<Object> l = new ArrayList<>();
l.add(choices.get(0)); l.add(choices.get(0));
assertEquals(l, CLIPrompter.promptMultiChoice(out, in, assertEquals("Asserted provided value to be retrieved",
keys, choices, message)); l, CLIPrompter.promptMultiChoice(out, in, keys,
choices, message));
l.add(choices.get(1)); l.add(choices.get(1));
assertEquals(l, CLIPrompter.promptMultiChoice(out, in, assertEquals("Asserted provided value to be retrieved",
keys, choices, message)); l, CLIPrompter.promptMultiChoice(out, in, keys,
choices, message));
} catch (final IOException e) { } catch (final IOException e) {
fail("Unexpected io excpetion"); //$NON-NLS-1$ fail("Unexpected io excpetion"); //$NON-NLS-1$
e.printStackTrace(); e.printStackTrace();
@ -778,7 +861,7 @@ public class CLIPrompterTest {
assertTrue(out.readNextLine().contains(keys.get(0))); assertTrue(out.readNextLine().contains(keys.get(0)));
assertTrue(out.readNextLine().contains(keys.get(1))); assertTrue(out.readNextLine().contains(keys.get(1)));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
in.type("yoyo"); //$NON-NLS-1$ in.type("yoyo"); //$NON-NLS-1$
// fail, reprompt // fail, reprompt
final String msg = CLIPrompterMessages.getString( final String msg = CLIPrompterMessages.getString(
@ -790,14 +873,14 @@ public class CLIPrompterTest {
assertTrue(out.readNextLine().contains(keys.get(0))); assertTrue(out.readNextLine().contains(keys.get(0)));
assertTrue(out.readNextLine().contains(keys.get(1))); assertTrue(out.readNextLine().contains(keys.get(1)));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
in.type(""); //$NON-NLS-1$ in.type(""); //$NON-NLS-1$
// Sucess, reprompt without cancel // Sucess, reprompt without cancel
assertTrue(out.readNextLine().startsWith(message)); assertTrue(out.readNextLine().startsWith(message));
assertTrue(out.readNextLine().contains(keys.get(0))); assertTrue(out.readNextLine().contains(keys.get(0)));
assertTrue(out.readNextLine().contains(keys.get(1))); assertTrue(out.readNextLine().contains(keys.get(1)));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
in.type("2"); //$NON-NLS-1$ in.type("2"); //$NON-NLS-1$
// fail, reprompt // fail, reprompt
assertEquals( assertEquals(
@ -807,14 +890,14 @@ public class CLIPrompterTest {
assertTrue(out.readNextLine().contains(keys.get(0))); assertTrue(out.readNextLine().contains(keys.get(0)));
assertTrue(out.readNextLine().contains(keys.get(1))); assertTrue(out.readNextLine().contains(keys.get(1)));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
in.type("0"); //$NON-NLS-1$ in.type("0"); //$NON-NLS-1$
// Sucess, prompt with cancel // Sucess, prompt with cancel
assertTrue(out.readNextLine().startsWith(message)); assertTrue(out.readNextLine().startsWith(message));
assertTrue(out.readNextLine().contains(keys.get(0))); assertTrue(out.readNextLine().contains(keys.get(0)));
assertTrue(out.readNextLine().contains(keys.get(1))); assertTrue(out.readNextLine().contains(keys.get(1)));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
in.type("0 1"); //$NON-NLS-1$ in.type("0 1"); //$NON-NLS-1$
th.join(); th.join();
} catch (IOException | InterruptedException e) { } catch (IOException | InterruptedException e) {
@ -828,7 +911,12 @@ public class CLIPrompterTest {
@Test @Test
public final void testPromptMultiChoiceConsoleManagerListOfUMapOfUTString() { public final void testPromptMultiChoiceConsoleManagerListOfUMapOfUTString() {
try (final PipedConsoleOutput out = new PipedConsoleOutput(); 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<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$
@ -842,15 +930,18 @@ public class CLIPrompterTest {
@Override @Override
public void run() { public void run() {
try { try {
assertTrue(CLIPrompter.promptMultiChoice(out, in, assertTrue("Asserted provided value to be retrieved",
keys, choices, message).isEmpty()); CLIPrompter.promptMultiChoice(out, in, keys,
choices, message).isEmpty());
final ArrayList<Object> l = new ArrayList<>(); final ArrayList<Object> l = new ArrayList<>();
l.add(choices.get(keys.get(0))); l.add(choices.get(keys.get(0)));
assertEquals(l, CLIPrompter.promptMultiChoice(out, in, assertEquals("Asserted provided value to be retrieved",
keys, choices, message)); l, CLIPrompter.promptMultiChoice(out, in, keys,
choices, message));
l.add(choices.get(keys.get(1))); l.add(choices.get(keys.get(1)));
assertEquals(l, CLIPrompter.promptMultiChoice(out, in, assertEquals("Asserted provided value to be retrieved",
keys, choices, message)); l, CLIPrompter.promptMultiChoice(out, in, keys,
choices, message));
} catch (final IOException e) { } catch (final IOException e) {
fail("Unexpected io excpetion"); //$NON-NLS-1$ fail("Unexpected io excpetion"); //$NON-NLS-1$
e.printStackTrace(); e.printStackTrace();
@ -862,7 +953,7 @@ public class CLIPrompterTest {
assertTrue(out.readNextLine().contains(keys.get(0).toString())); assertTrue(out.readNextLine().contains(keys.get(0).toString()));
assertTrue(out.readNextLine().contains(keys.get(1).toString())); assertTrue(out.readNextLine().contains(keys.get(1).toString()));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
in.type("yoyo"); //$NON-NLS-1$ in.type("yoyo"); //$NON-NLS-1$
// fail, reprompt // fail, reprompt
final String msg = CLIPrompterMessages.getString( final String msg = CLIPrompterMessages.getString(
@ -874,14 +965,14 @@ public class CLIPrompterTest {
assertTrue(out.readNextLine().contains(keys.get(0).toString())); assertTrue(out.readNextLine().contains(keys.get(0).toString()));
assertTrue(out.readNextLine().contains(keys.get(1).toString())); assertTrue(out.readNextLine().contains(keys.get(1).toString()));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
in.type(""); //$NON-NLS-1$ in.type(""); //$NON-NLS-1$
// Sucess, reprompt without cancel // Sucess, reprompt without cancel
assertTrue(out.readNextLine().startsWith(message)); assertTrue(out.readNextLine().startsWith(message));
assertTrue(out.readNextLine().contains(keys.get(0).toString())); assertTrue(out.readNextLine().contains(keys.get(0).toString()));
assertTrue(out.readNextLine().contains(keys.get(1).toString())); assertTrue(out.readNextLine().contains(keys.get(1).toString()));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
in.type("2"); //$NON-NLS-1$ in.type("2"); //$NON-NLS-1$
// fail, reprompt // fail, reprompt
assertEquals( assertEquals(
@ -891,14 +982,14 @@ public class CLIPrompterTest {
assertTrue(out.readNextLine().contains(keys.get(0).toString())); assertTrue(out.readNextLine().contains(keys.get(0).toString()));
assertTrue(out.readNextLine().contains(keys.get(1).toString())); assertTrue(out.readNextLine().contains(keys.get(1).toString()));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
in.type("0"); //$NON-NLS-1$ in.type("0"); //$NON-NLS-1$
// Sucess, prompt with cancel // Sucess, prompt with cancel
assertTrue(out.readNextLine().startsWith(message)); assertTrue(out.readNextLine().startsWith(message));
assertTrue(out.readNextLine().contains(keys.get(0).toString())); assertTrue(out.readNextLine().contains(keys.get(0).toString()));
assertTrue(out.readNextLine().contains(keys.get(1).toString())); assertTrue(out.readNextLine().contains(keys.get(1).toString()));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
in.type("0 1"); //$NON-NLS-1$ in.type("0 1"); //$NON-NLS-1$
th.join(); th.join();
} catch (IOException | InterruptedException e) { } catch (IOException | InterruptedException e) {
@ -912,7 +1003,12 @@ public class CLIPrompterTest {
@Test @Test
public final void testPromptMultiChoiceConsoleManagerListOfUString() { public final void testPromptMultiChoiceConsoleManagerListOfUString() {
try (final PipedConsoleOutput out = new PipedConsoleOutput(); 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<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$
@ -923,16 +1019,18 @@ public class CLIPrompterTest {
@Override @Override
public void run() { public void run() {
try { try {
assertTrue(CLIPrompter assertTrue("Asserted provided value to be retrieved",
.promptMultiChoice(out, in, keys, message) CLIPrompter.promptMultiChoice(out, in, keys,
.isEmpty()); message).isEmpty());
final ArrayList<Integer> l = new ArrayList<>(); final ArrayList<Integer> l = new ArrayList<>();
l.add(0); l.add(0);
assertEquals(l, CLIPrompter.promptMultiChoice(out, in, assertEquals("Asserted provided value to be retrieved",
keys, message)); l, CLIPrompter.promptMultiChoice(out, in, keys,
message));
l.add(1); l.add(1);
assertEquals(l, CLIPrompter.promptMultiChoice(out, in, assertEquals("Asserted provided value to be retrieved",
keys, message)); l, CLIPrompter.promptMultiChoice(out, in, keys,
message));
} catch (final IOException e) { } catch (final IOException e) {
fail("Unexpected io excpetion"); //$NON-NLS-1$ fail("Unexpected io excpetion"); //$NON-NLS-1$
e.printStackTrace(); e.printStackTrace();
@ -944,7 +1042,7 @@ public class CLIPrompterTest {
assertTrue(out.readNextLine().contains(keys.get(0).toString())); assertTrue(out.readNextLine().contains(keys.get(0).toString()));
assertTrue(out.readNextLine().contains(keys.get(1).toString())); assertTrue(out.readNextLine().contains(keys.get(1).toString()));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
in.type("yoyo"); //$NON-NLS-1$ in.type("yoyo"); //$NON-NLS-1$
// fail, reprompt // fail, reprompt
final String msg = CLIPrompterMessages.getString( final String msg = CLIPrompterMessages.getString(
@ -956,14 +1054,14 @@ public class CLIPrompterTest {
assertTrue(out.readNextLine().contains(keys.get(0).toString())); assertTrue(out.readNextLine().contains(keys.get(0).toString()));
assertTrue(out.readNextLine().contains(keys.get(1).toString())); assertTrue(out.readNextLine().contains(keys.get(1).toString()));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
in.type(""); //$NON-NLS-1$ in.type(""); //$NON-NLS-1$
// Sucess, reprompt without cancel // Sucess, reprompt without cancel
assertTrue(out.readNextLine().startsWith(message)); assertTrue(out.readNextLine().startsWith(message));
assertTrue(out.readNextLine().contains(keys.get(0).toString())); assertTrue(out.readNextLine().contains(keys.get(0).toString()));
assertTrue(out.readNextLine().contains(keys.get(1).toString())); assertTrue(out.readNextLine().contains(keys.get(1).toString()));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
in.type("2"); //$NON-NLS-1$ in.type("2"); //$NON-NLS-1$
// fail, reprompt // fail, reprompt
assertEquals( assertEquals(
@ -973,14 +1071,14 @@ public class CLIPrompterTest {
assertTrue(out.readNextLine().contains(keys.get(0).toString())); assertTrue(out.readNextLine().contains(keys.get(0).toString()));
assertTrue(out.readNextLine().contains(keys.get(1).toString())); assertTrue(out.readNextLine().contains(keys.get(1).toString()));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
in.type("0"); //$NON-NLS-1$ in.type("0"); //$NON-NLS-1$
// Sucess, prompt with cancel // Sucess, prompt with cancel
assertTrue(out.readNextLine().startsWith(message)); assertTrue(out.readNextLine().startsWith(message));
assertTrue(out.readNextLine().contains(keys.get(0).toString())); assertTrue(out.readNextLine().contains(keys.get(0).toString()));
assertTrue(out.readNextLine().contains(keys.get(1).toString())); assertTrue(out.readNextLine().contains(keys.get(1).toString()));
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$ assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
in.readNextLine()); buf.readLine());
in.type("0 1"); //$NON-NLS-1$ in.type("0 1"); //$NON-NLS-1$
th.join(); th.join();
} catch (IOException | InterruptedException e) { } catch (IOException | InterruptedException e) {
@ -994,7 +1092,12 @@ public class CLIPrompterTest {
@Test @Test
public final void testPromptMultiChoiceConsoleManagerMapOfUTString() { public final void testPromptMultiChoiceConsoleManagerMapOfUTString() {
try (final PipedConsoleOutput out = new PipedConsoleOutput(); 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<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$
@ -1008,15 +1111,18 @@ public class CLIPrompterTest {
@Override @Override
public void run() { public void run() {
try { try {
assertTrue(CLIPrompter.promptMultiChoice(out, in, keys, assertTrue("Asserted provided value to be retrieved",
choices, message).isEmpty()); CLIPrompter.promptMultiChoice(out, in, keys,
choices, message).isEmpty());
final ArrayList<Object> l = new ArrayList<>(); final ArrayList<Object> l = new ArrayList<>();
l.add(choices.get(keys.get(0))); l.add(choices.get(keys.get(0)));
assertEquals(l, CLIPrompter.promptMultiChoice(out, in, assertEquals("Asserted provided value to be retrieved",
keys, choices, message)); l, CLIPrompter.promptMultiChoice(out, in, keys,
choices, message));
l.add(choices.get(keys.get(1))); l.add(choices.get(keys.get(1)));
assertEquals(l, CLIPrompter.promptMultiChoice(out, in, assertEquals("Asserted provided value to be retrieved",
keys, choices, message)); l, CLIPrompter.promptMultiChoice(out, in, keys,
choices, message));
} catch (final IOException e) { } catch (final IOException e) {
fail("Unexpected io excpetion"); //$NON-NLS-1$ fail("Unexpected io excpetion"); //$NON-NLS-1$
e.printStackTrace(); e.printStackTrace();
@ -1069,15 +1175,21 @@ 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 PipedConsoleInput test = new PipedConsoleInput()) { 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 String res = "some content"; //$NON-NLS-1$ final String res = "some content"; //$NON-NLS-1$
final Thread th = new Thread(new Runnable() { final Thread th = new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
assertEquals(res, CLIPrompter.promptNonEmpty(test, assertEquals("Expected provided message to be returned",
"My message", "my reprompt")); //$NON-NLS-1$ //$NON-NLS-2$ res, CLIPrompter.promptNonEmpty(in,
"My message", "my reprompt")); //$NON-NLS-1$ //$NON-NLS-2$
} catch (final IOException e) { } catch (final IOException e) {
fail("Unexpected io excpetion"); //$NON-NLS-1$ fail("Unexpected io excpetion"); //$NON-NLS-1$
e.printStackTrace(); e.printStackTrace();
@ -1085,10 +1197,10 @@ public class CLIPrompterTest {
} }
}); });
th.start(); th.start();
assertTrue(test.readNextLine().startsWith("My message")); //$NON-NLS-1$ assertTrue(buf.readLine().startsWith("My message")); //$NON-NLS-1$
test.type(""); //$NON-NLS-1$ in.type(""); //$NON-NLS-1$
assertTrue(test.readNextLine().startsWith("my reprompt")); //$NON-NLS-1$ assertTrue(buf.readLine().startsWith("my reprompt")); //$NON-NLS-1$
test.type(res); in.type(res);
th.join(); th.join();
} catch (IOException | InterruptedException e) { } catch (IOException | InterruptedException e) {