Compare commits
25 Commits
socket-1.1
...
socket-1.1
| Author | SHA1 | Date | |
|---|---|---|---|
| c1050b1b4f | |||
| a3e4758d1b | |||
| c436b5af2f | |||
| 3272261be1 | |||
| f82f0b9f16 | |||
| 1fa9ca213e | |||
| 265f90526a | |||
| 71b325508f | |||
| 98857e425a | |||
| 8c6b1d8884 | |||
| 2972e82f4a | |||
| 0ab39b24f6 | |||
| 171f79518d | |||
| 0e422a81ce | |||
| ea4164fbfb | |||
| e2f45c77d9 | |||
| 55bb54ca43 | |||
| cf29eb37cc | |||
| 579e85dcb3 | |||
| 7e36a378c4 | |||
| 80242c79e8 | |||
| ab9a4c474b | |||
| b8e5ea0b78 | |||
| 2637d99bce | |||
| cfd3450afe |
@@ -1,38 +1,4 @@
|
|||||||
|
<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">
|
||||||
<!-- process, Distribution repositories and basic setup for Emmanuel Bigeon
|
|
||||||
projects -->
|
|
||||||
<!-- Copyright (C) 2014-2018 E. Bigeon -->
|
|
||||||
<!-- mailto:emmanuel@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. -->
|
|
||||||
|
|
||||||
<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">
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>net.bigeon.config</groupId>
|
<groupId>net.bigeon.config</groupId>
|
||||||
@@ -42,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.5-SNAPSHOT</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>process</name>
|
<name>process</name>
|
||||||
@@ -83,11 +49,16 @@
|
|||||||
<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>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<reporting>
|
<reporting>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-javadoc-plugin</artifactId>
|
||||||
|
<version>2.10.3</version>
|
||||||
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>com.github.sevntu-checkstyle</groupId>
|
<groupId>com.github.sevntu-checkstyle</groupId>
|
||||||
<artifactId>dsm-maven-plugin</artifactId>
|
<artifactId>dsm-maven-plugin</artifactId>
|
||||||
|
|||||||
@@ -132,16 +132,23 @@ public class CommandForeground extends ParametrizedCommand {
|
|||||||
protected void doExecute(final ConsoleOutput out, final ConsoleInput in,
|
protected void doExecute(final ConsoleOutput out, final ConsoleInput in,
|
||||||
final CommandParameters parameters) throws CommandRunException {
|
final CommandParameters parameters) throws CommandRunException {
|
||||||
String string = parameters.get("pid");
|
String string = parameters.get("pid");
|
||||||
final List<String> additionals = parameters.getAdditionals();
|
if (string == null) {
|
||||||
if (string == null && !additionals.isEmpty()) {
|
final List<String> additionals = parameters.getAdditionals();
|
||||||
|
if (additionals.isEmpty()) {
|
||||||
|
throw new CommandRunException(CommandRunExceptionType.USAGE,
|
||||||
|
"Missing process id");
|
||||||
|
}
|
||||||
string = additionals.get(0);
|
string = additionals.get(0);
|
||||||
}
|
}
|
||||||
if (string == null) {
|
Integer pid;
|
||||||
|
try {
|
||||||
|
pid = Integer.valueOf(string);
|
||||||
|
} catch (final NumberFormatException e) {
|
||||||
throw new CommandRunException(CommandRunExceptionType.USAGE,
|
throw new CommandRunException(CommandRunExceptionType.USAGE,
|
||||||
"Missing process id");
|
"PID should be an integer");
|
||||||
}
|
}
|
||||||
// Join the command.
|
// Join the command.
|
||||||
final Task cmd = pool.get(string);
|
final Task cmd = pool.get(pid);
|
||||||
if (!(cmd instanceof ForkTask)) {
|
if (!(cmd instanceof ForkTask)) {
|
||||||
throw new CommandRunException("No such forked process");
|
throw new CommandRunException("No such forked process");
|
||||||
}
|
}
|
||||||
@@ -151,7 +158,8 @@ public class CommandForeground extends ParametrizedCommand {
|
|||||||
delai = Long.parseLong(delaiOpt) * MILLIS_IN_A_SEC;
|
delai = Long.parseLong(delaiOpt) * MILLIS_IN_A_SEC;
|
||||||
}
|
}
|
||||||
if (delai < 0) {
|
if (delai < 0) {
|
||||||
throw new CommandRunException("Join delai cannot be negative");
|
throw new CommandRunException(CommandRunExceptionType.USAGE,
|
||||||
|
"Join delai cannot be negative");
|
||||||
}
|
}
|
||||||
((ForkTask) cmd).join(out, in, delai);
|
((ForkTask) cmd).join(out, in, delai);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
*/
|
*/
|
||||||
package net.bigeon.gclc.process;
|
package net.bigeon.gclc.process;
|
||||||
|
|
||||||
|
import java.text.MessageFormat;
|
||||||
/*-
|
/*-
|
||||||
* #%L
|
* #%L
|
||||||
* process
|
* process
|
||||||
@@ -43,6 +44,7 @@ import net.bigeon.gclc.command.Command;
|
|||||||
import net.bigeon.gclc.command.ICommand;
|
import net.bigeon.gclc.command.ICommand;
|
||||||
import net.bigeon.gclc.command.ICommandProvider;
|
import net.bigeon.gclc.command.ICommandProvider;
|
||||||
import net.bigeon.gclc.exception.CommandRunException;
|
import net.bigeon.gclc.exception.CommandRunException;
|
||||||
|
import net.bigeon.gclc.exception.CommandRunExceptionType;
|
||||||
import net.bigeon.gclc.manager.ConsoleInput;
|
import net.bigeon.gclc.manager.ConsoleInput;
|
||||||
import net.bigeon.gclc.manager.ConsoleOutput;
|
import net.bigeon.gclc.manager.ConsoleOutput;
|
||||||
|
|
||||||
@@ -95,12 +97,18 @@ public class CommandFork extends Command {
|
|||||||
public void execute(final ConsoleOutput out, final ConsoleInput in,
|
public void execute(final ConsoleOutput out, final ConsoleInput in,
|
||||||
final String... args) throws CommandRunException {
|
final String... args) throws CommandRunException {
|
||||||
if (args.length < 1) {
|
if (args.length < 1) {
|
||||||
throw new CommandRunException("No command to fork");
|
throw new CommandRunException(CommandRunExceptionType.USAGE,
|
||||||
|
"No command to fork");
|
||||||
|
}
|
||||||
|
final String string = args[0];
|
||||||
|
final ICommand cmd = provider.get(string);
|
||||||
|
if (cmd == null) {
|
||||||
|
throw new CommandRunException(CommandRunExceptionType.USAGE,
|
||||||
|
MessageFormat.format("No such command {0}", string));
|
||||||
}
|
}
|
||||||
final ICommand cmd = provider.get(args[0]);
|
|
||||||
final String[] inner = Arrays.copyOfRange(args, 1, args.length);
|
final String[] inner = Arrays.copyOfRange(args, 1, args.length);
|
||||||
final ForkTask task = new ForkCommandTask(cmd, inner, lines);
|
final ForkTask task = new ForkCommandTask(cmd, inner, lines);
|
||||||
final Thread th = new Thread(task);
|
final Thread th = new Thread(task, MessageFormat.format("fork [{0}]", string));
|
||||||
pool.add(task);
|
pool.add(task);
|
||||||
th.start();
|
th.start();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -85,7 +85,9 @@ public abstract class ForkTask implements Task {
|
|||||||
/** THe listeners. */
|
/** THe listeners. */
|
||||||
private final Set<InterruptionListener> listeners = new HashSet<>();
|
private final Set<InterruptionListener> listeners = new HashSet<>();
|
||||||
/** The running state. */
|
/** The running state. */
|
||||||
private boolean running = false;
|
private boolean running = true;
|
||||||
|
/** The running state. */
|
||||||
|
private boolean started = false;
|
||||||
/** The connecting input for this task */
|
/** The connecting input for this task */
|
||||||
protected final ConnectingConsoleInput in = new ConnectingConsoleInput();
|
protected final ConnectingConsoleInput in = new ConnectingConsoleInput();
|
||||||
/** The connecting output for this task */
|
/** The connecting output for this task */
|
||||||
@@ -169,7 +171,7 @@ public abstract class ForkTask implements Task {
|
|||||||
@Override
|
@Override
|
||||||
public final void run() {
|
public final void run() {
|
||||||
synchronized (runLock) {
|
synchronized (runLock) {
|
||||||
running = true;
|
started = true;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
doRun();
|
doRun();
|
||||||
@@ -192,4 +194,11 @@ public abstract class ForkTask implements Task {
|
|||||||
runLock.notifyAll();
|
runLock.notifyAll();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @return the started */
|
||||||
|
public boolean isStarted() {
|
||||||
|
synchronized (runLock) {
|
||||||
|
return started;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -92,7 +92,7 @@ public final class ProcessClear extends Command {
|
|||||||
@Override
|
@Override
|
||||||
public void execute(final ConsoleOutput out, final ConsoleInput in,
|
public void execute(final ConsoleOutput out, final ConsoleInput in,
|
||||||
final String... args) {
|
final String... args) {
|
||||||
for (final String id : pool.getPIDs()) {
|
for (final Integer id : pool.getPIDs()) {
|
||||||
if (!pool.get(id).isRunning()) {
|
if (!pool.get(id).isRunning()) {
|
||||||
pool.remove(id);
|
pool.remove(id);
|
||||||
}
|
}
|
||||||
@@ -103,14 +103,15 @@ public final class ProcessClear extends Command {
|
|||||||
* @see fr.bigeon.gclc.command.ICommand#tip() */
|
* @see fr.bigeon.gclc.command.ICommand#tip() */
|
||||||
@Override
|
@Override
|
||||||
public String tip() {
|
public String tip() {
|
||||||
return "Request a process to stop (softly)";
|
return "Remove non running processes from pool";
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
* @see fr.bigeon.gclc.command.Command#usageDetail() */
|
* @see fr.bigeon.gclc.command.Command#usageDetail() */
|
||||||
@Override
|
@Override
|
||||||
protected String usageDetail() {
|
protected String usageDetail() {
|
||||||
return null;
|
return " All processes in the pool have their running status checked and the "
|
||||||
|
+ "non running ones are removed from the list of tasks.";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -69,6 +69,8 @@ package net.bigeon.gclc.process;
|
|||||||
* #L%
|
* #L%
|
||||||
*/
|
*/
|
||||||
import net.bigeon.gclc.command.Command;
|
import net.bigeon.gclc.command.Command;
|
||||||
|
import net.bigeon.gclc.exception.CommandRunException;
|
||||||
|
import net.bigeon.gclc.exception.CommandRunExceptionType;
|
||||||
import net.bigeon.gclc.manager.ConsoleInput;
|
import net.bigeon.gclc.manager.ConsoleInput;
|
||||||
import net.bigeon.gclc.manager.ConsoleOutput;
|
import net.bigeon.gclc.manager.ConsoleOutput;
|
||||||
|
|
||||||
@@ -91,8 +93,12 @@ public final class ProcessKill extends Command {
|
|||||||
* ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput, java.lang.String[]) */
|
* ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput, java.lang.String[]) */
|
||||||
@Override
|
@Override
|
||||||
public void execute(final ConsoleOutput out, final ConsoleInput in,
|
public void execute(final ConsoleOutput out, final ConsoleInput in,
|
||||||
final String... args) {
|
final String... args) throws CommandRunException {
|
||||||
pool.get(args[0]).setRunning(false);
|
if (args.length < 1) {
|
||||||
|
throw new CommandRunException(CommandRunExceptionType.USAGE,
|
||||||
|
"A pid should be specified.");
|
||||||
|
}
|
||||||
|
pool.get(Integer.parseInt(args[0])).setRunning(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
@@ -106,7 +112,13 @@ public final class ProcessKill extends Command {
|
|||||||
* @see fr.bigeon.gclc.command.Command#usageDetail() */
|
* @see fr.bigeon.gclc.command.Command#usageDetail() */
|
||||||
@Override
|
@Override
|
||||||
protected String usageDetail() {
|
protected String usageDetail() {
|
||||||
return null;
|
return " <pid> is the identification of the process to request the stop to in the pool";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see net.bigeon.gclc.command.Command#usagePattern() */
|
||||||
|
@Override
|
||||||
|
protected String usagePattern() {
|
||||||
|
return super.usagePattern() + " <pid>";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -99,9 +99,9 @@ public final class ProcessList extends Command {
|
|||||||
@Override
|
@Override
|
||||||
public void execute(final ConsoleOutput out, final ConsoleInput in,
|
public void execute(final ConsoleOutput out, final ConsoleInput in,
|
||||||
final String... args) throws CommandRunException {
|
final String... args) throws CommandRunException {
|
||||||
final ArrayList<String> pids = new ArrayList<>(pool.getPIDs());
|
final ArrayList<Integer> pids = new ArrayList<>(pool.getPIDs());
|
||||||
Collections.sort(pids);
|
Collections.sort(pids);
|
||||||
for (final String string : pids) {
|
for (final Integer string : pids) {
|
||||||
try {
|
try {
|
||||||
out.println(MessageFormat.format("{0}\t{1}", string, //$NON-NLS-1$
|
out.println(MessageFormat.format("{0}\t{1}", string, //$NON-NLS-1$
|
||||||
pool.get(string).getName()));
|
pool.get(string).getName()));
|
||||||
@@ -124,7 +124,7 @@ public final class ProcessList extends Command {
|
|||||||
* @see fr.bigeon.gclc.command.Command#usageDetail() */
|
* @see fr.bigeon.gclc.command.Command#usageDetail() */
|
||||||
@Override
|
@Override
|
||||||
protected String usageDetail() {
|
protected String usageDetail() {
|
||||||
return null;
|
return " No argument is considered by this command";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ import java.util.Map;
|
|||||||
* @author Emmanuel Bigeon */
|
* @author Emmanuel Bigeon */
|
||||||
public final class TaskPool {
|
public final class TaskPool {
|
||||||
/** The running processes. */
|
/** The running processes. */
|
||||||
private final Map<String, Task> running = new HashMap<>();
|
private final Map<Integer, Task> running = new HashMap<>();
|
||||||
/** The count for process id. */
|
/** The count for process id. */
|
||||||
private int count = 0;
|
private int count = 0;
|
||||||
/** The lock for pid attribution synchronization. */
|
/** The lock for pid attribution synchronization. */
|
||||||
@@ -99,11 +99,11 @@ public final class TaskPool {
|
|||||||
*
|
*
|
||||||
* @param cmd the process
|
* @param cmd the process
|
||||||
* @return the pid */
|
* @return the pid */
|
||||||
public String add(final Task cmd) {
|
public int add(final Task cmd) {
|
||||||
if (cmd == null) {
|
if (cmd == null) {
|
||||||
throw new IllegalArgumentException("Task cannot be null"); //$NON-NLS-1$
|
throw new IllegalArgumentException("Task cannot be null"); //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
final String pid;
|
final int pid;
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
pid = getPID();
|
pid = getPID();
|
||||||
running.put(pid, cmd);
|
running.put(pid, cmd);
|
||||||
@@ -124,21 +124,21 @@ public final class TaskPool {
|
|||||||
*
|
*
|
||||||
* @param pid the task id
|
* @param pid the task id
|
||||||
* @return the task, if any, associated to this id */
|
* @return the task, if any, associated to this id */
|
||||||
public Task get(final String pid) {
|
public Task get(final int pid) {
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
return running.get(pid);
|
return running.get(Integer.valueOf(pid));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Get the next process id.
|
/** Get the next process id.
|
||||||
*
|
*
|
||||||
* @return the process id */
|
* @return the process id */
|
||||||
private String getPID() {
|
private int getPID() {
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
String pid;
|
int pid;
|
||||||
do {
|
do {
|
||||||
pid = Integer.toString(count++);
|
pid = count++;
|
||||||
} while (running.containsKey(pid));
|
} while (running.containsKey(Integer.valueOf(pid)));
|
||||||
return pid;
|
return pid;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -146,7 +146,7 @@ public final class TaskPool {
|
|||||||
/** Get the running processes' identifiers.
|
/** Get the running processes' identifiers.
|
||||||
*
|
*
|
||||||
* @return the pids */
|
* @return the pids */
|
||||||
public Collection<String> getPIDs() {
|
public Collection<Integer> getPIDs() {
|
||||||
return new HashSet<>(running.keySet());
|
return new HashSet<>(running.keySet());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -158,10 +158,10 @@ public final class TaskPool {
|
|||||||
/** Remove a task from the pool
|
/** Remove a task from the pool
|
||||||
*
|
*
|
||||||
* @param pid the task id */
|
* @param pid the task id */
|
||||||
public void remove(final String pid) {
|
public void remove(final int pid) {
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
running.remove(pid);
|
running.remove(Integer.valueOf(pid));
|
||||||
count = Math.min(count, Integer.parseInt(pid));
|
count = Math.min(count, (pid));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -90,15 +90,21 @@ public final class ConnectingConsoleInput implements ConsoleInput {
|
|||||||
private boolean close = false;
|
private boolean close = false;
|
||||||
/** The prompt string. */
|
/** The prompt string. */
|
||||||
private StringProvider prompt = EMPTY_STRING;
|
private StringProvider prompt = EMPTY_STRING;
|
||||||
/** If the input is currently in prompting state. */
|
/** If the input is currently in prompting state.
|
||||||
|
* <p>
|
||||||
|
* To change it you should be in a promptLock. */
|
||||||
private boolean prompting = false;
|
private boolean prompting = false;
|
||||||
/** The synchronization lock for the rompting status. */
|
/** The synchronization lock for the prompting status. */
|
||||||
private final Object promptLock = new Object();
|
private final Object promptLock = new Object();
|
||||||
/** The synchronization lock for the connection status. */
|
/** The synchronization lock for the connection status. */
|
||||||
private final Object connectionLock = new Object();
|
private final Object connectionLock = new Object();
|
||||||
/** The connected console input. */
|
/** The connected console input.
|
||||||
|
* <p>
|
||||||
|
* To use it, you should be in a promptLock and connectionLock. */
|
||||||
private ConsoleInput connected = null;
|
private ConsoleInput connected = null;
|
||||||
/** The connection state. */
|
/** The connection state.
|
||||||
|
* <p>
|
||||||
|
* To read or modify it, you should be in a connectionLock synchronize block. */
|
||||||
private boolean disconnection = false;
|
private boolean disconnection = false;
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
@@ -121,13 +127,13 @@ public final class ConnectingConsoleInput implements ConsoleInput {
|
|||||||
|
|
||||||
/** Disconnect the current input. */
|
/** Disconnect the current input. */
|
||||||
public void disconnect() {
|
public void disconnect() {
|
||||||
synchronized (connectionLock) {
|
synchronized (promptLock) {
|
||||||
if (connected != null) {
|
synchronized (connectionLock) {
|
||||||
disconnection = true;
|
if (connected != null) {
|
||||||
synchronized (promptLock) {
|
disconnection = true;
|
||||||
connected.interruptPrompt();
|
connected.interruptPrompt();
|
||||||
|
connected = null;
|
||||||
}
|
}
|
||||||
connected = null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -185,7 +191,11 @@ public final class ConnectingConsoleInput implements ConsoleInput {
|
|||||||
if (!prompting) {
|
if (!prompting) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (connected == null) {
|
boolean connect;
|
||||||
|
synchronized (connectionLock) {
|
||||||
|
connect = connected != null;
|
||||||
|
}
|
||||||
|
if (!connect) {
|
||||||
try {
|
try {
|
||||||
promptLock.wait();
|
promptLock.wait();
|
||||||
} catch (final InterruptedException e) {
|
} catch (final InterruptedException e) {
|
||||||
@@ -193,8 +203,8 @@ public final class ConnectingConsoleInput implements ConsoleInput {
|
|||||||
Thread.currentThread().interrupt();
|
Thread.currentThread().interrupt();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
final String res = connected.prompt(message);
|
|
||||||
synchronized (connectionLock) {
|
synchronized (connectionLock) {
|
||||||
|
final String res = connected.prompt(message);
|
||||||
if (disconnection) {
|
if (disconnection) {
|
||||||
disconnection = false;
|
disconnection = false;
|
||||||
} else if (prompting) {
|
} else if (prompting) {
|
||||||
@@ -219,22 +229,26 @@ public final class ConnectingConsoleInput implements ConsoleInput {
|
|||||||
synchronized (promptLock) {
|
synchronized (promptLock) {
|
||||||
prompting = true;
|
prompting = true;
|
||||||
}
|
}
|
||||||
while (true) {
|
do {
|
||||||
synchronized (promptLock) {
|
synchronized (promptLock) {
|
||||||
if (!prompting) {
|
if (!prompting) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (connected == null) {
|
boolean connect;
|
||||||
|
synchronized (connectionLock) {
|
||||||
|
connect = connected != null;
|
||||||
|
}
|
||||||
|
if (!connect) {
|
||||||
try {
|
try {
|
||||||
promptLock.wait();
|
promptLock.wait(timeout);
|
||||||
} catch (final InterruptedException e) {
|
} catch (final InterruptedException e) {
|
||||||
LOGGER.log(Level.WARNING, "Inerruption of console thread", e);
|
LOGGER.log(Level.WARNING, "Inerruption of console thread", e);
|
||||||
Thread.currentThread().interrupt();
|
Thread.currentThread().interrupt();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
final String res = connected.prompt(message,
|
|
||||||
end - System.currentTimeMillis());
|
|
||||||
synchronized (connectionLock) {
|
synchronized (connectionLock) {
|
||||||
|
final String res = connected.prompt(message,
|
||||||
|
end - System.currentTimeMillis());
|
||||||
if (disconnection) {
|
if (disconnection) {
|
||||||
disconnection = false;
|
disconnection = false;
|
||||||
} else if (prompting) {
|
} else if (prompting) {
|
||||||
@@ -245,19 +259,15 @@ public final class ConnectingConsoleInput implements ConsoleInput {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} while (System.currentTimeMillis() < end);
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
* @see fr.bigeon.gclc.manager.ConsoleInput#setPrompt(java.lang.String) */
|
* @see fr.bigeon.gclc.manager.ConsoleInput#setPrompt(java.lang.String) */
|
||||||
@Override
|
@Override
|
||||||
public void setPrompt(final String prompt) {
|
public void setPrompt(final String prompt) {
|
||||||
this.prompt = new StringProvider() {
|
this.prompt = new ConstantString(prompt);
|
||||||
@Override
|
|
||||||
public String apply() {
|
|
||||||
return prompt;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
|
|||||||
@@ -0,0 +1,90 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package net.bigeon.gclc.process;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import net.bigeon.gclc.exception.CommandRunException;
|
||||||
|
import net.bigeon.gclc.exception.CommandRunExceptionType;
|
||||||
|
import net.bigeon.gclc.process.mocks.ForkTaskMock;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Emmanuel Bigeon
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class CommandForegroundTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test method for {@link net.bigeon.gclc.process.CommandForeground#CommandForeground(java.lang.String, net.bigeon.gclc.process.TaskPool)}.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testCommandForeground() {
|
||||||
|
final CommandForeground pl = new CommandForeground("fg", new TaskPool());
|
||||||
|
assertEquals("Command name should be kept as specified", "fg",
|
||||||
|
pl.getCommandName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Test method for
|
||||||
|
* {@link net.bigeon.gclc.process.CommandForeground#doExecute(net.bigeon.gclc.manager.ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, net.bigeon.gclc.command.CommandParameters)}.
|
||||||
|
*
|
||||||
|
* @throws CommandRunException if a valid command failed */
|
||||||
|
@Test
|
||||||
|
public void testDoExecuteConsoleOutputConsoleInputCommandParameters()
|
||||||
|
throws CommandRunException {
|
||||||
|
final TaskPool pool = new TaskPool(false);
|
||||||
|
final CommandForeground pl = new CommandForeground("fg", pool);
|
||||||
|
|
||||||
|
try {
|
||||||
|
pl.execute(null, null, "-delai", "1");
|
||||||
|
fail("PID should be mandatory");
|
||||||
|
} catch (final CommandRunException e) {
|
||||||
|
assertEquals("No pid specified is a usage error",
|
||||||
|
CommandRunExceptionType.USAGE, e.getType());
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
pl.execute(null, null, "-pid", "invalid", "-delai", "1");
|
||||||
|
fail("PID should be a number");
|
||||||
|
} catch (final CommandRunException e) {
|
||||||
|
assertEquals("Invalid pid specified is a usage error",
|
||||||
|
CommandRunExceptionType.USAGE, e.getType());
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
pl.execute(null, null, "-pid", "2", "-delai", "1");
|
||||||
|
fail("PID should exist");
|
||||||
|
} catch (final CommandRunException e) {
|
||||||
|
assertEquals("Inexistent pid specified is a run error",
|
||||||
|
CommandRunExceptionType.EXECUTION, e.getType());
|
||||||
|
}
|
||||||
|
|
||||||
|
final ForkTaskMock cmd = new ForkTaskMock();
|
||||||
|
final int id = pool.add(cmd);
|
||||||
|
pl.execute(null, null, "-pid", Integer.toString(id), "-delai", "1");
|
||||||
|
|
||||||
|
try {
|
||||||
|
pl.execute(null, null, "-pid", Integer.toString(id), "-delai", "-1");
|
||||||
|
fail("delai should be a posistive number");
|
||||||
|
} catch (final CommandRunException e) {
|
||||||
|
assertEquals("Negative delai specified is a usage error",
|
||||||
|
CommandRunExceptionType.USAGE, e.getType());
|
||||||
|
}
|
||||||
|
|
||||||
|
assert cmd.getRunCall() == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test method for {@link net.bigeon.gclc.process.CommandForeground#tip()}.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testTip() {
|
||||||
|
final CommandForeground pl = new CommandForeground("fg", new TaskPool());
|
||||||
|
assertNotNull("Command tip should be defined", pl.tip());
|
||||||
|
assertNotNull("Command usage should be defined", pl.usagePattern());
|
||||||
|
assertNotNull("Command usage should be defined", pl.usageDetail());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package net.bigeon.gclc.process;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import net.bigeon.gclc.command.ICommandProvider;
|
||||||
|
import net.bigeon.gclc.command.SubedCommand;
|
||||||
|
import net.bigeon.gclc.exception.CommandRunException;
|
||||||
|
import net.bigeon.gclc.exception.CommandRunExceptionType;
|
||||||
|
import net.bigeon.gclc.exception.InvalidCommandName;
|
||||||
|
import net.bigeon.gclc.process.mocks.CommandMock;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Emmanuel Bigeon
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class CommandForkTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test method for {@link net.bigeon.gclc.process.CommandFork#CommandFork(java.lang.String, net.bigeon.gclc.command.ICommandProvider, net.bigeon.gclc.process.TaskPool)}.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testCommandForkStringICommandProviderTaskPool() {
|
||||||
|
final TaskPool pool = new TaskPool();
|
||||||
|
final ICommandProvider provider = new SubedCommand("test");
|
||||||
|
final CommandFork pl = new CommandFork("fork", provider, pool);
|
||||||
|
assertEquals("Command name should be preserved", "fork", pl.getCommandName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Test method for
|
||||||
|
* {@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 CommandRunException */
|
||||||
|
@Test
|
||||||
|
public void testExecute() throws InvalidCommandName, CommandRunException {
|
||||||
|
final TaskPool pool = new TaskPool(false);
|
||||||
|
final ICommandProvider provider = new SubedCommand("test");
|
||||||
|
final CommandFork pl = new CommandFork("fork", provider, pool);
|
||||||
|
|
||||||
|
try {
|
||||||
|
pl.execute(null, null);
|
||||||
|
fail("No command specified should through an exception");
|
||||||
|
} catch (final CommandRunException e) {
|
||||||
|
assertEquals("No command specified is a usage error",
|
||||||
|
CommandRunExceptionType.USAGE, e.getType());
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
pl.execute(null, null, "invalid");
|
||||||
|
fail("Invalid command specified should through an exception");
|
||||||
|
} catch (final CommandRunException e) {
|
||||||
|
assertEquals("Invalid command specified is a usage error",
|
||||||
|
CommandRunExceptionType.USAGE, e.getType());
|
||||||
|
}
|
||||||
|
|
||||||
|
final CommandMock mock = new CommandMock();
|
||||||
|
provider.add(mock);
|
||||||
|
|
||||||
|
pl.execute(null, null, mock.getCommandName());
|
||||||
|
|
||||||
|
final ForkTask task = (ForkTask) pool.get(pool.getPIDs().iterator().next());
|
||||||
|
task.join(null, null, 1000);
|
||||||
|
|
||||||
|
assertEquals("Command should be executed when forked", 1, mock.getExecuteCall());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test method for {@link net.bigeon.gclc.process.CommandFork#tip()}.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testTip() {
|
||||||
|
final TaskPool pool = new TaskPool();
|
||||||
|
final ICommandProvider provider = new SubedCommand("test");
|
||||||
|
final CommandFork pl = new CommandFork("fork", provider, pool);
|
||||||
|
assertNotNull("Command tip should be defined", pl.tip());
|
||||||
|
assertNotNull("Command usage should be defined", pl.usagePattern());
|
||||||
|
assertNotNull("Command usage should be defined", pl.usageDetail());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package net.bigeon.gclc.process;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import net.bigeon.gclc.process.mocks.CommandMock;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Emmanuel Bigeon
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class ForkCommandTaskTest {
|
||||||
|
|
||||||
|
/** Test method for
|
||||||
|
* {@link net.bigeon.gclc.process.ForkCommandTask#ForkCommandTask(net.bigeon.gclc.command.ICommand, java.lang.String[], int)}.
|
||||||
|
*
|
||||||
|
* @throws InterruptedException if there is an error in the thread join */
|
||||||
|
@Test
|
||||||
|
public void testForkCommandTask() throws InterruptedException {
|
||||||
|
final CommandMock cmd = new CommandMock();
|
||||||
|
final ForkCommandTask task = new ForkCommandTask(cmd, new String[0], 1);
|
||||||
|
|
||||||
|
final Thread th = new Thread(task);
|
||||||
|
th.start();
|
||||||
|
th.join();
|
||||||
|
|
||||||
|
assertEquals("Task should be executed once", 1, cmd.getExecuteCall());
|
||||||
|
assertEquals("Names should be consistent between task and command",
|
||||||
|
cmd.getCommandName(), task.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package net.bigeon.gclc.process;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import net.bigeon.gclc.exception.CommandRunException;
|
||||||
|
import net.bigeon.gclc.exception.CommandRunExceptionType;
|
||||||
|
import net.bigeon.gclc.utils.PipedConsoleInput;
|
||||||
|
import net.bigeon.gclc.utils.PipedConsoleOutput;
|
||||||
|
|
||||||
|
/** @author Emmanuel Bigeon */
|
||||||
|
public class ForkTaskTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGenericForkTask() throws IOException {
|
||||||
|
final ForkTask task = new ForkTask(5) {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return "name";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doRun() throws CommandRunException {
|
||||||
|
String msg;
|
||||||
|
try {
|
||||||
|
msg = in.prompt();
|
||||||
|
} catch (final IOException e) {
|
||||||
|
throw new CommandRunException(CommandRunExceptionType.INTERACTION,
|
||||||
|
"Unable to prompt user");
|
||||||
|
}
|
||||||
|
if ("ok".equals(msg)) {
|
||||||
|
out.println("Message");
|
||||||
|
} else {
|
||||||
|
out.println("fail");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
final Thread execThread = new Thread(task);
|
||||||
|
execThread.start();
|
||||||
|
|
||||||
|
try (PipedConsoleOutput pco = new PipedConsoleOutput();
|
||||||
|
PipedConsoleInput pci = new PipedConsoleInput(null)) {
|
||||||
|
pci.type("ok");
|
||||||
|
while (!pco.available()) {
|
||||||
|
task.join(pco, pci, 1000);
|
||||||
|
}
|
||||||
|
assertEquals("Execution should work", "Message", pco.readNextLine());
|
||||||
|
}
|
||||||
|
assertFalse("Running state should be updated by task on its completion",
|
||||||
|
task.isRunning());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package net.bigeon.gclc.process;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import net.bigeon.gclc.process.mocks.TaskMock;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Emmanuel Bigeon
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class ProcessClearTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test method for {@link net.bigeon.gclc.process.ProcessClear#ProcessClear(java.lang.String, net.bigeon.gclc.process.TaskPool)}.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testProcessClear() {
|
||||||
|
final ProcessClear pl = new ProcessClear("clear", new TaskPool());
|
||||||
|
assertEquals("Command name should be kept as specified", "clear",
|
||||||
|
pl.getCommandName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test method for {@link net.bigeon.gclc.process.ProcessClear#execute(net.bigeon.gclc.manager.ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, java.lang.String[])}.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testExecute() {
|
||||||
|
final TaskPool pool = new TaskPool();
|
||||||
|
final TaskMock task = new TaskMock();
|
||||||
|
pool.add(task);
|
||||||
|
final ProcessClear pl = new ProcessClear("clear", pool);
|
||||||
|
pl.execute(null, null);
|
||||||
|
assertTrue("cleared pool should be empty", pool.getPIDs().isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test method for {@link net.bigeon.gclc.process.ProcessClear#tip()}.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testTip() {
|
||||||
|
final ProcessClear pl = new ProcessClear("clear", new TaskPool());
|
||||||
|
assertNotNull("Command tip should be defined", pl.tip());
|
||||||
|
assertNotNull("Command usage should be defined", pl.usageDetail());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package net.bigeon.gclc.process;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import net.bigeon.gclc.exception.CommandRunException;
|
||||||
|
import net.bigeon.gclc.exception.CommandRunExceptionType;
|
||||||
|
import net.bigeon.gclc.process.mocks.TaskMock;
|
||||||
|
|
||||||
|
/** @author Emmanuel Bigeon */
|
||||||
|
public class ProcessKillTest {
|
||||||
|
|
||||||
|
/** Test method for
|
||||||
|
* {@link net.bigeon.gclc.process.ProcessKill#ProcessKill(java.lang.String, net.bigeon.gclc.process.TaskPool)}. */
|
||||||
|
@Test
|
||||||
|
public void testProcessKill() {
|
||||||
|
final ProcessList pl = new ProcessList("kill", new TaskPool());
|
||||||
|
assertEquals("Command name should be kept as specified", "kill",
|
||||||
|
pl.getCommandName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Test method for
|
||||||
|
* {@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 IOException if an error occured with the console output */
|
||||||
|
@Test
|
||||||
|
public void testExecute() throws CommandRunException {
|
||||||
|
final TaskPool pool = new TaskPool();
|
||||||
|
final TaskMock task = new TaskMock();
|
||||||
|
final int id = pool.add(task);
|
||||||
|
final ProcessKill pl = new ProcessKill("kill", pool);
|
||||||
|
task.reset();
|
||||||
|
try {
|
||||||
|
pl.execute(null, null);
|
||||||
|
fail("Execution with no pid should raise exception");
|
||||||
|
} catch (final CommandRunException e) {
|
||||||
|
assertEquals("Usage exception expected", CommandRunExceptionType.USAGE,
|
||||||
|
e.getType());
|
||||||
|
}
|
||||||
|
pl.execute(null, null, Integer.toString(id));
|
||||||
|
assertEquals("Task should be shutdown", 1, task.getNumberSetRunning());
|
||||||
|
task.reset();
|
||||||
|
pl.execute(null, null, Integer.toString(id));
|
||||||
|
assertEquals("Task should be shutdown", 1, task.getNumberSetRunning());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Test method for {@link net.bigeon.gclc.process.ProcessKill#tip()}. */
|
||||||
|
@Test
|
||||||
|
public void testTip() {
|
||||||
|
final ProcessKill pl = new ProcessKill("kill", new TaskPool());
|
||||||
|
assertNotNull("Command tip should be defined", pl.tip());
|
||||||
|
assertNotNull("Command usage should be defined", pl.usagePattern());
|
||||||
|
assertNotNull("Command usage should be defined", pl.usageDetail());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package net.bigeon.gclc.process;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import net.bigeon.gclc.exception.CommandRunException;
|
||||||
|
import net.bigeon.gclc.exception.CommandRunExceptionType;
|
||||||
|
import net.bigeon.gclc.process.mocks.TaskMock;
|
||||||
|
import net.bigeon.gclc.utils.PipedConsoleOutput;
|
||||||
|
|
||||||
|
/** @author Emmanuel Bigeon */
|
||||||
|
public class ProcessListTest {
|
||||||
|
|
||||||
|
/** Test method for
|
||||||
|
* {@link net.bigeon.gclc.process.ProcessList#ProcessList(java.lang.String, net.bigeon.gclc.process.TaskPool)}. */
|
||||||
|
@Test
|
||||||
|
public void testProcessList() {
|
||||||
|
final ProcessList pl = new ProcessList("list", new TaskPool());
|
||||||
|
assertEquals("Command name should be kept as specified", "list",
|
||||||
|
pl.getCommandName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Test method for
|
||||||
|
* {@link net.bigeon.gclc.process.ProcessList#execute(net.bigeon.gclc.manager.ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, java.lang.String[])}.
|
||||||
|
*
|
||||||
|
* @throws CommandRunException if an error occured
|
||||||
|
* @throws IOException if an error in initialization of console occured */
|
||||||
|
@Test
|
||||||
|
public void testExecute() throws CommandRunException, IOException {
|
||||||
|
final TaskPool pool = new TaskPool();
|
||||||
|
final TaskMock task = new TaskMock();
|
||||||
|
pool.add(task);
|
||||||
|
final ProcessList pl = new ProcessList("list", pool);
|
||||||
|
final PipedConsoleOutput out = new PipedConsoleOutput();
|
||||||
|
pl.execute(out, null);
|
||||||
|
|
||||||
|
assertTrue("Task should be contained in list",
|
||||||
|
out.readNextLine().contains(task.getName()));
|
||||||
|
|
||||||
|
out.close();
|
||||||
|
|
||||||
|
try {
|
||||||
|
pl.execute(out, null);
|
||||||
|
fail("Printing element to closed console should raise interaction exception");
|
||||||
|
} catch (final CommandRunException e) {
|
||||||
|
assertEquals("Console printing error should be an interaction error",
|
||||||
|
CommandRunExceptionType.INTERACTION, e.getType());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Test method for {@link net.bigeon.gclc.process.ProcessList#tip()}. */
|
||||||
|
@Test
|
||||||
|
public void testTip() {
|
||||||
|
final ProcessList pl = new ProcessList("list", new TaskPool());
|
||||||
|
assertNotNull("Command tip should be defined", pl.tip());
|
||||||
|
assertNotNull("Command usage should be defined", pl.usageDetail());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
package net.bigeon.gclc.process;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import net.bigeon.gclc.process.mocks.TaskMock;
|
||||||
|
|
||||||
|
public class TaskPoolTest {
|
||||||
|
|
||||||
|
public TaskMock task = new TaskMock();
|
||||||
|
public TaskPool pool = new TaskPool(false);
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testTaskPool() {
|
||||||
|
assertFalse("task pool should not auto clear if specified",
|
||||||
|
pool.isAutoClearing());
|
||||||
|
pool = new TaskPool();
|
||||||
|
assertTrue("Default task pool should not auto clear", pool.isAutoClearing());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAdd() {
|
||||||
|
final int id = pool.add(task);
|
||||||
|
assertTrue("Added task pid should be in pool", pool.getPIDs().contains(id));
|
||||||
|
assertEquals("Added task should be in pool", task, pool.get(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGet() {
|
||||||
|
assertNull("Inexistent task id should provide null as task in pool",
|
||||||
|
pool.get(60));
|
||||||
|
final int id = pool.add(task);
|
||||||
|
assertEquals("Added task should be in pool at its id", task, pool.get(id));
|
||||||
|
assertNull("Added task should be in pool at its id", pool.get(id + 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetPIDs() {
|
||||||
|
assertTrue("new pool should be empty", pool.getPIDs().isEmpty());
|
||||||
|
final int id = pool.add(task);
|
||||||
|
assertNull("Added task should be in pool at its id", pool.get(id + 1));
|
||||||
|
assertEquals(
|
||||||
|
"Pool task addition should change the pid collection size accordingly", 1,
|
||||||
|
pool.getPIDs().size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRemove() {
|
||||||
|
assertTrue("new pool should be empty", pool.getPIDs().isEmpty());
|
||||||
|
pool.remove(60);
|
||||||
|
assertTrue("removal of inexistent task should be NOP", pool.getPIDs().isEmpty());
|
||||||
|
final int id = pool.add(task);
|
||||||
|
assertEquals(
|
||||||
|
"Pool task addition should change the pid collection size accordingly", 1,
|
||||||
|
pool.getPIDs().size());
|
||||||
|
pool.remove(id + 1);
|
||||||
|
assertEquals("removal of inexistent task should be NOP", 1,
|
||||||
|
pool.getPIDs().size());
|
||||||
|
pool.remove(id);
|
||||||
|
assertTrue("removal of task should reduce the number of tasks",
|
||||||
|
pool.getPIDs().isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testShutdown() {
|
||||||
|
final int id = pool.add(task);
|
||||||
|
task.reset();
|
||||||
|
pool.shutdown();
|
||||||
|
assertEquals("Shutdown should request shut down of tasks, only once", 1,
|
||||||
|
task.getNumberSetRunning());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
package net.bigeon.gclc.process.io;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import net.bigeon.gclc.tools.ConstantString;
|
||||||
|
import net.bigeon.gclc.utils.PipedConsoleInput;
|
||||||
|
|
||||||
|
public class ConnectingConsoleInputTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConnect() throws IOException {
|
||||||
|
final ConnectingConsoleInput in = new ConnectingConsoleInput();
|
||||||
|
final PipedConsoleInput pci = new PipedConsoleInput(null);
|
||||||
|
assertNull("Inputs should not transmit before connection", in.prompt(1000));
|
||||||
|
in.connect(pci);
|
||||||
|
pci.type("Some message");
|
||||||
|
assertEquals("Messages should be buffered", "Some message", in.prompt());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDisconnect() throws IOException {
|
||||||
|
final ConnectingConsoleInput in = new ConnectingConsoleInput();
|
||||||
|
final PipedConsoleInput pci = new PipedConsoleInput(null);
|
||||||
|
assertNull("Inputs should not transmit before connection", in.prompt(1000));
|
||||||
|
in.connect(pci);
|
||||||
|
pci.type("Some message");
|
||||||
|
assertEquals("Messages should be buffered", "Some message", in.prompt());
|
||||||
|
in.disconnect();
|
||||||
|
assertNull("Disconnected inputs should not transmit anymore", in.prompt(1000));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIsClosed() throws IOException {
|
||||||
|
final ConnectingConsoleInput in = new ConnectingConsoleInput();
|
||||||
|
assertFalse("New input should not be closed", in.isClosed());
|
||||||
|
final PipedConsoleInput pci = new PipedConsoleInput(null);
|
||||||
|
in.connect(pci);
|
||||||
|
pci.close();
|
||||||
|
assertFalse("Connected input closing should not close connecting", in.isClosed());
|
||||||
|
final PipedConsoleInput pci2 = new PipedConsoleInput(null);
|
||||||
|
in.connect(pci2);
|
||||||
|
in.close();
|
||||||
|
assertTrue("Input should close on request", in.isClosed());
|
||||||
|
assertFalse("Connected input should not be closed by connecting closure",
|
||||||
|
pci2.isClosed());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCoveragePrompt() {
|
||||||
|
final ConnectingConsoleInput in = new ConnectingConsoleInput();
|
||||||
|
in.setPrompt("test");
|
||||||
|
assertEquals("Prompt should be set correctly", "test",
|
||||||
|
((ConstantString) in.getPrompt()).apply());
|
||||||
|
final ConstantString prompt = new ConstantString("other");
|
||||||
|
in.setPrompt(prompt);
|
||||||
|
assertEquals("Prompt should be set correctly", prompt, in.getPrompt());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
package net.bigeon.gclc.process.io;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import net.bigeon.gclc.utils.PipedConsoleOutput;
|
||||||
|
|
||||||
|
public class ConnectingConsoleOutputTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConnect() throws IOException {
|
||||||
|
final ConnectingConsoleOutput out = new ConnectingConsoleOutput(
|
||||||
|
ConnectingConsoleOutput.QUEUE, 10);
|
||||||
|
final PipedConsoleOutput output = new PipedConsoleOutput();
|
||||||
|
out.println("Some message");
|
||||||
|
assertFalse("Message should not be transmitted", output.available());
|
||||||
|
// Connect
|
||||||
|
out.connect(output);
|
||||||
|
|
||||||
|
// Connected outputs should receive data
|
||||||
|
final String res = output.readNextLine();
|
||||||
|
assertEquals("Message should be correctly transmitted", "Some message", res);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDisconnect() throws IOException {
|
||||||
|
final ConnectingConsoleOutput out = new ConnectingConsoleOutput(
|
||||||
|
ConnectingConsoleOutput.QUEUE, 10);
|
||||||
|
final PipedConsoleOutput output = new PipedConsoleOutput();
|
||||||
|
// NOP disconnection when no connection
|
||||||
|
out.disconnect();
|
||||||
|
// Connect
|
||||||
|
out.connect(output);
|
||||||
|
|
||||||
|
// Connected outputs should receive data
|
||||||
|
out.println("Some message");
|
||||||
|
final String res = output.readNextLine();
|
||||||
|
assertEquals("Message should be correctly transmitted", "Some message", res);
|
||||||
|
out.disconnect();
|
||||||
|
out.println("Some message");
|
||||||
|
assertFalse("Message should not be transmitted", output.available());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIsClosed() throws IOException {
|
||||||
|
final ConnectingConsoleOutput out = new ConnectingConsoleOutput(
|
||||||
|
ConnectingConsoleOutput.QUEUE, 10);
|
||||||
|
final PipedConsoleOutput output = new PipedConsoleOutput();
|
||||||
|
assertFalse("New output should not be closed", out.isClosed());
|
||||||
|
// Connect
|
||||||
|
out.connect(output);
|
||||||
|
|
||||||
|
output.close();
|
||||||
|
|
||||||
|
assertFalse("Output should not close when connected outputs do", out.isClosed());
|
||||||
|
final PipedConsoleOutput output2 = new PipedConsoleOutput();
|
||||||
|
out.connect(output2);
|
||||||
|
out.close();
|
||||||
|
assertTrue("Output should close on request", out.isClosed());
|
||||||
|
assertFalse("Output should not close connected output when they do",
|
||||||
|
output2.isClosed());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPrint() throws IOException {
|
||||||
|
final ConnectingConsoleOutput out = new ConnectingConsoleOutput(
|
||||||
|
ConnectingConsoleOutput.PERSIST, 10);
|
||||||
|
|
||||||
|
out.print("Some text");
|
||||||
|
out.println();
|
||||||
|
final PipedConsoleOutput output = new PipedConsoleOutput();
|
||||||
|
out.connect(output);
|
||||||
|
String res = output.readNextLine();
|
||||||
|
assertEquals("Message should be correctly transmitted", "Some text", res);
|
||||||
|
out.disconnect();
|
||||||
|
|
||||||
|
// Persist should be reprinted on any connect
|
||||||
|
out.connect(output);
|
||||||
|
res = output.readNextLine();
|
||||||
|
assertEquals("Message should be correctly transmitted", "Some text", res);
|
||||||
|
out.disconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package net.bigeon.gclc.process.mocks;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import net.bigeon.gclc.command.ICommand;
|
||||||
|
import net.bigeon.gclc.exception.CommandRunException;
|
||||||
|
import net.bigeon.gclc.manager.ConsoleInput;
|
||||||
|
import net.bigeon.gclc.manager.ConsoleOutput;
|
||||||
|
|
||||||
|
/** @author Emmanuel Bigeon */
|
||||||
|
public class CommandMock implements ICommand {
|
||||||
|
|
||||||
|
private int executeCall = 0;
|
||||||
|
private int tipCall = 0;
|
||||||
|
private int helpCall = 0;
|
||||||
|
private int commandNameCall = 0;
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see net.bigeon.gclc.command.ICommand#execute(net.bigeon.gclc.manager.
|
||||||
|
* ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, java.lang.String[]) */
|
||||||
|
@Override
|
||||||
|
public void execute(final ConsoleOutput out, final ConsoleInput in,
|
||||||
|
final String... args) throws CommandRunException {
|
||||||
|
executeCall++;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see net.bigeon.gclc.command.ICommand#getCommandName() */
|
||||||
|
@Override
|
||||||
|
public String getCommandName() {
|
||||||
|
commandNameCall++;
|
||||||
|
return "name";
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see
|
||||||
|
* net.bigeon.gclc.command.ICommand#help(net.bigeon.gclc.manager.ConsoleOutput,
|
||||||
|
* java.lang.String[]) */
|
||||||
|
@Override
|
||||||
|
public void help(final ConsoleOutput output, final String... args)
|
||||||
|
throws IOException {
|
||||||
|
helpCall++;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see net.bigeon.gclc.command.ICommand#tip() */
|
||||||
|
@Override
|
||||||
|
public String tip() {
|
||||||
|
tipCall++;
|
||||||
|
return "tip";
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return the executeCall */
|
||||||
|
public int getExecuteCall() {
|
||||||
|
return executeCall;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package net.bigeon.gclc.process.mocks;
|
||||||
|
|
||||||
|
import net.bigeon.gclc.exception.CommandRunException;
|
||||||
|
import net.bigeon.gclc.process.ForkTask;
|
||||||
|
import net.bigeon.gclc.process.Task;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Emmanuel Bigeon
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class ForkTaskMock extends ForkTask implements Task {
|
||||||
|
|
||||||
|
private int runCall = 0;
|
||||||
|
|
||||||
|
/** @param lines */
|
||||||
|
public ForkTaskMock() {
|
||||||
|
super(10);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see net.bigeon.gclc.process.Task#getName()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return "name";
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see net.bigeon.gclc.process.ForkTask#doRun()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected void doRun() throws CommandRunException {
|
||||||
|
runCall++;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return the runCall */
|
||||||
|
public int getRunCall() {
|
||||||
|
return runCall;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package net.bigeon.gclc.process.mocks;
|
||||||
|
|
||||||
|
import net.bigeon.gclc.process.InterruptionListener;
|
||||||
|
import net.bigeon.gclc.process.Task;
|
||||||
|
|
||||||
|
public class TaskMock implements Task {
|
||||||
|
|
||||||
|
private int runCall = 0;
|
||||||
|
private int setRunningCall = 0;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
runCall++;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addInterruptionListener(final InterruptionListener listener) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return "name";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isRunning() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void rmInterruptionListener(final InterruptionListener listener) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setRunning(final boolean running) {
|
||||||
|
setRunningCall++;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public void reset() {
|
||||||
|
runCall = 0;
|
||||||
|
setRunningCall = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return */
|
||||||
|
public Object getNumberSetRunning() {
|
||||||
|
return setRunningCall;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
/** Mock definitions for testing.
|
||||||
|
*
|
||||||
|
* @author Emmanuel Bigeon */
|
||||||
|
package net.bigeon.gclc.process.mocks;
|
||||||
@@ -5,9 +5,9 @@
|
|||||||
<artifactId>ebigeon-config</artifactId>
|
<artifactId>ebigeon-config</artifactId>
|
||||||
<version>1.8.21</version>
|
<version>1.8.21</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>socket</artifactId>
|
|
||||||
<groupId>net.bigeon.gclc</groupId>
|
<groupId>net.bigeon.gclc</groupId>
|
||||||
<version>1.1.12</version>
|
<artifactId>socket</artifactId>
|
||||||
|
<version>1.1.13</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>
|
||||||
@@ -36,7 +36,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>socket-1.1.12</tag>
|
<tag>socket-1.1.13</tag>
|
||||||
</scm>
|
</scm>
|
||||||
<properties>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
@@ -47,16 +47,27 @@
|
|||||||
<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>smu</artifactId>
|
<artifactId>smu</artifactId>
|
||||||
<version>1.0.6</version>
|
<version>1.0.6</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>net.bigeon.test</groupId>
|
||||||
|
<artifactId>junitmt</artifactId>
|
||||||
|
<version>1.0.0</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<reporting>
|
<reporting>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-javadoc-plugin</artifactId>
|
||||||
|
<version>2.10.3</version>
|
||||||
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>com.github.sevntu-checkstyle</groupId>
|
<groupId>com.github.sevntu-checkstyle</groupId>
|
||||||
<artifactId>dsm-maven-plugin</artifactId>
|
<artifactId>dsm-maven-plugin</artifactId>
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ import net.bigeon.gclc.ConsoleApplication;
|
|||||||
@SuppressWarnings({ "javadoc", "nls" })
|
@SuppressWarnings({ "javadoc", "nls" })
|
||||||
public class TestServer {
|
public class TestServer {
|
||||||
|
|
||||||
|
public static final int PORT = 3300;
|
||||||
private static SocketConsoleApplicationShell SHELL;
|
private static SocketConsoleApplicationShell SHELL;
|
||||||
private static ConnexionManager<Socket> manager;
|
private static ConnexionManager<Socket> manager;
|
||||||
|
|
||||||
@@ -75,7 +76,7 @@ public class TestServer {
|
|||||||
input.setPrompt("> \n");
|
input.setPrompt("> \n");
|
||||||
output = new PluggableConsoleOutput();
|
output = new PluggableConsoleOutput();
|
||||||
manager = new DConnexionManager<>();
|
manager = new DConnexionManager<>();
|
||||||
SHELL = new SocketConsoleApplicationShell(3300,
|
SHELL = new SocketConsoleApplicationShell(PORT,
|
||||||
InetAddress.getByName("127.0.0.1"));
|
InetAddress.getByName("127.0.0.1"));
|
||||||
final ConsoleApplication app = ConsoleTestApplication.create(output, input,
|
final ConsoleApplication app = ConsoleTestApplication.create(output, input,
|
||||||
manager);
|
manager);
|
||||||
@@ -91,13 +92,13 @@ public class TestServer {
|
|||||||
});
|
});
|
||||||
th.start();
|
th.start();
|
||||||
try {
|
try {
|
||||||
Object waiting = new Object();
|
final Object waiting = new Object();
|
||||||
synchronized (waiting) {
|
synchronized (waiting) {
|
||||||
while (!input.isPrompting()) {
|
while (!input.isPrompting()) {
|
||||||
waiting.wait(50);
|
waiting.wait(50);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (InterruptedException e) {
|
} catch (final InterruptedException e) {
|
||||||
Thread.currentThread().interrupt();
|
Thread.currentThread().interrupt();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,8 +13,8 @@
|
|||||||
<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>
|
||||||
<inceptionYear>2015</inceptionYear>
|
|
||||||
<url>https://bigeon.net/projects/gclc.html</url>
|
<url>https://bigeon.net/projects/gclc.html</url>
|
||||||
|
<inceptionYear>2015</inceptionYear>
|
||||||
<organization>
|
<organization>
|
||||||
<name>Bigeon</name>
|
<name>Bigeon</name>
|
||||||
<url>https://bigeon.net/</url>
|
<url>https://bigeon.net/</url>
|
||||||
@@ -50,17 +50,22 @@
|
|||||||
<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>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>net.bigeon</groupId>
|
<groupId>net.bigeon</groupId>
|
||||||
<artifactId>collections</artifactId>
|
<artifactId>collections</artifactId>
|
||||||
<version>1.1.4</version>
|
<version>1.1.6</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<reporting>
|
<reporting>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-javadoc-plugin</artifactId>
|
||||||
|
<version>2.10.3</version>
|
||||||
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>com.github.sevntu-checkstyle</groupId>
|
<groupId>com.github.sevntu-checkstyle</groupId>
|
||||||
<artifactId>dsm-maven-plugin</artifactId>
|
<artifactId>dsm-maven-plugin</artifactId>
|
||||||
|
|||||||
@@ -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>
|
||||||
@@ -45,25 +43,31 @@
|
|||||||
<copyright.email>emmanuel@bigeon.fr</copyright.email>
|
<copyright.email>emmanuel@bigeon.fr</copyright.email>
|
||||||
<license.licenseName>cecill_2.1</license.licenseName>
|
<license.licenseName>cecill_2.1</license.licenseName>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<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>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
<reporting>
|
<reporting>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-javadoc-plugin</artifactId>
|
||||||
|
<version>2.10.3</version>
|
||||||
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>com.github.sevntu-checkstyle</groupId>
|
<groupId>com.github.sevntu-checkstyle</groupId>
|
||||||
<artifactId>dsm-maven-plugin</artifactId>
|
<artifactId>dsm-maven-plugin</artifactId>
|
||||||
<version>2.2.0</version>
|
<version>2.2.0</version>
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</reporting><build>
|
</reporting>
|
||||||
<plugins>
|
|
||||||
</plugins>
|
|
||||||
</build>
|
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@@ -39,7 +39,6 @@ package net.bigeon.gclc.system;
|
|||||||
*/
|
*/
|
||||||
import java.io.BufferedWriter;
|
import java.io.BufferedWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.io.OutputStreamWriter;
|
import java.io.OutputStreamWriter;
|
||||||
|
|
||||||
@@ -92,15 +91,17 @@ public class ExecSystemCommand extends Command {
|
|||||||
}
|
}
|
||||||
// Stream forwarding to the application's console
|
// Stream forwarding to the application's console
|
||||||
// This is started in a parallel thread.
|
// This is started in a parallel thread.
|
||||||
final InputStream is = proc.getInputStream();
|
final Thread th = ForwardingRunnable.connect(proc.getInputStream(), out,
|
||||||
final Thread th = new Thread(new ForwardingRunnable(out, is));
|
"Command output");
|
||||||
th.start();
|
// Set the empty prompt for the processes promptings.
|
||||||
in.setPrompt(""); //$NON-NLS-1$
|
in.setPrompt(""); //$NON-NLS-1$
|
||||||
// Forward console input to the process.
|
// Forward console input to the process, whether the process is waiting or not
|
||||||
|
// for input.
|
||||||
final OutputStream os = proc.getOutputStream();
|
final OutputStream os = proc.getOutputStream();
|
||||||
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();
|
||||||
|
// Avoid interruption being sent to process.
|
||||||
if (!user.isEmpty()) {
|
if (!user.isEmpty()) {
|
||||||
writer.write(user + EOL);
|
writer.write(user + EOL);
|
||||||
}
|
}
|
||||||
@@ -125,7 +126,7 @@ public class ExecSystemCommand extends Command {
|
|||||||
return " The system command is a system dependend command like sh on linux or" + //$NON-NLS-1$
|
return " The system command is a system dependend command like sh on linux or" + //$NON-NLS-1$
|
||||||
System.lineSeparator() + "powershell on windows." + //$NON-NLS-1$
|
System.lineSeparator() + "powershell on windows." + //$NON-NLS-1$
|
||||||
System.lineSeparator() + System.lineSeparator()
|
System.lineSeparator() + System.lineSeparator()
|
||||||
+ " As an example if you give \"cat /etc/hostname\" as argument, on a linux" //$NON-NLS-1$
|
+ " As an example if you give \"cat\", \"/etc/hostname\" as arguments, on a linux" //$NON-NLS-1$
|
||||||
+ System.lineSeparator() + "system, you would get the computer name." + //$NON-NLS-1$
|
+ System.lineSeparator() + "system, you would get the computer name." + //$NON-NLS-1$
|
||||||
System.lineSeparator();
|
System.lineSeparator();
|
||||||
}
|
}
|
||||||
@@ -136,5 +137,4 @@ public class ExecSystemCommand extends Command {
|
|||||||
protected String usagePattern() {
|
protected String usagePattern() {
|
||||||
return " CMD <system command>"; //$NON-NLS-1$
|
return " CMD <system command>"; //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,6 +44,12 @@ import net.bigeon.gclc.exception.CommandRunExceptionType;
|
|||||||
import net.bigeon.gclc.manager.ConsoleOutput;
|
import net.bigeon.gclc.manager.ConsoleOutput;
|
||||||
|
|
||||||
/** A class forwarding the content of a stream into a console output.
|
/** A class forwarding the content of a stream into a console output.
|
||||||
|
* <p>
|
||||||
|
* This class is useful for processes that run in a different environment than
|
||||||
|
* the application but return an output. Such processes include command
|
||||||
|
* processes and socket communications for example.
|
||||||
|
* <p>
|
||||||
|
* In the current library is is used to retrieve a system command output.
|
||||||
*
|
*
|
||||||
* @author Emmanuel Bigeon */
|
* @author Emmanuel Bigeon */
|
||||||
public final class ForwardingRunnable implements Runnable {
|
public final class ForwardingRunnable implements Runnable {
|
||||||
@@ -75,8 +81,7 @@ public final class ForwardingRunnable implements Runnable {
|
|||||||
readToEnd(out, is);
|
readToEnd(out, is);
|
||||||
is.close();
|
is.close();
|
||||||
} catch (final CommandRunException e) {
|
} catch (final CommandRunException e) {
|
||||||
LOGGER.log(Level.WARNING,
|
LOGGER.log(Level.WARNING, "Manager was closed in the meantime...", e); //$NON-NLS-1$
|
||||||
"Manager was closed in the meantime...", e); //$NON-NLS-1$
|
|
||||||
} catch (final IOException e) {
|
} catch (final IOException e) {
|
||||||
LOGGER.log(Level.WARNING, "Input stream was closed...", e); //$NON-NLS-1$
|
LOGGER.log(Level.WARNING, "Input stream was closed...", e); //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
@@ -87,12 +92,12 @@ public final class ForwardingRunnable implements Runnable {
|
|||||||
* @param is the input stream
|
* @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 void readToEnd(final ConsoleOutput out, final InputStream is)
|
private static void readToEnd(final ConsoleOutput out, final InputStream is)
|
||||||
throws CommandRunException {
|
throws CommandRunException {
|
||||||
int c;
|
int c;
|
||||||
try {
|
try {
|
||||||
while ((c = is.read()) >= 0) {
|
while ((c = is.read()) >= 0) {
|
||||||
// Print the chaeacter in the output
|
// Print the character in the output
|
||||||
printCharacter(out, (char) c);
|
printCharacter(out, (char) c);
|
||||||
}
|
}
|
||||||
} catch (final IOException e) {
|
} catch (final IOException e) {
|
||||||
@@ -114,4 +119,26 @@ public final class ForwardingRunnable implements Runnable {
|
|||||||
MANAGER_WAS_CLOSED, e); // $NON-NLS-1$
|
MANAGER_WAS_CLOSED, e); // $NON-NLS-1$
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Tool method to connect an input stream to a console output.
|
||||||
|
*
|
||||||
|
* @param stream the stream
|
||||||
|
* @param output the output
|
||||||
|
* @param name the identification of the thread
|
||||||
|
* @return the thread the forwarding runnable runs into. */
|
||||||
|
public static Thread connect(final InputStream stream, final ConsoleOutput output,
|
||||||
|
final String name) {
|
||||||
|
final Thread th = new Thread(new ForwardingRunnable(output, stream), name);
|
||||||
|
th.start();
|
||||||
|
return th;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Tool method to connect an input stream to a console output.
|
||||||
|
*
|
||||||
|
* @param stream the stream
|
||||||
|
* @param output the output
|
||||||
|
* @return the thread the forwarding runnable runs into. */
|
||||||
|
public static Thread connect(final InputStream stream, final ConsoleOutput output) {
|
||||||
|
return connect(stream, output, "Forwarding");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,8 +33,9 @@ package net.bigeon.gclc.system;
|
|||||||
* knowledge of the CeCILL license and that you accept its terms.
|
* knowledge of the CeCILL license and that you accept its terms.
|
||||||
* #L%
|
* #L%
|
||||||
*/
|
*/
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
@@ -46,29 +47,35 @@ import net.bigeon.gclc.utils.SinkOutput;
|
|||||||
|
|
||||||
public class ExecSystemCommandTest {
|
public class ExecSystemCommandTest {
|
||||||
|
|
||||||
|
/** Test the help methods of the command. */
|
||||||
@Test
|
@Test
|
||||||
public void testExecSystemCommand() {
|
public void testExecSystemCommand() {
|
||||||
ExecSystemCommand cmd = new ExecSystemCommand("test");
|
final ExecSystemCommand cmd = new ExecSystemCommand("test");
|
||||||
assertEquals("Name should be preserved", "test", cmd.getCommandName());
|
assertEquals("Name should be preserved", "test", cmd.getCommandName());
|
||||||
assertNotNull("tip should be defined", cmd.tip());
|
assertNotNull("tip should be defined", cmd.tip());
|
||||||
assertNotNull("usage should be defined", cmd.usagePattern());
|
assertNotNull("usage should be defined", cmd.usagePattern());
|
||||||
assertNotNull("usage should be defined", cmd.usageDetail());
|
assertNotNull("usage should be defined", cmd.usageDetail());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Test the execution of the command.
|
||||||
|
*
|
||||||
|
* @throws CommandRunException */
|
||||||
@Test
|
@Test
|
||||||
public void testExecute() throws CommandRunException {
|
public void testExecute() throws CommandRunException {
|
||||||
ConsoleOutput out = SinkOutput.INSTANCE;
|
final ConsoleOutput out = SinkOutput.INSTANCE;
|
||||||
ConsoleInput in = EmptyInput.INSTANCE;
|
final ConsoleInput in = EmptyInput.INSTANCE;
|
||||||
|
|
||||||
ExecSystemCommand cmd = new ExecSystemCommand();
|
final ExecSystemCommand cmd = new ExecSystemCommand();
|
||||||
if (System.getProperty("os.name").contains("indows")) {
|
if (System.getProperty("os.name").contains("indows")) {
|
||||||
cmd.execute(out, in, "cmd", "/C", "dir");
|
cmd.execute(out, in, "cmd", "/C", "dir");
|
||||||
try {
|
} else if (System.getProperty("os.name").contains("inux")) {
|
||||||
cmd.execute(out, in, "inexistent");
|
cmd.execute(out, in, "ls");
|
||||||
fail("Able to execute inexistent command in system");
|
}
|
||||||
} catch (CommandRunException e) {
|
try {
|
||||||
// ok
|
cmd.execute(out, in, "inexistent");
|
||||||
}
|
fail("Able to execute inexistent command in system");
|
||||||
|
} catch (final CommandRunException e) {
|
||||||
|
// ok
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
36
gclc/pom.xml
36
gclc/pom.xml
@@ -8,7 +8,7 @@
|
|||||||
</parent>
|
</parent>
|
||||||
<groupId>net.bigeon</groupId>
|
<groupId>net.bigeon</groupId>
|
||||||
<artifactId>gclc</artifactId>
|
<artifactId>gclc</artifactId>
|
||||||
<version>2.0.11-SNAPSHOT</version>
|
<version>2.0.12-SNAPSHOT</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>
|
||||||
@@ -47,26 +47,12 @@
|
|||||||
</properties>
|
</properties>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>net.bigeon.test</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>junitmt</artifactId>
|
||||||
<version>4.11</version>
|
<version>1.0.0</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<reporting>
|
|
||||||
<plugins>
|
|
||||||
<plugin>
|
|
||||||
<groupId>com.github.sevntu-checkstyle</groupId>
|
|
||||||
<artifactId>dsm-maven-plugin</artifactId>
|
|
||||||
<version>2.2.0</version>
|
|
||||||
</plugin>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.codehaus.mojo</groupId>
|
|
||||||
<artifactId>jdepend-maven-plugin</artifactId>
|
|
||||||
<version>2.0</version>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
|
||||||
</reporting>
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
@@ -81,4 +67,18 @@
|
|||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
<reporting>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>com.github.sevntu-checkstyle</groupId>
|
||||||
|
<artifactId>dsm-maven-plugin</artifactId>
|
||||||
|
<version>2.2.0</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.codehaus.mojo</groupId>
|
||||||
|
<artifactId>jdepend-maven-plugin</artifactId>
|
||||||
|
<version>2.0</version>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</reporting>
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ public class ConstantString implements StringProvider {
|
|||||||
/** Create a provider for a string.
|
/** Create a provider for a string.
|
||||||
*
|
*
|
||||||
* @param string the string */
|
* @param string the string */
|
||||||
public ConstantString(String string) {
|
public ConstantString(final String string) {
|
||||||
this.string = string;
|
this.string = string;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,4 +87,38 @@ public class ConstantString implements StringProvider {
|
|||||||
public String apply() {
|
public String apply() {
|
||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see java.lang.Object#hashCode() */
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
final int prime = 31;
|
||||||
|
int result = 1;
|
||||||
|
result = prime * result + ((string == null) ? 0 : string.hashCode());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see java.lang.Object#equals(java.lang.Object) */
|
||||||
|
@Override
|
||||||
|
public boolean equals(final Object obj) {
|
||||||
|
if (this == obj) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (obj == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (getClass() != obj.getClass()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
final ConstantString other = (ConstantString) obj;
|
||||||
|
if (string == null) {
|
||||||
|
if (other.string != null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else if (!string.equals(other.string)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -177,7 +177,7 @@ public final class ReadingRunnable implements Runnable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void waitForDelivery(String message) throws InterruptedException {
|
public void waitForDelivery(final String message) throws InterruptedException {
|
||||||
Object mLock;
|
Object mLock;
|
||||||
synchronized (messageBlockerLock) {
|
synchronized (messageBlockerLock) {
|
||||||
if (!messageBlocker.containsKey(message)) {
|
if (!messageBlocker.containsKey(message)) {
|
||||||
@@ -196,10 +196,11 @@ public final class ReadingRunnable implements Runnable {
|
|||||||
* @throws IOException if the pipe is closed */
|
* @throws IOException if the pipe is closed */
|
||||||
public boolean hasMessage() throws IOException {
|
public boolean hasMessage() throws IOException {
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
if (!running) {
|
final boolean empty = messages.isEmpty();
|
||||||
|
if (!running && empty) {
|
||||||
throw new IOException(CLOSED_PIPE);
|
throw new IOException(CLOSED_PIPE);
|
||||||
}
|
}
|
||||||
return !messages.isEmpty();
|
return !empty;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -93,6 +93,8 @@ import net.bigeon.gclc.manager.ConsoleInput;
|
|||||||
import net.bigeon.gclc.manager.ConsoleOutput;
|
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.ThreadTest;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
@@ -509,10 +511,10 @@ 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 Thread th = new Thread(new Runnable() {
|
final ATestRunnable testConsole = new ATestRunnable() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
protected void testRun() {
|
||||||
try {
|
try {
|
||||||
assertEquals("value of " + str1 + "? ", buf.readLine());
|
assertEquals("value of " + str1 + "? ", buf.readLine());
|
||||||
in.type("");
|
in.type("");
|
||||||
@@ -523,15 +525,17 @@ public class ParametrizedCommandTest {
|
|||||||
buf.readLine());
|
buf.readLine());
|
||||||
in.type(str2);
|
in.type(str2);
|
||||||
} catch (final IOException e) {
|
} catch (final IOException e) {
|
||||||
assertNull(e);
|
fail("IO exception in test");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
|
|
||||||
|
final Thread th = new Thread(testConsole);
|
||||||
th.start();
|
th.start();
|
||||||
|
|
||||||
cmd.execute(out, in);
|
cmd.execute(out, in);
|
||||||
|
|
||||||
th.join();
|
ThreadTest.assertRuns(th, testConsole);
|
||||||
}
|
}
|
||||||
try (PipedConsoleOutput out = new PipedConsoleOutput();
|
try (PipedConsoleOutput out = new PipedConsoleOutput();
|
||||||
PipedOutputStream pout = new PipedOutputStream();
|
PipedOutputStream pout = new PipedOutputStream();
|
||||||
@@ -540,23 +544,25 @@ 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 Thread th = new Thread(new Runnable() {
|
final ATestRunnable testConsole = new ATestRunnable() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
protected void testRun() {
|
||||||
try {
|
try {
|
||||||
assertEquals("value of " + str1 + "? ", buf.readLine());
|
assertEquals("value of " + str1 + "? ", buf.readLine());
|
||||||
in.type(str2);
|
in.type(str2);
|
||||||
} catch (final IOException e) {
|
} catch (final IOException e) {
|
||||||
assertNull(e);
|
fail("IO exception in test");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
|
|
||||||
|
final Thread th = new Thread(testConsole);
|
||||||
th.start();
|
th.start();
|
||||||
|
|
||||||
cmd.execute(out, in, "-" + addParam);
|
cmd.execute(out, in, "-" + addParam);
|
||||||
|
|
||||||
th.join();
|
ThreadTest.assertRuns(th, testConsole);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
final PipedConsoleOutput out = new PipedConsoleOutput();
|
final PipedConsoleOutput out = new PipedConsoleOutput();
|
||||||
|
|||||||
@@ -94,6 +94,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.ThreadTest;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
@@ -115,19 +117,21 @@ public class CLIPrompterTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Test method for
|
/** Test method for
|
||||||
* {@link net.bigeon.gclc.prompt.CLIPrompter#promptBoolean(net.bigeon.gclc.manager.ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, String)}. */
|
* {@link net.bigeon.gclc.prompt.CLIPrompter#promptBoolean(net.bigeon.gclc.manager.ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, String)}.
|
||||||
|
*
|
||||||
|
* @throws IOException if an io occurs */
|
||||||
@Test
|
@Test
|
||||||
public final void testPromptBoolean() {
|
public final void testPromptBoolean() 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);
|
||||||
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 ATestRunnable target = new ATestRunnable() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void testRun() {
|
||||||
try {
|
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$
|
||||||
@@ -135,10 +139,10 @@ public class CLIPrompterTest {
|
|||||||
assertFalse(CLIPrompter.promptBoolean(out, in, "My message")); //$NON-NLS-1$
|
assertFalse(CLIPrompter.promptBoolean(out, in, "My message")); //$NON-NLS-1$
|
||||||
} catch (final IOException e) {
|
} catch (final IOException e) {
|
||||||
fail("Unexpected io excpetion"); //$NON-NLS-1$
|
fail("Unexpected io excpetion"); //$NON-NLS-1$
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
|
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$
|
||||||
in.type(""); //$NON-NLS-1$
|
in.type(""); //$NON-NLS-1$
|
||||||
@@ -151,10 +155,7 @@ public class CLIPrompterTest {
|
|||||||
in.type("N"); //$NON-NLS-1$
|
in.type("N"); //$NON-NLS-1$
|
||||||
assertTrue(buf.readLine().startsWith("My message")); //$NON-NLS-1$
|
assertTrue(buf.readLine().startsWith("My message")); //$NON-NLS-1$
|
||||||
in.type("nO"); //$NON-NLS-1$
|
in.type("nO"); //$NON-NLS-1$
|
||||||
th.join();
|
ThreadTest.assertRuns(th, target);
|
||||||
} catch (IOException | InterruptedException e) {
|
|
||||||
fail("Unexpected excpetion"); //$NON-NLS-1$
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1080,10 +1081,10 @@ 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 message = "My message"; //$NON-NLS-1$
|
final String message = "My message"; //$NON-NLS-1$
|
||||||
|
|
||||||
final Thread th = new Thread(new Runnable() {
|
final ATestRunnable target = new ATestRunnable() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void testRun() {
|
||||||
try {
|
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)
|
||||||
@@ -1102,7 +1103,8 @@ public class CLIPrompterTest {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
|
final Thread th = new Thread(target);
|
||||||
th.start();
|
th.start();
|
||||||
assertTrue(out.readNextLine().startsWith(message));
|
assertTrue(out.readNextLine().startsWith(message));
|
||||||
final String readNextLine = out.readNextLine();
|
final String readNextLine = out.readNextLine();
|
||||||
@@ -1135,8 +1137,8 @@ public class CLIPrompterTest {
|
|||||||
assertTrue(out.readNextLine().contains(keys.get(0).toString()));
|
assertTrue(out.readNextLine().contains(keys.get(0).toString()));
|
||||||
assertTrue(out.readNextLine().contains(keys.get(1).toString()));
|
assertTrue(out.readNextLine().contains(keys.get(1).toString()));
|
||||||
in.type("0 1"); //$NON-NLS-1$
|
in.type("0 1"); //$NON-NLS-1$
|
||||||
th.join();
|
ThreadTest.assertRuns(th, target);
|
||||||
} catch (IOException | InterruptedException e) {
|
} catch (final IOException e) {
|
||||||
fail("Unexpected excpetion, the other thread probably failed"); //$NON-NLS-1$
|
fail("Unexpected excpetion, the other thread probably failed"); //$NON-NLS-1$
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@@ -1152,28 +1154,28 @@ public class CLIPrompterTest {
|
|||||||
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 String res = "some content"; //$NON-NLS-1$
|
final String res = "some content"; //$NON-NLS-1$
|
||||||
final Thread th = new Thread(new Runnable() {
|
final ATestRunnable target = new ATestRunnable() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void testRun() {
|
||||||
try {
|
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) {
|
} catch (final IOException e) {
|
||||||
fail("Unexpected io excpetion"); //$NON-NLS-1$
|
fail("Unexpected io excpetion"); //$NON-NLS-1$
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
|
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$
|
||||||
in.type(""); //$NON-NLS-1$
|
in.type(""); //$NON-NLS-1$
|
||||||
assertTrue(buf.readLine().startsWith("my reprompt")); //$NON-NLS-1$
|
assertTrue(buf.readLine().startsWith("my reprompt")); //$NON-NLS-1$
|
||||||
in.type(res);
|
in.type(res);
|
||||||
th.join();
|
ThreadTest.assertRuns(th, target);
|
||||||
|
|
||||||
} catch (IOException | InterruptedException e) {
|
} catch (final IOException e) {
|
||||||
fail("Unexpected excpetion"); //$NON-NLS-1$
|
fail("Unexpected excpetion"); //$NON-NLS-1$
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user