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 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * SonarQube Scanner
  3. * Copyright (C) 2011-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.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 static org.mockito.Mockito.verify;
  28. import static org.mockito.Mockito.verifyNoMoreInteractions;
  29. public class LogsTest {
  30. @Mock
  31. private PrintStream stdOut;
  32. @Mock
  33. private PrintStream stdErr;
  34. private Logs logs;
  35. @Before
  36. public void setUp() {
  37. MockitoAnnotations.initMocks(this);
  38. logs = new Logs(stdOut, stdErr);
  39. }
  40. @Test
  41. public void testInfo() {
  42. logs.info("info");
  43. verify(stdOut).println("INFO: info");
  44. verifyNoMoreInteractions(stdOut, stdErr);
  45. }
  46. @Test
  47. public void testWarn() {
  48. logs.warn("warn");
  49. verify(stdOut).println("WARN: warn");
  50. verifyNoMoreInteractions(stdOut, stdErr);
  51. }
  52. @Test
  53. public void testWarnWithTimestamp() {
  54. logs.setDebugEnabled(true);
  55. logs.warn("warn");
  56. verify(stdOut).println(ArgumentMatchers.matches("\\d\\d:\\d\\d:\\d\\d.\\d\\d\\d WARN: warn"));
  57. verifyNoMoreInteractions(stdOut, stdErr);
  58. }
  59. @Test
  60. public void testError() {
  61. Exception e = new NullPointerException("exception");
  62. logs.error("error1");
  63. verify(stdErr).println("ERROR: error1");
  64. logs.error("error2", e);
  65. verify(stdErr).println("ERROR: error2");
  66. verify(stdErr).println(e);
  67. // other interactions to print the exception..
  68. }
  69. @Test
  70. public void testDebug() {
  71. logs.setDebugEnabled(true);
  72. logs.debug("debug");
  73. verify(stdOut).println(ArgumentMatchers.matches("\\d\\d:\\d\\d:\\d\\d.\\d\\d\\d DEBUG: debug$"));
  74. logs.setDebugEnabled(false);
  75. logs.debug("debug");
  76. verifyNoMoreInteractions(stdOut, stdErr);
  77. }
  78. }