1 /* 2 * Copyright (C) 2012 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.io; 18 19 import static com.google.common.io.TestOption.CLOSE_THROWS; 20 import static com.google.common.io.TestOption.OPEN_THROWS; 21 import static com.google.common.io.TestOption.READ_THROWS; 22 import static com.google.common.io.TestOption.WRITE_THROWS; 23 import static org.junit.Assert.assertArrayEquals; 24 import static org.junit.Assert.assertThrows; 25 26 import java.io.ByteArrayInputStream; 27 import java.io.IOException; 28 import java.io.OutputStream; 29 import java.util.EnumSet; 30 31 /** 32 * Tests for the default implementations of {@code ByteSink} methods. 33 * 34 * @author Colin Decker 35 */ 36 public class ByteSinkTest extends IoTestCase { 37 38 private final byte[] bytes = newPreFilledByteArray(10000); 39 40 private TestByteSink sink; 41 42 @Override setUp()43 protected void setUp() throws Exception { 44 sink = new TestByteSink(); 45 } 46 testOpenBufferedStream()47 public void testOpenBufferedStream() throws IOException { 48 OutputStream out = sink.openBufferedStream(); 49 assertTrue(sink.wasStreamOpened()); 50 assertFalse(sink.wasStreamClosed()); 51 52 out.write(new byte[] {1, 2, 3, 4}); 53 out.close(); 54 55 assertTrue(sink.wasStreamClosed()); 56 assertArrayEquals(new byte[] {1, 2, 3, 4}, sink.getBytes()); 57 } 58 testWrite_bytes()59 public void testWrite_bytes() throws IOException { 60 assertArrayEquals(new byte[0], sink.getBytes()); 61 sink.write(bytes); 62 63 assertTrue(sink.wasStreamOpened() && sink.wasStreamClosed()); 64 assertArrayEquals(bytes, sink.getBytes()); 65 } 66 testWriteFrom_inputStream()67 public void testWriteFrom_inputStream() throws IOException { 68 ByteArrayInputStream in = new ByteArrayInputStream(bytes); 69 sink.writeFrom(in); 70 71 assertTrue(sink.wasStreamOpened() && sink.wasStreamClosed()); 72 assertArrayEquals(bytes, sink.getBytes()); 73 } 74 testWriteFromStream_doesNotCloseThatStream()75 public void testWriteFromStream_doesNotCloseThatStream() throws IOException { 76 TestInputStream in = new TestInputStream(new ByteArrayInputStream(new byte[10])); 77 assertFalse(in.closed()); 78 sink.writeFrom(in); 79 assertFalse(in.closed()); 80 } 81 testClosesOnErrors_copyingFromByteSourceThatThrows()82 public void testClosesOnErrors_copyingFromByteSourceThatThrows() { 83 for (TestOption option : EnumSet.of(OPEN_THROWS, READ_THROWS, CLOSE_THROWS)) { 84 TestByteSource failSource = new TestByteSource(new byte[10], option); 85 TestByteSink okSink = new TestByteSink(); 86 assertThrows(IOException.class, () -> failSource.copyTo(okSink)); 87 // ensure stream was closed IF it was opened (depends on implementation whether or not it's 88 // opened at all if source.newInputStream() throws). 89 assertTrue( 90 "stream not closed when copying from source with option: " + option, 91 !okSink.wasStreamOpened() || okSink.wasStreamClosed()); 92 } 93 } 94 testClosesOnErrors_whenWriteThrows()95 public void testClosesOnErrors_whenWriteThrows() { 96 TestByteSink failSink = new TestByteSink(WRITE_THROWS); 97 assertThrows(IOException.class, () -> new TestByteSource(new byte[10]).copyTo(failSink)); 98 assertTrue(failSink.wasStreamClosed()); 99 } 100 testClosesOnErrors_writingFromInputStreamThatThrows()101 public void testClosesOnErrors_writingFromInputStreamThatThrows() throws IOException { 102 TestByteSink okSink = new TestByteSink(); 103 TestInputStream in = new TestInputStream(new ByteArrayInputStream(new byte[10]), READ_THROWS); 104 assertThrows(IOException.class, () -> okSink.writeFrom(in)); 105 assertTrue(okSink.wasStreamClosed()); 106 } 107 } 108