private UciProtocol createUciProtocol(String[] commands) { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); PrintStream stream = new PrintStream(buffer); try { for (String command : commands) { stream.println(command); } } finally { if (stream != null) { stream.close(); } } return new UciProtocol( new BufferedReader(new InputStreamReader(new ByteArrayInputStream(buffer.toByteArray()))), new PrintStream(new ByteArrayOutputStream())); }
@Test public void canFollowLogfile() throws IOException { File tempFile = File.createTempFile("commons-io", "", new File(System.getProperty("java.io.tmpdir"))); tempFile.deleteOnExit(); System.out.println("Temp file = " + tempFile.getAbsolutePath()); PrintStream log = new PrintStream(tempFile); LogfileFollower follower = new LogfileFollower(tempFile); List<String> lines; // Empty file: lines = follower.newLines(); assertEquals(0, lines.size()); // Write two lines: log.println("Line 1"); log.println("Line 2"); lines = follower.newLines(); assertEquals(2, lines.size()); assertEquals("Line 2", lines.get(1)); // Write one more line: log.println("Line 3"); lines = follower.newLines(); assertEquals(1, lines.size()); assertEquals("Line 3", lines.get(0)); // Write one and a half line and finish later: log.println("Line 4"); log.print("Line 5 begin"); lines = follower.newLines(); assertEquals(1, lines.size()); // End last line and start a new one: log.println(" end"); log.print("Line 6 begin"); lines = follower.newLines(); assertEquals(1, lines.size()); assertEquals("Line 5 begin end", lines.get(0)); // End last line: log.println(" end"); lines = follower.newLines(); assertEquals(1, lines.size()); assertEquals("Line 6 begin end", lines.get(0)); // A line only missing a newline: log.print("Line 7"); lines = follower.newLines(); assertEquals(0, lines.size()); log.println(); lines = follower.newLines(); assertEquals(1, lines.size()); assertEquals("Line 7", lines.get(0)); // Delete: log.close(); lines = follower.newLines(); assertEquals(0, lines.size()); }