From 72eac35f15c5b373840c1d80e26b39b20b18128b Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Mon, 11 Feb 2013 10:33:54 +0100 Subject: Rename pico *Module classes to *Container to not confuse with Sonar Module concept --- .../src/main/java/org/sonar/batch/Batch.java | 12 +- .../sonar/batch/bootstrap/BootstrapContainer.java | 104 +++++++++++ .../org/sonar/batch/bootstrap/BootstrapModule.java | 104 ----------- .../java/org/sonar/batch/bootstrap/Container.java | 100 +++++++++++ .../sonar/batch/bootstrap/InspectionContainer.java | 129 ++++++++++++++ .../sonar/batch/bootstrap/InspectionModule.java | 129 -------------- .../java/org/sonar/batch/bootstrap/Module.java | 100 ----------- .../batch/bootstrap/TaskBootstrapContainer.java | 79 +++++++++ .../sonar/batch/bootstrap/TaskBootstrapModule.java | 79 --------- .../org/sonar/batch/bootstrap/TaskContainer.java | 192 +++++++++++++++++++++ .../java/org/sonar/batch/bootstrap/TaskModule.java | 192 --------------------- .../java/org/sonar/batch/bootstrapper/Batch.java | 8 +- .../java/org/sonar/batch/tasks/InspectionTask.java | 4 +- .../src/test/java/org/sonar/batch/BatchTest.java | 4 +- .../batch/bootstrap/BootstrapContainerTest.java | 85 +++++++++ .../sonar/batch/bootstrap/BootstrapModuleTest.java | 85 --------- .../org/sonar/batch/bootstrap/ContainerTest.java | 166 ++++++++++++++++++ .../batch/bootstrap/InspectionContainerTest.java | 68 ++++++++ .../batch/bootstrap/InspectionModuleTest.java | 68 -------- .../java/org/sonar/batch/bootstrap/ModuleTest.java | 166 ------------------ .../bootstrap/TaskBootstrapContainerTest.java | 56 ++++++ .../batch/bootstrap/TaskBootstrapModuleTest.java | 56 ------ .../sonar/batch/bootstrap/TaskContainerTest.java | 65 +++++++ .../org/sonar/batch/bootstrap/TaskModuleTest.java | 65 ------- 24 files changed, 1058 insertions(+), 1058 deletions(-) create mode 100644 sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapContainer.java delete mode 100644 sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapModule.java create mode 100644 sonar-batch/src/main/java/org/sonar/batch/bootstrap/Container.java create mode 100644 sonar-batch/src/main/java/org/sonar/batch/bootstrap/InspectionContainer.java delete mode 100644 sonar-batch/src/main/java/org/sonar/batch/bootstrap/InspectionModule.java delete mode 100644 sonar-batch/src/main/java/org/sonar/batch/bootstrap/Module.java create mode 100644 sonar-batch/src/main/java/org/sonar/batch/bootstrap/TaskBootstrapContainer.java delete mode 100644 sonar-batch/src/main/java/org/sonar/batch/bootstrap/TaskBootstrapModule.java create mode 100644 sonar-batch/src/main/java/org/sonar/batch/bootstrap/TaskContainer.java delete mode 100644 sonar-batch/src/main/java/org/sonar/batch/bootstrap/TaskModule.java create mode 100644 sonar-batch/src/test/java/org/sonar/batch/bootstrap/BootstrapContainerTest.java delete mode 100644 sonar-batch/src/test/java/org/sonar/batch/bootstrap/BootstrapModuleTest.java create mode 100644 sonar-batch/src/test/java/org/sonar/batch/bootstrap/ContainerTest.java create mode 100644 sonar-batch/src/test/java/org/sonar/batch/bootstrap/InspectionContainerTest.java delete mode 100644 sonar-batch/src/test/java/org/sonar/batch/bootstrap/InspectionModuleTest.java delete mode 100644 sonar-batch/src/test/java/org/sonar/batch/bootstrap/ModuleTest.java create mode 100644 sonar-batch/src/test/java/org/sonar/batch/bootstrap/TaskBootstrapContainerTest.java delete mode 100644 sonar-batch/src/test/java/org/sonar/batch/bootstrap/TaskBootstrapModuleTest.java create mode 100644 sonar-batch/src/test/java/org/sonar/batch/bootstrap/TaskContainerTest.java delete mode 100644 sonar-batch/src/test/java/org/sonar/batch/bootstrap/TaskModuleTest.java diff --git a/sonar-batch/src/main/java/org/sonar/batch/Batch.java b/sonar-batch/src/main/java/org/sonar/batch/Batch.java index 4e56e1f36b6..6ae437afeeb 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/Batch.java +++ b/sonar-batch/src/main/java/org/sonar/batch/Batch.java @@ -22,8 +22,8 @@ package org.sonar.batch; import org.apache.commons.configuration.Configuration; import org.apache.commons.lang.StringUtils; import org.sonar.api.batch.bootstrap.ProjectReactor; -import org.sonar.batch.bootstrap.BootstrapModule; -import org.sonar.batch.bootstrap.Module; +import org.sonar.batch.bootstrap.BootstrapContainer; +import org.sonar.batch.bootstrap.Container; import org.sonar.batch.bootstrapper.Reactor; import java.util.Iterator; @@ -35,10 +35,10 @@ import java.util.Properties; @Deprecated public final class Batch { - private Module bootstrapModule; + private Container bootstrapModule; public Batch(ProjectReactor reactor, Object... bootstrapperComponents) { - this.bootstrapModule = new BootstrapModule(reactor, bootstrapperComponents); + this.bootstrapModule = new BootstrapContainer(reactor, bootstrapperComponents); this.bootstrapModule.init(); } @@ -48,7 +48,7 @@ public final class Batch { @Deprecated public Batch(Configuration configuration, Object... bootstrapperComponents) {//NOSONAR configuration is not needed // because it's already included in ProjectDefinition. - this.bootstrapModule = new BootstrapModule(extractProjectReactor(bootstrapperComponents), bootstrapperComponents); + this.bootstrapModule = new BootstrapContainer(extractProjectReactor(bootstrapperComponents), bootstrapperComponents); this.bootstrapModule.init(); } @@ -97,7 +97,7 @@ public final class Batch { /** * for unit tests */ - Batch(Module bootstrapModule) { + Batch(Container bootstrapModule) { this.bootstrapModule = bootstrapModule; } diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapContainer.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapContainer.java new file mode 100644 index 00000000000..1c69c721c79 --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapContainer.java @@ -0,0 +1,104 @@ +/* + * 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.batch.bootstrap; + +import org.apache.commons.configuration.PropertiesConfiguration; +import org.sonar.api.batch.bootstrap.ProjectReactor; +import org.sonar.api.utils.HttpDownloader; +import org.sonar.api.utils.UriReader; +import org.sonar.batch.FakeMavenPluginExecutor; +import org.sonar.batch.MavenPluginExecutor; +import org.sonar.core.config.Logback; + +import javax.annotation.Nullable; + +/** + * Level 1 components + */ +public class BootstrapContainer extends Container { + + private Object[] boostrapperComponents; + private ProjectReactor reactor; + private GlobalBatchProperties globalProperties; + private String taskCommand; + + /** + * @deprecated Use {@link #BootstrapModule(GlobalBatchProperties, String, ProjectReactor, Object...)} + */ + @Deprecated + public BootstrapContainer(ProjectReactor reactor, Object... boostrapperComponents) { + this(new GlobalBatchProperties(), null, reactor, boostrapperComponents); + } + + public BootstrapContainer(GlobalBatchProperties globalProperties, @Nullable String taskCommand, @Nullable ProjectReactor reactor, + Object... boostrapperComponents) { + this.globalProperties = globalProperties; + this.taskCommand = taskCommand; + this.reactor = reactor; + this.boostrapperComponents = boostrapperComponents; + } + + @Override + protected void configure() { + container.addSingleton(globalProperties); + if (reactor != null) { + container.addSingleton(reactor); + } + container.addSingleton(new PropertiesConfiguration()); + container.addSingleton(BootstrapSettings.class); + container.addSingleton(ServerClient.class); + container.addSingleton(BatchSettings.class); + container.addSingleton(BatchPluginRepository.class); + container.addSingleton(ExtensionInstaller.class); + container.addSingleton(Logback.class); + container.addSingleton(ServerMetadata.class); + container.addSingleton(org.sonar.batch.ServerMetadata.class); + container.addSingleton(TempDirectories.class); + container.addSingleton(HttpDownloader.class); + container.addSingleton(UriReader.class); + container.addSingleton(PluginDownloader.class); + container.addPicoAdapter(new FileCacheProvider()); + for (Object component : boostrapperComponents) { + if (component != null) { + container.addSingleton(component); + } + } + if (!isMavenPluginExecutorRegistered()) { + container.addSingleton(FakeMavenPluginExecutor.class); + } + } + + boolean isMavenPluginExecutorRegistered() { + if (boostrapperComponents != null) { + for (Object component : boostrapperComponents) { + if (component instanceof Class && MavenPluginExecutor.class.isAssignableFrom((Class) component)) { + return true; + } + } + } + return false; + } + + @Override + protected void doStart() { + Container taskBootstrap = installChild(new TaskBootstrapContainer(taskCommand)); + taskBootstrap.start(); + } +} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapModule.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapModule.java deleted file mode 100644 index 9e4d54b201f..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapModule.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * 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.batch.bootstrap; - -import org.apache.commons.configuration.PropertiesConfiguration; -import org.sonar.api.batch.bootstrap.ProjectReactor; -import org.sonar.api.utils.HttpDownloader; -import org.sonar.api.utils.UriReader; -import org.sonar.batch.FakeMavenPluginExecutor; -import org.sonar.batch.MavenPluginExecutor; -import org.sonar.core.config.Logback; - -import javax.annotation.Nullable; - -/** - * Level 1 components - */ -public class BootstrapModule extends Module { - - private Object[] boostrapperComponents; - private ProjectReactor reactor; - private GlobalBatchProperties globalProperties; - private String taskCommand; - - /** - * @deprecated Use {@link #BootstrapModule(GlobalBatchProperties, String, ProjectReactor, Object...)} - */ - @Deprecated - public BootstrapModule(ProjectReactor reactor, Object... boostrapperComponents) { - this(new GlobalBatchProperties(), null, reactor, boostrapperComponents); - } - - public BootstrapModule(GlobalBatchProperties globalProperties, @Nullable String taskCommand, @Nullable ProjectReactor reactor, - Object... boostrapperComponents) { - this.globalProperties = globalProperties; - this.taskCommand = taskCommand; - this.reactor = reactor; - this.boostrapperComponents = boostrapperComponents; - } - - @Override - protected void configure() { - container.addSingleton(globalProperties); - if (reactor != null) { - container.addSingleton(reactor); - } - container.addSingleton(new PropertiesConfiguration()); - container.addSingleton(BootstrapSettings.class); - container.addSingleton(ServerClient.class); - container.addSingleton(BatchSettings.class); - container.addSingleton(BatchPluginRepository.class); - container.addSingleton(ExtensionInstaller.class); - container.addSingleton(Logback.class); - container.addSingleton(ServerMetadata.class); - container.addSingleton(org.sonar.batch.ServerMetadata.class); - container.addSingleton(TempDirectories.class); - container.addSingleton(HttpDownloader.class); - container.addSingleton(UriReader.class); - container.addSingleton(PluginDownloader.class); - container.addPicoAdapter(new FileCacheProvider()); - for (Object component : boostrapperComponents) { - if (component != null) { - container.addSingleton(component); - } - } - if (!isMavenPluginExecutorRegistered()) { - container.addSingleton(FakeMavenPluginExecutor.class); - } - } - - boolean isMavenPluginExecutorRegistered() { - if (boostrapperComponents != null) { - for (Object component : boostrapperComponents) { - if (component instanceof Class && MavenPluginExecutor.class.isAssignableFrom((Class) component)) { - return true; - } - } - } - return false; - } - - @Override - protected void doStart() { - Module taskBootstrap = installChild(new TaskBootstrapModule(taskCommand)); - taskBootstrap.start(); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/Container.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/Container.java new file mode 100644 index 00000000000..45d72ef4599 --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/Container.java @@ -0,0 +1,100 @@ +/* + * 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.batch.bootstrap; + +import org.sonar.api.platform.ComponentContainer; + +/** + * Container describes group of components - {@link #configure()}. + * Several containers can be grouped together - {@link #installChild(Container)}. + *

+ */ +public abstract class Container { + + protected ComponentContainer container; + + /** + * @return this + */ + public final void init() { + init(new ComponentContainer()); + } + + /** + * @return this + */ + public final void init(ComponentContainer container) { + this.container = container; + configure(); + } + + /** + * Installs container into new scope - see http://picocontainer.org/scopes.html + * + * @return installed module + */ + public final Container installChild(Container child) { + ComponentContainer childContainer = container.createChild(); + child.init(childContainer); + return child; + } + + public final void uninstallChild() { + container.removeChild(); + } + + /** + * @return this + */ + public final Container start() { + container.startComponents(); + doStart(); + return this; + } + + protected void doStart() { + // empty method to be overridden + } + + /** + * @return this + */ + public final Container stop() { + try { + doStop(); + container.stopComponents(); + container.removeChild(); + } catch (Exception e) { + // ignore + } + return this; + } + + protected void doStop() { + // empty method to be overridden + } + + /** + * Implementation of this method must not contain conditional logic and just should contain several invocations on + * {@link #container}. + */ + protected abstract void configure(); + +} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/InspectionContainer.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/InspectionContainer.java new file mode 100644 index 00000000000..83d16a37fe6 --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/InspectionContainer.java @@ -0,0 +1,129 @@ +/* + * 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.batch.bootstrap; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.sonar.api.batch.BatchExtensionDictionnary; +import org.sonar.api.batch.bootstrap.ProjectDefinition; +import org.sonar.api.profiles.RulesProfile; +import org.sonar.api.resources.Languages; +import org.sonar.api.resources.Project; +import org.sonar.batch.DefaultProfileLoader; +import org.sonar.batch.DefaultProjectClasspath; +import org.sonar.batch.DefaultProjectFileSystem2; +import org.sonar.batch.DefaultSensorContext; +import org.sonar.batch.DefaultTimeMachine; +import org.sonar.batch.ProfileProvider; +import org.sonar.batch.ProjectTree; +import org.sonar.batch.ResourceFilters; +import org.sonar.batch.ViolationFilters; +import org.sonar.batch.components.TimeMachineConfiguration; +import org.sonar.batch.events.EventBus; +import org.sonar.batch.index.DefaultIndex; +import org.sonar.batch.index.ResourcePersister; +import org.sonar.batch.local.DryRunExporter; +import org.sonar.batch.phases.Phases; +import org.sonar.batch.phases.PhasesTimeProfiler; +import org.sonar.core.qualitymodel.DefaultModelFinder; +import org.sonar.jpa.dao.ProfilesDao; +import org.sonar.jpa.dao.RulesDao; + +public class InspectionContainer extends Container { + private static final Logger LOG = LoggerFactory.getLogger(InspectionContainer.class); + private Project project; + + public InspectionContainer(Project project) { + this.project = project; + } + + @Override + protected void configure() { + logSettings(); + addCoreComponents(); + addPluginExtensions(); + } + + private void addCoreComponents() { + ProjectDefinition projectDefinition = container.getComponentByType(ProjectTree.class).getProjectDefinition(project); + container.addSingleton(projectDefinition); + container.addSingleton(project.getConfiguration()); + container.addSingleton(project); + container.addSingleton(ProjectSettings.class); + + // hack to initialize commons-configuration before ExtensionProviders + container.getComponentByType(ProjectSettings.class); + + container.addSingleton(EventBus.class); + container.addSingleton(Phases.class); + container.addSingleton(PhasesTimeProfiler.class); + for (Class clazz : Phases.getPhaseClasses()) { + container.addSingleton(clazz); + } + container.addSingleton(UnsupportedProperties.class); + + for (Object component : projectDefinition.getContainerExtensions()) { + container.addSingleton(component); + } + container.addSingleton(Languages.class); + container.addSingleton(DefaultProjectClasspath.class); + container.addSingleton(DefaultProjectFileSystem2.class); + container.addSingleton(RulesDao.class); + + // the Snapshot component will be removed when asynchronous measures are improved (required for AsynchronousMeasureSensor) + container.addSingleton(container.getComponentByType(ResourcePersister.class).getSnapshot(project)); + + container.addSingleton(TimeMachineConfiguration.class); + container.addSingleton(org.sonar.api.database.daos.MeasuresDao.class); + container.addSingleton(ProfilesDao.class); + container.addSingleton(DefaultSensorContext.class); + container.addSingleton(BatchExtensionDictionnary.class); + container.addSingleton(DefaultTimeMachine.class); + container.addSingleton(ViolationFilters.class); + container.addSingleton(ResourceFilters.class); + container.addSingleton(DefaultModelFinder.class); + container.addSingleton(DefaultProfileLoader.class); + container.addSingleton(DryRunExporter.class); + container.addPicoAdapter(new ProfileProvider()); + } + + private void addPluginExtensions() { + ExtensionInstaller installer = container.getComponentByType(ExtensionInstaller.class); + installer.installInspectionExtensions(container); + } + + private void logSettings() { + LOG.info("------------- Inspecting {}", project.getName()); + } + + /** + * Analyze project + */ + @Override + protected void doStart() { + DefaultIndex index = container.getComponentByType(DefaultIndex.class); + index.setCurrentProject(project, + container.getComponentByType(ResourceFilters.class), + container.getComponentByType(ViolationFilters.class), + container.getComponentByType(RulesProfile.class)); + + container.getComponentByType(Phases.class).execute(project); + } +} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/InspectionModule.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/InspectionModule.java deleted file mode 100644 index bd91ce3d85e..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/InspectionModule.java +++ /dev/null @@ -1,129 +0,0 @@ -/* - * 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.batch.bootstrap; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.batch.BatchExtensionDictionnary; -import org.sonar.api.batch.bootstrap.ProjectDefinition; -import org.sonar.api.profiles.RulesProfile; -import org.sonar.api.resources.Languages; -import org.sonar.api.resources.Project; -import org.sonar.batch.DefaultProfileLoader; -import org.sonar.batch.DefaultProjectClasspath; -import org.sonar.batch.DefaultProjectFileSystem2; -import org.sonar.batch.DefaultSensorContext; -import org.sonar.batch.DefaultTimeMachine; -import org.sonar.batch.ProfileProvider; -import org.sonar.batch.ProjectTree; -import org.sonar.batch.ResourceFilters; -import org.sonar.batch.ViolationFilters; -import org.sonar.batch.components.TimeMachineConfiguration; -import org.sonar.batch.events.EventBus; -import org.sonar.batch.index.DefaultIndex; -import org.sonar.batch.index.ResourcePersister; -import org.sonar.batch.local.DryRunExporter; -import org.sonar.batch.phases.Phases; -import org.sonar.batch.phases.PhasesTimeProfiler; -import org.sonar.core.qualitymodel.DefaultModelFinder; -import org.sonar.jpa.dao.ProfilesDao; -import org.sonar.jpa.dao.RulesDao; - -public class InspectionModule extends Module { - private static final Logger LOG = LoggerFactory.getLogger(InspectionModule.class); - private Project project; - - public InspectionModule(Project project) { - this.project = project; - } - - @Override - protected void configure() { - logSettings(); - addCoreComponents(); - addPluginExtensions(); - } - - private void addCoreComponents() { - ProjectDefinition projectDefinition = container.getComponentByType(ProjectTree.class).getProjectDefinition(project); - container.addSingleton(projectDefinition); - container.addSingleton(project.getConfiguration()); - container.addSingleton(project); - container.addSingleton(ProjectSettings.class); - - // hack to initialize commons-configuration before ExtensionProviders - container.getComponentByType(ProjectSettings.class); - - container.addSingleton(EventBus.class); - container.addSingleton(Phases.class); - container.addSingleton(PhasesTimeProfiler.class); - for (Class clazz : Phases.getPhaseClasses()) { - container.addSingleton(clazz); - } - container.addSingleton(UnsupportedProperties.class); - - for (Object component : projectDefinition.getContainerExtensions()) { - container.addSingleton(component); - } - container.addSingleton(Languages.class); - container.addSingleton(DefaultProjectClasspath.class); - container.addSingleton(DefaultProjectFileSystem2.class); - container.addSingleton(RulesDao.class); - - // the Snapshot component will be removed when asynchronous measures are improved (required for AsynchronousMeasureSensor) - container.addSingleton(container.getComponentByType(ResourcePersister.class).getSnapshot(project)); - - container.addSingleton(TimeMachineConfiguration.class); - container.addSingleton(org.sonar.api.database.daos.MeasuresDao.class); - container.addSingleton(ProfilesDao.class); - container.addSingleton(DefaultSensorContext.class); - container.addSingleton(BatchExtensionDictionnary.class); - container.addSingleton(DefaultTimeMachine.class); - container.addSingleton(ViolationFilters.class); - container.addSingleton(ResourceFilters.class); - container.addSingleton(DefaultModelFinder.class); - container.addSingleton(DefaultProfileLoader.class); - container.addSingleton(DryRunExporter.class); - container.addPicoAdapter(new ProfileProvider()); - } - - private void addPluginExtensions() { - ExtensionInstaller installer = container.getComponentByType(ExtensionInstaller.class); - installer.installInspectionExtensions(container); - } - - private void logSettings() { - LOG.info("------------- Inspecting {}", project.getName()); - } - - /** - * Analyze project - */ - @Override - protected void doStart() { - DefaultIndex index = container.getComponentByType(DefaultIndex.class); - index.setCurrentProject(project, - container.getComponentByType(ResourceFilters.class), - container.getComponentByType(ViolationFilters.class), - container.getComponentByType(RulesProfile.class)); - - container.getComponentByType(Phases.class).execute(project); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/Module.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/Module.java deleted file mode 100644 index 39398129bdd..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/Module.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * 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.batch.bootstrap; - -import org.sonar.api.platform.ComponentContainer; - -/** - * Module describes group of components - {@link #configure()}. - * Several modules can be grouped together - {@link #installChild(Module)}. - *

- */ -public abstract class Module { - - protected ComponentContainer container; - - /** - * @return this - */ - public final void init() { - init(new ComponentContainer()); - } - - /** - * @return this - */ - public final void init(ComponentContainer container) { - this.container = container; - configure(); - } - - /** - * Installs module into new scope - see http://picocontainer.org/scopes.html - * - * @return installed module - */ - public final Module installChild(Module child) { - ComponentContainer childContainer = container.createChild(); - child.init(childContainer); - return child; - } - - public final void uninstallChild() { - container.removeChild(); - } - - /** - * @return this - */ - public final Module start() { - container.startComponents(); - doStart(); - return this; - } - - protected void doStart() { - // empty method to be overridden - } - - /** - * @return this - */ - public final Module stop() { - try { - doStop(); - container.stopComponents(); - container.removeChild(); - } catch (Exception e) { - // ignore - } - return this; - } - - protected void doStop() { - // empty method to be overridden - } - - /** - * Implementation of this method must not contain conditional logic and just should contain several invocations on - * {@link #container}. - */ - protected abstract void configure(); - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TaskBootstrapContainer.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TaskBootstrapContainer.java new file mode 100644 index 00000000000..6504eeee35d --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TaskBootstrapContainer.java @@ -0,0 +1,79 @@ +/* + * 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.batch.bootstrap; + +import org.sonar.api.batch.bootstrap.ProjectReactor; +import org.sonar.api.task.TaskDefinition; +import org.sonar.api.utils.SonarException; +import org.sonar.batch.tasks.InspectionTask; +import org.sonar.batch.tasks.ListTasksTask; +import org.sonar.batch.tasks.Tasks; + +import javax.annotation.Nullable; + +/** + * Level-2 components. Collect tasks definitions. + */ +public class TaskBootstrapContainer extends Container { + + private String taskCommand; + + public TaskBootstrapContainer(@Nullable String taskCommand) { + this.taskCommand = taskCommand; + } + + @Override + protected void configure() { + registerCoreTaskDefinitions(); + registerTaskDefinitionExtensions(); + container.addSingleton(Tasks.class); + } + + private void registerCoreTaskDefinitions() { + container.addSingleton(InspectionTask.DEFINITION); + container.addSingleton(ListTasksTask.DEFINITION); + } + + private void registerTaskDefinitionExtensions() { + ExtensionInstaller installer = container.getComponentByType(ExtensionInstaller.class); + installer.installTaskDefinitionExtensions(container); + } + + @Override + protected void doStart() { + Tasks tasks = container.getComponentByType(Tasks.class); + executeTask(tasks.getTaskDefinition(taskCommand)); + } + + private void executeTask(TaskDefinition taskDefinition) { + boolean projectPresent = container.getComponentByType(ProjectReactor.class) != null; + if (ExtensionUtils.requiresProject(taskDefinition.getTask()) && !projectPresent) { + throw new SonarException("Task " + taskDefinition.getName() + " requires to be run on a project"); + } + Container childModule = new TaskContainer(taskDefinition, projectPresent); + try { + installChild(childModule); + childModule.start(); + } finally { + childModule.stop(); + uninstallChild(); + } + } +} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TaskBootstrapModule.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TaskBootstrapModule.java deleted file mode 100644 index 5db5fcf3454..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TaskBootstrapModule.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * 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.batch.bootstrap; - -import org.sonar.api.batch.bootstrap.ProjectReactor; -import org.sonar.api.task.TaskDefinition; -import org.sonar.api.utils.SonarException; -import org.sonar.batch.tasks.InspectionTask; -import org.sonar.batch.tasks.ListTasksTask; -import org.sonar.batch.tasks.Tasks; - -import javax.annotation.Nullable; - -/** - * Level-2 components. Collect tasks definitions. - */ -public class TaskBootstrapModule extends Module { - - private String taskCommand; - - public TaskBootstrapModule(@Nullable String taskCommand) { - this.taskCommand = taskCommand; - } - - @Override - protected void configure() { - registerCoreTaskDefinitions(); - registerTaskDefinitionExtensions(); - container.addSingleton(Tasks.class); - } - - private void registerCoreTaskDefinitions() { - container.addSingleton(InspectionTask.DEFINITION); - container.addSingleton(ListTasksTask.DEFINITION); - } - - private void registerTaskDefinitionExtensions() { - ExtensionInstaller installer = container.getComponentByType(ExtensionInstaller.class); - installer.installTaskDefinitionExtensions(container); - } - - @Override - protected void doStart() { - Tasks tasks = container.getComponentByType(Tasks.class); - executeTask(tasks.getTaskDefinition(taskCommand)); - } - - private void executeTask(TaskDefinition taskDefinition) { - boolean projectPresent = container.getComponentByType(ProjectReactor.class) != null; - if (ExtensionUtils.requiresProject(taskDefinition.getTask()) && !projectPresent) { - throw new SonarException("Task " + taskDefinition.getName() + " requires to be run on a project"); - } - Module childModule = new TaskModule(taskDefinition, projectPresent); - try { - installChild(childModule); - childModule.start(); - } finally { - childModule.stop(); - uninstallChild(); - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TaskContainer.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TaskContainer.java new file mode 100644 index 00000000000..3b754d15b34 --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TaskContainer.java @@ -0,0 +1,192 @@ +/* + * 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.batch.bootstrap; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.sonar.api.config.EmailSettings; +import org.sonar.api.resources.ResourceTypes; +import org.sonar.api.task.Task; +import org.sonar.api.task.TaskDefinition; +import org.sonar.api.utils.SonarException; +import org.sonar.batch.DefaultFileLinesContextFactory; +import org.sonar.batch.DefaultResourceCreationLock; +import org.sonar.batch.ProjectConfigurator; +import org.sonar.batch.ProjectTree; +import org.sonar.batch.components.PastMeasuresLoader; +import org.sonar.batch.components.PastSnapshotFinder; +import org.sonar.batch.components.PastSnapshotFinderByDate; +import org.sonar.batch.components.PastSnapshotFinderByDays; +import org.sonar.batch.components.PastSnapshotFinderByPreviousAnalysis; +import org.sonar.batch.components.PastSnapshotFinderByPreviousVersion; +import org.sonar.batch.components.PastSnapshotFinderByVersion; +import org.sonar.batch.index.DefaultIndex; +import org.sonar.batch.index.DefaultPersistenceManager; +import org.sonar.batch.index.DefaultResourcePersister; +import org.sonar.batch.index.DependencyPersister; +import org.sonar.batch.index.EventPersister; +import org.sonar.batch.index.LinkPersister; +import org.sonar.batch.index.MeasurePersister; +import org.sonar.batch.index.MemoryOptimizer; +import org.sonar.batch.index.SourcePersister; +import org.sonar.batch.tasks.InspectionTask; +import org.sonar.batch.tasks.ListTasksTask; +import org.sonar.core.component.ScanGraph; +import org.sonar.core.component.ScanGraphStore; +import org.sonar.core.component.ScanPerspectives; +import org.sonar.core.i18n.I18nManager; +import org.sonar.core.i18n.RuleI18nManager; +import org.sonar.core.metric.CacheMetricFinder; +import org.sonar.core.notification.DefaultNotificationManager; +import org.sonar.core.persistence.DaoUtils; +import org.sonar.core.persistence.DatabaseVersion; +import org.sonar.core.persistence.MyBatis; +import org.sonar.core.persistence.SemaphoresImpl; +import org.sonar.core.resource.DefaultResourcePermissions; +import org.sonar.core.rule.CacheRuleFinder; +import org.sonar.core.test.TestPlanBuilder; +import org.sonar.core.test.TestableBuilder; +import org.sonar.core.user.DefaultUserFinder; +import org.sonar.jpa.dao.MeasuresDao; +import org.sonar.jpa.session.DefaultDatabaseConnector; +import org.sonar.jpa.session.JpaDatabaseSession; + +/** + * Level-3 components. Task-level components that don't depends on project. + */ +public class TaskContainer extends Container { + + private static final Logger LOG = LoggerFactory.getLogger(TaskContainer.class); + + private TaskDefinition taskDefinition; + private boolean projectPresent; + + public TaskContainer(TaskDefinition task, boolean projectPresent) { + this.taskDefinition = task; + this.projectPresent = projectPresent; + } + + @Override + protected void configure() { + logSettings(); + registerCoreComponents(); + registerDatabaseComponents(); + registerCoreTasks(); + if (projectPresent) { + registerCoreComponentsRequiringProject(); + } + registerTaskExtensions(); + } + + private void registerCoreComponents() { + container.addSingleton(EmailSettings.class); + container.addSingleton(I18nManager.class); + container.addSingleton(RuleI18nManager.class); + container.addSingleton(MeasuresDao.class); + container.addSingleton(CacheRuleFinder.class); + container.addSingleton(CacheMetricFinder.class); + container.addSingleton(DefaultUserFinder.class); + container.addSingleton(ResourceTypes.class); + container.addSingleton(SemaphoresImpl.class); + container.addSingleton(PastSnapshotFinderByDate.class); + container.addSingleton(PastSnapshotFinderByDays.class); + container.addSingleton(PastSnapshotFinderByPreviousAnalysis.class); + container.addSingleton(PastSnapshotFinderByVersion.class); + container.addSingleton(PastSnapshotFinderByPreviousVersion.class); + container.addSingleton(PastMeasuresLoader.class); + container.addSingleton(PastSnapshotFinder.class); + } + + private void registerDatabaseComponents() { + container.addSingleton(JdbcDriverHolder.class); + container.addSingleton(BatchDatabase.class); + container.addSingleton(MyBatis.class); + container.addSingleton(DatabaseVersion.class); + container.addSingleton(DatabaseCompatibility.class); + for (Class daoClass : DaoUtils.getDaoClasses()) { + container.addSingleton(daoClass); + } + + // hibernate + container.addSingleton(DefaultDatabaseConnector.class); + container.addSingleton(JpaDatabaseSession.class); + container.addSingleton(BatchDatabaseSessionFactory.class); + } + + private void registerCoreTasks() { + container.addSingleton(ListTasksTask.class); + if (projectPresent) { + container.addSingleton(InspectionTask.class); + } + } + + private void registerTaskExtensions() { + ExtensionInstaller installer = container.getComponentByType(ExtensionInstaller.class); + installer.installTaskExtensions(container, projectPresent); + } + + private void registerCoreComponentsRequiringProject() { + container.addSingleton(DefaultResourceCreationLock.class); + container.addSingleton(DefaultPersistenceManager.class); + container.addSingleton(DependencyPersister.class); + container.addSingleton(EventPersister.class); + container.addSingleton(LinkPersister.class); + container.addSingleton(MeasurePersister.class); + container.addSingleton(MemoryOptimizer.class); + container.addSingleton(DefaultResourcePermissions.class); + container.addSingleton(DefaultResourcePersister.class); + container.addSingleton(SourcePersister.class); + container.addSingleton(DefaultNotificationManager.class); + container.addSingleton(MetricProvider.class); + container.addSingleton(ProjectExclusions.class); + container.addSingleton(ProjectReactorReady.class); + container.addSingleton(ProjectTree.class); + container.addSingleton(ProjectConfigurator.class); + container.addSingleton(DefaultIndex.class); + container.addSingleton(DefaultFileLinesContextFactory.class); + container.addSingleton(ProjectLock.class); + container.addSingleton(DryRunDatabase.class); + + // graphs + container.addSingleton(ScanGraph.create()); + container.addSingleton(TestPlanBuilder.class); + container.addSingleton(TestableBuilder.class); + container.addSingleton(ScanPerspectives.class); + container.addSingleton(ScanGraphStore.class); + } + + private void logSettings() { + LOG.info("------------- Executing {}", taskDefinition.getName()); + } + + /** + * Execute task + */ + @Override + protected void doStart() { + Task task = container.getComponentByType(taskDefinition.getTask()); + if (task != null) { + task.execute(); + } else { + throw new SonarException("Extension " + taskDefinition.getTask() + " was not found in declared extensions."); + } + } + +} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TaskModule.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TaskModule.java deleted file mode 100644 index 28d31500a82..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TaskModule.java +++ /dev/null @@ -1,192 +0,0 @@ -/* - * 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.batch.bootstrap; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.config.EmailSettings; -import org.sonar.api.resources.ResourceTypes; -import org.sonar.api.task.Task; -import org.sonar.api.task.TaskDefinition; -import org.sonar.api.utils.SonarException; -import org.sonar.batch.DefaultFileLinesContextFactory; -import org.sonar.batch.DefaultResourceCreationLock; -import org.sonar.batch.ProjectConfigurator; -import org.sonar.batch.ProjectTree; -import org.sonar.batch.components.PastMeasuresLoader; -import org.sonar.batch.components.PastSnapshotFinder; -import org.sonar.batch.components.PastSnapshotFinderByDate; -import org.sonar.batch.components.PastSnapshotFinderByDays; -import org.sonar.batch.components.PastSnapshotFinderByPreviousAnalysis; -import org.sonar.batch.components.PastSnapshotFinderByPreviousVersion; -import org.sonar.batch.components.PastSnapshotFinderByVersion; -import org.sonar.batch.index.DefaultIndex; -import org.sonar.batch.index.DefaultPersistenceManager; -import org.sonar.batch.index.DefaultResourcePersister; -import org.sonar.batch.index.DependencyPersister; -import org.sonar.batch.index.EventPersister; -import org.sonar.batch.index.LinkPersister; -import org.sonar.batch.index.MeasurePersister; -import org.sonar.batch.index.MemoryOptimizer; -import org.sonar.batch.index.SourcePersister; -import org.sonar.batch.tasks.InspectionTask; -import org.sonar.batch.tasks.ListTasksTask; -import org.sonar.core.component.ScanGraph; -import org.sonar.core.component.ScanGraphStore; -import org.sonar.core.component.ScanPerspectives; -import org.sonar.core.i18n.I18nManager; -import org.sonar.core.i18n.RuleI18nManager; -import org.sonar.core.metric.CacheMetricFinder; -import org.sonar.core.notification.DefaultNotificationManager; -import org.sonar.core.persistence.DaoUtils; -import org.sonar.core.persistence.DatabaseVersion; -import org.sonar.core.persistence.MyBatis; -import org.sonar.core.persistence.SemaphoresImpl; -import org.sonar.core.resource.DefaultResourcePermissions; -import org.sonar.core.rule.CacheRuleFinder; -import org.sonar.core.test.TestPlanBuilder; -import org.sonar.core.test.TestableBuilder; -import org.sonar.core.user.DefaultUserFinder; -import org.sonar.jpa.dao.MeasuresDao; -import org.sonar.jpa.session.DefaultDatabaseConnector; -import org.sonar.jpa.session.JpaDatabaseSession; - -/** - * Level-3 components. Task-level components that don't depends on project. - */ -public class TaskModule extends Module { - - private static final Logger LOG = LoggerFactory.getLogger(TaskModule.class); - - private TaskDefinition taskDefinition; - private boolean projectPresent; - - public TaskModule(TaskDefinition task, boolean projectPresent) { - this.taskDefinition = task; - this.projectPresent = projectPresent; - } - - @Override - protected void configure() { - logSettings(); - registerCoreComponents(); - registerDatabaseComponents(); - registerCoreTasks(); - if (projectPresent) { - registerCoreComponentsRequiringProject(); - } - registerTaskExtensions(); - } - - private void registerCoreComponents() { - container.addSingleton(EmailSettings.class); - container.addSingleton(I18nManager.class); - container.addSingleton(RuleI18nManager.class); - container.addSingleton(MeasuresDao.class); - container.addSingleton(CacheRuleFinder.class); - container.addSingleton(CacheMetricFinder.class); - container.addSingleton(DefaultUserFinder.class); - container.addSingleton(ResourceTypes.class); - container.addSingleton(SemaphoresImpl.class); - container.addSingleton(PastSnapshotFinderByDate.class); - container.addSingleton(PastSnapshotFinderByDays.class); - container.addSingleton(PastSnapshotFinderByPreviousAnalysis.class); - container.addSingleton(PastSnapshotFinderByVersion.class); - container.addSingleton(PastSnapshotFinderByPreviousVersion.class); - container.addSingleton(PastMeasuresLoader.class); - container.addSingleton(PastSnapshotFinder.class); - } - - private void registerDatabaseComponents() { - container.addSingleton(JdbcDriverHolder.class); - container.addSingleton(BatchDatabase.class); - container.addSingleton(MyBatis.class); - container.addSingleton(DatabaseVersion.class); - container.addSingleton(DatabaseCompatibility.class); - for (Class daoClass : DaoUtils.getDaoClasses()) { - container.addSingleton(daoClass); - } - - // hibernate - container.addSingleton(DefaultDatabaseConnector.class); - container.addSingleton(JpaDatabaseSession.class); - container.addSingleton(BatchDatabaseSessionFactory.class); - } - - private void registerCoreTasks() { - container.addSingleton(ListTasksTask.class); - if (projectPresent) { - container.addSingleton(InspectionTask.class); - } - } - - private void registerTaskExtensions() { - ExtensionInstaller installer = container.getComponentByType(ExtensionInstaller.class); - installer.installTaskExtensions(container, projectPresent); - } - - private void registerCoreComponentsRequiringProject() { - container.addSingleton(DefaultResourceCreationLock.class); - container.addSingleton(DefaultPersistenceManager.class); - container.addSingleton(DependencyPersister.class); - container.addSingleton(EventPersister.class); - container.addSingleton(LinkPersister.class); - container.addSingleton(MeasurePersister.class); - container.addSingleton(MemoryOptimizer.class); - container.addSingleton(DefaultResourcePermissions.class); - container.addSingleton(DefaultResourcePersister.class); - container.addSingleton(SourcePersister.class); - container.addSingleton(DefaultNotificationManager.class); - container.addSingleton(MetricProvider.class); - container.addSingleton(ProjectExclusions.class); - container.addSingleton(ProjectReactorReady.class); - container.addSingleton(ProjectTree.class); - container.addSingleton(ProjectConfigurator.class); - container.addSingleton(DefaultIndex.class); - container.addSingleton(DefaultFileLinesContextFactory.class); - container.addSingleton(ProjectLock.class); - container.addSingleton(DryRunDatabase.class); - - // graphs - container.addSingleton(ScanGraph.create()); - container.addSingleton(TestPlanBuilder.class); - container.addSingleton(TestableBuilder.class); - container.addSingleton(ScanPerspectives.class); - container.addSingleton(ScanGraphStore.class); - } - - private void logSettings() { - LOG.info("------------- Executing {}", taskDefinition.getName()); - } - - /** - * Execute task - */ - @Override - protected void doStart() { - Task task = container.getComponentByType(taskDefinition.getTask()); - if (task != null) { - task.execute(); - } else { - throw new SonarException("Extension " + taskDefinition.getTask() + " was not found in declared extensions."); - } - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/Batch.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/Batch.java index cbab3ddb416..36283edec49 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/Batch.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/Batch.java @@ -23,9 +23,9 @@ import com.google.common.collect.Lists; import com.google.common.collect.Maps; import org.slf4j.LoggerFactory; import org.sonar.api.batch.bootstrap.ProjectReactor; -import org.sonar.batch.bootstrap.BootstrapModule; +import org.sonar.batch.bootstrap.BootstrapContainer; import org.sonar.batch.bootstrap.GlobalBatchProperties; -import org.sonar.batch.bootstrap.Module; +import org.sonar.batch.bootstrap.Container; import org.sonar.core.PicoUtils; import java.util.Collections; @@ -80,9 +80,9 @@ public final class Batch { } private void startBatch() { - Module bootstrapModule = null; + Container bootstrapModule = null; try { - bootstrapModule = new BootstrapModule(new GlobalBatchProperties(globalProperties), taskCommand, + bootstrapModule = new BootstrapContainer(new GlobalBatchProperties(globalProperties), taskCommand, projectReactor, components.toArray(new Object[components.size()])); bootstrapModule.init(); bootstrapModule.start(); diff --git a/sonar-batch/src/main/java/org/sonar/batch/tasks/InspectionTask.java b/sonar-batch/src/main/java/org/sonar/batch/tasks/InspectionTask.java index a98a960a50b..438e55450e8 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/tasks/InspectionTask.java +++ b/sonar-batch/src/main/java/org/sonar/batch/tasks/InspectionTask.java @@ -19,7 +19,7 @@ */ package org.sonar.batch.tasks; -import org.sonar.batch.bootstrap.InspectionModule; +import org.sonar.batch.bootstrap.InspectionContainer; import org.sonar.api.platform.ComponentContainer; import org.sonar.api.resources.Project; @@ -54,7 +54,7 @@ public class InspectionTask implements Task { analyze(subProject); } - InspectionModule projectModule = new InspectionModule(project); + InspectionContainer projectModule = new InspectionContainer(project); try { ComponentContainer childContainer = container.createChild(); projectModule.init(childContainer); diff --git a/sonar-batch/src/test/java/org/sonar/batch/BatchTest.java b/sonar-batch/src/test/java/org/sonar/batch/BatchTest.java index 8931f08e437..bbcd8365b6a 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/BatchTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/BatchTest.java @@ -21,7 +21,7 @@ package org.sonar.batch; import org.apache.commons.configuration.PropertiesConfiguration; import org.junit.Test; -import org.sonar.batch.bootstrap.Module; +import org.sonar.batch.bootstrap.Container; import java.util.Properties; @@ -40,7 +40,7 @@ public class BatchTest { assertThat(module.stopped, is(true)); } - public static class FakeModule extends Module { + public static class FakeModule extends Container { private boolean started=false; private boolean stopped=false; diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BootstrapContainerTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BootstrapContainerTest.java new file mode 100644 index 00000000000..069840eaf86 --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BootstrapContainerTest.java @@ -0,0 +1,85 @@ +/* + * 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.batch.bootstrap; + +import org.junit.Test; +import org.sonar.api.batch.bootstrap.ProjectDefinition; +import org.sonar.api.batch.bootstrap.ProjectReactor; +import org.sonar.api.batch.maven.MavenPluginHandler; +import org.sonar.api.resources.Project; +import org.sonar.batch.FakeMavenPluginExecutor; +import org.sonar.batch.MavenPluginExecutor; + +import static org.fest.assertions.Assertions.assertThat; + + +public class BootstrapContainerTest { + + private ProjectReactor reactor = new ProjectReactor(ProjectDefinition.create()); + + @Test + public void should_register_fake_maven_executor_if_not_maven_env() { + BootstrapContainer module = new BootstrapContainer(reactor, null, MyMavenPluginExecutor.class); + module.init(); + + assertThat(module.isMavenPluginExecutorRegistered()).isTrue(); + assertThat(module.container.getComponentByType(MavenPluginExecutor.class)).isInstanceOf(MyMavenPluginExecutor.class); + } + + @Test + public void should_use_plugin_executor_provided_by_maven() { + BootstrapContainer module = new BootstrapContainer(reactor); + module.init(); + assertThat(module.isMavenPluginExecutorRegistered()).isFalse(); + assertThat(module.container.getComponentByType(MavenPluginExecutor.class)).isInstanceOf(FakeMavenPluginExecutor.class); + } + + @Test + public void should_register_bootstrap_components() { + BootstrapContainer module = new BootstrapContainer(reactor, new FakeComponent()); + module.init(); + + assertThat(module.container).isNotNull(); + assertThat(module.container.getComponentByType(FakeComponent.class)).isNotNull(); + assertThat(module.container.getComponentByType(ProjectReactor.class)).isSameAs(reactor); + } + + @Test + public void should_not_fail_if_no_bootstrap_components() { + BootstrapContainer module = new BootstrapContainer(reactor); + module.init(); + + assertThat(module.container).isNotNull(); + assertThat(module.container.getComponentByType(ProjectReactor.class)).isSameAs(reactor); + } + + public static class FakeComponent { + + } + + public static class MyMavenPluginExecutor implements MavenPluginExecutor { + public void execute(Project project, ProjectDefinition projectDef, String goal) { + } + + public MavenPluginHandler execute(Project project, ProjectDefinition projectDef, MavenPluginHandler handler) { + return handler; + } + } +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BootstrapModuleTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BootstrapModuleTest.java deleted file mode 100644 index 2b57de9bd40..00000000000 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BootstrapModuleTest.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * 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.batch.bootstrap; - -import org.junit.Test; -import org.sonar.api.batch.bootstrap.ProjectDefinition; -import org.sonar.api.batch.bootstrap.ProjectReactor; -import org.sonar.api.batch.maven.MavenPluginHandler; -import org.sonar.api.resources.Project; -import org.sonar.batch.FakeMavenPluginExecutor; -import org.sonar.batch.MavenPluginExecutor; - -import static org.fest.assertions.Assertions.assertThat; - - -public class BootstrapModuleTest { - - private ProjectReactor reactor = new ProjectReactor(ProjectDefinition.create()); - - @Test - public void should_register_fake_maven_executor_if_not_maven_env() { - BootstrapModule module = new BootstrapModule(reactor, null, MyMavenPluginExecutor.class); - module.init(); - - assertThat(module.isMavenPluginExecutorRegistered()).isTrue(); - assertThat(module.container.getComponentByType(MavenPluginExecutor.class)).isInstanceOf(MyMavenPluginExecutor.class); - } - - @Test - public void should_use_plugin_executor_provided_by_maven() { - BootstrapModule module = new BootstrapModule(reactor); - module.init(); - assertThat(module.isMavenPluginExecutorRegistered()).isFalse(); - assertThat(module.container.getComponentByType(MavenPluginExecutor.class)).isInstanceOf(FakeMavenPluginExecutor.class); - } - - @Test - public void should_register_bootstrap_components() { - BootstrapModule module = new BootstrapModule(reactor, new FakeComponent()); - module.init(); - - assertThat(module.container).isNotNull(); - assertThat(module.container.getComponentByType(FakeComponent.class)).isNotNull(); - assertThat(module.container.getComponentByType(ProjectReactor.class)).isSameAs(reactor); - } - - @Test - public void should_not_fail_if_no_bootstrap_components() { - BootstrapModule module = new BootstrapModule(reactor); - module.init(); - - assertThat(module.container).isNotNull(); - assertThat(module.container.getComponentByType(ProjectReactor.class)).isSameAs(reactor); - } - - public static class FakeComponent { - - } - - public static class MyMavenPluginExecutor implements MavenPluginExecutor { - public void execute(Project project, ProjectDefinition projectDef, String goal) { - } - - public MavenPluginHandler execute(Project project, ProjectDefinition projectDef, MavenPluginHandler handler) { - return handler; - } - } -} diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/ContainerTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/ContainerTest.java new file mode 100644 index 00000000000..d19a17675e0 --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/ContainerTest.java @@ -0,0 +1,166 @@ +/* + * 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.batch.bootstrap; + +import org.hamcrest.Matchers; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.nullValue; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; + +public class ContainerTest { + + @Test + public void shouldInitModule() { + Container module = new FakeModule(FakeService.class); + module.init(); + + FakeService service = module.container.getComponentByType(FakeService.class); + assertThat(service, not(nullValue())); + assertThat(service.started, is(false)); + assertThat(module.container, notNullValue()); + } + + @Test + public void shouldStartAndStopModule() { + Container module = new FakeModule(FakeService.class); + module.init(); + module.start(); + + FakeService service = module.container.getComponentByType(FakeService.class); + assertThat(service.started, is(true)); + + module.stop(); + assertThat(service.started, is(false)); + } + + @Test(expected = RuntimeException.class) + public void shouldNotIgnoreStartFailures() { + Container module = new FakeModule(NonStartableService.class); + module.init(); + module.start(); + } + + @Test + public void shouldIgnoreStopFailures() { + Container module = new FakeModule(NonStoppableService.class); + module.init(); + module.start(); + module.stop(); // no exception is raised + } + + @Test + public void componentsShouldBeSingletons() { + Container module = new FakeModule(FakeService.class); + module.init(); + + assertThat(module.container.getComponentByType(FakeService.class) == module.container.getComponentByType(FakeService.class), is(true)); + } + + @Test + public void shouldInstallChildModule() { + Container parent = new FakeModule(FakeService.class); + parent.init(); + parent.start(); + + Container child = parent.installChild(new FakeModule(ChildService.class)); + + assertThat(parent.container.getComponentByType(ChildService.class), Matchers.nullValue());// child not accessible from parent + assertThat(child.container.getComponentByType(FakeService.class), not(nullValue())); + assertThat(child.container.getComponentByType(ChildService.class).started, is(false)); + assertThat(child.container.getComponentByType(ChildService.class).dependency, not(nullValue())); + + child.start(); + assertThat(child.container.getComponentByType(ChildService.class).started, is(true)); + + child.stop(); + assertThat(child.container.getComponentByType(ChildService.class).started, is(false)); + } + + public static class FakeModule extends Container { + private Class[] components; + + public FakeModule(Class... components) { + this.components = components; + } + + @Override + protected void configure() { + for (Class component : components) { + container.addSingleton(component); + } + } + + @Override + public boolean equals(Object obj) { + return false; + } + + @Override + public int hashCode() { + return 42; + } + } + + public static class FakeService { + boolean started = false; + + public void start() { + this.started = true; + } + + public void stop() { + this.started = false; + } + } + + public static class ChildService { + private FakeService dependency; + private boolean started = false; + + public ChildService(FakeService dependency) { + this.dependency = dependency; + } + + public void start() { + this.started = true; + } + + public void stop() { + this.started = false; + } + } + + public static class NonStoppableService { + public void stop() { + throw new IllegalStateException("Can not stop !"); + } + } + + public static class NonStartableService { + public void start() { + throw new IllegalStateException("Can not start !"); + } + } + +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/InspectionContainerTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/InspectionContainerTest.java new file mode 100644 index 00000000000..bf10a117242 --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/InspectionContainerTest.java @@ -0,0 +1,68 @@ +/* + * 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.batch.bootstrap; + +import org.apache.commons.configuration.PropertiesConfiguration; +import org.junit.Test; +import org.mockito.Matchers; +import org.sonar.api.batch.bootstrap.ProjectDefinition; +import org.sonar.api.database.model.Snapshot; +import org.sonar.api.platform.ComponentContainer; +import org.sonar.api.resources.Project; +import org.sonar.api.resources.Resource; +import org.sonar.batch.ProjectTree; +import org.sonar.batch.index.ResourcePersister; + +import static org.fest.assertions.Assertions.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class InspectionContainerTest { + @Test + public void should_register_project_extensions() { + // components injected in the parent container + final Project project = new Project("foo"); + project.setConfiguration(new PropertiesConfiguration()); + final ProjectTree projectTree = mock(ProjectTree.class); + when(projectTree.getProjectDefinition(project)).thenReturn(ProjectDefinition.create()); + final ResourcePersister resourcePersister = mock(ResourcePersister.class); + when(resourcePersister.getSnapshot(Matchers. any())).thenReturn(new Snapshot()); + + final ExtensionInstaller extensionInstaller = mock(ExtensionInstaller.class); + Container batchModule = new Container() { + @Override + protected void configure() { + container.addSingleton(extensionInstaller); + container.addSingleton(projectTree); + container.addSingleton(resourcePersister); + container.addSingleton(new BatchSettings()); + } + }; + + batchModule.init(); + InspectionContainer projectModule = new InspectionContainer(project); + batchModule.installChild(projectModule); + + verify(extensionInstaller).installInspectionExtensions(any(ComponentContainer.class)); + assertThat(projectModule.container.getComponentByType(ProjectSettings.class)).isNotNull(); + } +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/InspectionModuleTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/InspectionModuleTest.java deleted file mode 100644 index 6b8d3481580..00000000000 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/InspectionModuleTest.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * 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.batch.bootstrap; - -import org.apache.commons.configuration.PropertiesConfiguration; -import org.junit.Test; -import org.mockito.Matchers; -import org.sonar.api.batch.bootstrap.ProjectDefinition; -import org.sonar.api.database.model.Snapshot; -import org.sonar.api.platform.ComponentContainer; -import org.sonar.api.resources.Project; -import org.sonar.api.resources.Resource; -import org.sonar.batch.ProjectTree; -import org.sonar.batch.index.ResourcePersister; - -import static org.fest.assertions.Assertions.assertThat; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -public class InspectionModuleTest { - @Test - public void should_register_project_extensions() { - // components injected in the parent container - final Project project = new Project("foo"); - project.setConfiguration(new PropertiesConfiguration()); - final ProjectTree projectTree = mock(ProjectTree.class); - when(projectTree.getProjectDefinition(project)).thenReturn(ProjectDefinition.create()); - final ResourcePersister resourcePersister = mock(ResourcePersister.class); - when(resourcePersister.getSnapshot(Matchers. any())).thenReturn(new Snapshot()); - - final ExtensionInstaller extensionInstaller = mock(ExtensionInstaller.class); - Module batchModule = new Module() { - @Override - protected void configure() { - container.addSingleton(extensionInstaller); - container.addSingleton(projectTree); - container.addSingleton(resourcePersister); - container.addSingleton(new BatchSettings()); - } - }; - - batchModule.init(); - InspectionModule projectModule = new InspectionModule(project); - batchModule.installChild(projectModule); - - verify(extensionInstaller).installInspectionExtensions(any(ComponentContainer.class)); - assertThat(projectModule.container.getComponentByType(ProjectSettings.class)).isNotNull(); - } -} diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/ModuleTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/ModuleTest.java deleted file mode 100644 index 67b8921f685..00000000000 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/ModuleTest.java +++ /dev/null @@ -1,166 +0,0 @@ -/* - * 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.batch.bootstrap; - -import org.hamcrest.Matchers; -import org.junit.Test; - -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.hamcrest.Matchers.not; -import static org.hamcrest.Matchers.nullValue; -import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; - -public class ModuleTest { - - @Test - public void shouldInitModule() { - Module module = new FakeModule(FakeService.class); - module.init(); - - FakeService service = module.container.getComponentByType(FakeService.class); - assertThat(service, not(nullValue())); - assertThat(service.started, is(false)); - assertThat(module.container, notNullValue()); - } - - @Test - public void shouldStartAndStopModule() { - Module module = new FakeModule(FakeService.class); - module.init(); - module.start(); - - FakeService service = module.container.getComponentByType(FakeService.class); - assertThat(service.started, is(true)); - - module.stop(); - assertThat(service.started, is(false)); - } - - @Test(expected = RuntimeException.class) - public void shouldNotIgnoreStartFailures() { - Module module = new FakeModule(NonStartableService.class); - module.init(); - module.start(); - } - - @Test - public void shouldIgnoreStopFailures() { - Module module = new FakeModule(NonStoppableService.class); - module.init(); - module.start(); - module.stop(); // no exception is raised - } - - @Test - public void componentsShouldBeSingletons() { - Module module = new FakeModule(FakeService.class); - module.init(); - - assertThat(module.container.getComponentByType(FakeService.class) == module.container.getComponentByType(FakeService.class), is(true)); - } - - @Test - public void shouldInstallChildModule() { - Module parent = new FakeModule(FakeService.class); - parent.init(); - parent.start(); - - Module child = parent.installChild(new FakeModule(ChildService.class)); - - assertThat(parent.container.getComponentByType(ChildService.class), Matchers.nullValue());// child not accessible from parent - assertThat(child.container.getComponentByType(FakeService.class), not(nullValue())); - assertThat(child.container.getComponentByType(ChildService.class).started, is(false)); - assertThat(child.container.getComponentByType(ChildService.class).dependency, not(nullValue())); - - child.start(); - assertThat(child.container.getComponentByType(ChildService.class).started, is(true)); - - child.stop(); - assertThat(child.container.getComponentByType(ChildService.class).started, is(false)); - } - - public static class FakeModule extends Module { - private Class[] components; - - public FakeModule(Class... components) { - this.components = components; - } - - @Override - protected void configure() { - for (Class component : components) { - container.addSingleton(component); - } - } - - @Override - public boolean equals(Object obj) { - return false; - } - - @Override - public int hashCode() { - return 42; - } - } - - public static class FakeService { - boolean started = false; - - public void start() { - this.started = true; - } - - public void stop() { - this.started = false; - } - } - - public static class ChildService { - private FakeService dependency; - private boolean started = false; - - public ChildService(FakeService dependency) { - this.dependency = dependency; - } - - public void start() { - this.started = true; - } - - public void stop() { - this.started = false; - } - } - - public static class NonStoppableService { - public void stop() { - throw new IllegalStateException("Can not stop !"); - } - } - - public static class NonStartableService { - public void start() { - throw new IllegalStateException("Can not start !"); - } - } - -} diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/TaskBootstrapContainerTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/TaskBootstrapContainerTest.java new file mode 100644 index 00000000000..9145caa5436 --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/TaskBootstrapContainerTest.java @@ -0,0 +1,56 @@ +/* + * 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.batch.bootstrap; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.config.Settings; +import org.sonar.api.utils.SonarException; + +import static org.mockito.Mockito.mock; + +public class TaskBootstrapContainerTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void should_throw_when_no_project_and_task_require_project() { + final ExtensionInstaller extensionInstaller = mock(ExtensionInstaller.class); + Container bootstrapModule = new Container() { + @Override + protected void configure() { + // used to install project extensions + container.addSingleton(extensionInstaller); + container.addSingleton(Settings.class); + } + }; + bootstrapModule.init(); + TaskBootstrapContainer module = new TaskBootstrapContainer("inspect"); + bootstrapModule.installChild(module); + + thrown.expect(SonarException.class); + thrown.expectMessage("Task Sonar project inspection requires to be run on a project"); + + module.start(); + } + +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/TaskBootstrapModuleTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/TaskBootstrapModuleTest.java deleted file mode 100644 index c47f0774a6a..00000000000 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/TaskBootstrapModuleTest.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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.batch.bootstrap; - -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.sonar.api.config.Settings; -import org.sonar.api.utils.SonarException; - -import static org.mockito.Mockito.mock; - -public class TaskBootstrapModuleTest { - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @Test - public void should_throw_when_no_project_and_task_require_project() { - final ExtensionInstaller extensionInstaller = mock(ExtensionInstaller.class); - Module bootstrapModule = new Module() { - @Override - protected void configure() { - // used to install project extensions - container.addSingleton(extensionInstaller); - container.addSingleton(Settings.class); - } - }; - bootstrapModule.init(); - TaskBootstrapModule module = new TaskBootstrapModule("inspect"); - bootstrapModule.installChild(module); - - thrown.expect(SonarException.class); - thrown.expectMessage("Task Sonar project inspection requires to be run on a project"); - - module.start(); - } - -} diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/TaskContainerTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/TaskContainerTest.java new file mode 100644 index 00000000000..03944ea0d77 --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/TaskContainerTest.java @@ -0,0 +1,65 @@ +/* + * 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.batch.bootstrap; + +import org.junit.Test; +import org.sonar.api.platform.ComponentContainer; +import org.sonar.api.task.TaskDefinition; + +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +public class TaskContainerTest { + @Test + public void should_register_task_extensions_when_project_present() { + final ExtensionInstaller extensionInstaller = mock(ExtensionInstaller.class); + Container bootstrapModule = new Container() { + @Override + protected void configure() { + // used to install project extensions + container.addSingleton(extensionInstaller); + } + }; + bootstrapModule.init(); + TaskContainer module = new TaskContainer(TaskDefinition.create(), true); + bootstrapModule.installChild(module); + + verify(extensionInstaller).installTaskExtensions(any(ComponentContainer.class), eq(true)); + } + + @Test + public void should_register_task_extensions_when_no_project() { + final ExtensionInstaller extensionInstaller = mock(ExtensionInstaller.class); + Container bootstrapModule = new Container() { + @Override + protected void configure() { + // used to install project extensions + container.addSingleton(extensionInstaller); + } + }; + bootstrapModule.init(); + TaskContainer module = new TaskContainer(TaskDefinition.create(), false); + bootstrapModule.installChild(module); + + verify(extensionInstaller).installTaskExtensions(any(ComponentContainer.class), eq(false)); + } +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/TaskModuleTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/TaskModuleTest.java deleted file mode 100644 index e5b7f49b035..00000000000 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/TaskModuleTest.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * 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.batch.bootstrap; - -import org.junit.Test; -import org.sonar.api.platform.ComponentContainer; -import org.sonar.api.task.TaskDefinition; - -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; - -public class TaskModuleTest { - @Test - public void should_register_task_extensions_when_project_present() { - final ExtensionInstaller extensionInstaller = mock(ExtensionInstaller.class); - Module bootstrapModule = new Module() { - @Override - protected void configure() { - // used to install project extensions - container.addSingleton(extensionInstaller); - } - }; - bootstrapModule.init(); - TaskModule module = new TaskModule(TaskDefinition.create(), true); - bootstrapModule.installChild(module); - - verify(extensionInstaller).installTaskExtensions(any(ComponentContainer.class), eq(true)); - } - - @Test - public void should_register_task_extensions_when_no_project() { - final ExtensionInstaller extensionInstaller = mock(ExtensionInstaller.class); - Module bootstrapModule = new Module() { - @Override - protected void configure() { - // used to install project extensions - container.addSingleton(extensionInstaller); - } - }; - bootstrapModule.init(); - TaskModule module = new TaskModule(TaskDefinition.create(), false); - bootstrapModule.installChild(module); - - verify(extensionInstaller).installTaskExtensions(any(ComponentContainer.class), eq(false)); - } -} -- cgit v1.2.3