try { for (CharSource in : BROKEN_SOURCES) { runFailureTest(in, newNormalCharSink()); assertTrue(logHandler.getStoredLogRecords().isEmpty()); runFailureTest(in, BROKEN_CLOSE_SINK); assertEquals((in == BROKEN_OPEN_SOURCE) ? 0 : 1, getAndResetRecords(logHandler)); runFailureTest(newNormalCharSource(), out); assertTrue(logHandler.getStoredLogRecords().isEmpty()); runFailureTest(BROKEN_CLOSE_SOURCE, out); assertEquals(1, getAndResetRecords(logHandler)); runFailureTest(in, out); assertTrue(getAndResetRecords(logHandler) <= 1); int suppressed = runSuppressionFailureTest(in, newNormalCharSink()); assertEquals(0, suppressed); suppressed = runSuppressionFailureTest(in, BROKEN_CLOSE_SINK); assertEquals((in == BROKEN_OPEN_SOURCE) ? 0 : 1, suppressed); int suppressed = runSuppressionFailureTest(newNormalCharSource(), out); assertEquals(0, suppressed); suppressed = runSuppressionFailureTest(BROKEN_CLOSE_SOURCE, out); assertEquals(1, suppressed);
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 testClosesOnErrors_whenReadThrows() { TestCharSource failSource = new TestCharSource(STRING, READ_THROWS); try { failSource.copyTo(new TestCharSink()); fail(); } catch (IOException expected) { } assertTrue(failSource.wasStreamClosed()); }
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()); }
private static void runFailureTest(CharSource in, CharSink out) { try { in.copyTo(out); fail(); } catch (IOException expected) { } }
public void testCopyToAppendable_doesNotCloseIfWriter() throws IOException { TestWriter writer = new TestWriter(); assertFalse(writer.closed()); source.copyTo(writer); assertFalse(writer.closed()); }
public void testConcat_infiniteIterable() throws IOException { CharSource source = CharSource.wrap("abcd"); Iterable<CharSource> cycle = Iterables.cycle(ImmutableList.of(source)); CharSource concatenated = CharSource.concat(cycle); String expected = "abcdabcd"; // read the first 8 chars manually, since there's no equivalent to ByteSource.slice // TODO(cgdecker): Add CharSource.slice? StringBuilder builder = new StringBuilder(); Reader reader = concatenated.openStream(); // no need to worry about closing for (int i = 0; i < 8; i++) { builder.append((char) reader.read()); } assertEquals(expected, builder.toString()); }
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()); }
/** @return the number of exceptions that were suppressed on the expected thrown exception */ private static int runSuppressionFailureTest(CharSource in, CharSink out) { try { in.copyTo(out); fail(); } catch (IOException expected) { return CloserTest.getSuppressed(expected).length; } throw new AssertionError(); // can't happen }
public void testCopyToAppendable_doesNotCloseIfWriter() throws IOException { TestWriter writer = new TestWriter(); assertFalse(writer.closed()); source.copyTo(writer); assertFalse(writer.closed()); }
public void testConcat_infiniteIterable() throws IOException { CharSource source = CharSource.wrap("abcd"); Iterable<CharSource> cycle = Iterables.cycle(ImmutableList.of(source)); CharSource concatenated = CharSource.concat(cycle); String expected = "abcdabcd"; // read the first 8 chars manually, since there's no equivalent to ByteSource.slice // TODO(cgdecker): Add CharSource.slice? StringBuilder builder = new StringBuilder(); Reader reader = concatenated.openStream(); // no need to worry about closing for (int i = 0; i < 8; i++) { builder.append((char) reader.read()); } assertEquals(expected, builder.toString()); }
try { for (CharSource in : BROKEN_SOURCES) { runFailureTest(in, newNormalCharSink()); assertTrue(logHandler.getStoredLogRecords().isEmpty()); runFailureTest(in, BROKEN_CLOSE_SINK); assertEquals((in == BROKEN_OPEN_SOURCE) ? 0 : 1, getAndResetRecords(logHandler)); runFailureTest(newNormalCharSource(), out); assertTrue(logHandler.getStoredLogRecords().isEmpty()); runFailureTest(BROKEN_CLOSE_SOURCE, out); assertEquals(1, getAndResetRecords(logHandler)); runFailureTest(in, out); assertTrue(getAndResetRecords(logHandler) <= 1); int suppressed = runSuppressionFailureTest(in, newNormalCharSink()); assertEquals(0, suppressed); suppressed = runSuppressionFailureTest(in, BROKEN_CLOSE_SINK); assertEquals((in == BROKEN_OPEN_SOURCE) ? 0 : 1, suppressed); int suppressed = runSuppressionFailureTest(newNormalCharSource(), out); assertEquals(0, suppressed); suppressed = runSuppressionFailureTest(BROKEN_CLOSE_SOURCE, out); assertEquals(1, suppressed);
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 testRead_toString() throws IOException { assertEquals(STRING, source.read()); assertTrue(source.wasStreamOpened() && 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()); }
private static void runFailureTest(CharSource in, CharSink out) { try { in.copyTo(out); fail(); } catch (IOException expected) { } }
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 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 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()); } }
/** * @return the number of exceptions that were suppressed on the expected thrown exception */ private static int runSuppressionFailureTest(CharSource in, CharSink out) { try { in.copyTo(out); fail(); } catch (IOException expected) { return CloserTest.getSuppressed(expected).length; } throw new AssertionError(); // can't happen }