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.6KB

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