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 java.io.IOException;
20 import java.io.Writer;
21 import junit.framework.TestCase;
22
23
24
25
26
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
33
34
35 public StringBuilderWriterTest(String name) {
36 super(name);
37 }
38
39
40 public void testAppendConstructCapacity() throws IOException {
41 Writer writer = new StringBuilderWriter(100);
42 writer.append("Foo");
43 assertEquals("Foo", writer.toString());
44 }
45
46
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
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
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
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
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
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
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
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
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
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
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 }