Change to Supplier. Fix tests

Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
Emmanuel Bigeon 2018-11-25 09:16:38 -05:00
parent ddd818d93b
commit ad79e3ccb6
11 changed files with 280 additions and 439 deletions

View File

@ -49,7 +49,7 @@
<dependency>
<groupId>net.bigeon.test</groupId>
<artifactId>junitmt</artifactId>
<version>1.0.0</version>
<version>1.0.2</version>
<scope>test</scope>
</dependency>
</dependencies>

View File

@ -73,8 +73,7 @@ package net.bigeon.gclc.manager;
*/
import java.io.IOException;
import java.io.InterruptedIOException;
import net.bigeon.gclc.tools.StringProvider;
import java.util.function.Supplier;
/** A console application input.
*
@ -90,7 +89,7 @@ public interface ConsoleInput extends AutoCloseable {
/** Get the prompt string.
*
* @return the prompt prefix */
StringProvider getPrompt();
Supplier<String> getPrompt();
/** Indicate to the input that is should interrompt the prompting, if possible.
* <p>
@ -148,5 +147,5 @@ public interface ConsoleInput extends AutoCloseable {
/** Set the prompting string.
*
* @param string the prompt string */
void setPrompt(StringProvider string);
void setPrompt(Supplier<String> string);
}

View File

@ -34,6 +34,9 @@
*/
package net.bigeon.gclc.tools;
import java.util.Objects;
import java.util.function.Supplier;
/*-
* #%L
* Generic Command Ligne console
@ -68,10 +71,10 @@ package net.bigeon.gclc.tools;
* #L%
*/
/** A {@link StringProvider} that hold a constant string.
/** A supplier of string that hold a constant string.
*
* @author Emmanuel Bigeon */
public class ConstantString implements StringProvider {
public class ConstantString implements Supplier<String> {
private final String string;
/** Create a provider for a string.
@ -81,10 +84,8 @@ public class ConstantString implements StringProvider {
this.string = string;
}
/* (non-Javadoc)
* @see net.bigeon.gclc.tools.StringProvider#apply() */
@Override
public String apply() {
public String get() {
return string;
}
@ -92,10 +93,7 @@ public class ConstantString implements StringProvider {
* @see java.lang.Object#hashCode() */
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((string == null) ? 0 : string.hashCode());
return result;
return Objects.hash(string);
}
/* (non-Javadoc)
@ -112,13 +110,6 @@ public class ConstantString implements StringProvider {
return false;
}
final ConstantString other = (ConstantString) obj;
if (string == null) {
if (other.string != null) {
return false;
}
} else if (!string.equals(other.string)) {
return false;
}
return true;
return Objects.equals(string, other.string);
}
}

View File

@ -1,83 +0,0 @@
/*
* 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.
*/
package net.bigeon.gclc.tools;
/*-
* #%L
* Generic Command Ligne console
* %%
* Copyright (C) 2014 - 2018 bigeon.fr
* %%
* 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.
* #L%
*/
/** A string providing object.
* <p>
* Implementations of this interface will provide a string, this internal state
* of the object may be so that successive calls to the apply method return
* different results.
*
* @author Emmanuel */
public interface StringProvider {
/** Provide a string.
*
* @return the string */
String apply();
}

View File

@ -38,6 +38,8 @@
*/
package net.bigeon.gclc.utils;
import java.util.function.Supplier;
import net.bigeon.gclc.manager.ConsoleInput;
/*-
@ -75,7 +77,6 @@ import net.bigeon.gclc.manager.ConsoleInput;
*/
import net.bigeon.gclc.tools.ConstantString;
import net.bigeon.gclc.tools.StringProvider;
/** A console input that return empty to all prompting.
*
@ -101,8 +102,8 @@ public final class EmptyInput implements ConsoleInput {
/* (non-Javadoc)
* @see net.bigeon.gclc.manager.ConsoleInput#getPrompt() */
@Override
public StringProvider getPrompt() {
return CONSTANT_STRING; //$NON-NLS-1$
public Supplier<String> getPrompt() {
return CONSTANT_STRING;
}
/* (non-Javadoc)
@ -155,7 +156,7 @@ public final class EmptyInput implements ConsoleInput {
}
@Override
public void setPrompt(StringProvider string) {
public void setPrompt(final Supplier<String> string) {
//
}
}

View File

@ -76,9 +76,9 @@ import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.util.function.Supplier;
import net.bigeon.gclc.manager.ConsoleInput;
import net.bigeon.gclc.tools.StringProvider;
/** This console input allows to enter commands and retrieve the output as an
* input.
@ -120,7 +120,7 @@ public final class PipedConsoleInput implements ConsoleInput {
}
@Override
public StringProvider getPrompt() {
public Supplier<String> getPrompt() {
return innerManager.getPrompt();
}
@ -174,7 +174,7 @@ public final class PipedConsoleInput implements ConsoleInput {
}
@Override
public void setPrompt(StringProvider string) {
public void setPrompt(final Supplier<String> string) {
innerManager.setPrompt(string);
}

View File

@ -77,10 +77,10 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.nio.charset.Charset;
import java.util.function.Supplier;
import net.bigeon.gclc.manager.ConsoleInput;
import net.bigeon.gclc.tools.ConstantString;
import net.bigeon.gclc.tools.StringProvider;
/** A console using the input stream and print stream.
* <p>
@ -90,10 +90,10 @@ import net.bigeon.gclc.tools.StringProvider;
public final class StreamConsoleInput implements ConsoleInput {
/** The default prompt. */
public static final StringProvider DEFAULT_PROMPT = new ConstantString("> "); //$NON-NLS-1$
public static final Supplier<String> DEFAULT_PROMPT = new ConstantString("> "); //$NON-NLS-1$
/** The command prompt. It can be changed. */
private StringProvider prompt = DEFAULT_PROMPT;
private Supplier<String> prompt = DEFAULT_PROMPT;
/** The print stream. */
private final PrintStream out;
@ -152,7 +152,7 @@ public final class StreamConsoleInput implements ConsoleInput {
/* (non-Javadoc)
* @see net.bigeon.gclc.manager.ConsoleInput#getPrompt() */
@Override
public StringProvider getPrompt() {
public Supplier<String> getPrompt() {
return prompt;
}
@ -175,14 +175,14 @@ public final class StreamConsoleInput implements ConsoleInput {
* @see net.bigeon.gclc.ConsoleManager#prompt() */
@Override
public String prompt() throws IOException {
return prompt(prompt.apply());
return prompt(prompt.get());
}
/* (non-Javadoc)
* @see net.bigeon.gclc.manager.ConsoleManager#prompt(long) */
@Override
public String prompt(final long timeout) throws IOException {
return prompt(prompt.apply(), timeout);
return prompt(prompt.get(), timeout);
}
/* (non-Javadoc)
@ -217,7 +217,7 @@ public final class StreamConsoleInput implements ConsoleInput {
}
@Override
public void setPrompt(StringProvider string) {
public void setPrompt(final Supplier<String> string) {
prompt = string;
}
}

View File

@ -94,6 +94,8 @@ import net.bigeon.gclc.manager.ConsoleOutput;
import net.bigeon.gclc.utils.PipedConsoleInput;
import net.bigeon.gclc.utils.PipedConsoleOutput;
import net.bigeon.test.junitmt.ATestRunnable;
import net.bigeon.test.junitmt.FunctionalTestRunnable;
import net.bigeon.test.junitmt.TestFunction;
import net.bigeon.test.junitmt.ThreadTest;
/**
@ -511,24 +513,21 @@ public class ParametrizedCommandTest {
cmd.execute(out, in, "-" + str1, str2, "-" + addParam);
cmd.execute(out, in, "-" + str1, str2, "-" + addParam, addParam);
final ATestRunnable testConsole = new ATestRunnable() {
final TestFunction function = new TestFunction() {
@Override
protected void testRun() {
try {
assertEquals("value of " + str1 + "? ", buf.readLine());
in.type("");
assertEquals("value of " + str1 + "? (cannot be empty) ",
buf.readLine());
in.type("");
assertEquals("value of " + str1 + "? (cannot be empty) ",
buf.readLine());
in.type(str2);
} catch (final IOException e) {
fail("IO exception in test");
}
public void apply() throws Exception {
assertEquals("value of " + str1 + "? ", buf.readLine());
in.type("");
assertEquals("value of " + str1 + "? (cannot be empty) ",
buf.readLine());
in.type("");
assertEquals("value of " + str1 + "? (cannot be empty) ",
buf.readLine());
in.type(str2);
}
};
final ATestRunnable testConsole = new FunctionalTestRunnable(function);
final Thread th = new Thread(testConsole);
th.start();
@ -544,18 +543,15 @@ public class ParametrizedCommandTest {
new InputStreamReader(pis, StandardCharsets.UTF_8));
PipedConsoleInput in = new PipedConsoleInput(new PrintStream(pout))) {
final ATestRunnable testConsole = new ATestRunnable() {
final TestFunction function = new TestFunction() {
@Override
protected void testRun() {
try {
assertEquals("value of " + str1 + "? ", buf.readLine());
in.type(str2);
} catch (final IOException e) {
fail("IO exception in test");
}
public void apply() throws Exception {
assertEquals("value of " + str1 + "? ", buf.readLine());
in.type(str2);
}
};
final ATestRunnable testConsole = new FunctionalTestRunnable(function);
final Thread th = new Thread(testConsole);
th.start();

View File

@ -174,7 +174,7 @@ public class SystemConsoleManagerTest {
final String prt = "++";
manager.setPrompt(prt);
assertEquals(prt, manager.getPrompt().apply());
assertEquals(prt, manager.getPrompt().get());
}
}

View File

@ -95,6 +95,8 @@ import org.junit.Test;
import net.bigeon.gclc.utils.PipedConsoleInput;
import net.bigeon.gclc.utils.PipedConsoleOutput;
import net.bigeon.test.junitmt.ATestRunnable;
import net.bigeon.test.junitmt.FunctionalTestRunnable;
import net.bigeon.test.junitmt.TestFunction;
import net.bigeon.test.junitmt.ThreadTest;
/**
@ -128,20 +130,17 @@ public class CLIPrompterTest {
BufferedReader buf = new BufferedReader(
new InputStreamReader(pis, StandardCharsets.UTF_8));
PipedConsoleInput in = new PipedConsoleInput(new PrintStream(pout))) {
final ATestRunnable target = new ATestRunnable() {
final TestFunction func = new TestFunction() {
@Override
public void testRun() {
try {
assertTrue(CLIPrompter.promptBoolean(out, in, "My message")); //$NON-NLS-1$
assertTrue(CLIPrompter.promptBoolean(out, in, "My message")); //$NON-NLS-1$
assertFalse(CLIPrompter.promptBoolean(out, in, "My message")); //$NON-NLS-1$
assertFalse(CLIPrompter.promptBoolean(out, in, "My message")); //$NON-NLS-1$
} catch (final IOException e) {
fail("Unexpected io excpetion"); //$NON-NLS-1$
}
public void apply() throws Exception {
assertTrue(CLIPrompter.promptBoolean(out, in, "My message")); //$NON-NLS-1$
assertTrue(CLIPrompter.promptBoolean(out, in, "My message")); //$NON-NLS-1$
assertFalse(CLIPrompter.promptBoolean(out, in, "My message")); //$NON-NLS-1$
assertFalse(CLIPrompter.promptBoolean(out, in, "My message")); //$NON-NLS-1$
}
};
final ATestRunnable target = new FunctionalTestRunnable(func);
final Thread th = new Thread(target);
th.start();
assertTrue(buf.readLine().startsWith("My message")); //$NON-NLS-1$
@ -161,9 +160,12 @@ public class CLIPrompterTest {
}
/** Test method for
* {@link net.bigeon.gclc.prompt.CLIPrompter#promptChoice(net.bigeon.gclc.manager.ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, List, List, String, String)}. */
* {@link net.bigeon.gclc.prompt.CLIPrompter#promptChoice(net.bigeon.gclc.manager.ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, List, List, String, String)}.
*
* @throws IOException if an IO occurred */
@Test
public final void testPromptChoiceConsoleManagerListOfStringListOfUStringString() {
public final void testPromptChoiceConsoleManagerListOfStringListOfUStringString()
throws IOException {
try (final PipedConsoleOutput out = new PipedConsoleOutput();
PipedOutputStream pout = new PipedOutputStream();
PipedInputStream pis = new PipedInputStream(pout);
@ -179,26 +181,23 @@ public class CLIPrompterTest {
final String cancel = "Cancel"; //$NON-NLS-1$
final String message = "My message"; //$NON-NLS-1$
final Thread th = new Thread(new Runnable() {
final TestFunction func = new TestFunction() {
@Override
public void run() {
try {
assertEquals("Asserted provided value to be retrieved",
choices.get(0), CLIPrompter.promptChoice(out, in, keys,
choices, message, cancel));
assertEquals("Asserted provided value to be retrieved",
choices.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));
} catch (final IOException e) {
fail("Unexpected io excpetion"); //$NON-NLS-1$
e.printStackTrace();
}
public void apply() throws Exception {
assertEquals("Asserted provided value to be retrieved",
choices.get(0), CLIPrompter.promptChoice(out, in, keys,
choices, message, cancel));
assertEquals("Asserted provided value to be retrieved",
choices.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));
}
});
};
final ATestRunnable target = new FunctionalTestRunnable(func);
final Thread th = new Thread(target);
th.start();
assertTrue(out.readNextLine().startsWith(message));
assertTrue(out.readNextLine().contains(keys.get(0)));
@ -242,15 +241,13 @@ public class CLIPrompterTest {
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
buf.readLine());
in.type("2"); //$NON-NLS-1$
th.join();
} catch (IOException | InterruptedException e) {
fail("Unexpected excpetion"); //$NON-NLS-1$
e.printStackTrace();
ThreadTest.assertRuns(th, target);
}
}
@Test
public final void testPromptChoiceConsoleManagerListOfUMapOfUTStringString() {
public final void testPromptChoiceConsoleManagerListOfUMapOfUTStringString()
throws IOException {
try (final PipedConsoleOutput out = new PipedConsoleOutput();
PipedOutputStream pout = new PipedOutputStream();
PipedInputStream pis = new PipedInputStream(pout);
@ -265,27 +262,23 @@ public class CLIPrompterTest {
choices.put(keys.get(1), "The actual other"); //$NON-NLS-1$
final String cancel = "Cancel"; //$NON-NLS-1$
final String message = "My message"; //$NON-NLS-1$
final Thread th = new Thread(new Runnable() {
final TestFunction func = new TestFunction() {
@Override
public void run() {
try {
assertEquals("Asserted provided value to be retrieved",
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));
} catch (final IOException e) {
fail("Unexpected io excpetion"); //$NON-NLS-1$
e.printStackTrace();
}
public void apply() throws Exception {
assertEquals("Asserted provided value to be retrieved",
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));
}
});
};
final ATestRunnable target = new FunctionalTestRunnable(func);
final Thread th = new Thread(target);
th.start();
assertTrue(out.readNextLine().startsWith(message));
assertTrue(out.readNextLine().contains(keys.get(0).toString()));
@ -329,15 +322,13 @@ public class CLIPrompterTest {
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
buf.readLine());
in.type("2"); //$NON-NLS-1$
th.join();
} catch (IOException | InterruptedException e) {
fail("Unexpected excpetion"); //$NON-NLS-1$
e.printStackTrace();
ThreadTest.assertRuns(th, target);
}
}
@Test
public final void testPromptChoiceConsoleManagerListOfUStringString() {
public final void testPromptChoiceConsoleManagerListOfUStringString()
throws IOException {
try (final PipedConsoleOutput out = new PipedConsoleOutput();
PipedOutputStream pout = new PipedOutputStream();
PipedInputStream pis = new PipedInputStream(pout);
@ -349,29 +340,26 @@ public class CLIPrompterTest {
keys.add("An other"); //$NON-NLS-1$
final String cancel = "Cancel"; //$NON-NLS-1$
final String message = "My message"; //$NON-NLS-1$
final Thread th = new Thread(new Runnable() {
final TestFunction func = new TestFunction() {
@Override
public void run() {
try {
assertEquals("Asserted provided value to be retrieved",
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));
} catch (final IOException e) {
fail("Unexpected io excpetion"); //$NON-NLS-1$
e.printStackTrace();
}
public void apply() throws Exception {
assertEquals("Asserted provided value to be retrieved",
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));
}
});
};
final ATestRunnable target = new FunctionalTestRunnable(func);
final Thread th = new Thread(target);
th.start();
assertTrue(out.readNextLine().startsWith(message));
assertTrue(out.readNextLine().contains(keys.get(0).toString()));
@ -422,17 +410,17 @@ public class CLIPrompterTest {
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
buf.readLine());
in.type("2"); //$NON-NLS-1$
th.join();
} catch (IOException | InterruptedException e) {
fail("Unexpected excpetion"); //$NON-NLS-1$
e.printStackTrace();
ThreadTest.assertRuns(th, target);
}
}
/** Test method for
* {@link net.bigeon.gclc.prompt.CLIPrompter#promptChoice(net.bigeon.gclc.manager.ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, Map, String, String)}. */
* {@link net.bigeon.gclc.prompt.CLIPrompter#promptChoice(net.bigeon.gclc.manager.ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, Map, String, String)}.
*
* @throws IOException if errors */
@Test
public final void testPromptChoiceConsoleManagerMapOfUTStringString() {
public final void testPromptChoiceConsoleManagerMapOfUTStringString()
throws IOException {
try (final PipedConsoleOutput out = new PipedConsoleOutput();
PipedOutputStream pout = new PipedOutputStream();
PipedInputStream pis = new PipedInputStream(pout);
@ -448,26 +436,23 @@ public class CLIPrompterTest {
final String cancel = "Cancel"; //$NON-NLS-1$
final String message = "My message"; //$NON-NLS-1$
final Thread th = new Thread(new Runnable() {
final TestFunction func = new TestFunction() {
@Override
public void run() {
try {
assertEquals("Asserted provided value to be retrieved",
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));
} catch (final IOException e) {
fail("Unexpected io excpetion"); //$NON-NLS-1$
e.printStackTrace();
}
public void apply() throws Exception {
assertEquals("Asserted provided value to be retrieved",
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));
}
});
};
final ATestRunnable runnable = new FunctionalTestRunnable(func);
final Thread th = new Thread(runnable);
th.start();
assertTrue(out.readNextLine().startsWith(message));
final String readNextLine = out.readNextLine();
@ -513,37 +498,33 @@ public class CLIPrompterTest {
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
buf.readLine());
in.type("2"); //$NON-NLS-1$
th.join();
} catch (IOException | InterruptedException e) {
fail("Unexpected excpetion"); //$NON-NLS-1$
e.printStackTrace();
ThreadTest.assertRuns(th, runnable);
}
}
/** Test method for
* {@link net.bigeon.gclc.prompt.CLIPrompter#promptInteger(net.bigeon.gclc.manager.ConsoleInput, String)}. */
* {@link net.bigeon.gclc.prompt.CLIPrompter#promptInteger(net.bigeon.gclc.manager.ConsoleInput, String)}.
*
* @throws IOException if errors */
@Test
public final void testPromptInteger() {
public final void testPromptInteger() throws IOException {
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 TestFunction func = new TestFunction() {
@Override
public void run() {
try {
assertEquals("Asserted provided value to be retrieved", 10,
CLIPrompter.promptInteger(in, "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) {
fail("Unexpected io excpetion"); //$NON-NLS-1$
e.printStackTrace();
}
public void apply() throws Exception {
assertEquals("Asserted provided value to be retrieved", 10,
CLIPrompter.promptInteger(in, "My message")); //$NON-NLS-1$
assertEquals("Asserted provided value to be retrieved", -15,
CLIPrompter.promptInteger(in, "My message")); //$NON-NLS-1$
}
});
};
final ATestRunnable runnable = new FunctionalTestRunnable(func);
final Thread th = new Thread(runnable);
th.start();
assertTrue(buf.readLine().startsWith("My message")); //$NON-NLS-1$
in.type(""); //$NON-NLS-1$
@ -553,18 +534,16 @@ public class CLIPrompterTest {
in.type("10"); //$NON-NLS-1$
assertTrue(buf.readLine().startsWith("My message")); //$NON-NLS-1$
in.type("-15"); //$NON-NLS-1$
th.join();
} catch (IOException | InterruptedException e) {
fail("Unexpected excpetion"); //$NON-NLS-1$
e.printStackTrace();
ThreadTest.assertRuns(th, runnable);
}
}
/** Test method for
* {@link net.bigeon.gclc.prompt.CLIPrompter#promptList(net.bigeon.gclc.manager.ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, String)}. */
* {@link net.bigeon.gclc.prompt.CLIPrompter#promptList(net.bigeon.gclc.manager.ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, String)}.
*
* @throws IOException if errors */
@Test
public final void testPromptListConsoleManagerString() {
public final void testPromptListConsoleManagerString() throws IOException {
try (final PipedConsoleOutput out = new PipedConsoleOutput();
PipedOutputStream pout = new PipedOutputStream();
PipedInputStream pis = new PipedInputStream(pout);
@ -576,22 +555,19 @@ public class CLIPrompterTest {
keys.add("An other"); //$NON-NLS-1$
final String message = "My message"; //$NON-NLS-1$
final Thread th = new Thread(new Runnable() {
final TestFunction func = new TestFunction() {
@Override
public void run() {
try {
assertEquals("Asserted provided value to be retrieved",
new ArrayList<String>(),
CLIPrompter.promptList(out, in, message));
assertEquals("Asserted provided value to be retrieved", keys,
CLIPrompter.promptList(out, in, message));
} catch (final IOException e) {
fail("Unexpected io excpetion"); //$NON-NLS-1$
e.printStackTrace();
}
public void apply() throws Exception {
assertEquals("Asserted provided value to be retrieved",
new ArrayList<String>(),
CLIPrompter.promptList(out, in, message));
assertEquals("Asserted provided value to be retrieved", keys,
CLIPrompter.promptList(out, in, message));
}
});
};
final ATestRunnable runnable = new FunctionalTestRunnable(func);
final Thread th = new Thread(runnable);
th.start();
String nLine = out.readNextLine();
assertTrue(nLine.startsWith(message));
@ -615,17 +591,16 @@ public class CLIPrompterTest {
buf.readLine());
}
in.type(CLIPrompterMessages.getString("promptlist.exit.defaultkey")); //$NON-NLS-1$
th.join();
} catch (IOException | InterruptedException e) {
fail("Unexpected excpetion"); //$NON-NLS-1$
e.printStackTrace();
ThreadTest.assertRuns(th, runnable);
}
}
/** Test method for
* {@link net.bigeon.gclc.prompt.CLIPrompter#promptList(net.bigeon.gclc.manager.ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, String, String)}. */
* {@link net.bigeon.gclc.prompt.CLIPrompter#promptList(net.bigeon.gclc.manager.ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, String, String)}.
*
* @throws IOException if errors */
@Test
public final void testPromptListConsoleManagerStringString() {
public final void testPromptListConsoleManagerStringString() throws IOException {
try (final PipedConsoleOutput out = new PipedConsoleOutput();
PipedOutputStream pout = new PipedOutputStream();
PipedInputStream pis = new PipedInputStream(pout);
@ -638,22 +613,19 @@ public class CLIPrompterTest {
final String ender = "*"; //$NON-NLS-1$
final String message = "My message"; //$NON-NLS-1$
final Thread th = new Thread(new Runnable() {
final TestFunction func = new TestFunction() {
@Override
public void run() {
try {
assertEquals("Asserted provided value to be retrieved",
new ArrayList<String>(),
CLIPrompter.promptList(out, in, message, ender));
assertEquals("Asserted provided value to be retrieved", keys,
CLIPrompter.promptList(out, in, message, ender));
} catch (final IOException e) {
fail("Unexpected io excpetion"); //$NON-NLS-1$
e.printStackTrace();
}
public void apply() throws Exception {
assertEquals("Asserted provided value to be retrieved",
new ArrayList<String>(),
CLIPrompter.promptList(out, in, message, ender));
assertEquals("Asserted provided value to be retrieved", keys,
CLIPrompter.promptList(out, in, message, ender));
}
});
};
final ATestRunnable runnable = new FunctionalTestRunnable(func);
final Thread th = new Thread(runnable);
th.start();
String nLine = out.readNextLine();
assertTrue(nLine.startsWith(message));
@ -675,17 +647,16 @@ public class CLIPrompterTest {
buf.readLine());
}
in.type(ender);
th.join();
} catch (IOException | InterruptedException e) {
fail("Unexpected excpetion"); //$NON-NLS-1$
e.printStackTrace();
ThreadTest.assertRuns(th, runnable);
}
}
/** Test method for
* {@link net.bigeon.gclc.prompt.CLIPrompter#promptLongText(net.bigeon.gclc.manager.ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, String)}. */
* {@link net.bigeon.gclc.prompt.CLIPrompter#promptLongText(net.bigeon.gclc.manager.ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, String)}.
*
* @throws IOException if errors */
@Test
public final void testPromptLongTextConsoleManagerString() {
public final void testPromptLongTextConsoleManagerString() throws IOException {
try (final PipedConsoleOutput out = new PipedConsoleOutput();
PipedOutputStream pout = new PipedOutputStream();
PipedInputStream pis = new PipedInputStream(pout);
@ -697,22 +668,19 @@ public class CLIPrompterTest {
+ "line feeds and other" + System.lineSeparator()
+ System.lineSeparator() + " \tspecial characters";
final Thread th = new Thread(new Runnable() {
final TestFunction func = new TestFunction() {
@Override
public void run() {
try {
assertEquals("Asserted provided value to be retrieved", "",
CLIPrompter.promptLongText(out, in, message));
assertEquals("Asserted provided value to be retrieved",
longText + System.lineSeparator(),
CLIPrompter.promptLongText(out, in, message));
} catch (final IOException e) {
fail("Unexpected io excpetion"); //$NON-NLS-1$
e.printStackTrace();
}
public void apply() throws Exception {
assertEquals("Asserted provided value to be retrieved", "",
CLIPrompter.promptLongText(out, in, message));
assertEquals("Asserted provided value to be retrieved",
longText + System.lineSeparator(),
CLIPrompter.promptLongText(out, in, message));
}
});
};
final ATestRunnable runnable = new FunctionalTestRunnable(func);
final Thread th = new Thread(runnable);
th.start();
String nLine = out.readNextLine();
assertTrue(nLine.startsWith(message));
@ -737,17 +705,16 @@ public class CLIPrompterTest {
buf.readLine());
}
in.type(CLIPrompterMessages.getString("promptlongtext.exit.defaultkey"));
th.join();
} catch (IOException | InterruptedException e) {
fail("Unexpected excpetion"); //$NON-NLS-1$
e.printStackTrace();
ThreadTest.assertRuns(th, runnable);
}
}
/** Test method for
* {@link net.bigeon.gclc.prompt.CLIPrompter#promptLongText(net.bigeon.gclc.manager.ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, String, String)}. */
* {@link net.bigeon.gclc.prompt.CLIPrompter#promptLongText(net.bigeon.gclc.manager.ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, String, String)}.
*
* @throws IOException if errors */
@Test
public final void testPromptLongTextConsoleManagerStringString() {
public final void testPromptLongTextConsoleManagerStringString() throws IOException {
try (final PipedConsoleOutput out = new PipedConsoleOutput();
PipedOutputStream pout = new PipedOutputStream();
PipedInputStream pis = new PipedInputStream(pout);
@ -761,22 +728,19 @@ public class CLIPrompterTest {
final String longText = text[0] + System.lineSeparator() + text[1]
+ System.lineSeparator() + text[2];
final Thread th = new Thread(new Runnable() {
final TestFunction func = new TestFunction() {
@Override
public void run() {
try {
assertEquals("Asserted provided value to be retrieved", //$NON-NLS-1$
"", CLIPrompter.promptLongText(out, in, message, ender));
assertEquals("Asserted provided value to be retrieved",
longText + System.lineSeparator(),
CLIPrompter.promptLongText(out, in, message, ender));
} catch (final IOException e) {
fail("Unexpected io excpetion"); //$NON-NLS-1$
e.printStackTrace();
}
public void apply() throws Exception {
assertEquals("Asserted provided value to be retrieved", //$NON-NLS-1$
"", CLIPrompter.promptLongText(out, in, message, ender));
assertEquals("Asserted provided value to be retrieved",
longText + System.lineSeparator(),
CLIPrompter.promptLongText(out, in, message, ender));
}
});
};
final ATestRunnable runnable = new FunctionalTestRunnable(func);
final Thread th = new Thread(runnable);
th.start();
String nLine = out.readNextLine();
assertTrue(nLine.startsWith(message));
@ -798,17 +762,17 @@ public class CLIPrompterTest {
buf.readLine());
}
in.type(ender);
th.join();
} catch (IOException | InterruptedException e) {
fail("Unexpected excpetion"); //$NON-NLS-1$
e.printStackTrace();
ThreadTest.assertRuns(th, runnable);
}
}
/** Test method for
* {@link net.bigeon.gclc.prompt.CLIPrompter#promptMultiChoice(net.bigeon.gclc.manager.ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, List, List, String)}. */
* {@link net.bigeon.gclc.prompt.CLIPrompter#promptMultiChoice(net.bigeon.gclc.manager.ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, List, List, String)}.
*
* @throws IOException if errors */
@Test
public final void testPromptMultiChoiceConsoleManagerListOfStringListOfUString() {
public final void testPromptMultiChoiceConsoleManagerListOfStringListOfUString()
throws IOException {
try (final PipedConsoleOutput out = new PipedConsoleOutput();
PipedOutputStream pout = new PipedOutputStream();
PipedInputStream pis = new PipedInputStream(pout);
@ -823,29 +787,24 @@ public class CLIPrompterTest {
choices.add("The actual other"); //$NON-NLS-1$
final String message = "My message"; //$NON-NLS-1$
final Thread th = new Thread(new Runnable() {
final TestFunction func = new TestFunction() {
@Override
public void run() {
try {
assertTrue("Asserted provided value to be retrieved", CLIPrompter
.promptMultiChoice(out, in, keys, choices, message)
.isEmpty());
final ArrayList<Object> l = new ArrayList<>();
l.add(choices.get(0));
assertEquals("Asserted provided value to be retrieved", l,
CLIPrompter.promptMultiChoice(out, in, keys, choices,
message));
l.add(choices.get(1));
assertEquals("Asserted provided value to be retrieved", l,
CLIPrompter.promptMultiChoice(out, in, keys, choices,
message));
} catch (final IOException e) {
fail("Unexpected io excpetion"); //$NON-NLS-1$
e.printStackTrace();
}
public void apply() throws Exception {
assertTrue("Asserted provided value to be retrieved",
CLIPrompter.promptMultiChoice(out, in, keys, choices, message)
.isEmpty());
final ArrayList<Object> l = new ArrayList<>();
l.add(choices.get(0));
assertEquals("Asserted provided value to be retrieved", l, CLIPrompter
.promptMultiChoice(out, in, keys, choices, message));
l.add(choices.get(1));
assertEquals("Asserted provided value to be retrieved", l, CLIPrompter
.promptMultiChoice(out, in, keys, choices, message));
}
});
};
final ATestRunnable runnable = new FunctionalTestRunnable(func);
final Thread th = new Thread(runnable);
th.start();
assertTrue(out.readNextLine().startsWith(message));
assertTrue(out.readNextLine().contains(keys.get(0)));
@ -886,17 +845,17 @@ public class CLIPrompterTest {
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
buf.readLine());
in.type("0 1"); //$NON-NLS-1$
th.join();
} catch (IOException | InterruptedException e) {
fail("Unexpected excpetion"); //$NON-NLS-1$
e.printStackTrace();
ThreadTest.assertRuns(th, runnable);
}
}
/** Test method for
* {@link net.bigeon.gclc.prompt.CLIPrompter#promptMultiChoice(net.bigeon.gclc.manager.ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, List, Map, String)}. */
* {@link net.bigeon.gclc.prompt.CLIPrompter#promptMultiChoice(net.bigeon.gclc.manager.ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, List, Map, String)}.
*
* @throws IOException if errors */
@Test
public final void testPromptMultiChoiceConsoleManagerListOfUMapOfUTString() {
public final void testPromptMultiChoiceConsoleManagerListOfUMapOfUTString()
throws IOException {
try (final PipedConsoleOutput out = new PipedConsoleOutput();
PipedOutputStream pout = new PipedOutputStream();
PipedInputStream pis = new PipedInputStream(pout);
@ -910,30 +869,24 @@ public class CLIPrompterTest {
keys.add("An other"); //$NON-NLS-1$
choices.put(keys.get(1), "The actual other"); //$NON-NLS-1$
final String message = "My message"; //$NON-NLS-1$
final Thread th = new Thread(new Runnable() {
final TestFunction func = new TestFunction() {
@Override
public void run() {
try {
assertTrue("Asserted provided value to be retrieved", CLIPrompter
.promptMultiChoice(out, in, keys, choices, message)
.isEmpty());
final ArrayList<Object> l = new ArrayList<>();
l.add(choices.get(keys.get(0)));
assertEquals("Asserted provided value to be retrieved", l,
CLIPrompter.promptMultiChoice(out, in, keys, choices,
message));
l.add(choices.get(keys.get(1)));
assertEquals("Asserted provided value to be retrieved", l,
CLIPrompter.promptMultiChoice(out, in, keys, choices,
message));
} catch (final IOException e) {
fail("Unexpected io excpetion"); //$NON-NLS-1$
e.printStackTrace();
}
public void apply() throws Exception {
assertTrue("Asserted provided value to be retrieved",
CLIPrompter.promptMultiChoice(out, in, keys, choices, message)
.isEmpty());
final ArrayList<Object> l = new ArrayList<>();
l.add(choices.get(keys.get(0)));
assertEquals("Asserted provided value to be retrieved", l, CLIPrompter
.promptMultiChoice(out, in, keys, choices, message));
l.add(choices.get(keys.get(1)));
assertEquals("Asserted provided value to be retrieved", l, CLIPrompter
.promptMultiChoice(out, in, keys, choices, message));
}
});
};
final ATestRunnable target = new FunctionalTestRunnable(func);
final Thread th = new Thread(target);
th.start();
assertTrue(out.readNextLine().startsWith(message));
assertTrue(out.readNextLine().contains(keys.get(0).toString()));
@ -974,17 +927,17 @@ public class CLIPrompterTest {
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
buf.readLine());
in.type("0 1"); //$NON-NLS-1$
th.join();
} catch (IOException | InterruptedException e) {
fail("Unexpected excpetion"); //$NON-NLS-1$
e.printStackTrace();
ThreadTest.assertRuns(th, target);
}
}
/** Test method for
* {@link net.bigeon.gclc.prompt.CLIPrompter#promptMultiChoice(net.bigeon.gclc.manager.ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, List, String)}. */
* {@link net.bigeon.gclc.prompt.CLIPrompter#promptMultiChoice(net.bigeon.gclc.manager.ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, List, String)}.
*
* @throws IOException if error */
@Test
public final void testPromptMultiChoiceConsoleManagerListOfUString() {
public final void testPromptMultiChoiceConsoleManagerListOfUString()
throws IOException {
try (final PipedConsoleOutput out = new PipedConsoleOutput();
PipedOutputStream pout = new PipedOutputStream();
PipedInputStream pis = new PipedInputStream(pout);
@ -995,27 +948,23 @@ public class CLIPrompterTest {
keys.add("A choice"); //$NON-NLS-1$
keys.add("An other"); //$NON-NLS-1$
final String message = "My message"; //$NON-NLS-1$
final Thread th = new Thread(new Runnable() {
final TestFunction func = new TestFunction() {
@Override
public void run() {
try {
assertTrue("Asserted provided value to be retrieved", CLIPrompter
.promptMultiChoice(out, in, keys, message).isEmpty());
final ArrayList<Integer> l = new ArrayList<>();
l.add(0);
assertEquals("Asserted provided value to be retrieved", l,
CLIPrompter.promptMultiChoice(out, in, keys, message));
l.add(1);
assertEquals("Asserted provided value to be retrieved", l,
CLIPrompter.promptMultiChoice(out, in, keys, message));
} catch (final IOException e) {
fail("Unexpected io excpetion"); //$NON-NLS-1$
e.printStackTrace();
}
public void apply() throws Exception {
assertTrue("Asserted provided value to be retrieved", CLIPrompter
.promptMultiChoice(out, in, keys, message).isEmpty());
final ArrayList<Integer> l = new ArrayList<>();
l.add(0);
assertEquals("Asserted provided value to be retrieved", l,
CLIPrompter.promptMultiChoice(out, in, keys, message));
l.add(1);
assertEquals("Asserted provided value to be retrieved", l,
CLIPrompter.promptMultiChoice(out, in, keys, message));
}
});
};
final ATestRunnable target = new FunctionalTestRunnable(func);
final Thread th = new Thread(target);
th.start();
assertTrue(out.readNextLine().startsWith(message));
assertTrue(out.readNextLine().contains(keys.get(0).toString()));
@ -1056,10 +1005,7 @@ public class CLIPrompterTest {
assertEquals(CLIPrompterMessages.getString("prompt.lineprompt"), //$NON-NLS-1$
buf.readLine());
in.type("0 1"); //$NON-NLS-1$
th.join();
} catch (IOException | InterruptedException e) {
fail("Unexpected excpetion"); //$NON-NLS-1$
e.printStackTrace();
ThreadTest.assertRuns(th, target);
}
}
@ -1084,8 +1030,7 @@ public class CLIPrompterTest {
final ATestRunnable target = new ATestRunnable() {
@Override
public void testRun() {
try {
public void testRun() throws IOException {
assertTrue("Asserted provided value to be retrieved", CLIPrompter
.promptMultiChoice(out, in, keys, choices, message)
.isEmpty());
@ -1098,10 +1043,6 @@ public class CLIPrompterTest {
assertEquals("Asserted provided value to be retrieved", l,
CLIPrompter.promptMultiChoice(out, in, keys, choices,
message));
} catch (final IOException e) {
fail("Unexpected io excpetion"); //$NON-NLS-1$
e.printStackTrace();
}
}
};
final Thread th = new Thread(target);
@ -1157,14 +1098,10 @@ public class CLIPrompterTest {
final ATestRunnable target = new ATestRunnable() {
@Override
public void testRun() {
try {
public void testRun() throws IOException {
assertEquals("Expected provided message to be returned", res,
CLIPrompter.promptNonEmpty(in, "My message", //$NON-NLS-1$
"my reprompt")); //$NON-NLS-1$
} catch (final IOException e) {
fail("Unexpected io excpetion"); //$NON-NLS-1$
}
}
};
final Thread th = new Thread(target);

View File

@ -1,6 +1,7 @@
package net.bigeon.gclc.utils;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import java.io.IOException;
@ -8,13 +9,12 @@ import org.junit.Test;
import net.bigeon.gclc.manager.ConsoleInput;
import net.bigeon.gclc.tools.ConstantString;
import net.bigeon.gclc.utils.EmptyInput;
public class EmptyInputTest {
@Test
public void testClose() throws IOException {
ConsoleInput input = EmptyInput.INSTANCE;
final ConsoleInput input = EmptyInput.INSTANCE;
// several close operation shold create no error
input.close();
input.close();
@ -24,19 +24,19 @@ public class EmptyInputTest {
@Test
public void testGetPrompt() {
ConsoleInput input = EmptyInput.INSTANCE;
String init = input.getPrompt().apply();
final ConsoleInput input = EmptyInput.INSTANCE;
final String init = input.getPrompt().get();
input.setPrompt("some prompt different from "+init);
assertEquals("Prompts should not be changed", init, input.getPrompt().apply());
assertEquals("Prompts should not be changed", init, input.getPrompt().get());
input.setPrompt(new ConstantString("some other prompt different from "+init));
assertEquals("Prompts should not be changed", init, input.getPrompt().apply());
assertEquals("Prompts should not be changed", init, input.getPrompt().get());
}
@Test
public void testInterruptPrompt() {
// Nothing to test, really...
ConsoleInput input = EmptyInput.INSTANCE;
final ConsoleInput input = EmptyInput.INSTANCE;
// several close operation shold create no error
input.interruptPrompt();
assertFalse("This source input should never be closed", input.isClosed());
@ -44,7 +44,7 @@ public class EmptyInputTest {
@Test
public void testIsClosed() throws IOException {
ConsoleInput input = EmptyInput.INSTANCE;
final ConsoleInput input = EmptyInput.INSTANCE;
// several close operation shold create no error
assertFalse("This source input should never be closed", input.isClosed());
input.close();
@ -55,7 +55,7 @@ public class EmptyInputTest {
@Test
public void testPrompt() throws IOException {
ConsoleInput input = EmptyInput.INSTANCE;
final ConsoleInput input = EmptyInput.INSTANCE;
// several close operation shold create no error
assertEquals("Any prompt should return the empty value", "", input.prompt());
assertEquals("Any prompt should return the empty value", "", input.prompt(0));