aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/sonarsource/scanner/cli/LogsTest.java
blob: 9789660ff9f16611e0b96f2cba517906288f2b97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
 * SonarScanner CLI
 * Copyright (C) 2011-2021 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonarsource.scanner.cli;

import java.io.PrintStream;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;

public class LogsTest {
  @Mock
  private PrintStream stdOut;

  @Mock
  private PrintStream stdErr;

  private Logs logs;

  @Before
  public void setUp() {
    MockitoAnnotations.initMocks(this);
    logs = new Logs(stdOut, stdErr);
  }

  @Test
  public void testInfo() {
    logs.info("info");
    verify(stdOut).println("INFO: info");
    verifyNoMoreInteractions(stdOut, stdErr);
  }

  @Test
  public void testWarn() {
    logs.warn("warn");
    verify(stdOut).println("WARN: warn");
    verifyNoMoreInteractions(stdOut, stdErr);
  }

  @Test
  public void testWarnWithTimestamp() {
    logs.setDebugEnabled(true);
    logs.warn("warn");
    verify(stdOut).println(ArgumentMatchers.matches("\\d\\d:\\d\\d:\\d\\d.\\d\\d\\d WARN: warn"));
    verifyNoMoreInteractions(stdOut, stdErr);
  }

  @Test
  public void testError() {
    Exception e = new NullPointerException("exception");
    logs.error("error1");
    verify(stdErr).println("ERROR: error1");

    logs.error("error2", e);
    verify(stdErr).println("ERROR: error2");
    verify(stdErr).println(e);
    // other interactions to print the exception..
  }

  @Test
  public void testDebug() {
    logs.setDebugEnabled(true);

    logs.debug("debug");
    verify(stdOut).println(ArgumentMatchers.matches("\\d\\d:\\d\\d:\\d\\d.\\d\\d\\d DEBUG: debug$"));

    logs.setDebugEnabled(false);
    logs.debug("debug");
    verifyNoMoreInteractions(stdOut, stdErr);
  }
}