Compare commits

...

25 Commits

Author SHA1 Message Date
bd311d185e [maven-release-plugin] prepare release gclc-2.0.12 2018-11-25 12:00:41 -05:00
aa26173f26 Update port
Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
2018-11-25 11:37:07 -05:00
df688b1a85 Code factoring
Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
2018-11-25 11:31:52 -05:00
bcd0faceef Fixed runnable to allow acces to pending messages
Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
2018-11-25 11:22:37 -05:00
5e5cc2a1cd Added tests
Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
2018-11-25 11:05:06 -05:00
b24b72f3e2 Fixed console input
Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
2018-11-25 11:05:00 -05:00
ad79e3ccb6 Change to Supplier. Fix tests
Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
2018-11-25 09:16:38 -05:00
ddd818d93b Fix test
Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
2018-11-25 00:40:54 -05:00
6993513f38 Improve exception mechanics. remove closure of stream.
Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
2018-11-25 00:27:30 -05:00
b9a7142c5c Added comments
Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
2018-11-24 23:37:27 -05:00
85ef68f6ae Added error storing in the forwarding runnable. Added tests
Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
2018-11-24 21:07:28 -05:00
dfca63b179 [maven-release-plugin] prepare for next development iteration 2018-10-27 17:17:47 -04:00
787f951ff7 [maven-release-plugin] prepare release process-0.0.5 2018-10-27 17:17:44 -04:00
a364b6442b Added javadoc
Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
2018-10-27 17:16:15 -04:00
211b965e9e [maven-release-plugin] prepare for next development iteration 2018-10-27 16:39:13 -04:00
e1d030c6c5 [maven-release-plugin] prepare release swt-1.1.6 2018-10-27 16:39:10 -04:00
8bc2b13a92 [maven-release-plugin] prepare for next development iteration 2018-10-27 15:58:12 -04:00
c1050b1b4f [maven-release-plugin] prepare release socket-1.1.13 2018-10-27 15:58:09 -04:00
a3e4758d1b [maven-release-plugin] prepare for next development iteration 2018-10-27 14:54:06 -04:00
c436b5af2f [maven-release-plugin] prepare release process-0.0.4 2018-10-27 14:54:03 -04:00
3272261be1 [maven-release-plugin] prepare for next development iteration 2018-10-27 14:42:49 -04:00
f82f0b9f16 [maven-release-plugin] prepare release system-0.0.2 2018-10-27 14:42:41 -04:00
1fa9ca213e Fix process forwarding of
Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
2018-10-27 14:42:22 -04:00
265f90526a Update dependency versions
Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
2018-10-27 14:38:07 -04:00
71b325508f [maven-release-plugin] prepare for next development iteration 2018-10-27 14:27:47 -04:00
31 changed files with 717 additions and 532 deletions

View File

@@ -1,6 +1,4 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<groupId>net.bigeon.config</groupId> <groupId>net.bigeon.config</groupId>
@@ -10,7 +8,7 @@
<groupId>net.bigeon.gclc</groupId> <groupId>net.bigeon.gclc</groupId>
<artifactId>process</artifactId> <artifactId>process</artifactId>
<version>0.0.4-SNAPSHOT</version> <version>0.0.6-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>process</name> <name>process</name>
@@ -51,7 +49,13 @@
<dependency> <dependency>
<groupId>net.bigeon</groupId> <groupId>net.bigeon</groupId>
<artifactId>gclc</artifactId> <artifactId>gclc</artifactId>
<version>2.0.10</version> <version>2.0.11</version>
</dependency>
<dependency>
<groupId>net.bigeon.test</groupId>
<artifactId>junitmt</artifactId>
<version>1.0.2</version>
<scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<reporting> <reporting>

View File

@@ -68,7 +68,6 @@ package net.bigeon.gclc.process.io;
* #L% * #L%
*/ */
import java.io.IOException; import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import net.bigeon.gclc.manager.ConsoleInput; import net.bigeon.gclc.manager.ConsoleInput;
@@ -120,15 +119,18 @@ public final class ConnectingConsoleInput implements ConsoleInput {
public void connect(final ConsoleInput input) { public void connect(final ConsoleInput input) {
disconnect(); disconnect();
synchronized (promptLock) { synchronized (promptLock) {
connected = input;
promptLock.notifyAll(); promptLock.notifyAll();
synchronized (connectionLock) {
connected = input;
connectionLock.notifyAll();
}
} }
} }
/** Disconnect the current input. */ /** Disconnect the current input. */
public void disconnect() { public void disconnect() {
synchronized (promptLock) { synchronized (connectionLock) {
synchronized (connectionLock) { synchronized (promptLock) {
if (connected != null) { if (connected != null) {
disconnection = true; disconnection = true;
connected.interruptPrompt(); connected.interruptPrompt();
@@ -149,12 +151,15 @@ public final class ConnectingConsoleInput implements ConsoleInput {
* @see fr.bigeon.gclc.manager.ConsoleInput#interruptPrompt() */ * @see fr.bigeon.gclc.manager.ConsoleInput#interruptPrompt() */
@Override @Override
public void interruptPrompt() { public void interruptPrompt() {
synchronized (promptLock) { synchronized (connectionLock) {
prompting = false; synchronized (promptLock) {
if (connected != null) { connectionLock.notifyAll();
connected.interruptPrompt(); if (connected != null) {
connected.interruptPrompt();
}
prompting = false;
promptLock.notifyAll();
} }
promptLock.notifyAll();
} }
} }
@@ -191,27 +196,23 @@ public final class ConnectingConsoleInput implements ConsoleInput {
if (!prompting) { if (!prompting) {
return null; return null;
} }
boolean connect; }
synchronized (connectionLock) { getConnection(0);
connect = connected != null; boolean connect;
} ConsoleInput actualConnected;
if (!connect) { synchronized (connectionLock) {
try { connect = connected != null;
promptLock.wait(); actualConnected = connected;
} catch (final InterruptedException e) { }
LOGGER.log(Level.WARNING, "Inerruption of console thread", e); if (connect) {
Thread.currentThread().interrupt(); final String res = actualConnected.prompt(message);
} synchronized (promptLock) {
} else { if (disconnection) {
synchronized (connectionLock) { disconnection = false;
final String res = connected.prompt(message); } else if (prompting) {
if (disconnection) { return res;
disconnection = false; } else {
} else if (prompting) { // prompt interrupted, lose the result.
return res;
} else {
// prompt interrupted, lose the result.
}
} }
} }
} }
@@ -232,35 +233,46 @@ public final class ConnectingConsoleInput implements ConsoleInput {
do { do {
synchronized (promptLock) { synchronized (promptLock) {
if (!prompting) { if (!prompting) {
return null; break;
} }
boolean connect; }
synchronized (connectionLock) { getConnection(timeout);
connect = connected != null; boolean connect;
} ConsoleInput actualConnected;
if (!connect) { synchronized (connectionLock) {
try { connect = connected != null;
promptLock.wait(timeout); actualConnected = connected;
} catch (final InterruptedException e) { }
LOGGER.log(Level.WARNING, "Inerruption of console thread", e); if (connect) {
Thread.currentThread().interrupt(); synchronized (promptLock) {
} final String res = actualConnected.prompt(message,
} else { end - System.currentTimeMillis());
synchronized (connectionLock) { if (disconnection) {
final String res = connected.prompt(message, disconnection = false;
end - System.currentTimeMillis()); } else if (prompting) {
if (disconnection) { return res;
disconnection = false; } else {
} else if (prompting) { // prompt interrupted, lose the result.
return res;
} else {
// prompt interrupted, lose the result.
}
} }
} }
} }
} while (System.currentTimeMillis() < end); } while (System.currentTimeMillis() < end);
return null; return null;
}
private void getConnection(final long timeout) {
boolean connect;
synchronized (connectionLock) {
connect = connected != null;
if (!connect) {
try {
connectionLock.wait(timeout);
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
} }
/* (non-Javadoc) /* (non-Javadoc)

View File

@@ -37,7 +37,7 @@ public class CommandForkTest {
* {@link net.bigeon.gclc.process.CommandFork#execute(net.bigeon.gclc.manager.ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, java.lang.String[])}. * {@link net.bigeon.gclc.process.CommandFork#execute(net.bigeon.gclc.manager.ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, java.lang.String[])}.
* *
* @throws InvalidCommandName if the test init failed * @throws InvalidCommandName if the test init failed
* @throws CommandRunException */ * @throws CommandRunException if the command execution failed in the test */
@Test @Test
public void testExecute() throws InvalidCommandName, CommandRunException { public void testExecute() throws InvalidCommandName, CommandRunException {
final TaskPool pool = new TaskPool(false); final TaskPool pool = new TaskPool(false);

View File

@@ -0,0 +1,34 @@
/**
*
*/
package net.bigeon.gclc.process;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import net.bigeon.gclc.command.SubedCommand;
import net.bigeon.gclc.exception.InvalidCommandName;
/**
* @author Emmanuel Bigeon
*
*/
public class ProcessAttachementTest {
/** Test method for
* {@link net.bigeon.gclc.process.ProcessAttachement#attach(net.bigeon.gclc.command.ICommandProvider)}.
*
* @throws InvalidCommandName if the command has already some subcommand that is
* incompatible */
@Test
public void testAttach() throws InvalidCommandName {
final TaskPool pool = new TaskPool();
final ProcessAttachement attachement = new ProcessAttachement(pool);
final SubedCommand testPoint = new SubedCommand("test");
attachement.attach(testPoint);
assertNotNull("Commands should be added", testPoint.get("kill"));
assertNotNull("Commands should be added", testPoint.get("list"));
}
}

View File

@@ -7,8 +7,6 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import java.io.IOException;
import org.junit.Test; import org.junit.Test;
import net.bigeon.gclc.exception.CommandRunException; import net.bigeon.gclc.exception.CommandRunException;
@@ -30,8 +28,7 @@ public class ProcessKillTest {
/** Test method for /** Test method for
* {@link net.bigeon.gclc.process.ProcessKill#execute(net.bigeon.gclc.manager.ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, java.lang.String[])}. * {@link net.bigeon.gclc.process.ProcessKill#execute(net.bigeon.gclc.manager.ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, java.lang.String[])}.
* *
* @throws CommandRunException if an error occured in test * @throws CommandRunException if an error occured in test */
* @throws IOException if an error occured with the console output */
@Test @Test
public void testExecute() throws CommandRunException { public void testExecute() throws CommandRunException {
final TaskPool pool = new TaskPool(); final TaskPool pool = new TaskPool();

View File

@@ -0,0 +1,33 @@
/**
*
*/
package net.bigeon.gclc.process;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import net.bigeon.gclc.command.SubedCommand;
import net.bigeon.gclc.exception.InvalidCommandName;
/** @author Emmanuel Bigeon */
public class ScreenAttachementTest {
/** Test method for
* {@link net.bigeon.gclc.process.ScreenAttachement#attach(net.bigeon.gclc.command.ICommandProvider)}.
*
* @throws InvalidCommandName if an error occured in attachement */
@Test
public void testAttach() throws InvalidCommandName {
final TaskPool pool = new TaskPool();
final ScreenAttachement attachement = new ScreenAttachement(pool, 15);
final SubedCommand testPoint = new SubedCommand("test");
attachement.attach(testPoint);
assertNotNull("Commands should be added", testPoint.get("terminate"));
assertNotNull("Commands should be added", testPoint.get("list"));
assertNotNull("Commands should be added", testPoint.get("clear"));
assertNotNull("Commands should be added", testPoint.get("fg"));
assertNotNull("Commands should be added", testPoint.get("fork"));
}
}

View File

@@ -0,0 +1,80 @@
/**
*
*/
package net.bigeon.gclc.process;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.junit.Test;
import net.bigeon.gclc.exception.CommandRunException;
import net.bigeon.gclc.manager.ConsoleInput;
import net.bigeon.gclc.manager.ConsoleOutput;
/** @author Emmanuel Bigeon */
public class TaskSpawnerTest {
/** Test method for
* {@link net.bigeon.gclc.process.TaskSpawner#execute(net.bigeon.gclc.manager.ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, java.lang.String[])}.
*
* @throws CommandRunException if an error occurred */
@Test
public void testExecute() throws CommandRunException {
final Task task = new Task() {
@Override
public void run() {
//
}
@Override
public void setRunning(final boolean running) {
//
}
@Override
public void rmInterruptionListener(final InterruptionListener listener) {
//
}
@Override
public boolean isRunning() {
return false;
}
@Override
public String getName() {
return "abc";
}
@Override
public void addInterruptionListener(final InterruptionListener listener) {
//
}
};
final TaskPool pool = new TaskPool();
assertTrue("Pool not empty", pool.getPIDs().isEmpty());
final ExecutorService service = Executors.newSingleThreadExecutor();
final TaskSpawner spawner = new TaskSpawner("name", pool, service) {
@Override
public String tip() {
return "tip";
}
@Override
protected String usageDetail() {
return "no details";
}
@Override
protected Task createTask(final ConsoleOutput out, final ConsoleInput in,
final String... args) throws CommandRunException {
return task;
}
};
spawner.execute(null, null);
assertEquals("Added the task", 1, pool.getPIDs().size());
final Integer id = pool.getPIDs().iterator().next();
assertEquals("Added task is not the one", task, pool.get(id));
}
}

View File

@@ -6,11 +6,20 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.io.IOException; import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.Test; import org.junit.Test;
import net.bigeon.gclc.tools.ConstantString; import net.bigeon.gclc.tools.ConstantString;
import net.bigeon.gclc.utils.PipedConsoleInput; import net.bigeon.gclc.utils.PipedConsoleInput;
import net.bigeon.gclc.utils.StreamConsoleInput;
import net.bigeon.test.junitmt.ATestRunnable;
import net.bigeon.test.junitmt.FunctionalTestRunnable;
import net.bigeon.test.junitmt.TestFunction;
import net.bigeon.test.junitmt.ThreadTest;
public class ConnectingConsoleInputTest { public class ConnectingConsoleInputTest {
@@ -62,4 +71,67 @@ public class ConnectingConsoleInputTest {
in.setPrompt(prompt); in.setPrompt(prompt);
assertEquals("Prompt should be set correctly", prompt, in.getPrompt()); assertEquals("Prompt should be set correctly", prompt, in.getPrompt());
} }
@Test
public void testPromptSequence() throws IOException, InterruptedException {
final ConnectingConsoleInput in = new ConnectingConsoleInput();
// Unconnected
final AtomicBoolean ended = new AtomicBoolean(false);
final TestFunction one = new TestFunction() {
@Override
public void apply() throws Exception {
assertNull("Interrupted should return null", in.prompt("m1", -1));
assertNull("Interrupted should return null", in.prompt("m2", 5000));
ended.set(true);
assertNull("Overtime should return null", in.prompt("m3", 200));
}
};
final ATestRunnable runnable = new FunctionalTestRunnable(one);
final Thread th = new Thread(runnable);
final Thread inter = new Thread(new Runnable() {
@Override
public void run() {
while (!ended.get()) {
try {
th.join(500);
} catch (final InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
in.interruptPrompt();
}
}
});
th.start();
inter.start();
ThreadTest.assertRuns(th, runnable);
inter.join();
ended.set(false);
final PipedOutputStream os = new PipedOutputStream();
final PipedInputStream pis = new PipedInputStream(os);
in.connect(new StreamConsoleInput(System.out, pis, StandardCharsets.UTF_8));
final ATestRunnable runnable2 = new FunctionalTestRunnable(one);
final Thread th2 = new Thread(runnable2);
final Thread inter2 = new Thread(new Runnable() {
@Override
public void run() {
while (!ended.get()) {
try {
th2.join(500);
} catch (final InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
in.interruptPrompt();
}
}
});
th2.start();
inter2.start();
ThreadTest.assertRuns(th2, runnable2);
}
} }

View File

@@ -15,7 +15,7 @@ public class ForkTaskMock extends ForkTask implements Task {
private int runCall = 0; private int runCall = 0;
/** @param lines */ /** Default constructor. */
public ForkTaskMock() { public ForkTaskMock() {
super(10); super(10);
} }

View File

@@ -49,8 +49,8 @@ public class TaskMock implements Task {
setRunningCall = 0; setRunningCall = 0;
} }
/** @return */ /** @return the number of calls to set running method */
public Object getNumberSetRunning() { public int getNumberSetRunning() {
return setRunningCall; return setRunningCall;
} }

View File

@@ -7,7 +7,7 @@
</parent> </parent>
<groupId>net.bigeon.gclc</groupId> <groupId>net.bigeon.gclc</groupId>
<artifactId>socket</artifactId> <artifactId>socket</artifactId>
<version>1.1.13-SNAPSHOT</version> <version>1.1.14-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>GCLC Socket</name> <name>GCLC Socket</name>
<description>Socket implementation of GCLC</description> <description>Socket implementation of GCLC</description>
@@ -47,7 +47,7 @@
<dependency> <dependency>
<groupId>net.bigeon</groupId> <groupId>net.bigeon</groupId>
<artifactId>gclc</artifactId> <artifactId>gclc</artifactId>
<version>2.0.10</version> <version>2.0.11</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>net.bigeon</groupId> <groupId>net.bigeon</groupId>

View File

@@ -77,7 +77,7 @@ public class SocketConsoleApplicationTest {
Thread server; Thread server;
server = TestServer.getServer(); server = TestServer.getServer();
final String hostName = "127.0.0.1"; final String hostName = "127.0.0.1";
final int portNumber = 3300; final int portNumber = 12345;
try (Socket kkSocket = new Socket(hostName, portNumber); try (Socket kkSocket = new Socket(hostName, portNumber);
PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true); PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true);

View File

@@ -1,6 +1,4 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<groupId>net.bigeon.config</groupId> <groupId>net.bigeon.config</groupId>
@@ -9,7 +7,7 @@
</parent> </parent>
<groupId>net.bigeon.gclc</groupId> <groupId>net.bigeon.gclc</groupId>
<artifactId>swt</artifactId> <artifactId>swt</artifactId>
<version>1.1.6-SNAPSHOT</version> <version>1.1.7-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>GCLC swt</name> <name>GCLC swt</name>
<description>A swt window for console applications</description> <description>A swt window for console applications</description>
@@ -50,12 +48,12 @@
<dependency> <dependency>
<groupId>net.bigeon</groupId> <groupId>net.bigeon</groupId>
<artifactId>gclc</artifactId> <artifactId>gclc</artifactId>
<version>2.0.10</version> <version>2.0.11</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>net.bigeon</groupId> <groupId>net.bigeon</groupId>
<artifactId>collections</artifactId> <artifactId>collections</artifactId>
<version>1.1.5</version> <version>1.1.6</version>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@@ -1,6 +1,4 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<groupId>net.bigeon.config</groupId> <groupId>net.bigeon.config</groupId>
@@ -9,7 +7,7 @@
</parent> </parent>
<groupId>net.bigeon.gclc</groupId> <groupId>net.bigeon.gclc</groupId>
<artifactId>system</artifactId> <artifactId>system</artifactId>
<version>0.0.2-SNAPSHOT</version> <version>0.0.3-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>GCLC system command</name> <name>GCLC system command</name>
<description>Provide an exec command to execute system commands</description> <description>Provide an exec command to execute system commands</description>
@@ -50,7 +48,13 @@
<dependency> <dependency>
<groupId>net.bigeon</groupId> <groupId>net.bigeon</groupId>
<artifactId>gclc</artifactId> <artifactId>gclc</artifactId>
<version>2.0.9</version> <version>2.0.11</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.23.0</version>
<scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@@ -101,7 +101,10 @@ public class ExecSystemCommand extends Command {
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os))) { try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os))) {
while (th.isAlive()) { while (th.isAlive()) {
final String user = in.prompt(); final String user = in.prompt();
writer.write(user + EOL); // Avoid interruption being sent to process.
if (!user.isEmpty()) {
writer.write(user + EOL);
}
} }
} catch (final IOException e1) { } catch (final IOException e1) {
throw new CommandRunException(CommandRunExceptionType.INTERACTION, throw new CommandRunException(CommandRunExceptionType.INTERACTION,

View File

@@ -63,6 +63,7 @@ public final class ForwardingRunnable implements Runnable {
private final ConsoleOutput out; private final ConsoleOutput out;
/** The input stream. */ /** The input stream. */
private final InputStream is; private final InputStream is;
private Exception error;
/** Create the runnable. /** Create the runnable.
* *
@@ -78,22 +79,23 @@ public final class ForwardingRunnable implements Runnable {
@Override @Override
public void run() { public void run() {
try { try {
readToEnd(out, is); readToEnd();
is.close();
} catch (final CommandRunException e) { } catch (final CommandRunException e) {
LOGGER.log(Level.WARNING, "Manager was closed in the meantime...", e); //$NON-NLS-1$ LOGGER.log(Level.WARNING, "Manager was closed in the meantime...", e); //$NON-NLS-1$
} catch (final IOException e) { setError(e);
LOGGER.log(Level.WARNING, "Input stream was closed...", e); //$NON-NLS-1$
} }
} }
/** Read the input until its end. /** Read the input until its end.
* <p>
* This method actually perform a loop on the input stream to read the next
* character and then print int on the screen.
* <p>
* When the stream returns a negative value, it will exit its loop and return.
* *
* @param is the input stream
* @throws CommandRunException if the manager was closed while writing the * @throws CommandRunException if the manager was closed while writing the
* stream */ * stream */
private static void readToEnd(final ConsoleOutput out, final InputStream is) private void readToEnd() throws CommandRunException {
throws CommandRunException {
int c; int c;
try { try {
while ((c = is.read()) >= 0) { while ((c = is.read()) >= 0) {
@@ -102,6 +104,7 @@ public final class ForwardingRunnable implements Runnable {
} }
} catch (final IOException e) { } catch (final IOException e) {
LOGGER.log(Level.INFO, "input stream reading failed", e); //$NON-NLS-1$ LOGGER.log(Level.INFO, "input stream reading failed", e); //$NON-NLS-1$
setError(e);
} }
} }
@@ -141,4 +144,22 @@ public final class ForwardingRunnable implements Runnable {
public static Thread connect(final InputStream stream, final ConsoleOutput output) { public static Thread connect(final InputStream stream, final ConsoleOutput output) {
return connect(stream, output, "Forwarding"); return connect(stream, output, "Forwarding");
} }
/** Get the error in this runnable execution if any.
* <p>
* In the current implementation, this error can be an {@link IOException} if
* the input stream failed or a {@link CommandRunException} if the output was
* closed.
*
* @return the error */
public synchronized Exception getError() {
return error;
}
/** Set the error that caused this runnable to stop executing.
*
* @param error the error to set */
private synchronized void setError(final Exception error) {
this.error = error;
}
} }

View File

@@ -59,7 +59,7 @@ public class ExecSystemCommandTest {
/** Test the execution of the command. /** Test the execution of the command.
* *
* @throws CommandRunException */ * @throws CommandRunException if the command fails */
@Test @Test
public void testExecute() throws CommandRunException { public void testExecute() throws CommandRunException {
final ConsoleOutput out = SinkOutput.INSTANCE; final ConsoleOutput out = SinkOutput.INSTANCE;

View File

@@ -0,0 +1,44 @@
/**
*
*/
package net.bigeon.gclc.system;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.junit.Test;
import org.mockito.Mockito;
import net.bigeon.gclc.exception.CommandRunException;
import net.bigeon.gclc.utils.SinkOutput;
import net.bigeon.gclc.utils.StreamConsoleOutput;
/** @author Emmanuel Bigeon */
public class ForwardingRunnableTest {
public void testClosedOutput() {
final StreamConsoleOutput out = new StreamConsoleOutput();
out.close();
final InputStream is = new ByteArrayInputStream(new byte[] { 1, 2, 3 });
final ForwardingRunnable runnable = new ForwardingRunnable(out, is);
// Runnable should close immediatly.
runnable.run();
assertTrue("Error should be a CommandRunException",
runnable.getError() instanceof CommandRunException);
}
@Test
public void testIOInInput() throws IOException {
final InputStream is = Mockito.mock(InputStream.class);
Mockito.when(is.read()).thenThrow(new IOException());
final ForwardingRunnable runnable = new ForwardingRunnable(SinkOutput.INSTANCE,
is);
// Runnable should close immediatly.
runnable.run();
assertTrue("Error should be an IO", runnable.getError() instanceof IOException);
}
}

View File

@@ -8,7 +8,7 @@
</parent> </parent>
<groupId>net.bigeon</groupId> <groupId>net.bigeon</groupId>
<artifactId>gclc</artifactId> <artifactId>gclc</artifactId>
<version>2.0.11</version> <version>2.0.12</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>Generic Command Ligne console</name> <name>Generic Command Ligne console</name>
<description>A generic framework for console applications, with customized command input and output streams.</description> <description>A generic framework for console applications, with customized command input and output streams.</description>
@@ -37,7 +37,7 @@
</developers> </developers>
<scm> <scm>
<developerConnection>scm:git:gogs@git.code.bigeon.net:emmanuel/gclc.git</developerConnection> <developerConnection>scm:git:gogs@git.code.bigeon.net:emmanuel/gclc.git</developerConnection>
<tag>gclc-2.0.11</tag> <tag>gclc-2.0.12</tag>
</scm> </scm>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@@ -49,7 +49,7 @@
<dependency> <dependency>
<groupId>net.bigeon.test</groupId> <groupId>net.bigeon.test</groupId>
<artifactId>junitmt</artifactId> <artifactId>junitmt</artifactId>
<version>1.0.0</version> <version>1.0.2</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@@ -73,8 +73,7 @@ package net.bigeon.gclc.manager;
*/ */
import java.io.IOException; import java.io.IOException;
import java.io.InterruptedIOException; import java.io.InterruptedIOException;
import java.util.function.Supplier;
import net.bigeon.gclc.tools.StringProvider;
/** A console application input. /** A console application input.
* *
@@ -90,7 +89,7 @@ public interface ConsoleInput extends AutoCloseable {
/** Get the prompt string. /** Get the prompt string.
* *
* @return the prompt prefix */ * @return the prompt prefix */
StringProvider getPrompt(); Supplier<String> getPrompt();
/** Indicate to the input that is should interrompt the prompting, if possible. /** Indicate to the input that is should interrompt the prompting, if possible.
* <p> * <p>
@@ -148,5 +147,5 @@ public interface ConsoleInput extends AutoCloseable {
/** Set the prompting string. /** Set the prompting string.
* *
* @param string the prompt 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; package net.bigeon.gclc.tools;
import java.util.Objects;
import java.util.function.Supplier;
/*- /*-
* #%L * #%L
* Generic Command Ligne console * Generic Command Ligne console
@@ -68,10 +71,10 @@ package net.bigeon.gclc.tools;
* #L% * #L%
*/ */
/** A {@link StringProvider} that hold a constant string. /** A supplier of string that hold a constant string.
* *
* @author Emmanuel Bigeon */ * @author Emmanuel Bigeon */
public class ConstantString implements StringProvider { public class ConstantString implements Supplier<String> {
private final String string; private final String string;
/** Create a provider for a string. /** Create a provider for a string.
@@ -81,10 +84,8 @@ public class ConstantString implements StringProvider {
this.string = string; this.string = string;
} }
/* (non-Javadoc)
* @see net.bigeon.gclc.tools.StringProvider#apply() */
@Override @Override
public String apply() { public String get() {
return string; return string;
} }
@@ -92,10 +93,7 @@ public class ConstantString implements StringProvider {
* @see java.lang.Object#hashCode() */ * @see java.lang.Object#hashCode() */
@Override @Override
public int hashCode() { public int hashCode() {
final int prime = 31; return Objects.hash(string);
int result = 1;
result = prime * result + ((string == null) ? 0 : string.hashCode());
return result;
} }
/* (non-Javadoc) /* (non-Javadoc)
@@ -112,13 +110,6 @@ public class ConstantString implements StringProvider {
return false; return false;
} }
final ConstantString other = (ConstantString) obj; final ConstantString other = (ConstantString) obj;
if (string == null) { return Objects.equals(string, other.string);
if (other.string != null) {
return false;
}
} else if (!string.equals(other.string)) {
return false;
}
return true;
} }
} }

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; package net.bigeon.gclc.utils;
import java.util.function.Supplier;
import net.bigeon.gclc.manager.ConsoleInput; 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.ConstantString;
import net.bigeon.gclc.tools.StringProvider;
/** A console input that return empty to all prompting. /** A console input that return empty to all prompting.
* *
@@ -101,8 +102,8 @@ public final class EmptyInput implements ConsoleInput {
/* (non-Javadoc) /* (non-Javadoc)
* @see net.bigeon.gclc.manager.ConsoleInput#getPrompt() */ * @see net.bigeon.gclc.manager.ConsoleInput#getPrompt() */
@Override @Override
public StringProvider getPrompt() { public Supplier<String> getPrompt() {
return CONSTANT_STRING; //$NON-NLS-1$ return CONSTANT_STRING;
} }
/* (non-Javadoc) /* (non-Javadoc)
@@ -155,7 +156,7 @@ public final class EmptyInput implements ConsoleInput {
} }
@Override @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.PipedOutputStream;
import java.io.PrintStream; import java.io.PrintStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.function.Supplier;
import net.bigeon.gclc.manager.ConsoleInput; 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 /** This console input allows to enter commands and retrieve the output as an
* input. * input.
@@ -120,7 +120,7 @@ public final class PipedConsoleInput implements ConsoleInput {
} }
@Override @Override
public StringProvider getPrompt() { public Supplier<String> getPrompt() {
return innerManager.getPrompt(); return innerManager.getPrompt();
} }
@@ -174,7 +174,7 @@ public final class PipedConsoleInput implements ConsoleInput {
} }
@Override @Override
public void setPrompt(StringProvider string) { public void setPrompt(final Supplier<String> string) {
innerManager.setPrompt(string); innerManager.setPrompt(string);
} }

View File

@@ -145,6 +145,9 @@ public final class ReadingRunnable implements Runnable {
* @throws IOException if the pipe is closed */ * @throws IOException if the pipe is closed */
public String getMessage() throws IOException { public String getMessage() throws IOException {
synchronized (lock) { synchronized (lock) {
if (!messages.isEmpty()) {
return messages.poll();
}
if (!running) { if (!running) {
throw new IOException(CLOSED_PIPE); throw new IOException(CLOSED_PIPE);
} }
@@ -164,6 +167,9 @@ public final class ReadingRunnable implements Runnable {
* @throws IOException if the input was closed */ * @throws IOException if the input was closed */
public String getNextMessage(final long timeout) throws IOException { public String getNextMessage(final long timeout) throws IOException {
synchronized (lock) { synchronized (lock) {
if (!messages.isEmpty()) {
return messages.poll();
}
if (!running) { if (!running) {
throw new IOException(CLOSED_PIPE); throw new IOException(CLOSED_PIPE);
} }

View File

@@ -77,10 +77,10 @@ import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.PrintStream; import java.io.PrintStream;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.function.Supplier;
import net.bigeon.gclc.manager.ConsoleInput; import net.bigeon.gclc.manager.ConsoleInput;
import net.bigeon.gclc.tools.ConstantString; import net.bigeon.gclc.tools.ConstantString;
import net.bigeon.gclc.tools.StringProvider;
/** A console using the input stream and print stream. /** A console using the input stream and print stream.
* <p> * <p>
@@ -90,10 +90,10 @@ import net.bigeon.gclc.tools.StringProvider;
public final class StreamConsoleInput implements ConsoleInput { public final class StreamConsoleInput implements ConsoleInput {
/** The default prompt. */ /** 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. */ /** The command prompt. It can be changed. */
private StringProvider prompt = DEFAULT_PROMPT; private Supplier<String> prompt = DEFAULT_PROMPT;
/** The print stream. */ /** The print stream. */
private final PrintStream out; private final PrintStream out;
@@ -152,7 +152,7 @@ public final class StreamConsoleInput implements ConsoleInput {
/* (non-Javadoc) /* (non-Javadoc)
* @see net.bigeon.gclc.manager.ConsoleInput#getPrompt() */ * @see net.bigeon.gclc.manager.ConsoleInput#getPrompt() */
@Override @Override
public StringProvider getPrompt() { public Supplier<String> getPrompt() {
return prompt; return prompt;
} }
@@ -175,14 +175,14 @@ public final class StreamConsoleInput implements ConsoleInput {
* @see net.bigeon.gclc.ConsoleManager#prompt() */ * @see net.bigeon.gclc.ConsoleManager#prompt() */
@Override @Override
public String prompt() throws IOException { public String prompt() throws IOException {
return prompt(prompt.apply()); return prompt(prompt.get());
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see net.bigeon.gclc.manager.ConsoleManager#prompt(long) */ * @see net.bigeon.gclc.manager.ConsoleManager#prompt(long) */
@Override @Override
public String prompt(final long timeout) throws IOException { public String prompt(final long timeout) throws IOException {
return prompt(prompt.apply(), timeout); return prompt(prompt.get(), timeout);
} }
/* (non-Javadoc) /* (non-Javadoc)
@@ -217,7 +217,7 @@ public final class StreamConsoleInput implements ConsoleInput {
} }
@Override @Override
public void setPrompt(StringProvider string) { public void setPrompt(final Supplier<String> string) {
prompt = 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.PipedConsoleInput;
import net.bigeon.gclc.utils.PipedConsoleOutput; import net.bigeon.gclc.utils.PipedConsoleOutput;
import net.bigeon.test.junitmt.ATestRunnable; import net.bigeon.test.junitmt.ATestRunnable;
import net.bigeon.test.junitmt.FunctionalTestRunnable;
import net.bigeon.test.junitmt.TestFunction;
import net.bigeon.test.junitmt.ThreadTest; 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);
cmd.execute(out, in, "-" + str1, str2, "-" + addParam, addParam); cmd.execute(out, in, "-" + str1, str2, "-" + addParam, addParam);
final ATestRunnable testConsole = new ATestRunnable() { final TestFunction function = new TestFunction() {
@Override @Override
protected void testRun() { public void apply() throws Exception {
try { assertEquals("value of " + str1 + "? ", buf.readLine());
assertEquals("value of " + str1 + "? ", buf.readLine()); in.type("");
in.type(""); assertEquals("value of " + str1 + "? (cannot be empty) ",
assertEquals("value of " + str1 + "? (cannot be empty) ", buf.readLine());
buf.readLine()); in.type("");
in.type(""); assertEquals("value of " + str1 + "? (cannot be empty) ",
assertEquals("value of " + str1 + "? (cannot be empty) ", buf.readLine());
buf.readLine()); in.type(str2);
in.type(str2);
} catch (final IOException e) {
fail("IO exception in test");
}
} }
}; };
final ATestRunnable testConsole = new FunctionalTestRunnable(function);
final Thread th = new Thread(testConsole); final Thread th = new Thread(testConsole);
th.start(); th.start();
@@ -544,18 +543,15 @@ public class ParametrizedCommandTest {
new InputStreamReader(pis, StandardCharsets.UTF_8)); new InputStreamReader(pis, StandardCharsets.UTF_8));
PipedConsoleInput in = new PipedConsoleInput(new PrintStream(pout))) { PipedConsoleInput in = new PipedConsoleInput(new PrintStream(pout))) {
final ATestRunnable testConsole = new ATestRunnable() { final TestFunction function = new TestFunction() {
@Override @Override
protected void testRun() { public void apply() throws Exception {
try { assertEquals("value of " + str1 + "? ", buf.readLine());
assertEquals("value of " + str1 + "? ", buf.readLine()); in.type(str2);
in.type(str2);
} catch (final IOException e) {
fail("IO exception in test");
}
} }
}; };
final ATestRunnable testConsole = new FunctionalTestRunnable(function);
final Thread th = new Thread(testConsole); final Thread th = new Thread(testConsole);
th.start(); th.start();

View File

@@ -38,6 +38,7 @@
*/ */
package net.bigeon.gclc.manager; package net.bigeon.gclc.manager;
import static org.junit.Assert.assertEquals;
/*- /*-
* #%L * #%L
* Generic Command Ligne console * Generic Command Ligne console
@@ -75,12 +76,15 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PipedInputStream; import java.io.PipedInputStream;
import java.io.PipedOutputStream; import java.io.PipedOutputStream;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@@ -131,13 +135,13 @@ public class ReadingRunnableTest {
final ReadingRunnable runnable = new ReadingRunnable(reader); final ReadingRunnable runnable = new ReadingRunnable(reader);
final Thread th0 = new Thread(runnable, "read"); final Thread th0 = new Thread(runnable, "read");
th0.start(); th0.start();
Thread th = new Thread(new Runnable() { final Thread th = new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
runnable.waitForDelivery("msg"); runnable.waitForDelivery("msg");
} catch (InterruptedException e) { } catch (final InterruptedException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
@@ -186,4 +190,36 @@ public class ReadingRunnableTest {
} }
} }
/** Test method for {@link net.bigeon.gclc.utils.ReadingRunnable#hasMessage()}.
*
* @throws IOException if an error occurred
* @throws InterruptedException if an error occured in the reading thread */
@Test
public final void testGetPendingMessages() throws IOException, InterruptedException {
final PipedOutputStream out = new PipedOutputStream();
final BufferedReader reader = new BufferedReader(new InputStreamReader(new PipedInputStream(out), StandardCharsets.UTF_8));
final ReadingRunnable runnable = new ReadingRunnable(reader);
final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8));
writer.write("one");
writer.newLine();
writer.write("two");
writer.close();
out.close();
final Thread th = new Thread(runnable);
th.start();
th.join();
assertEquals("Pending messages should be retrievable", "one",
runnable.getMessage());
assertEquals("Pending messages should be retrievable", "two",
runnable.getMessage());
try {
runnable.getMessage();
fail("reading from closed runnable");
} catch (final IOException e) {
// ok
}
}
} }

View File

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

View File

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