diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2015-01-20 16:45:27 +0100 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2015-01-20 16:55:06 +0100 |
commit | fb21d4cbafc3a5e7b9c63c0df300129d9135045b (patch) | |
tree | 6897400db2f4618dfa9544525f2766f68e1eacb6 /sonar-batch/src/test/java | |
parent | 0d24e1857064b3346461d77e7b53824c77cc7681 (diff) | |
download | sonarqube-fb21d4cbafc3a5e7b9c63c0df300129d9135045b.tar.gz sonarqube-fb21d4cbafc3a5e7b9c63c0df300129d9135045b.zip |
SONAR-5077 Fix regression when a Sensor try to read "lines" measure. LinesSensor forced to be executed first.
Diffstat (limited to 'sonar-batch/src/test/java')
5 files changed, 344 insertions, 12 deletions
diff --git a/sonar-batch/src/test/java/org/sonar/batch/DecoratorsSelectorTest.java b/sonar-batch/src/test/java/org/sonar/batch/DecoratorsSelectorTest.java index fee6a665262..8a0e9a24764 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/DecoratorsSelectorTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/DecoratorsSelectorTest.java @@ -21,7 +21,6 @@ package org.sonar.batch; import com.google.common.collect.Iterables; import org.junit.Test; -import org.sonar.api.batch.BatchExtensionDictionnary; import org.sonar.api.batch.Decorator; import org.sonar.api.batch.DecoratorContext; import org.sonar.api.batch.DependedUpon; @@ -33,6 +32,7 @@ import org.sonar.api.measures.Metric; import org.sonar.api.platform.ComponentContainer; import org.sonar.api.resources.Project; import org.sonar.api.resources.Resource; +import org.sonar.batch.bootstrap.BatchExtensionDictionnary; import java.util.Arrays; import java.util.Collection; @@ -81,7 +81,7 @@ public class DecoratorsSelectorTest { for (Object extension : extensions) { ioc.addSingleton(extension); } - return new BatchExtensionDictionnary(ioc); + return new BatchExtensionDictionnary(ioc, null, null); } class FakeFormula implements Formula { diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchExtensionDictionnaryTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchExtensionDictionnaryTest.java index f6979d924c6..046f6664967 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchExtensionDictionnaryTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchExtensionDictionnaryTest.java @@ -19,17 +19,28 @@ */ package org.sonar.batch.bootstrap; -import org.sonar.batch.sensor.AnalyzerOptimizer; - +import com.google.common.collect.Lists; import org.junit.Test; import org.sonar.api.BatchExtension; +import org.sonar.api.batch.BuildBreaker; +import org.sonar.api.batch.CheckProject; +import org.sonar.api.batch.Decorator; +import org.sonar.api.batch.DependedUpon; +import org.sonar.api.batch.DependsUpon; +import org.sonar.api.batch.Phase; +import org.sonar.api.batch.PostJob; import org.sonar.api.batch.Sensor; +import org.sonar.api.batch.SensorContext; import org.sonar.api.platform.ComponentContainer; import org.sonar.api.resources.Project; +import org.sonar.batch.sensor.AnalyzerOptimizer; import org.sonar.batch.sensor.DefaultSensorContext; +import java.util.Arrays; import java.util.Collection; +import java.util.List; +import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.Matchers.hasItem; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; @@ -61,16 +72,338 @@ public class BatchExtensionDictionnaryTest { assertEquals(1, sensors.size()); } + @Test + public void testGetFilteredExtensions() { + 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, null); + + assertThat(sensors).containsOnly(sensor1, sensor2); + } + + @Test + public void shouldSearchInParentContainers() { + BatchExtension a = new FakeSensor(); + BatchExtension b = new FakeSensor(); + BatchExtension c = new FakeSensor(); + + ComponentContainer grandParent = new ComponentContainer(); + grandParent.addSingleton(a); + + ComponentContainer parent = grandParent.createChild(); + parent.addSingleton(b); + + ComponentContainer child = parent.createChild(); + child.addSingleton(c); + + BatchExtensionDictionnary dictionnary = new BatchExtensionDictionnary(child, mock(DefaultSensorContext.class), mock(AnalyzerOptimizer.class)); + assertThat(dictionnary.select(BatchExtension.class, null, true, null)).containsOnly(a, b, c); + } + + @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, null)); + + assertThat(extensions).hasSize(3); + assertThat(extensions.get(0)).isEqualTo(a); + assertThat(extensions.get(1)).isEqualTo(b); + assertThat(extensions.get(2)).isEqualTo(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, null)); + + assertThat(extensions.size()).isEqualTo(2); + assertThat(extensions.get(0)).isEqualTo(a); + assertThat(extensions.get(1)).isEqualTo(b); + + // different initial order + selector = newSelector(b, a); + extensions = Lists.newArrayList(selector.select(BatchExtension.class, null, true, null)); + + assertThat(extensions).hasSize(2); + assertThat(extensions.get(0)).isEqualTo(a); + assertThat(extensions.get(1)).isEqualTo(b); + } + + @Test + public void methodDependsUponCollection() { + BatchExtension a = new GeneratesSomething("foo"); + BatchExtension b = new MethodDependentOf(Arrays.asList("foo")); + + BatchExtensionDictionnary selector = newSelector(a, b); + List<BatchExtension> extensions = Lists.newArrayList(selector.select(BatchExtension.class, null, true, null)); + + assertThat(extensions).hasSize(2); + assertThat(extensions.get(0)).isEqualTo(a); + assertThat(extensions.get(1)).isEqualTo(b); + + // different initial order + selector = newSelector(b, a); + extensions = Lists.newArrayList(selector.select(BatchExtension.class, null, true, null)); + + assertThat(extensions).hasSize(2); + assertThat(extensions.get(0)).isEqualTo(a); + assertThat(extensions.get(1)).isEqualTo(b); + } + + @Test + public void methodDependsUponArray() { + BatchExtension a = new GeneratesSomething("foo"); + BatchExtension b = new MethodDependentOf(new String[] {"foo"}); + + BatchExtensionDictionnary selector = newSelector(a, b); + List<BatchExtension> extensions = Lists.newArrayList(selector.select(BatchExtension.class, null, true, null)); + + assertThat(extensions).hasSize(2); + assertThat(extensions.get(0)).isEqualTo(a); + assertThat(extensions.get(1)).isEqualTo(b); + + // different initial order + selector = newSelector(b, a); + extensions = Lists.newArrayList(selector.select(BatchExtension.class, null, true, null)); + + assertThat(extensions).hasSize(2); + assertThat(extensions.get(0)).isEqualTo(a); + assertThat(extensions.get(1)).isEqualTo(b); + } + + @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, null)); + + assertThat(extensions).hasSize(2); + assertThat(extensions.get(0)).isEqualTo(a); + assertThat(extensions.get(1)).isEqualTo(b); + + // different initial order + selector = newSelector(b, a); + extensions = Lists.newArrayList(selector.select(BatchExtension.class, null, true, null)); + + assertThat(extensions).hasSize(2); + assertThat(extensions.get(0)).isEqualTo(a); + assertThat(extensions.get(1)).isEqualTo(b); + } + + @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, null)); + + assertThat(extensions).hasSize(2); + assertThat(extensions.get(0)).isEqualTo(a); + assertThat(extensions.get(1)).isEqualTo(b); + + // different initial order + selector = newSelector(b, a); + extensions = Lists.newArrayList(selector.select(BatchExtension.class, null, true, null)); + + assertThat(extensions).hasSize(2); + assertThat(extensions.get(0)).isEqualTo(a); + assertThat(extensions.get(1)).isEqualTo(b); + } + + @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, null)); + + assertThat(extensions).hasSize(1); + assertThat(extensions.get(0)).isInstanceOf(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, null)); + + assertThat(extensions).hasSize(2); + assertThat(extensions.get(0)).isEqualTo(a); + assertThat(extensions.get(1)).isEqualTo(b); + + // change initial order + selector = newSelector(a, b); + extensions = Lists.newArrayList(selector.select(BatchExtension.class, null, true, null)); + + assertThat(extensions).hasSize(2); + assertThat(extensions.get(0)).isEqualTo(a); + assertThat(extensions.get(1)).isEqualTo(b); + } + + @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, null)); + + assertThat(extensions).hasSize(3); + assertThat(extensions.get(0)).isEqualTo(pre); + assertThat(extensions.get(1)).isEqualTo(analyze); + assertThat(extensions.get(2)).isEqualTo(post); + } + + @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, null)); + + assertThat(extensions).hasSize(3); + assertThat(extensions.get(0)).isEqualTo(pre); + assertThat(extensions.get(1)).isEqualTo(analyze); + assertThat(extensions.get(2)).isEqualTo(post); + } + + @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, null)); + + assertThat(extensions).hasSize(3); + assertThat(extensions.get(2)).isEqualTo(checker); + } + class FakeSensor implements Sensor { - @Override - public void analyse(Project project, org.sonar.api.batch.SensorContext context) { + 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; } - @Override + @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) { + } + } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/phases/DecoratorsExecutorTest.java b/sonar-batch/src/test/java/org/sonar/batch/phases/DecoratorsExecutorTest.java index 3f471711146..45774d11f95 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/phases/DecoratorsExecutorTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/phases/DecoratorsExecutorTest.java @@ -20,7 +20,6 @@ package org.sonar.batch.phases; import org.junit.Test; -import org.sonar.api.batch.BatchExtensionDictionnary; import org.sonar.api.batch.Decorator; import org.sonar.api.batch.DecoratorContext; import org.sonar.api.batch.SonarIndex; @@ -30,6 +29,7 @@ import org.sonar.api.resources.Project; import org.sonar.api.resources.Resource; import org.sonar.api.utils.SonarException; import org.sonar.batch.DefaultDecoratorContext; +import org.sonar.batch.bootstrap.BatchExtensionDictionnary; import org.sonar.batch.duplication.DuplicationCache; import org.sonar.batch.events.EventBus; import org.sonar.batch.scan.measure.MeasureCache; diff --git a/sonar-batch/src/test/java/org/sonar/batch/phases/PostJobsExecutorTest.java b/sonar-batch/src/test/java/org/sonar/batch/phases/PostJobsExecutorTest.java index e8f3371c950..b2617374f6b 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/phases/PostJobsExecutorTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/phases/PostJobsExecutorTest.java @@ -21,10 +21,10 @@ package org.sonar.batch.phases; import org.junit.Before; import org.junit.Test; -import org.sonar.api.batch.BatchExtensionDictionnary; import org.sonar.api.batch.PostJob; import org.sonar.api.batch.SensorContext; import org.sonar.api.resources.Project; +import org.sonar.batch.bootstrap.BatchExtensionDictionnary; import org.sonar.batch.events.EventBus; import org.sonar.batch.scan.filesystem.DefaultModuleFileSystem; import org.sonar.batch.scan.maven.MavenPluginExecutor; @@ -52,7 +52,7 @@ public class PostJobsExecutorTest { @Test public void should_execute_post_jobs() { - when(selector.select(PostJob.class, project, true)).thenReturn(Arrays.asList(job1, job2)); + when(selector.select(PostJob.class, project, true, null)).thenReturn(Arrays.asList(job1, job2)); executor.execute(context); diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/maven/MavenPluginsConfiguratorTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/maven/MavenPluginsConfiguratorTest.java index c0a69019d52..ca1af7d653f 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/maven/MavenPluginsConfiguratorTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/maven/MavenPluginsConfiguratorTest.java @@ -21,12 +21,11 @@ package org.sonar.batch.scan.maven; import org.junit.Test; import org.mockito.ArgumentMatcher; -import org.sonar.api.batch.BatchExtensionDictionnary; import org.sonar.api.batch.maven.MavenPlugin; import org.sonar.api.batch.maven.MavenPluginHandler; import org.sonar.api.resources.Project; import org.sonar.api.test.MavenTestUtils; -import org.sonar.batch.scan.maven.MavenPluginsConfigurator; +import org.sonar.batch.bootstrap.BatchExtensionDictionnary; import java.util.Arrays; |