View Javadoc
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 static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertSame;
21  
22  import java.io.IOException;
23  import java.io.Writer;
24  
25  import org.junit.jupiter.api.Test;
26  
27  /**
28   * Test case for {@link StringBuilderWriter}.
29   */
30  public class StringBuilderWriterTest {
31      private static final char[] FOOBAR_CHARS = {'F', 'o', 'o', 'B', 'a', 'r'};
32  
33      @Test
34      public void testAppendChar() throws IOException {
35          try (Writer writer = new StringBuilderWriter()) {
36              writer.append('F').append('o').append('o');
37              assertEquals("Foo", writer.toString());
38          }
39      }
40  
41      @Test
42      public void testAppendCharSequence() throws IOException {
43          try (Writer writer = new StringBuilderWriter()) {
44              writer.append("Foo").append("Bar");
45              assertEquals("FooBar", writer.toString());
46          }
47      }
48  
49      @Test
50      public void testAppendCharSequencePortion() throws IOException {
51          try (Writer writer = new StringBuilderWriter()) {
52              writer.append("FooBar", 3, 6).append(new StringBuffer("FooBar"), 0, 3);
53              assertEquals("BarFoo", writer.toString());
54          }
55      }
56  
57      @Test
58      public void testAppendConstructCapacity() throws IOException {
59          try (Writer writer = new StringBuilderWriter(100)) {
60              writer.append("Foo");
61              assertEquals("Foo", writer.toString());
62          }
63      }
64  
65      @Test
66      public void testAppendConstructNull() throws IOException {
67          try (Writer writer = new StringBuilderWriter(null)) {
68              writer.append("Foo");
69              assertEquals("Foo", writer.toString());
70          }
71      }
72  
73      @Test
74      public void testAppendConstructStringBuilder() {
75          final StringBuilder builder = new StringBuilder("Foo");
76          try (StringBuilderWriter writer = new StringBuilderWriter(builder)) {
77              writer.append("Bar");
78              assertEquals("FooBar", writer.toString());
79              assertSame(builder, writer.getBuilder());
80          }
81      }
82  
83      @Test
84      public void testClose() throws IOException {
85          try (Writer writer = new StringBuilderWriter()) {
86              writer.append("Foo");
87              writer.close();
88              writer.append("Bar");
89              assertEquals("FooBar", writer.toString());
90          }
91      }
92  
93      @Test
94      public void testWriteChar() throws IOException {
95          try (Writer writer = new StringBuilderWriter()) {
96              writer.write('F');
97              assertEquals("F", writer.toString());
98              writer.write('o');
99              assertEquals("Fo", writer.toString());
100             writer.write('o');
101             assertEquals("Foo", writer.toString());
102         }
103     }
104 
105     @Test
106     public void testWriteCharArray() throws IOException {
107         try (Writer writer = new StringBuilderWriter()) {
108             writer.write(new char[] { 'F', 'o', 'o' });
109             assertEquals("Foo", writer.toString());
110             writer.write(new char[] { 'B', 'a', 'r' });
111             assertEquals("FooBar", writer.toString());
112         }
113     }
114 
115     @Test
116     public void testWriteCharArrayPortion() throws IOException {
117         try (Writer writer = new StringBuilderWriter()) {
118             writer.write(FOOBAR_CHARS, 3, 3);
119             assertEquals("Bar", writer.toString());
120             writer.write(FOOBAR_CHARS, 0, 3);
121             assertEquals("BarFoo", writer.toString());
122         }
123     }
124 
125     @Test
126     public void testWriteString() throws IOException {
127         try (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 
135     @Test
136     public void testWriteStringPortion() throws IOException {
137         try (Writer writer = new StringBuilderWriter()) {
138             writer.write("FooBar", 3, 3);
139             assertEquals("Bar", writer.toString());
140             writer.write("FooBar", 0, 3);
141             assertEquals("BarFoo", writer.toString());
142         }
143     }
144 
145 }