• 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       }
90       // ensure stream was closed IF it was opened (depends on implementation whether or not it's
91       // opened at all if source.newInputStream() throws).
92       assertTrue(
93           "stream not closed when copying from source with option: " + option,
94           !okSink.wasStreamOpened() || okSink.wasStreamClosed());
95     }
96   }
97 
testClosesOnErrors_whenWriteThrows()98   public void testClosesOnErrors_whenWriteThrows() {
99     TestByteSink failSink = new TestByteSink(WRITE_THROWS);
100     try {
101       new TestByteSource(new byte[10]).copyTo(failSink);
102       fail();
103     } catch (IOException expected) {
104     }
105     assertTrue(failSink.wasStreamClosed());
106   }
107 
testClosesOnErrors_writingFromInputStreamThatThrows()108   public void testClosesOnErrors_writingFromInputStreamThatThrows() {
109     TestByteSink okSink = new TestByteSink();
110     try {
111       TestInputStream in = new TestInputStream(new ByteArrayInputStream(new byte[10]), READ_THROWS);
112       okSink.writeFrom(in);
113       fail();
114     } catch (IOException expected) {
115     }
116     assertTrue(okSink.wasStreamClosed());
117   }
118 }
119