/** * Effective value as boolean. It is {@code empty} if {@link #get(String)} is empty or if it * does not return {@code "true"}, even if it's not a boolean representation. * @return {@code true} if the effective value is {@code "true"}, {@code false} for any other non empty value. * If the property does not have value nor default value, then {@code empty} is returned. */ default Optional<Boolean> getBoolean(String key) { return get(key).map(String::trim).map(Boolean::parseBoolean); }
private static Map<String, LanguageSpecificConfiguration> initLanguageSpecificConfigurationByLanguageKey(Configuration config) { ImmutableMap.Builder<String, LanguageSpecificConfiguration> builder = ImmutableMap.builder(); String[] languageConfigIndexes = config.getStringArray(LANGUAGE_SPECIFIC_PARAMETERS); for (String languageConfigIndex : languageConfigIndexes) { String languagePropertyKey = LANGUAGE_SPECIFIC_PARAMETERS + "." + languageConfigIndex + "." + LANGUAGE_SPECIFIC_PARAMETERS_LANGUAGE_KEY; String languageKey = config.get(languagePropertyKey) .orElseThrow(() -> MessageException.of("Technical debt configuration is corrupted. At least one language specific parameter has no Language key. " + "Contact your administrator to update this configuration in the global administration section of SonarQube.")); builder.put(languageKey, LanguageSpecificConfiguration.create(config, languageConfigIndex)); } return builder.build(); }
/** * Can be null when server is waiting for migration */ @Override @CheckForNull public String getId() { return config.get(CoreProperties.SERVER_ID).orElse(null); }
private static long initDefaultDevelopmentCost(Configuration config) { try { return Long.parseLong(config.get(DEVELOPMENT_COST).get()); } catch (NumberFormatException e) { throw new IllegalArgumentException("The value of the development cost property '" + DEVELOPMENT_COST + "' is incorrect. Expected long but got '" + config.get(DEVELOPMENT_COST).get() + "'", e); } }
static LanguageSpecificConfiguration create(Configuration config, String configurationId) { String configurationPrefix = LANGUAGE_SPECIFIC_PARAMETERS + "." + configurationId + "."; String language = config.get(configurationPrefix + LANGUAGE_SPECIFIC_PARAMETERS_LANGUAGE_KEY).orElse(null); String manDays = config.get(configurationPrefix + LANGUAGE_SPECIFIC_PARAMETERS_MAN_DAYS_KEY).orElse(null); String metric = config.get(configurationPrefix + LANGUAGE_SPECIFIC_PARAMETERS_SIZE_METRIC_KEY).orElse(null); return new LanguageSpecificConfiguration(language, manDays, metric); }
private String getRequiredSetting(String property) { String value = config.get(property).orElse(""); checkArgument(isNotEmpty(value), "Missing property %s", property); return value; }
/** * Effective value as {@code Float}. * @return the value as {@code Float}. If the property does not have value nor default value, then {@code empty} is returned. * @throws NumberFormatException if value is not empty and is not a parsable number */ default Optional<Float> getFloat(String key) { try { return get(key).map(String::trim).map(Float::valueOf); } catch (NumberFormatException e) { throw new IllegalStateException(String.format("The property '%s' is not an float value: %s", key, e.getMessage())); } }
/** * Effective value as {@code Double}. * @return the value as {@code Double}. If the property does not have value nor default value, then {@code empty} is returned. * @throws NumberFormatException if value is not empty and is not a parsable number */ default Optional<Double> getDouble(String key) { try { return get(key).map(String::trim).map(Double::valueOf); } catch (NumberFormatException e) { throw new IllegalStateException(String.format("The property '%s' is not an double value: %s", key, e.getMessage())); } }
public String getBaseUrl() { String url = config.get(SERVER_BASE_URL).orElse(""); if (isEmpty(url)) { url = computeBaseUrl(); } // Remove trailing slashes return StringUtils.removeEnd(url, "/"); }
@Override public void start() { configuration.get(PROPERTY_SONAR_CE_WORKER_COUNT) .ifPresent(workerCount -> LOG.warn("Property {} is not supported anymore and will be ignored." + " Remove it from sonar.properties to remove this warning.", PROPERTY_SONAR_CE_WORKER_COUNT)); }
@Override @CheckForNull public String getString(String key) { return config.getConfiguration().get(key).orElse(null); }
private void considerOldScmUrl() { settings.get(ScannerProperties.LINKS_SOURCES_DEV).ifPresent(url -> { if (StringUtils.startsWith(url, "scm:")) { String[] split = url.split(":"); if (split.length > 1) { setProviderIfSupported(split[1]); } } }); }
@CheckForNull public String label(int periodIndex) { String periodProperty = config.get(LEAK_PERIOD + periodIndex).orElse(null); PeriodParameters periodParameters = new PeriodParameters(periodProperty); return label(periodParameters.getMode(), periodParameters.getParam(), periodParameters.getDate()); }
@CheckForNull public String abbreviation(int periodIndex) { String periodProperty = config.get(LEAK_PERIOD + periodIndex).orElse(null); PeriodParameters periodParameters = new PeriodParameters(periodProperty); return abbreviation(periodParameters.getMode(), periodParameters.getParam(), periodParameters.getDate()); }
@Override public void start() { if (embeddedDatabase == null) { String jdbcUrl = config.get(JDBC_URL.getKey()).get(); if (startsWith(jdbcUrl, URL_PREFIX)) { embeddedDatabase = createEmbeddedDatabase(); embeddedDatabase.start(); } } }
@Test public void set_provider_from_valid_link() { when(settings.get(ScannerProperties.LINKS_SOURCES_DEV)).thenReturn(Optional.of("scm:" + scmProviderKey)); underTest.start(); assertThat(underTest.provider()).isSameAs(scmProvider); }
@Test public void return_global_settings() { settings.setProperty("key", "value"); Configuration config = underTest.newProjectConfiguration(PROJECT_KEY, new DefaultBranchImpl()); assertThat(config.get("key")).hasValue("value"); }
@Test public void project_settings_are_cached_to_avoid_db_access() { ComponentDto project = db.components().insertPrivateProject(); insertProjectProperty(project, "key", "value"); analysisMetadataHolder.setProject(Project.from(project)); Configuration config = underTest.getConfiguration(); assertThat(config.get("key")).hasValue("value"); db.executeUpdateSql("delete from properties"); db.commit(); assertThat(config.get("key")).hasValue("value"); }
@Test public void legacy_branch() { ComponentDto project = db.components().insertMainBranch(); db.properties().insertProperties(newComponentPropertyDto(project).setKey("sonar.leak.period").setValue("1")); Branch branch = createBranch("legacy", true); when(branch.isLegacyFeature()).thenReturn(true); Configuration config = underTest.newProjectConfiguration(project.getKey(), createBranch(branch.getName(), true)); assertThat(config.get("sonar.leak.period")).hasValue("1"); }
@Test public void branch_settings_contains_project_settings() { ComponentDto project = db.components().insertMainBranch(); db.properties().insertProperties(newComponentPropertyDto(project).setKey("key").setValue("value")); ComponentDto branch = db.components().insertProjectBranch(project); db.properties().insertProperties(newComponentPropertyDto(branch).setKey("sonar.leak.period").setValue("1")); Configuration config = underTest.newProjectConfiguration(project.getKey(), createBranch(branch.getBranch(), false)); assertThat(config.get("key")).hasValue("value"); assertThat(config.get("sonar.leak.period")).hasValue("1"); }