Создание jUnit тестов
This commit is contained in:
@@ -16,19 +16,19 @@ public class CommandLoop implements Runnable {
|
||||
private static final String[] EMPTY_ARGS = new String[0];
|
||||
private static final String RED = Ansi.ansi().fgBright(Ansi.Color.RED).toString();
|
||||
private ConsoleReader console;
|
||||
private boolean run;
|
||||
private Shell shell;
|
||||
protected Map<String, Command> commandMap = new HashMap<>();
|
||||
|
||||
public CommandLoop(ConsoleReader consoleReader) {
|
||||
console = consoleReader;
|
||||
public CommandLoop( Shell shell) {
|
||||
this.shell = shell;
|
||||
this.console = shell.console;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
Thread loop = Thread.currentThread();
|
||||
run = true;
|
||||
|
||||
while (run && !loop.isInterrupted()) {
|
||||
while (shell.run && !loop.isInterrupted()) {
|
||||
try {
|
||||
String line;
|
||||
if ((line = console.readLine()) != null) {
|
||||
@@ -59,10 +59,8 @@ public class CommandLoop implements Runnable {
|
||||
// чтобы не нагружать процессор
|
||||
safeSleep(1);
|
||||
}
|
||||
}
|
||||
|
||||
public void shutdown() {
|
||||
run = false;
|
||||
shell.run = false;
|
||||
}
|
||||
|
||||
private void safeSleep(long millis) {
|
||||
|
||||
@@ -18,9 +18,10 @@ public class Shell {
|
||||
private PrintStream sysOut, sysErr;
|
||||
private ShellPrintStream newErr;
|
||||
private String promt;
|
||||
private ConsoleReader console;
|
||||
protected ConsoleReader console;
|
||||
private CommandLoop commandLoop;
|
||||
private CommandCompleter commandCompleter;
|
||||
protected boolean run = false;
|
||||
|
||||
public void start() throws IOException, InterruptedException {
|
||||
overrideSysErr();
|
||||
@@ -30,7 +31,7 @@ public class Shell {
|
||||
console.setPrompt(ConsoleReader.RESET_LINE + promt);
|
||||
console.addCompleter((commandCompleter = new CommandCompleter()));
|
||||
newErr.setConsoleReader(console);
|
||||
commandLoop = new CommandLoop(console);
|
||||
commandLoop = new CommandLoop(this);
|
||||
|
||||
if (!commandLoop.commandMap.containsKey("exit")) {
|
||||
addCommand(new ExitCommand());
|
||||
@@ -39,10 +40,11 @@ public class Shell {
|
||||
Thread loopCommandReader = new Thread(commandLoop, "Command reader loop");
|
||||
loopCommandReader.join();
|
||||
loopCommandReader.start();
|
||||
run = true;
|
||||
}
|
||||
|
||||
public void shutdown() {
|
||||
commandLoop.shutdown();
|
||||
run = false;
|
||||
|
||||
newErr.setConsoleReader(null);
|
||||
console.shutdown();
|
||||
@@ -65,6 +67,10 @@ public class Shell {
|
||||
commandCompleter.stringsCompleter.getStrings().add(name);
|
||||
}
|
||||
|
||||
public boolean isRunning() {
|
||||
return run;
|
||||
}
|
||||
|
||||
/**
|
||||
* Подмена стандартных 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