• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
25 import java.io.ByteArrayInputStream;
26 import java.io.IOException;
27 import java.io.OutputStream;
28 import java.util.EnumSet;
29 
30 /**
31  * Tests for the default implementations of {@code ByteSink} methods.
32  *
33  * @author Colin Decker
34  */
35 public class ByteSinkTest extends IoTestCase {
36 
37   private final byte[] bytes = newPreFilledByteArray(10000);
38 
39   private TestByteSink sink;
40 
41   @Override
setUp()42   protected void setUp() throws Exception {
43     sink = new TestByteSink();
44   }
45 
testOpenBufferedStream()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 
testWrite_bytes()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 
testWriteFrom_inputStream()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 
testWriteFromStream_doesNotCloseThatStream()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 
testClosesOnErrors_copyingFromByteSourceThatThrows()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       // ensure stream was closed IF it was opened (depends on implementation whether or not it's
90       // opened at all if source.newInputStream() throws).
91       assertTrue("stream not closed when copying from source with option: " + option,
92           !okSink.wasStreamOpened() || okSink.wasStreamClosed());
93     }
94   }
95 
testClosesOnErrors_whenWriteThrows()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 
testClosesOnErrors_writingFromInputStreamThatThrows()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 }
118