From: David Gageot Date: Mon, 24 Sep 2012 11:38:18 +0000 (+0200) Subject: SONAR-3529 API: ability to define property sets X-Git-Tag: 3.3~230 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=7a0aa306e3971891f9e3bbb8bd1037d8d02f3516;p=sonarqube.git SONAR-3529 API: ability to define property sets --- diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertySet.java b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertySet.java new file mode 100644 index 00000000000..fcc808eed91 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertySet.java @@ -0,0 +1,36 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.config; + +import com.google.common.collect.Lists; + +import java.util.List; + +public class PropertySet { + private final List fields = Lists.newArrayList(); + + public void add(PropertySetField field) { + fields.add(field); + } + + public List getFields() { + return fields; + } +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertySetDefinitions.java b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertySetDefinitions.java new file mode 100644 index 00000000000..07c6075725b --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertySetDefinitions.java @@ -0,0 +1,43 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.config; + +import com.google.common.collect.Maps; + +import java.util.Map; +import java.util.NoSuchElementException; + +public class PropertySetDefinitions { + private final Map index = Maps.newHashMap(); + + public void register(String name, PropertySet propertySet) { + if (null != index.put(name, propertySet)) { + throw new IllegalStateException("Unable to register two property sets with same name " + name); + } + } + + public PropertySet findByName(String name) { + PropertySet propertySet = index.get(name); + if (propertySet == null) { + throw new NoSuchElementException("Property set not found " + name); + } + return propertySet; + } +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertySetField.java b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertySetField.java new file mode 100644 index 00000000000..69376a4b6e3 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertySetField.java @@ -0,0 +1,64 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.config; + +import org.sonar.api.PropertyType; + +public final class PropertySetField { + private final String name; + private final PropertyType type; + private String description = ""; + private String defaultValue = ""; + + private PropertySetField(String name, PropertyType type) { + this.name = name; + this.type = type; + } + + public static PropertySetField create(String name, PropertyType type) { + return new PropertySetField(name, type); + } + + public String getName() { + return name; + } + + public PropertyType getType() { + return type; + } + + public String getDescription() { + return description; + } + + public PropertySetField setDescription(String description) { + this.description = description; + return this; + } + + public String getDefaultValue() { + return defaultValue; + } + + public PropertySetField setDefaultValue(String defaultValue) { + this.defaultValue = defaultValue; + return this; + } +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertySetTemplate.java b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertySetTemplate.java new file mode 100644 index 00000000000..cef931d16ad --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertySetTemplate.java @@ -0,0 +1,41 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.config; + +import org.sonar.api.ServerExtension; + +/** + * This extension point must be implemented to define a new property set. + * + * @since 3.3 + */ +public abstract class PropertySetTemplate implements ServerExtension { + /** + * Returns the {@link org.sonar.api.config.PropertySet} object that a property set. + * + * @return the property set + */ + public abstract PropertySet createPropertySet(); + + /** + * Property set unique name. + */ + public abstract String getName(); +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertySetDefinitionsTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertySetDefinitionsTest.java new file mode 100644 index 00000000000..5fcd1e21d22 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertySetDefinitionsTest.java @@ -0,0 +1,53 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.config; + +import org.junit.Test; + +import java.util.NoSuchElementException; + +import static org.fest.assertions.Assertions.assertThat; + +public class PropertySetDefinitionsTest { + PropertySetDefinitions definitions = new PropertySetDefinitions(); + + @Test + public void should_register_by_name() { + PropertySet set = new PropertySet(); + PropertySet other = new PropertySet(); + + definitions.register("name", set); + definitions.register("other", other); + + assertThat(definitions.findByName("name")).isSameAs(set); + assertThat(definitions.findByName("other")).isSameAs(other); + } + + @Test(expected = IllegalStateException.class) + public void should_fail_to_register_twice() { + definitions.register("name", new PropertySet()); + definitions.register("name", new PropertySet()); + } + + @Test(expected = NoSuchElementException.class) + public void should_fail_to_find_unknown_set() { + definitions.findByName("UNKNOWN"); + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertySetFieldTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertySetFieldTest.java new file mode 100644 index 00000000000..c1a06cb1d96 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertySetFieldTest.java @@ -0,0 +1,48 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.config; + +import org.junit.Test; +import org.sonar.api.PropertyType; + +import static org.fest.assertions.Assertions.assertThat; + +public class PropertySetFieldTest { + @Test + public void should_set_name_and_type() { + PropertySetField field = PropertySetField.create("name", PropertyType.STRING); + + assertThat(field.getName()).isEqualTo("name"); + assertThat(field.getType()).isEqualTo(PropertyType.STRING); + assertThat(field.getDefaultValue()).isEmpty(); + assertThat(field.getDescription()).isEmpty(); + } + + @Test + public void should_set_optional_characteristics() { + PropertySetField field = PropertySetField.create("name", PropertyType.STRING); + + field.setDefaultValue("default"); + field.setDescription("description"); + + assertThat(field.getDefaultValue()).isEqualTo("default"); + assertThat(field.getDescription()).isEqualTo("description"); + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertySetTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertySetTest.java new file mode 100644 index 00000000000..cd999147ed7 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertySetTest.java @@ -0,0 +1,39 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.config; + +import org.junit.Test; +import org.sonar.api.PropertyType; + +import static org.fest.assertions.Assertions.assertThat; + +public class PropertySetTest { + @Test + public void should_add_fields() { + PropertySetField firstField = PropertySetField.create("first", PropertyType.STRING); + PropertySetField secondField = PropertySetField.create("second", PropertyType.STRING); + + PropertySet set = new PropertySet(); + set.add(firstField); + set.add(secondField); + + assertThat(set.getFields()).containsExactly(firstField, secondField); + } +} diff --git a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java index ed12de72e04..5f369ba3454 100644 --- a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java @@ -246,6 +246,7 @@ public final class Platform { startupContainer.addSingleton(GeneratePluginIndex.class); startupContainer.addSingleton(RegisterNewFilters.class); startupContainer.addSingleton(RegisterNewDashboards.class); + startupContainer.addSingleton(RegisterPropertySets.class); startupContainer.startComponents(); startupContainer.getComponentByType(ServerLifecycleNotifier.class).notifyStart(); diff --git a/sonar-server/src/main/java/org/sonar/server/startup/RegisterPropertySets.java b/sonar-server/src/main/java/org/sonar/server/startup/RegisterPropertySets.java new file mode 100644 index 00000000000..a3a19951391 --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/startup/RegisterPropertySets.java @@ -0,0 +1,50 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.server.startup; + +import com.google.common.collect.ImmutableList; +import org.sonar.api.config.PropertySetDefinitions; +import org.sonar.api.config.PropertySetTemplate; +import org.sonar.api.utils.TimeProfiler; + +import java.util.List; + +/** + * @since 3.3 + */ +public final class RegisterPropertySets { + private final List propertySetTemplates; + private final PropertySetDefinitions propertySetDefinitions; + + public RegisterPropertySets(PropertySetTemplate[] propertySetTemplates, PropertySetDefinitions propertySetDefinitions) { + this.propertySetTemplates = ImmutableList.copyOf(propertySetTemplates); + this.propertySetDefinitions = propertySetDefinitions; + } + + public void start() { + TimeProfiler profiler = new TimeProfiler().start("Register dashboards"); + + for (PropertySetTemplate template : propertySetTemplates) { + propertySetDefinitions.register(template.getName(), template.createPropertySet()); + } + + profiler.stop(); + } +} diff --git a/sonar-server/src/test/java/org/sonar/server/startup/RegisterPropertySetsTest.java b/sonar-server/src/test/java/org/sonar/server/startup/RegisterPropertySetsTest.java new file mode 100644 index 00000000000..b74d1b8eb54 --- /dev/null +++ b/sonar-server/src/test/java/org/sonar/server/startup/RegisterPropertySetsTest.java @@ -0,0 +1,55 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.server.startup; + +import org.junit.Before; +import org.junit.Test; +import org.sonar.api.config.PropertySet; +import org.sonar.api.config.PropertySetDefinitions; +import org.sonar.api.config.PropertySetTemplate; + +import static org.mockito.Mockito.*; + +public class RegisterPropertySetsTest { + private RegisterPropertySets task; + private PropertySetDefinitions propertySetDefinitions = mock(PropertySetDefinitions.class); + private PropertySetTemplate firstTemplate = mock(PropertySetTemplate.class); + private PropertySetTemplate secondTemplate = mock(PropertySetTemplate.class); + private PropertySet firstPropertySet = mock(PropertySet.class); + private PropertySet secondPropertySet = mock(PropertySet.class); + + @Before + public void init() { + task = new RegisterPropertySets(new PropertySetTemplate[]{firstTemplate, secondTemplate}, propertySetDefinitions); + } + + @Test + public void should_register_on_startup() { + when(firstTemplate.getName()).thenReturn("first"); + when(secondTemplate.getName()).thenReturn("second"); + when(firstTemplate.createPropertySet()).thenReturn(firstPropertySet); + when(secondTemplate.createPropertySet()).thenReturn(secondPropertySet); + + task.start(); + + verify(propertySetDefinitions).register("first", firstPropertySet); + verify(propertySetDefinitions).register("second", secondPropertySet); + } +}