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.

LogsTest.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * SonarScanner CLI
  3. * Copyright (C) 2011-2024 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.sonarsource.scanner.cli;
  21. import java.io.PrintStream;
  22. import org.junit.Before;
  23. import org.junit.Test;
  24. import org.mockito.ArgumentMatchers;
  25. import org.mockito.Mock;
  26. import org.mockito.MockitoAnnotations;
  27. import org.sonarsource.scanner.lib.LogOutput;
  28. import static org.mockito.Mockito.mock;
  29. import static org.mockito.Mockito.reset;
  30. import static org.mockito.Mockito.verify;
  31. import static org.mockito.Mockito.verifyNoMoreInteractions;
  32. public class LogsTest {
  33. @Mock
  34. private PrintStream stdOut;
  35. @Mock
  36. private PrintStream stdErr;
  37. private Logs logs;
  38. @Before
  39. public void setUp() {
  40. MockitoAnnotations.initMocks(this);
  41. logs = new Logs(stdOut, stdErr);
  42. }
  43. @Test
  44. public void testInfo() {
  45. logs.info("info");
  46. verify(stdOut).println("INFO: info");
  47. verifyNoMoreInteractions(stdOut, stdErr);
  48. }
  49. @Test
  50. public void testWarn() {
  51. logs.warn("warn");
  52. verify(stdOut).println("WARN: warn");
  53. verifyNoMoreInteractions(stdOut, stdErr);
  54. }
  55. @Test
  56. public void testWarnWithTimestamp() {
  57. logs.setDebugEnabled(true);
  58. logs.warn("warn");
  59. verify(stdOut).println(ArgumentMatchers.matches("\\d\\d:\\d\\d:\\d\\d.\\d\\d\\d WARN: warn"));
  60. verifyNoMoreInteractions(stdOut, stdErr);
  61. }
  62. @Test
  63. public void testError() {
  64. Exception e = new NullPointerException("exception");
  65. logs.error("error1");
  66. verify(stdErr).println("ERROR: error1");
  67. logs.error("error2", e);
  68. verify(stdErr).println("ERROR: error2");
  69. verify(stdErr).println(e);
  70. // other interactions to print the exception..
  71. }
  72. @Test
  73. public void testDebug() {
  74. logs.setDebugEnabled(true);
  75. logs.debug("debug");
  76. verify(stdOut).println(ArgumentMatchers.matches("\\d\\d:\\d\\d:\\d\\d.\\d\\d\\d DEBUG: debug$"));
  77. logs.setDebugEnabled(false);
  78. logs.debug("debug");
  79. verifyNoMoreInteractions(stdOut, stdErr);
  80. }
  81. @Test
  82. public void should_forward_logs() {
  83. var mockedLogs = mock(Logs.class);
  84. var logOutput = new Logs.LogOutputAdapter(mockedLogs);
  85. String msg = "test";
  86. logOutput.log(msg, LogOutput.Level.DEBUG);
  87. verify(mockedLogs).debug(msg);
  88. verifyNoMoreInteractions(mockedLogs);
  89. reset(mockedLogs);
  90. logOutput.log(msg, LogOutput.Level.INFO);
  91. verify(mockedLogs).info(msg);
  92. verifyNoMoreInteractions(mockedLogs);
  93. reset(mockedLogs);
  94. logOutput.log(msg, LogOutput.Level.ERROR);
  95. verify(mockedLogs).error(msg);
  96. verifyNoMoreInteractions(mockedLogs);
  97. reset(mockedLogs);
  98. logOutput.log(msg, LogOutput.Level.WARN);
  99. verify(mockedLogs).warn(msg);
  100. verifyNoMoreInteractions(mockedLogs);
  101. reset(mockedLogs);
  102. logOutput.log(msg, LogOutput.Level.TRACE);
  103. verify(mockedLogs).debug(msg);
  104. verifyNoMoreInteractions(mockedLogs);
  105. reset(mockedLogs);
  106. }
  107. }