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  
23  import org.junit.jupiter.api.BeforeEach;
24  import org.junit.jupiter.api.Test;
25  
26  import java.io.StringReader;
27  import java.util.regex.Pattern;
28  
29  import static org.junit.jupiter.api.Assertions.assertFalse;
30  import static org.junit.jupiter.api.Assertions.assertTrue;
31  
32  public class HeaderMatcherWithBeansTest {
33  
34      private HeaderMatcher matcher;
35      private SimpleCharFilter filter;
36      private HeaderBean[] beans;
37  
38      @BeforeEach
39      public void setUp() throws Exception {
40          HeaderBean[] beans = {
41                  new HeaderBean(),
42                  new HeaderBean(),
43                  new HeaderBean()
44              };
45          this.beans = beans;
46          filter = new SimpleCharFilter();
47          matcher = new HeaderMatcher(filter, 20, beans);
48      }
49  
50      @Test
51      public void nulls() throws Exception {
52          beans[0].setMatch(false);
53          beans[1].setMatch(true);
54          beans[2].setMatch(false);
55          StringReader reader = new StringReader("Whatever");
56          matcher.read(reader);   
57          assertFalse(beans[0].isMatch(),"State preserved");
58          assertTrue( beans[1].isMatch(),"State preserved");
59          assertFalse(beans[2].isMatch(), "State preserved");
60          beans[0].setMatch(true);
61          beans[1].setMatch(false);
62          beans[2].setMatch(true);
63          assertTrue(beans[0].isMatch(), "State preserved");
64          assertFalse(beans[1].isMatch(), "State preserved");
65          assertTrue(beans[2].isMatch(), "State preserved");
66      }
67  
68      @Test
69      public void matches() throws Exception {
70          beans[0].setHeaderPattern(Pattern.compile("What(.*)"));
71          beans[1].setHeaderPattern(Pattern.compile("(.*)ever"));
72          beans[2].setHeaderPattern(Pattern.compile("What"));
73          StringReader reader = new StringReader("Whatever");
74          matcher.read(reader);   
75          assertTrue(beans[0].isMatch(), "Match header pattern");
76          assertTrue(beans[1].isMatch(), "Match header pattern");
77          assertFalse(beans[2].isMatch(), "Match header pattern");
78      }
79  }