diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2017-01-17 17:45:09 +0100 |
---|---|---|
committer | Duarte Meneses <duarte.meneses@sonarsource.com> | 2017-01-27 16:26:30 +0100 |
commit | eea589c564a924993e8edba9d8fa9691e756bce4 (patch) | |
tree | c112f6d41d7f4dfeb7ad8d14f895e1b879a68411 /sonar-scanner-engine/src/test/java/org/sonar/scanner/report | |
parent | 211a993bd85b5d12ace1686b133677381da8c597 (diff) | |
download | sonarqube-eea589c564a924993e8edba9d8fa9691e756bce4.tar.gz sonarqube-eea589c564a924993e8edba9d8fa9691e756bce4.zip |
Refactor resources API
Diffstat (limited to 'sonar-scanner-engine/src/test/java/org/sonar/scanner/report')
5 files changed, 176 insertions, 184 deletions
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ComponentsPublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ComponentsPublisherTest.java index abd3a13b8dd..1fbb2ad50b4 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ComponentsPublisherTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ComponentsPublisherTest.java @@ -21,79 +21,88 @@ package org.sonar.scanner.report; import java.io.File; import java.io.IOException; +import java.util.Collections; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.sonar.api.CoreProperties; import org.sonar.api.batch.bootstrap.ProjectDefinition; +import org.sonar.api.batch.fs.InputFile.Type; import org.sonar.api.batch.fs.internal.DefaultInputDir; +import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.fs.internal.DefaultInputModule; +import org.sonar.api.batch.fs.internal.InputModuleHierarchy; import org.sonar.api.batch.fs.internal.TestInputFileBuilder; -import org.sonar.api.resources.Directory; -import org.sonar.api.resources.Project; import org.sonar.api.utils.DateUtils; -import org.sonar.scanner.FakeJava; -import org.sonar.scanner.index.BatchComponentCache; +import org.sonar.scanner.ProjectAnalysisInfo; import org.sonar.scanner.protocol.output.FileStructure; import org.sonar.scanner.protocol.output.ScannerReport.Component; import org.sonar.scanner.protocol.output.ScannerReport.ComponentLink.ComponentLinkType; import org.sonar.scanner.report.ComponentsPublisher; -import org.sonar.scanner.scan.ImmutableProjectReactor; +import org.sonar.scanner.scan.DefaultComponentTree; import org.sonar.scanner.protocol.output.ScannerReportReader; import org.sonar.scanner.protocol.output.ScannerReportWriter; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class ComponentsPublisherTest { - @Rule public TemporaryFolder temp = new TemporaryFolder(); - BatchComponentCache resourceCache = new BatchComponentCache(); + private DefaultComponentTree tree; + private InputModuleHierarchy moduleHierarchy; + private File outputDir; + private ScannerReportWriter writer; + + @Before + public void setUp() throws IOException { + tree = new DefaultComponentTree(); + outputDir = temp.newFolder(); + writer = new ScannerReportWriter(outputDir); + } @Test public void add_components_to_report() throws Exception { + ProjectAnalysisInfo projectAnalysisInfo = mock(ProjectAnalysisInfo.class); + when(projectAnalysisInfo.analysisDate()).thenReturn(DateUtils.parseDate("2012-12-12")); + + ProjectDefinition rootDef = ProjectDefinition.create() + .setKey("foo") + .setProperty(CoreProperties.PROJECT_VERSION_PROPERTY, "1.0") + .setName("Root project") + .setDescription("Root description"); + DefaultInputModule root = new DefaultInputModule(rootDef, 1); + + ProjectDefinition module1Def = ProjectDefinition.create() + .setKey("module1") + .setName("Module1") + .setDescription("Module description"); + rootDef.addSubProject(module1Def); + + DefaultInputModule module1 = new DefaultInputModule(module1Def, 2); + + moduleHierarchy = mock(InputModuleHierarchy.class); + when(moduleHierarchy.root()).thenReturn(root); + when(moduleHierarchy.children(root)).thenReturn(Collections.singleton(module1)); + tree.index(module1, root); + + DefaultInputDir dir = new DefaultInputDir("module1", "src", 3); + tree.index(dir, module1); + + DefaultInputFile file = new TestInputFileBuilder("module1", "src/Foo.java", 4).setLines(2).build(); + tree.index(file, dir); + + DefaultInputFile fileWithoutLang = new TestInputFileBuilder("module1", "src/make", 5).setLines(10).build(); + tree.index(fileWithoutLang, dir); - ProjectDefinition rootDef = ProjectDefinition.create().setKey("foo"); - rootDef.properties().put(CoreProperties.PROJECT_VERSION_PROPERTY, "1.0"); - Project root = new Project("foo").setName("Root project").setDescription("Root description") - .setAnalysisDate(DateUtils.parseDate(("2012-12-12"))); - root.setId(1).setUuid("PROJECT_UUID"); - resourceCache.add(root, null).setInputComponent(new DefaultInputModule("foo")); - - Project module1 = new Project("module1").setName("Module1").setDescription("Module description"); - module1.setParent(root); - module1.setId(2).setUuid("MODULE_UUID"); - resourceCache.add(module1, root).setInputComponent(new DefaultInputModule("module1")); - rootDef.addSubProject(ProjectDefinition.create().setKey("module1")); - - Directory dir = Directory.create("src"); - dir.setEffectiveKey("module1:src"); - dir.setId(3).setUuid("DIR_UUID"); - resourceCache.add(dir, module1).setInputComponent(new DefaultInputDir("foo", "src")); - - org.sonar.api.resources.File file = org.sonar.api.resources.File.create("src/Foo.java", FakeJava.INSTANCE, false); - file.setEffectiveKey("module1:src/Foo.java"); - file.setId(4).setUuid("FILE_UUID"); - resourceCache.add(file, dir).setInputComponent(new TestInputFileBuilder("module1", "src/Foo.java").setLines(2).build()); - - org.sonar.api.resources.File fileWithoutLang = org.sonar.api.resources.File.create("src/make", null, false); - fileWithoutLang.setEffectiveKey("module1:src/make"); - fileWithoutLang.setId(5).setUuid("FILE_WITHOUT_LANG_UUID"); - resourceCache.add(fileWithoutLang, dir).setInputComponent(new TestInputFileBuilder("module1", "src/make").setLines(10).build()); - - org.sonar.api.resources.File testFile = org.sonar.api.resources.File.create("test/FooTest.java", FakeJava.INSTANCE, true); - testFile.setEffectiveKey("module1:test/FooTest.java"); - testFile.setId(6).setUuid("TEST_FILE_UUID"); - resourceCache.add(testFile, dir).setInputComponent(new TestInputFileBuilder("module1", "test/FooTest.java").setLines(4).build()); - - ImmutableProjectReactor reactor = new ImmutableProjectReactor(rootDef); - - ComponentsPublisher publisher = new ComponentsPublisher(reactor, resourceCache); - - File outputDir = temp.newFolder(); - ScannerReportWriter writer = new ScannerReportWriter(outputDir); + DefaultInputFile testFile = new TestInputFileBuilder("module1", "test/FooTest.java", 6).setType(Type.TEST).setLines(4).build(); + tree.index(testFile, dir); + + ComponentsPublisher publisher = new ComponentsPublisher(moduleHierarchy, tree); publisher.publish(writer); assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 1)).isTrue(); @@ -121,44 +130,38 @@ public class ComponentsPublisherTest { @Test public void add_components_without_version_and_name() throws IOException { - ProjectDefinition rootDef = ProjectDefinition.create().setKey("foo"); - Project root = new Project("foo").setDescription("Root description") - .setAnalysisDate(DateUtils.parseDate(("2012-12-12"))); - root.setId(1).setUuid("PROJECT_UUID"); - resourceCache.add(root, null).setInputComponent(new DefaultInputModule("foo")); - - Project module1 = new Project("module1").setDescription("Module description"); - module1.setParent(root); - module1.setId(2).setUuid("MODULE_UUID"); - resourceCache.add(module1, root).setInputComponent(new DefaultInputModule("module1")); - rootDef.addSubProject(ProjectDefinition.create().setKey("module1")); - - Directory dir = Directory.create("src"); - dir.setEffectiveKey("module1:src"); - dir.setId(3).setUuid("DIR_UUID"); - resourceCache.add(dir, module1).setInputComponent(new DefaultInputDir("foo", "src")); - - org.sonar.api.resources.File file = org.sonar.api.resources.File.create("src/Foo.java", FakeJava.INSTANCE, false); - file.setEffectiveKey("module1:src/Foo.java"); - file.setId(4).setUuid("FILE_UUID"); - resourceCache.add(file, dir).setInputComponent(new TestInputFileBuilder("module1", "src/Foo.java").setLines(2).build()); - - org.sonar.api.resources.File fileWithoutLang = org.sonar.api.resources.File.create("src/make", null, false); - fileWithoutLang.setEffectiveKey("module1:src/make"); - fileWithoutLang.setId(5).setUuid("FILE_WITHOUT_LANG_UUID"); - resourceCache.add(fileWithoutLang, dir).setInputComponent(new TestInputFileBuilder("module1", "src/make").setLines(10).build()); - - org.sonar.api.resources.File testFile = org.sonar.api.resources.File.create("test/FooTest.java", FakeJava.INSTANCE, true); - testFile.setEffectiveKey("module1:test/FooTest.java"); - testFile.setId(6).setUuid("TEST_FILE_UUID"); - resourceCache.add(testFile, dir).setInputComponent(new TestInputFileBuilder("module1", "test/FooTest.java").setLines(4).build()); - - ImmutableProjectReactor reactor = new ImmutableProjectReactor(rootDef); - - ComponentsPublisher publisher = new ComponentsPublisher(reactor, resourceCache); - - File outputDir = temp.newFolder(); - ScannerReportWriter writer = new ScannerReportWriter(outputDir); + ProjectAnalysisInfo projectAnalysisInfo = mock(ProjectAnalysisInfo.class); + when(projectAnalysisInfo.analysisDate()).thenReturn(DateUtils.parseDate("2012-12-12")); + + ProjectDefinition rootDef = ProjectDefinition.create() + .setKey("foo") + .setDescription("Root description"); + DefaultInputModule root = new DefaultInputModule(rootDef, 1); + + ProjectDefinition module1Def = ProjectDefinition.create() + .setKey("module1") + .setDescription("Module description"); + rootDef.addSubProject(module1Def); + DefaultInputModule module1 = new DefaultInputModule(module1Def, 2); + + moduleHierarchy = mock(InputModuleHierarchy.class); + when(moduleHierarchy.root()).thenReturn(root); + when(moduleHierarchy.children(root)).thenReturn(Collections.singleton(module1)); + tree.index(module1, root); + + DefaultInputDir dir = new DefaultInputDir("module1", "src", 3); + tree.index(dir, module1); + + DefaultInputFile file = new TestInputFileBuilder("module1", "src/Foo.java", 4).setLines(2).build(); + tree.index(file, dir); + + DefaultInputFile fileWithoutLang = new TestInputFileBuilder("module1", "src/make", 5).setLines(10).build(); + tree.index(fileWithoutLang, dir); + + DefaultInputFile testFile = new TestInputFileBuilder("module1", "test/FooTest.java", 6).setType(Type.TEST).setLines(4).build(); + tree.index(testFile, dir); + + ComponentsPublisher publisher = new ComponentsPublisher(moduleHierarchy, tree); publisher.publish(writer); assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 1)).isTrue(); @@ -188,40 +191,38 @@ public class ComponentsPublisherTest { @Test public void add_components_with_links_and_branch() throws Exception { - // inputs - ProjectDefinition rootDef = ProjectDefinition.create().setKey("foo"); - rootDef.properties().put(CoreProperties.PROJECT_VERSION_PROPERTY, "1.0"); - Project root = new Project("foo:my_branch").setName("Root project") - .setAnalysisDate(DateUtils.parseDate(("2012-12-12"))); - root.setId(1).setUuid("PROJECT_UUID"); - resourceCache.add(root, null).setInputComponent(new DefaultInputModule("foo")); - rootDef.properties().put(CoreProperties.LINKS_HOME_PAGE, "http://home"); - rootDef.properties().put(CoreProperties.PROJECT_BRANCH_PROPERTY, "my_branch"); - - Project module1 = new Project("module1:my_branch").setName("Module1"); - module1.setParent(root); - module1.setId(2).setUuid("MODULE_UUID"); - resourceCache.add(module1, root).setInputComponent(new DefaultInputModule("module1")); - ProjectDefinition moduleDef = ProjectDefinition.create().setKey("module1"); - moduleDef.properties().put(CoreProperties.LINKS_CI, "http://ci"); - rootDef.addSubProject(moduleDef); - - Directory dir = Directory.create("src"); - dir.setEffectiveKey("module1:my_branch:my_branch:src"); - dir.setId(3).setUuid("DIR_UUID"); - resourceCache.add(dir, module1).setInputComponent(new DefaultInputDir("foo", "src")); - - org.sonar.api.resources.File file = org.sonar.api.resources.File.create("src/Foo.java", FakeJava.INSTANCE, false); - file.setEffectiveKey("module1:my_branch:my_branch:src/Foo.java"); - file.setId(4).setUuid("FILE_UUID"); - resourceCache.add(file, dir).setInputComponent(new TestInputFileBuilder("module1", "src/Foo.java").setLines(2).build()); - - ImmutableProjectReactor reactor = new ImmutableProjectReactor(rootDef); - - ComponentsPublisher publisher = new ComponentsPublisher(reactor, resourceCache); - - File outputDir = temp.newFolder(); - ScannerReportWriter writer = new ScannerReportWriter(outputDir); + ProjectAnalysisInfo projectAnalysisInfo = mock(ProjectAnalysisInfo.class); + when(projectAnalysisInfo.analysisDate()).thenReturn(DateUtils.parseDate("2012-12-12")); + + ProjectDefinition rootDef = ProjectDefinition.create() + .setKey("foo") + .setProperty(CoreProperties.PROJECT_VERSION_PROPERTY, "1.0") + .setProperty(CoreProperties.PROJECT_BRANCH_PROPERTY, "my_branch") + .setName("Root project") + .setProperty(CoreProperties.LINKS_HOME_PAGE, "http://home") + .setDescription("Root description"); + DefaultInputModule root = new DefaultInputModule(rootDef, 1); + + ProjectDefinition module1Def = ProjectDefinition.create() + .setKey("module1") + .setName("Module1") + .setProperty(CoreProperties.LINKS_CI, "http://ci") + .setDescription("Module description"); + rootDef.addSubProject(module1Def); + DefaultInputModule module1 = new DefaultInputModule(module1Def, 2); + + moduleHierarchy = mock(InputModuleHierarchy.class); + when(moduleHierarchy.root()).thenReturn(root); + when(moduleHierarchy.children(root)).thenReturn(Collections.singleton(module1)); + tree.index(module1, root); + + DefaultInputDir dir = new DefaultInputDir("module1", "src", 3); + tree.index(dir, module1); + + DefaultInputFile file = new TestInputFileBuilder("module1", "src/Foo.java", 4).setLines(2).build(); + tree.index(file, dir); + + ComponentsPublisher publisher = new ComponentsPublisher(moduleHierarchy, tree); publisher.publish(writer); ScannerReportReader reader = new ScannerReportReader(outputDir); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/CoveragePublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/CoveragePublisherTest.java index 067169696e4..2f141e94fbf 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/CoveragePublisherTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/CoveragePublisherTest.java @@ -20,21 +20,20 @@ package org.sonar.scanner.report; import java.io.File; -import java.util.Date; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.fs.internal.DefaultInputModule; import org.sonar.api.batch.fs.internal.TestInputFileBuilder; import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure; import org.sonar.api.measures.CoreMetrics; -import org.sonar.api.resources.Project; import org.sonar.core.util.CloseableIterator; -import org.sonar.scanner.index.BatchComponentCache; import org.sonar.scanner.protocol.output.ScannerReport.LineCoverage; import org.sonar.scanner.protocol.output.ScannerReportReader; import org.sonar.scanner.protocol.output.ScannerReportWriter; +import org.sonar.scanner.scan.filesystem.InputComponentStore; import org.sonar.scanner.scan.measure.MeasureCache; import static org.assertj.core.api.Assertions.assertThat; @@ -50,18 +49,19 @@ public class CoveragePublisherTest { private MeasureCache measureCache; private CoveragePublisher publisher; - private org.sonar.api.resources.Resource sampleFile; + private InputComponentStore componentCache; + private DefaultInputFile inputFile; @Before public void prepare() { - Project p = new Project("foo").setAnalysisDate(new Date(1234567L)); - BatchComponentCache resourceCache = new BatchComponentCache(); - sampleFile = org.sonar.api.resources.File.create("src/Foo.php").setEffectiveKey("foo:src/Foo.php"); - resourceCache.add(p, null).setInputComponent(new DefaultInputModule("foo")); - resourceCache.add(sampleFile, null).setInputComponent(new TestInputFileBuilder("foo", "src/Foo.php").setLines(5).build()); + inputFile = new TestInputFileBuilder("foo", "src/Foo.php").setLines(5).build(); + componentCache = new InputComponentStore(); + componentCache.put(new DefaultInputModule("foo")); + componentCache.put(inputFile); + measureCache = mock(MeasureCache.class); when(measureCache.byMetric(anyString(), anyString())).thenReturn(null); - publisher = new CoveragePublisher(resourceCache, measureCache); + publisher = new CoveragePublisher(componentCache, measureCache); } @Test @@ -81,7 +81,7 @@ public class CoveragePublisherTest { publisher.publish(writer); - try (CloseableIterator<LineCoverage> it = new ScannerReportReader(outputDir).readComponentCoverage(2)) { + try (CloseableIterator<LineCoverage> it = new ScannerReportReader(outputDir).readComponentCoverage(inputFile.batchId())) { assertThat(it.next()).isEqualTo(LineCoverage.newBuilder() .setLine(2) .setHits(true) diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MeasuresPublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MeasuresPublisherTest.java index 6ee89c3e5b9..f6caf2f6bbd 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MeasuresPublisherTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MeasuresPublisherTest.java @@ -20,25 +20,25 @@ package org.sonar.scanner.report; import java.io.File; +import java.io.IOException; import java.util.Collections; -import java.util.Date; import org.apache.commons.lang.exception.ExceptionUtils; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; +import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.fs.internal.DefaultInputModule; import org.sonar.api.batch.fs.internal.TestInputFileBuilder; import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure; import org.sonar.api.measures.CoreMetrics; -import org.sonar.api.resources.Project; import org.sonar.core.util.CloseableIterator; import org.sonar.scanner.deprecated.test.TestPlanBuilder; -import org.sonar.scanner.index.BatchComponentCache; import org.sonar.scanner.protocol.output.ScannerReport; import org.sonar.scanner.protocol.output.ScannerReportReader; import org.sonar.scanner.protocol.output.ScannerReportWriter; +import org.sonar.scanner.scan.filesystem.InputComponentStore; import org.sonar.scanner.scan.measure.MeasureCache; import static java.util.Arrays.asList; @@ -61,18 +61,22 @@ public class MeasuresPublisherTest { private MeasureCache measureCache; private MeasuresPublisher publisher; - private org.sonar.api.resources.Resource sampleFile; + private InputComponentStore componentCache; + private File outputDir; + private ScannerReportWriter writer; + private DefaultInputFile inputFile; @Before - public void prepare() { - Project p = new Project("foo").setAnalysisDate(new Date(1234567L)); - BatchComponentCache resourceCache = new BatchComponentCache(); - sampleFile = org.sonar.api.resources.File.create("src/Foo.php").setEffectiveKey(FILE_KEY); - resourceCache.add(p, null).setInputComponent(new DefaultInputModule("foo")); - resourceCache.add(sampleFile, null).setInputComponent(new TestInputFileBuilder("foo", "src/Foo.php").build()); + public void prepare() throws IOException { + inputFile = new TestInputFileBuilder("foo", "src/Foo.php").build(); + componentCache = new InputComponentStore(); + componentCache.put(new DefaultInputModule("foo")); + componentCache.put(inputFile); measureCache = mock(MeasureCache.class); when(measureCache.byComponentKey(anyString())).thenReturn(Collections.<DefaultMeasure<?>>emptyList()); - publisher = new MeasuresPublisher(resourceCache, measureCache, mock(TestPlanBuilder.class)); + publisher = new MeasuresPublisher(componentCache, measureCache, mock(TestPlanBuilder.class)); + outputDir = temp.newFolder(); + writer = new ScannerReportWriter(outputDir); } @Test @@ -84,15 +88,11 @@ public class MeasuresPublisherTest { .withValue("foo bar"); when(measureCache.byComponentKey(FILE_KEY)).thenReturn(asList(measure, stringMeasure)); - File outputDir = temp.newFolder(); - ScannerReportWriter writer = new ScannerReportWriter(outputDir); - publisher.publish(writer); - ScannerReportReader reader = new ScannerReportReader(outputDir); assertThat(reader.readComponentMeasures(1)).hasSize(0); - try (CloseableIterator<ScannerReport.Measure> componentMeasures = reader.readComponentMeasures(2)) { + try (CloseableIterator<ScannerReport.Measure> componentMeasures = reader.readComponentMeasures(inputFile.batchId())) { assertThat(componentMeasures).hasSize(2); } } @@ -102,9 +102,6 @@ public class MeasuresPublisherTest { DefaultMeasure<Integer> measure = new DefaultMeasure<Integer>().forMetric(CoreMetrics.LINES_TO_COVER); when(measureCache.byComponentKey(FILE_KEY)).thenReturn(Collections.singletonList(measure)); - File outputDir = temp.newFolder(); - ScannerReportWriter writer = new ScannerReportWriter(outputDir); - try { publisher.publish(writer); fail(); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java index 95dc51a8aef..6e5ee36b182 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java @@ -29,14 +29,15 @@ import org.sonar.api.CoreProperties; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.config.MapSettings; import org.sonar.api.config.Settings; -import org.sonar.api.resources.Project; -import org.sonar.scanner.index.BatchComponentCache; +import org.sonar.api.batch.fs.internal.DefaultInputModule; +import org.sonar.api.batch.fs.internal.InputModuleHierarchy; +import org.sonar.api.batch.fs.internal.TestInputFileBuilder; +import org.sonar.scanner.ProjectAnalysisInfo; import org.sonar.scanner.protocol.output.ScannerReport; import org.sonar.scanner.protocol.output.ScannerReportReader; import org.sonar.scanner.protocol.output.ScannerReportWriter; import org.sonar.scanner.rule.ModuleQProfiles; import org.sonar.scanner.rule.QProfile; -import org.sonar.scanner.scan.ImmutableProjectReactor; import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat; @@ -50,22 +51,24 @@ public class MetadataPublisherTest { public TemporaryFolder temp = new TemporaryFolder(); private ProjectDefinition projectDef; - private Project project; + private DefaultInputModule rootModule; private MetadataPublisher underTest; private Settings settings; private ModuleQProfiles qProfiles; + private ProjectAnalysisInfo projectAnalysisInfo; + private InputModuleHierarchy inputModuleHierarchy; @Before public void prepare() { projectDef = ProjectDefinition.create().setKey("foo"); - project = new Project("foo").setAnalysisDate(new Date(1234567L)); - BatchComponentCache componentCache = new BatchComponentCache(); - org.sonar.api.resources.Resource sampleFile = org.sonar.api.resources.File.create("src/Foo.php").setEffectiveKey("foo:src/Foo.php"); - componentCache.add(project, null); - componentCache.add(sampleFile, project); + rootModule = new DefaultInputModule(projectDef, TestInputFileBuilder.batchId++); + projectAnalysisInfo = mock(ProjectAnalysisInfo.class); + when(projectAnalysisInfo.analysisDate()).thenReturn(new Date(1234567L)); + inputModuleHierarchy = mock(InputModuleHierarchy.class); + when(inputModuleHierarchy.root()).thenReturn(rootModule); settings = new MapSettings(); qProfiles = mock(ModuleQProfiles.class); - underTest = new MetadataPublisher(componentCache, new ImmutableProjectReactor(projectDef), settings, qProfiles); + underTest = new MetadataPublisher(projectAnalysisInfo, inputModuleHierarchy, settings, qProfiles); } @Test @@ -101,8 +104,7 @@ public class MetadataPublisherTest { settings.setProperty(CoreProperties.CPD_CROSS_PROJECT, "true"); settings.setProperty(CoreProperties.PROJECT_BRANCH_PROPERTY, "myBranch"); projectDef.properties().put(CoreProperties.PROJECT_BRANCH_PROPERTY, "myBranch"); - project.setKey("foo:myBranch"); - project.setEffectiveKey("foo:myBranch"); + projectDef.setKey("foo"); File outputDir = temp.newFolder(); ScannerReportWriter writer = new ScannerReportWriter(outputDir); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/SourcePublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/SourcePublisherTest.java index 0ecc47a1088..8044f49d440 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/SourcePublisherTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/SourcePublisherTest.java @@ -22,19 +22,17 @@ package org.sonar.scanner.report; import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; -import java.util.Date; import org.apache.commons.io.FileUtils; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.fs.internal.DefaultInputModule; import org.sonar.api.batch.fs.internal.TestInputFileBuilder; -import org.sonar.api.resources.Project; -import org.sonar.api.resources.Qualifiers; -import org.sonar.scanner.index.BatchComponentCache; import org.sonar.scanner.protocol.output.ScannerReportWriter; import org.sonar.scanner.report.SourcePublisher; +import org.sonar.scanner.scan.filesystem.InputComponentStore; import static org.assertj.core.api.Assertions.assertThat; @@ -42,31 +40,25 @@ public class SourcePublisherTest { @Rule public TemporaryFolder temp = new TemporaryFolder(); - private SourcePublisher publisher; - private File sourceFile; - private ScannerReportWriter writer; - - private org.sonar.api.resources.File sampleFile; + private DefaultInputFile inputFile; + private InputComponentStore componentStore; @Before public void prepare() throws IOException { - Project p = new Project("foo").setAnalysisDate(new Date(1234567L)); - BatchComponentCache resourceCache = new BatchComponentCache(); - sampleFile = org.sonar.api.resources.File.create("src/Foo.php"); - sampleFile.setEffectiveKey("foo:src/Foo.php"); - resourceCache.add(p, null).setInputComponent(new DefaultInputModule("foo")); File baseDir = temp.newFolder(); sourceFile = new File(baseDir, "src/Foo.php"); - resourceCache.add(sampleFile, null).setInputComponent( - new TestInputFileBuilder("foo", "src/Foo.php") + inputFile = new TestInputFileBuilder("foo", "src/Foo.php") .setLines(5) .setModuleBaseDir(baseDir.toPath()) .setCharset(StandardCharsets.ISO_8859_1) - .build()); - publisher = new SourcePublisher(resourceCache); + .build(); + componentStore = new InputComponentStore(); + componentStore.put(new DefaultInputModule("foo")); + componentStore.put(inputFile); + publisher = new SourcePublisher(componentStore); File outputDir = temp.newFolder(); writer = new ScannerReportWriter(outputDir); } @@ -77,7 +69,7 @@ public class SourcePublisherTest { publisher.publish(writer); - File out = writer.getSourceFile(2); + File out = writer.getSourceFile(inputFile.batchId()); assertThat(FileUtils.readFileToString(out, StandardCharsets.UTF_8)).isEqualTo(""); } @@ -87,18 +79,18 @@ public class SourcePublisherTest { publisher.publish(writer); - File out = writer.getSourceFile(2); + File out = writer.getSourceFile(inputFile.batchId()); assertThat(FileUtils.readFileToString(out, StandardCharsets.UTF_8)).isEqualTo("1\n2\n3\n4\n"); } @Test public void publishTestSource() throws Exception { FileUtils.write(sourceFile, "1\n2\n3\n4\n", StandardCharsets.ISO_8859_1); - sampleFile.setQualifier(Qualifiers.UNIT_TEST_FILE); + // sampleFile.setQualifier(Qualifiers.UNIT_TEST_FILE); publisher.publish(writer); - File out = writer.getSourceFile(2); + File out = writer.getSourceFile(inputFile.batchId()); assertThat(FileUtils.readFileToString(out, StandardCharsets.UTF_8)).isEqualTo("1\n2\n3\n4\n"); } @@ -108,7 +100,7 @@ public class SourcePublisherTest { publisher.publish(writer); - File out = writer.getSourceFile(2); + File out = writer.getSourceFile(inputFile.batchId()); assertThat(FileUtils.readFileToString(out, StandardCharsets.UTF_8)).isEqualTo("1\n2\n3\n4\n5"); } @@ -118,7 +110,7 @@ public class SourcePublisherTest { publisher.publish(writer); - File out = writer.getSourceFile(2); + File out = writer.getSourceFile(inputFile.batchId()); assertThat(FileUtils.readFileToString(out, StandardCharsets.UTF_8)).isEqualTo("\n2\n3\n4\n5"); } } |