View Javadoc
1   /*
2    * Copyright 2018 SPF4J.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.spf4j.test.log;
17  
18  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
19  import org.spf4j.log.Level;
20  
21  /**
22   *
23   * @author Zoltan Farkas
24   */
25  @SuppressWarnings("checkstyle:VisibilityModifier")
26  abstract class LogMatchingHandler implements LogHandler, LogAssert {
27  
28    private final Level minLevel;
29  
30    final LogStreamMatcher streamMatcher;
31  
32    LogMatchingHandler(final Level minLevel, final LogStreamMatcher streamMatcher) {
33      this.streamMatcher = streamMatcher;
34      this.minLevel = minLevel;
35    }
36  
37    public abstract void close();
38  
39  
40    @Override
41    public Handling handles(final Level level) {
42      return level.ordinal() >= minLevel.ordinal() ? Handling.HANDLE_PASS : Handling.NONE;
43    }
44  
45    @Override
46    @SuppressFBWarnings("CFS_CONFUSING_FUNCTION_SEMANTICS")
47    public TestLogRecord handle(final TestLogRecord record) {
48      streamMatcher.accept(record);
49      return record;
50    }
51  
52  
53    @Override
54    public void assertObservation() {
55      close();
56      if (!streamMatcher.isMatched()) {
57        throw new AssertionError(streamMatcher.toString());
58      }
59    }
60  
61  
62    @Override
63    public String toString() {
64      return "LogMatchingHandler{" + "minLevel=" + minLevel + ", matchers=" + streamMatcher + '}';
65    }
66  
67  
68  }