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