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  
21  import org.junit.jupiter.api.BeforeEach;
22  import org.junit.jupiter.api.Test;
23  
24  /**
25   * Tests {@link AppendableWriter}.
26   */
27  public class AppendableWriterTest {
28  
29      private AppendableWriter<StringBuilder> out;
30  
31      @BeforeEach
32      public void setUp() {
33          out = new AppendableWriter<>(new StringBuilder());
34      }
35  
36      @SuppressWarnings("resource")
37      @Test
38      public void testAppendChar() throws Exception {
39          out.append('F');
40  
41          assertEquals("F", out.getAppendable().toString());
42      }
43  
44      @SuppressWarnings("resource")
45      @Test
46      public void testAppendCharSequence() throws Exception {
47          final String testData = "ABCD";
48  
49          out.append(testData);
50          out.append(null);
51  
52          assertEquals(testData + "null", out.getAppendable().toString());
53      }
54  
55      @SuppressWarnings("resource")
56      @Test
57      public void testAppendSubSequence() throws Exception {
58          final String testData = "ABCD";
59  
60          out.append(testData, 1, 3);
61          out.append(null, 1, 3);
62  
63          assertEquals(testData.substring(1, 3) + "ul", out.getAppendable().toString());
64      }
65  
66      @Test
67      public void testWriteChars() throws Exception {
68          final String testData = "ABCD";
69  
70          out.write(testData.toCharArray());
71  
72          assertEquals(testData, out.getAppendable().toString());
73      }
74  
75      @Test
76      public void testWriteInt() throws Exception {
77          out.write('F');
78  
79          assertEquals("F", out.getAppendable().toString());
80      }
81  
82      @Test
83      public void testWriteString() throws Exception {
84          final String testData = "ABCD";
85  
86          out.write(testData);
87  
88          assertEquals(testData, out.getAppendable().toString());
89      }
90  }