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 java.io.StringReader;
22  
23  import junit.framework.TestCase;
24  
25  public class FilteringSequenceFactoryTest extends TestCase {
26  
27      int capacity;
28      FilteringSequenceFactory factory;
29      SimpleCharFilter filter;
30      
31      protected void setUp() throws Exception {
32          super.setUp();
33          capacity = 50;
34          filter = new SimpleCharFilter();
35          factory = new FilteringSequenceFactory(capacity, filter);
36      }
37  
38      protected void tearDown() throws Exception {
39          super.tearDown();
40      }
41  
42      public void testNoFiltering() throws Exception {
43          final String INPUT = "Whatever";
44          StringReader reader = new StringReader(INPUT);
45          CharSequence result = factory.filter(reader);
46          assertNotNull(result);
47          String output = new StringBuffer().append(result).toString();
48          assertEquals("No filtering so input equals output.", INPUT, output);
49          reader = new StringReader(INPUT);
50          result = factory.filter(reader);
51          assertNotNull(result);
52          output = new StringBuffer().append(result).toString();
53          assertEquals("No filtering so input equals output. Independent of previous input", INPUT, output);
54      }
55  
56      public void testFiltering() throws Exception {
57          final String INPUT = "Whatever";
58          StringReader reader = new StringReader(INPUT);
59          CharSequence result = factory.filter(reader);
60          assertNotNull(result);
61          String output = new StringBuffer().append(result).toString();
62          assertEquals("No filtering so input equals output.", INPUT, output);
63          filter.filterOut = true;
64          reader = new StringReader(INPUT);
65          result = factory.filter(reader);
66          assertNotNull(result);
67          assertEquals("All filtered output is empty. Independent of previous input", 0, result.length());
68      }
69      
70      public void testOverCapacity() throws Exception {
71          final String INPUT = "WhateverWhateverWhateverWhateverWhateverWhateverWhateverWhateverWhateverWhatever";
72          StringReader reader = new StringReader(INPUT);
73          CharSequence result = factory.filter(reader);
74          assertNotNull(result);
75          String output = new StringBuffer().append(result).toString();
76          assertEquals("No filtering so input equals output.", INPUT.substring(0, capacity), output);
77      }
78  }