diff options
author | Evgeny Mandrikov <mandrikov@gmail.com> | 2012-02-09 21:11:53 +0400 |
---|---|---|
committer | Evgeny Mandrikov <mandrikov@gmail.com> | 2012-02-09 22:48:17 +0400 |
commit | e476635c3816598feefd88517b6f12421b807a0c (patch) | |
tree | f6ab574ee7d62f7a97c27a2c19620af18782422e /plugins/sonar-squid-java-plugin | |
parent | c3bc6982b7e165304e269d7b7ef309a05148be98 (diff) | |
download | sonarqube-e476635c3816598feefd88517b6f12421b807a0c.tar.gz sonarqube-e476635c3816598feefd88517b6f12421b807a0c.zip |
SONAR-3209 Add new batch component - FileLinesContextFactory
* As was discussed with Simon: better to have new beta class
than new beta method.
* This also provides ability to use this factory from Decorator.
Diffstat (limited to 'plugins/sonar-squid-java-plugin')
5 files changed, 32 insertions, 26 deletions
diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/visitor/FileLinesVisitor.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/visitor/FileLinesVisitor.java index 596df2cc277..60ace8723ea 100644 --- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/visitor/FileLinesVisitor.java +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/ast/visitor/FileLinesVisitor.java @@ -20,10 +20,8 @@ package org.sonar.java.ast.visitor; import com.puppycrawl.tools.checkstyle.api.DetailAST; -import org.sonar.api.batch.SquidUtils; import org.sonar.api.measures.CoreMetrics; import org.sonar.api.measures.FileLinesContext; -import org.sonar.api.resources.JavaFile; import org.sonar.plugins.squid.SonarAccessor; import org.sonar.squid.api.SourceFile; import org.sonar.squid.measures.Metric; @@ -56,8 +54,7 @@ public class FileLinesVisitor extends JavaAstVisitor { private void processFile() { SourceFile file = (SourceFile) peekSourceCode(); - JavaFile javaFile = SquidUtils.convertJavaFileKeyFromSquidFormat(file.getKey()); - FileLinesContext measures = sonarAccessor.getSensorContext().createFileLinesContext(javaFile); + FileLinesContext measures = sonarAccessor.createFileLinesContext(file); Source source = getSource(); for (int line = 1; line <= source.getNumberOfLines(); line++) { diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SonarAccessor.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SonarAccessor.java index 0e6b9b661dd..2b11eba119b 100644 --- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SonarAccessor.java +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SonarAccessor.java @@ -19,8 +19,12 @@ */ package org.sonar.plugins.squid; -import org.sonar.api.batch.SensorContext; +import org.sonar.api.batch.SquidUtils; +import org.sonar.api.measures.FileLinesContext; +import org.sonar.api.measures.FileLinesContextFactory; +import org.sonar.api.resources.JavaFile; import org.sonar.squid.api.CodeScanner; +import org.sonar.squid.api.SourceFile; import java.util.Collection; import java.util.Collections; @@ -30,14 +34,10 @@ import java.util.Collections; */ public class SonarAccessor extends CodeScanner { - private SensorContext sensorContext; + private FileLinesContextFactory factory; - public void setSensorContext(SensorContext sensorContext) { - this.sensorContext = sensorContext; - } - - public SensorContext getSensorContext() { - return sensorContext; + public void setFileLinesContextFactory(FileLinesContextFactory factory) { + this.factory = factory; } @Override @@ -45,4 +45,9 @@ public class SonarAccessor extends CodeScanner { return Collections.emptyList(); } + public FileLinesContext createFileLinesContext(SourceFile file) { + JavaFile javaFile = SquidUtils.convertJavaFileKeyFromSquidFormat(file.getKey()); + return factory.createFor(javaFile); + } + } diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidSensor.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidSensor.java index 5f35c1ee39c..68a332285b3 100644 --- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidSensor.java +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidSensor.java @@ -23,6 +23,7 @@ import org.sonar.api.CoreProperties; import org.sonar.api.batch.*; import org.sonar.api.checks.AnnotationCheckFactory; import org.sonar.api.checks.NoSonarFilter; +import org.sonar.api.measures.FileLinesContextFactory; import org.sonar.api.profiles.RulesProfile; import org.sonar.api.resources.InputFile; import org.sonar.api.resources.Java; @@ -45,12 +46,15 @@ public class SquidSensor implements Sensor { private RulesProfile profile; private ProjectClasspath projectClasspath; private ResourceCreationLock lock; + private FileLinesContextFactory fileLinesContextFactory; - public SquidSensor(RulesProfile profile, NoSonarFilter noSonarFilter, ProjectClasspath projectClasspath, ResourceCreationLock lock) { + public SquidSensor(RulesProfile profile, NoSonarFilter noSonarFilter, ProjectClasspath projectClasspath, ResourceCreationLock lock, + FileLinesContextFactory fileLinesContextFactory) { this.noSonarFilter = noSonarFilter; this.profile = profile; this.projectClasspath = projectClasspath; this.lock = lock; + this.fileLinesContextFactory = fileLinesContextFactory; } public boolean shouldExecuteOnProject(Project project) { @@ -74,7 +78,7 @@ public class SquidSensor implements Sensor { AnnotationCheckFactory factory = AnnotationCheckFactory.create(profile, SquidConstants.REPOSITORY_KEY, SquidRuleRepository.getCheckClasses()); SquidExecutor squidExecutor = new SquidExecutor(analyzePropertyAccessors, fieldNamesToExcludeFromLcom4Computation, factory, charset); - squidExecutor.getSquid().register(SonarAccessor.class).setSensorContext(context); + squidExecutor.getSquid().register(SonarAccessor.class).setFileLinesContextFactory(fileLinesContextFactory); squidExecutor.scan(getMainSourceFiles(project), getBytecodeFiles(project)); squidExecutor.save(project, context, noSonarFilter); squidExecutor.flush(); diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/visitor/FileLinesVisitorTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/visitor/FileLinesVisitorTest.java index a4be9d8ff2d..b92e56890ef 100644 --- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/visitor/FileLinesVisitorTest.java +++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/ast/visitor/FileLinesVisitorTest.java @@ -22,9 +22,9 @@ package org.sonar.java.ast.visitor; import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentCaptor; -import org.sonar.api.batch.SensorContext; import org.sonar.api.measures.CoreMetrics; import org.sonar.api.measures.FileLinesContext; +import org.sonar.api.measures.FileLinesContextFactory; import org.sonar.api.resources.Resource; import org.sonar.java.ast.JavaAstScanner; import org.sonar.java.ast.SquidTestUtils; @@ -39,23 +39,23 @@ import static org.mockito.Mockito.*; public class FileLinesVisitorTest { private Squid squid; - private SensorContext context; + private FileLinesContextFactory factory; private FileLinesContext measures; @Before public void setUp() { squid = new Squid(new JavaSquidConfiguration()); - context = mock(SensorContext.class); + factory = mock(FileLinesContextFactory.class); measures = mock(FileLinesContext.class); } @Test public void analyseTestNcloc() { ArgumentCaptor<Resource> resourceCaptor = ArgumentCaptor.forClass(Resource.class); - when(context.createFileLinesContext(resourceCaptor.capture())) + when(factory.createFor(resourceCaptor.capture())) .thenReturn(measures); - squid.register(SonarAccessor.class).setSensorContext(context); + squid.register(SonarAccessor.class).setFileLinesContextFactory(factory); squid.register(JavaAstScanner.class).scanFile(SquidTestUtils.getInputFile("/metrics/ncloc/TestNcloc.java")); assertThat(resourceCaptor.getValue().getKey(), is("[default].TestNcloc")); diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/SquidSensorTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/SquidSensorTest.java index 70a83928884..3a1cb1bb5c2 100644 --- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/SquidSensorTest.java +++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/SquidSensorTest.java @@ -19,11 +19,6 @@ */ package org.sonar.plugins.squid; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.sameInstance; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.*; - import org.apache.commons.configuration.BaseConfiguration; import org.apache.commons.configuration.Configuration; import org.junit.Test; @@ -36,12 +31,17 @@ import java.io.File; import java.util.Arrays; import java.util.Collection; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.sameInstance; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.*; + public class SquidSensorTest { @Test public void testGetBytecodeFiles() { ProjectClasspath projectClasspath = mock(ProjectClasspath.class); when(projectClasspath.getElements()).thenReturn(Arrays.asList(new File("classes"))); - SquidSensor sensor = new SquidSensor(null, null, projectClasspath, null); + SquidSensor sensor = new SquidSensor(null, null, projectClasspath, null, null); Configuration configuration = new BaseConfiguration(); Project project = mock(Project.class); when(project.getConfiguration()).thenReturn(configuration); @@ -56,7 +56,7 @@ public class SquidSensorTest { @Test public void onlyForJava() { - SquidSensor sensor = new SquidSensor(null, null, null, null); + SquidSensor sensor = new SquidSensor(null, null, null, null, null); Project project = mock(Project.class); when(project.getLanguageKey()).thenReturn(Java.KEY).thenReturn("groovy"); assertThat(sensor.shouldExecuteOnProject(project), is(true)); |