diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2013-01-16 17:53:57 +0100 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2013-01-16 17:53:57 +0100 |
commit | af572a416fdc8d2a4de4c9162224cf568a56f1e3 (patch) | |
tree | 7f1d3594b42215b99344656c173148c2a39282eb | |
parent | 3182ee0ca57db767c614fa66deb4e82f6d4699be (diff) | |
download | sonarqube-af572a416fdc8d2a4de4c9162224cf568a56f1e3.tar.gz sonarqube-af572a416fdc8d2a4de4c9162224cf568a56f1e3.zip |
SONAR-4069 Small improvements on new task feature
* Simon feedback
* Add additional JUnit tests
20 files changed, 290 insertions, 234 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/AbstractTaskModule.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/AbstractTaskModule.java index 37f6ad15a8f..c433bb84f2d 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/AbstractTaskModule.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/AbstractTaskModule.java @@ -21,10 +21,10 @@ package org.sonar.batch.bootstrap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.sonar.api.batch.TaskDefinition; -import org.sonar.api.batch.TaskExecutor; 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.DefaultResourceCreationLock; import org.sonar.batch.components.PastMeasuresLoader; @@ -42,7 +42,7 @@ 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.ListTaskExecutor; +import org.sonar.batch.tasks.ListTasksTask; import org.sonar.core.i18n.I18nManager; import org.sonar.core.i18n.RuleI18nManager; import org.sonar.core.metric.CacheMetricFinder; @@ -65,11 +65,11 @@ public abstract class AbstractTaskModule extends Module { private static final Logger LOG = LoggerFactory.getLogger(AbstractTaskModule.class); - private TaskDefinition task; + private TaskDefinition taskDefinition; private boolean projectPresent; public AbstractTaskModule(TaskDefinition task, boolean projectPresent) { - this.task = task; + this.taskDefinition = task; this.projectPresent = projectPresent; } @@ -132,7 +132,7 @@ public abstract class AbstractTaskModule extends Module { } private void registerCoreTasks() { - container.addSingleton(ListTaskExecutor.class); + container.addSingleton(ListTasksTask.class); } private void registerTaskExtensions() { @@ -141,7 +141,7 @@ public abstract class AbstractTaskModule extends Module { } private void logSettings() { - LOG.info("------------- Executing {}", task.getTaskDescriptor().getName()); + LOG.info("------------- Executing {}", taskDefinition.getName()); } /** @@ -149,12 +149,12 @@ public abstract class AbstractTaskModule extends Module { */ @Override protected void doStart() { - TaskExecutor taskExecutor = container.getComponentByType(task.getExecutor()); - if (taskExecutor != null) { - taskExecutor.execute(); + Task task = container.getComponentByType(taskDefinition.getTask()); + if (task != null) { + task.execute(); } else { - throw new SonarException("Extension " + task.getExecutor() + " was not found in declared extensions."); + throw new SonarException("Extension " + taskDefinition.getTask() + " was not found in declared extensions."); } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ExtensionInstaller.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ExtensionInstaller.java index f34fe6276b7..3a54b6d2164 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ExtensionInstaller.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ExtensionInstaller.java @@ -137,7 +137,7 @@ public class ExtensionInstaller implements BatchComponent { boolean installTaskExtension(ComponentContainer container, @Nullable PluginMetadata plugin, Object extension, boolean projectPresent) { boolean installed; if (ExtensionUtils.isTaskExtension(extension) && - (projectPresent || !ExtensionUtils.requireProject(extension)) && + (projectPresent || !ExtensionUtils.requiresProject(extension)) && ExtensionUtils.supportsEnvironment(extension, environment)) { if (plugin != null) { LOG.debug("Installing task extension {} from plugin {}", extension.toString(), plugin.getKey()); diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ExtensionUtils.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ExtensionUtils.java index 40fa0286aca..aaf50e54f0a 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ExtensionUtils.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ExtensionUtils.java @@ -19,8 +19,6 @@ */ package org.sonar.batch.bootstrap; -import org.sonar.api.batch.RequiresProject; - import org.apache.commons.lang.StringUtils; import org.sonar.api.BatchExtension; import org.sonar.api.Extension; @@ -28,6 +26,7 @@ import org.sonar.api.TaskDefinitionExtension; import org.sonar.api.TaskExtension; import org.sonar.api.batch.InstantiationStrategy; import org.sonar.api.batch.SupportedEnvironment; +import org.sonar.api.task.RequiresProject; import org.sonar.api.utils.AnnotationUtils; import org.sonar.batch.bootstrapper.EnvironmentInformation; import org.sonar.core.DryRunIncompatible; @@ -75,8 +74,8 @@ final class ExtensionUtils { return AnnotationUtils.getAnnotation(extension, DryRunIncompatible.class) == null; } - static boolean requireProject(Object extension) { - return AnnotationUtils.getAnnotation(extension, RequiresProject.class) == null; + static boolean requiresProject(Object extension) { + return AnnotationUtils.getAnnotation(extension, RequiresProject.class) != null; } static boolean isMavenExtensionOnly(Object extension) { diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectLessTaskModule.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectLessTaskModule.java index 603a39fa185..49c95063e77 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectLessTaskModule.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectLessTaskModule.java @@ -19,7 +19,7 @@ */ package org.sonar.batch.bootstrap; -import org.sonar.api.batch.TaskDefinition; +import org.sonar.api.task.TaskDefinition; public class ProjectLessTaskModule extends AbstractTaskModule { diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectTaskModule.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectTaskModule.java index 3025491d452..ae540020ce6 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectTaskModule.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectTaskModule.java @@ -19,12 +19,13 @@ */ package org.sonar.batch.bootstrap; -import org.sonar.api.batch.TaskDefinition; +import org.sonar.api.task.TaskDefinition; + import org.sonar.batch.DefaultFileLinesContextFactory; import org.sonar.batch.ProjectConfigurator; import org.sonar.batch.ProjectTree; import org.sonar.batch.index.DefaultIndex; -import org.sonar.batch.tasks.InspectionTaskExecutor; +import org.sonar.batch.tasks.InspectionTask; public class ProjectTaskModule extends AbstractTaskModule { @@ -52,7 +53,7 @@ public class ProjectTaskModule extends AbstractTaskModule { } private void registerCoreTasksRequiringProject() { - container.addSingleton(InspectionTaskExecutor.class); + container.addSingleton(InspectionTask.class); } } 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 index d4489222d3e..de57549c961 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TaskBootstrapModule.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TaskBootstrapModule.java @@ -19,13 +19,12 @@ */ package org.sonar.batch.bootstrap; -import org.apache.commons.lang.StringUtils; -import org.sonar.api.batch.TaskDefinition; import org.sonar.api.batch.bootstrap.ProjectReactor; +import org.sonar.api.task.TaskDefinition; import org.sonar.api.utils.SonarException; -import org.sonar.batch.tasks.InspectionTaskDefinition; -import org.sonar.batch.tasks.ListTaskDefinition; -import org.sonar.batch.tasks.TaskManager; +import org.sonar.batch.tasks.InspectionTask; +import org.sonar.batch.tasks.ListTasksTask; +import org.sonar.batch.tasks.Tasks; /** * Level-2 components. Collect tasks definitions. @@ -42,12 +41,12 @@ public class TaskBootstrapModule extends Module { protected void configure() { registerCoreTaskDefinitions(); registerTaskDefinitionExtensions(); - container.addSingleton(TaskManager.class); + container.addSingleton(Tasks.class); } private void registerCoreTaskDefinitions() { - container.addSingleton(InspectionTaskDefinition.class); - container.addSingleton(ListTaskDefinition.class); + container.addSingleton(InspectionTask.DEFINITION); + container.addSingleton(ListTasksTask.DEFINITION); } private void registerTaskDefinitionExtensions() { @@ -57,22 +56,21 @@ public class TaskBootstrapModule extends Module { @Override protected void doStart() { - String command = StringUtils.isNotBlank(taskCommand) ? taskCommand : InspectionTaskDefinition.COMMAND; - TaskManager manager = container.getComponentByType(TaskManager.class); - executeTask(manager.getTask(command)); + Tasks tasks = container.getComponentByType(Tasks.class); + executeTask(tasks.getTaskDefinition(taskCommand)); } - private void executeTask(TaskDefinition task) { + private void executeTask(TaskDefinition taskDefinition) { boolean projectPresent = container.getComponentByType(ProjectReactor.class) != null; - if (task.getTaskDescriptor().isRequiresProject() && !projectPresent) { - throw new SonarException("Task " + task.getTaskDescriptor().getName() + " requires to be run on a project"); + if (ExtensionUtils.requiresProject(taskDefinition.getTask()) && !projectPresent) { + throw new SonarException("Task " + taskDefinition.getName() + " requires to be run on a project"); } Module childModule; if (projectPresent) { - childModule = new ProjectTaskModule(task); + childModule = new ProjectTaskModule(taskDefinition); } else { - childModule = new ProjectLessTaskModule(task); + childModule = new ProjectLessTaskModule(taskDefinition); } try { installChild(childModule); diff --git a/sonar-batch/src/main/java/org/sonar/batch/tasks/InspectionTaskExecutor.java b/sonar-batch/src/main/java/org/sonar/batch/tasks/InspectionTask.java index 9fcb221dccf..bc08ec3f9b4 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/tasks/InspectionTaskExecutor.java +++ b/sonar-batch/src/main/java/org/sonar/batch/tasks/InspectionTask.java @@ -19,19 +19,27 @@ */ package org.sonar.batch.tasks; -import org.sonar.api.batch.RequiresProject; -import org.sonar.api.batch.TaskExecutor; import org.sonar.api.platform.ComponentContainer; import org.sonar.api.resources.Project; +import org.sonar.api.task.RequiresProject; +import org.sonar.api.task.Task; +import org.sonar.api.task.TaskDefinition; import org.sonar.batch.ProjectTree; @RequiresProject -public class InspectionTaskExecutor implements TaskExecutor { +public class InspectionTask implements Task { + + public static final String COMMAND = "inspect"; + public static final TaskDefinition DEFINITION = TaskDefinition.create() + .setDescription("Start a Sonar inspection of a project") + .setName("Sonar project inspection") + .setCommand(COMMAND) + .setTask(InspectionTask.class); private final ComponentContainer container; private final ProjectTree projectTree; - public InspectionTaskExecutor(ProjectTree projectTree, ComponentContainer container) { + public InspectionTask(ProjectTree projectTree, ComponentContainer container) { this.container = container; this.projectTree = projectTree; } diff --git a/sonar-batch/src/main/java/org/sonar/batch/tasks/InspectionTaskDefinition.java b/sonar-batch/src/main/java/org/sonar/batch/tasks/InspectionTaskDefinition.java deleted file mode 100644 index 530a9d8c33c..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/tasks/InspectionTaskDefinition.java +++ /dev/null @@ -1,42 +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.tasks; - -import org.sonar.api.batch.TaskDefinition; -import org.sonar.api.batch.TaskDescriptor; -import org.sonar.api.batch.TaskExecutor; - -public class InspectionTaskDefinition implements TaskDefinition { - - public static final String COMMAND = "inspect"; - - public TaskDescriptor getTaskDescriptor() { - return TaskDescriptor.create() - .setDescription("Start a Sonar inspection of a project") - .setName("Sonar project inspection") - .setCommand(COMMAND) - .setRequiresProject(true); - } - - public Class<? extends TaskExecutor> getExecutor() { - return InspectionTaskExecutor.class; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/tasks/ListTaskDefinition.java b/sonar-batch/src/main/java/org/sonar/batch/tasks/ListTaskDefinition.java deleted file mode 100644 index 8e71f18eec5..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/tasks/ListTaskDefinition.java +++ /dev/null @@ -1,42 +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.tasks; - -import org.sonar.api.batch.TaskDefinition; -import org.sonar.api.batch.TaskDescriptor; -import org.sonar.api.batch.TaskExecutor; - -public class ListTaskDefinition implements TaskDefinition { - - public static final String COMMAND = "list-tasks"; - - public TaskDescriptor getTaskDescriptor() { - return TaskDescriptor.create() - .setDescription("List all available tasks on the Sonar server") - .setName("List tasks") - .setCommand(COMMAND) - .setRequiresProject(false); - } - - public Class<? extends TaskExecutor> getExecutor() { - return ListTaskExecutor.class; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/tasks/ListTaskExecutor.java b/sonar-batch/src/main/java/org/sonar/batch/tasks/ListTasksTask.java index 428c8ea8f2b..74f961a577d 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/tasks/ListTaskExecutor.java +++ b/sonar-batch/src/main/java/org/sonar/batch/tasks/ListTasksTask.java @@ -19,22 +19,28 @@ */ package org.sonar.batch.tasks; -import org.sonar.api.batch.TaskDefinition; -import org.sonar.api.batch.TaskDescriptor; -import org.sonar.api.batch.TaskExecutor; +import org.sonar.api.task.Task; +import org.sonar.api.task.TaskDefinition; -public class ListTaskExecutor implements TaskExecutor { +public class ListTasksTask implements Task { - private final TaskManager taskManager; + public static final String COMMAND = "list-tasks"; - public ListTaskExecutor(TaskManager taskManager) { + public static final TaskDefinition DEFINITION = TaskDefinition.create() + .setDescription("List all available tasks on the Sonar server") + .setName("List tasks") + .setCommand(COMMAND) + .setTask(ListTasksTask.class); + + private final Tasks taskManager; + + public ListTasksTask(Tasks taskManager) { this.taskManager = taskManager; } public void execute() { - for (TaskDefinition task : taskManager.getTasks()) { - TaskDescriptor desc = task.getTaskDescriptor(); - System.out.println(" " + desc.getCommand() + ": " + desc.getDescription()); + for (TaskDefinition taskDef : taskManager.getTaskDefinitions()) { + System.out.println(" " + taskDef.getCommand() + ": " + taskDef.getDescription()); } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/tasks/TaskManager.java b/sonar-batch/src/main/java/org/sonar/batch/tasks/TaskManager.java deleted file mode 100644 index 92ef002a4ef..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/tasks/TaskManager.java +++ /dev/null @@ -1,46 +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.tasks; - -import org.sonar.api.batch.TaskDefinition; -import org.sonar.api.utils.SonarException; - -public class TaskManager { - - private final TaskDefinition[] tasks; - - public TaskManager(TaskDefinition[] tasks) { - this.tasks = tasks; - } - - public TaskDefinition getTask(String command) { - for (TaskDefinition task : tasks) { - if (command.equals(task.getTaskDescriptor().getCommand())) { - return task; - } - } - throw new SonarException("No task found for command: " + command); - } - - public TaskDefinition[] getTasks() { - return tasks; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/tasks/Tasks.java b/sonar-batch/src/main/java/org/sonar/batch/tasks/Tasks.java new file mode 100644 index 00000000000..9da1ad65271 --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/tasks/Tasks.java @@ -0,0 +1,58 @@ +/* + * 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.tasks; + +import org.apache.commons.lang.StringUtils; +import org.sonar.api.CoreProperties; +import org.sonar.api.config.Settings; +import org.sonar.api.task.TaskDefinition; +import org.sonar.api.utils.SonarException; + +public class Tasks { + + private final TaskDefinition[] taskDefinitions; + private final Settings settings; + + public Tasks(Settings settings, TaskDefinition[] taskDefinitions) { + this.settings = settings; + this.taskDefinitions = taskDefinitions; + } + + public TaskDefinition getTaskDefinition(String command) { + String finalCommand = command; + if (StringUtils.isBlank(finalCommand)) { + // Try with a property + finalCommand = settings.getString(CoreProperties.TASK); + } + // Default to inspection task + finalCommand = StringUtils.isNotBlank(finalCommand) ? finalCommand : InspectionTask.COMMAND; + for (TaskDefinition taskDef : taskDefinitions) { + if (finalCommand.equals(taskDef.getCommand())) { + return taskDef; + } + } + throw new SonarException("No task found for command: " + finalCommand); + } + + public TaskDefinition[] getTaskDefinitions() { + return taskDefinitions; + } + +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/AbstractTaskModuleTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/AbstractTaskModuleTest.java index 6569ce948c3..4963c9cd0c8 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/AbstractTaskModuleTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/AbstractTaskModuleTest.java @@ -21,7 +21,7 @@ package org.sonar.batch.bootstrap; import org.junit.Test; import org.sonar.api.platform.ComponentContainer; -import org.sonar.batch.tasks.ListTaskDefinition; +import org.sonar.api.task.TaskDefinition; import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; @@ -40,7 +40,7 @@ public class AbstractTaskModuleTest { } }; bootstrapModule.init(); - ProjectTaskModule module = new ProjectTaskModule(new ListTaskDefinition()); + ProjectTaskModule module = new ProjectTaskModule(TaskDefinition.create()); bootstrapModule.installChild(module); verify(extensionInstaller).installTaskExtensions(any(ComponentContainer.class), eq(true)); @@ -57,7 +57,7 @@ public class AbstractTaskModuleTest { } }; bootstrapModule.init(); - ProjectLessTaskModule module = new ProjectLessTaskModule(new ListTaskDefinition()); + ProjectLessTaskModule module = new ProjectLessTaskModule(TaskDefinition.create()); bootstrapModule.installChild(module); verify(extensionInstaller).installTaskExtensions(any(ComponentContainer.class), eq(false)); 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 new file mode 100644 index 00000000000..568f9ef9b3d --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/TaskBootstrapModuleTest.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 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.doStart(); + } + +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/tasks/TasksTest.java b/sonar-batch/src/test/java/org/sonar/batch/tasks/TasksTest.java new file mode 100644 index 00000000000..f95fa5c31c3 --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/tasks/TasksTest.java @@ -0,0 +1,83 @@ +/* + * 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.tasks; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.CoreProperties; +import org.sonar.api.config.Settings; +import org.sonar.api.task.TaskDefinition; +import org.sonar.api.utils.SonarException; + +import static org.fest.assertions.Assertions.assertThat; + +public class TasksTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + private Settings settings; + + @Before + public void prepare() { + settings = new Settings(); + } + + @Test + public void shouldReturnTaskDefinitions() { + Tasks tasks = new Tasks(settings, new TaskDefinition[] {InspectionTask.DEFINITION, ListTasksTask.DEFINITION}); + assertThat(tasks.getTaskDefinitions().length).isEqualTo(2); + } + + @Test + public void shouldReturnInspectionTask() { + Tasks tasks = new Tasks(settings, new TaskDefinition[] {InspectionTask.DEFINITION, ListTasksTask.DEFINITION}); + assertThat(tasks.getTaskDefinition(InspectionTask.COMMAND)).isEqualTo(InspectionTask.DEFINITION); + } + + @Test + public void shouldReturnInspectionTaskByDefault() { + Tasks tasks = new Tasks(settings, new TaskDefinition[] {InspectionTask.DEFINITION, ListTasksTask.DEFINITION}); + assertThat(tasks.getTaskDefinition(null)).isEqualTo(InspectionTask.DEFINITION); + } + + @Test + public void shouldReturnUsePropertyWhenNoCommand() { + Tasks tasks = new Tasks(settings, new TaskDefinition[] {InspectionTask.DEFINITION, ListTasksTask.DEFINITION}); + assertThat(tasks.getTaskDefinition(ListTasksTask.COMMAND)).isEqualTo(ListTasksTask.DEFINITION); + assertThat(tasks.getTaskDefinition(null)).isEqualTo(InspectionTask.DEFINITION); + + settings.setProperty(CoreProperties.TASK, ListTasksTask.COMMAND); + assertThat(tasks.getTaskDefinition(null)).isEqualTo(ListTasksTask.DEFINITION); + assertThat(tasks.getTaskDefinition(InspectionTask.COMMAND)).isEqualTo(InspectionTask.DEFINITION); + } + + @Test + public void shouldThrowWhenCommandNotFound() { + Tasks tasks = new Tasks(settings, new TaskDefinition[] {InspectionTask.DEFINITION, ListTasksTask.DEFINITION}); + + thrown.expect(SonarException.class); + thrown.expectMessage("No task found for command: not-exists"); + + tasks.getTaskDefinition("not-exists"); + } +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java b/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java index 7c5c07df2b8..3186a6ca6d7 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java @@ -161,7 +161,6 @@ public interface CoreProperties { String BATCH_INCLUDE_PLUGINS = "sonar.includePlugins"; String BATCH_EXCLUDE_PLUGINS = "sonar.excludePlugins"; - /** * @since 3.4 */ @@ -373,6 +372,11 @@ public interface CoreProperties { String FORCE_ANALYSIS = "sonar.forceAnalysis"; /** + * @since 3.5 + */ + String TASK = "sonar.task"; + + /** * @deprecated replaced in v3.4 by properties specific to languages, for example sonar.java.coveragePlugin * See http://jira.codehaus.org/browse/SONARJAVA-39 for more details. */ diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/TaskDefinition.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/TaskDefinition.java deleted file mode 100644 index e442a60d45f..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/TaskDefinition.java +++ /dev/null @@ -1,34 +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.api.batch; - -import org.sonar.api.TaskDefinitionExtension; - -/** - * Implement this interface to provide a new task. - * @since 3.5 - */ -public interface TaskDefinition extends TaskDefinitionExtension { - - TaskDescriptor getTaskDescriptor(); - - Class<? extends TaskExecutor> getExecutor(); - -} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/RequiresProject.java b/sonar-plugin-api/src/main/java/org/sonar/api/task/RequiresProject.java index 010cd0f82e6..23f1e52645e 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/RequiresProject.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/task/RequiresProject.java @@ -17,7 +17,7 @@ * License along with Sonar; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 */ -package org.sonar.api.batch; +package org.sonar.api.task; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/TaskExecutor.java b/sonar-plugin-api/src/main/java/org/sonar/api/task/Task.java index cb6998b92f7..1f112f8f5cf 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/TaskExecutor.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/task/Task.java @@ -17,7 +17,7 @@ * License along with Sonar; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 */ -package org.sonar.api.batch; +package org.sonar.api.task; import org.sonar.api.TaskExtension; @@ -25,7 +25,7 @@ import org.sonar.api.TaskExtension; * Implement this interface to provide the behavior of a task. * @since 3.5 */ -public interface TaskExecutor extends TaskExtension { +public interface Task extends TaskExtension { void execute(); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/TaskDescriptor.java b/sonar-plugin-api/src/main/java/org/sonar/api/task/TaskDefinition.java index 3933c6da735..75f48b670b0 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/TaskDescriptor.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/task/TaskDefinition.java @@ -17,32 +17,34 @@ * License along with Sonar; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 */ -package org.sonar.api.batch; +package org.sonar.api.task; + +import org.sonar.api.TaskDefinitionExtension; /** - * Provide description of a task. + * Implement this interface to provide a new task. * @since 3.5 */ -public class TaskDescriptor { +public class TaskDefinition implements TaskDefinitionExtension { private String name; private String description; private String command; - private boolean requiresProject = false; + private Class<? extends Task> task; - private TaskDescriptor() { + private TaskDefinition() { } - public static TaskDescriptor create() { - return new TaskDescriptor(); + public static TaskDefinition create() { + return new TaskDefinition(); } public String getName() { return name; } - public TaskDescriptor setName(String name) { + public TaskDefinition setName(String name) { this.name = name; return this; } @@ -51,7 +53,7 @@ public class TaskDescriptor { return description; } - public TaskDescriptor setDescription(String description) { + public TaskDefinition setDescription(String description) { this.description = description; return this; } @@ -60,18 +62,23 @@ public class TaskDescriptor { return command; } - public TaskDescriptor setCommand(String command) { + public TaskDefinition setCommand(String command) { this.command = command; return this; } - public boolean isRequiresProject() { - return requiresProject; + public Class<? extends Task> getTask() { + return task; } - public TaskDescriptor setRequiresProject(boolean requiresProject) { - this.requiresProject = requiresProject; + public TaskDefinition setTask(Class<? extends Task> task) { + this.task = task; return this; } + @Override + public String toString() { + return "Definition of task " + task + " with command " + command; + } + } |