aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api/src/test/java/org/sonar/api/batch/BatchExtensionDictionnaryTest.java
blob: 210da5a4c595e2e4dc08b579a7a2cd38718f66ad (plain)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
 * Sonar, open source software quality management tool.
 * Copyright (C) 2008-2011 SonarSource
 * mailto:contact AT sonarsource DOT com
 *
 * Sonar is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * Sonar is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sonar; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
 */
package org.sonar.api.batch;

import com.google.common.collect.Lists;
import org.junit.Test;
import org.picocontainer.containers.TransientPicoContainer;
import org.sonar.api.BatchExtension;
import org.sonar.api.resources.Project;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.internal.matchers.IsCollectionContaining.hasItem;
import static org.mockito.Mockito.mock;

public class BatchExtensionDictionnaryTest {

  private BatchExtensionDictionnary newSelector(BatchExtension... extensions) {
    TransientPicoContainer iocContainer = new TransientPicoContainer();
    int key = 0;
    for (BatchExtension extension : extensions) {
      iocContainer.addComponent(key++, extension);
    }
    return new BatchExtensionDictionnary(iocContainer);
  }

  @Test
  public void getFilteredExtensions() {
    Sensor sensor1 = new FakeSensor(), sensor2 = new FakeSensor();
    Decorator decorator = mock(Decorator.class);

    BatchExtensionDictionnary selector = newSelector(sensor1, sensor2, decorator);
    Collection<Sensor> sensors = selector.select(Sensor.class, null, true);

    assertThat(sensors, hasItem(sensor1));
    assertThat(sensors, hasItem(sensor2));
    assertEquals(2, sensors.size());
  }

  @Test
  public void sortExtensionsByDependency() {
    BatchExtension a = new MethodDependentOf(null);
    BatchExtension b = new MethodDependentOf(a);
    BatchExtension c = new MethodDependentOf(b);

    BatchExtensionDictionnary selector = newSelector(b, c, a);
    List<BatchExtension> extensions = Lists.newArrayList(selector.select(BatchExtension.class, null, true));

    assertThat(extensions.size(), is(3));
    assertThat(extensions.get(0), is(a));
    assertThat(extensions.get(1), is(b));
    assertThat(extensions.get(2), is(c));
  }

  @Test
  public void useMethodAnnotationsToSortExtensions() {
    BatchExtension a = new GeneratesSomething("foo");
    BatchExtension b = new MethodDependentOf("foo");

    BatchExtensionDictionnary selector = newSelector(a, b);
    List<BatchExtension> extensions = Lists.newArrayList(selector.select(BatchExtension.class, null, true));

    assertThat(extensions.size(), is(2));
    assertThat(extensions.get(0), is(a));
    assertThat(extensions.get(1), is(b));

    // different initial order
    selector = newSelector(b, a);
    extensions = Lists.newArrayList(selector.select(BatchExtension.class, null, true));

    assertEquals(2, extensions.size());
    assertEquals(a, extensions.get(0));
    assertEquals(b, extensions.get(1));
  }

  @Test
  public void useClassAnnotationsToSortExtensions() {
    BatchExtension a = new ClassDependedUpon();
    BatchExtension b = new ClassDependsUpon();

    BatchExtensionDictionnary selector = newSelector(a, b);
    List<BatchExtension> extensions = Lists.newArrayList(selector.select(BatchExtension.class, null, true));

    assertThat(extensions.size(), is(2));
    assertThat(extensions.get(0), is(a));
    assertThat(extensions.get(1), is(b));

    // different initial order
    selector = newSelector(b, a);
    extensions = Lists.newArrayList(selector.select(BatchExtension.class, null, true));

    assertEquals(2, extensions.size());
    assertEquals(a, extensions.get(0));
    assertEquals(b, extensions.get(1));
  }


  @Test
  public void useClassAnnotationsOnInterfaces() {
    BatchExtension a = new InterfaceDependedUpon() {
    };
    BatchExtension b = new InterfaceDependsUpon() {
    };

    BatchExtensionDictionnary selector = newSelector(a, b);
    List<BatchExtension> extensions = Lists.newArrayList(selector.select(BatchExtension.class, null, true));

    assertThat(extensions.size(), is(2));
    assertThat(extensions.get(0), is(a));
    assertThat(extensions.get(1), is(b));

    // different initial order
    selector = newSelector(b, a);
    extensions = Lists.newArrayList(selector.select(BatchExtension.class, null, true));

    assertEquals(2, extensions.size());
    assertEquals(a, extensions.get(0));
    assertEquals(b, extensions.get(1));
  }


  @Test
  public void checkProject() {
    BatchExtension ok = new CheckProjectOK();
    BatchExtension ko = new CheckProjectKO();

    BatchExtensionDictionnary selector = newSelector(ok, ko);
    List<BatchExtension> extensions = Lists.newArrayList(selector.select(BatchExtension.class, new Project("key"), true));

    assertThat(extensions.size(), is(1));
    assertThat(extensions.get(0), is(CheckProjectOK.class));
  }

  @Test
  public void inheritAnnotations() {
    BatchExtension a = new SubClass("foo");
    BatchExtension b = new MethodDependentOf("foo");

    BatchExtensionDictionnary selector = newSelector(b, a);
    List<BatchExtension> extensions = Lists.newArrayList(selector.select(BatchExtension.class, null, true));

    assertEquals(2, extensions.size());
    assertEquals(a, extensions.get(0));
    assertEquals(b, extensions.get(1));

    // change initial order
    selector = newSelector(a, b);
    extensions = Lists.newArrayList(selector.select(BatchExtension.class, null, true));

    assertEquals(2, extensions.size());
    assertEquals(a, extensions.get(0));
    assertEquals(b, extensions.get(1));
  }

  @Test(expected = IllegalStateException.class)
  public void annotatedMethodsCanNotBePrivate() {
    BatchExtensionDictionnary selector = newSelector();
    BatchExtension wrong = new BatchExtension() {
      @DependsUpon
      private Object foo() {
        return "foo";
      }
    };
    selector.evaluateAnnotatedClasses(wrong, DependsUpon.class);
  }

  @Test
  public void dependsUponPhase() {
    BatchExtension pre = new PreSensor();
    BatchExtension analyze = new GeneratesSomething("something");
    BatchExtension post = new PostSensor();

    BatchExtensionDictionnary selector = newSelector(analyze, post, pre);
    List extensions = Lists.newArrayList(selector.select(BatchExtension.class, null, true));

    assertEquals(3, extensions.size());
    assertEquals(pre, extensions.get(0));
    assertEquals(analyze, extensions.get(1));
    assertEquals(post, extensions.get(2));
  }

  @Test
  public void dependsUponInheritedPhase() {
    BatchExtension pre = new PreSensorSubclass();
    BatchExtension analyze = new GeneratesSomething("something");
    BatchExtension post = new PostSensorSubclass();

    BatchExtensionDictionnary selector = newSelector(analyze, post, pre);
    List extensions = Lists.newArrayList(selector.select(BatchExtension.class, null, true));

    assertEquals(3, extensions.size());
    assertEquals(pre, extensions.get(0));
    assertEquals(analyze, extensions.get(1));
    assertEquals(post, extensions.get(2));
  }

  @Test
  public void buildStatusCheckersAreExecutedAfterOtherPostJobs() {
    BuildBreaker checker = new BuildBreaker() {
      public void executeOn(Project project, SensorContext context) {
      }
    };

    BatchExtensionDictionnary selector = newSelector(new FakePostJob(), checker, new FakePostJob());
    List extensions = Lists.newArrayList(selector.select(BatchExtension.class, null, true));

    assertEquals(3, extensions.size());
    assertEquals(checker, extensions.get(2));
  }

  class FakeSensor implements Sensor {

    public void analyse(Project project, SensorContext context) {

    }

    public boolean shouldExecuteOnProject(Project project) {
      return true;
    }
  }

  class MethodDependentOf implements BatchExtension {
    private Object dep;

    MethodDependentOf(Object o) {
      this.dep = o;
    }

    @DependsUpon
    public Object dependsUponObject() {
      return dep;
    }
  }

  @DependsUpon("flag")
  class ClassDependsUpon implements BatchExtension {
  }

  @DependedUpon("flag")
  class ClassDependedUpon implements BatchExtension {
  }

  @DependsUpon("flag")
  interface InterfaceDependsUpon extends BatchExtension {
  }

  @DependedUpon("flag")
  interface InterfaceDependedUpon extends BatchExtension {
  }

  class GeneratesSomething implements BatchExtension {
    private Object gen;

    GeneratesSomething(Object o) {
      this.gen = o;
    }

    @DependedUpon
    public Object generates() {
      return gen;
    }
  }

  class SubClass extends GeneratesSomething {
    SubClass(Object o) {
      super(o);
    }
  }

  @Phase(name = Phase.Name.PRE)
  class PreSensor implements BatchExtension {

  }

  class PreSensorSubclass extends PreSensor {

  }

  @Phase(name = Phase.Name.POST)
  class PostSensor implements BatchExtension {

  }

  class PostSensorSubclass extends PostSensor {

  }

  class CheckProjectOK implements BatchExtension, CheckProject {
    public boolean shouldExecuteOnProject(Project project) {
      return true;
    }
  }

  class CheckProjectKO implements BatchExtension, CheckProject {
    public boolean shouldExecuteOnProject(Project project) {
      return false;
    }
  }

  private class FakePostJob implements PostJob {
    public void executeOn(Project project, SensorContext context) {
    }
  }
}