1+ import org .junit .jupiter .api .AfterEach ;
2+ import org .junit .jupiter .api .BeforeAll ;
3+ import org .junit .jupiter .api .BeforeEach ;
4+ import org .junit .jupiter .api .Test ;
5+
6+ import java .nio .charset .StandardCharsets ;
7+
8+ import static org .junit .jupiter .api .Assertions .*;
9+
10+ class StringOutputStreamTest {
11+ static final String E_ACUTE = "\u00E9 " ;
12+ static final String SMILEY = "\uD83D \uDE0A " ;
13+
14+ StringOutputStream stream ;
15+
16+ @ BeforeEach
17+ void setUp () {
18+ stream = new StringOutputStream (10 );
19+ }
20+
21+ @ AfterEach
22+ void tearDown () {
23+ stream .close ();
24+ }
25+
26+ @ Test
27+ void testNoOverflow () {
28+ final String hello = "HelloWorld" ; // length = 10
29+ for (byte b : hello .getBytes (StandardCharsets .UTF_8 )) {
30+ stream .write (b );
31+ }
32+ assertResult (false , hello , stream .readAll ());
33+
34+ final String eAcuteX10 = E_ACUTE .repeat (10 );
35+ for (byte b : eAcuteX10 .getBytes (StandardCharsets .UTF_8 )) {
36+ stream .write (b );
37+ }
38+ assertResult (false , eAcuteX10 , stream .readAll ());
39+
40+ final String smileyX5 = SMILEY .repeat (5 );
41+ for (byte b : smileyX5 .getBytes (StandardCharsets .UTF_8 )) {
42+ stream .write (b );
43+ }
44+ assertResult (false , smileyX5 , stream .readAll ());
45+ }
46+ @ Test
47+ void testOverflow () {
48+ final String hello = "Hello World" ; // length = 11
49+ for (byte b : hello .getBytes (StandardCharsets .UTF_8 )) {
50+ stream .write (b );
51+ }
52+ assertResult (true , "Hello Worl" , stream .readAll ());
53+
54+ final String eAcuteX11 = E_ACUTE .repeat (11 );
55+ for (byte b : eAcuteX11 .getBytes (StandardCharsets .UTF_8 )) {
56+ stream .write (b );
57+ }
58+ assertResult (true , E_ACUTE .repeat (10 ), stream .readAll ());
59+ }
60+ @ Test
61+ void testOverflowWithHalfCharacter () {
62+ final String aAndSmileyX5 = 'a' + SMILEY .repeat (5 );
63+ for (byte b : aAndSmileyX5 .getBytes (StandardCharsets .UTF_8 )) {
64+ stream .write (b );
65+ }
66+ assertResult (true , 'a' + SMILEY .repeat (4 ), stream .readAll ());
67+ }
68+
69+ @ Test
70+ void testWriteOverload () {
71+ final String hello = "HelloWorld" ; // length = 10
72+ stream .write (hello .getBytes (StandardCharsets .UTF_8 ));
73+ assertResult (false , hello , stream .readAll ());
74+
75+ final String eAcuteX15 = E_ACUTE .repeat (15 );
76+ stream .write (eAcuteX15 .getBytes (StandardCharsets .UTF_8 ), 0 , 2 *10 );
77+ assertResult (false , E_ACUTE .repeat (10 ), stream .readAll ());
78+
79+ final String eAcuteX11 = E_ACUTE .repeat (11 );
80+ stream .write (eAcuteX11 .getBytes (StandardCharsets .UTF_8 ), 0 , 2 *11 );
81+ assertResult (true , E_ACUTE .repeat (10 ), stream .readAll ());
82+
83+ final String eAcuteX5AndSmileyX5 = E_ACUTE .repeat (5 ) + SMILEY .repeat (5 );
84+ stream .write (eAcuteX5AndSmileyX5 .getBytes (StandardCharsets .UTF_8 ), 2 *3 , (2 *2 )+(4 *4 ));
85+ assertResult (false , E_ACUTE .repeat (2 ) + SMILEY .repeat (4 ), stream .readAll ());
86+
87+ final String eAcuteX5AndSmileyX5AndA = E_ACUTE .repeat (5 ) + SMILEY .repeat (5 ) + 'a' ;
88+ stream .write (eAcuteX5AndSmileyX5AndA .getBytes (StandardCharsets .UTF_8 ), 2 *3 , (2 *2 )+(4 *4 )+1 );
89+ assertResult (true , E_ACUTE .repeat (2 ) + SMILEY .repeat (4 ), stream .readAll ());
90+ }
91+
92+ void assertResult (boolean isOverflow , String expected , StringOutputStream .Result result ) {
93+ assertEquals (isOverflow , result .isOverflow ());
94+ assertEquals (expected , result .content ());
95+ }
96+ }
0 commit comments