diff options
Diffstat (limited to 'sonar-plugin-api/src')
11 files changed, 260 insertions, 223 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java index f3971eb7fb7..fc7e4b169ad 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java @@ -62,6 +62,7 @@ import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure; import org.sonar.api.batch.sensor.symbol.NewSymbolTable; import org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable; import org.sonar.api.config.Settings; +import org.sonar.api.config.MapSettings; import org.sonar.api.internal.ApiVersion; import org.sonar.api.internal.SonarRuntimeImpl; import org.sonar.api.measures.Metric; @@ -95,7 +96,7 @@ public class SensorContextTester implements SensorContext { private boolean cancelled; private SensorContextTester(Path moduleBaseDir) { - this.settings = new Settings(); + this.settings = new MapSettings(); this.fs = new DefaultFileSystem(moduleBaseDir); this.activeRules = new ActiveRulesBuilder().build(); this.sensorStorage = new InMemorySensorStorage(); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/config/Encryption.java b/sonar-plugin-api/src/main/java/org/sonar/api/config/Encryption.java index f07affe4767..03d40bc4822 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/config/Encryption.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/config/Encryption.java @@ -44,8 +44,8 @@ public final class Encryption { public Encryption(@Nullable String pathToSecretKey) { aesCipher = new AesCipher(pathToSecretKey); ciphers = ImmutableMap.of( - BASE64_ALGORITHM, new Base64Cipher(), - AES_ALGORITHM, aesCipher); + BASE64_ALGORITHM, new Base64Cipher(), + AES_ALGORITHM, aesCipher); } public void setPathToSecretKey(@Nullable String pathToSecretKey) { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/config/MapSettings.java b/sonar-plugin-api/src/main/java/org/sonar/api/config/MapSettings.java new file mode 100644 index 00000000000..d8726881e1e --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/config/MapSettings.java @@ -0,0 +1,71 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact 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.api.config; + +import com.google.common.collect.ImmutableMap; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +/** + * In-memory map-based implementation of {@link Settings}. It must be used + * <b>only for unit tests</b>. This is not the implementation + * deployed at runtime, so non-test code must never cast + * {@link Settings} to {@link MapSettings}. + * + * @since 6.1 + */ +public class MapSettings extends Settings { + + private final Map<String, String> props = new HashMap<>(); + + public MapSettings() { + super(new PropertyDefinitions(), new Encryption(null)); + } + + public MapSettings(PropertyDefinitions definitions) { + super(definitions, new Encryption(null)); + } + + @Override + protected Optional<String> get(String key) { + return Optional.ofNullable(props.get(key)); + } + + @Override + protected void set(String key, String value) { + props.put(key, value); + } + + @Override + protected void remove(String key) { + props.remove(key); + } + + @Override + public Map<String, String> getProperties() { + return ImmutableMap.copyOf(props); + } + + public MapSettings clear() { + props.clear(); + return this; + } +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/config/Settings.java b/sonar-plugin-api/src/main/java/org/sonar/api/config/Settings.java index f2cdf1aad8d..aa3f3bdc760 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/config/Settings.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/config/Settings.java @@ -22,13 +22,13 @@ package org.sonar.api.config; import com.google.common.base.Joiner; import com.google.common.base.Splitter; import com.google.common.base.Strings; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Maps; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Properties; +import java.util.stream.Collectors; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.apache.commons.lang.ArrayUtils; @@ -38,7 +38,11 @@ import org.sonar.api.ce.ComputeEngineSide; import org.sonar.api.server.ServerSide; import org.sonar.api.utils.DateUtils; +import static java.util.Objects.requireNonNull; +import static org.apache.commons.lang.StringUtils.trim; + /** + * * Project settings on batch side, or global settings on server side. This component does not access to database, so * property changed via setter methods are not persisted. * <br> @@ -55,7 +59,7 @@ import org.sonar.api.utils.DateUtils; * defaultValue = "A default value", * name = "My property"), * }) - * public class MyPlugin extends SonarPlugin { + * class MyPlugin extends SonarPlugin { * </code> * </pre> * then you can use: @@ -67,8 +71,8 @@ import org.sonar.api.utils.DateUtils; * <li>If you are using the {@link PropertyDefinition#builder(String)} way like: * <pre> * <code> - * public class MyPlugin extends SonarPlugin { - * public List getExtensions() { + * class MyPlugin extends SonarPlugin { + * List getExtensions() { * return Arrays.asList( * PropertyDefinition.builder("sonar.myProp").name("My property").defaultValue("A default value").build() * ); @@ -83,89 +87,130 @@ import org.sonar.api.utils.DateUtils; * </pre> * </li> * </ul> - * + * + * History - this class is abstract since 6.1. * @since 2.12 */ @ScannerSide @ServerSide @ComputeEngineSide -public class Settings { +public abstract class Settings { - protected Map<String, String> properties; - protected PropertyDefinitions definitions; - private Encryption encryption; + private final PropertyDefinitions definitions; + private final Encryption encryption; - public Settings() { - this(new PropertyDefinitions()); + protected Settings(PropertyDefinitions definitions, Encryption encryption) { + this.definitions = requireNonNull(definitions); + this.encryption = requireNonNull(encryption); } - public Settings(PropertyDefinitions definitions) { - this.properties = Maps.newHashMap(); - this.definitions = definitions; - this.encryption = new Encryption(null); + protected abstract Optional<String> get(String key); + + protected abstract void set(String key, String value); + + protected abstract void remove(String key); + + /** + * Immutable map of the properties that have non-default values. + * The default values defined by {@link PropertyDefinitions} are ignored, + * so the returned values are not the effective values. Basically only + * the non-empty results of {@link #getRawString(String)} are returned. + * <p> + * Values are not decrypted if they are encrypted with a secret key. + * </p> + */ + public abstract Map<String, String> getProperties(); + + // FIXME scope to be replaced by "protected" as soon as not used by JRubyFacade + public Encryption getEncryption() { + return encryption; } /** - * Clone settings. Actions are not propagated to cloned settings. + * The value that overrides the default value. It + * may be encrypted with a secret key. Use {@link #getString(String)} to get + * the effective and decrypted value. * - * @since 3.1 + * @since 6.1 */ - public Settings(Settings other) { - this.properties = Maps.newHashMap(other.getProperties()); - this.definitions = other.getDefinitions(); - this.encryption = other.getEncryption(); + public Optional<String> getRawString(String key) { + return get(definitions.validKey(requireNonNull(key))); } - public Encryption getEncryption() { - return encryption; + /** + * All the property definitions declared by core and plugins. + */ + public PropertyDefinitions getDefinitions() { + return definitions; } - @CheckForNull - public String getDefaultValue(String key) { - return definitions.getDefaultValue(key); + /** + * The definition related to the specified property. It may + * be empty. + * + * @since 6.1 + */ + public Optional<PropertyDefinition> getDefinition(String key) { + return Optional.ofNullable(definitions.get(key)); } + /** + * @return {@code true} if the property has a non-default value, else {@code false}. + */ public boolean hasKey(String key) { - return properties.containsKey(key); + return getRawString(key).isPresent(); + } + + @CheckForNull + public String getDefaultValue(String key) { + return definitions.getDefaultValue(key); } public boolean hasDefaultValue(String key) { return StringUtils.isNotEmpty(getDefaultValue(key)); } + /** + * The effective value of the specified property. Can return + * {@code null} if the property is not set and has no + * defined default value. + * <p> + * If the property is encrypted with a secret key, + * then the returned value is decrypted. + * </p> + * + * @throws IllegalStateException if value is encrypted but fails to be decrypted. + */ @CheckForNull public String getString(String key) { - String value = getClearString(key); - if (value != null && encryption.isEncrypted(value)) { + String effectiveKey = definitions.validKey(key); + Optional<String> value = getRawString(effectiveKey); + if (!value.isPresent()) { + // default values cannot be encrypted, so return value as-is. + return getDefaultValue(effectiveKey); + } + if (encryption.isEncrypted(value.get())) { try { - value = encryption.decrypt(value); + return encryption.decrypt(value.get()); } catch (Exception e) { - throw new IllegalStateException("Fail to decrypt the property " + key + ". Please check your secret key.", e); + throw new IllegalStateException("Fail to decrypt the property " + effectiveKey + ". Please check your secret key.", e); } } - return value; + return value.get(); } /** - * Does not decrypt value. + * Effective value as boolean. It is {@code false} if {@link #getString(String)} + * does not return {@code "true"}, even if it's not a boolean representation. + * @return {@code true} if the effective value is {@code "true"}, else {@code false}. */ - @CheckForNull - protected String getClearString(String key) { - doOnGetProperties(key); - String validKey = definitions.validKey(key); - String value = properties.get(validKey); - if (value == null) { - value = getDefaultValue(validKey); - } - return value; - } - public boolean getBoolean(String key) { String value = getString(key); return StringUtils.isNotEmpty(value) && Boolean.parseBoolean(value); } /** + * Effective value as int. * @return the value as int. If the property does not exist and has no default value, then 0 is returned. */ public int getInt(String key) { @@ -239,8 +284,8 @@ public class Settings { * </ul> */ public String[] getStringArray(String key) { - PropertyDefinition property = getDefinitions().get(key); - if ((null != property) && (property.multiValues())) { + Optional<PropertyDefinition> def = getDefinition(key); + if ((def.isPresent()) && (def.get().multiValues())) { String value = getString(key); if (value == null) { return ArrayUtils.EMPTY_STRING_ARRAY; @@ -271,7 +316,7 @@ public class Settings { } /** - * Value is splitted and trimmed. + * Value is split and trimmed. */ public String[] getStringArrayBySeparator(String key, String separator) { String value = getString(key); @@ -279,36 +324,27 @@ public class Settings { String[] strings = StringUtils.splitByWholeSeparator(value, separator); String[] result = new String[strings.length]; for (int index = 0; index < strings.length; index++) { - result[index] = StringUtils.trim(strings[index]); + result[index] = trim(strings[index]); } return result; } return ArrayUtils.EMPTY_STRING_ARRAY; } - public List<String> getKeysStartingWith(String prefix) { - List<String> result = new ArrayList<>(); - for (String key : properties.keySet()) { - if (StringUtils.startsWith(key, prefix)) { - result.add(key); - } - } - return result; - } - - public Settings appendProperty(String key, String value) { - String newValue = properties.get(definitions.validKey(key)); - if (StringUtils.isEmpty(newValue)) { - newValue = StringUtils.trim(value); + public Settings appendProperty(String key, @Nullable String value) { + Optional<String> existingValue = getRawString(definitions.validKey(key)); + String newValue; + if (!existingValue.isPresent()) { + newValue = trim(value); } else { - newValue += "," + StringUtils.trim(value); + newValue = existingValue.get() + "," + trim(value); } return setProperty(key, newValue); } public Settings setProperty(String key, @Nullable String[] values) { - PropertyDefinition property = getDefinitions().get(key); - if ((null == property) || (!property.multiValues())) { + Optional<PropertyDefinition> def = getDefinition(key); + if (!def.isPresent() || (!def.get().multiValues())) { throw new IllegalStateException("Fail to set multiple values on a single value property " + key); } @@ -324,7 +360,7 @@ public class Settings { } String escapedValue = Joiner.on(',').join(escaped); - text = StringUtils.trim(escapedValue); + text = trim(escapedValue); } return setProperty(key, text); } @@ -332,11 +368,10 @@ public class Settings { public Settings setProperty(String key, @Nullable String value) { String validKey = definitions.validKey(key); if (value == null) { - properties.remove(validKey); - doOnRemoveProperty(validKey); + removeProperty(validKey); + } else { - properties.put(validKey, StringUtils.trim(value)); - doOnSetProperty(validKey, value); + set(validKey, trim(value)); } return this; } @@ -379,11 +414,6 @@ public class Settings { return this; } - public Settings setProperties(Map<String, String> props) { - clear(); - return addProperties(props); - } - public Settings setProperty(String key, @Nullable Date date, boolean includeTime) { if (date == null) { return removeProperty(key); @@ -392,48 +422,14 @@ public class Settings { } public Settings removeProperty(String key) { - return setProperty(key, (String) null); - } - - public Settings clear() { - properties.clear(); - doOnClearProperties(); + remove(key); return this; } - /** - * - * @return immutable copy of properties. Encrypted values are kept and not decrypted. - */ - public Map<String, String> getProperties() { - return ImmutableMap.copyOf(properties); - } - - public PropertyDefinitions getDefinitions() { - return definitions; - } - - /** - * Create empty settings. Definition of available properties is loaded from the given annotated class. - * This method is usually used by unit tests. - */ - public static Settings createForComponent(Object component) { - return new Settings(new PropertyDefinitions(component)); - } - - protected void doOnSetProperty(String key, @Nullable String value) { - // can be overridden - } - - protected void doOnRemoveProperty(String key) { - // can be overridden - } - - protected void doOnClearProperties() { - // can be overridden + public List<String> getKeysStartingWith(String prefix) { + return getProperties().keySet().stream() + .filter(key -> StringUtils.startsWith(key, prefix)) + .collect(Collectors.toList()); } - protected void doOnGetProperties(String key) { - // can be overridden - } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectBuilderTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectBuilderTest.java index 6fbf0bdfdc7..5bb9adc01eb 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectBuilderTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/bootstrap/ProjectBuilderTest.java @@ -19,17 +19,15 @@ */ package org.sonar.api.batch.bootstrap; +import java.io.File; import org.junit.Test; import org.sonar.api.batch.bootstrap.internal.ProjectBuilderContext; import org.sonar.api.config.Settings; +import org.sonar.api.config.MapSettings; import static org.assertj.core.api.Assertions.assertThat; - -import java.io.File; - import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertThat; -import static org.junit.matchers.JUnitMatchers.hasItem; public class ProjectBuilderTest { @@ -38,7 +36,7 @@ public class ProjectBuilderTest { // this reactor is created and provided by Sonar final ProjectReactor projectReactor = new ProjectReactor(ProjectDefinition.create()); - ProjectBuilder builder = new ProjectBuilderSample(new Settings()); + ProjectBuilder builder = new ProjectBuilderSample(new MapSettings()); builder.build(new ProjectBuilderContext(projectReactor)); assertThat(projectReactor.getProjects().size(), is(2)); diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/cpd/internal/DefaultCpdTokensTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/cpd/internal/DefaultCpdTokensTest.java index 96310b70977..c5772e3b349 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/cpd/internal/DefaultCpdTokensTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/cpd/internal/DefaultCpdTokensTest.java @@ -23,6 +23,7 @@ import org.junit.Test; import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.sensor.internal.SensorStorage; import org.sonar.api.config.Settings; +import org.sonar.api.config.MapSettings; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.fail; @@ -42,7 +43,7 @@ public class DefaultCpdTokensTest { @Test public void save_no_tokens() { SensorStorage sensorStorage = mock(SensorStorage.class); - DefaultCpdTokens tokens = new DefaultCpdTokens(new Settings(), sensorStorage) + DefaultCpdTokens tokens = new DefaultCpdTokens(new MapSettings(), sensorStorage) .onFile(INPUT_FILE); tokens.save(); @@ -55,7 +56,7 @@ public class DefaultCpdTokensTest { @Test public void save_one_token() { SensorStorage sensorStorage = mock(SensorStorage.class); - DefaultCpdTokens tokens = new DefaultCpdTokens(new Settings(), sensorStorage) + DefaultCpdTokens tokens = new DefaultCpdTokens(new MapSettings(), sensorStorage) .onFile(INPUT_FILE) .addToken(INPUT_FILE.newRange(1, 2, 1, 5), "foo"); @@ -69,7 +70,7 @@ public class DefaultCpdTokensTest { @Test public void handle_exclusions_by_pattern() { SensorStorage sensorStorage = mock(SensorStorage.class); - Settings settings = new Settings(); + Settings settings = new MapSettings(); settings.setProperty("sonar.cpd.exclusions", "src/Foo.java,another"); DefaultCpdTokens tokens = new DefaultCpdTokens(settings, sensorStorage) .onFile(INPUT_FILE) @@ -85,7 +86,7 @@ public class DefaultCpdTokensTest { @Test public void save_many_tokens() { SensorStorage sensorStorage = mock(SensorStorage.class); - DefaultCpdTokens tokens = new DefaultCpdTokens(new Settings(), sensorStorage) + DefaultCpdTokens tokens = new DefaultCpdTokens(new MapSettings(), sensorStorage) .onFile(INPUT_FILE) .addToken(INPUT_FILE.newRange(1, 2, 1, 5), "foo") .addToken(INPUT_FILE.newRange(1, 6, 1, 10), "bar") @@ -106,7 +107,7 @@ public class DefaultCpdTokensTest { @Test public void basic_validation() { SensorStorage sensorStorage = mock(SensorStorage.class); - DefaultCpdTokens tokens = new DefaultCpdTokens(new Settings(), sensorStorage); + DefaultCpdTokens tokens = new DefaultCpdTokens(new MapSettings(), sensorStorage); try { tokens.save(); fail("Expected exception"); @@ -136,7 +137,7 @@ public class DefaultCpdTokensTest { @Test public void validate_tokens_order() { SensorStorage sensorStorage = mock(SensorStorage.class); - DefaultCpdTokens tokens = new DefaultCpdTokens(new Settings(), sensorStorage) + DefaultCpdTokens tokens = new DefaultCpdTokens(new MapSettings(), sensorStorage) .onFile(INPUT_FILE) .addToken(INPUT_FILE.newRange(1, 6, 1, 10), "bar"); diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java index 79f96f23e55..807d8663773 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java @@ -42,6 +42,7 @@ import org.sonar.api.batch.sensor.highlighting.TypeOfText; import org.sonar.api.batch.sensor.issue.NewIssue; import org.sonar.api.batch.sensor.symbol.NewSymbolTable; import org.sonar.api.config.Settings; +import org.sonar.api.config.MapSettings; import org.sonar.api.measures.CoreMetrics; import org.sonar.api.rule.RuleKey; import org.sonar.api.utils.SonarException; @@ -69,7 +70,7 @@ public class SensorContextTesterTest { @Test public void testSettings() { - Settings settings = new Settings(); + Settings settings = new MapSettings(); settings.setProperty("foo", "bar"); tester.setSettings(settings); assertThat(tester.settings().getString("foo")).isEqualTo("bar"); diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/config/EmailSettingsTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/config/EmailSettingsTest.java index 4f326137891..2c395c980ca 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/config/EmailSettingsTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/config/EmailSettingsTest.java @@ -19,31 +19,24 @@ */ package org.sonar.api.config; -import org.junit.Before; import org.junit.Test; import org.sonar.api.CoreProperties; -import org.sonar.api.config.EmailSettings; -import org.sonar.api.config.Settings; import static org.assertj.core.api.Assertions.assertThat; public class EmailSettingsTest { - EmailSettings emailSettings; - @Before - public void setUp() { - emailSettings = new EmailSettings(new Settings()); - } + private EmailSettings underTest = new EmailSettings(new MapSettings()); @Test public void should_return_default_values() { - assertThat(emailSettings.getSmtpHost()).isEqualTo(""); - assertThat(emailSettings.getSmtpPort()).isEqualTo(25); - assertThat(emailSettings.getSmtpUsername()).isEmpty(); - assertThat(emailSettings.getSmtpPassword()).isEmpty(); - assertThat(emailSettings.getSecureConnection()).isEmpty(); - assertThat(emailSettings.getFrom()).isEqualTo("noreply@nowhere"); - assertThat(emailSettings.getPrefix()).isEqualTo("[SONARQUBE]"); - assertThat(emailSettings.getServerBaseURL()).isEqualTo(CoreProperties.SERVER_BASE_URL_DEFAULT_VALUE); + assertThat(underTest.getSmtpHost()).isEqualTo(""); + assertThat(underTest.getSmtpPort()).isEqualTo(25); + assertThat(underTest.getSmtpUsername()).isEmpty(); + assertThat(underTest.getSmtpPassword()).isEmpty(); + assertThat(underTest.getSecureConnection()).isEmpty(); + assertThat(underTest.getFrom()).isEqualTo("noreply@nowhere"); + assertThat(underTest.getPrefix()).isEqualTo("[SONARQUBE]"); + assertThat(underTest.getServerBaseURL()).isEqualTo(CoreProperties.SERVER_BASE_URL_DEFAULT_VALUE); } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/config/SettingsTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/config/MapSettingsTest.java index 58822cadb7f..e4ba990c7ef 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/config/SettingsTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/config/MapSettingsTest.java @@ -19,7 +19,7 @@ */ package org.sonar.api.config; -import com.google.common.collect.ImmutableMap; +import java.util.Date; import org.assertj.core.data.Offset; import org.junit.Before; import org.junit.Rule; @@ -30,11 +30,9 @@ import org.sonar.api.Property; import org.sonar.api.PropertyType; import org.sonar.api.utils.DateUtils; -import java.util.Date; - import static org.assertj.core.api.Assertions.assertThat; -public class SettingsTest { +public class MapSettingsTest { private PropertyDefinitions definitions; @@ -52,7 +50,7 @@ public class SettingsTest { @Property(key = "newKeyWithDefaultValue", name = "New key with default value", deprecatedKey = "oldKeyWithDefaultValue", defaultValue = "default_value"), @Property(key = "new_multi_values", name = "New multi values", defaultValue = "1,2,3", multiValues = true, deprecatedKey = "old_multi_values") }) - static class Init { + private static class Init { } @Rule @@ -66,13 +64,13 @@ public class SettingsTest { @Test public void default_values_should_be_loaded_from_definitions() { - Settings settings = new Settings(definitions); + Settings settings = new MapSettings(definitions); assertThat(settings.getDefaultValue("hello")).isEqualTo("world"); } @Test public void set_property_int() { - Settings settings = new Settings(); + Settings settings = new MapSettings(); settings.setProperty("foo", 123); assertThat(settings.getInt("foo")).isEqualTo(123); assertThat(settings.getString("foo")).isEqualTo("123"); @@ -81,7 +79,7 @@ public class SettingsTest { @Test public void default_number_values_are_zero() { - Settings settings = new Settings(); + Settings settings = new MapSettings(); assertThat(settings.getInt("foo")).isEqualTo(0); assertThat(settings.getLong("foo")).isEqualTo(0L); } @@ -90,41 +88,34 @@ public class SettingsTest { public void getInt_value_must_be_valid() { thrown.expect(NumberFormatException.class); - Settings settings = new Settings(); + Settings settings = new MapSettings(); settings.setProperty("foo", "not a number"); settings.getInt("foo"); } @Test public void all_values_should_be_trimmed_set_property() { - Settings settings = new Settings(); + Settings settings = new MapSettings(); settings.setProperty("foo", " FOO "); assertThat(settings.getString("foo")).isEqualTo("FOO"); } @Test - public void all_values_should_be_trimmed_set_properties() { - Settings settings = new Settings(); - settings.setProperties(ImmutableMap.of("foo", " FOO ")); - assertThat(settings.getString("foo")).isEqualTo("FOO"); - } - - @Test public void test_get_default_value() { - Settings settings = new Settings(definitions); + Settings settings = new MapSettings(definitions); assertThat(settings.getDefaultValue("unknown")).isNull(); } @Test public void test_get_string() { - Settings settings = new Settings(definitions); + Settings settings = new MapSettings(definitions); settings.setProperty("hello", "Russia"); assertThat(settings.getString("hello")).isEqualTo("Russia"); } @Test public void setProperty_date() { - Settings settings = new Settings(); + Settings settings = new MapSettings(); Date date = DateUtils.parseDateTime("2010-05-18T15:50:45+0100"); settings.setProperty("aDate", date); settings.setProperty("aDateTime", date, true); @@ -135,7 +126,7 @@ public class SettingsTest { @Test public void test_get_date() { - Settings settings = new Settings(definitions); + Settings settings = new MapSettings(definitions); assertThat(settings.getDate("unknown")).isNull(); assertThat(settings.getDate("date").getDate()).isEqualTo(18); assertThat(settings.getDate("date").getMonth()).isEqualTo(4); @@ -143,13 +134,13 @@ public class SettingsTest { @Test public void test_get_date_not_found() { - Settings settings = new Settings(definitions); + Settings settings = new MapSettings(definitions); assertThat(settings.getDate("unknown")).isNull(); } @Test public void test_get_datetime() { - Settings settings = new Settings(definitions); + Settings settings = new MapSettings(definitions); assertThat(settings.getDateTime("unknown")).isNull(); assertThat(settings.getDateTime("datetime").getDate()).isEqualTo(18); assertThat(settings.getDateTime("datetime").getMonth()).isEqualTo(4); @@ -158,7 +149,7 @@ public class SettingsTest { @Test public void test_get_double() { - Settings settings = new Settings(); + Settings settings = new MapSettings(); settings.setProperty("from_double", 3.14159); settings.setProperty("from_string", "3.14159"); assertThat(settings.getDouble("from_double")).isEqualTo(3.14159, Offset.offset(0.00001)); @@ -168,7 +159,7 @@ public class SettingsTest { @Test public void test_get_float() { - Settings settings = new Settings(); + Settings settings = new MapSettings(); settings.setProperty("from_float", 3.14159f); settings.setProperty("from_string", "3.14159"); assertThat(settings.getDouble("from_float")).isEqualTo(3.14159f, Offset.offset(0.00001)); @@ -178,7 +169,7 @@ public class SettingsTest { @Test public void test_get_bad_float() { - Settings settings = new Settings(); + Settings settings = new MapSettings(); settings.setProperty("foo", "bar"); thrown.expect(IllegalStateException.class); @@ -188,7 +179,7 @@ public class SettingsTest { @Test public void test_get_bad_double() { - Settings settings = new Settings(); + Settings settings = new MapSettings(); settings.setProperty("foo", "bar"); thrown.expect(IllegalStateException.class); @@ -198,28 +189,28 @@ public class SettingsTest { @Test public void testSetNullFloat() { - Settings settings = new Settings(); + Settings settings = new MapSettings(); settings.setProperty("foo", (Float) null); assertThat(settings.getFloat("foo")).isNull(); } @Test public void testSetNullDouble() { - Settings settings = new Settings(); + Settings settings = new MapSettings(); settings.setProperty("foo", (Double) null); assertThat(settings.getDouble("foo")).isNull(); } @Test public void getStringArray() { - Settings settings = new Settings(definitions); + Settings settings = new MapSettings(definitions); String[] array = settings.getStringArray("array"); assertThat(array).isEqualTo(new String[] {"one", "two", "three"}); } @Test public void setStringArray() { - Settings settings = new Settings(definitions); + Settings settings = new MapSettings(definitions); settings.setProperty("multi_values", new String[] {"A", "B"}); String[] array = settings.getStringArray("multi_values"); assertThat(array).isEqualTo(new String[] {"A", "B"}); @@ -227,7 +218,7 @@ public class SettingsTest { @Test public void setStringArrayTrimValues() { - Settings settings = new Settings(definitions); + Settings settings = new MapSettings(definitions); settings.setProperty("multi_values", new String[] {" A ", " B "}); String[] array = settings.getStringArray("multi_values"); assertThat(array).isEqualTo(new String[] {"A", "B"}); @@ -235,7 +226,7 @@ public class SettingsTest { @Test public void setStringArrayEscapeCommas() { - Settings settings = new Settings(definitions); + Settings settings = new MapSettings(definitions); settings.setProperty("multi_values", new String[] {"A,B", "C,D"}); String[] array = settings.getStringArray("multi_values"); assertThat(array).isEqualTo(new String[] {"A,B", "C,D"}); @@ -243,7 +234,7 @@ public class SettingsTest { @Test public void setStringArrayWithEmptyValues() { - Settings settings = new Settings(definitions); + Settings settings = new MapSettings(definitions); settings.setProperty("multi_values", new String[] {"A,B", "", "C,D"}); String[] array = settings.getStringArray("multi_values"); assertThat(array).isEqualTo(new String[] {"A,B", "", "C,D"}); @@ -251,7 +242,7 @@ public class SettingsTest { @Test public void setStringArrayWithNullValues() { - Settings settings = new Settings(definitions); + Settings settings = new MapSettings(definitions); settings.setProperty("multi_values", new String[] {"A,B", null, "C,D"}); String[] array = settings.getStringArray("multi_values"); assertThat(array).isEqualTo(new String[] {"A,B", "", "C,D"}); @@ -259,20 +250,20 @@ public class SettingsTest { @Test(expected = IllegalStateException.class) public void shouldFailToSetArrayValueOnSingleValueProperty() { - Settings settings = new Settings(definitions); + Settings settings = new MapSettings(definitions); settings.setProperty("array", new String[] {"A", "B", "C"}); } @Test public void getStringArray_no_value() { - Settings settings = new Settings(); + Settings settings = new MapSettings(); String[] array = settings.getStringArray("array"); assertThat(array).isEmpty(); } @Test public void shouldTrimArray() { - Settings settings = new Settings(); + Settings settings = new MapSettings(); settings.setProperty("foo", " one, two, three "); String[] array = settings.getStringArray("foo"); assertThat(array).isEqualTo(new String[] {"one", "two", "three"}); @@ -280,7 +271,7 @@ public class SettingsTest { @Test public void shouldKeepEmptyValuesWhenSplitting() { - Settings settings = new Settings(); + Settings settings = new MapSettings(); settings.setProperty("foo", " one, , two"); String[] array = settings.getStringArray("foo"); assertThat(array).isEqualTo(new String[] {"one", "", "two"}); @@ -288,13 +279,13 @@ public class SettingsTest { @Test public void testDefaultValueOfGetString() { - Settings settings = new Settings(definitions); + Settings settings = new MapSettings(definitions); assertThat(settings.getString("hello")).isEqualTo("world"); } @Test public void set_property_boolean() { - Settings settings = new Settings(); + Settings settings = new MapSettings(); settings.setProperty("foo", true); settings.setProperty("bar", false); assertThat(settings.getBoolean("foo")).isTrue(); @@ -305,7 +296,7 @@ public class SettingsTest { @Test public void ignore_case_of_boolean_values() { - Settings settings = new Settings(); + Settings settings = new MapSettings(); settings.setProperty("foo", "true"); settings.setProperty("bar", "TRUE"); // labels in UI @@ -318,7 +309,7 @@ public class SettingsTest { @Test public void get_boolean() { - Settings settings = new Settings(definitions); + Settings settings = new MapSettings(definitions); assertThat(settings.getBoolean("boolean")).isTrue(); assertThat(settings.getBoolean("falseboolean")).isFalse(); assertThat(settings.getBoolean("unknown")).isFalse(); @@ -327,7 +318,8 @@ public class SettingsTest { @Test public void shouldCreateByIntrospectingComponent() { - Settings settings = Settings.createForComponent(MyComponent.class); + Settings settings = new MapSettings(); + settings.getDefinitions().addComponent(MyComponent.class); // property definition has been loaded, ie for default value assertThat(settings.getDefaultValue("foo")).isEqualTo("bar"); @@ -339,38 +331,20 @@ public class SettingsTest { } @Test - public void cloneSettings() { - Settings target = new Settings(definitions).setProperty("foo", "bar"); - Settings settings = new Settings(target); - - assertThat(settings.getString("foo")).isEqualTo("bar"); - assertThat(settings.getDefinitions()).isSameAs(definitions); - - // do not propagate changes - settings.setProperty("foo", "changed"); - settings.setProperty("new", "value"); - - assertThat(settings.getString("foo")).isEqualTo("changed"); - assertThat(settings.getString("new")).isEqualTo("value"); - assertThat(target.getString("foo")).isEqualTo("bar"); - assertThat(target.getString("new")).isNull(); - } - - @Test public void getStringLines_no_value() { - assertThat(new Settings().getStringLines("foo")).hasSize(0); + assertThat(new MapSettings().getStringLines("foo")).hasSize(0); } @Test public void getStringLines_single_line() { - Settings settings = new Settings(); + Settings settings = new MapSettings(); settings.setProperty("foo", "the line"); assertThat(settings.getStringLines("foo")).isEqualTo(new String[] {"the line"}); } @Test public void getStringLines_linux() { - Settings settings = new Settings(); + Settings settings = new MapSettings(); settings.setProperty("foo", "one\ntwo"); assertThat(settings.getStringLines("foo")).isEqualTo(new String[] {"one", "two"}); @@ -380,7 +354,7 @@ public class SettingsTest { @Test public void getStringLines_windows() { - Settings settings = new Settings(); + Settings settings = new MapSettings(); settings.setProperty("foo", "one\r\ntwo"); assertThat(settings.getStringLines("foo")).isEqualTo(new String[] {"one", "two"}); @@ -390,14 +364,14 @@ public class SettingsTest { @Test public void getStringLines_mix() { - Settings settings = new Settings(); + Settings settings = new MapSettings(); settings.setProperty("foo", "one\r\ntwo\nthree"); assertThat(settings.getStringLines("foo")).isEqualTo(new String[] {"one", "two", "three"}); } @Test public void getKeysStartingWith() { - Settings settings = new Settings(); + Settings settings = new MapSettings(); settings.setProperty("sonar.jdbc.url", "foo"); settings.setProperty("sonar.jdbc.username", "bar"); settings.setProperty("sonar.security", "admin"); @@ -409,7 +383,7 @@ public class SettingsTest { @Test public void should_fallback_deprecated_key_to_default_value_of_new_key() { - Settings settings = new Settings(definitions); + Settings settings = new MapSettings(definitions); assertThat(settings.getString("newKeyWithDefaultValue")).isEqualTo("default_value"); assertThat(settings.getString("oldKeyWithDefaultValue")).isEqualTo("default_value"); @@ -417,7 +391,7 @@ public class SettingsTest { @Test public void should_fallback_deprecated_key_to_new_key() { - Settings settings = new Settings(definitions); + Settings settings = new MapSettings(definitions); settings.setProperty("newKey", "value of newKey"); assertThat(settings.getString("newKey")).isEqualTo("value of newKey"); @@ -427,7 +401,7 @@ public class SettingsTest { @Test public void should_load_value_of_deprecated_key() { // it's used for example when deprecated settings are set through command-line - Settings settings = new Settings(definitions); + Settings settings = new MapSettings(definitions); settings.setProperty("oldKey", "value of oldKey"); assertThat(settings.getString("newKey")).isEqualTo("value of oldKey"); @@ -436,7 +410,7 @@ public class SettingsTest { @Test public void should_load_values_of_deprecated_key() { - Settings settings = new Settings(definitions); + Settings settings = new MapSettings(definitions); settings.setProperty("oldKey", "a,b"); assertThat(settings.getStringArray("newKey")).containsOnly("a", "b"); @@ -445,7 +419,7 @@ public class SettingsTest { @Test public void should_support_deprecated_props_with_multi_values() { - Settings settings = new Settings(definitions); + Settings settings = new MapSettings(definitions); settings.setProperty("new_multi_values", new String[] {" A ", " B "}); assertThat(settings.getStringArray("new_multi_values")).isEqualTo(new String[] {"A", "B"}); assertThat(settings.getStringArray("old_multi_values")).isEqualTo(new String[] {"A", "B"}); diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/FileExclusionsTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/FileExclusionsTest.java index dd01bd616b8..373631daff8 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/FileExclusionsTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/FileExclusionsTest.java @@ -22,13 +22,14 @@ package org.sonar.api.scan.filesystem; import org.junit.Test; import org.sonar.api.CoreProperties; import org.sonar.api.config.Settings; +import org.sonar.api.config.MapSettings; import static org.assertj.core.api.Assertions.assertThat; public class FileExclusionsTest { @Test public void ignore_inclusion_of_world() { - Settings settings = new Settings(); + Settings settings = new MapSettings(); settings.setProperty(CoreProperties.PROJECT_INCLUSIONS_PROPERTY, "**/*"); settings.setProperty(CoreProperties.PROJECT_TEST_INCLUSIONS_PROPERTY, "**/*"); assertThat(new FileExclusions(settings).sourceInclusions()).isEmpty(); @@ -37,7 +38,7 @@ public class FileExclusionsTest { @Test public void load_inclusions() { - Settings settings = new Settings(); + Settings settings = new MapSettings(); settings.setProperty(CoreProperties.PROJECT_INCLUSIONS_PROPERTY, "**/*Foo.java"); settings.setProperty(CoreProperties.PROJECT_TEST_INCLUSIONS_PROPERTY, "**/*FooTest.java"); FileExclusions moduleExclusions = new FileExclusions(settings); @@ -48,7 +49,7 @@ public class FileExclusionsTest { @Test public void load_exclusions() { - Settings settings = new Settings(); + Settings settings = new MapSettings(); settings.setProperty(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY, "**/*Foo.java"); settings.setProperty(CoreProperties.PROJECT_TEST_EXCLUSIONS_PROPERTY, "**/*FooTest.java"); FileExclusions moduleExclusions = new FileExclusions(settings); diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/DurationsTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/DurationsTest.java index cbda72dc5f7..409da22f751 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/DurationsTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/DurationsTest.java @@ -26,6 +26,7 @@ import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import org.sonar.api.CoreProperties; import org.sonar.api.config.Settings; +import org.sonar.api.config.MapSettings; import org.sonar.api.i18n.I18n; import java.util.Locale; @@ -54,7 +55,7 @@ public class DurationsTest { @Before public void setUp() { - settings = new Settings(); + settings = new MapSettings(); settings.setProperty(CoreProperties.HOURS_IN_DAY, HOURS_IN_DAY); durations = new Durations(settings, i18n); } |