Code compliance

This commit is contained in:
Emmanuel Bigeon 2016-06-12 16:07:14 -04:00
parent 3c1c8e85c8
commit e04e7ceaa5
5 changed files with 103 additions and 34 deletions

View File

@ -1,3 +1,37 @@
/*
* Copyright Bigeon Emmanuel (2014)
*
* emmanuel@bigeon.fr
*
* This software is a computer program whose purpose is to
* provide a generic framework for console applications.
*
* This software is governed by the CeCILL license under French law and
* abiding by the rules of distribution of free software. You can use,
* modify and/or redistribute the software under the terms of the CeCILL
* license as circulated by CEA, CNRS and INRIA at the following URL
* "http://www.cecill.info".
*
* As a counterpart to the access to the source code and rights to copy,
* modify and redistribute granted by the license, users are provided only
* with a limited warranty and the software's author, the holder of the
* economic rights, and the successive licensors have only limited
* liability.
*
* In this respect, the user's attention is drawn to the risks associated
* with loading, using, modifying and/or developing or reproducing the
* software by the user in light of its specific status of free software,
* that may mean that it is complicated to manipulate, and that also
* therefore means that it is reserved for developers and experienced
* professionals having in-depth computer knowledge. Users are therefore
* encouraged to load and test the software's suitability as regards their
* requirements in conditions enabling the security of their systems and/or
* data to be ensured and, more generally, to use and operate it in the
* same conditions as regards security.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL license and that you accept its terms.
*/
/** /**
* gclc:fr.bigeon.gclc.command.ScriptExecution.java * gclc:fr.bigeon.gclc.command.ScriptExecution.java
* Created on: Jun 12, 2016 * Created on: Jun 12, 2016
@ -18,18 +52,22 @@ import fr.bigeon.gclc.exception.CommandParsingException;
import fr.bigeon.gclc.exception.CommandRunException; import fr.bigeon.gclc.exception.CommandRunException;
import fr.bigeon.gclc.exception.CommandRunExceptionType; import fr.bigeon.gclc.exception.CommandRunExceptionType;
/** <p> /** A command that will laucnh a series of command from a file
* TODO * <p>
* This command will read a file and execute each non empty non commented line
* as a command of the application.
* *
* @author Emmanuel Bigeon */ * @author Emmanuel Bigeon */
public class ScriptExecution extends Command { public class ScriptExecution extends Command {
/** The application */
private final ConsoleApplication application; private final ConsoleApplication application;
/** The commenting prefix */
private final String commentPrefix; private final String commentPrefix;
/** @param name /** @param name the name of the command
* @param application * @param application the application
* @param commentPrefix */ * @param commentPrefix the comment prefix in the script files */
public ScriptExecution(String name, ConsoleApplication application, public ScriptExecution(String name, ConsoleApplication application,
String commentPrefix) { String commentPrefix) {
super(name); super(name);
@ -39,11 +77,12 @@ public class ScriptExecution extends Command {
/* (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 {
if (args.length == 0) { if (args.length == 0) {
throw new CommandRunException(CommandRunExceptionType.USAGE, throw new CommandRunException(CommandRunExceptionType.USAGE,
"Expecting a file", this); "Expecting a file", this); //$NON-NLS-1$
} }
List<String> argsList = Arrays.asList(args); List<String> argsList = Arrays.asList(args);
String scriptFile = argsList.remove(0); String scriptFile = argsList.remove(0);
@ -55,9 +94,11 @@ public class ScriptExecution extends Command {
params[i - 1] = args[i]; params[i - 1] = args[i];
} }
while ((cmd = reader.readLine()) != null) { while ((cmd = reader.readLine()) != null) {
if (cmd.startsWith(" ") || cmd.startsWith("\t")) { if (cmd.startsWith(" ") || cmd.startsWith("\t")) { //$NON-NLS-1$ //$NON-NLS-2$
reader.close();
fReader.close();
throw new CommandRunException( throw new CommandRunException(
"Invalid command in script (line starts with space character)", "Invalid command in script (line starts with space character)", //$NON-NLS-1$
this); this);
} }
if (cmd.isEmpty() || cmd.startsWith(commentPrefix)) { if (cmd.isEmpty() || cmd.startsWith(commentPrefix)) {
@ -69,12 +110,12 @@ public class ScriptExecution extends Command {
application.executeSub(command, ps.toArray(new String[0])); application.executeSub(command, ps.toArray(new String[0]));
} }
} catch (CommandParsingException e) { } catch (CommandParsingException e) {
throw new CommandRunException("Invalid command in script (" + throw new CommandRunException("Invalid command in script (" + //$NON-NLS-1$
e.getLocalizedMessage() + ")", e.getLocalizedMessage() + ")", //$NON-NLS-1$
e, this); e, this);
} catch (IOException e) { } catch (IOException e) {
throw new CommandRunException( throw new CommandRunException(
"Unable to read script (" + e.getLocalizedMessage() + ")", "Unable to read script (" + e.getLocalizedMessage() + ")", //$NON-NLS-1$ //$NON-NLS-2$
e, this); e, this);
} }
} }
@ -85,17 +126,17 @@ public class ScriptExecution extends Command {
protected String usageDetail() { protected String usageDetail() {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
builder.append( builder.append(
" scriptfile: path to the file containing the script to execute."); " scriptfile: path to the file containing the script to execute."); //$NON-NLS-1$
builder.append(System.lineSeparator()); builder.append(System.lineSeparator());
builder.append(System.lineSeparator()); builder.append(System.lineSeparator());
builder.append( builder.append(
" The script file must contain one line commands. The lines must never"); " The script file must contain one line commands. The lines must never"); //$NON-NLS-1$
builder.append(System.lineSeparator()); builder.append(System.lineSeparator());
builder.append( builder.append(
"start with whitespace characters. The lines starting with"); "start with whitespace characters. The lines starting with"); //$NON-NLS-1$
builder.append(System.lineSeparator()); builder.append(System.lineSeparator());
builder.append("\"" + commentPrefix + builder.append("\"" + commentPrefix + //$NON-NLS-1$
"\" will be ignored as well as empty lines."); "\" will be ignored as well as empty lines."); //$NON-NLS-1$
builder.append(System.lineSeparator()); builder.append(System.lineSeparator());
return builder.toString(); return builder.toString();
@ -105,7 +146,7 @@ public class ScriptExecution 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 "Execute a script"; return "Execute a script"; //$NON-NLS-1$
} }
} }

View File

@ -51,11 +51,13 @@ public class CommandRunException extends Exception {
*/ */
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
/** The type of run exception */
private final CommandRunExceptionType type; private final CommandRunExceptionType type;
/** The command that caused the error */ /** The command that caused the error */
private final ICommand source; private final ICommand source;
/** @param message a message */ /** @param message a message
* @param source the source */
public CommandRunException(String message, ICommand source) { public CommandRunException(String message, ICommand source) {
super(message); super(message);
type = CommandRunExceptionType.EXECUTION; type = CommandRunExceptionType.EXECUTION;
@ -63,7 +65,8 @@ public class CommandRunException extends Exception {
} }
/** @param message a message /** @param message a message
* @param cause the cause */ * @param cause the cause
* @param source the source */
public CommandRunException(String message, Throwable cause, public CommandRunException(String message, Throwable cause,
ICommand source) { ICommand source) {
super(message, cause); super(message, cause);
@ -72,7 +75,8 @@ public class CommandRunException extends Exception {
} }
/** @param type the type of exception /** @param type the type of exception
* @param message the message */ * @param message the message
* @param source the source */
public CommandRunException(CommandRunExceptionType type, String message, public CommandRunException(CommandRunExceptionType type, String message,
ICommand source) { ICommand source) {
super(message); super(message);
@ -82,7 +86,8 @@ public class CommandRunException extends Exception {
/** @param type the type of exception /** @param type the type of exception
* @param message a message * @param message a message
* @param cause the cause */ * @param cause the cause
* @param source the source */
public CommandRunException(CommandRunExceptionType type, String message, public CommandRunException(CommandRunExceptionType type, String message,
Throwable cause, ICommand source) { Throwable cause, ICommand source) {
super(message, cause); super(message, cause);

View File

@ -1,3 +1,37 @@
/*
* Copyright Bigeon Emmanuel (2014)
*
* emmanuel@bigeon.fr
*
* This software is a computer program whose purpose is to
* provide a generic framework for console applications.
*
* This software is governed by the CeCILL license under French law and
* abiding by the rules of distribution of free software. You can use,
* modify and/or redistribute the software under the terms of the CeCILL
* license as circulated by CEA, CNRS and INRIA at the following URL
* "http://www.cecill.info".
*
* As a counterpart to the access to the source code and rights to copy,
* modify and redistribute granted by the license, users are provided only
* with a limited warranty and the software's author, the holder of the
* economic rights, and the successive licensors have only limited
* liability.
*
* In this respect, the user's attention is drawn to the risks associated
* with loading, using, modifying and/or developing or reproducing the
* software by the user in light of its specific status of free software,
* that may mean that it is complicated to manipulate, and that also
* therefore means that it is reserved for developers and experienced
* professionals having in-depth computer knowledge. Users are therefore
* encouraged to load and test the software's suitability as regards their
* requirements in conditions enabling the security of their systems and/or
* data to be ensured and, more generally, to use and operate it in the
* same conditions as regards security.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL license and that you accept its terms.
*/
/** /**
* gclc:fr.bigeon.gclc.exception.CommandRunExceptionType.java * gclc:fr.bigeon.gclc.exception.CommandRunExceptionType.java
* Created on: Jun 12, 2016 * Created on: Jun 12, 2016

View File

@ -137,17 +137,8 @@ public class SystemConsoleManager implements ConsoleManager { // NOSONAR
} }
String result = new String(); String result = new String();
out.print(message + ' '); out.print(message + ' ');
char c;
try { try {
result = in.readLine(); result = in.readLine();
// c = (char) in.read();
// while (c != System.lineSeparator().charAt(0)) {
// result += c;
// c = (char) in.read();
// }
// while (in.available() != 0) {
// in.read();
// }
} catch (final IOException e) { } catch (final IOException e) {
LOGGER.log(Level.SEVERE, "Unable to read prompt", e); //$NON-NLS-1$ LOGGER.log(Level.SEVERE, "Unable to read prompt", e); //$NON-NLS-1$
} }

View File

@ -44,7 +44,6 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.PipedInputStream; import java.io.PipedInputStream;
import java.io.PipedOutputStream; import java.io.PipedOutputStream;
import java.io.PrintStream;
import java.io.PrintWriter; import java.io.PrintWriter;
import org.junit.Test; import org.junit.Test;
@ -54,9 +53,10 @@ import fr.bigeon.gclc.manager.SystemConsoleManager;
/** Test class for ConsoleApplication /** Test class for ConsoleApplication
* *
* @author Emmanuel Bigeon */ * @author Emmanuel Bigeon */
@SuppressWarnings("static-method") @SuppressWarnings({"resource", "javadoc", "nls", "static-method"})
public class ConsoleApplicationTest { public class ConsoleApplicationTest {
/** 3 seconds in milliseconds */
protected static final long THREE_SECONDS = 3000; protected static final long THREE_SECONDS = 3000;
/** Test the base of a console application */ /** Test the base of a console application */
@ -73,8 +73,6 @@ public class ConsoleApplicationTest {
final PipedOutputStream src = new PipedOutputStream(); final PipedOutputStream src = new PipedOutputStream();
InputStream in = new PipedInputStream(src); InputStream in = new PipedInputStream(src);
final PipedInputStream snk = new PipedInputStream();
PrintStream out = new PrintStream(new PipedOutputStream(snk));
final ConsoleTestApplication app = new ConsoleTestApplication( final ConsoleTestApplication app = new ConsoleTestApplication(
new SystemConsoleManager(System.out, in)); new SystemConsoleManager(System.out, in));
Thread th = new Thread(new Runnable() { Thread th = new Thread(new Runnable() {