@Test public void test_default_category_on_annotation_plugin() { PropertyDefinitions def = new PropertyDefinitions(); def.addComponent(Categories.class, "default"); assertThat(def.getCategory("inCateg")).isEqualTo("categ"); assertThat(def.getCategory("noCateg")).isEqualTo("default"); }
/** * 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)); }
private void assertProperties(PropertyDefinitions definitions) { assertThat(definitions.get("foo").name()).isEqualTo("Foo"); assertThat(definitions.get("one").name()).isEqualTo("One"); assertThat(definitions.get("two").name()).isEqualTo("Two"); assertThat(definitions.get("unknown")).isNull(); assertThat(definitions.getDefaultValue("foo")).isNull(); assertThat(definitions.getDefaultValue("two")).isEqualTo("2"); assertThat(definitions.getAll().size()).isEqualTo(3); }
@Override public Optional<String> get(String key) { String effectiveKey = definitions.validKey(key); PropertyDefinition def = definitions.get(effectiveKey); if (def != null && (def.multiValues() || !def.fields().isEmpty())) { LOG.warn("Access to the multi-values/property set property '{}' should be made using 'getStringArray' method. The SonarQube plugin using this property should be updated.", key); } return getInternal(effectiveKey); }
@Before public void init_definitions() { definitions = new PropertyDefinitions(); definitions.addComponent(Init.class); }
@Test public void getProperties_return_empty_if_DB_error_on_first_call_ever_out_of_thread_cache() { SettingLoader settingLoaderMock = mock(SettingLoader.class); PersistenceException toBeThrown = new PersistenceException("Faking an error connecting to DB"); doThrow(toBeThrown).when(settingLoaderMock).loadAll(); underTest = new ThreadLocalSettings(new PropertyDefinitions(), new Properties(), settingLoaderMock); assertThat(underTest.getProperties()) .isEmpty(); }
public PropertyDefinitions addComponents(Collection components, String defaultCategory) { for (Object component : components) { addComponent(component, defaultCategory); } return this; }
private Set<String> loadKeys(ValuesRequest valuesRequest) { List<String> keys = valuesRequest.getKeys(); Set<String> result; if (keys == null || keys.isEmpty()) { result = concat(propertyDefinitions.getAll().stream().map(PropertyDefinition::key), SERVER_SETTING_KEYS.stream()).collect(Collectors.toSet()); } else { result = ImmutableSet.copyOf(keys); } result.forEach(SettingsWsSupport::validateKey); return result; }
@Test public void get_throws_NPE_if_key_is_null() { PropertyDefinitions underTest = new PropertyDefinitions(); expectedException.expect(NullPointerException.class); expectedException.expectMessage("key can't be null"); underTest.get(null); }
@Test public void test_categories_on_annotation_plugin() { PropertyDefinitions def = new PropertyDefinitions(Categories.class); assertThat(def.getCategory("inCateg")).isEqualTo("categ"); assertThat(def.getCategory("noCateg")).isEqualTo(""); }
private void assertPropertyDefinitions(ComponentContainer container, String... keys) { PropertyDefinitions propertyDefinitions = container.getComponentByType(PropertyDefinitions.class); if (keys.length == 0) { assertThat(propertyDefinitions.getAll()).isEmpty(); } else { for (String key : keys) { assertThat(propertyDefinitions.get(key)).isNotNull(); } } }
@CheckForNull public PropertyDefinition get(String key) { return definitions.get(validKey(key)); }
@Test public void validKey_throws_NPE_if_key_is_null() { PropertyDefinitions underTest = new PropertyDefinitions(); expectedException.expect(NullPointerException.class); expectedException.expectMessage("key can't be null"); underTest.validKey(null); }
public PropertyDefinitions(Collection<PropertyDefinition> components) { addComponents(components); }
@Test public void should_return_special_categories() { PropertyDefinitions def = new PropertyDefinitions(); assertThat(def.propertiesByCategory(null).get(new Category("general")).keySet()).containsOnly(new SubCategory("email")); assertThat(def.propertiesByCategory(null).get(new Category("general")).keySet().iterator().next().isSpecial()).isTrue(); assertThat(def.propertiesByCategory(null).get(new Category("security")).keySet()).containsOnly(new SubCategory("encryption")); assertThat(def.propertiesByCategory(null).get(new Category("security")).keySet().iterator().next().isSpecial()).isTrue(); assertThat(def.propertiesByCategory(null).get(new Category("licenses")).keySet()).containsOnly(new SubCategory("server_id")); assertThat(def.propertiesByCategory(null).get(new Category("licenses")).keySet().iterator().next().isSpecial()).isTrue(); }
@CheckForNull public String getDefaultValue(String key) { return definitions.getDefaultValue(key); }
private void addDefinition(PropertyDefinition definition, ListDefinitionsWsResponse.Builder wsResponse) { String key = definition.key(); Settings.Definition.Builder builder = wsResponse.addDefinitionsBuilder() .setKey(key) .setType(Settings.Type.valueOf(definition.type().name())) .setMultiValues(definition.multiValues()); ofNullable(emptyToNull(definition.deprecatedKey())).ifPresent(builder::setDeprecatedKey); ofNullable(emptyToNull(definition.name())).ifPresent(builder::setName); ofNullable(emptyToNull(definition.description())).ifPresent(builder::setDescription); String category = propertyDefinitions.getCategory(key); ofNullable(emptyToNull(category)).ifPresent(builder::setCategory); String subCategory = propertyDefinitions.getSubCategory(key); ofNullable(emptyToNull(subCategory)).ifPresent(builder::setSubCategory); ofNullable(emptyToNull(definition.defaultValue())).ifPresent(builder::setDefaultValue); List<String> options = definition.options(); if (!options.isEmpty()) { builder.addAllOptions(options); } List<PropertyFieldDefinition> fields = definition.fields(); if (!fields.isEmpty()) { fields.forEach(fieldDefinition -> addField(fieldDefinition, builder)); } }
public String getCategory(Property prop) { return getCategory(prop.key()); }
@Test public void should_inspect_plugin_classes() { PropertyDefinitions def = new PropertyDefinitions(PluginWithProperty.class, PluginWithProperties.class); assertProperties(def); }
@Test public void return_default_category() { logIn(); propertyDefinitions.addComponent(PropertyDefinition.builder("foo").build(), "default"); propertyDefinitions.addComponent(PropertyDefinition.builder("foo").category("").build(), "default"); ListDefinitionsWsResponse result = executeRequest(); assertThat(result.getDefinitionsList()).hasSize(1); assertThat(result.getDefinitions(0).getCategory()).isEqualTo("default"); assertThat(result.getDefinitions(0).getSubCategory()).isEqualTo("default"); }