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 .input ;
18+
19+ import static org .junit .jupiter .api .Assertions .assertEquals ;
20+ import static org .junit .jupiter .api .Assertions .assertThrows ;
21+ import static org .junit .jupiter .api .Assertions .assertFalse ;
22+ import static org .junit .jupiter .api .Assertions .assertTrue ;
23+
24+ import java .io .IOException ;
25+ import java .io .Reader ;
26+ import java .io .StringReader ;
27+ import java .util .ArrayList ;
28+ import java .util .Collection ;
29+ import java .util .List ;
30+
31+ import org .apache .commons .lang3 .StringUtils ;
32+ import org .junit .jupiter .api .Test ;
33+
34+ /**
35+ * Test case for {@link SequenceReader}.
36+ */
37+ public class SequenceReaderTest {
38+
39+ private static final char NUL = 0 ;
40+
41+ private void checkArray (final char [] expected , final char [] actual ) {
42+ for (int i = 0 ; i < expected .length ; i ++) {
43+ assertEquals (expected [i ], actual [i ], "Compare[" + i + "]" );
44+ }
45+ }
46+
47+ private void checkRead (final Reader reader , final String expected ) throws IOException {
48+ for (int i = 0 ; i < expected .length (); i ++) {
49+ assertEquals (expected .charAt (i ), (char ) reader .read (), "Read[" + i + "] of '" + expected + "'" );
50+ }
51+ }
52+
53+ private void checkReadEof (final Reader reader ) throws IOException {
54+ for (int i = 0 ; i < 10 ; i ++) {
55+ assertEquals (-1 , reader .read ());
56+ }
57+ }
58+
59+ @ Test
60+ public void testClose () throws IOException {
61+ try (final Reader reader = new SequenceReader (new CharSequenceReader ("FooBar" ))) {
62+ checkRead (reader , "Foo" );
63+ reader .close ();
64+ checkReadEof (reader );
65+ }
66+ }
67+
68+ @ Test
69+ public void testReadClosedReader () throws IOException {
70+ @ SuppressWarnings ("resource" )
71+ final Reader reader = new SequenceReader (new CharSequenceReader ("FooBar" ));
72+ reader .close ();
73+ checkReadEof (reader );
74+ }
75+
76+ @ Test
77+ public void testMarkSupported () throws Exception {
78+ try (final Reader reader = new SequenceReader ()) {
79+ assertFalse (reader .markSupported ());
80+ }
81+ }
82+
83+ @ Test
84+ public void testRead () throws IOException {
85+ try (final Reader reader = new SequenceReader (new StringReader ("Foo" ), new StringReader ("Bar" ))) {
86+ assertEquals ('F' , reader .read ());
87+ assertEquals ('o' , reader .read ());
88+ assertEquals ('o' , reader .read ());
89+ assertEquals ('B' , reader .read ());
90+ assertEquals ('a' , reader .read ());
91+ assertEquals ('r' , reader .read ());
92+ checkReadEof (reader );
93+ }
94+ }
95+
96+ @ Test
97+ public void testReadCharArray () throws IOException {
98+ try (final Reader reader = new SequenceReader (new StringReader ("Foo" ), new StringReader ("Bar" ))) {
99+ char [] chars = new char [2 ];
100+ assertEquals (2 , reader .read (chars ));
101+ checkArray (new char [] { 'F' , 'o' }, chars );
102+ chars = new char [3 ];
103+ assertEquals (3 , reader .read (chars ));
104+ checkArray (new char [] { 'o' , 'B' , 'a' }, chars );
105+ chars = new char [3 ];
106+ assertEquals (1 , reader .read (chars ));
107+ checkArray (new char [] { 'r' , NUL , NUL }, chars );
108+ assertEquals (-1 , reader .read (chars ));
109+ }
110+ }
111+
112+ @ Test
113+ public void testReadCharArrayPortion () throws IOException {
114+ final char [] chars = new char [10 ];
115+ try (final Reader reader = new SequenceReader (new StringReader ("Foo" ), new StringReader ("Bar" ))) {
116+ assertEquals (3 , reader .read (chars , 3 , 3 ));
117+ checkArray (new char [] { NUL , NUL , NUL , 'F' , 'o' , 'o' }, chars );
118+ assertEquals (3 , reader .read (chars , 0 , 3 ));
119+ checkArray (new char [] { 'B' , 'a' , 'r' , 'F' , 'o' , 'o' , NUL }, chars );
120+ assertEquals (-1 , reader .read (chars ));
121+ assertThrows (IndexOutOfBoundsException .class , () -> reader .read (chars , 10 , 10 ));
122+ assertThrows (NullPointerException .class , () -> reader .read (null , 0 , 10 ));
123+ }
124+ }
125+
126+ @ Test
127+ public void testReadCollection () throws IOException {
128+ Collection <Reader > readers = new ArrayList <>();
129+ readers .add (new StringReader ("F" ));
130+ readers .add (new StringReader ("B" ));
131+ try (final Reader reader = new SequenceReader (readers )) {
132+ assertEquals ('F' , reader .read ());
133+ assertEquals ('B' , reader .read ());
134+ checkReadEof (reader );
135+ }
136+ }
137+
138+ @ Test
139+ public void testReadIterable () throws IOException {
140+ Collection <Reader > readers = new ArrayList <>();
141+ readers .add (new StringReader ("F" ));
142+ readers .add (new StringReader ("B" ));
143+ Iterable <Reader > iterable = readers ;
144+ try (final Reader reader = new SequenceReader (iterable )) {
145+ assertEquals ('F' , reader .read ());
146+ assertEquals ('B' , reader .read ());
147+ checkReadEof (reader );
148+ }
149+ }
150+
151+ @ Test
152+ public void testReadLength0Readers () throws IOException {
153+ try (final Reader reader = new SequenceReader (new StringReader (StringUtils .EMPTY ),
154+ new StringReader (StringUtils .EMPTY ), new StringReader (StringUtils .EMPTY ))) {
155+ checkReadEof (reader );
156+ }
157+ }
158+
159+ @ Test
160+ public void testReadLength1Readers () throws IOException {
161+ try (final Reader reader = new SequenceReader (
162+ // @formatter:off
163+ new StringReader ("0" ),
164+ new StringReader ("1" ),
165+ new StringReader ("2" ),
166+ new StringReader ("3" ))) {
167+ // @formatter:on
168+ assertEquals ('0' , reader .read ());
169+ assertEquals ('1' , reader .read ());
170+ assertEquals ('2' , reader .read ());
171+ assertEquals ('3' , reader .read ());
172+ }
173+ }
174+
175+ @ Test
176+ public void testReadList () throws IOException {
177+ List <Reader > readers = new ArrayList <>();
178+ readers .add (new StringReader ("F" ));
179+ readers .add (new StringReader ("B" ));
180+ try (final Reader reader = new SequenceReader (readers )) {
181+ assertEquals ('F' , reader .read ());
182+ assertEquals ('B' , reader .read ());
183+ checkReadEof (reader );
184+ }
185+ }
186+
187+ @ Test
188+ public void testSkip () throws IOException {
189+ try (final Reader reader = new SequenceReader (new StringReader ("Foo" ), new StringReader ("Bar" ))) {
190+ assertEquals (3 , reader .skip (3 ));
191+ checkRead (reader , "Bar" );
192+ assertEquals (0 , reader .skip (3 ));
193+ }
194+ }
195+ }
0 commit comments