• 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 
24 import com.google.common.collect.ImmutableList;
25 import java.io.IOException;
26 import java.io.StringReader;
27 import java.io.Writer;
28 import java.util.EnumSet;
29 
30 /**
31  * Tests for the default implementations of {@code CharSink} methods.
32  *
33  * @author Colin Decker
34  */
35 public class CharSinkTest extends IoTestCase {
36 
37   private static final String STRING = ASCII + I18N;
38 
39   private TestCharSink sink;
40 
41   @Override
setUp()42   public void setUp() {
43     sink = new TestCharSink();
44   }
45 
testOpenBufferedStream()46   public void testOpenBufferedStream() throws IOException {
47     Writer writer = sink.openBufferedStream();
48     assertTrue(sink.wasStreamOpened());
49     assertFalse(sink.wasStreamClosed());
50 
51     writer.write(STRING);
52     writer.close();
53 
54     assertTrue(sink.wasStreamClosed());
55     assertEquals(STRING, sink.getString());
56   }
57 
testWrite_string()58   public void testWrite_string() throws IOException {
59     assertEquals("", sink.getString());
60     sink.write(STRING);
61 
62     assertTrue(sink.wasStreamOpened() && sink.wasStreamClosed());
63     assertEquals(STRING, sink.getString());
64   }
65 
testWriteFrom_reader()66   public void testWriteFrom_reader() throws IOException {
67     StringReader reader = new StringReader(STRING);
68     sink.writeFrom(reader);
69 
70     assertTrue(sink.wasStreamOpened() && sink.wasStreamClosed());
71     assertEquals(STRING, sink.getString());
72   }
73 
testWriteFromStream_doesNotCloseThatStream()74   public void testWriteFromStream_doesNotCloseThatStream() throws IOException {
75     TestReader in = new TestReader();
76     assertFalse(in.closed());
77     sink.writeFrom(in);
78     assertFalse(in.closed());
79   }
80 
testWriteLines_withSpecificSeparator()81   public void testWriteLines_withSpecificSeparator() throws IOException {
82     sink.writeLines(ImmutableList.of("foo", "bar", "baz"), "\n");
83     assertEquals("foo\nbar\nbaz\n", sink.getString());
84   }
85 
testWriteLines_withDefaultSeparator()86   public void testWriteLines_withDefaultSeparator() throws IOException {
87     sink.writeLines(ImmutableList.of("foo", "bar", "baz"));
88     String separator = System.getProperty("line.separator");
89     assertEquals("foo" + separator + "bar" + separator + "baz" + separator, sink.getString());
90   }
91 
testClosesOnErrors_copyingFromCharSourceThatThrows()92   public void testClosesOnErrors_copyingFromCharSourceThatThrows() {
93     for (TestOption option : EnumSet.of(OPEN_THROWS, READ_THROWS, CLOSE_THROWS)) {
94       TestCharSource failSource = new TestCharSource(STRING, option);
95       TestCharSink okSink = new TestCharSink();
96       try {
97         failSource.copyTo(okSink);
98         fail();
99       } catch (IOException expected) {
100       }
101       // ensure writer was closed IF it was opened (depends on implementation whether or not it's
102       // opened at all if source.newReader() throws).
103       assertTrue(
104           "stream not closed when copying from source with option: " + option,
105           !okSink.wasStreamOpened() || okSink.wasStreamClosed());
106     }
107   }
108 
testClosesOnErrors_whenWriteThrows()109   public void testClosesOnErrors_whenWriteThrows() {
110     TestCharSink failSink = new TestCharSink(WRITE_THROWS);
111     try {
112       new TestCharSource(STRING).copyTo(failSink);
113       fail();
114     } catch (IOException expected) {
115     }
116     assertTrue(failSink.wasStreamClosed());
117   }
118 
testClosesOnErrors_whenWritingFromReaderThatThrows()119   public void testClosesOnErrors_whenWritingFromReaderThatThrows() {
120     TestCharSink okSink = new TestCharSink();
121     try {
122       okSink.writeFrom(new TestReader(READ_THROWS));
123       fail();
124     } catch (IOException expected) {
125     }
126     assertTrue(okSink.wasStreamClosed());
127   }
128 }
129