View Javadoc
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.apache.commons.io.IOUtils.EOF;
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
22  
23  import java.io.IOException;
24  import java.io.StringReader;
25  import java.time.Duration;
26  import java.util.HashSet;
27  
28  import org.apache.commons.io.IOUtils;
29  import org.apache.commons.io.output.StringBuilderWriter;
30  import org.junit.jupiter.api.Test;
31  
32  public class CharacterFilterReaderTest {
33  
34      private static final String STRING_FIXTURE = "ababcabcd";
35  
36      @Test
37      public void testInputSize0FilterSize1() throws IOException {
38          final StringReader input = new StringReader("");
39          final HashSet<Integer> codePoints = new HashSet<>();
40          codePoints.add(Integer.valueOf('a'));
41          try (CharacterFilterReader reader = new CharacterFilterReader(input, 'A')) {
42              assertEquals(-1, reader.read());
43          }
44      }
45  
46      @Test
47      public void testInputSize1FilterSize1() throws IOException {
48          try (StringReader input = new StringReader("a");
49              CharacterFilterReader reader = new CharacterFilterReader(input, 'a')) {
50              assertEquals(-1, reader.read());
51          }
52      }
53  
54      @Test
55      public void testInputSize2FilterSize1FilterAll() throws IOException {
56          final StringReader input = new StringReader("aa");
57          try (CharacterFilterReader reader = new CharacterFilterReader(input, 'a')) {
58              assertEquals(-1, reader.read());
59          }
60      }
61  
62      @Test
63      public void testInputSize2FilterSize1FilterFirst() throws IOException {
64          final StringReader input = new StringReader("ab");
65          try (CharacterFilterReader reader = new CharacterFilterReader(input, 'a')) {
66              assertEquals('b', reader.read());
67              assertEquals(-1, reader.read());
68          }
69      }
70  
71      @Test
72      public void testInputSize2FilterSize1FilterLast() throws IOException {
73          final StringReader input = new StringReader("ab");
74          try (CharacterFilterReader reader = new CharacterFilterReader(input, 'b')) {
75              assertEquals('a', reader.read());
76              assertEquals(-1, reader.read());
77          }
78      }
79  
80      @Test
81      public void testReadFilteringEOF() {
82          final StringReader input = new StringReader(STRING_FIXTURE);
83          assertTimeoutPreemptively(Duration.ofMillis(500), () -> {
84              try (StringBuilderWriter output = new StringBuilderWriter();
85                  CharacterFilterReader reader = new CharacterFilterReader(input, EOF)) {
86                  int c;
87                  while ((c = reader.read()) != EOF) {
88                      output.write(c);
89                  }
90                  assertEquals(STRING_FIXTURE, output.toString());
91              }
92          });
93      }
94  
95      @Test
96      public void testReadIntoBuffer() throws IOException {
97          final StringReader input = new StringReader(STRING_FIXTURE);
98          try (CharacterFilterReader reader = new CharacterFilterReader(input, 'b')) {
99              final char[] buff = new char[9];
100             final int charCount = reader.read(buff);
101             assertEquals(6, charCount);
102             assertEquals("aacacd", new String(buff, 0, charCount));
103         }
104     }
105 
106     @Test
107     public void testReadUsingReader() throws IOException {
108         final StringReader input = new StringReader(STRING_FIXTURE);
109         try (StringBuilderWriter output = new StringBuilderWriter();
110             CharacterFilterReader reader = new CharacterFilterReader(input, 'b')) {
111             IOUtils.copy(reader, output);
112             assertEquals("aacacd", output.toString());
113         }
114     }
115 
116 }