public void testOpenBufferedStream() throws IOException { BufferedReader reader = source.openBufferedStream(); assertTrue(source.wasStreamOpened()); assertFalse(source.wasStreamClosed()); StringWriter writer = new StringWriter(); char[] buf = new char[64]; int read; while ((read = reader.read(buf)) != -1) { writer.write(buf, 0, read); } reader.close(); writer.close(); assertTrue(source.wasStreamClosed()); assertEquals(STRING, writer.toString()); }
public void testCopyTo_appendable() throws IOException { StringBuilder builder = new StringBuilder(); assertEquals(STRING.length(), source.copyTo(builder)); assertTrue(source.wasStreamOpened() && source.wasStreamClosed()); assertEquals(STRING, builder.toString()); }
public void testLines() throws IOException { source = new TestCharSource(LINES); ImmutableList<String> lines; try (Stream<String> linesStream = source.lines()) { assertTrue(source.wasStreamOpened()); assertFalse(source.wasStreamClosed()); lines = linesStream.collect(toImmutableList()); } assertTrue(source.wasStreamClosed()); assertEquals(SPLIT_LINES, lines); }
public void testForEachLine() throws IOException { source = new TestCharSource(LINES); ImmutableList.Builder<String> builder = ImmutableList.builder(); source.forEachLine(builder::add); assertEquals(SPLIT_LINES, builder.build()); assertTrue(source.wasStreamOpened()); assertTrue(source.wasStreamClosed()); }
public void testReadLines_toList() throws IOException { TestCharSource lines = new TestCharSource(LINES); assertEquals(ImmutableList.of("foo", "bar", "baz", "something"), lines.readLines()); assertTrue(lines.wasStreamOpened() && lines.wasStreamClosed()); }
public void testReadLines_withProcessor() throws IOException { TestCharSource lines = new TestCharSource(LINES); List<String> list = lines.readLines( new LineProcessor<List<String>>() { List<String> list = Lists.newArrayList(); @Override public boolean processLine(String line) throws IOException { list.add(line); return true; } @Override public List<String> getResult() { return list; } }); assertEquals(ImmutableList.of("foo", "bar", "baz", "something"), list); assertTrue(lines.wasStreamOpened() && lines.wasStreamClosed()); }
public void testReadLines_withProcessor_stopsOnFalse() throws IOException { TestCharSource lines = new TestCharSource(LINES); List<String> list = lines.readLines( new LineProcessor<List<String>>() { List<String> list = Lists.newArrayList(); @Override public boolean processLine(String line) throws IOException { list.add(line); return false; } @Override public List<String> getResult() { return list; } }); assertEquals(ImmutableList.of("foo"), list); assertTrue(lines.wasStreamOpened() && lines.wasStreamClosed()); }
public void testRead_toString() throws IOException { assertEquals(STRING, source.read()); assertTrue(source.wasStreamOpened() && source.wasStreamClosed()); }
public void testClosesOnErrors_copyingToCharSinkThatThrows() { for (TestOption option : EnumSet.of(OPEN_THROWS, WRITE_THROWS, CLOSE_THROWS)) { TestCharSource okSource = new TestCharSource(STRING); try { okSource.copyTo(new TestCharSink(option)); fail(); } catch (IOException expected) { } // ensure reader was closed IF it was opened (depends on implementation whether or not it's // opened at all if sink.newWriter() throws). assertTrue( "stream not closed when copying to sink with option: " + option, !okSource.wasStreamOpened() || okSource.wasStreamClosed()); } }
public void testClosesOnErrors_whenReadThrows() { TestCharSource failSource = new TestCharSource(STRING, READ_THROWS); try { failSource.copyTo(new TestCharSink()); fail(); } catch (IOException expected) { } assertTrue(failSource.wasStreamClosed()); }
public void testReadFirstLine() throws IOException { TestCharSource lines = new TestCharSource(LINES); assertEquals("foo", lines.readFirstLine()); assertTrue(lines.wasStreamOpened() && lines.wasStreamClosed()); }
public void testClosesOnErrors_copyingToWriterThatThrows() { TestCharSource okSource = new TestCharSource(STRING); try { okSource.copyTo(new TestWriter(WRITE_THROWS)); fail(); } catch (IOException expected) { } assertTrue(okSource.wasStreamClosed()); }
public void testCopyTo_charSink() throws IOException { TestCharSink sink = new TestCharSink(); assertFalse(sink.wasStreamOpened() || sink.wasStreamClosed()); assertEquals(STRING.length(), source.copyTo(sink)); assertTrue(source.wasStreamOpened() && source.wasStreamClosed()); assertTrue(sink.wasStreamOpened() && sink.wasStreamClosed()); assertEquals(STRING, sink.getString()); }
public void testCopyTo_appendable() throws IOException { StringBuilder builder = new StringBuilder(); assertEquals(STRING.length(), source.copyTo(builder)); assertTrue(source.wasStreamOpened() && source.wasStreamClosed()); assertEquals(STRING, builder.toString()); }
public void testReadLines_toList() throws IOException { TestCharSource lines = new TestCharSource(LINES); assertEquals(ImmutableList.of("foo", "bar", "baz", "something"), lines.readLines()); assertTrue(lines.wasStreamOpened() && lines.wasStreamClosed()); }
public void testForEachLine() throws IOException { source = new TestCharSource(LINES); ImmutableList.Builder<String> builder = ImmutableList.builder(); source.forEachLine(builder::add); assertEquals(SPLIT_LINES, builder.build()); assertTrue(source.wasStreamOpened()); assertTrue(source.wasStreamClosed()); }
public void testClosesOnErrors_copyingToWriterThatThrows() { TestCharSource okSource = new TestCharSource(STRING); try { okSource.copyTo(new TestWriter(WRITE_THROWS)); fail(); } catch (IOException expected) { } assertTrue(okSource.wasStreamClosed()); }
public void testReadFirstLine() throws IOException { TestCharSource lines = new TestCharSource(LINES); assertEquals("foo", lines.readFirstLine()); assertTrue(lines.wasStreamOpened() && lines.wasStreamClosed()); }
public void testClosesOnErrors_whenReadThrows() { TestCharSource failSource = new TestCharSource(STRING, READ_THROWS); try { failSource.copyTo(new TestCharSink()); fail(); } catch (IOException expected) { } assertTrue(failSource.wasStreamClosed()); }
public void testCopyTo_charSink() throws IOException { TestCharSink sink = new TestCharSink(); assertFalse(sink.wasStreamOpened() || sink.wasStreamClosed()); assertEquals(STRING.length(), source.copyTo(sink)); assertTrue(source.wasStreamOpened() && source.wasStreamClosed()); assertTrue(sink.wasStreamOpened() && sink.wasStreamClosed()); assertEquals(STRING, sink.getString()); }