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.assertThrows;
21  
22  import java.io.IOException;
23  import java.io.StringWriter;
24  import java.io.UncheckedIOException;
25  
26  import org.junit.jupiter.api.BeforeEach;
27  import org.junit.jupiter.api.Test;
28  
29  /**
30   * Tests {@link UncheckedAppendable}.
31   */
32  public class UncheckedAppendableTest {
33  
34      private IOException exception;
35  
36      private UncheckedAppendable appendableBroken;
37      private UncheckedAppendable appendableString;
38  
39      @SuppressWarnings("resource")
40      @BeforeEach
41      public void setUp() {
42          exception = new IOException("test exception");
43          appendableBroken = UncheckedAppendable.on(new BrokenWriter(exception));
44          appendableString = UncheckedAppendable.on(new StringWriter());
45      }
46  
47      @Test
48      public void testAppendChar() {
49          appendableString.append('a').append('b');
50          assertEquals("ab", appendableString.toString());
51      }
52  
53      @Test
54      public void testAppendCharSequence() {
55          appendableString.append("a").append("b");
56          assertEquals("ab", appendableString.toString());
57      }
58  
59      @Test
60      public void testAppendCharSequenceIndexed() {
61          appendableString.append("a", 0, 1).append("b", 0, 1);
62          assertEquals("ab", appendableString.toString());
63      }
64  
65      @Test
66      public void testAppendCharSequenceIndexedThrows() {
67          final UncheckedIOException e = assertThrows(UncheckedIOException.class, () -> appendableBroken.append("a", 0, 1));
68          assertEquals(exception, e.getCause());
69      }
70  
71      @Test
72      public void testAppendCharSequenceThrows() {
73          final UncheckedIOException e = assertThrows(UncheckedIOException.class, () -> appendableBroken.append("a"));
74          assertEquals(exception, e.getCause());
75      }
76  
77      @Test
78      public void testAppendCharThrows() {
79          final UncheckedIOException e2 = assertThrows(UncheckedIOException.class, () -> appendableBroken.append('a'));
80          assertEquals(exception, e2.getCause());
81      }
82  
83      @Test
84      public void testToString() {
85          assertEquals("ab", UncheckedAppendable.on(new StringWriter(2).append("ab")).toString());
86      }
87  
88  }