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