1 /* 2 * Copyright (C) 2007 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 java.io.Closeable; 20 import java.io.Flushable; 21 import java.io.IOException; 22 import java.io.Writer; 23 24 /** 25 * Unit test for {@link AppendableWriter}. 26 * 27 * @author Alan Green 28 */ 29 public class AppendableWriterTest extends IoTestCase { 30 31 /** Helper class for testing behavior with Flushable and Closeable targets. */ 32 private static class SpyAppendable implements Appendable, Flushable, Closeable { 33 boolean flushed; 34 boolean closed; 35 StringBuilder result = new StringBuilder(); 36 37 @Override append(CharSequence csq)38 public Appendable append(CharSequence csq) { 39 result.append(csq); 40 return this; 41 } 42 43 @Override append(char c)44 public Appendable append(char c) { 45 result.append(c); 46 return this; 47 } 48 49 @Override append(CharSequence csq, int start, int end)50 public Appendable append(CharSequence csq, int start, int end) { 51 result.append(csq, start, end); 52 return this; 53 } 54 55 @Override flush()56 public void flush() { 57 flushed = true; 58 } 59 60 @Override close()61 public void close() { 62 closed = true; 63 } 64 } 65 testWriteMethods()66 public void testWriteMethods() throws IOException { 67 StringBuilder builder = new StringBuilder(); 68 Writer writer = new AppendableWriter(builder); 69 70 writer.write("Hello".toCharArray()); 71 writer.write(','); 72 writer.write(0xBEEF0020); // only lower 16 bits are important 73 writer.write("Wo"); 74 writer.write("Whirled".toCharArray(), 3, 2); 75 writer.write("Mad! Mad, I say", 2, 2); 76 77 assertEquals("Hello, World!", builder.toString()); 78 } 79 testAppendMethods()80 public void testAppendMethods() throws IOException { 81 StringBuilder builder = new StringBuilder(); 82 Writer writer = new AppendableWriter(builder); 83 84 writer.append("Hello,"); 85 writer.append(' '); 86 writer.append("The World Wide Web", 4, 9); 87 writer.append("!"); 88 89 assertEquals("Hello, World!", builder.toString()); 90 } 91 testCloseFlush()92 public void testCloseFlush() throws IOException { 93 SpyAppendable spy = new SpyAppendable(); 94 Writer writer = new AppendableWriter(spy); 95 96 writer.write("Hello"); 97 assertFalse(spy.flushed); 98 assertFalse(spy.closed); 99 100 writer.flush(); 101 assertTrue(spy.flushed); 102 assertFalse(spy.closed); 103 104 writer.close(); 105 assertTrue(spy.flushed); 106 assertTrue(spy.closed); 107 } 108 testCloseIsFinal()109 public void testCloseIsFinal() throws IOException { 110 StringBuilder builder = new StringBuilder(); 111 Writer writer = new AppendableWriter(builder); 112 113 writer.write("Hi"); 114 writer.close(); 115 116 try { 117 writer.write(" Greg"); 118 fail("Should have thrown IOException due to writer already closed"); 119 } catch (IOException expected) { 120 } 121 122 try { 123 writer.flush(); 124 fail("Should have thrown IOException due to writer already closed"); 125 } catch (IOException expected) { 126 } 127 128 // close()ing already closed writer is allowed 129 writer.close(); 130 } 131 } 132