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.testhelpers;
20  
21  import java.util.LinkedList;
22  import java.util.Queue;
23  
24  import org.apache.rat.analysis.IHeaders;
25  import org.apache.rat.analysis.matchers.AbstractHeaderMatcher;
26  import org.apache.rat.config.parameters.ComponentType;
27  import org.apache.rat.config.parameters.ConfigComponent;
28  
29  /**
30   * A Matcher for testing.
31   */
32  @ConfigComponent(type = ComponentType.MATCHER, name = "TestingMatcher", desc = "Matcher used in testing")
33  public class TestingMatcher extends AbstractHeaderMatcher {
34      private final boolean[] initialResults;
35      private final Queue<Boolean> results;
36  
37      /**
38       * Constructs a matcher with an ID of "dfltMtch" that does not match anything.
39       */
40      public TestingMatcher() {
41          this("dfltMtch", false);
42      }
43  
44      /**
45       * Constructs a matcher with the specified id and matching result.
46       * @param result if {@code true} will match everything, otherwise it matches nothing.
47       */
48      public TestingMatcher(boolean result) {
49          this("dfltMtch", result);
50      }
51  
52      /**
53       * Constructs a matcher with the specified id and matching result.
54       * @param id the ID for this matcher
55       * @param result if {@code true} will match everything, otherwise it matches nothing.
56       */
57      public TestingMatcher(String id, boolean result) {
58          this(id, new boolean[] { result });
59      }
60  
61      /**
62       * Constructs a matcher with the specified ID that returns the matching values in order.
63       * Will throw NPE if more {@code matches()} are called than there are results.
64       * @param id the id of the matcher.
65       * @param results the result for each call to match.
66       */
67      public TestingMatcher(String id, boolean... results) {
68          super(id);
69          initialResults = results;
70          this.results = new LinkedList<>();
71          reset();
72      }
73  
74      @Override
75      public final boolean matches(IHeaders headers) {
76          return Boolean.TRUE.equals(results.poll());
77      }
78  
79      @Override
80      public void reset() {
81          results.clear();
82          for (boolean b : initialResults) {
83              this.results.add(b);
84          }
85      }
86  }