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