From 00d89dcda54a028de885a67746f9d16886d198e1 Mon Sep 17 00:00:00 2001 From: DmitriyMX Date: Mon, 14 Dec 2015 16:45:20 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20jUnit=20=D1=82=D0=B5=D1=81=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 1 + .../java/ru/dmitriymx/shell/CommandLoop.java | 14 ++- src/main/java/ru/dmitriymx/shell/Shell.java | 12 ++- src/test/java/test/TestShell.java | 88 +++++++++++++++++++ 4 files changed, 104 insertions(+), 11 deletions(-) create mode 100644 src/test/java/test/TestShell.java 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