diff options
Diffstat (limited to 'sonar-batch/src/test/java/org/sonar/batch/tasks/TasksTest.java')
-rw-r--r-- | sonar-batch/src/test/java/org/sonar/batch/tasks/TasksTest.java | 126 |
1 files changed, 20 insertions, 106 deletions
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 index 102576ec196..a2e394d9b1b 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/tasks/TasksTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/tasks/TasksTest.java @@ -19,12 +19,9 @@ */ 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.Task; import org.sonar.api.task.TaskDefinition; import org.sonar.api.utils.SonarException; @@ -37,115 +34,43 @@ 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[] {ScanTask.DEFINITION, ListTasksTask.DEFINITION}); - assertThat(tasks.getTaskDefinitions().length).isEqualTo(2); - } - - @Test - public void shouldReturnInspectionTask() { - Tasks tasks = new Tasks(settings, new TaskDefinition[] {ScanTask.DEFINITION, ListTasksTask.DEFINITION}); - tasks.start(); - assertThat(tasks.getTaskDefinition(ScanTask.COMMAND)).isEqualTo(ScanTask.DEFINITION); - } - - @Test - public void shouldReturnInspectionTaskByDefault() { - Tasks tasks = new Tasks(settings, new TaskDefinition[] {ScanTask.DEFINITION, ListTasksTask.DEFINITION}); - tasks.start(); - assertThat(tasks.getTaskDefinition(null)).isEqualTo(ScanTask.DEFINITION); - } - - @Test - public void shouldReturnUsePropertyWhenNoCommand() { - Tasks tasks = new Tasks(settings, new TaskDefinition[] {ScanTask.DEFINITION, ListTasksTask.DEFINITION}); - tasks.start(); - assertThat(tasks.getTaskDefinition(ListTasksTask.COMMAND)).isEqualTo(ListTasksTask.DEFINITION); - assertThat(tasks.getTaskDefinition(null)).isEqualTo(ScanTask.DEFINITION); - - settings.setProperty(CoreProperties.TASK, ListTasksTask.COMMAND); - assertThat(tasks.getTaskDefinition(null)).isEqualTo(ListTasksTask.DEFINITION); - assertThat(tasks.getTaskDefinition(ScanTask.COMMAND)).isEqualTo(ScanTask.DEFINITION); - } - - @Test - public void shouldThrowWhenCommandNotFound() { - Tasks tasks = new Tasks(settings, new TaskDefinition[] {ScanTask.DEFINITION, ListTasksTask.DEFINITION}); - - thrown.expect(SonarException.class); - thrown.expectMessage("No task found for command: not-exists"); - - tasks.getTaskDefinition("not-exists"); - } - - @Test - public void shouldThrowWhenCommandMissing() { - Tasks tasks = new Tasks(settings, new TaskDefinition[] {TaskDefinition.create().setName("foo").setTask(FakeTask1.class)}); - - thrown.expect(SonarException.class); - thrown.expectMessage("Task definition 'foo' doesn't define task command"); - - tasks.start(); - } - @Test - public void shouldThrowWhenCommandInvalid() { - Tasks tasks = new Tasks(settings, new TaskDefinition[] {TaskDefinition.create().setName("foo").setTask(FakeTask1.class).setCommand("Azc-12_bbC")}); - tasks.start(); - - tasks = new Tasks(settings, new TaskDefinition[] {TaskDefinition.create().setName("foo").setTask(FakeTask1.class).setCommand("with space")}); - - thrown.expect(SonarException.class); - thrown.expectMessage("Command 'with space' for task definition 'foo' is not valid and should match [a-zA-Z0-9\\-\\_]+"); - - tasks.start(); + public void should_get_definitions() { + Tasks tasks = new Tasks(new TaskDefinition[]{ScanTask.DEFINITION, ListTask.DEFINITION}); + assertThat(tasks.definitions()).hasSize(2); } @Test - public void shouldThrowWhenDuplicateCommand() { - Tasks tasks = new Tasks(settings, new TaskDefinition[] { - TaskDefinition.create().setName("foo1").setTask(FakeTask1.class).setCommand("cmd"), - TaskDefinition.create().setName("foo2").setTask(FakeTask2.class).setCommand("cmd")}); - - thrown.expect(SonarException.class); - thrown.expectMessage("Task 'foo2' uses the same command than task 'foo1'"); - + public void should_get_definition_by_key() { + Tasks tasks = new Tasks(new TaskDefinition[]{ScanTask.DEFINITION, ListTask.DEFINITION}); tasks.start(); + assertThat(tasks.definition(ListTask.DEFINITION.key())).isEqualTo(ListTask.DEFINITION); } @Test - public void shouldThrowWhenNameMissing() { - Tasks tasks = new Tasks(settings, new TaskDefinition[] {TaskDefinition.create().setCommand("foo").setTask(FakeTask1.class)}); + public void should_return_null_if_task_not_found() { + Tasks tasks = new Tasks(new TaskDefinition[]{ScanTask.DEFINITION, ListTask.DEFINITION}); - thrown.expect(SonarException.class); - thrown.expectMessage("Task definition for task 'org.sonar.batch.tasks.TasksTest$FakeTask1' doesn't define task name"); - - tasks.start(); + assertThat(tasks.definition("not-exists")).isNull(); } @Test - public void shouldThrowWhenTaskMissing() { - Tasks tasks = new Tasks(settings, new TaskDefinition[] {TaskDefinition.create().setCommand("foo").setName("bar")}); - + public void should_fail_on_duplicated_keys() { thrown.expect(SonarException.class); - thrown.expectMessage("Task definition 'bar' doesn't define the associated task class"); + thrown.expectMessage("Task 'foo' is declared twice"); - tasks.start(); + new Tasks(new TaskDefinition[]{ + TaskDefinition.builder().key("foo").taskClass(FakeTask1.class).description("foo1").build(), + TaskDefinition.builder().key("foo").taskClass(FakeTask2.class).description("foo2").build() + }); } @Test - public void shouldThrowWhenDuplicateTask() { - Tasks tasks = new Tasks(settings, new TaskDefinition[] { - TaskDefinition.create().setName("foo1").setTask(FakeTask1.class).setCommand("cmd1"), - TaskDefinition.create().setName("foo2").setTask(FakeTask1.class).setCommand("cmd2")}); + public void should_fail_on_duplicated_class() { + Tasks tasks = new Tasks(new TaskDefinition[]{ + TaskDefinition.builder().key("foo1").taskClass(FakeTask1.class).description("foo1").build(), + TaskDefinition.builder().key("foo2").taskClass(FakeTask1.class).description("foo1").build() + }); thrown.expect(SonarException.class); thrown.expectMessage("Task 'org.sonar.batch.tasks.TasksTest$FakeTask1' is defined twice: first by 'foo1' and then by 'foo2'"); @@ -153,23 +78,12 @@ public class TasksTest { tasks.start(); } - @Test - public void shouldUseNameWhenDescriptionIsMissing() { - Tasks tasks = new Tasks(settings, new TaskDefinition[] {TaskDefinition.create().setName("foo").setCommand("cmd").setTask(FakeTask1.class)}); - tasks.start(); - - assertThat(tasks.getTaskDefinition("cmd").getDescription()).isEqualTo("foo"); - } - private static class FakeTask1 implements Task { - public void execute() { } - } private static class FakeTask2 implements Task { - public void execute() { } |