diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2014-07-24 17:16:41 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2014-07-24 17:25:10 +0200 |
commit | 2a510a2f76d2f384deba2eec2ca6a9297c0ebc63 (patch) | |
tree | bef0154a601465671ce3a2f074da3a4d09140b37 /sonar-batch/src/test/java | |
parent | ecea6110fad63f12345422d0e868f9b89ec6dc70 (diff) | |
download | sonarqube-2a510a2f76d2f384deba2eec2ca6a9297c0ebc63.tar.gz sonarqube-2a510a2f76d2f384deba2eec2ca6a9297c0ebc63.zip |
SONAR-5389 Port symbolizable API to new batch API
Diffstat (limited to 'sonar-batch/src/test/java')
7 files changed, 303 insertions, 68 deletions
diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/highlighting/HighlightingMediumTest.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/highlighting/HighlightingMediumTest.java index ab8e526fe68..259b2ffcec0 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/highlighting/HighlightingMediumTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/highlighting/HighlightingMediumTest.java @@ -82,10 +82,10 @@ public class HighlightingMediumTest { .start(); InputFile file = result.inputFiles().get(0); - assertThat(result.highlightingTypeAt(file, 0)).isEqualTo(HighlightingBuilder.TypeOfText.STRING); - assertThat(result.highlightingTypeAt(file, 9)).isEqualTo(HighlightingBuilder.TypeOfText.STRING); - assertThat(result.highlightingTypeAt(file, 10)).isNull(); - assertThat(result.highlightingTypeAt(file, 11)).isEqualTo(HighlightingBuilder.TypeOfText.KEYWORD); + assertThat(result.highlightingTypeFor(file, 0)).isEqualTo(HighlightingBuilder.TypeOfText.STRING); + assertThat(result.highlightingTypeFor(file, 9)).isEqualTo(HighlightingBuilder.TypeOfText.STRING); + assertThat(result.highlightingTypeFor(file, 10)).isNull(); + assertThat(result.highlightingTypeFor(file, 11)).isEqualTo(HighlightingBuilder.TypeOfText.KEYWORD); } diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/symbol/SymbolMediumTest.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/symbol/SymbolMediumTest.java new file mode 100644 index 00000000000..c3875c625f3 --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/symbol/SymbolMediumTest.java @@ -0,0 +1,87 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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 this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.batch.mediumtest.symbol; + +import com.google.common.collect.ImmutableMap; +import org.apache.commons.io.FileUtils; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.sonar.api.batch.fs.InputFile; +import org.sonar.batch.mediumtest.BatchMediumTester; +import org.sonar.batch.mediumtest.BatchMediumTester.TaskResult; +import org.sonar.batch.mediumtest.xoo.plugin.XooPlugin; + +import java.io.File; +import java.io.IOException; + +import static org.fest.assertions.Assertions.assertThat; + +public class SymbolMediumTest { + + @org.junit.Rule + public TemporaryFolder temp = new TemporaryFolder(); + + public BatchMediumTester tester = BatchMediumTester.builder() + .registerPlugin("xoo", new XooPlugin()) + .addDefaultQProfile("xoo", "Sonar Way") + .bootstrapProperties(ImmutableMap.of("sonar.analysis.mode", "sensor")) + .build(); + + @Before + public void prepare() { + tester.start(); + } + + @After + public void stop() { + tester.stop(); + } + + @Test + public void computeSyntaxHighlightingOnTempProject() throws IOException { + + File baseDir = temp.newFolder(); + File srcDir = new File(baseDir, "src"); + srcDir.mkdir(); + + File xooFile = new File(srcDir, "sample.xoo"); + File xooSymbolFile = new File(srcDir, "sample.xoo.symbol"); + FileUtils.write(xooFile, "Sample xoo\ncontent\nanother xoo"); + FileUtils.write(xooSymbolFile, "7,10,27"); + + TaskResult result = tester.newTask() + .properties(ImmutableMap.<String, String>builder() + .put("sonar.task", "scan") + .put("sonar.projectBaseDir", baseDir.getAbsolutePath()) + .put("sonar.projectKey", "com.foo.project") + .put("sonar.projectName", "Foo Project") + .put("sonar.projectVersion", "1.0-SNAPSHOT") + .put("sonar.projectDescription", "Description of Foo Project") + .put("sonar.sources", "src") + .build()) + .start(); + + InputFile file = result.inputFiles().get(0); + assertThat(result.symbolReferencesFor(file, 7, 10)).containsOnly(7, 27); + } + +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/xoo/plugin/XooPlugin.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/xoo/plugin/XooPlugin.java index 43e4c4a04b6..78718da3682 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/xoo/plugin/XooPlugin.java +++ b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/xoo/plugin/XooPlugin.java @@ -23,6 +23,7 @@ import org.sonar.api.SonarPlugin; import org.sonar.batch.mediumtest.xoo.plugin.base.Xoo; import org.sonar.batch.mediumtest.xoo.plugin.lang.MeasureSensor; import org.sonar.batch.mediumtest.xoo.plugin.lang.ScmActivitySensor; +import org.sonar.batch.mediumtest.xoo.plugin.lang.SymbolReferencesSensor; import org.sonar.batch.mediumtest.xoo.plugin.lang.SyntaxHighlightingSensor; import org.sonar.batch.mediumtest.xoo.plugin.rule.CreateIssueByInternalKeySensor; import org.sonar.batch.mediumtest.xoo.plugin.rule.OneIssueOnDirPerFileSensor; @@ -40,6 +41,7 @@ public final class XooPlugin extends SonarPlugin { MeasureSensor.class, ScmActivitySensor.class, SyntaxHighlightingSensor.class, + SymbolReferencesSensor.class, Xoo.class, // sensors diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/xoo/plugin/lang/SymbolReferencesSensor.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/xoo/plugin/lang/SymbolReferencesSensor.java new file mode 100644 index 00000000000..91fa61e5c78 --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/xoo/plugin/lang/SymbolReferencesSensor.java @@ -0,0 +1,98 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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 this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.batch.mediumtest.xoo.plugin.lang; + +import com.google.common.base.Splitter; +import org.apache.commons.io.FileUtils; +import org.apache.commons.lang.StringUtils; +import org.sonar.api.batch.fs.InputFile; +import org.sonar.api.batch.sensor.Sensor; +import org.sonar.api.batch.sensor.SensorContext; +import org.sonar.api.batch.sensor.SensorDescriptor; +import org.sonar.api.batch.sensor.symbol.Symbol; +import org.sonar.api.batch.sensor.symbol.SymbolTableBuilder; +import org.sonar.api.measures.CoreMetrics; +import org.sonar.batch.mediumtest.xoo.plugin.base.Xoo; +import org.sonar.batch.mediumtest.xoo.plugin.base.XooConstants; + +import java.io.File; +import java.io.IOException; +import java.util.Iterator; +import java.util.List; + +/** + * Parse files *.xoo.symbol + */ +public class SymbolReferencesSensor implements Sensor { + + private static final String SYMBOL_EXTENSION = ".symbol"; + + private void processFileHighlighting(InputFile inputFile, SensorContext context) { + File ioFile = inputFile.file(); + File symbolFile = new File(ioFile.getParentFile(), ioFile.getName() + SYMBOL_EXTENSION); + if (symbolFile.exists()) { + XooConstants.LOG.debug("Processing " + symbolFile.getAbsolutePath()); + try { + List<String> lines = FileUtils.readLines(symbolFile, context.fileSystem().encoding().name()); + int lineNumber = 0; + SymbolTableBuilder symbolTableBuilder = context.symbolTableBuilder(inputFile); + for (String line : lines) { + lineNumber++; + if (StringUtils.isBlank(line)) { + continue; + } + if (line.startsWith("#")) { + continue; + } + try { + Iterator<String> split = Splitter.on(",").split(line).iterator(); + int startOffset = Integer.parseInt(split.next()); + int endOffset = Integer.parseInt(split.next()); + Symbol s = symbolTableBuilder.newSymbol(startOffset, endOffset); + while (split.hasNext()) { + symbolTableBuilder.newReference(s, Integer.parseInt(split.next())); + } + } catch (Exception e) { + throw new IllegalStateException("Error processing line " + lineNumber + " of file " + symbolFile.getAbsolutePath(), e); + } + } + symbolTableBuilder.done(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } + + @Override + public void describe(SensorDescriptor descriptor) { + descriptor + .name("Xoo Highlighting Sensor") + .provides(CoreMetrics.LINES) + .workOnLanguages(Xoo.KEY) + .workOnFileTypes(InputFile.Type.MAIN, InputFile.Type.TEST); + } + + @Override + public void execute(SensorContext context) { + for (InputFile file : context.fileSystem().inputFiles(context.fileSystem().predicates().hasLanguages(Xoo.KEY))) { + processFileHighlighting(file, context); + } + } +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/source/DefaultSymbolTableTest.java b/sonar-batch/src/test/java/org/sonar/batch/source/DefaultSymbolTableTest.java index 2e65c12473b..a1058b27d64 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/source/DefaultSymbolTableTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/source/DefaultSymbolTableTest.java @@ -35,7 +35,7 @@ public class DefaultSymbolTableTest { @Test public void should_order_symbol_and_references() throws Exception { - Symbolizable.SymbolTableBuilder symbolTableBuilder = new DefaultSymbolTable.Builder(); + Symbolizable.SymbolTableBuilder symbolTableBuilder = new DefaultSymbolTable.Builder("foo"); Symbol firstSymbol = symbolTableBuilder.newSymbol(10, 20); symbolTableBuilder.newReference(firstSymbol, 32); Symbol secondSymbol = symbolTableBuilder.newSymbol(84, 92); @@ -54,16 +54,16 @@ public class DefaultSymbolTableTest { public void should_reject_reference_conflicting_with_declaration() throws Exception { throwable.expect(UnsupportedOperationException.class); - Symbolizable.SymbolTableBuilder symbolTableBuilder = new DefaultSymbolTable.Builder(); + Symbolizable.SymbolTableBuilder symbolTableBuilder = new DefaultSymbolTable.Builder("foo"); Symbol symbol = symbolTableBuilder.newSymbol(10, 20); symbolTableBuilder.newReference(symbol, 15); } @Test public void test_toString() throws Exception { - Symbolizable.SymbolTableBuilder symbolTableBuilder = new DefaultSymbolTable.Builder(); + Symbolizable.SymbolTableBuilder symbolTableBuilder = new DefaultSymbolTable.Builder("foo"); Symbol symbol = symbolTableBuilder.newSymbol(10, 20); - assertThat(symbol.toString()).isEqualTo("Symbol{offset=10-20}"); + assertThat(symbol.toString()).isEqualTo("Symbol{component=foo, offset=10-20}"); } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/source/SymbolDataTest.java b/sonar-batch/src/test/java/org/sonar/batch/source/SymbolDataTest.java deleted file mode 100644 index 8dfdd1cccd5..00000000000 --- a/sonar-batch/src/test/java/org/sonar/batch/source/SymbolDataTest.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube 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. - * - * SonarQube 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 this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package org.sonar.batch.source; - -import org.junit.Test; -import org.sonar.api.source.Symbol; -import org.sonar.api.source.Symbolizable; - -import static org.fest.assertions.Assertions.assertThat; - -public class SymbolDataTest { - - @Test - public void should_serialize_symbols_in_natural_order() throws Exception { - - Symbolizable.SymbolTableBuilder symbolTableBuilder = new DefaultSymbolTable.Builder(); - Symbol firstSymbol = symbolTableBuilder.newSymbol(10, 20); - symbolTableBuilder.newReference(firstSymbol, 32); - Symbol secondSymbol = symbolTableBuilder.newSymbol(84, 92); - symbolTableBuilder.newReference(secondSymbol, 124); - Symbol thirdSymbol = symbolTableBuilder.newSymbol(55, 62); - symbolTableBuilder.newReference(thirdSymbol, 70); - Symbolizable.SymbolTable symbolTable = symbolTableBuilder.build(); - - SymbolData dataRepository = new SymbolData(symbolTable); - String serializedSymbolData = dataRepository.writeString(); - - assertThat(serializedSymbolData).isEqualTo("10,20,10,32;55,62,55,70;84,92,84,124;"); - } - - @Test - public void should_serialize_unused_symbol() throws Exception { - - Symbolizable.SymbolTableBuilder symbolTableBuilder = new DefaultSymbolTable.Builder(); - symbolTableBuilder.newSymbol(10, 20); - - SymbolData dataRepository = new SymbolData(symbolTableBuilder.build()); - String serializedSymbolData = dataRepository.writeString(); - - assertThat(serializedSymbolData).isEqualTo("10,20,10;"); - } -} diff --git a/sonar-batch/src/test/java/org/sonar/batch/symbol/DefaultSymbolTableBuilderTest.java b/sonar-batch/src/test/java/org/sonar/batch/symbol/DefaultSymbolTableBuilderTest.java new file mode 100644 index 00000000000..c661da30af1 --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/symbol/DefaultSymbolTableBuilderTest.java @@ -0,0 +1,108 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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 this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.batch.symbol; + +import com.google.common.collect.SortedSetMultimap; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.mockito.ArgumentCaptor; +import org.sonar.api.batch.sensor.symbol.Symbol; +import org.sonar.api.batch.sensor.symbol.SymbolTableBuilder; +import org.sonar.batch.index.ComponentDataCache; +import org.sonar.core.source.SnapshotDataTypes; + +import java.util.ArrayList; + +import static org.fest.assertions.Assertions.assertThat; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +public class DefaultSymbolTableBuilderTest { + + @Rule + public ExpectedException throwable = ExpectedException.none(); + + @Test + public void should_order_symbol_and_references() throws Exception { + ComponentDataCache componentDataCache = mock(ComponentDataCache.class); + SymbolTableBuilder symbolTableBuilder = new DefaultSymbolTableBuilder("foo", componentDataCache); + Symbol firstSymbol = symbolTableBuilder.newSymbol(10, 20); + symbolTableBuilder.newReference(firstSymbol, 32); + Symbol secondSymbol = symbolTableBuilder.newSymbol(84, 92); + symbolTableBuilder.newReference(secondSymbol, 124); + Symbol thirdSymbol = symbolTableBuilder.newSymbol(55, 62); + symbolTableBuilder.newReference(thirdSymbol, 70); + symbolTableBuilder.done(); + + ArgumentCaptor<SymbolData> argCaptor = ArgumentCaptor.forClass(SymbolData.class); + verify(componentDataCache).setData(eq("foo"), eq(SnapshotDataTypes.SYMBOL_HIGHLIGHTING), argCaptor.capture()); + + SortedSetMultimap<Symbol, Integer> referencesBySymbol = argCaptor.getValue().referencesBySymbol(); + + assertThat(new ArrayList<Symbol>(referencesBySymbol.keySet())).containsExactly(firstSymbol, thirdSymbol, secondSymbol); + assertThat(new ArrayList<Integer>(referencesBySymbol.get(firstSymbol))).containsExactly(10, 32); + assertThat(new ArrayList<Integer>(referencesBySymbol.get(secondSymbol))).containsExactly(84, 124); + assertThat(new ArrayList<Integer>(referencesBySymbol.get(thirdSymbol))).containsExactly(55, 70); + + assertThat(argCaptor.getValue().writeString()).isEqualTo("10,20,10,32;55,62,55,70;84,92,84,124;"); + } + + @Test + public void should_serialize_unused_symbol() throws Exception { + + ComponentDataCache componentDataCache = mock(ComponentDataCache.class); + SymbolTableBuilder symbolTableBuilder = new DefaultSymbolTableBuilder("foo", componentDataCache); + symbolTableBuilder.newSymbol(10, 20); + symbolTableBuilder.done(); + + ArgumentCaptor<SymbolData> argCaptor = ArgumentCaptor.forClass(SymbolData.class); + verify(componentDataCache).setData(eq("foo"), eq(SnapshotDataTypes.SYMBOL_HIGHLIGHTING), argCaptor.capture()); + + assertThat(argCaptor.getValue().writeString()).isEqualTo("10,20,10;"); + } + + @Test + public void should_reject_reference_conflicting_with_declaration() throws Exception { + throwable.expect(UnsupportedOperationException.class); + + ComponentDataCache componentDataCache = mock(ComponentDataCache.class); + SymbolTableBuilder symbolTableBuilder = new DefaultSymbolTableBuilder("foo", componentDataCache); + Symbol symbol = symbolTableBuilder.newSymbol(10, 20); + symbolTableBuilder.newReference(symbol, 15); + } + + @Test + public void should_reject_reference_from_another_file() throws Exception { + throwable.expect(UnsupportedOperationException.class); + + ComponentDataCache componentDataCache = mock(ComponentDataCache.class); + SymbolTableBuilder symbolTableBuilder = new DefaultSymbolTableBuilder("foo", componentDataCache); + Symbol symbol = symbolTableBuilder.newSymbol(10, 20); + + SymbolTableBuilder symbolTableBuilder2 = new DefaultSymbolTableBuilder("foo2", componentDataCache); + Symbol symbol2 = symbolTableBuilder2.newSymbol(30, 40); + + symbolTableBuilder.newReference(symbol2, 15); + } + +} |