View Javadoc
1   
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one   *
4    * or more contributor license agreements.  See the NOTICE file *
5    * distributed with this work for additional information        *
6    * regarding copyright ownership.  The ASF licenses this file   *
7    * to you under the Apache License, Version 2.0 (the            *
8    * "License"); you may not use this file except in compliance   *
9    * with the License.  You may obtain a copy of the License at   *
10   *                                                              *
11   *   http://www.apache.org/licenses/LICENSE-2.0                 *
12   *                                                              *
13   * Unless required by applicable law or agreed to in writing,   *
14   * software distributed under the License is distributed on an  *
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
16   * KIND, either express or implied.  See the License for the    *
17   * specific language governing permissions and limitations      *
18   * under the License.                                           *
19   */ 
20  package org.apache.rat.header;
21  
22  import org.junit.Before;
23  import org.junit.Test;
24  
25  import java.io.StringReader;
26  import java.util.regex.Pattern;
27  
28  import static org.junit.Assert.assertFalse;
29  import static org.junit.Assert.assertTrue;
30  
31  public class HeaderMatcherWithBeansTest {
32  
33      int capacity;
34      HeaderMatcher matcher;
35      SimpleCharFilter filter;
36      HeaderBean[] beans;
37  
38      @Before
39      public void setUp() throws Exception {
40          HeaderBean[] beans = {
41                  new HeaderBean(),
42                  new HeaderBean(),
43                  new HeaderBean()
44              };
45          this.beans = beans;
46          capacity = 20;
47          filter = new SimpleCharFilter();
48          matcher = new HeaderMatcher(filter, 20, beans);
49      }
50  
51      @Test
52      public void nulls() throws Exception {
53          beans[0].setMatch(false);
54          beans[1].setMatch(true);
55          beans[2].setMatch(false);
56          StringReader reader = new StringReader("Whatever");
57          matcher.read(reader);   
58          assertFalse("State preserved", beans[0].isMatch());
59          assertTrue("State preserved", beans[1].isMatch());
60          assertFalse("State preserved", beans[2].isMatch());
61          beans[0].setMatch(true);
62          beans[1].setMatch(false);
63          beans[2].setMatch(true);
64          assertTrue("State preserved", beans[0].isMatch());
65          assertFalse("State preserved", beans[1].isMatch());
66          assertTrue("State preserved", beans[2].isMatch());
67      }
68  
69      @Test
70      public void matches() throws Exception {
71          beans[0].setHeaderPattern(Pattern.compile("What(.*)"));
72          beans[1].setHeaderPattern(Pattern.compile("(.*)ever"));
73          beans[2].setHeaderPattern(Pattern.compile("What"));
74          StringReader reader = new StringReader("Whatever");
75          matcher.read(reader);   
76          assertTrue("Match header pattern", beans[0].isMatch());
77          assertTrue("Match header pattern", beans[1].isMatch());
78          assertFalse("Match header pattern", beans[2].isMatch());
79      }
80  }