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:
Emmanuel Bigeon 2016-08-30 12:16:58 -04:00
parent ecc10994ca
commit 80d10d7d85
13 changed files with 72 additions and 78 deletions

View File

@ -198,7 +198,10 @@ public class SocketConsoleApplicationShell implements Runnable {
} }
communicate(writer, in); communicate(writer, in);
} catch (SocketException e) { } 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);
} }
} }
} }

View File

@ -61,7 +61,7 @@ public class ConsoleRunnableTest {
int i = 0; int i = 0;
String[] cmds; String[] cmds;
/** @param cmds */ /** @param cmds the commands to run */
public ConsoleManagerTestImplementation(String[] cmds) { public ConsoleManagerTestImplementation(String[] cmds) {
super(); super();
this.cmds = cmds; this.cmds = cmds;

View File

@ -60,7 +60,6 @@ public class TestServer {
return server; return server;
} }
/** @return */
private static SocketConsoleApplicationShell getShell() { private static SocketConsoleApplicationShell getShell() {
if (SHELL == null) { if (SHELL == null) {
SHELL = new SocketConsoleApplicationShell(3300, "close", SHELL = new SocketConsoleApplicationShell(3300, "close",

View File

@ -77,7 +77,7 @@ public final class HistoryTextKeyListener extends KeyAdapter {
pressedKeyCode(e.keyCode); pressedKeyCode(e.keyCode);
} }
/** @param keyCode */ /** @param keyCode the pressed key code */
public void pressedKeyCode(int keyCode) { public void pressedKeyCode(int keyCode) {
// Enter validates the command if prompting // Enter validates the command if prompting
if (keyCode == '\r') { if (keyCode == '\r') {

View File

@ -225,6 +225,8 @@ public class SWTConsole extends Composite implements ConsoleManager {
promptLock.notifyAll(); promptLock.notifyAll();
promptLock.wait(); promptLock.wait();
} catch (final InterruptedException e) { } catch (final InterruptedException e) {
LOGGER.log(Level.WARNING,
"Error in synchronization of prompting", e); //$NON-NLS-1$
command = null; command = null;
} }
} }
@ -260,6 +262,8 @@ public class SWTConsole extends Composite implements ConsoleManager {
throw new IOException(); throw new IOException();
} }
} catch (final InterruptedException e) { } catch (final InterruptedException e) {
LOGGER.log(Level.WARNING,
"Error in synchronization of prompting", e); //$NON-NLS-1$
command = null; command = null;
} finally { } finally {
Display.getDefault().syncExec(new Runnable() { Display.getDefault().syncExec(new Runnable() {

View File

@ -55,6 +55,7 @@ import fr.bigeon.gclc.exception.InvalidCommandName;
* TODO * TODO
* *
* @author Emmanuel Bigeon */ * @author Emmanuel Bigeon */
@SuppressWarnings({"javadoc", "static-method", "nls", "deprecation"})
public class SWTConsoleShellTest { public class SWTConsoleShellTest {
protected static final long TWO_SECONDS = 2000; protected static final long TWO_SECONDS = 2000;

View File

@ -23,53 +23,63 @@ import fr.bigeon.gclc.manager.ConsoleManager;
* @author Emmanuel Bigeon */ * @author Emmanuel Bigeon */
public class ExecSystemCommand extends Command { 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(); private static final String EOL = System.lineSeparator();
/** The class logger */ /** The class logger */
private static final Logger LOGGER = Logger private static final Logger LOGGER = Logger
.getLogger(ExecSystemCommand.class.getName()); .getLogger(ExecSystemCommand.class.getName());
/** The manager for the application's user interface */
private final ConsoleManager manager; private final ConsoleManager manager;
/** @param name /** @param name the name of the command (the input from the manager that
* @param manager */ * should trigger this command)
* @param manager the console manager for input and outputs */
public ExecSystemCommand(String name, ConsoleManager manager) { public ExecSystemCommand(String name, ConsoleManager manager) {
super(name); super(name);
this.manager = manager; this.manager = manager;
} }
/** @param name /** @param manager the console manager for input and outputs */
* @param manager */
public ExecSystemCommand(ConsoleManager manager) { public ExecSystemCommand(ConsoleManager manager) {
super("exec"); super(COMMAND_DEFAULT_NAME);
this.manager = manager; this.manager = manager;
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see fr.bigeon.gclc.command.ICommand#execute(java.lang.String[]) */ * @see fr.bigeon.gclc.command.ICommand#execute(java.lang.String[]) */
@SuppressWarnings("resource")
@Override @Override
public void execute(String... args) throws CommandRunException { public void execute(String... args) throws CommandRunException {
Process proc; Process proc;
try { try {
proc = Runtime.getRuntime().exec(args); proc = Runtime.getRuntime().exec(args);
} catch (IOException e2) { } 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; return;
} }
final InputStream is = proc.getInputStream(); final InputStream is = proc
.getInputStream();
Thread th = new Thread(new Runnable() { Thread th = new Thread(new Runnable() {
@SuppressWarnings("synthetic-access")
@Override @Override
public void run() { public void run() {
try { try {
readToEnd(is); readToEnd(is);
is.close();
} catch (CommandRunException e) { } catch (CommandRunException e) {
LOGGER.log(Level.WARNING, 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(); th.start();
manager.setPrompt(""); manager.setPrompt(new String());
final OutputStream os = proc.getOutputStream(); final OutputStream os = proc.getOutputStream();
try (BufferedWriter writer = new BufferedWriter( try (BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os))) { new OutputStreamWriter(os))) {
@ -80,18 +90,19 @@ public class ExecSystemCommand extends Command {
} catch (IOException e) { } catch (IOException e) {
throw new CommandRunException( throw new CommandRunException(
CommandRunExceptionType.INTERACTION, CommandRunExceptionType.INTERACTION,
"manager was closed", e, this); "manager was closed", e, this); //$NON-NLS-1$
} }
writer.write(user + EOL); writer.write(user + EOL);
} }
} catch (IOException e1) { } catch (IOException e1) {
// TODO Auto-generated catch block throw new CommandRunException(CommandRunExceptionType.INTERACTION,
e1.printStackTrace(); "manager was closed", e1, this); //$NON-NLS-1$
} }
} }
/** @param is the input stream /** @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 { protected void readToEnd(InputStream is) throws CommandRunException {
int c; int c;
try { try {
@ -101,11 +112,11 @@ public class ExecSystemCommand extends Command {
} catch (IOException e) { } catch (IOException e) {
throw new CommandRunException( throw new CommandRunException(
CommandRunExceptionType.INTERACTION, CommandRunExceptionType.INTERACTION,
"manager was closed", e, this); "manager was closed", e, this); //$NON-NLS-1$
} }
} }
} catch (IOException e) { } 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() */ * @see fr.bigeon.gclc.command.Command#usageDetail() */
@Override @Override
protected String usageDetail() { protected String usageDetail() {
return " The system command is a system dependend command like sh on linux or" + return " The system command is a system dependend command like sh on linux or" + //$NON-NLS-1$
System.lineSeparator() + "powershell on windows." + 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" + " As an example if you give \"cat /etc/hostname\" as argument, on a linux" + //$NON-NLS-1$
System.lineSeparator() + System.lineSeparator() +
"system, you would get the computer name." + "system, you would get the computer name." + //$NON-NLS-1$
System.lineSeparator(); System.lineSeparator();
} }
@ -126,14 +137,14 @@ public class ExecSystemCommand extends Command {
* @see fr.bigeon.gclc.command.Command#usagePattern() */ * @see fr.bigeon.gclc.command.Command#usagePattern() */
@Override @Override
protected String usagePattern() { protected String usagePattern() {
return " CMD <system command>"; return " CMD <system command>"; //$NON-NLS-1$
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see fr.bigeon.gclc.command.ICommand#tip() */ * @see fr.bigeon.gclc.command.ICommand#tip() */
@Override @Override
public String tip() { public String tip() {
return "Execute a system command"; return "Execute a system command"; //$NON-NLS-1$
} }
} }

View File

@ -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 );
}
}

View File

@ -198,6 +198,8 @@ public class CLIPrompter {
} }
} catch (final NumberFormatException e) { } catch (final NumberFormatException e) {
LOGGER.log(Level.FINER,
"Unrecognized number. Prompting user again.", e); //$NON-NLS-1$
keepOn = true; keepOn = true;
manager.println(CLIPrompterMessages manager.println(CLIPrompterMessages
.getString(PROMPTCHOICE_FORMATERR, 0, index)); .getString(PROMPTCHOICE_FORMATERR, 0, index));
@ -384,6 +386,8 @@ public class CLIPrompter {
try { try {
added = addUserChoice(val, chs, index); added = addUserChoice(val, chs, index);
} catch (final NumberFormatException e) { } catch (final NumberFormatException e) {
LOGGER.log(Level.FINER,
"Unrecognized number. Prompting user again.", e); //$NON-NLS-1$
keepOn = true; keepOn = true;
manager.println(CLIPrompterMessages manager.println(CLIPrompterMessages
.getString(PROMPTCHOICE_FORMATERR, 0, index)); .getString(PROMPTCHOICE_FORMATERR, 0, index));

View File

@ -55,6 +55,7 @@ import fr.bigeon.gclc.manager.SystemConsoleManager;
* @author Emmanuel Bigeon * @author Emmanuel Bigeon
* *
*/ */
@SuppressWarnings("javadoc")
public class CommandTestingApplication { public class CommandTestingApplication {
private final PrintWriter writer; private final PrintWriter writer;
@ -62,6 +63,7 @@ public class CommandTestingApplication {
private final Thread th; private final Thread th;
/** @throws IOException if the streams cannot be build */ /** @throws IOException if the streams cannot be build */
@SuppressWarnings("resource")
public CommandTestingApplication() throws IOException { public CommandTestingApplication() throws IOException {
final PipedOutputStream src = new PipedOutputStream(); final PipedOutputStream src = new PipedOutputStream();
InputStream in = new PipedInputStream(src); InputStream in = new PipedInputStream(src);
@ -70,6 +72,7 @@ public class CommandTestingApplication {
new SystemConsoleManager(System.out, in)); new SystemConsoleManager(System.out, in));
th = new Thread(new Runnable() { th = new Thread(new Runnable() {
@SuppressWarnings("synthetic-access")
@Override @Override
public void run() { public void run() {
application.start(); application.start();

View File

@ -124,7 +124,7 @@ public class ConsoleApplicationTest {
e.printStackTrace(); e.printStackTrace();
} }
} catch (IOException e) { } catch (IOException e) {
fail("pipe creation"); //$NON-NLS-1$ fail("pipe creation " + e.getLocalizedMessage()); //$NON-NLS-1$
} }
} }
} }

View File

@ -62,7 +62,7 @@ public class GCLCConstantsTest {
try { try {
res = GCLCConstants.splitCommand("aCommand"); res = GCLCConstants.splitCommand("aCommand");
} catch (CommandParsingException e) { } catch (CommandParsingException e) {
fail("Unable to parse simple command"); //$NON-NLS-1$ fail("Unable to parse simple command " + e.getLocalizedMessage()); //$NON-NLS-1$
return; return;
} }
assertTrue(res.size() == 1); assertTrue(res.size() == 1);
@ -71,7 +71,8 @@ public class GCLCConstantsTest {
try { try {
res = GCLCConstants.splitCommand("aCommand with some arguments"); res = GCLCConstants.splitCommand("aCommand with some arguments");
} catch (CommandParsingException e) { } 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; return;
} }
assertTrue(res.size() == 4); assertTrue(res.size() == 4);
@ -82,7 +83,8 @@ public class GCLCConstantsTest {
try { try {
res = GCLCConstants.splitCommand("aCommand with some arguments"); res = GCLCConstants.splitCommand("aCommand with some arguments");
} catch (CommandParsingException e) { } 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; return;
} }
assertTrue(res.size() == 4); assertTrue(res.size() == 4);
@ -94,7 +96,8 @@ public class GCLCConstantsTest {
res = GCLCConstants res = GCLCConstants
.splitCommand("aCommand \"with some\" arguments"); .splitCommand("aCommand \"with some\" arguments");
} catch (CommandParsingException e) { } 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; return;
} }
assertTrue(res.size() == 3); assertTrue(res.size() == 3);
@ -104,7 +107,8 @@ public class GCLCConstantsTest {
try { try {
res = GCLCConstants.splitCommand("aCommand with\\ some arguments"); res = GCLCConstants.splitCommand("aCommand with\\ some arguments");
} catch (CommandParsingException e) { } 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; return;
} }
assertTrue(res.size() == 3); assertTrue(res.size() == 3);
@ -115,7 +119,8 @@ public class GCLCConstantsTest {
res = GCLCConstants res = GCLCConstants
.splitCommand("aCommand wi\\\"th some arguments"); .splitCommand("aCommand wi\\\"th some arguments");
} catch (CommandParsingException e) { } 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; return;
} }
assertTrue(res.size() == 4); assertTrue(res.size() == 4);
@ -131,6 +136,7 @@ public class GCLCConstantsTest {
fail("Parsing argument with string cut"); fail("Parsing argument with string cut");
} catch (CommandParsingException e) { } catch (CommandParsingException e) {
// OK // OK
assertTrue(e.getLocalizedMessage(), true);
} }
} }

View File

@ -54,6 +54,7 @@ import fr.bigeon.gclc.exception.InvalidCommandName;
* @author Emmanuel Bigeon * @author Emmanuel Bigeon
* *
*/ */
@SuppressWarnings("static-method")
public class ScriptExecutionTest { public class ScriptExecutionTest {
/** /**
@ -64,20 +65,20 @@ public class ScriptExecutionTest {
try { try {
CommandTestingApplication application = new CommandTestingApplication(); CommandTestingApplication application = new CommandTestingApplication();
application.add(new ScriptExecution("script", application.add(new ScriptExecution("script", //$NON-NLS-1$
application.getApplication(), "#")); application.getApplication(), "#")); //$NON-NLS-1$
application.sendCommand("script src/test/resources/script1.txt"); application.sendCommand("script src/test/resources/script1.txt"); //$NON-NLS-1$
application.sendCommand("script src/test/resources/script2.txt"); application.sendCommand("script src/test/resources/script2.txt"); //$NON-NLS-1$
application.sendCommand("script src/test/resources/script3.txt"); application.sendCommand("script src/test/resources/script3.txt"); //$NON-NLS-1$
application.sendCommand("script src/test/resources/script4.txt"); application.sendCommand("script src/test/resources/script4.txt"); //$NON-NLS-1$
application application
.sendCommand("script src/test/resources/script5.txt test"); .sendCommand("script src/test/resources/script5.txt test"); //$NON-NLS-1$
application.waitAndStop(); application.waitAndStop();
} catch (IOException | InvalidCommandName e) { } catch (IOException | InvalidCommandName e) {
fail("Unexpected exception"); fail("Unexpected exception " + e.getLocalizedMessage()); //$NON-NLS-1$
} }
} }