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.matchers.AbstractHeaderMatcher; 25 26 /** 27 * An Matcher for testing. 28 */ 29 public class TestingMatcher extends AbstractHeaderMatcher { 30 private State lastState; 31 private final boolean[] initialResults; 32 private Queue<Boolean> results; 33 public State finalState = State.f; 34 35 /** 36 * Constructs a matcher with an ID of "dfltMtch" that does not match anyting. 37 */ 38 public TestingMatcher() { 39 this("dfltMtch", false); 40 } 41 42 /** 43 * Constructs a matcher with the specified id and matching result. 44 * @param id the ID for this matcher 45 * @param result if {@code true} will match everything, otherwise it matches nothing. 46 */ 47 public TestingMatcher(boolean result) { 48 this("dfltMtch", result); 49 } 50 /** 51 * Constructs a matcher with the specified id and matching result. 52 * @param id the ID for this matcher 53 * @param result if {@code true} will match everything, otherwise it matches nothing. 54 */ 55 public TestingMatcher(String id, boolean result) { 56 this(id, new boolean[] { result }); 57 } 58 59 /** 60 * Constructs a matcher with the specified ID that returns the matching values in order. 61 * Will throw NPE if more {@code matches()} are called than there are results. 62 * @param id the id of the matcher. 63 * @param results the result for each call to match. 64 */ 65 public TestingMatcher(String id, boolean... results) { 66 super(id); 67 initialResults = results; 68 this.results = new LinkedList<>(); 69 reset(); 70 } 71 72 @Override 73 public final State matches(String line) { 74 if (lastState == State.t) { 75 return lastState; 76 } 77 if (line != null && results.poll()) { 78 lastState = State.t; 79 } 80 return lastState; 81 } 82 83 @Override 84 public void reset() { 85 lastState = State.i; 86 results.clear(); 87 for (boolean b : initialResults) { 88 this.results.add(b); 89 } 90 } 91 92 @Override 93 public State finalizeState() { 94 if (lastState == State.i) { 95 lastState = finalState; 96 } 97 return lastState; 98 } 99 100 @Override 101 public final State currentState() { 102 return lastState; 103 } 104 }