From 2510ba884971b7245445dac69641478a67bfad9d Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Mon, 2 Oct 2017 11:10:35 +0200 Subject: [PATCH] SONAR-9836 Fix @Phase annotation support on new PostJob API --- .../ScannerExtensionDictionnary.java | 35 +++--- .../ScannerExtensionDictionnaryTest.java | 103 +++++++++++++++--- 2 files changed, 100 insertions(+), 38 deletions(-) diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerExtensionDictionnary.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerExtensionDictionnary.java index 0117f735ba6..37a1f7f5364 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerExtensionDictionnary.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerExtensionDictionnary.java @@ -30,7 +30,6 @@ import java.util.Iterator; import java.util.List; import java.util.stream.Collectors; import javax.annotation.Nullable; - import org.apache.commons.lang.ClassUtils; import org.sonar.api.batch.CheckProject; import org.sonar.api.batch.DependedUpon; @@ -102,6 +101,8 @@ public class ScannerExtensionDictionnary { Object extensionToEvaluate; if (extension instanceof SensorWrapper) { extensionToEvaluate = ((SensorWrapper) extension).wrappedSensor(); + } else if (extension instanceof PostJobWrapper) { + extensionToEvaluate = ((PostJobWrapper) extension).wrappedPostJob(); } else { extensionToEvaluate = extension; } @@ -114,32 +115,26 @@ public class ScannerExtensionDictionnary { private List getFilteredExtensions(Class type, @Nullable DefaultInputModule module, @Nullable ExtensionMatcher matcher) { List result = new ArrayList<>(); - for (Object extension : getExtensions(type)) { + List candidates = new ArrayList<>(); + candidates.addAll(getExtensions(type)); + if (org.sonar.api.batch.Sensor.class.equals(type)) { + candidates.addAll(getExtensions(Sensor.class)); + } + if (org.sonar.api.batch.PostJob.class.equals(type)) { + candidates.addAll(getExtensions(PostJob.class)); + } + + for (Object extension : candidates) { if (org.sonar.api.batch.Sensor.class.equals(type) && extension instanceof Sensor) { extension = new SensorWrapper((Sensor) extension, sensorContext, sensorOptimizer); } + if (org.sonar.api.batch.PostJob.class.equals(type) && extension instanceof PostJob) { + extension = new PostJobWrapper((PostJob) extension, postJobContext, postJobOptimizer); + } if (shouldKeep(type, extension, module, matcher)) { result.add((T) extension); } } - if (org.sonar.api.batch.Sensor.class.equals(type)) { - // Retrieve new Sensors and wrap then in SensorWrapper - for (Sensor sensor : getExtensions(Sensor.class)) { - org.sonar.api.batch.Sensor extension = new SensorWrapper(sensor, sensorContext, sensorOptimizer); - if (shouldKeep(type, extension, module, matcher)) { - result.add((T) extension); - } - } - } - if (org.sonar.api.batch.PostJob.class.equals(type)) { - // Retrieve new PostJob and wrap then in PostJobWrapper - for (PostJob postJob : getExtensions(PostJob.class)) { - org.sonar.api.batch.PostJob extension = new PostJobWrapper(postJob, postJobContext, postJobOptimizer); - if (shouldKeep(type, extension, module, matcher)) { - result.add((T) extension); - } - } - } return result; } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerExtensionDictionnaryTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerExtensionDictionnaryTest.java index 54a254b4333..1320c92c4af 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerExtensionDictionnaryTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerExtensionDictionnaryTest.java @@ -40,6 +40,7 @@ import org.sonar.api.batch.SensorContext; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.fs.internal.DefaultInputModule; import org.sonar.api.batch.postjob.PostJobContext; +import org.sonar.api.batch.postjob.PostJobDescriptor; import org.sonar.api.batch.sensor.SensorDescriptor; import org.sonar.api.resources.Project; import org.sonar.core.platform.ComponentContainer; @@ -285,30 +286,42 @@ public class ScannerExtensionDictionnaryTest { } @Test - public void dependsUponPhase() { - BatchExtension pre = new PreSensor(); - BatchExtension analyze = new GeneratesSomething("something"); - BatchExtension post = new PostSensor(); + public void dependsUponPhaseForNewSensors() { + PreSensor pre = new PreSensor(); + NormalSensor normal = new NormalSensor(); + PostSensor post = new PostSensor(); - ScannerExtensionDictionnary selector = newSelector(analyze, post, pre); - List extensions = Lists.newArrayList(selector.select(BatchExtension.class, null, true, null)); + ScannerExtensionDictionnary selector = newSelector(normal, post, pre); + List extensions = Lists.newArrayList(selector.select(org.sonar.api.batch.sensor.Sensor.class, null, true, null)); + assertThat(extensions).containsExactly(pre, normal, post); - assertThat(extensions).hasSize(3); - assertThat(extensions.get(0)).isEqualTo(pre); - assertThat(extensions.get(1)).isEqualTo(analyze); - assertThat(extensions.get(2)).isEqualTo(post); + List oldExtensions = Lists.newArrayList(selector.select(Sensor.class, null, true, null)); + assertThat(oldExtensions).extracting("wrappedSensor").containsExactly(pre, normal, post); + } + + @Test + public void dependsUponPhaseForNewPostJob() { + PrePostJob pre = new PrePostJob(); + NormalPostJob normal = new NormalPostJob(); + + ScannerExtensionDictionnary selector = newSelector(normal, pre); + List extensions = Lists.newArrayList(selector.select(org.sonar.api.batch.postjob.PostJob.class, null, true, null)); + assertThat(extensions).containsExactly(pre, normal); + + List oldExtensions = Lists.newArrayList(selector.select(PostJob.class, null, true, null)); + assertThat(oldExtensions).extracting("wrappedPostJob").containsExactly(pre, normal); } @Test public void dependsUponInheritedPhase() { - BatchExtension pre = new PreSensorSubclass(); - BatchExtension analyze = new GeneratesSomething("something"); - BatchExtension post = new PostSensorSubclass(); + PreSensorSubclass pre = new PreSensorSubclass(); + NormalSensor normal = new NormalSensor(); + PostSensorSubclass post = new PostSensorSubclass(); - ScannerExtensionDictionnary selector = newSelector(analyze, post, pre); - List extensions = Lists.newArrayList(selector.select(BatchExtension.class, null, true, null)); + ScannerExtensionDictionnary selector = newSelector(normal, post, pre); + List extensions = Lists.newArrayList(selector.select(org.sonar.api.batch.sensor.Sensor.class, null, true, null)); - assertThat(extensions).containsExactly(pre, analyze, post); + assertThat(extensions).containsExactly(pre, normal, post); } @Test @@ -428,8 +441,28 @@ public class ScannerExtensionDictionnaryTest { } } + class NormalSensor implements org.sonar.api.batch.sensor.Sensor { + + @Override + public void describe(SensorDescriptor descriptor) { + } + + @Override + public void execute(org.sonar.api.batch.sensor.SensorContext context) { + } + + } + @Phase(name = Phase.Name.PRE) - class PreSensor implements BatchExtension { + class PreSensor implements org.sonar.api.batch.sensor.Sensor { + + @Override + public void describe(SensorDescriptor descriptor) { + } + + @Override + public void execute(org.sonar.api.batch.sensor.SensorContext context) { + } } @@ -438,7 +471,15 @@ public class ScannerExtensionDictionnaryTest { } @Phase(name = Phase.Name.POST) - class PostSensor implements BatchExtension { + class PostSensor implements org.sonar.api.batch.sensor.Sensor { + + @Override + public void describe(SensorDescriptor descriptor) { + } + + @Override + public void execute(org.sonar.api.batch.sensor.SensorContext context) { + } } @@ -462,4 +503,30 @@ public class ScannerExtensionDictionnaryTest { public void executeOn(Project project, SensorContext context) { } } + + class NormalPostJob implements org.sonar.api.batch.postjob.PostJob { + + @Override + public void describe(PostJobDescriptor descriptor) { + } + + @Override + public void execute(PostJobContext context) { + } + + } + + @Phase(name = Phase.Name.PRE) + class PrePostJob implements org.sonar.api.batch.postjob.PostJob { + + @Override + public void describe(PostJobDescriptor descriptor) { + } + + @Override + public void execute(PostJobContext context) { + } + + } + } -- 2.39.5