Added piped stream manager

Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
2016-11-30 09:49:16 -05:00
parent 65b6be8283
commit 72362936be
15 changed files with 416 additions and 171 deletions

View File

@@ -42,7 +42,7 @@ import java.io.IOException;
import fr.bigeon.gclc.command.ICommand;
import fr.bigeon.gclc.exception.InvalidCommandName;
import fr.bigeon.gclc.test.utils.TestConsoleManager;
import fr.bigeon.gclc.manager.PipedConsoleManager;
/**
* <p>
@@ -56,11 +56,11 @@ public class CommandTestingApplication implements AutoCloseable {
private final ConsoleTestApplication application;
private final Thread th;
private final TestConsoleManager manager;
private final PipedConsoleManager manager;
/** @throws IOException if the streams cannot be build */
public CommandTestingApplication() throws IOException {
manager = new TestConsoleManager();
manager = new PipedConsoleManager();
application = new ConsoleTestApplication(manager);
th = new Thread(new Runnable() {
@@ -110,7 +110,7 @@ public class CommandTestingApplication implements AutoCloseable {
/** @return the next written line, null if it is the prompt
* @throws IOException if the reading failed
* @see fr.bigeon.gclc.test.utils.TestConsoleManager#readNextLine() */
* @see fr.bigeon.gclc.manager.PipedConsoleManager#readNextLine() */
public String readNextLine() throws IOException {
String ret = manager.readNextLine();
if (ret.equals(manager.getPrompt())) {

View File

@@ -56,7 +56,7 @@ import fr.bigeon.gclc.exception.InvalidCommandName;
import fr.bigeon.gclc.i18n.Messages;
import fr.bigeon.gclc.manager.ConsoleManager;
import fr.bigeon.gclc.manager.SystemConsoleManager;
import fr.bigeon.gclc.test.utils.TestConsoleManager;
import fr.bigeon.gclc.manager.PipedConsoleManager;
/** Test class for ConsoleApplication
*
@@ -136,7 +136,7 @@ public class ConsoleApplicationTest {
}
ConsoleApplication appli = null;
try (TestConsoleManager manager = new TestConsoleManager()) {
try (PipedConsoleManager manager = new PipedConsoleManager()) {
final ConsoleApplication app = new ConsoleApplication(manager, null,
null);
appli = app;
@@ -167,7 +167,7 @@ public class ConsoleApplicationTest {
@Test
public void interpretCommandTest() {
try (TestConsoleManager test = new TestConsoleManager()) {
try (PipedConsoleManager test = new PipedConsoleManager()) {
ConsoleApplication appl = new ConsoleApplication(test, "", "");
appl.interpretCommand("invalid cmd \"due to misplaced\"quote");

View File

@@ -45,7 +45,7 @@ import java.io.IOException;
import org.junit.Test;
import fr.bigeon.gclc.exception.CommandRunException;
import fr.bigeon.gclc.test.utils.TestConsoleManager;
import fr.bigeon.gclc.manager.PipedConsoleManager;
/** <p>
* TODO
@@ -55,7 +55,7 @@ public class CommandTest {
@Test
public final void test() {
try (TestConsoleManager test = new TestConsoleManager()) {
try (PipedConsoleManager test = new PipedConsoleManager()) {
Command cmd;
cmd = new Command("name") {

View File

@@ -45,7 +45,7 @@ import static org.junit.Assert.fail;
import org.junit.Test;
import fr.bigeon.gclc.exception.CommandRunException;
import fr.bigeon.gclc.test.utils.TestConsoleManager;
import fr.bigeon.gclc.manager.PipedConsoleManager;
/**
* <p>
@@ -68,7 +68,7 @@ public class HelpExecutorTest {
} catch (Exception e) {
assertNotNull(e);
}
try (TestConsoleManager test = new TestConsoleManager()) {
try (PipedConsoleManager test = new PipedConsoleManager()) {
help = new HelpExecutor("?", test, new MockCommand("mock"));
} catch (Exception e) {
assertNull(e);
@@ -81,7 +81,7 @@ public class HelpExecutorTest {
@Test
public final void testExecute(){
try {
TestConsoleManager test = new TestConsoleManager();
PipedConsoleManager test = new PipedConsoleManager();
HelpExecutor help = new HelpExecutor("?", test,
new Command("mock") {
@@ -116,7 +116,7 @@ public class HelpExecutorTest {
*/
@Test
public final void testTip(){
try (TestConsoleManager test = new TestConsoleManager()) {
try (PipedConsoleManager test = new PipedConsoleManager()) {
HelpExecutor help = new HelpExecutor("?", test,
new MockCommand("mock"));
help.tip();
@@ -124,7 +124,7 @@ public class HelpExecutorTest {
} catch (Exception e) {
assertNull(e);
}
try (TestConsoleManager test = new TestConsoleManager()) {
try (PipedConsoleManager test = new PipedConsoleManager()) {
HelpExecutor help = new HelpExecutor("?", test,
new SubedCommand("sub", new MockCommand("mock")));
help.tip();

View File

@@ -51,7 +51,7 @@ import org.junit.Test;
import fr.bigeon.gclc.exception.CommandRunException;
import fr.bigeon.gclc.exception.InvalidParameterException;
import fr.bigeon.gclc.test.utils.TestConsoleManager;
import fr.bigeon.gclc.manager.PipedConsoleManager;
/** <p>
* TODO
@@ -63,7 +63,7 @@ public class ParametrizedCommandTest {
* {@link fr.bigeon.gclc.command.ParametrizedCommand#ParametrizedCommand(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String)}. */
@Test
public final void testParametrizedCommandConsoleManagerString() {
try (TestConsoleManager test = new TestConsoleManager()) {
try (PipedConsoleManager test = new PipedConsoleManager()) {
ParametrizedCommand cmd = new ParametrizedCommand(test, "name") {
@Override
@@ -116,7 +116,7 @@ public class ParametrizedCommandTest {
* {@link fr.bigeon.gclc.command.ParametrizedCommand#ParametrizedCommand(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String, boolean)}. */
@Test
public final void testParametrizedCommandConsoleManagerStringBoolean() {
try (TestConsoleManager test = new TestConsoleManager()) {
try (PipedConsoleManager test = new PipedConsoleManager()) {
ParametrizedCommand cmd = new ParametrizedCommand(test, "name",
false) {
@@ -528,7 +528,7 @@ public class ParametrizedCommandTest {
assertNotNull(e);
}
// TODO Test of interactive not providing and providing all needed
try (TestConsoleManager test = new TestConsoleManager()) {
try (PipedConsoleManager test = new PipedConsoleManager()) {
cmd = new ParametrizedCommand(test, "name", false) {
{
try {
@@ -610,7 +610,7 @@ public class ParametrizedCommandTest {
fail("unepected error");
}
try {
TestConsoleManager test = new TestConsoleManager();
PipedConsoleManager test = new PipedConsoleManager();
cmd = new ParametrizedCommand(test, "name") {
{
try {

View File

@@ -50,7 +50,7 @@ import org.junit.Test;
import fr.bigeon.gclc.ConsoleTestApplication;
import fr.bigeon.gclc.exception.CommandRunException;
import fr.bigeon.gclc.exception.CommandRunExceptionType;
import fr.bigeon.gclc.test.utils.TestConsoleManager;
import fr.bigeon.gclc.manager.PipedConsoleManager;
/**
* <p>
@@ -67,9 +67,9 @@ public class ScriptExecutionTest {
*/
@Test
public void testExecute() {
TestConsoleManager test;
PipedConsoleManager test;
try {
test = new TestConsoleManager();
test = new PipedConsoleManager();
} catch (IOException e2) {
fail("creation of console manager failed"); //$NON-NLS-1$
assertNotNull(e2);
@@ -165,7 +165,7 @@ public class ScriptExecutionTest {
public void testHelp() {
ScriptExecution exec = new ScriptExecution("script", null, "#", //$NON-NLS-1$ //$NON-NLS-2$
Charset.forName("UTF-8"));
try (TestConsoleManager test = new TestConsoleManager()) {
try (PipedConsoleManager test = new PipedConsoleManager()) {
exec.help(test);
exec.help(test, "ignored element");
} catch (IOException e) {

View File

@@ -50,7 +50,7 @@ import org.junit.Test;
import fr.bigeon.gclc.exception.CommandRunException;
import fr.bigeon.gclc.exception.InvalidCommandName;
import fr.bigeon.gclc.manager.ConsoleManager;
import fr.bigeon.gclc.test.utils.TestConsoleManager;
import fr.bigeon.gclc.manager.PipedConsoleManager;
/** <p>
* TODO
@@ -307,8 +307,8 @@ public class SubedCommandTest {
assertNotNull(e);
}
try (TestConsoleManager manager = new TestConsoleManager();
TestConsoleManager manager2 = new TestConsoleManager()) {
try (PipedConsoleManager manager = new PipedConsoleManager();
PipedConsoleManager manager2 = new PipedConsoleManager()) {
cmd.help(manager);
assertEquals("\tid", manager.readNextLine());
cmd.help(manager, "id");
@@ -327,8 +327,8 @@ public class SubedCommandTest {
assertNotNull(e);
}
try (TestConsoleManager manager = new TestConsoleManager();
TestConsoleManager manager2 = new TestConsoleManager()) {
try (PipedConsoleManager manager = new PipedConsoleManager();
PipedConsoleManager manager2 = new PipedConsoleManager()) {
cmd.help(manager);
assertEquals("\tid", manager.readNextLine());
} catch (IOException e) {
@@ -368,8 +368,8 @@ public class SubedCommandTest {
assertNotNull(e);
}
try (TestConsoleManager manager = new TestConsoleManager();
TestConsoleManager manager2 = new TestConsoleManager()) {
try (PipedConsoleManager manager = new PipedConsoleManager();
PipedConsoleManager manager2 = new PipedConsoleManager()) {
cmd.help(manager);
assertEquals("\ttip", manager.readNextLine());
assertEquals("\tid: tip", manager.readNextLine());

View File

@@ -53,7 +53,7 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import fr.bigeon.gclc.test.utils.TestConsoleManager;
import fr.bigeon.gclc.manager.PipedConsoleManager;
/** <p>
* TODO
@@ -77,7 +77,7 @@ public class CLIPrompterTest {
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptBoolean(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String)}. */
@Test
public final void testPromptBoolean() {
try (final TestConsoleManager test = new TestConsoleManager()) {
try (final PipedConsoleManager test = new PipedConsoleManager()) {
Thread th = new Thread(new Runnable() {
@Override
@@ -121,7 +121,7 @@ public class CLIPrompterTest {
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptChoice(fr.bigeon.gclc.manager.ConsoleManager, java.util.List, java.util.List, java.lang.String, java.lang.String)}. */
@Test
public final void testPromptChoiceConsoleManagerListOfStringListOfUStringString() {
try (final TestConsoleManager test = new TestConsoleManager()) {
try (final PipedConsoleManager test = new PipedConsoleManager()) {
final List<String> keys = new ArrayList<>();
final List<Object> choices = new ArrayList<>();
keys.add("A choice"); //$NON-NLS-1$
@@ -201,7 +201,7 @@ public class CLIPrompterTest {
@Test
public final void testPromptChoiceConsoleManagerListOfUStringString() {
try (final TestConsoleManager test = new TestConsoleManager()) {
try (final PipedConsoleManager test = new PipedConsoleManager()) {
final List<Object> keys = new ArrayList<>();
keys.add("A choice"); //$NON-NLS-1$
keys.add("An other"); //$NON-NLS-1$
@@ -289,7 +289,7 @@ public class CLIPrompterTest {
@Test
public final void testPromptChoiceConsoleManagerListOfUMapOfUTStringString() {
try (final TestConsoleManager test = new TestConsoleManager()) {
try (final PipedConsoleManager test = new PipedConsoleManager()) {
final List<Object> keys = new ArrayList<>();
final Map<Object, Object> choices = new HashMap<>();
keys.add("A choice"); //$NON-NLS-1$
@@ -373,7 +373,7 @@ public class CLIPrompterTest {
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptChoice(fr.bigeon.gclc.manager.ConsoleManager, java.util.Map, java.lang.String, java.lang.String)}. */
@Test
public final void testPromptChoiceConsoleManagerMapOfUTStringString() {
try (final TestConsoleManager test = new TestConsoleManager()) {
try (final PipedConsoleManager test = new PipedConsoleManager()) {
final List<Object> keys = new ArrayList<>();
final Map<Object, Object> choices = new HashMap<>();
keys.add("A choice"); //$NON-NLS-1$
@@ -455,7 +455,7 @@ public class CLIPrompterTest {
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptInteger(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String)}. */
@Test
public final void testPromptInteger() {
try (final TestConsoleManager test = new TestConsoleManager()) {
try (final PipedConsoleManager test = new PipedConsoleManager()) {
Thread th = new Thread(new Runnable() {
@Override
@@ -492,7 +492,7 @@ public class CLIPrompterTest {
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptList(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String)}. */
@Test
public final void testPromptListConsoleManagerString() {
try (final TestConsoleManager test = new TestConsoleManager()) {
try (final PipedConsoleManager test = new PipedConsoleManager()) {
final List<String> keys = new ArrayList<>();
keys.add("A choice"); //$NON-NLS-1$
keys.add("An other"); //$NON-NLS-1$
@@ -549,7 +549,7 @@ public class CLIPrompterTest {
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptList(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String, java.lang.String)}. */
@Test
public final void testPromptListConsoleManagerStringString() {
try (final TestConsoleManager test = new TestConsoleManager()) {
try (final PipedConsoleManager test = new PipedConsoleManager()) {
final List<String> keys = new ArrayList<>();
keys.add("A choice"); //$NON-NLS-1$
keys.add("An other"); //$NON-NLS-1$
@@ -603,7 +603,7 @@ public class CLIPrompterTest {
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptLongText(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String)}. */
@Test
public final void testPromptLongTextConsoleManagerString() {
try (final TestConsoleManager test = new TestConsoleManager()) {
try (final PipedConsoleManager test = new PipedConsoleManager()) {
final String message = "My message";
final String longText = "Some text with" + System.lineSeparator() +
"line feeds and other" + System.lineSeparator() +
@@ -663,7 +663,7 @@ public class CLIPrompterTest {
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptLongText(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String, java.lang.String)}. */
@Test
public final void testPromptLongTextConsoleManagerStringString() {
try (final TestConsoleManager test = new TestConsoleManager()) {
try (final PipedConsoleManager test = new PipedConsoleManager()) {
final String message = "My message";
final String ender = "\\quit";
final String[] text = new String[]{"Some text with" ,
@@ -676,7 +676,7 @@ public class CLIPrompterTest {
@Override
public void run() {
try {
assertEquals("",
assertEquals("", //$NON-NLS-1$
CLIPrompter.promptLongText(test, message, ender));
assertEquals(longText,
CLIPrompter.promptLongText(test, message, ender));
@@ -720,7 +720,7 @@ public class CLIPrompterTest {
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptMultiChoice(fr.bigeon.gclc.manager.ConsoleManager, java.util.List, java.util.List, java.lang.String)}. */
@Test
public final void testPromptMultiChoiceConsoleManagerListOfStringListOfUString() {
try (final TestConsoleManager test = new TestConsoleManager()) {
try (final PipedConsoleManager test = new PipedConsoleManager()) {
final List<String> keys = new ArrayList<>();
final List<Object> choices = new ArrayList<>();
keys.add("A choice"); //$NON-NLS-1$
@@ -803,7 +803,7 @@ public class CLIPrompterTest {
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptMultiChoice(fr.bigeon.gclc.manager.ConsoleManager, java.util.List, java.util.Map, java.lang.String)}. */
@Test
public final void testPromptMultiChoiceConsoleManagerListOfUMapOfUTString() {
try (final TestConsoleManager test = new TestConsoleManager()) {
try (final PipedConsoleManager test = new PipedConsoleManager()) {
final List<Object> keys = new ArrayList<>();
final Map<Object, Object> choices = new HashMap<>();
keys.add("A choice"); //$NON-NLS-1$
@@ -886,7 +886,7 @@ public class CLIPrompterTest {
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptMultiChoice(fr.bigeon.gclc.manager.ConsoleManager, java.util.List, java.lang.String)}. */
@Test
public final void testPromptMultiChoiceConsoleManagerListOfUString() {
try (final TestConsoleManager test = new TestConsoleManager()) {
try (final PipedConsoleManager test = new PipedConsoleManager()) {
final List<Object> keys = new ArrayList<>();
keys.add("A choice"); //$NON-NLS-1$
keys.add("An other"); //$NON-NLS-1$
@@ -966,7 +966,7 @@ public class CLIPrompterTest {
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptMultiChoice(fr.bigeon.gclc.manager.ConsoleManager, java.util.Map, java.lang.String)}. */
@Test
public final void testPromptMultiChoiceConsoleManagerMapOfUTString() {
try (final TestConsoleManager test = new TestConsoleManager()) {
try (final PipedConsoleManager test = new PipedConsoleManager()) {
final List<Object> keys = new ArrayList<>();
final Map<Object, Object> choices = new HashMap<>();
keys.add("A choice"); //$NON-NLS-1$
@@ -1049,7 +1049,7 @@ public class CLIPrompterTest {
* {@link fr.bigeon.gclc.prompt.CLIPrompter#promptNonEmpty(fr.bigeon.gclc.manager.ConsoleManager, java.lang.String, java.lang.String)}. */
@Test
public final void testPromptNonEmpty() {
try (final TestConsoleManager test = new TestConsoleManager()) {
try (final PipedConsoleManager test = new PipedConsoleManager()) {
final String res = "some content"; //$NON-NLS-1$
Thread th = new Thread(new Runnable() {

View File

@@ -1,151 +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.
*/
/**
* gclc-test:fr.bigeon.gclc.test.TestConsoleManager.java
* Created on: Nov 18, 2016
*/
package fr.bigeon.gclc.test.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import fr.bigeon.gclc.manager.ConsoleManager;
import fr.bigeon.gclc.manager.SystemConsoleManager;
/** This console manager allows to enter commands and retrieve the output as an
* input.
* <p>
* This console manager is used to internally pilot an application. This can be
* used to test application behavior.
*
* @author Emmanuel Bigeon */
public class TestConsoleManager implements ConsoleManager, AutoCloseable {
private final SystemConsoleManager innerManager;
private final PipedOutputStream commandInput;
private final BufferedReader commandBuffOutput;
private final PipedInputStream commandOutput;
private final PrintStream outPrint;
private final PipedInputStream in;
/** @throws IOException if the piping failed for streams */
public TestConsoleManager() throws IOException {
commandInput = new PipedOutputStream();
in = new PipedInputStream(commandInput);
commandOutput = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream(commandOutput);
commandBuffOutput = new BufferedReader(
new InputStreamReader(commandOutput, Charset.defaultCharset()));
outPrint = new PrintStream(out, true, Charset.defaultCharset().name());
innerManager = new SystemConsoleManager(outPrint, in,
Charset.forName("UTF-8"));
}
@Override
public String getPrompt() {
return innerManager.getPrompt();
}
@Override
public void print(String object) throws IOException {
innerManager.print(object);
}
@Override
public void println() throws IOException {
innerManager.println();
}
@Override
public void println(String object) throws IOException {
innerManager.println(object);
}
@Override
public String prompt() throws IOException {
return innerManager
.prompt(innerManager.getPrompt() + System.lineSeparator());
}
@Override
public String prompt(String message) throws IOException {
return innerManager.prompt(message + System.lineSeparator());
}
@Override
public void setPrompt(String prompt) {
innerManager.setPrompt(prompt);
}
@Override
public void close() throws IOException {
innerManager.close();
outPrint.close();
commandBuffOutput.close();
commandOutput.close();
in.close();
commandInput.close();
}
@Override
public boolean isClosed() {
return innerManager.isClosed();
}
public void type(String content) throws IOException {
ByteBuffer buff = Charset.defaultCharset()
.encode(content + System.lineSeparator());
if (buff.hasArray()) {
commandInput.write(buff.array());
}
}
public String readNextLine() throws IOException {
return commandBuffOutput.readLine();
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.manager.ConsoleManager#interruptPrompt() */
@Override
public void interruptPrompt() {
innerManager.interruptPrompt();
}
}