1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
25 import java.io.ByteArrayInputStream;
26 import java.io.IOException;
27 import java.io.OutputStream;
28 import java.util.EnumSet;
29
30
31
32
33
34
35 public class ByteSinkTest extends IoTestCase {
36
37 private final byte[] bytes = newPreFilledByteArray(10000);
38
39 private TestByteSink sink;
40
41 @Override
42 protected void setUp() throws Exception {
43 sink = new TestByteSink();
44 }
45
46 public void testOpenBufferedStream() throws IOException {
47 OutputStream out = sink.openBufferedStream();
48 assertTrue(sink.wasStreamOpened());
49 assertFalse(sink.wasStreamClosed());
50
51 out.write(new byte[] {1, 2, 3, 4});
52 out.close();
53
54 assertTrue(sink.wasStreamClosed());
55 assertArrayEquals(new byte[] {1, 2, 3, 4}, sink.getBytes());
56 }
57
58 public void testWrite_bytes() throws IOException {
59 assertArrayEquals(new byte[0], sink.getBytes());
60 sink.write(bytes);
61
62 assertTrue(sink.wasStreamOpened() && sink.wasStreamClosed());
63 assertArrayEquals(bytes, sink.getBytes());
64 }
65
66 public void testWriteFrom_inputStream() throws IOException {
67 ByteArrayInputStream in = new ByteArrayInputStream(bytes);
68 sink.writeFrom(in);
69
70 assertTrue(sink.wasStreamOpened() && sink.wasStreamClosed());
71 assertArrayEquals(bytes, sink.getBytes());
72 }
73
74 public void testWriteFromStream_doesNotCloseThatStream() throws IOException {
75 TestInputStream in = new TestInputStream(new ByteArrayInputStream(new byte[10]));
76 assertFalse(in.closed());
77 sink.writeFrom(in);
78 assertFalse(in.closed());
79 }
80
81 public void testClosesOnErrors_copyingFromByteSourceThatThrows() {
82 for (TestOption option : EnumSet.of(OPEN_THROWS, READ_THROWS, CLOSE_THROWS)) {
83 TestByteSource failSource = new TestByteSource(new byte[10], option);
84 TestByteSink okSink = new TestByteSink();
85 try {
86 failSource.copyTo(okSink);
87 fail();
88 } catch (IOException expected) {}
89
90
91 assertTrue("stream not closed when copying from source with option: " + option,
92 !okSink.wasStreamOpened() || okSink.wasStreamClosed());
93 }
94 }
95
96 public void testClosesOnErrors_whenWriteThrows() {
97 TestByteSink failSink = new TestByteSink(WRITE_THROWS);
98 try {
99 new TestByteSource(new byte[10]).copyTo(failSink);
100 fail();
101 } catch (IOException expected) {
102 }
103 assertTrue(failSink.wasStreamClosed());
104 }
105
106 public void testClosesOnErrors_writingFromInputStreamThatThrows() {
107 TestByteSink okSink = new TestByteSink();
108 try {
109 TestInputStream in = new TestInputStream(
110 new ByteArrayInputStream(new byte[10]), READ_THROWS);
111 okSink.writeFrom(in);
112 fail();
113 } catch (IOException expected) {
114 }
115 assertTrue(okSink.wasStreamClosed());
116 }
117 }