Skip to content

Commit 7593c98

Browse files
committed
Add test to PropertyDumpPlugin
1 parent 8ee2085 commit 7593c98

File tree

3 files changed

+113
-6
lines changed

3 files changed

+113
-6
lines changed

property-dump-plugin/pom.xml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,25 @@
3333
<version>1.7.30</version>
3434
<scope>provided</scope>
3535
</dependency>
36+
<dependency>
37+
<groupId>org.junit.jupiter</groupId>
38+
<artifactId>junit-jupiter</artifactId>
39+
<version>5.11.4</version>
40+
<scope>test</scope>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.assertj</groupId>
44+
<artifactId>assertj-core</artifactId>
45+
<version>3.26.3</version>
46+
<scope>test</scope>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.sonarsource.sonarqube</groupId>
50+
<artifactId>sonar-plugin-api-impl</artifactId>
51+
<version>25.3.0.104237</version>
52+
<scope>test</scope>
53+
</dependency>
3654
</dependencies>
37-
38-
3955
<build>
4056
<plugins>
4157
<plugin>

property-dump-plugin/src/main/java/org/sonar/dump/PropertyDumpPlugin.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
import java.nio.file.Files;
2525
import java.nio.file.Path;
2626
import java.util.Arrays;
27+
import java.util.HashMap;
2728
import java.util.List;
29+
import java.util.Map;
2830
import java.util.Optional;
2931
import java.util.Properties;
3032
import org.slf4j.Logger;
@@ -39,6 +41,18 @@ public class PropertyDumpPlugin implements Plugin, Sensor {
3941

4042
private static final Logger LOG = LoggerFactory.getLogger(PropertyDumpPlugin.class);
4143

44+
// Visible for testing
45+
final Map<String, String> environmentVariables;
46+
47+
// Visible for testing
48+
final Properties systemProperties;
49+
50+
public PropertyDumpPlugin() {
51+
environmentVariables = new HashMap<>(System.getenv());
52+
systemProperties = new Properties();
53+
systemProperties.putAll(System.getProperties());
54+
}
55+
4256
@Override
4357
public void define(Context context) {
4458
context.addExtension(this);
@@ -57,8 +71,8 @@ public void execute(SensorContext sensorContext) {
5771
var props = new Properties();
5872
Configuration config = sensorContext.config();
5973
getPropertyKeys("DUMP_SENSOR_PROPERTIES").forEach(key -> props.setProperty(key, nonNull(config.get(key))));
60-
getPropertyKeys("DUMP_ENV_PROPERTIES").forEach(key -> props.setProperty(key, nonNull(System.getenv(key))));
61-
getPropertyKeys("DUMP_SYSTEM_PROPERTIES").forEach(key -> props.setProperty(key, nonNull(System.getProperty(key, ""))));
74+
getPropertyKeys("DUMP_ENV_PROPERTIES").forEach(key -> props.setProperty(key, nonNull(environmentVariables.get(key))));
75+
getPropertyKeys("DUMP_SYSTEM_PROPERTIES").forEach(key -> props.setProperty(key, nonNull(systemProperties.getProperty(key, ""))));
6276
props.stringPropertyNames().forEach(key -> LOG.info("{}={}", key, props.getProperty(key)));
6377
try (OutputStream out = Files.newOutputStream(filePath)) {
6478
props.store(out, null);
@@ -78,8 +92,8 @@ private static String nonNull(Object value) {
7892
return "";
7993
}
8094

81-
private static List<String> getPropertyKeys(String envName) {
82-
String list = System.getenv(envName);
95+
private List<String> getPropertyKeys(String envName) {
96+
String list = environmentVariables.get(envName);
8397
if (list == null) {
8498
return List.of();
8599
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* property-dump-plugin
3+
* Copyright (C) 2009-2025 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.sonar.dump;
21+
22+
import java.io.IOException;
23+
import java.nio.file.Files;
24+
import java.nio.file.Path;
25+
import java.util.List;
26+
import java.util.stream.Collectors;
27+
import org.junit.jupiter.api.Test;
28+
import org.junit.jupiter.api.io.TempDir;
29+
import org.sonar.api.batch.sensor.internal.SensorContextTester;
30+
31+
import static java.nio.charset.StandardCharsets.UTF_8;
32+
import static org.assertj.core.api.Assertions.assertThat;
33+
34+
class PropertyDumpPluginTest {
35+
36+
@Test
37+
void test_plugin_execute(@TempDir Path workDir) throws IOException {
38+
SensorContextTester sensorContext = SensorContextTester.create(Path.of("src", "test", "java"));
39+
sensorContext.fileSystem().setWorkDir(workDir);
40+
PropertyDumpPlugin plugin = new PropertyDumpPlugin();
41+
42+
// sensor properties
43+
sensorContext.settings().setProperty("sonar.my-settings-prop", "my-value");
44+
45+
// environment variables
46+
plugin.environmentVariables.put("MY_ENV1", "42");
47+
plugin.environmentVariables.put("MY_ENV2", "666");
48+
49+
// system properties
50+
plugin.systemProperties.setProperty("sonar.my-prop1", "Foo");
51+
plugin.systemProperties.setProperty("sonar.my-prop2", "Bar");
52+
53+
// define which elements to dump
54+
plugin.environmentVariables.put("DUMP_SENSOR_PROPERTIES", "sonar.my-settings-prop,sonar.unknown-settings-prop");
55+
plugin.environmentVariables.put("DUMP_ENV_PROPERTIES", "MY_ENV1,MY_ENV2,UNKNOWN_ENV");
56+
plugin.environmentVariables.put("DUMP_SYSTEM_PROPERTIES", "sonar.my-prop1,sonar.my-prop2,sonar.unknown-prop");
57+
58+
plugin.execute(sensorContext);
59+
60+
List<String> lines = Files.readAllLines(workDir.resolve("dumpSensor.system.properties"), UTF_8);
61+
String result = lines.stream()
62+
.filter(line -> !line.startsWith("#"))
63+
.sorted()
64+
.collect(Collectors.joining("\n"));
65+
66+
assertThat(result).isEqualTo("""
67+
MY_ENV1=42
68+
MY_ENV2=666
69+
UNKNOWN_ENV=
70+
sonar.my-prop1=Foo
71+
sonar.my-prop2=Bar
72+
sonar.my-settings-prop=my-value
73+
sonar.unknown-prop=
74+
sonar.unknown-settings-prop=""");
75+
}
76+
77+
}

0 commit comments

Comments
 (0)