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+
21+ import java .io .IOException ;
22+ import java .io .StringReader ;
23+ import java .util .function .IntPredicate ;
24+
25+ import org .apache .commons .io .IOUtils ;
26+ import org .apache .commons .io .output .StringBuilderWriter ;
27+ import org .apache .commons .lang3 .StringUtils ;
28+ import org .junit .jupiter .api .Test ;
29+
30+ /**
31+ * Tests {@link CharacterFilterReader} with an {@link IntPredicate}.
32+ */
33+ public class CharacterFilterReaderIntPredicateTest {
34+
35+ @ Test
36+ public void testInputSize0FilterAll () throws IOException {
37+ final StringReader input = new StringReader (StringUtils .EMPTY );
38+ try (CharacterFilterReader reader = new CharacterFilterReader (input , ch -> true )) {
39+ assertEquals (-1 , reader .read ());
40+ }
41+ }
42+
43+ @ Test
44+ public void testInputSize1FilterAll () throws IOException {
45+ try (StringReader input = new StringReader ("a" );
46+ CharacterFilterReader reader = new CharacterFilterReader (input , ch -> true )) {
47+ assertEquals (-1 , reader .read ());
48+ }
49+ }
50+
51+ @ Test
52+ public void testInputSize2FilterAll () throws IOException {
53+ final StringReader input = new StringReader ("aa" );
54+ try (CharacterFilterReader reader = new CharacterFilterReader (input , ch -> true )) {
55+ assertEquals (-1 , reader .read ());
56+ }
57+ }
58+
59+ @ Test
60+ public void testInputSize2FilterFirst () throws IOException {
61+ final StringReader input = new StringReader ("ab" );
62+ try (CharacterFilterReader reader = new CharacterFilterReader (input , ch -> ch == 'a' )) {
63+ assertEquals ('b' , reader .read ());
64+ assertEquals (-1 , reader .read ());
65+ }
66+ }
67+
68+ @ Test
69+ public void testInputSize2FilterLast () throws IOException {
70+ final StringReader input = new StringReader ("ab" );
71+ try (CharacterFilterReader reader = new CharacterFilterReader (input , ch -> ch == 'b' )) {
72+ assertEquals ('a' , reader .read ());
73+ assertEquals (-1 , reader .read ());
74+ }
75+ }
76+
77+ @ Test
78+ public void testInputSize5FilterWhitespace () throws IOException {
79+ final StringReader input = new StringReader (" a b " );
80+ try (CharacterFilterReader reader = new CharacterFilterReader (input , Character ::isWhitespace )) {
81+ assertEquals ('a' , reader .read ());
82+ assertEquals ('b' , reader .read ());
83+ assertEquals (-1 , reader .read ());
84+ }
85+ }
86+
87+ @ Test
88+ public void testReadIntoBuffer () throws IOException {
89+ final StringReader input = new StringReader ("ababcabcd" );
90+ try (CharacterFilterReader reader = new CharacterFilterReader (input , ch -> ch == 'b' )) {
91+ final char [] buff = new char [9 ];
92+ final int charCount = reader .read (buff );
93+ assertEquals (6 , charCount );
94+ assertEquals ("aacacd" , new String (buff , 0 , charCount ));
95+ }
96+ }
97+
98+ @ Test
99+ public void testReadIntoBufferFilterWhitespace () throws IOException {
100+ final StringReader input = new StringReader (" a b a b c a b c d " );
101+ try (CharacterFilterReader reader = new CharacterFilterReader (input , Character ::isWhitespace )) {
102+ final char [] buff = new char [19 ];
103+ final int charCount = reader .read (buff );
104+ assertEquals (9 , charCount );
105+ assertEquals ("ababcabcd" , new String (buff , 0 , charCount ));
106+ }
107+ }
108+
109+ @ Test
110+ public void testReadUsingReader () throws IOException {
111+ final StringReader input = new StringReader ("ababcabcd" );
112+ try (StringBuilderWriter output = new StringBuilderWriter ();
113+ CharacterFilterReader reader = new CharacterFilterReader (input , ch -> ch == 'b' )) {
114+ IOUtils .copy (reader , output );
115+ assertEquals ("aacacd" , output .toString ());
116+ }
117+ }
118+
119+ @ Test
120+ public void testReadUsingReaderFilterWhitespace () throws IOException {
121+ final StringReader input = new StringReader (" a b a b c a b c d " );
122+ try (StringBuilderWriter output = new StringBuilderWriter ();
123+ CharacterFilterReader reader = new CharacterFilterReader (input , Character ::isWhitespace )) {
124+ IOUtils .copy (reader , output );
125+ assertEquals ("ababcabcd" , output .toString ());
126+ }
127+ }
128+
129+ }
0 commit comments