Set socket closing by interruption trace as FINE level
Additional comments, extract exec command default name to constant Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
parent
ecc10994ca
commit
80d10d7d85
@ -198,7 +198,10 @@ public class SocketConsoleApplicationShell implements Runnable {
|
||||
}
|
||||
communicate(writer, in);
|
||||
} catch (SocketException e) {
|
||||
LOGGER.log(Level.INFO, "Socket closed", e); //$NON-NLS-1$
|
||||
LOGGER.log(Level.INFO, "Socket closed"); //$NON-NLS-1$
|
||||
LOGGER.log(Level.FINE,
|
||||
"Socket closed with exception (probably due to server interruption)", //$NON-NLS-1$
|
||||
e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -61,7 +61,7 @@ public class ConsoleRunnableTest {
|
||||
int i = 0;
|
||||
String[] cmds;
|
||||
|
||||
/** @param cmds */
|
||||
/** @param cmds the commands to run */
|
||||
public ConsoleManagerTestImplementation(String[] cmds) {
|
||||
super();
|
||||
this.cmds = cmds;
|
||||
|
@ -60,7 +60,6 @@ public class TestServer {
|
||||
return server;
|
||||
}
|
||||
|
||||
/** @return */
|
||||
private static SocketConsoleApplicationShell getShell() {
|
||||
if (SHELL == null) {
|
||||
SHELL = new SocketConsoleApplicationShell(3300, "close",
|
||||
|
@ -77,7 +77,7 @@ public final class HistoryTextKeyListener extends KeyAdapter {
|
||||
pressedKeyCode(e.keyCode);
|
||||
}
|
||||
|
||||
/** @param keyCode */
|
||||
/** @param keyCode the pressed key code */
|
||||
public void pressedKeyCode(int keyCode) {
|
||||
// Enter validates the command if prompting
|
||||
if (keyCode == '\r') {
|
||||
|
@ -225,6 +225,8 @@ public class SWTConsole extends Composite implements ConsoleManager {
|
||||
promptLock.notifyAll();
|
||||
promptLock.wait();
|
||||
} catch (final InterruptedException e) {
|
||||
LOGGER.log(Level.WARNING,
|
||||
"Error in synchronization of prompting", e); //$NON-NLS-1$
|
||||
command = null;
|
||||
}
|
||||
}
|
||||
@ -260,6 +262,8 @@ public class SWTConsole extends Composite implements ConsoleManager {
|
||||
throw new IOException();
|
||||
}
|
||||
} catch (final InterruptedException e) {
|
||||
LOGGER.log(Level.WARNING,
|
||||
"Error in synchronization of prompting", e); //$NON-NLS-1$
|
||||
command = null;
|
||||
} finally {
|
||||
Display.getDefault().syncExec(new Runnable() {
|
||||
|
@ -55,6 +55,7 @@ import fr.bigeon.gclc.exception.InvalidCommandName;
|
||||
* TODO
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
@SuppressWarnings({"javadoc", "static-method", "nls", "deprecation"})
|
||||
public class SWTConsoleShellTest {
|
||||
|
||||
protected static final long TWO_SECONDS = 2000;
|
||||
|
@ -23,53 +23,63 @@ import fr.bigeon.gclc.manager.ConsoleManager;
|
||||
* @author Emmanuel Bigeon */
|
||||
public class ExecSystemCommand extends Command {
|
||||
|
||||
/** The command default name */
|
||||
private static final String COMMAND_DEFAULT_NAME = "exec"; //$NON-NLS-1$
|
||||
/** The end of line separator */
|
||||
private static final String EOL = System.lineSeparator();
|
||||
/** The class logger */
|
||||
private static final Logger LOGGER = Logger
|
||||
.getLogger(ExecSystemCommand.class.getName());
|
||||
/** The manager for the application's user interface */
|
||||
private final ConsoleManager manager;
|
||||
|
||||
/** @param name
|
||||
* @param manager */
|
||||
/** @param name the name of the command (the input from the manager that
|
||||
* should trigger this command)
|
||||
* @param manager the console manager for input and outputs */
|
||||
public ExecSystemCommand(String name, ConsoleManager manager) {
|
||||
super(name);
|
||||
this.manager = manager;
|
||||
}
|
||||
|
||||
/** @param name
|
||||
* @param manager */
|
||||
/** @param manager the console manager for input and outputs */
|
||||
public ExecSystemCommand(ConsoleManager manager) {
|
||||
super("exec");
|
||||
super(COMMAND_DEFAULT_NAME);
|
||||
this.manager = manager;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.command.ICommand#execute(java.lang.String[]) */
|
||||
@SuppressWarnings("resource")
|
||||
@Override
|
||||
public void execute(String... args) throws CommandRunException {
|
||||
Process proc;
|
||||
try {
|
||||
proc = Runtime.getRuntime().exec(args);
|
||||
} catch (IOException e2) {
|
||||
LOGGER.log(Level.SEVERE, "Unable to run process", e2);
|
||||
LOGGER.log(Level.SEVERE, "Unable to run process", e2); //$NON-NLS-1$
|
||||
return;
|
||||
}
|
||||
|
||||
final InputStream is = proc.getInputStream();
|
||||
final InputStream is = proc
|
||||
.getInputStream();
|
||||
Thread th = new Thread(new Runnable() {
|
||||
|
||||
@SuppressWarnings("synthetic-access")
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
readToEnd(is);
|
||||
is.close();
|
||||
} catch (CommandRunException e) {
|
||||
LOGGER.log(Level.WARNING,
|
||||
"Manager was closed in the meantime...", e);
|
||||
"Manager was closed in the meantime...", e); //$NON-NLS-1$
|
||||
} catch (IOException e) {
|
||||
LOGGER.log(Level.WARNING, "Input stream was closed...", e); //$NON-NLS-1$
|
||||
}
|
||||
}
|
||||
});
|
||||
th.start();
|
||||
manager.setPrompt("");
|
||||
manager.setPrompt(new String());
|
||||
final OutputStream os = proc.getOutputStream();
|
||||
try (BufferedWriter writer = new BufferedWriter(
|
||||
new OutputStreamWriter(os))) {
|
||||
@ -80,18 +90,19 @@ public class ExecSystemCommand extends Command {
|
||||
} catch (IOException e) {
|
||||
throw new CommandRunException(
|
||||
CommandRunExceptionType.INTERACTION,
|
||||
"manager was closed", e, this);
|
||||
"manager was closed", e, this); //$NON-NLS-1$
|
||||
}
|
||||
writer.write(user + EOL);
|
||||
}
|
||||
} catch (IOException e1) {
|
||||
// TODO Auto-generated catch block
|
||||
e1.printStackTrace();
|
||||
throw new CommandRunException(CommandRunExceptionType.INTERACTION,
|
||||
"manager was closed", e1, this); //$NON-NLS-1$
|
||||
}
|
||||
}
|
||||
|
||||
/** @param is the input stream
|
||||
* @throws CommandRunException */
|
||||
* @throws CommandRunException if the manager was closed while writing the
|
||||
* stream */
|
||||
protected void readToEnd(InputStream is) throws CommandRunException {
|
||||
int c;
|
||||
try {
|
||||
@ -101,11 +112,11 @@ public class ExecSystemCommand extends Command {
|
||||
} catch (IOException e) {
|
||||
throw new CommandRunException(
|
||||
CommandRunExceptionType.INTERACTION,
|
||||
"manager was closed", e, this);
|
||||
"manager was closed", e, this); //$NON-NLS-1$
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
LOGGER.log(Level.INFO, "input stream reading failed", e);
|
||||
LOGGER.log(Level.INFO, "input stream reading failed", e); //$NON-NLS-1$
|
||||
}
|
||||
}
|
||||
|
||||
@ -113,12 +124,12 @@ public class ExecSystemCommand extends Command {
|
||||
* @see fr.bigeon.gclc.command.Command#usageDetail() */
|
||||
@Override
|
||||
protected String usageDetail() {
|
||||
return " The system command is a system dependend command like sh on linux or" +
|
||||
System.lineSeparator() + "powershell on windows." +
|
||||
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() + System.lineSeparator() +
|
||||
" As an example if you give \"cat /etc/hostname\" as argument, on a linux" +
|
||||
" As an example if you give \"cat /etc/hostname\" as argument, on a linux" + //$NON-NLS-1$
|
||||
System.lineSeparator() +
|
||||
"system, you would get the computer name." +
|
||||
"system, you would get the computer name." + //$NON-NLS-1$
|
||||
System.lineSeparator();
|
||||
}
|
||||
|
||||
@ -126,14 +137,14 @@ public class ExecSystemCommand extends Command {
|
||||
* @see fr.bigeon.gclc.command.Command#usagePattern() */
|
||||
@Override
|
||||
protected String usagePattern() {
|
||||
return " CMD <system command>";
|
||||
return " CMD <system command>"; //$NON-NLS-1$
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.command.ICommand#tip() */
|
||||
@Override
|
||||
public String tip() {
|
||||
return "Execute a system command";
|
||||
return "Execute a system command"; //$NON-NLS-1$
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,38 +0,0 @@
|
||||
package net.bigeon.gclc.system;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
/**
|
||||
* Unit test for simple App.
|
||||
*/
|
||||
public class AppTest
|
||||
extends TestCase
|
||||
{
|
||||
/**
|
||||
* Create the test case
|
||||
*
|
||||
* @param testName name of the test case
|
||||
*/
|
||||
public AppTest( String testName )
|
||||
{
|
||||
super( testName );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the suite of tests being tested
|
||||
*/
|
||||
public static Test suite()
|
||||
{
|
||||
return new TestSuite( AppTest.class );
|
||||
}
|
||||
|
||||
/**
|
||||
* Rigourous Test :-)
|
||||
*/
|
||||
public void testApp()
|
||||
{
|
||||
assertTrue( true );
|
||||
}
|
||||
}
|
@ -198,6 +198,8 @@ public class CLIPrompter {
|
||||
}
|
||||
|
||||
} catch (final NumberFormatException e) {
|
||||
LOGGER.log(Level.FINER,
|
||||
"Unrecognized number. Prompting user again.", e); //$NON-NLS-1$
|
||||
keepOn = true;
|
||||
manager.println(CLIPrompterMessages
|
||||
.getString(PROMPTCHOICE_FORMATERR, 0, index));
|
||||
@ -384,6 +386,8 @@ public class CLIPrompter {
|
||||
try {
|
||||
added = addUserChoice(val, chs, index);
|
||||
} catch (final NumberFormatException e) {
|
||||
LOGGER.log(Level.FINER,
|
||||
"Unrecognized number. Prompting user again.", e); //$NON-NLS-1$
|
||||
keepOn = true;
|
||||
manager.println(CLIPrompterMessages
|
||||
.getString(PROMPTCHOICE_FORMATERR, 0, index));
|
||||
|
@ -55,6 +55,7 @@ import fr.bigeon.gclc.manager.SystemConsoleManager;
|
||||
* @author Emmanuel Bigeon
|
||||
*
|
||||
*/
|
||||
@SuppressWarnings("javadoc")
|
||||
public class CommandTestingApplication {
|
||||
|
||||
private final PrintWriter writer;
|
||||
@ -62,6 +63,7 @@ public class CommandTestingApplication {
|
||||
private final Thread th;
|
||||
|
||||
/** @throws IOException if the streams cannot be build */
|
||||
@SuppressWarnings("resource")
|
||||
public CommandTestingApplication() throws IOException {
|
||||
final PipedOutputStream src = new PipedOutputStream();
|
||||
InputStream in = new PipedInputStream(src);
|
||||
@ -70,6 +72,7 @@ public class CommandTestingApplication {
|
||||
new SystemConsoleManager(System.out, in));
|
||||
th = new Thread(new Runnable() {
|
||||
|
||||
@SuppressWarnings("synthetic-access")
|
||||
@Override
|
||||
public void run() {
|
||||
application.start();
|
||||
|
@ -124,7 +124,7 @@ public class ConsoleApplicationTest {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
fail("pipe creation"); //$NON-NLS-1$
|
||||
fail("pipe creation " + e.getLocalizedMessage()); //$NON-NLS-1$
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -62,7 +62,7 @@ public class GCLCConstantsTest {
|
||||
try {
|
||||
res = GCLCConstants.splitCommand("aCommand");
|
||||
} catch (CommandParsingException e) {
|
||||
fail("Unable to parse simple command"); //$NON-NLS-1$
|
||||
fail("Unable to parse simple command " + e.getLocalizedMessage()); //$NON-NLS-1$
|
||||
return;
|
||||
}
|
||||
assertTrue(res.size() == 1);
|
||||
@ -71,7 +71,8 @@ public class GCLCConstantsTest {
|
||||
try {
|
||||
res = GCLCConstants.splitCommand("aCommand with some arguments");
|
||||
} catch (CommandParsingException e) {
|
||||
fail("Unable to parse command with arguments"); //$NON-NLS-1$
|
||||
fail("Unable to parse command with arguments " + //$NON-NLS-1$
|
||||
e.getLocalizedMessage());
|
||||
return;
|
||||
}
|
||||
assertTrue(res.size() == 4);
|
||||
@ -82,7 +83,8 @@ public class GCLCConstantsTest {
|
||||
try {
|
||||
res = GCLCConstants.splitCommand("aCommand with some arguments");
|
||||
} catch (CommandParsingException e) {
|
||||
fail("Unable to parse command with arguments and double whitspaces"); //$NON-NLS-1$
|
||||
fail("Unable to parse command with arguments and double whitspaces " + //$NON-NLS-1$
|
||||
e.getLocalizedMessage());
|
||||
return;
|
||||
}
|
||||
assertTrue(res.size() == 4);
|
||||
@ -94,7 +96,8 @@ public class GCLCConstantsTest {
|
||||
res = GCLCConstants
|
||||
.splitCommand("aCommand \"with some\" arguments");
|
||||
} catch (CommandParsingException e) {
|
||||
fail("Unable to parse command with string argument"); //$NON-NLS-1$
|
||||
fail("Unable to parse command with string argument " + //$NON-NLS-1$
|
||||
e.getLocalizedMessage());
|
||||
return;
|
||||
}
|
||||
assertTrue(res.size() == 3);
|
||||
@ -104,7 +107,8 @@ public class GCLCConstantsTest {
|
||||
try {
|
||||
res = GCLCConstants.splitCommand("aCommand with\\ some arguments");
|
||||
} catch (CommandParsingException e) {
|
||||
fail("Unable to parse command with arguments with escaped whitspaces"); //$NON-NLS-1$
|
||||
fail("Unable to parse command with arguments with escaped whitspaces " + //$NON-NLS-1$
|
||||
e.getLocalizedMessage());
|
||||
return;
|
||||
}
|
||||
assertTrue(res.size() == 3);
|
||||
@ -115,7 +119,8 @@ public class GCLCConstantsTest {
|
||||
res = GCLCConstants
|
||||
.splitCommand("aCommand wi\\\"th some arguments");
|
||||
} catch (CommandParsingException e) {
|
||||
fail("Unable to parse command with string argument"); //$NON-NLS-1$
|
||||
fail("Unable to parse command with string argument " + //$NON-NLS-1$
|
||||
e.getLocalizedMessage());
|
||||
return;
|
||||
}
|
||||
assertTrue(res.size() == 4);
|
||||
@ -131,6 +136,7 @@ public class GCLCConstantsTest {
|
||||
fail("Parsing argument with string cut");
|
||||
} catch (CommandParsingException e) {
|
||||
// OK
|
||||
assertTrue(e.getLocalizedMessage(), true);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -54,6 +54,7 @@ import fr.bigeon.gclc.exception.InvalidCommandName;
|
||||
* @author Emmanuel Bigeon
|
||||
*
|
||||
*/
|
||||
@SuppressWarnings("static-method")
|
||||
public class ScriptExecutionTest {
|
||||
|
||||
/**
|
||||
@ -64,20 +65,20 @@ public class ScriptExecutionTest {
|
||||
try {
|
||||
CommandTestingApplication application = new CommandTestingApplication();
|
||||
|
||||
application.add(new ScriptExecution("script",
|
||||
application.getApplication(), "#"));
|
||||
application.add(new ScriptExecution("script", //$NON-NLS-1$
|
||||
application.getApplication(), "#")); //$NON-NLS-1$
|
||||
|
||||
application.sendCommand("script src/test/resources/script1.txt");
|
||||
application.sendCommand("script src/test/resources/script2.txt");
|
||||
application.sendCommand("script src/test/resources/script3.txt");
|
||||
application.sendCommand("script src/test/resources/script4.txt");
|
||||
application.sendCommand("script src/test/resources/script1.txt"); //$NON-NLS-1$
|
||||
application.sendCommand("script src/test/resources/script2.txt"); //$NON-NLS-1$
|
||||
application.sendCommand("script src/test/resources/script3.txt"); //$NON-NLS-1$
|
||||
application.sendCommand("script src/test/resources/script4.txt"); //$NON-NLS-1$
|
||||
application
|
||||
.sendCommand("script src/test/resources/script5.txt test");
|
||||
.sendCommand("script src/test/resources/script5.txt test"); //$NON-NLS-1$
|
||||
|
||||
application.waitAndStop();
|
||||
|
||||
} catch (IOException | InvalidCommandName e) {
|
||||
fail("Unexpected exception");
|
||||
fail("Unexpected exception " + e.getLocalizedMessage()); //$NON-NLS-1$
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user