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