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 static org.junit.jupiter.api.Assertions.assertEquals;
22  
23  import org.apache.rat.analysis.IHeaderMatcher;
24  import org.apache.rat.analysis.IHeaderMatcher.State;
25  import org.junit.jupiter.api.BeforeEach;
26  import org.junit.jupiter.api.Test;
27  
28  public class SPDXMatcherTest {
29  
30      IHeaderMatcher target = SPDXMatcherFactory.INSTANCE.create("hello");
31  
32      @BeforeEach
33      public void setup() {
34          target.reset();
35      }
36  
37      @Test
38      public void testMatch() {
39          assertEquals(State.i, target.currentState());
40          assertEquals(State.i, target.matches("SPDX-License-Identifier: Apache-2"));
41          assertEquals(State.i, target.currentState());
42          assertEquals(State.t, target.matches("SPDX-License-Identifier: hello"));
43          assertEquals(State.t, target.currentState());
44          assertEquals(State.t, target.finalizeState());
45          assertEquals(State.t, target.currentState());
46          target.reset();
47          assertEquals(State.i, target.currentState());
48      }
49  
50      @Test
51      public void testNoMatch() {
52          assertEquals(State.i, target.currentState());
53          assertEquals(State.i, target.matches("SPDX-License-Identifier: Apache-2"));
54          assertEquals(State.i, target.currentState());
55          assertEquals(State.i, target.matches("SPDX-License-Identifier: MIT"));
56          assertEquals(State.i, target.currentState());
57          assertEquals(State.f, target.finalizeState());
58          assertEquals(State.f, target.currentState());
59          target.reset();
60          assertEquals(State.i, target.currentState());
61      }
62  
63      @Test
64      public void testTrueIsAlwaysTrue() {
65          assertEquals(State.i, target.currentState());
66          assertEquals(State.t, target.matches("SPDX-License-Identifier: hello"));
67          assertEquals(State.t, target.currentState());
68          assertEquals(State.t, target.matches("SPDX-License-Identifier: Apache-2"));
69          assertEquals(State.t, target.currentState());
70          assertEquals(State.t, target.finalizeState());
71          assertEquals(State.t, target.currentState());
72          target.reset();
73          assertEquals(State.i, target.currentState());
74      }
75      
76      @Test
77      public void testResetClearsLastMatch() {
78          
79          assertEquals(State.i, target.currentState());
80          assertEquals(State.t, target.matches("SPDX-License-Identifier: hello"));
81          assertEquals(State.t, target.currentState());
82          target.reset();
83          assertEquals(State.i, target.currentState());
84          assertEquals(State.i, target.matches("Something weird"));;
85      }
86  }