public void testClosesOnErrors_whenReadThrows() { TestByteSource failSource = new TestByteSource(bytes, READ_THROWS); try { failSource.copyTo(new TestByteSink()); fail(); } catch (IOException expected) { } assertTrue(failSource.wasStreamClosed()); }
public void testContentEquals() throws IOException { assertTrue(source.contentEquals(source)); assertTrue(source.wasStreamOpened() && source.wasStreamClosed()); ByteSource equalSource = new TestByteSource(bytes); assertTrue(source.contentEquals(equalSource)); assertTrue(new TestByteSource(bytes).contentEquals(source)); ByteSource fewerBytes = new TestByteSource(newPreFilledByteArray(bytes.length / 2)); assertFalse(source.contentEquals(fewerBytes)); byte[] copy = bytes.clone(); copy[9876] = 1; ByteSource oneByteOff = new TestByteSource(copy); assertFalse(source.contentEquals(oneByteOff)); }
public void testOpenBufferedStream() throws IOException { InputStream in = source.openBufferedStream(); assertTrue(source.wasStreamOpened()); assertFalse(source.wasStreamClosed()); ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteStreams.copy(in, out); in.close(); out.close(); assertTrue(source.wasStreamClosed()); assertArrayEquals(bytes, out.toByteArray()); }
public void testSize() throws IOException { assertEquals(bytes.length, source.size()); assertTrue(source.wasStreamOpened() && source.wasStreamClosed()); // test that we can get the size even if skip() isn't supported assertEquals(bytes.length, new TestByteSource(bytes, SKIP_THROWS).size()); // test that we can get the size even if available() always returns zero assertEquals(bytes.length, new TestByteSource(bytes, AVAILABLE_ALWAYS_ZERO).size()); }
public void testRead_withProcessor_stopsOnFalse() throws IOException { ByteProcessor<Void> processor = new ByteProcessor<Void>() { boolean firstCall = true; @Override public boolean processBytes(byte[] buf, int off, int len) throws IOException { assertTrue("consume() called twice", firstCall); firstCall = false; return false; } @Override public Void getResult() { return null; } }; source.read(processor); assertTrue(source.wasStreamOpened() && source.wasStreamClosed()); }
public void testCopyTo_outputStream() throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); assertEquals(bytes.length, source.copyTo(out)); assertTrue(source.wasStreamOpened() && source.wasStreamClosed()); assertArrayEquals(bytes, out.toByteArray()); }
public void testClosesOnErrors_copyingToByteSinkThatThrows() { for (TestOption option : EnumSet.of(OPEN_THROWS, WRITE_THROWS, CLOSE_THROWS)) { TestByteSource okSource = new TestByteSource(bytes); try { okSource.copyTo(new TestByteSink(option)); fail(); } catch (IOException expected) { } // ensure stream was closed IF it was opened (depends on implementation whether or not it's // opened at all if sink.newOutputStream() throws). assertTrue( "stream not closed when copying to sink with option: " + option, !okSource.wasStreamOpened() || okSource.wasStreamClosed()); } }
public void testClosesOnErrors_whenWriteThrows() { TestByteSink failSink = new TestByteSink(WRITE_THROWS); try { new TestByteSource(new byte[10]).copyTo(failSink); fail(); } catch (IOException expected) { } assertTrue(failSink.wasStreamClosed()); }
@Override protected void setUp() throws Exception { source = new TestByteSource(bytes); }
@Override public Reader openStream() throws IOException { return new InputStreamReader(byteSource.openStream(), UTF_8); } }
public void testCopyToStream_doesNotCloseThatStream() throws IOException { TestOutputStream out = new TestOutputStream(ByteStreams.nullOutputStream()); assertFalse(out.closed()); source.copyTo(out); assertFalse(out.closed()); }
@Override public boolean wasStreamClosed() { return byteSource.wasStreamClosed(); }
@Override public boolean wasStreamOpened() { return byteSource.wasStreamOpened(); }
public void testSlice() throws IOException { // Test preconditions try { source.slice(-1, 10); fail(); } catch (IllegalArgumentException expected) { } try { source.slice(0, -1); fail(); } catch (IllegalArgumentException expected) { } assertCorrectSlice(0, 0, 0, 0); assertCorrectSlice(0, 0, 1, 0); assertCorrectSlice(100, 0, 10, 10); assertCorrectSlice(100, 0, 100, 100); assertCorrectSlice(100, 5, 10, 10); assertCorrectSlice(100, 5, 100, 95); assertCorrectSlice(100, 100, 0, 0); assertCorrectSlice(100, 100, 10, 0); assertCorrectSlice(100, 101, 10, 0); }
public void testRead_toArray() throws IOException { assertArrayEquals(bytes, source.read()); assertTrue(source.wasStreamOpened() && source.wasStreamClosed()); }
public void testCopyTo_byteSink() throws IOException { TestByteSink sink = new TestByteSink(); assertFalse(sink.wasStreamOpened() || sink.wasStreamClosed()); assertEquals(bytes.length, source.copyTo(sink)); assertTrue(source.wasStreamOpened() && source.wasStreamClosed()); assertTrue(sink.wasStreamOpened() && sink.wasStreamClosed()); assertArrayEquals(bytes, sink.getBytes()); }
public void testSize() throws IOException { assertEquals(bytes.length, source.size()); assertTrue(source.wasStreamOpened() && source.wasStreamClosed()); // test that we can get the size even if skip() isn't supported assertEquals(bytes.length, new TestByteSource(bytes, SKIP_THROWS).size()); // test that we can get the size even if available() always returns zero assertEquals(bytes.length, new TestByteSource(bytes, AVAILABLE_ALWAYS_ZERO).size()); }
public void testClosesOnErrors_copyingToByteSinkThatThrows() { for (TestOption option : EnumSet.of(OPEN_THROWS, WRITE_THROWS, CLOSE_THROWS)) { TestByteSource okSource = new TestByteSource(bytes); try { okSource.copyTo(new TestByteSink(option)); fail(); } catch (IOException expected) { } // ensure stream was closed IF it was opened (depends on implementation whether or not it's // opened at all if sink.newOutputStream() throws). assertTrue("stream not closed when copying to sink with option: " + option, !okSource.wasStreamOpened() || okSource.wasStreamClosed()); } }
public void testClosesOnErrors_copyingFromByteSourceThatThrows() { for (TestOption option : EnumSet.of(OPEN_THROWS, READ_THROWS, CLOSE_THROWS)) { TestByteSource failSource = new TestByteSource(new byte[10], option); TestByteSink okSink = new TestByteSink(); try { failSource.copyTo(okSink); fail(); } catch (IOException expected) { } // ensure stream was closed IF it was opened (depends on implementation whether or not it's // opened at all if source.newInputStream() throws). assertTrue( "stream not closed when copying from source with option: " + option, !okSink.wasStreamOpened() || okSink.wasStreamClosed()); } }
public TestCharSource(String content, TestOption... options) { this.byteSource = new TestByteSource(content.getBytes(UTF_8), options); }