diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2011-05-27 14:58:26 +0200 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2011-05-27 14:58:26 +0200 |
commit | 6e3c6469ee84039c669a5040c8770a4e7e56edff (patch) | |
tree | 1be3abaf555da53624dfc756320c3dbf8a5738a0 /sonar-batch | |
parent | 220c27d45e9577c4c27dcc48a5219c7f15aad4e9 (diff) | |
download | sonarqube-6e3c6469ee84039c669a5040c8770a4e7e56edff.tar.gz sonarqube-6e3c6469ee84039c669a5040c8770a4e7e56edff.zip |
SONAR-2469 add unit tests
Diffstat (limited to 'sonar-batch')
3 files changed, 179 insertions, 67 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectExtensionInstaller.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectExtensionInstaller.java index 49debff0d19..194d2f022ed 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectExtensionInstaller.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectExtensionInstaller.java @@ -22,6 +22,7 @@ package org.sonar.batch.bootstrap; import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; import org.sonar.api.BatchComponent; +import org.sonar.api.CoreProperties; import org.sonar.api.ExtensionProvider; import org.sonar.api.Plugin; import org.sonar.api.batch.AbstractCoverageExtension; @@ -73,21 +74,25 @@ public final class ProjectExtensionInstaller implements BatchComponent { ExtensionUtils.isInstantiationStrategy(extension, InstantiationStrategy.PER_PROJECT) && !isDeactivatedCoverageExtension(extension, project, pluginKey) && !isMavenExtensionOnEmulatedMavenProject(extension, project)) { - module.addComponent(extension); return extension; } return null; } - boolean isMavenExtensionOnEmulatedMavenProject(Object extension, Project project) { + /** + * Special use-case: the extension point ProjectBuilder is used in a Maven environment to define some + * new sub-projects without pom. + * Example : C# plugin adds sub-projects at runtime, even if they are not defined in root pom. + */ + static boolean isMavenExtensionOnEmulatedMavenProject(Object extension, Project project) { return ExtensionUtils.isMavenExtensionOnly(extension) && project.getPom() == null; } /** * TODO this code is specific to Java projects and should be moved somewhere else */ - boolean isDeactivatedCoverageExtension(Object extension, Project project, String pluginKey) { + static boolean isDeactivatedCoverageExtension(Object extension, Project project, String pluginKey) { if (!ExtensionUtils.isType(extension, CoverageExtension.class)) { return false; } @@ -98,7 +103,7 @@ public final class ProjectExtensionInstaller implements BatchComponent { } if (StringUtils.equals(project.getLanguageKey(), Java.KEY)) { - String[] selectedPluginKeys = project.getConfiguration().getStringArray(AbstractCoverageExtension.PARAM_PLUGIN); + String[] selectedPluginKeys = project.getConfiguration().getStringArray(CoreProperties.CORE_COVERAGE_PLUGIN_PROPERTY); if (ArrayUtils.isEmpty(selectedPluginKeys)) { selectedPluginKeys = new String[]{AbstractCoverageExtension.DEFAULT_PLUGIN}; } diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchPluginRepositoryTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchPluginRepositoryTest.java index 00c914319cc..6e848f4278d 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchPluginRepositoryTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchPluginRepositoryTest.java @@ -88,7 +88,7 @@ public class BatchPluginRepositoryTest { private void prepareDownloader(ExtensionDownloader extensionDownloader, final String pluginKey, final String filename) { when(extensionDownloader.downloadExtension(argThat(new BaseMatcher<JpaPluginFile>() { public boolean matches(Object o) { - return o!=null && ((JpaPluginFile) o).getPluginKey().equals(pluginKey); + return o != null && ((JpaPluginFile) o).getPluginKey().equals(pluginKey); } public void describeTo(Description description) { @@ -96,66 +96,4 @@ public class BatchPluginRepositoryTest { } }))).thenReturn(FileUtils.toFile(getClass().getResource(filename))); } - -// @Test -// public void shouldRegisterBatchExtension() { -// MutablePicoContainer pico = IocContainer.buildPicoContainer(); -// pico.addComponent(new PropertiesConfiguration()); -// BatchPluginRepository repository = new BatchPluginRepository(); -// -// // check classes -// assertThat(repository.shouldRegisterExtension(pico, "foo", FakeBatchExtension.class), is(true)); -// assertThat(repository.shouldRegisterExtension(pico, "foo", FakeServerExtension.class), is(false)); -// assertThat(repository.shouldRegisterExtension(pico, "foo", String.class), is(false)); -// -// // check objects -// assertThat(repository.shouldRegisterExtension(pico, "foo", new FakeBatchExtension()), is(true)); -// assertThat(repository.shouldRegisterExtension(pico, "foo", new FakeServerExtension()), is(false)); -// assertThat(repository.shouldRegisterExtension(pico, "foo", "bar"), is(false)); -// } -// -// @Test -// public void shouldRegisterOnlyCoberturaExtensionByDefault() { -// BatchPluginRepository repository = new BatchPluginRepository(); -// PropertiesConfiguration conf = new PropertiesConfiguration(); -// assertThat(repository.shouldRegisterCoverageExtension("cobertura", newJavaProject(), conf), is(true)); -// assertThat(repository.shouldRegisterCoverageExtension("clover", newJavaProject(), conf), is(false)); -// } -// -// @Test -// public void shouldRegisterCustomCoverageExtension() { -// Configuration conf = new PropertiesConfiguration(); -// conf.setProperty(AbstractCoverageExtension.PARAM_PLUGIN, "clover,phpunit"); -// BatchPluginRepository repository = new BatchPluginRepository(); -// assertThat(repository.shouldRegisterCoverageExtension("cobertura", newJavaProject(), conf), is(false)); -// assertThat(repository.shouldRegisterCoverageExtension("clover", newJavaProject(), conf), is(true)); -// assertThat(repository.shouldRegisterCoverageExtension("phpunit", newJavaProject(), conf), is(true)); -// assertThat(repository.shouldRegisterCoverageExtension("other", newJavaProject(), conf), is(false)); -// } -// -// @Test -// public void shouldNotCheckCoverageExtensionsOnNonJavaProjects() { -// Configuration conf = new PropertiesConfiguration(); -// conf.setProperty(AbstractCoverageExtension.PARAM_PLUGIN, "cobertura"); -// BatchPluginRepository repository = new BatchPluginRepository(); -// -// assertThat(repository.shouldRegisterCoverageExtension("groovy", newGroovyProject(), conf), is(true)); -// assertThat(repository.shouldRegisterCoverageExtension("groovy", newJavaProject(), conf), is(false)); -// } -// -// private static Project newJavaProject() { -// return new Project("foo").setLanguageKey(Java.KEY).setAnalysisType(AnalysisType.DYNAMIC); -// } -// -// private static Project newGroovyProject() { -// return new Project("foo").setLanguageKey("grvy").setAnalysisType(AnalysisType.DYNAMIC); -// } -// -// public static class FakeBatchExtension implements BatchExtension { -// -// } -// -// public static class FakeServerExtension implements ServerExtension { -// -// } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/ProjectExtensionInstallerTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/ProjectExtensionInstallerTest.java new file mode 100644 index 00000000000..12915c409d9 --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/ProjectExtensionInstallerTest.java @@ -0,0 +1,169 @@ +/* + * 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.batch.bootstrap; + +import com.google.common.collect.Maps; +import org.apache.commons.configuration.Configuration; +import org.apache.commons.configuration.PropertiesConfiguration; +import org.apache.maven.project.MavenProject; +import org.junit.Test; +import org.sonar.api.*; +import org.sonar.api.batch.AbstractCoverageExtension; +import org.sonar.api.batch.CoverageExtension; +import org.sonar.api.batch.InstantiationStrategy; +import org.sonar.api.batch.SupportedEnvironment; +import org.sonar.api.resources.Java; +import org.sonar.api.resources.Project; +import org.sonar.batch.bootstrapper.EnvironmentInformation; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class ProjectExtensionInstallerTest { + + @Test + public void shouldBeMavenExtensionOnEmulatedMavenProject() { + Project mavenProject = new Project("foo").setPom(new MavenProject()); + Project otherProject = new Project("bar"); + assertThat(ProjectExtensionInstaller.isMavenExtensionOnEmulatedMavenProject(MavenService.class, mavenProject), is(false)); + assertThat(ProjectExtensionInstaller.isMavenExtensionOnEmulatedMavenProject(MavenService.class, otherProject), is(true)); + + // this service is not for Maven only + assertThat(ProjectExtensionInstaller.isMavenExtensionOnEmulatedMavenProject(BuildToolService.class, mavenProject), is(false)); + assertThat(ProjectExtensionInstaller.isMavenExtensionOnEmulatedMavenProject(BuildToolService.class, otherProject), is(false)); + } + + @Test + public void shouldInstallExtensionsWithProjectInstantiationStrategy() { + BatchPluginRepository pluginRepository = mock(BatchPluginRepository.class); + Map<String, Plugin> pluginsMap = Maps.newHashMap(); + pluginsMap.put("fooPlugin", new SonarPlugin() { + public List getExtensions() { + return Arrays.asList(BatchService.class, ProjectService.class, ServerService.class); + } + }); + when(pluginRepository.getPluginsByKey()).thenReturn(pluginsMap); + Module module = new FakeModule().init(); + ProjectExtensionInstaller installer = new ProjectExtensionInstaller(pluginRepository, new EnvironmentInformation("ant", "1.7")); + + installer.install(module, new Project("foo")); + + assertThat(module.getComponent(BatchService.class), nullValue()); + assertThat(module.getComponent(ProjectService.class), not(nullValue())); + assertThat(module.getComponent(ServerService.class), nullValue()); + } + + @Test + public void shouldNotInstallPluginsOnNonSupportedEnvironment() { + BatchPluginRepository pluginRepository = mock(BatchPluginRepository.class); + Map<String, Plugin> pluginsMap = Maps.newHashMap(); + pluginsMap.put("fooPlugin", new SonarPlugin() { + public List getExtensions() { + return Arrays.asList(MavenService.class, BuildToolService.class); + } + }); + when(pluginRepository.getPluginsByKey()).thenReturn(pluginsMap); + Module module = new FakeModule().init(); + ProjectExtensionInstaller installer = new ProjectExtensionInstaller(pluginRepository, new EnvironmentInformation("ant", "1.7")); + + installer.install(module, new Project("foo")); + + assertThat(module.getComponent(MavenService.class), nullValue()); + assertThat(module.getComponent(BuildToolService.class), not(nullValue())); + } + + + @Test + public void shouldRegisterOnlyCoberturaExtensionByDefault() { + assertThat(ProjectExtensionInstaller.isDeactivatedCoverageExtension(FakeCoverageExtension.class, newJavaProject(null), "cobertura"), is(false)); + assertThat(ProjectExtensionInstaller.isDeactivatedCoverageExtension(FakeCoverageExtension.class, newJavaProject(null), "clover"), is(true)); + } + + private static Project newJavaProject(Configuration conf) { + Project project = new Project("foo").setLanguageKey(Java.KEY).setAnalysisType(Project.AnalysisType.DYNAMIC); + project.setConfiguration(conf!=null ? conf : new PropertiesConfiguration()); + return project; + } + + private static Project newGroovyProject() { + return new Project("foo").setLanguageKey("grvy").setAnalysisType(Project.AnalysisType.DYNAMIC); + } + + @Test + public void shouldRegisterCustomCoverageExtension() { + Configuration conf = new PropertiesConfiguration(); + conf.setProperty(CoreProperties.CORE_COVERAGE_PLUGIN_PROPERTY, "clover,phpunit"); + + assertThat(ProjectExtensionInstaller.isDeactivatedCoverageExtension(FakeCoverageExtension.class, newJavaProject(conf), "cobertura"), is(true)); + assertThat(ProjectExtensionInstaller.isDeactivatedCoverageExtension(FakeCoverageExtension.class, newJavaProject(conf), "clover"), is(false)); + assertThat(ProjectExtensionInstaller.isDeactivatedCoverageExtension(FakeCoverageExtension.class, newJavaProject(conf), "phpunit"), is(false)); + assertThat(ProjectExtensionInstaller.isDeactivatedCoverageExtension(FakeCoverageExtension.class, newJavaProject(conf), "other"), is(true)); + } + + @Test + public void shouldNotCheckCoverageExtensionsOnNonJavaProjects() { + Configuration conf = new PropertiesConfiguration(); + conf.setProperty(CoreProperties.CORE_COVERAGE_PLUGIN_PROPERTY, "cobertura"); + + assertThat(ProjectExtensionInstaller.isDeactivatedCoverageExtension(FakeCoverageExtension.class, newGroovyProject(), "groovy"), is(false)); + assertThat(ProjectExtensionInstaller.isDeactivatedCoverageExtension(FakeCoverageExtension.class, newJavaProject(null), "groovy"), is(true)); + + } + + + @SupportedEnvironment("maven") + public static class MavenService implements BatchExtension { + + } + + @SupportedEnvironment({"maven", "ant", "gradle"}) + public static class BuildToolService implements BatchExtension { + + } + + @InstantiationStrategy(InstantiationStrategy.PER_BATCH) + public static class BatchService implements BatchExtension { + + } + + public static class ProjectService implements BatchExtension { + + } + + public static class ServerService implements ServerExtension { + + } + + public static class FakeCoverageExtension implements CoverageExtension { + + } + + public static class FakeModule extends Module { + @Override + protected void configure() { + } + } +} |