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 public Appendable append(CharSequence csq) { 38 result.append(csq); 39 return this; 40 } 41 42 @Override public Appendable append(char c) { 43 result.append(c); 44 return this; 45 } 46 47 @Override public Appendable append(CharSequence csq, int start, int end) { 48 result.append(csq, start, end); 49 return this; 50 } 51 52 @Override public void flush() { 53 flushed = true; 54 } 55 56 @Override public void close() { 57 closed = true; 58 } 59 } 60 61 public void testWriteMethods() throws IOException { 62 StringBuilder builder = new StringBuilder(); 63 Writer writer = new AppendableWriter(builder); 64 65 writer.write("Hello".toCharArray()); 66 writer.write(','); 67 writer.write(0xBEEF0020); // only lower 16 bits are important 68 writer.write("Wo"); 69 writer.write("Whirled".toCharArray(), 3, 2); 70 writer.write("Mad! Mad, I say", 2, 2); 71 72 assertEquals("Hello, World!", builder.toString()); 73 } 74 75 public void testAppendMethods() throws IOException { 76 StringBuilder builder = new StringBuilder(); 77 Writer writer = new AppendableWriter(builder); 78 79 writer.append("Hello,"); 80 writer.append(' '); 81 writer.append("The World Wide Web", 4, 9); 82 writer.append("!"); 83 84 assertEquals("Hello, World!", builder.toString()); 85 } 86 87 public void testCloseFlush() throws IOException { 88 SpyAppendable spy = new SpyAppendable(); 89 Writer writer = new AppendableWriter(spy); 90 91 writer.write("Hello"); 92 assertFalse(spy.flushed); 93 assertFalse(spy.closed); 94 95 writer.flush(); 96 assertTrue(spy.flushed); 97 assertFalse(spy.closed); 98 99 writer.close(); 100 assertTrue(spy.flushed); 101 assertTrue(spy.closed); 102 } 103 104 public void testCloseIsFinal() throws IOException { 105 StringBuilder builder = new StringBuilder(); 106 Writer writer = new AppendableWriter(builder); 107 108 writer.write("Hi"); 109 writer.close(); 110 111 try { 112 writer.write(" Greg"); 113 fail("Should have thrown IOException due to writer already closed"); 114 } catch (IOException es) { 115 // expected 116 } 117 118 try { 119 writer.flush(); 120 fail("Should have thrown IOException due to writer already closed"); 121 } catch (IOException es) { 122 // expected 123 } 124 125 // close()ing already closed writer is allowed 126 writer.close(); 127 } 128 } 129