From 1447a88043e5280e79344b07a84e9c9994893391 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Thu, 17 Mar 2016 16:00:37 +0100 Subject: [PATCH] SONAR-7389 Improve testability --- .../xoo/lang/CpdTokenizerSensorTest.java | 87 +++++++++++++++++++ .../sensor/internal/SensorContextTester.java | 7 ++ .../internal/SensorContextTesterTest.java | 20 +++++ 3 files changed, 114 insertions(+) create mode 100644 plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/CpdTokenizerSensorTest.java diff --git a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/CpdTokenizerSensorTest.java b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/CpdTokenizerSensorTest.java new file mode 100644 index 00000000000..c58f85a9be7 --- /dev/null +++ b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/CpdTokenizerSensorTest.java @@ -0,0 +1,87 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program 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. + * + * This program 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.xoo.lang; + +import java.io.File; +import java.io.IOException; +import java.io.StringReader; +import org.apache.commons.io.FileUtils; +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.FileMetadata; +import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor; +import org.sonar.api.batch.sensor.internal.SensorContextTester; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.tuple; + +public class CpdTokenizerSensorTest { + + private CpdTokenizerSensor sensor; + private SensorContextTester context; + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + private File baseDir; + + @Before + public void prepare() throws IOException { + baseDir = temp.newFolder(); + sensor = new CpdTokenizerSensor(); + context = SensorContextTester.create(baseDir); + } + + @Test + public void testDescriptor() { + sensor.describe(new DefaultSensorDescriptor()); + } + + @Test + public void testExecution() throws IOException { + String content = "public class Foo {\n\n}"; + + createSourceFile(content); + + sensor.execute(context); + + assertThat(context.cpdTokens("foo:src/foo.xoo")).extracting("value", "startLine", "startUnit", "endUnit") + .containsExactly( + tuple("publicclassFoo{", 1, 1, 4), + tuple("}", 3, 5, 5)); + } + + private void createSourceFile(String content) throws IOException { + File sourceFile = new File(baseDir, "src/foo.xoo"); + FileUtils.write(sourceFile, content); + DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo") + .setLanguage("xoo") + .initMetadata(new FileMetadata().readMetadata(new StringReader(content))); + context.fileSystem().add(inputFile); + } + +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java index 46301c089ec..82d42584141 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java @@ -58,6 +58,7 @@ import org.sonar.api.batch.sensor.measure.NewMeasure; import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure; import org.sonar.api.config.Settings; import org.sonar.api.measures.Metric; +import org.sonar.duplications.internal.pmd.TokensLine; /** * Utility class to help testing {@link Sensor}. @@ -182,6 +183,12 @@ public class SensorContextTester implements SensorContext { return null; } + @CheckForNull + public List cpdTokens(String componentKey) { + DefaultCpdTokens defaultCpdTokens = sensorStorage.cpdTokensByComponent.get(componentKey); + return defaultCpdTokens != null ? defaultCpdTokens.getTokenLines() : null; + } + @Override public NewHighlighting newHighlighting() { return new DefaultHighlighting(sensorStorage); diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java index 4e70a3e293b..7be3ecc5ba3 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java @@ -41,6 +41,7 @@ import org.sonar.api.measures.CoreMetrics; import org.sonar.api.rule.RuleKey; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.tuple; public class SensorContextTesterTest { @@ -192,4 +193,23 @@ public class SensorContextTesterTest { assertThat(tester.conditions("foo:src/Foo.java", CoverageType.IT, 1)).isNull(); assertThat(tester.coveredConditions("foo:src/Foo.java", CoverageType.IT, 1)).isNull(); } + + @Test + public void testCpdTokens() { + assertThat(tester.cpdTokens("foo:src/Foo.java")).isNull(); + DefaultInputFile inputFile = + new DefaultInputFile("foo", "src/Foo.java").initMetadata(new FileMetadata().readMetadata(new StringReader("public class Foo {\n\n}"))); + tester.newCpdTokens() + .onFile(inputFile) + .addToken(inputFile.newRange(0, 6), "public") + .addToken(inputFile.newRange(7, 12), "class") + .addToken(inputFile.newRange(13, 16), "$IDENTIFIER") + .addToken(inputFile.newRange(17, 18), "{") + .addToken(inputFile.newRange(3, 0, 3, 1), "}") + .save(); + assertThat(tester.cpdTokens("foo:src/Foo.java")).extracting("value", "startLine", "startUnit", "endUnit") + .containsExactly( + tuple("publicclass$IDENTIFIER{", 1, 1, 4), + tuple("}", 3, 5, 5)); + } } -- 2.39.5