1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.io.output;
18  
19  import java.io.IOException;
20  import java.io.Writer;
21  import junit.framework.TestCase;
22  
23  /**
24   * Test case for {@link StringBuilderWriter}.
25   *
26   * @version $Revision: 619141 $ $Date: 2008-02-06 20:16:55 +0000 (Wed, 06 Feb 2008) $
27   */
28  public class StringBuilderWriterTest extends TestCase {
29      private static final char[] FOOBAR_CHARS = new char[] {'F', 'o', 'o', 'B', 'a', 'r'};
30  
31      /**
32       * Contruct a new test case.
33       * @param name The name of the test
34       */
35      public StringBuilderWriterTest(String name) {
36          super(name);
37      }
38  
39      /** Test {@link StringBuilderWriter} constructor. */
40      public void testAppendConstructCapacity() throws IOException {
41          Writer writer = new StringBuilderWriter(100);
42          writer.append("Foo");
43          assertEquals("Foo", writer.toString());
44      }
45  
46      /** Test {@link StringBuilderWriter} constructor. */
47      public void testAppendConstructStringBuilder() throws IOException {
48          StringBuilder builder = new StringBuilder("Foo");
49          StringBuilderWriter writer = new StringBuilderWriter(builder);
50          writer.append("Bar");
51          assertEquals("FooBar", writer.toString());
52          assertTrue(builder == writer.getBuilder());
53      }
54  
55      /** Test {@link StringBuilderWriter} constructor. */
56      public void testAppendConstructNull() throws IOException {
57          Writer writer = new StringBuilderWriter((StringBuilder)null);
58          writer.append("Foo");
59          assertEquals("Foo", writer.toString());
60      }
61  
62      /** Test {@link Writer#append(char)}. */
63      public void testAppendChar() throws IOException {
64          Writer writer = new StringBuilderWriter();
65          writer.append('F').append('o').append('o');
66          assertEquals("Foo", writer.toString());
67      }
68  
69      /** Test {@link Writer#append(CharSequence)}. */
70      public void testAppendCharSequence() throws IOException {
71          Writer writer = new StringBuilderWriter();
72          writer.append("Foo").append("Bar");
73          assertEquals("FooBar", writer.toString());
74      }
75  
76      /** Test {@link Writer#append(CharSequence, int, int)}. */
77      public void testAppendCharSequencePortion() throws IOException {
78          Writer writer = new StringBuilderWriter();
79          writer.append("FooBar", 3, 6).append(new StringBuffer("FooBar"), 0, 3);
80          assertEquals("BarFoo", writer.toString());
81      }
82  
83      /** Test {@link Writer#close()}. */
84      public void testClose() {
85          Writer writer = new StringBuilderWriter();
86          try {
87              writer.append("Foo");
88              writer.close();
89              writer.append("Bar");
90          } catch (Throwable t) {
91              fail("Threw: " + t);
92          }
93          assertEquals("FooBar", writer.toString());
94      }
95  
96      /** Test {@link Writer#write(int)}. */
97      public void testWriteChar() throws IOException {
98          Writer writer = new StringBuilderWriter();
99          writer.write('F');
100         assertEquals("F", writer.toString());
101         writer.write('o');
102         assertEquals("Fo", writer.toString());
103         writer.write('o');
104         assertEquals("Foo", writer.toString());
105     }
106 
107     /** Test {@link Writer#write(char[])}. */
108     public void testWriteCharArray() throws IOException {
109         Writer writer = new StringBuilderWriter();
110         writer.write(new char[] {'F', 'o', 'o'});
111         assertEquals("Foo", writer.toString());
112         writer.write(new char[] {'B', 'a', 'r'});
113         assertEquals("FooBar", writer.toString());
114     }
115 
116     /** Test {@link Writer#write(char[], int, int)}. */
117     public void testWriteCharArrayPortion() throws IOException {
118         Writer writer = new StringBuilderWriter();
119         writer.write(FOOBAR_CHARS, 3, 3);
120         assertEquals("Bar", writer.toString());
121         writer.write(FOOBAR_CHARS, 0, 3);
122         assertEquals("BarFoo", writer.toString());
123     }
124 
125     /** Test {@link Writer#write(String)}. */
126     public void testWriteString() throws IOException {
127         Writer writer = new StringBuilderWriter();
128         writer.write("Foo");
129         assertEquals("Foo", writer.toString());
130         writer.write("Bar");
131         assertEquals("FooBar", writer.toString());
132     }
133 
134     /** Test {@link Writer#write(String, int, int)}. */
135     public void testWriteStringPortion() throws IOException {
136         Writer writer = new StringBuilderWriter();
137         writer.write("FooBar", 3, 3);
138         assertEquals("Bar", writer.toString());
139         writer.write("FooBar", 0, 3);
140         assertEquals("BarFoo", writer.toString());
141     }
142 
143 }