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