1717package org .apache .commons .io .input ;
1818
1919import static org .junit .jupiter .api .Assertions .assertEquals ;
20+ import static org .junit .jupiter .api .Assertions .assertThrows ;
2021import static org .junit .jupiter .api .Assertions .assertTrue ;
2122
23+ import java .io .ByteArrayInputStream ;
24+ import java .io .ByteArrayOutputStream ;
2225import java .io .IOException ;
26+ import java .io .ObjectInputStream ;
27+ import java .io .ObjectOutputStream ;
2328import java .io .Reader ;
2429import java .nio .CharBuffer ;
30+ import java .util .Arrays ;
2531
2632import org .junit .jupiter .api .Test ;
2733
@@ -38,6 +44,11 @@ public void testClose() throws IOException {
3844 checkRead (reader , "Foo" );
3945 reader .close ();
4046 checkRead (reader , "Foo" );
47+
48+ final Reader subReader = new CharSequenceReader ("xFooBarx" , 1 , 7 );
49+ checkRead (subReader , "Foo" );
50+ subReader .close ();
51+ checkRead (subReader , "Foo" );
4152 }
4253
4354 @ Test
@@ -60,6 +71,17 @@ public void testMark() throws IOException {
6071 reader .reset ();
6172 checkRead (reader , "Foo" );
6273 }
74+ try (final Reader subReader = new CharSequenceReader ("xFooBarx" , 1 , 7 )) {
75+ checkRead (subReader , "Foo" );
76+ subReader .mark (0 );
77+ checkRead (subReader , "Bar" );
78+ subReader .reset ();
79+ checkRead (subReader , "Bar" );
80+ subReader .close ();
81+ checkRead (subReader , "Foo" );
82+ subReader .reset ();
83+ checkRead (subReader , "Foo" );
84+ }
6385 }
6486
6587 @ Test
@@ -75,6 +97,18 @@ public void testSkip() throws IOException {
7597 reader .close ();
7698 assertEquals (6 , reader .skip (20 ));
7799 assertEquals (-1 , reader .read ());
100+
101+ final Reader subReader = new CharSequenceReader ("xFooBarx" , 1 , 7 );
102+ assertEquals (3 , subReader .skip (3 ));
103+ checkRead (subReader , "Bar" );
104+ assertEquals (-1 , subReader .skip (3 ));
105+ subReader .reset ();
106+ assertEquals (2 , subReader .skip (2 ));
107+ assertEquals (4 , subReader .skip (10 ));
108+ assertEquals (-1 , subReader .skip (1 ));
109+ subReader .close ();
110+ assertEquals (6 , subReader .skip (20 ));
111+ assertEquals (-1 , subReader .read ());
78112 }
79113
80114 @ Test
@@ -94,6 +128,12 @@ private void testRead(final CharSequence charSequence) throws IOException {
94128 assertEquals (-1 , reader .read ());
95129 assertEquals (-1 , reader .read ());
96130 }
131+ try (final Reader reader = new CharSequenceReader (charSequence , 1 , 5 )) {
132+ assertEquals ('o' , reader .read ());
133+ assertEquals ('o' , reader .read ());
134+ assertEquals (-1 , reader .read ());
135+ assertEquals (-1 , reader .read ());
136+ }
97137 }
98138
99139 @ Test
@@ -118,6 +158,18 @@ private void testReadCharArray(final CharSequence charSequence) throws IOExcepti
118158 checkArray (new char [] { 'r' , NONE , NONE }, chars );
119159 assertEquals (-1 , reader .read (chars ));
120160 }
161+ try (final Reader reader = new CharSequenceReader (charSequence , 1 , 5 )) {
162+ char [] chars = new char [2 ];
163+ assertEquals (2 , reader .read (chars ));
164+ checkArray (new char [] { 'o' , 'o' }, chars );
165+ chars = new char [3 ];
166+ assertEquals (2 , reader .read (chars ));
167+ checkArray (new char [] { 'B' , 'a' , NONE }, chars );
168+ chars = new char [3 ];
169+ assertEquals (-1 , reader .read (chars ));
170+ checkArray (new char [] { NONE , NONE , NONE }, chars );
171+ assertEquals (-1 , reader .read (chars ));
172+ }
121173 }
122174
123175 @ Test
@@ -138,6 +190,14 @@ private void testReadCharArrayPortion(final CharSequence charSequence) throws IO
138190 checkArray (new char [] { 'B' , 'a' , 'r' , 'F' , 'o' , 'o' , NONE }, chars );
139191 assertEquals (-1 , reader .read (chars ));
140192 }
193+ Arrays .fill (chars , NONE );
194+ try (final Reader reader = new CharSequenceReader (charSequence , 1 , 5 )) {
195+ assertEquals (2 , reader .read (chars , 3 , 2 ));
196+ checkArray (new char [] { NONE , NONE , NONE , 'o' , 'o' , NONE }, chars );
197+ assertEquals (2 , reader .read (chars , 0 , 3 ));
198+ checkArray (new char [] { 'B' , 'a' , NONE , 'o' , 'o' , NONE }, chars );
199+ assertEquals (-1 , reader .read (chars ));
200+ }
141201 }
142202
143203 private void checkRead (final Reader reader , final String expected ) throws IOException {
@@ -151,4 +211,65 @@ private void checkArray(final char[] expected, final char[] actual) {
151211 assertEquals (expected [i ], actual [i ], "Compare[" +i + "]" );
152212 }
153213 }
214+
215+ @ Test
216+ public void testConstructor () {
217+ assertThrows (IllegalArgumentException .class , () -> new CharSequenceReader ("FooBar" , -1 , 6 ),
218+ "Expected exception not thrown for negative start." );
219+ assertThrows (IllegalArgumentException .class , () -> new CharSequenceReader ("FooBar" , 1 , 0 ),
220+ "Expected exception not thrown for end before start." );
221+ }
222+
223+ @ Test
224+ public void testToString () {
225+ assertEquals ("FooBar" , new CharSequenceReader ("FooBar" ).toString ());
226+ assertEquals ("FooBar" , new CharSequenceReader ("xFooBarx" , 1 , 7 ).toString ());
227+ }
228+
229+ @ Test
230+ public void testSerialization () throws IOException , ClassNotFoundException {
231+ /*
232+ * File CharSequenceReader.bin contains a CharSequenceReader that was serialized before
233+ * the start and end fields were added. Its CharSequence is "FooBar".
234+ * This part of the test will test that adding the fields does not break any existing
235+ * serialized CharSequenceReaders.
236+ */
237+ try (ObjectInputStream ois = new ObjectInputStream (getClass ().getResourceAsStream ("CharSequenceReader.bin" ))) {
238+ final CharSequenceReader reader = (CharSequenceReader ) ois .readObject ();
239+ assertEquals ('F' , reader .read ());
240+ assertEquals ('o' , reader .read ());
241+ assertEquals ('o' , reader .read ());
242+ assertEquals ('B' , reader .read ());
243+ assertEquals ('a' , reader .read ());
244+ assertEquals ('r' , reader .read ());
245+ assertEquals (-1 , reader .read ());
246+ assertEquals (-1 , reader .read ());
247+ }
248+
249+ ByteArrayOutputStream baos = new ByteArrayOutputStream ();
250+ try (ObjectOutputStream oos = new ObjectOutputStream (baos )) {
251+ final CharSequenceReader reader = new CharSequenceReader ("xFooBarx" , 1 , 7 );
252+ oos .writeObject (reader );
253+ }
254+ try (ObjectInputStream ois = new ObjectInputStream (new ByteArrayInputStream (baos .toByteArray ()))) {
255+ final CharSequenceReader reader = (CharSequenceReader ) ois .readObject ();
256+ assertEquals ('F' , reader .read ());
257+ assertEquals ('o' , reader .read ());
258+ assertEquals ('o' , reader .read ());
259+ assertEquals ('B' , reader .read ());
260+ assertEquals ('a' , reader .read ());
261+ assertEquals ('r' , reader .read ());
262+ assertEquals (-1 , reader .read ());
263+ assertEquals (-1 , reader .read ());
264+ reader .reset ();
265+ assertEquals ('F' , reader .read ());
266+ assertEquals ('o' , reader .read ());
267+ assertEquals ('o' , reader .read ());
268+ assertEquals ('B' , reader .read ());
269+ assertEquals ('a' , reader .read ());
270+ assertEquals ('r' , reader .read ());
271+ assertEquals (-1 , reader .read ());
272+ assertEquals (-1 , reader .read ());
273+ }
274+ }
154275}
0 commit comments