aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-process/src
diff options
context:
space:
mode:
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>2017-08-24 16:56:40 +0200
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>2017-09-19 10:34:53 +0200
commit8677caa79431fd4f9c2fb990c9db742a2157a8f4 (patch)
tree76a1e25b9bb5ab70346c058119304bf419620891 /server/sonar-process/src
parentee9750c2fafc4e1e0f6d8e5e115ae759da4453c4 (diff)
downloadsonarqube-8677caa79431fd4f9c2fb990c9db742a2157a8f4.tar.gz
sonarqube-8677caa79431fd4f9c2fb990c9db742a2157a8f4.zip
SONAR-9283 Settings, Configuration and Props trim values at insert
Diffstat (limited to 'server/sonar-process/src')
-rw-r--r--server/sonar-process/src/main/java/org/sonar/process/Props.java15
-rw-r--r--server/sonar-process/src/test/java/org/sonar/process/PropsTest.java19
-rw-r--r--server/sonar-process/src/test/java/org/sonar/process/jmvoptions/JvmOptionsTest.java21
3 files changed, 32 insertions, 23 deletions
diff --git a/server/sonar-process/src/main/java/org/sonar/process/Props.java b/server/sonar-process/src/main/java/org/sonar/process/Props.java
index 53cb323b365..5869f9877e1 100644
--- a/server/sonar-process/src/main/java/org/sonar/process/Props.java
+++ b/server/sonar-process/src/main/java/org/sonar/process/Props.java
@@ -31,7 +31,8 @@ public class Props {
private final Encryption encryption;
public Props(Properties props) {
- this.properties = props;
+ this.properties = new Properties();
+ props.forEach((k, v) -> this.properties.put(k.toString().trim(), v == null ? null : v.toString().trim()));
this.encryption = new Encryption(props.getProperty(AesCipher.ENCRYPTION_SECRET_KEY_PATH));
}
@@ -41,7 +42,7 @@ public class Props {
@CheckForNull
public String value(String key) {
- String value = valueImpl(key);
+ String value = properties.getProperty(key);
if (value != null && encryption.isEncrypted(value)) {
value = encryption.decrypt(value);
}
@@ -110,18 +111,10 @@ public class Props {
}
public void setDefault(String key, String value) {
- String s = valueImpl(key);
+ String s = properties.getProperty(key);
if (StringUtils.isBlank(s)) {
properties.setProperty(key, value);
}
}
- @CheckForNull
- private String valueImpl(String key) {
- String value = properties.getProperty(key);
- if (value == null) {
- return null;
- }
- return value.trim();
- }
}
diff --git a/server/sonar-process/src/test/java/org/sonar/process/PropsTest.java b/server/sonar-process/src/test/java/org/sonar/process/PropsTest.java
index 0a6d4311dba..1f6d2412b61 100644
--- a/server/sonar-process/src/test/java/org/sonar/process/PropsTest.java
+++ b/server/sonar-process/src/test/java/org/sonar/process/PropsTest.java
@@ -25,6 +25,7 @@ import com.tngtech.java.junit.dataprovider.UseDataProvider;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
+import org.apache.commons.lang.RandomStringUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
@@ -44,9 +45,25 @@ public class PropsTest {
@Test
@UseDataProvider("beforeAndAfterBlanks")
+ public void constructor_trims_key_and_values_from_Properties_argument(String blankBefore, String blankAfter) {
+ Properties properties = new Properties();
+ String key = RandomStringUtils.randomAlphanumeric(3);
+ String value = RandomStringUtils.randomAlphanumeric(3);
+ properties.put(blankBefore + key + blankAfter, blankBefore + value + blankAfter);
+
+ Props underTest = new Props(properties);
+
+ if (!blankBefore.isEmpty() || !blankAfter.isEmpty()) {
+ assertThat(underTest.contains(blankBefore + key + blankAfter)).isFalse();
+ }
+ assertThat(underTest.value(key)).isEqualTo(value);
+ }
+
+ @Test
+ @UseDataProvider("beforeAndAfterBlanks")
public void value(String blankBefore, String blankAfter) {
Properties p = new Properties();
- p.setProperty("foo", blankBefore + "bar" + blankAfter);
+ p.setProperty(blankBefore + "foo" + blankAfter, blankBefore + "bar" + blankAfter);
p.setProperty("blank", blankBefore + blankAfter);
Props props = new Props(p);
diff --git a/server/sonar-process/src/test/java/org/sonar/process/jmvoptions/JvmOptionsTest.java b/server/sonar-process/src/test/java/org/sonar/process/jmvoptions/JvmOptionsTest.java
index 84d54ed339e..35de8ef0b8c 100644
--- a/server/sonar-process/src/test/java/org/sonar/process/jmvoptions/JvmOptionsTest.java
+++ b/server/sonar-process/src/test/java/org/sonar/process/jmvoptions/JvmOptionsTest.java
@@ -56,7 +56,6 @@ public class JvmOptionsTest {
private final String randomPrefix = "-" + randomAlphabetic(5).toLowerCase(Locale.ENGLISH);
private final String randomValue = randomAlphanumeric(4).toLowerCase(Locale.ENGLISH);
private final Properties properties = new Properties();
- private final Props props = new Props(properties);
private final JvmOptions underTest = new JvmOptions();
@Test
@@ -230,14 +229,14 @@ public class JvmOptionsTest {
public void addFromMandatoryProperty_fails_with_IAE_if_property_does_not_exist() {
expectMissingPropertyIAE(this.randomPropertyName);
- underTest.addFromMandatoryProperty(props, this.randomPropertyName);
+ underTest.addFromMandatoryProperty(new Props(properties), this.randomPropertyName);
}
@Test
public void addFromMandatoryProperty_fails_with_IAE_if_property_contains_an_empty_value() {
expectMissingPropertyIAE(this.randomPropertyName);
- underTest.addFromMandatoryProperty(props, randomPropertyName);
+ underTest.addFromMandatoryProperty(new Props(properties), randomPropertyName);
}
@Test
@@ -245,7 +244,7 @@ public class JvmOptionsTest {
public void addFromMandatoryProperty_adds_single_option_of_property_with_trimming(String emptyString) {
properties.put(randomPropertyName, emptyString + "-foo" + emptyString);
- underTest.addFromMandatoryProperty(props, randomPropertyName);
+ underTest.addFromMandatoryProperty(new Props(properties), randomPropertyName);
assertThat(underTest.getAll()).containsOnly("-foo");
}
@@ -257,7 +256,7 @@ public class JvmOptionsTest {
expectJvmOptionNotEmptyAndStartByDashMessageException(randomPropertyName, "foo");
- underTest.addFromMandatoryProperty(props, randomPropertyName);
+ underTest.addFromMandatoryProperty(new Props(properties), randomPropertyName);
}
@Test
@@ -265,7 +264,7 @@ public class JvmOptionsTest {
public void addFromMandatoryProperty_adds_options_of_property_with_trimming(String emptyString) {
properties.put(randomPropertyName, emptyString + "-foo" + emptyString + " -bar" + emptyString + " -duck" + emptyString);
- underTest.addFromMandatoryProperty(props, randomPropertyName);
+ underTest.addFromMandatoryProperty(new Props(properties), randomPropertyName);
assertThat(underTest.getAll()).containsOnly("-foo", "-bar", "-duck");
}
@@ -274,7 +273,7 @@ public class JvmOptionsTest {
public void addFromMandatoryProperty_supports_spaces_inside_options() {
properties.put(randomPropertyName, "-foo bar -duck");
- underTest.addFromMandatoryProperty(props, randomPropertyName);
+ underTest.addFromMandatoryProperty(new Props(properties), randomPropertyName);
assertThat(underTest.getAll()).containsOnly("-foo bar", "-duck");
}
@@ -298,7 +297,7 @@ public class JvmOptionsTest {
for (String optionOverride : optionOverrides) {
try {
properties.put(randomPropertyName, optionOverride);
- underTest.addFromMandatoryProperty(props, randomPropertyName);
+ underTest.addFromMandatoryProperty(new Props(properties), randomPropertyName);
fail("an MessageException should have been thrown");
} catch (MessageException e) {
assertThat(e.getMessage())
@@ -326,7 +325,7 @@ public class JvmOptionsTest {
for (String optionOverride : optionOverrides) {
properties.setProperty(randomPropertyName, optionOverride.toUpperCase(Locale.ENGLISH));
- underTest.addFromMandatoryProperty(props, randomPropertyName);
+ underTest.addFromMandatoryProperty(new Props(properties), randomPropertyName);
}
}
@@ -343,7 +342,7 @@ public class JvmOptionsTest {
"The following JVM options defined by property '" + randomPropertyName + "' are invalid: " +
overriding1 + " overwrites " + randomPrefix + randomValue + ", " + overriding2 + " overwrites " + randomPrefix + randomValue);
- underTest.addFromMandatoryProperty(props, randomPropertyName);
+ underTest.addFromMandatoryProperty(new Props(properties), randomPropertyName);
}
@Test
@@ -351,7 +350,7 @@ public class JvmOptionsTest {
JvmOptions underTest = new JvmOptions(ImmutableMap.of(randomPrefix, randomValue));
properties.put(randomPropertyName, randomPrefix + randomValue);
- underTest.addFromMandatoryProperty(props, randomPropertyName);
+ underTest.addFromMandatoryProperty(new Props(properties), randomPropertyName);
assertThat(underTest.getAll()).containsOnly(randomPrefix + randomValue);
}