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.analysis.matchers;
20  
21  import org.junit.jupiter.api.BeforeEach;
22  import org.junit.jupiter.api.Test;
23  
24  import static org.junit.jupiter.api.Assertions.assertEquals;
25  
26  import java.util.regex.Pattern;
27  
28  import org.apache.rat.analysis.IHeaderMatcher.State;
29  
30  public class SimpleRegexMatcherTest {
31  
32      SimpleRegexMatcher target = new SimpleRegexMatcher(Pattern.compile("hello\\sworld"));
33      
34      @BeforeEach
35      public void setup() {
36          target.reset();
37      }
38      
39      @Test
40      public void testMatch() {
41          assertEquals( State.i, target.currentState());
42          assertEquals( State.i, target.matches("what in the world"));
43          assertEquals( State.i, target.currentState());
44          assertEquals( State.t, target.matches("hello world"));
45          assertEquals( State.t, target.currentState());
46          assertEquals( State.t, target.finalizeState()); 
47          assertEquals( State.t, target.currentState());
48          target.reset();
49          assertEquals( State.i, target.currentState());
50      }
51  
52      @Test
53      public void testNoMatch() {
54          assertEquals( State.i, target.currentState());
55          assertEquals( State.i, target.matches("what in the world"));
56          assertEquals( State.i, target.currentState());
57          assertEquals( State.i, target.matches("hello there"));
58          assertEquals( State.i, target.currentState());
59          assertEquals( State.f, target.finalizeState());
60          assertEquals( State.f, target.currentState());
61          target.reset();
62          assertEquals( State.i, target.currentState());
63      }
64      
65      @Test
66      public void testTrueIsAlwaysTrue() {
67          assertEquals( State.i, target.currentState());
68          assertEquals( State.t, target.matches("hello world"));
69          assertEquals( State.t, target.currentState());
70          assertEquals( State.t, target.matches("A non matching line"));
71          assertEquals( State.t, target.currentState());        
72          assertEquals( State.t, target.finalizeState()); 
73          assertEquals( State.t, target.currentState());
74          target.reset();
75          assertEquals( State.i, target.currentState());
76      }
77  }