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 org.apache.rat.utils.Log;
22  
23  /**
24   * Log that captures output for later review.
25   */
26  public class TestingLog implements Log {
27      /** The captured logging. */
28      private StringBuilder captured = new StringBuilder();
29      /** The log level to capture. */
30      private Log.Level level = Log.Level.INFO;
31  
32      /**
33       * Clears the captured buffer.
34       */
35      public void clear() {
36          captured = new StringBuilder();
37      }
38  
39      /**
40       * Gets the captured log entries.
41       * @return the log entries.
42       */
43      public String getCaptured() {
44          return captured.toString();
45      }
46  
47      @Override
48      public Level getLevel() {
49          return level;
50      }
51  
52      @Override
53      public void setLevel(Level level) {
54          this.level = level;
55      }
56  
57      @Override
58      public void log(Level level, String msg) {
59          if (isEnabled(level))
60              captured.append(String.format("%s: %s%n", level, msg));
61      }
62  
63      /**
64       * Determines if the log is empty.
65       * @return {@code true} if the log is empty.
66       */
67      public boolean isEmpty() {
68          return captured.isEmpty();
69      }
70  }