diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2011-10-05 00:44:37 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2011-10-07 13:36:25 +0200 |
commit | ef5bf7fdece84e5a52c8d478c8a733ecdf4d57f1 (patch) | |
tree | c64a8a041183379e92fbc81becc77af07c9b38e3 /sonar-plugin-api/src/test | |
parent | 3174a29201e922db758b51f4b693ca131e7037ec (diff) | |
download | sonarqube-ef5bf7fdece84e5a52c8d478c8a733ecdf4d57f1.tar.gz sonarqube-ef5bf7fdece84e5a52c8d478c8a733ecdf4d57f1.zip |
SONAR-2861 New Configuration API
The component org.apache.commons.Configuration is still available but plugins should use org.sonar.api.config.Settings.
It also implies the following issues :
SONAR-2870 do not rebuild the WAR file when editing sonar.properties
SONAR-2869 allow to use the annotations @Properties/@Property on extensions
Diffstat (limited to 'sonar-plugin-api/src/test')
6 files changed, 381 insertions, 64 deletions
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/BatchExtensionDictionnaryTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/BatchExtensionDictionnaryTest.java index 37e6e644c49..bda9de6d1bc 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/BatchExtensionDictionnaryTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/BatchExtensionDictionnaryTest.java @@ -21,16 +21,12 @@ package org.sonar.api.batch; import com.google.common.collect.Lists; import org.junit.Test; -import org.picocontainer.Characteristics; -import org.picocontainer.DefaultPicoContainer; -import org.picocontainer.PicoContainer; -import org.picocontainer.containers.TransientPicoContainer; import org.sonar.api.BatchExtension; import org.sonar.api.measures.CoreMetrics; import org.sonar.api.measures.Metric; +import org.sonar.api.platform.ComponentContainer; import org.sonar.api.resources.Project; -import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -44,10 +40,9 @@ import static org.mockito.Mockito.mock; public class BatchExtensionDictionnaryTest { private BatchExtensionDictionnary newSelector(BatchExtension... extensions) { - TransientPicoContainer iocContainer = new TransientPicoContainer(); - int key = 0; + ComponentContainer iocContainer = new ComponentContainer(); for (BatchExtension extension : extensions) { - iocContainer.addComponent(key++, extension); + iocContainer.addSingleton(extension); } return new BatchExtensionDictionnary(iocContainer); } @@ -67,14 +62,14 @@ public class BatchExtensionDictionnaryTest { @Test public void shouldSearchInParentContainers() { - TransientPicoContainer grandParent = new TransientPicoContainer(); - grandParent.as(Characteristics.CACHE).addComponent("ncloc", CoreMetrics.NCLOC); + ComponentContainer grandParent = new ComponentContainer(); + grandParent.addSingleton(CoreMetrics.NCLOC); - DefaultPicoContainer parent = (DefaultPicoContainer)grandParent.makeChildContainer(); - parent.as(Characteristics.CACHE).addComponent("coverage", CoreMetrics.COVERAGE); + ComponentContainer parent = grandParent.createChild(); + parent.addSingleton(CoreMetrics.COVERAGE); - DefaultPicoContainer child = (DefaultPicoContainer)parent.makeChildContainer(); - child.as(Characteristics.CACHE).addComponent("complexity", CoreMetrics.COMPLEXITY); + ComponentContainer child = parent.createChild(); + child.addSingleton(CoreMetrics.COMPLEXITY); BatchExtensionDictionnary dictionnary = new BatchExtensionDictionnary(child); assertThat(dictionnary.select(Metric.class).size(), is(3)); diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionsTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionsTest.java new file mode 100644 index 00000000000..857dbb82aed --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionsTest.java @@ -0,0 +1,93 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 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.Properties; +import org.sonar.api.Property; + +import java.util.Arrays; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.junit.Assert.assertThat; + +public class PropertyDefinitionsTest { + + @Test + public void shouldInspectPluginObjects() { + PropertyDefinitions def = new PropertyDefinitions(new PluginWithProperty(), new PluginWithProperties()); + + assertProperties(def); + } + + @Test + public void shouldInspectPluginClasses() { + PropertyDefinitions def = new PropertyDefinitions(PluginWithProperty.class, PluginWithProperties.class); + + assertProperties(def); + } + + private void assertProperties(PropertyDefinitions def) { + assertThat(def.getProperty("foo").name(), is("Foo")); + assertThat(def.getProperty("one").name(), is("One")); + assertThat(def.getProperty("two").name(), is("Two")); + assertThat(def.getProperty("unknown"), nullValue()); + + assertThat(def.getDefaultValue("foo"), nullValue()); + assertThat(def.getDefaultValue("two"), is("2")); + + assertThat(def.getProperties().size(), is(3)); + } + + @Property(key = "foo", name = "Foo") + static final class PluginWithProperty { + } + + @Properties({ + @Property(key = "one", name = "One"), + @Property(key = "two", name = "Two", defaultValue = "2") + }) + static final class PluginWithProperties { + } + + + @Test + public void testCategories() { + PropertyDefinitions def = new PropertyDefinitions(Categories.class); + assertThat(def.getCategory("inCateg"), is("categ")); + assertThat(def.getCategory("noCateg"), is("")); + } + + @Test + public void testDefaultCategory() { + PropertyDefinitions def = new PropertyDefinitions(); + def.addComponent(Categories.class, "default"); + assertThat(def.getCategory("inCateg"), is("categ")); + assertThat(def.getCategory("noCateg"), is("default")); + } + + @Properties({ + @Property(key = "inCateg", name="In Categ", category = "categ"), + @Property(key = "noCateg", name="No categ") + }) + static final class Categories { + } +} 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/SettingsTest.java new file mode 100644 index 00000000000..c34f1278ca4 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/config/SettingsTest.java @@ -0,0 +1,124 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 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.hamcrest.CoreMatchers; +import org.junit.Before; +import org.junit.Test; +import org.sonar.api.Property; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class SettingsTest { + + private PropertyDefinitions definitions; + + @Before + public void initDefinitions() { + definitions = new PropertyDefinitions(); + definitions.addProperty(newProperty("hello", "world")); + definitions.addProperty(newProperty("date", "2010-05-18")); + definitions.addProperty(newProperty("boolean", "true")); + definitions.addProperty(newProperty("falseboolean", "false")); + definitions.addProperty(newProperty("integer", "12345")); + definitions.addProperty(newProperty("array", "one,two,three")); + } + + private Property newProperty(String key, String defaultValue) { + Property prop = mock(Property.class); + when(prop.key()).thenReturn(key); + when(prop.defaultValue()).thenReturn(defaultValue); + return prop; + } + + @Test + public void defaultValuesShouldBeLoadedFromDefinitions() { + Settings settings = new Settings(definitions); + assertThat(settings.getDefaultValue("hello"), is("world")); + } + + @Test + public void testGetDefaultValue() { + Settings settings = new Settings(definitions); + assertThat(settings.getDefaultValue("unknown"), nullValue()); + } + + @Test + public void testGetString() { + Settings settings = new Settings(definitions); + settings.setProperty("hello", "Russia"); + assertThat(settings.getString("hello"), is("Russia")); + } + + @Test + public void testGetDate() { + Settings settings = new Settings(definitions); + assertThat(settings.getDate("date").getDate(), is(18)); + assertThat(settings.getDate("date").getMonth(), is(4)); + } + + @Test + public void testGetDateNotFound() { + Settings settings = new Settings(definitions); + assertThat(settings.getDate("unknown"), CoreMatchers.<Object>nullValue()); + } + + @Test + public void testGetArray() { + Settings settings = new Settings(definitions); + String[] array = settings.getStringArray("array"); + assertThat(array.length, is(3)); + assertThat(array[0], is("one")); + assertThat(array[1], is("two")); + assertThat(array[2], is("three")); + } + + @Test + public void testDefaultValueOfGetString() { + Settings settings = new Settings(definitions); + assertThat(settings.getString("hello"), is("world")); + } + + @Test + public void testGetBoolean() { + Settings settings = new Settings(definitions); + assertThat(settings.getBoolean("boolean"), is(true)); + assertThat(settings.getBoolean("falseboolean"), is(false)); + assertThat(settings.getBoolean("unknown"), is(false)); + assertThat(settings.getBoolean("hello"), is(false)); + } + + @Test + public void shouldCreateByIntrospectingComponent() { + Settings settings = Settings.createForComponent(MyComponent.class); + + // property definition has been loaded, ie for default value + assertThat(settings.getDefaultValue("foo"), is("bar")); + } + + @Property(key="foo", name="Foo", defaultValue = "bar") + public static class MyComponent { + + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/platform/ComponentContainerTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/platform/ComponentContainerTest.java new file mode 100644 index 00000000000..f5a173598c3 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/platform/ComponentContainerTest.java @@ -0,0 +1,147 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 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.platform; + +import org.junit.Test; +import org.sonar.api.Property; +import org.sonar.api.config.PropertyDefinitions; + +import static junit.framework.Assert.assertTrue; +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; + +public class ComponentContainerTest { + + @Test + public void shouldRegisterItself() { + ComponentContainer container = new ComponentContainer(); + assertTrue(container.getComponentByType(ComponentContainer.class) == container); + } + + @Test + public void testStartAndStop() { + ComponentContainer container = new ComponentContainer(); + container.addSingleton(StartableComponent.class); + container.startComponents(); + + assertThat(container.getComponentByType(StartableComponent.class).started, is(true)); + assertThat(container.getComponentByType(StartableComponent.class).stopped, is(false)); + + container.stopComponents(); + assertThat(container.getComponentByType(StartableComponent.class).stopped, is(true)); + } + + @Test + public void testChild() { + ComponentContainer parent = new ComponentContainer(); + parent.startComponents(); + + ComponentContainer child = parent.createChild(); + child.addSingleton(StartableComponent.class); + child.startComponents(); + + assertTrue(child.getParent() == parent); + assertTrue(parent.getChild() == child); + assertTrue(child.getComponentByType(ComponentContainer.class) == child); + assertTrue(parent.getComponentByType(ComponentContainer.class) == parent); + assertThat(child.getComponentByType(StartableComponent.class), notNullValue()); + assertThat(parent.getComponentByType(StartableComponent.class), nullValue()); + + parent.stopComponents(); + } + + @Test + public void testRemoveChild() { + ComponentContainer parent = new ComponentContainer(); + parent.startComponents(); + + ComponentContainer child = parent.createChild(); + assertTrue(parent.getChild() == child); + + parent.removeChild(); + assertThat(parent.getChild(), nullValue()); + } + + @Test + public void shouldForwardStartAndStopToDescendants() { + ComponentContainer grandParent = new ComponentContainer(); + ComponentContainer parent = grandParent.createChild(); + ComponentContainer child = parent.createChild(); + child.addSingleton(StartableComponent.class); + + grandParent.startComponents(); + + StartableComponent component = child.getComponentByType(StartableComponent.class); + assertTrue(component.started); + + parent.stopComponents(); + assertTrue(component.stopped); + } + + @Test + public void shouldDeclareComponentProperties() { + ComponentContainer container = new ComponentContainer(); + container.addSingleton(ComponentWithProperty.class); + + PropertyDefinitions propertyDefinitions = container.getComponentByType(PropertyDefinitions.class); + assertThat(propertyDefinitions.getProperty("foo"), notNullValue()); + assertThat(propertyDefinitions.getProperty("foo").defaultValue(), is("bar")); + } + + @Test + public void shouldDeclareExtensionWithoutAddingIt() { + ComponentContainer container = new ComponentContainer(); + PluginMetadata plugin = mock(PluginMetadata.class); + container.declareExtension(plugin, ComponentWithProperty.class); + + PropertyDefinitions propertyDefinitions = container.getComponentByType(PropertyDefinitions.class); + assertThat(propertyDefinitions.getProperty("foo"), notNullValue()); + assertThat(container.getComponentByType(ComponentWithProperty.class), nullValue()); + } + + @Test + public void shouldDeclareExtensionWhenAdding() { + ComponentContainer container = new ComponentContainer(); + PluginMetadata plugin = mock(PluginMetadata.class); + container.addExtension(plugin, ComponentWithProperty.class); + + PropertyDefinitions propertyDefinitions = container.getComponentByType(PropertyDefinitions.class); + assertThat(propertyDefinitions.getProperty("foo"), notNullValue()); + assertThat(container.getComponentByType(ComponentWithProperty.class), notNullValue()); + } + + public static class StartableComponent { + public boolean started = false, stopped = false; + + public void start() { + started = true; + } + + public void stop() { + stopped = true; + } + } + + @Property(key = "foo", defaultValue = "bar", name = "Foo") + public static class ComponentWithProperty { + + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/HttpDownloaderTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/HttpDownloaderTest.java index e40cffe871b..257b52b90ab 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/HttpDownloaderTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/HttpDownloaderTest.java @@ -19,15 +19,14 @@ */ package org.sonar.api.utils; -import org.apache.commons.configuration.PropertiesConfiguration; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; -import org.apache.commons.lang.SystemUtils; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; import org.mortbay.jetty.testing.ServletTester; +import org.sonar.api.config.Settings; import org.sonar.api.platform.Server; import java.io.File; @@ -37,10 +36,8 @@ import java.util.Arrays; import java.util.Properties; import static org.hamcrest.Matchers.greaterThan; -import static org.hamcrest.Matchers.not; import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertThat; -import static org.junit.Assume.assumeThat; import static org.junit.internal.matchers.StringContains.containsString; import static org.mockito.Matchers.anyObject; import static org.mockito.Mockito.mock; @@ -161,20 +158,20 @@ public class HttpDownloaderTest { @Test public void downloadBytes() throws URISyntaxException { - byte[] bytes = new HttpDownloader().download(new URI(baseUrl)); + byte[] bytes = new HttpDownloader(new Settings()).download(new URI(baseUrl)); assertThat(bytes.length, greaterThan(10)); } @Test public void downloadPlainText() throws URISyntaxException { - String text = new HttpDownloader().downloadPlainText(new URI(baseUrl), "UTF-8"); + String text = new HttpDownloader(new Settings()).downloadPlainText(new URI(baseUrl), "UTF-8"); assertThat(text.length(), greaterThan(10)); } @Test(expected = SonarException.class) public void failIfServerDown() throws URISyntaxException { // I hope that the port 1 is not used ! - new HttpDownloader().download(new URI("http://localhost:1/unknown")); + new HttpDownloader(new Settings()).download(new URI("http://localhost:1/unknown")); } @Test @@ -184,7 +181,7 @@ public class HttpDownloaderTest { FileUtils.cleanDirectory(toDir); File toFile = new File(toDir, "downloadToFile.txt"); - new HttpDownloader().download(new URI(baseUrl), toFile); + new HttpDownloader(new Settings()).download(new URI(baseUrl), toFile); assertThat(toFile.exists(), is(true)); assertThat(toFile.length(), greaterThan(10l)); } @@ -198,7 +195,7 @@ public class HttpDownloaderTest { try { // I hope that the port 1 is not used ! - new HttpDownloader().download(new URI("http://localhost:1/unknown"), toFile); + new HttpDownloader(new Settings()).download(new URI("http://localhost:1/unknown"), toFile); } catch (SonarException e) { assertThat(toFile.exists(), is(false)); } @@ -209,7 +206,7 @@ public class HttpDownloaderTest { Server server = mock(Server.class); when(server.getVersion()).thenReturn("2.2"); - byte[] bytes = new HttpDownloader(server, new PropertiesConfiguration()).download(new URI(baseUrl)); + byte[] bytes = new HttpDownloader(server, new Settings()).download(new URI(baseUrl)); Properties props = new Properties(); props.load(IOUtils.toInputStream(new String(bytes))); assertThat(props.getProperty("agent"), is("Sonar 2.2")); @@ -217,7 +214,7 @@ public class HttpDownloaderTest { @Test public void followRedirect() throws URISyntaxException { - byte[] bytes = new HttpDownloader().download(new URI(baseUrl + "/redirect/")); + byte[] bytes = new HttpDownloader(new Settings()).download(new URI(baseUrl + "/redirect/")); assertThat(new String(bytes), containsString("count")); } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/IocContainerTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/IocContainerTest.java deleted file mode 100644 index abe3578c40c..00000000000 --- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/IocContainerTest.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2011 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.utils; - -import static junit.framework.Assert.assertTrue; -import static org.hamcrest.Matchers.nullValue; -import static org.hamcrest.core.IsNot.not; -import static org.junit.Assert.assertThat; -import org.junit.Test; -import org.picocontainer.MutablePicoContainer; - -public class IocContainerTest { - - @Test - public void injectContainerAsComponent() { - MutablePicoContainer container = IocContainer.buildPicoContainer(); - assertThat(container.getComponent(IocContainer.class), not(nullValue())); - - // only one instance - assertTrue(container.getComponent(IocContainer.class) == container.getComponent(IocContainer.class)); - } -} |