Создание jUnit тестов
This commit is contained in:
@@ -11,4 +11,5 @@ repositories {
|
|||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
compile (['jline:jline:2.13'])
|
compile (['jline:jline:2.13'])
|
||||||
|
testCompile(['junit:junit:4.12'])
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,19 +16,19 @@ public class CommandLoop implements Runnable {
|
|||||||
private static final String[] EMPTY_ARGS = new String[0];
|
private static final String[] EMPTY_ARGS = new String[0];
|
||||||
private static final String RED = Ansi.ansi().fgBright(Ansi.Color.RED).toString();
|
private static final String RED = Ansi.ansi().fgBright(Ansi.Color.RED).toString();
|
||||||
private ConsoleReader console;
|
private ConsoleReader console;
|
||||||
private boolean run;
|
private Shell shell;
|
||||||
protected Map<String, Command> commandMap = new HashMap<>();
|
protected Map<String, Command> commandMap = new HashMap<>();
|
||||||
|
|
||||||
public CommandLoop(ConsoleReader consoleReader) {
|
public CommandLoop( Shell shell) {
|
||||||
console = consoleReader;
|
this.shell = shell;
|
||||||
|
this.console = shell.console;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
Thread loop = Thread.currentThread();
|
Thread loop = Thread.currentThread();
|
||||||
run = true;
|
|
||||||
|
|
||||||
while (run && !loop.isInterrupted()) {
|
while (shell.run && !loop.isInterrupted()) {
|
||||||
try {
|
try {
|
||||||
String line;
|
String line;
|
||||||
if ((line = console.readLine()) != null) {
|
if ((line = console.readLine()) != null) {
|
||||||
@@ -59,10 +59,8 @@ public class CommandLoop implements Runnable {
|
|||||||
// чтобы не нагружать процессор
|
// чтобы не нагружать процессор
|
||||||
safeSleep(1);
|
safeSleep(1);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void shutdown() {
|
shell.run = false;
|
||||||
run = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void safeSleep(long millis) {
|
private void safeSleep(long millis) {
|
||||||
|
|||||||
@@ -18,9 +18,10 @@ public class Shell {
|
|||||||
private PrintStream sysOut, sysErr;
|
private PrintStream sysOut, sysErr;
|
||||||
private ShellPrintStream newErr;
|
private ShellPrintStream newErr;
|
||||||
private String promt;
|
private String promt;
|
||||||
private ConsoleReader console;
|
protected ConsoleReader console;
|
||||||
private CommandLoop commandLoop;
|
private CommandLoop commandLoop;
|
||||||
private CommandCompleter commandCompleter;
|
private CommandCompleter commandCompleter;
|
||||||
|
protected boolean run = false;
|
||||||
|
|
||||||
public void start() throws IOException, InterruptedException {
|
public void start() throws IOException, InterruptedException {
|
||||||
overrideSysErr();
|
overrideSysErr();
|
||||||
@@ -30,7 +31,7 @@ public class Shell {
|
|||||||
console.setPrompt(ConsoleReader.RESET_LINE + promt);
|
console.setPrompt(ConsoleReader.RESET_LINE + promt);
|
||||||
console.addCompleter((commandCompleter = new CommandCompleter()));
|
console.addCompleter((commandCompleter = new CommandCompleter()));
|
||||||
newErr.setConsoleReader(console);
|
newErr.setConsoleReader(console);
|
||||||
commandLoop = new CommandLoop(console);
|
commandLoop = new CommandLoop(this);
|
||||||
|
|
||||||
if (!commandLoop.commandMap.containsKey("exit")) {
|
if (!commandLoop.commandMap.containsKey("exit")) {
|
||||||
addCommand(new ExitCommand());
|
addCommand(new ExitCommand());
|
||||||
@@ -39,10 +40,11 @@ public class Shell {
|
|||||||
Thread loopCommandReader = new Thread(commandLoop, "Command reader loop");
|
Thread loopCommandReader = new Thread(commandLoop, "Command reader loop");
|
||||||
loopCommandReader.join();
|
loopCommandReader.join();
|
||||||
loopCommandReader.start();
|
loopCommandReader.start();
|
||||||
|
run = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void shutdown() {
|
public void shutdown() {
|
||||||
commandLoop.shutdown();
|
run = false;
|
||||||
|
|
||||||
newErr.setConsoleReader(null);
|
newErr.setConsoleReader(null);
|
||||||
console.shutdown();
|
console.shutdown();
|
||||||
@@ -65,6 +67,10 @@ public class Shell {
|
|||||||
commandCompleter.stringsCompleter.getStrings().add(name);
|
commandCompleter.stringsCompleter.getStrings().add(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isRunning() {
|
||||||
|
return run;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Подмена стандартных SysErr и SysOut
|
* Подмена стандартных SysErr и SysOut
|
||||||
*/
|
*/
|
||||||
|
|||||||
88
src/test/java/test/TestShell.java
Normal file
88
src/test/java/test/TestShell.java
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
package test;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import ru.dmitriymx.shell.Shell;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author DmitriyMX <mail@dmitriymx.ru>
|
||||||
|
* 2015
|
||||||
|
*/
|
||||||
|
public class TestShell {
|
||||||
|
PrintStream origOut;
|
||||||
|
PrintStream origErr;
|
||||||
|
InputStream origIn;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void overrideSys() {
|
||||||
|
origOut = System.out;
|
||||||
|
origErr = System.err;
|
||||||
|
origIn = System.in;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void returnSys() {
|
||||||
|
System.setIn(origIn);
|
||||||
|
System.setOut(origOut);
|
||||||
|
System.setErr(origErr);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSysOutErr() throws IOException, InterruptedException {
|
||||||
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
|
PrintStream shellout = new PrintStream(baos);
|
||||||
|
System.setOut(shellout);
|
||||||
|
System.setErr(shellout);
|
||||||
|
|
||||||
|
ByteArrayInputStream bais = new ByteArrayInputStream("qwe\nexit\n".getBytes());
|
||||||
|
System.setIn(bais);
|
||||||
|
|
||||||
|
Shell shell = new Shell();
|
||||||
|
shell.start();
|
||||||
|
|
||||||
|
while (shell.isRunning()) {
|
||||||
|
Thread.sleep(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
String resultOut = baos.toString();
|
||||||
|
returnSys();
|
||||||
|
|
||||||
|
assertTrue(resultOut.contains("Unknown command"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// not work in gradle
|
||||||
|
@Test
|
||||||
|
public void testLoggerOut() throws IOException, InterruptedException {
|
||||||
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
|
PrintStream shellout = new PrintStream(baos);
|
||||||
|
System.setOut(shellout);
|
||||||
|
System.setErr(shellout);
|
||||||
|
|
||||||
|
ByteArrayInputStream bais = new ByteArrayInputStream("qwe\nexit\n".getBytes());
|
||||||
|
System.setIn(bais);
|
||||||
|
|
||||||
|
Shell shell = new Shell();
|
||||||
|
shell.start();
|
||||||
|
|
||||||
|
Logger logger = Logger.getLogger("testLogger");
|
||||||
|
logger.info("Hello");
|
||||||
|
logger.warning("world");
|
||||||
|
logger.severe("123");
|
||||||
|
|
||||||
|
while (shell.isRunning()) {
|
||||||
|
Thread.sleep(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
String resultOut = baos.toString();
|
||||||
|
returnSys();
|
||||||
|
|
||||||
|
assertTrue(resultOut.contains("Hello"));
|
||||||
|
assertTrue(resultOut.contains("world"));
|
||||||
|
assertTrue(resultOut.contains("123"));
|
||||||
|
assertTrue(resultOut.contains("Unknown command"));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user