001/*
002 * SonarQube
003 * Copyright (C) 2009-2017 SonarSource SA
004 * mailto:info AT sonarsource DOT com
005 *
006 * This program is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019 */
020package org.sonar.api.utils.log;
021
022import java.util.List;
023import org.junit.rules.ExternalResource;
024
025/**
026 * <b>For tests only</b>
027 * <br>
028 * This JUnit rule allows to configure and access logs in tests. By default
029 * trace level is enabled.
030 * <br>
031 * Warning - not compatible with parallel execution of tests in the same JVM fork.
032 * <br>
033 * Example:
034 * <pre>
035 * public class MyClass {
036 *   private final Logger logger = Loggers.get("logger_name");
037 *
038 *   public void doSomething() {
039 *     logger.info("foo");
040 *   }
041 * }
042 *
043 * public class MyTest {
044 *   &#064;org.junit.Rule
045 *   public LogTester logTester = new LogTester();
046 *
047 *   &#064;org.junit.Test
048 *   public void test_log() {
049 *     new MyClass().doSomething();
050 *
051 *     assertThat(logTester.logs()).containsOnly("foo");
052 *   }
053 * }
054 * </pre>
055 *
056 * @since 5.1
057 */
058public class LogTester extends ExternalResource {
059
060  @Override
061  protected void before() throws Throwable {
062    // this shared instance breaks compatibility with parallel execution of tests
063    LogInterceptors.set(new ListInterceptor());
064    setLevel(LoggerLevel.INFO);
065  }
066
067  @Override
068  protected void after() {
069    LogInterceptors.set(NullInterceptor.NULL_INSTANCE);
070    setLevel(LoggerLevel.INFO);
071  }
072
073  LoggerLevel getLevel() {
074    return Loggers.getFactory().getLevel();
075  }
076
077  /**
078   * Enable/disable debug logs. Info, warn and error logs are always enabled.
079   * By default INFO logs are enabled when LogTester is started.
080   */
081  public LogTester setLevel(LoggerLevel level) {
082    Loggers.getFactory().setLevel(level);
083    return this;
084  }
085
086  /**
087   * Logs in chronological order (item at index 0 is the oldest one)
088   */
089  public List<String> logs() {
090    return ((ListInterceptor) LogInterceptors.get()).logs();
091  }
092
093  /**
094   * Logs in chronological order (item at index 0 is the oldest one) for
095   * a given level
096   */
097  public List<String> logs(LoggerLevel level) {
098    return ((ListInterceptor) LogInterceptors.get()).logs(level);
099  }
100
101  public LogTester clear() {
102    ((ListInterceptor) LogInterceptors.get()).clear();
103    return this;
104  }
105}