1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
/*
* 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.Task;
import org.sonar.api.task.TaskDefinition;
import org.sonar.api.utils.SonarException;
import org.sonar.batch.scan.ScanTask;
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[] {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();
}
@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'");
tasks.start();
}
@Test
public void shouldThrowWhenNameMissing() {
Tasks tasks = new Tasks(settings, new TaskDefinition[] {TaskDefinition.create().setCommand("foo").setTask(FakeTask1.class)});
thrown.expect(SonarException.class);
thrown.expectMessage("Task definition for task 'org.sonar.batch.tasks.TasksTest$FakeTask1' doesn't define task name");
tasks.start();
}
@Test
public void shouldThrowWhenTaskMissing() {
Tasks tasks = new Tasks(settings, new TaskDefinition[] {TaskDefinition.create().setCommand("foo").setName("bar")});
thrown.expect(SonarException.class);
thrown.expectMessage("Task definition 'bar' doesn't define the associated task class");
tasks.start();
}
@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")});
thrown.expect(SonarException.class);
thrown.expectMessage("Task 'org.sonar.batch.tasks.TasksTest$FakeTask1' is defined twice: first by 'foo1' and then by 'foo2'");
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() {
}
}
}
|