Skip to content

Commit 2913e45

Browse files
committed
Create StringBuilderWriterTest.java
1 parent ce0399f commit 2913e45

1 file changed

Lines changed: 149 additions & 0 deletions

File tree

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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+
18+
package io.microsphere.io;
19+
20+
import io.microsphere.AbstractTestCase;
21+
import org.junit.jupiter.api.BeforeEach;
22+
import org.junit.jupiter.api.Test;
23+
24+
import static io.microsphere.util.StringUtils.EMPTY_STRING;
25+
import static org.junit.jupiter.api.Assertions.assertEquals;
26+
27+
/**
28+
* {@link StringBuilderWriter} Test
29+
*
30+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
31+
* @see StringBuilderWriter
32+
* @since 1.0.0
33+
*/
34+
class StringBuilderWriterTest extends AbstractTestCase {
35+
36+
private StringBuilderWriter writer;
37+
38+
@BeforeEach
39+
void setUp() {
40+
this.writer = new StringBuilderWriter();
41+
}
42+
43+
@Test
44+
public void testConstructorWithCapacity() {
45+
int size = random.nextInt(1024);
46+
StringBuilderWriter writer = new StringBuilderWriter(size);
47+
assertEquals(size, writer.getBuilder().capacity());
48+
}
49+
50+
@Test
51+
void testConstructorWithStringBuilder() {
52+
StringBuilderWriter writer = new StringBuilderWriter(new StringBuilder("Hello"));
53+
assertEquals("Hello", writer.toString());
54+
}
55+
56+
@Test
57+
void testWriteWithChar() {
58+
writer.write('H');
59+
assertEquals("H", writer.toString());
60+
}
61+
62+
@Test
63+
void testWriteWithChars() {
64+
writer.write("Hello".toCharArray());
65+
assertEquals("Hello", writer.toString());
66+
}
67+
68+
@Test
69+
void testWriteWithCharsOnEmpty() {
70+
writer.write((char[]) null);
71+
writer.write(new char[0]);
72+
}
73+
74+
@Test
75+
void testWriteWithCharsAndRange() {
76+
writer.write("Hello".toCharArray(), 0, 2);
77+
assertEquals("He", writer.toString());
78+
}
79+
80+
@Test
81+
void testWriteWithCharsAndRangeOnNull() {
82+
writer.write((char[]) null, 0, 0);
83+
writer.write(new char[0], 0, 0);
84+
}
85+
86+
@Test
87+
void testWriteWithString() {
88+
writer.write("Hello");
89+
assertEquals("Hello", writer.toString());
90+
}
91+
92+
@Test
93+
void testWriteWithStringOnEmpty() {
94+
writer.write(TEST_NULL_STRING);
95+
writer.write(EMPTY_STRING);
96+
}
97+
98+
@Test
99+
void testWriteWithStringAndRange() {
100+
writer.write("Hello", 0, 2);
101+
assertEquals("He", writer.toString());
102+
}
103+
104+
@Test
105+
void testWriteWithStringAndRangeOnEmpty() {
106+
writer.write(TEST_NULL_STRING, 0, 0);
107+
writer.write(EMPTY_STRING, 0, 0);
108+
}
109+
110+
@Test
111+
void testAppendWithChar() {
112+
writer.append('H');
113+
assertEquals("H", writer.toString());
114+
}
115+
116+
@Test
117+
void testAppendWithCharSequence() {
118+
writer.append("Hello");
119+
assertEquals("Hello", writer.toString());
120+
}
121+
122+
@Test
123+
void testAppendWithCharSequenceOnEmpty() {
124+
writer.append(TEST_NULL_STRING);
125+
writer.append(EMPTY_STRING);
126+
}
127+
128+
@Test
129+
void testAppendWithCharSequenceAndRange() {
130+
writer.append("Hello", 0, 2);
131+
assertEquals("He", writer.toString());
132+
}
133+
134+
@Test
135+
void testAppendWithCharSequenceAndRangeOnEmpty() {
136+
writer.append(TEST_NULL_STRING, 0, 0);
137+
writer.append(EMPTY_STRING, 0, 0);
138+
}
139+
140+
@Test
141+
void testClose() {
142+
writer.close();
143+
}
144+
145+
@Test
146+
void testFlush() {
147+
writer.flush();
148+
}
149+
}

0 commit comments

Comments
 (0)