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