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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
/*
* SonarQube
* Copyright (C) 2009-2025 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.sonar.batch.bootstrapper;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.assertj.core.api.Assertions.assertThat;
public class LoggingConfiguratorTest {
private static final String DEFAULT_CLASSPATH_CONF = "/org/sonar/batch/bootstrapper/logback.xml";
private static final String TEST_STR = "foo";
private LoggingConfiguration conf = new LoggingConfiguration();
private ByteArrayOutputStream out;
private SimpleLogListener listener;
@Rule
public TemporaryFolder folder = new TemporaryFolder();
@Before
public void setUp() {
out = new ByteArrayOutputStream();
conf = new LoggingConfiguration();
listener = new SimpleLogListener();
}
private static class SimpleLogListener implements LogOutput {
String msg;
LogOutput.Level level;
@Override
public void log(String msg, LogOutput.Level level) {
this.msg = msg;
this.level = level;
}
}
@Test
public void testWithFile() throws IOException {
InputStream is = this.getClass().getResourceAsStream(DEFAULT_CLASSPATH_CONF);
File tmpFolder = folder.getRoot();
File testFile = new File(tmpFolder, "test");
OutputStream os = new FileOutputStream(testFile);
IOUtils.copy(is, os);
os.close();
conf.setLogOutput(listener);
LoggingConfigurator.apply(conf, testFile);
Logger logger = LoggerFactory.getLogger(this.getClass());
logger.info(TEST_STR);
assertThat(listener.msg).endsWith(TEST_STR);
assertThat(listener.level).isEqualTo(LogOutput.Level.INFO);
}
@Test
public void testCustomAppender() {
conf.setLogOutput(listener);
LoggingConfigurator.apply(conf);
Logger logger = LoggerFactory.getLogger(this.getClass());
logger.info(TEST_STR);
assertThat(listener.msg).endsWith(TEST_STR);
assertThat(listener.level).isEqualTo(LogOutput.Level.INFO);
}
@Test
public void testNoStdout() throws UnsupportedEncodingException {
System.setOut(new PrintStream(out, false, StandardCharsets.UTF_8.name()));
conf.setLogOutput(listener);
LoggingConfigurator.apply(conf);
Logger logger = LoggerFactory.getLogger(this.getClass());
logger.error(TEST_STR);
logger.info(TEST_STR);
logger.debug(TEST_STR);
assertThat(out.size()).isZero();
}
@Test
public void testConfigureMultipleTimes() throws UnsupportedEncodingException {
System.setOut(new PrintStream(out, false, StandardCharsets.UTF_8.name()));
conf.setLogOutput(listener);
LoggingConfigurator.apply(conf);
Logger logger = LoggerFactory.getLogger(this.getClass());
logger.debug("debug");
assertThat(listener.msg).isNull();
conf.setVerbose(true);
LoggingConfigurator.apply(conf);
logger.debug("debug");
assertThat(listener.msg).isEqualTo("debug");
}
@Test
public void testFormatNoEffect() {
conf.setLogOutput(listener);
conf.setFormat("%t");
LoggingConfigurator.apply(conf);
Logger logger = LoggerFactory.getLogger(this.getClass());
logger.info("info");
assertThat(listener.msg).isEqualTo("info");
}
@Test
public void testNoListener() throws UnsupportedEncodingException {
System.setOut(new PrintStream(out, false, StandardCharsets.UTF_8.name()));
LoggingConfigurator.apply(conf);
Logger logger = LoggerFactory.getLogger(this.getClass());
logger.info("info");
assertThat(out.toString(StandardCharsets.UTF_8)).contains("info");
}
}
|