Adapted applications to make only attachement to the framework
Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
parent
ab47d6573b
commit
8322454f72
@ -0,0 +1,28 @@
|
||||
/**
|
||||
* gclc:fr.bigeon.gclc.ApplicationAttachement.java
|
||||
* Created on: Apr 19, 2017
|
||||
*/
|
||||
package fr.bigeon.gclc;
|
||||
|
||||
import fr.bigeon.gclc.exception.InvalidCommandName;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* TODO
|
||||
*
|
||||
* @author Emmanuel Bigeon
|
||||
*
|
||||
*/
|
||||
public interface ApplicationAttachement {
|
||||
/** Attach this object to a console application.
|
||||
* <p>
|
||||
* The attaching usually consist in the addition of commands in the console
|
||||
* application. The attached command should be specific to the attachement
|
||||
* (typically, the generic help command or the script command should not be
|
||||
* added through this mechanism).
|
||||
*
|
||||
* @param application the application
|
||||
* @throws InvalidCommandName if a command name is invalid for the
|
||||
* application. */
|
||||
void attach(ConsoleApplication application) throws InvalidCommandName;
|
||||
}
|
@ -73,19 +73,19 @@ import fr.bigeon.gclc.manager.ConsoleManager;
|
||||
* start method.
|
||||
*
|
||||
* @author Emmanuel BIGEON */
|
||||
public class ConsoleApplication implements ICommandProvider {
|
||||
public final class ConsoleApplication implements ICommandProvider {
|
||||
|
||||
/** The class logger */
|
||||
private static final Logger LOGGER = Logger
|
||||
.getLogger(ConsoleApplication.class.getName());
|
||||
/** The welcome message */
|
||||
private final String header;
|
||||
public final String header;
|
||||
/** The good bye message */
|
||||
private final String footer;
|
||||
public final String footer;
|
||||
/** The console manager */
|
||||
protected final ConsoleManager manager;
|
||||
public final ConsoleManager manager;
|
||||
/** The container of commands */
|
||||
private final SubedCommand root;
|
||||
public final SubedCommand root;
|
||||
/** The state of this application */
|
||||
private boolean running;
|
||||
/** The listeners */
|
||||
@ -135,11 +135,6 @@ public class ConsoleApplication implements ICommandProvider {
|
||||
return root.get(command);
|
||||
}
|
||||
|
||||
/** @return the manager */
|
||||
public final ConsoleManager getManager() {
|
||||
return manager;
|
||||
}
|
||||
|
||||
/** @param cmd the command
|
||||
* @throws IOException if the command could not be parsed */
|
||||
public final void interpretCommand(String cmd) throws IOException {
|
||||
@ -161,7 +156,7 @@ public class ConsoleApplication implements ICommandProvider {
|
||||
.getString("ConsoleApplication.cmd.failed", cmd)); //$NON-NLS-1$
|
||||
manager.println(e.getLocalizedMessage());
|
||||
if (e.getType() == CommandRunExceptionType.USAGE) {
|
||||
e.getSource().help(getManager());
|
||||
e.getSource().help(manager);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -247,19 +242,4 @@ public class ConsoleApplication implements ICommandProvider {
|
||||
public boolean isRunning() {
|
||||
return running;
|
||||
}
|
||||
|
||||
/** @return the root */
|
||||
public SubedCommand getRoot() {
|
||||
return root;
|
||||
}
|
||||
|
||||
/** @return the header */
|
||||
public String getHeader() {
|
||||
return header;
|
||||
}
|
||||
|
||||
/** @return the footer */
|
||||
public String getFooter() {
|
||||
return footer;
|
||||
}
|
||||
}
|
||||
|
@ -184,4 +184,11 @@ public class CommandParameters {
|
||||
public Set<String> getStringArgumentKeys() {
|
||||
return stringArguments.keySet();
|
||||
}
|
||||
|
||||
/** @param key the key
|
||||
* @return if the key is present in string arguments or boolean ones. */
|
||||
public boolean hasArgument(String key) {
|
||||
return stringArguments.containsKey(key) ||
|
||||
booleanArguments.containsKey(key);
|
||||
}
|
||||
}
|
||||
|
@ -247,9 +247,10 @@ public abstract class ParametrizedCommand extends Command {
|
||||
for (final String string : toProvide) {
|
||||
String value;
|
||||
try {
|
||||
value = manager.prompt(string);
|
||||
value = manager.prompt("value of " + string + "? ");
|
||||
while (value.isEmpty()) {
|
||||
value = manager.prompt(string);
|
||||
value = manager.prompt(
|
||||
"value of " + string + "? (cannot be empty) ");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new CommandRunException(
|
||||
|
@ -54,14 +54,15 @@ import fr.bigeon.gclc.manager.PipedConsoleManager;
|
||||
@SuppressWarnings("javadoc")
|
||||
public class CommandTestingApplication implements AutoCloseable {
|
||||
|
||||
private final ConsoleTestApplication application;
|
||||
private final ConsoleApplication application;
|
||||
private final Thread th;
|
||||
private final PipedConsoleManager manager;
|
||||
|
||||
/** @throws IOException if the streams cannot be build */
|
||||
public CommandTestingApplication() throws IOException {
|
||||
manager = new PipedConsoleManager();
|
||||
application = new ConsoleTestApplication(manager);
|
||||
application = new ConsoleApplication(manager, "", "");
|
||||
new ConsoleTestApplication().attach(application);
|
||||
th = new Thread(new Runnable() {
|
||||
|
||||
@SuppressWarnings("synthetic-access")
|
||||
@ -93,7 +94,7 @@ public class CommandTestingApplication implements AutoCloseable {
|
||||
}
|
||||
|
||||
/** @return the application */
|
||||
public ConsoleTestApplication getApplication() {
|
||||
public ConsoleApplication getApplication() {
|
||||
return application;
|
||||
}
|
||||
|
||||
|
@ -72,7 +72,7 @@ public class ConsoleApplicationTest {
|
||||
public void test() {
|
||||
|
||||
try (PipedConsoleManager manager = new PipedConsoleManager()) {
|
||||
ConsoleTestApplication app = new ConsoleTestApplication(manager);
|
||||
ConsoleApplication app = new ConsoleApplication(manager, "", "");
|
||||
app.exit();
|
||||
} catch (IOException e) {
|
||||
fail("System Console Manager failed");
|
||||
@ -85,7 +85,7 @@ public class ConsoleApplicationTest {
|
||||
try (CommandTestingApplication application = new CommandTestingApplication()) {
|
||||
|
||||
// remove welcome
|
||||
assertEquals(application.getApplication().getHeader(),
|
||||
assertEquals(application.getApplication().header,
|
||||
application.readNextLine());
|
||||
// Remove first prompt
|
||||
assertNull(application.readNextLine());
|
||||
@ -133,7 +133,7 @@ public class ConsoleApplicationTest {
|
||||
|
||||
assertNull(application.readNextLine());
|
||||
application.sendCommand("exit");
|
||||
assertEquals(application.getApplication().getFooter(),
|
||||
assertEquals(application.getApplication().footer,
|
||||
application.readNextLine());
|
||||
assertFalse(application.getApplication().isRunning());
|
||||
} catch (IOException e1) {
|
||||
|
@ -41,12 +41,11 @@ import fr.bigeon.gclc.command.ExitCommand;
|
||||
import fr.bigeon.gclc.command.HelpExecutor;
|
||||
import fr.bigeon.gclc.exception.CommandRunException;
|
||||
import fr.bigeon.gclc.exception.InvalidCommandName;
|
||||
import fr.bigeon.gclc.manager.ConsoleManager;
|
||||
|
||||
/** A test-purpose application
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
public class ConsoleTestApplication extends ConsoleApplication {
|
||||
public class ConsoleTestApplication implements ApplicationAttachement {
|
||||
|
||||
/** Exit command */
|
||||
public static final String EXIT = "exit"; //$NON-NLS-1$
|
||||
@ -54,14 +53,14 @@ public class ConsoleTestApplication extends ConsoleApplication {
|
||||
protected static final long TWO_SECONDS = 2000;
|
||||
|
||||
/** @param manager the manager */
|
||||
@Override
|
||||
@SuppressWarnings("nls")
|
||||
public ConsoleTestApplication(final ConsoleManager manager) {
|
||||
super(manager, "Welcome to the test application. Type help or test.",
|
||||
"See you");
|
||||
public void attach(final ConsoleApplication application) {
|
||||
try {
|
||||
add(new ExitCommand(EXIT, this));
|
||||
add(new HelpExecutor("help", manager, this.getRoot()));
|
||||
add(new Command("test") {
|
||||
application.add(new ExitCommand(EXIT, application));
|
||||
application.add(new HelpExecutor("help", application.manager,
|
||||
application.root));
|
||||
application.add(new Command("test") {
|
||||
|
||||
@Override
|
||||
public String tip() {
|
||||
@ -71,14 +70,14 @@ public class ConsoleTestApplication extends ConsoleApplication {
|
||||
@Override
|
||||
public void execute(String... args) throws CommandRunException {
|
||||
try {
|
||||
manager.println("Test command ran fine");
|
||||
application.manager.println("Test command ran fine");
|
||||
} catch (IOException e) {
|
||||
throw new CommandRunException("manager closed", e,
|
||||
this);
|
||||
}
|
||||
}
|
||||
});
|
||||
add(new Command("long") {
|
||||
application.add(new Command("long") {
|
||||
|
||||
@Override
|
||||
public String tip() {
|
||||
@ -88,9 +87,9 @@ public class ConsoleTestApplication extends ConsoleApplication {
|
||||
@Override
|
||||
public void execute(String... args) throws CommandRunException {
|
||||
try {
|
||||
manager.println("Waita minute");
|
||||
application.manager.println("Waita minute");
|
||||
Thread.sleep(TWO_SECONDS);
|
||||
manager.println("done!");
|
||||
application.manager.println("done!");
|
||||
} catch (IOException e) {
|
||||
throw new CommandRunException("manager closed", e,
|
||||
this);
|
||||
@ -100,7 +99,7 @@ public class ConsoleTestApplication extends ConsoleApplication {
|
||||
}
|
||||
}
|
||||
});
|
||||
add(new Command("failingCmd") {
|
||||
application.add(new Command("failingCmd") {
|
||||
|
||||
@Override
|
||||
public String tip() {
|
||||
|
@ -578,9 +578,16 @@ public class ParametrizedCommandTest {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
assertEquals(str1, test.readNextLine());
|
||||
assertEquals("value of " + str1 + "? ",
|
||||
test.readNextLine());
|
||||
test.type("");
|
||||
assertEquals(str1, test.readNextLine());
|
||||
assertEquals(
|
||||
"value of " + str1 + "? (cannot be empty) ",
|
||||
test.readNextLine());
|
||||
test.type("");
|
||||
assertEquals(
|
||||
"value of " + str1 + "? (cannot be empty) ",
|
||||
test.readNextLine());
|
||||
test.type(str2);
|
||||
} catch (IOException e) {
|
||||
assertNull(e);
|
||||
@ -598,7 +605,8 @@ public class ParametrizedCommandTest {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
assertEquals(str1, test.readNextLine());
|
||||
assertEquals("value of " + str1 + "? ",
|
||||
test.readNextLine());
|
||||
test.type(str2);
|
||||
} catch (IOException e) {
|
||||
assertNull(e);
|
||||
|
@ -47,6 +47,7 @@ import java.nio.charset.Charset;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import fr.bigeon.gclc.ConsoleApplication;
|
||||
import fr.bigeon.gclc.ConsoleTestApplication;
|
||||
import fr.bigeon.gclc.exception.CommandRunException;
|
||||
import fr.bigeon.gclc.exception.CommandRunExceptionType;
|
||||
@ -75,8 +76,9 @@ public class ScriptExecutionTest {
|
||||
assertNotNull(e2);
|
||||
return;
|
||||
}
|
||||
ConsoleTestApplication app = new ConsoleTestApplication(
|
||||
test);
|
||||
ConsoleApplication app = new ConsoleApplication(
|
||||
test, "", "");
|
||||
new ConsoleTestApplication().attach(app);
|
||||
ScriptExecution exec = new ScriptExecution("script", app, "#", //$NON-NLS-1$ //$NON-NLS-2$
|
||||
Charset.forName("UTF-8"));
|
||||
try {
|
||||
|
Loading…
Reference in New Issue
Block a user