diff --git a/build.gradle b/build.gradle index ecd8f8e..b527a5c 100644 --- a/build.gradle +++ b/build.gradle @@ -11,4 +11,5 @@ repositories { dependencies { compile (['jline:jline:2.13']) + testCompile(['junit:junit:4.12']) } diff --git a/src/main/java/ru/dmitriymx/shell/CommandLoop.java b/src/main/java/ru/dmitriymx/shell/CommandLoop.java index 4dd786e..d02c886 100644 --- a/src/main/java/ru/dmitriymx/shell/CommandLoop.java +++ b/src/main/java/ru/dmitriymx/shell/CommandLoop.java @@ -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 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) { diff --git a/src/main/java/ru/dmitriymx/shell/Shell.java b/src/main/java/ru/dmitriymx/shell/Shell.java index 0e2edba..f7ad70f 100644 --- a/src/main/java/ru/dmitriymx/shell/Shell.java +++ b/src/main/java/ru/dmitriymx/shell/Shell.java @@ -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 */ diff --git a/src/test/java/test/TestShell.java b/src/test/java/test/TestShell.java new file mode 100644 index 0000000..d4f09d8 --- /dev/null +++ b/src/test/java/test/TestShell.java @@ -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 + * 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")); + } +} \ No newline at end of file