You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LoggingConfiguratorTest.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.batch.bootstrapper;
  21. import java.io.ByteArrayOutputStream;
  22. import java.io.File;
  23. import java.io.FileOutputStream;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.io.OutputStream;
  27. import java.io.PrintStream;
  28. import java.io.UnsupportedEncodingException;
  29. import java.nio.charset.StandardCharsets;
  30. import org.apache.commons.io.IOUtils;
  31. import org.junit.Before;
  32. import org.junit.Rule;
  33. import org.junit.Test;
  34. import org.junit.rules.TemporaryFolder;
  35. import org.slf4j.Logger;
  36. import org.slf4j.LoggerFactory;
  37. import static org.assertj.core.api.Assertions.assertThat;
  38. public class LoggingConfiguratorTest {
  39. private static final String DEFAULT_CLASSPATH_CONF = "/org/sonar/batch/bootstrapper/logback.xml";
  40. private static final String TEST_STR = "foo";
  41. private LoggingConfiguration conf = new LoggingConfiguration();
  42. private ByteArrayOutputStream out;
  43. private SimpleLogListener listener;
  44. @Rule
  45. public TemporaryFolder folder = new TemporaryFolder();
  46. @Before
  47. public void setUp() {
  48. out = new ByteArrayOutputStream();
  49. conf = new LoggingConfiguration();
  50. listener = new SimpleLogListener();
  51. }
  52. private static class SimpleLogListener implements LogOutput {
  53. String msg;
  54. LogOutput.Level level;
  55. @Override
  56. public void log(String msg, LogOutput.Level level) {
  57. this.msg = msg;
  58. this.level = level;
  59. }
  60. }
  61. @Test
  62. public void testWithFile() throws IOException {
  63. InputStream is = this.getClass().getResourceAsStream(DEFAULT_CLASSPATH_CONF);
  64. File tmpFolder = folder.getRoot();
  65. File testFile = new File(tmpFolder, "test");
  66. OutputStream os = new FileOutputStream(testFile);
  67. IOUtils.copy(is, os);
  68. os.close();
  69. conf.setLogOutput(listener);
  70. LoggingConfigurator.apply(conf, testFile);
  71. Logger logger = LoggerFactory.getLogger(this.getClass());
  72. logger.info(TEST_STR);
  73. assertThat(listener.msg).endsWith(TEST_STR);
  74. assertThat(listener.level).isEqualTo(LogOutput.Level.INFO);
  75. }
  76. @Test
  77. public void testCustomAppender() {
  78. conf.setLogOutput(listener);
  79. LoggingConfigurator.apply(conf);
  80. Logger logger = LoggerFactory.getLogger(this.getClass());
  81. logger.info(TEST_STR);
  82. assertThat(listener.msg).endsWith(TEST_STR);
  83. assertThat(listener.level).isEqualTo(LogOutput.Level.INFO);
  84. }
  85. @Test
  86. public void testNoStdout() throws UnsupportedEncodingException {
  87. System.setOut(new PrintStream(out, false, StandardCharsets.UTF_8.name()));
  88. conf.setLogOutput(listener);
  89. LoggingConfigurator.apply(conf);
  90. Logger logger = LoggerFactory.getLogger(this.getClass());
  91. logger.error(TEST_STR);
  92. logger.info(TEST_STR);
  93. logger.debug(TEST_STR);
  94. assertThat(out.size()).isEqualTo(0);
  95. }
  96. @Test
  97. public void testConfigureMultipleTimes() throws UnsupportedEncodingException {
  98. System.setOut(new PrintStream(out, false, StandardCharsets.UTF_8.name()));
  99. conf.setLogOutput(listener);
  100. LoggingConfigurator.apply(conf);
  101. Logger logger = LoggerFactory.getLogger(this.getClass());
  102. logger.debug("debug");
  103. assertThat(listener.msg).isNull();
  104. conf.setVerbose(true);
  105. LoggingConfigurator.apply(conf);
  106. logger.debug("debug");
  107. assertThat(listener.msg).isEqualTo("debug");
  108. }
  109. @Test
  110. public void testFormatNoEffect() {
  111. conf.setLogOutput(listener);
  112. conf.setFormat("%t");
  113. LoggingConfigurator.apply(conf);
  114. Logger logger = LoggerFactory.getLogger(this.getClass());
  115. logger.info("info");
  116. assertThat(listener.msg).isEqualTo("info");
  117. }
  118. @Test
  119. public void testNoListener() throws UnsupportedEncodingException {
  120. System.setOut(new PrintStream(out, false, StandardCharsets.UTF_8.name()));
  121. LoggingConfigurator.apply(conf);
  122. Logger logger = LoggerFactory.getLogger(this.getClass());
  123. logger.info("info");
  124. assertThat(new String(out.toByteArray(), StandardCharsets.UTF_8)).contains("info");
  125. }
  126. }