View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   */ 
19  package org.apache.rat.header;
20  
21  import org.junit.Before;
22  import org.junit.Test;
23  
24  import java.io.StringReader;
25  
26  import static org.junit.Assert.assertEquals;
27  import static org.junit.Assert.assertNotNull;
28  
29  public class FilteringSequenceFactoryTest {
30  
31      private int capacity;
32      private FilteringSequenceFactory factory;
33      private SimpleCharFilter filter;
34  
35      @Before
36      public void setUp() throws Exception {
37          capacity = 50;
38          filter = new SimpleCharFilter();
39          factory = new FilteringSequenceFactory(capacity, filter);
40      }
41  
42      @Test
43      public void noFiltering() throws Exception {
44          final String INPUT = "Whatever";
45          StringReader reader = new StringReader(INPUT);
46          CharSequence result = factory.filter(reader);
47          assertNotNull(result);
48          String output = result.toString();
49          assertEquals("No filtering so input equals output.", INPUT, output);
50          reader = new StringReader(INPUT);
51          result = factory.filter(reader);
52          assertNotNull(result);
53          output = result.toString();
54          assertEquals("No filtering so input equals output. Independent of previous input", INPUT, output);
55      }
56  
57      @Test
58      public void filtering() throws Exception {
59          final String INPUT = "Whatever";
60          StringReader reader = new StringReader(INPUT);
61          CharSequence result = factory.filter(reader);
62          assertNotNull(result);
63          String output = result.toString();
64          assertEquals("No filtering so input equals output.", INPUT, output);
65          filter.filterOut = true;
66          reader = new StringReader(INPUT);
67          result = factory.filter(reader);
68          assertNotNull(result);
69          assertEquals("All filtered output is empty. Independent of previous input", 0, result.length());
70      }
71      
72      @Test
73      public void overCapacity() throws Exception {
74          final String INPUT = "WhateverWhateverWhateverWhateverWhateverWhateverWhateverWhateverWhateverWhatever";
75          StringReader reader = new StringReader(INPUT);
76          CharSequence result = factory.filter(reader);
77          assertNotNull(result);
78          String output = result.toString();
79          assertEquals("No filtering so input equals output.", INPUT.substring(0, capacity), output);
80      }
81  }