1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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 }