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  import java.util.regex.Pattern;
23  
24  import junit.framework.TestCase;
25  
26  public class HeaderMatcherTest extends TestCase {
27  
28      int capacity;
29      HeaderMatcher matcher;
30      SimpleCharFilter filter;
31      
32      protected void setUp() throws Exception {
33          super.setUp();
34          capacity = 20;
35          filter = new SimpleCharFilter();
36          matcher = new HeaderMatcher(filter, 20);
37      }
38  
39      protected void tearDown() throws Exception {
40          super.tearDown();
41      }
42      
43      public void testSimpleMatches() throws Exception {
44          Pattern hatPattern = Pattern.compile("(.*)hat(.*)");
45          Pattern headPattern = Pattern.compile("head....");
46          StringReader reader = new StringReader("The mad hatter");
47          matcher.read(reader);
48          assertTrue(matcher.matches(hatPattern));
49          assertFalse(matcher.matches(headPattern));
50          reader = new StringReader("headache");
51          matcher.read(reader);
52          assertFalse(matcher.matches(hatPattern));
53          assertTrue(matcher.matches(headPattern));   
54      }
55      
56      public void testFilteredMatches() throws Exception {
57          Pattern capPattern = Pattern.compile("cap(.*)");
58          StringReader reader = new StringReader("capped");
59          matcher.read(reader);
60          assertTrue(matcher.matches(capPattern));
61          filter.filterOut = true;
62          reader = new StringReader("capped");
63          matcher.read(reader);
64          assertFalse(matcher.matches(capPattern));
65      }
66  
67      public void testNoLines() throws Exception {
68          StringReader reader = new StringReader("None");
69          matcher.read(reader);
70          assertEquals("No lines read", 0, matcher.lines());
71      }
72      
73      public void testLines() throws Exception {
74          StringReader reader = new StringReader("One\n");
75          matcher.read(reader);
76          assertEquals("One line read", 1, matcher.lines());
77          reader = new StringReader("One\nTwo");
78          matcher.read(reader);
79          assertEquals("One line read", 1, matcher.lines());
80          reader = new StringReader("One\nTwo\nThree");
81          matcher.read(reader);
82          assertEquals("Two lines read", 2, matcher.lines());
83          reader = new StringReader("One\nTwo\nThree\n");
84          matcher.read(reader);
85          assertEquals("Three lines read", 3, matcher.lines());
86      }
87      
88      public void testTooManyLines() throws Exception {
89          StringReader reader = new StringReader("WhateverWhateverWhateverWhateverWhateverWhateverWhateverWhatever");
90          matcher.read(reader);
91          assertEquals("Too many lines read", -1, matcher.lines());
92      }
93  }