Changed exit mechanism on gclc
This commit is contained in:
118
gclc/src/test/java/fr/bigeon/gclc/ConsoleApplicationTest.java
Normal file
118
gclc/src/test/java/fr/bigeon/gclc/ConsoleApplicationTest.java
Normal file
@@ -0,0 +1,118 @@
|
||||
/*
|
||||
* 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.ConsoleApplicationTest.java
|
||||
* Created on: Jun 9, 2016
|
||||
*/
|
||||
package fr.bigeon.gclc;
|
||||
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.PipedInputStream;
|
||||
import java.io.PipedOutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import fr.bigeon.gclc.manager.SystemConsoleManager;
|
||||
|
||||
/** Test class for ConsoleApplication
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
@SuppressWarnings("static-method")
|
||||
public class ConsoleApplicationTest {
|
||||
|
||||
protected static final long THREE_SECONDS = 3000;
|
||||
|
||||
/** Test the base of a console application */
|
||||
@Test
|
||||
public void test() {
|
||||
ConsoleTestApplication app = new ConsoleTestApplication(
|
||||
new SystemConsoleManager());
|
||||
app.exit();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void executionTest() {
|
||||
try {
|
||||
final PipedOutputStream src = new PipedOutputStream();
|
||||
InputStream in = new PipedInputStream(src);
|
||||
|
||||
final PipedInputStream snk = new PipedInputStream();
|
||||
PrintStream out = new PrintStream(new PipedOutputStream(snk));
|
||||
final ConsoleTestApplication app = new ConsoleTestApplication(
|
||||
new SystemConsoleManager(out, in));
|
||||
Thread th = new Thread(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
app.start();
|
||||
}
|
||||
});
|
||||
|
||||
th.start();
|
||||
|
||||
Thread test = new Thread(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try (PrintWriter writer = new PrintWriter(src, true)) {
|
||||
writer.println("test");
|
||||
writer.println("long");
|
||||
writer.println("exit");
|
||||
}
|
||||
}
|
||||
});
|
||||
test.start();
|
||||
try {
|
||||
th.join();
|
||||
} catch (InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
test.join();
|
||||
} catch (InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
fail("pipe creation"); //$NON-NLS-1$
|
||||
}
|
||||
}
|
||||
}
|
||||
105
gclc/src/test/java/fr/bigeon/gclc/ConsoleTestApplication.java
Normal file
105
gclc/src/test/java/fr/bigeon/gclc/ConsoleTestApplication.java
Normal file
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
package fr.bigeon.gclc;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import fr.bigeon.gclc.ConsoleApplication;
|
||||
import fr.bigeon.gclc.command.Command;
|
||||
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 {
|
||||
|
||||
/** Exit command */
|
||||
public static final String EXIT = "exit"; //$NON-NLS-1$
|
||||
/** Two seconds in milliseconds */
|
||||
protected static final long TWO_SECONDS = 2000;
|
||||
|
||||
/** @param manager the manager */
|
||||
@SuppressWarnings("nls")
|
||||
public ConsoleTestApplication(final ConsoleManager manager) {
|
||||
super(manager, "Welcome to the test application. Type help or test.",
|
||||
"See you");
|
||||
try {
|
||||
add(new ExitCommand(EXIT, this));
|
||||
add(new HelpExecutor("help", manager, this.getRoot()));
|
||||
add(new Command("test") {
|
||||
|
||||
@Override
|
||||
public String tip() {
|
||||
return "A test command";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(String... args) throws CommandRunException {
|
||||
try {
|
||||
manager.println("Test command ran fine");
|
||||
} catch (IOException e) {
|
||||
throw new CommandRunException("manager closed", e);
|
||||
}
|
||||
}
|
||||
});
|
||||
add(new Command("long") {
|
||||
|
||||
@Override
|
||||
public String tip() {
|
||||
return "A long execution command";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(String... args) throws CommandRunException {
|
||||
try {
|
||||
manager.println("Waita minute");
|
||||
Thread.sleep(TWO_SECONDS);
|
||||
manager.println("done!");
|
||||
} catch (IOException e) {
|
||||
throw new CommandRunException("manager closed", e);
|
||||
} catch (InterruptedException e) {
|
||||
throw new CommandRunException("wait interrupted", e);
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (final InvalidCommandName e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
137
gclc/src/test/java/fr/bigeon/gclc/GCLCConstantsTest.java
Normal file
137
gclc/src/test/java/fr/bigeon/gclc/GCLCConstantsTest.java
Normal file
@@ -0,0 +1,137 @@
|
||||
/*
|
||||
* 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.GCLCConstantsTest.java
|
||||
* Created on: Jun 8, 2016
|
||||
*/
|
||||
package fr.bigeon.gclc;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import fr.bigeon.gclc.exception.CommandParsingException;
|
||||
|
||||
/** Test class for {@link GCLCConstants}
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
@SuppressWarnings({"nls", "static-method"})
|
||||
public class GCLCConstantsTest {
|
||||
|
||||
/**
|
||||
* Test method for {@link fr.bigeon.gclc.GCLCConstants#splitCommand(java.lang.String)}.
|
||||
*/
|
||||
@Test
|
||||
public void testSplitCommand() {
|
||||
List<String> res;
|
||||
try {
|
||||
res = GCLCConstants.splitCommand("aCommand");
|
||||
} catch (CommandParsingException e) {
|
||||
fail("Unable to parse simple command"); //$NON-NLS-1$
|
||||
return;
|
||||
}
|
||||
assertTrue(res.size() == 1);
|
||||
assertTrue(res.get(0).equals("aCommand"));
|
||||
|
||||
try {
|
||||
res = GCLCConstants.splitCommand("aCommand with some arguments");
|
||||
} catch (CommandParsingException e) {
|
||||
fail("Unable to parse command with arguments"); //$NON-NLS-1$
|
||||
return;
|
||||
}
|
||||
assertTrue(res.size() == 4);
|
||||
assertTrue(res.get(0).equals("aCommand"));
|
||||
assertTrue(res.get(1).equals("with"));
|
||||
assertTrue(res.get(2).equals("some"));
|
||||
assertTrue(res.get(3).equals("arguments"));
|
||||
try {
|
||||
res = GCLCConstants.splitCommand("aCommand with some arguments");
|
||||
} catch (CommandParsingException e) {
|
||||
fail("Unable to parse command with arguments and double whitspaces"); //$NON-NLS-1$
|
||||
return;
|
||||
}
|
||||
assertTrue(res.size() == 4);
|
||||
assertTrue(res.get(0).equals("aCommand"));
|
||||
assertTrue(res.get(1).equals("with"));
|
||||
assertTrue(res.get(2).equals("some"));
|
||||
assertTrue(res.get(3).equals("arguments"));
|
||||
try {
|
||||
res = GCLCConstants
|
||||
.splitCommand("aCommand \"with some\" arguments");
|
||||
} catch (CommandParsingException e) {
|
||||
fail("Unable to parse command with string argument"); //$NON-NLS-1$
|
||||
return;
|
||||
}
|
||||
assertTrue(res.size() == 3);
|
||||
assertTrue(res.get(0).equals("aCommand"));
|
||||
assertTrue(res.get(1).equals("with some"));
|
||||
assertTrue(res.get(2).equals("arguments"));
|
||||
try {
|
||||
res = GCLCConstants.splitCommand("aCommand with\\ some arguments");
|
||||
} catch (CommandParsingException e) {
|
||||
fail("Unable to parse command with arguments with escaped whitspaces"); //$NON-NLS-1$
|
||||
return;
|
||||
}
|
||||
assertTrue(res.size() == 3);
|
||||
assertTrue(res.get(0).equals("aCommand"));
|
||||
assertTrue(res.get(1).equals("with some"));
|
||||
assertTrue(res.get(2).equals("arguments"));
|
||||
try {
|
||||
res = GCLCConstants
|
||||
.splitCommand("aCommand wi\\\"th some arguments");
|
||||
} catch (CommandParsingException e) {
|
||||
fail("Unable to parse command with string argument"); //$NON-NLS-1$
|
||||
return;
|
||||
}
|
||||
assertTrue(res.size() == 4);
|
||||
assertTrue(res.get(0).equals("aCommand"));
|
||||
assertTrue(res.get(1).equals("wi\"th"));
|
||||
assertTrue(res.get(2).equals("some"));
|
||||
assertTrue(res.get(3).equals("arguments"));
|
||||
|
||||
// Wrong lines?
|
||||
try {
|
||||
res = GCLCConstants
|
||||
.splitCommand("aCommand with \"some ar\"guments");
|
||||
fail("Parsing argument with string cut");
|
||||
} catch (CommandParsingException e) {
|
||||
// OK
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user