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 26 import java.io.IOException; 27 import java.io.StringReader; 28 import java.io.Writer; 29 import java.util.EnumSet; 30 31 /** 32 * Tests for the default implementations of {@code CharSink} methods. 33 * 34 * @author Colin Decker 35 */ 36 public class CharSinkTest extends IoTestCase { 37 38 private static final String STRING = ASCII + I18N; 39 40 private TestCharSink sink; 41 42 @Override setUp()43 public void setUp() { 44 sink = new TestCharSink(); 45 } 46 testOpenBufferedStream()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 testWrite_string()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 testWriteFrom_reader()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 testWriteFromStream_doesNotCloseThatStream()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 testWriteLines_withSpecificSeparator()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 testWriteLines_withDefaultSeparator()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 testClosesOnErrors_copyingFromCharSourceThatThrows()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 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