]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7389 Improve testability
authorJulien HENRY <julien.henry@sonarsource.com>
Thu, 17 Mar 2016 15:00:37 +0000 (16:00 +0100)
committerJulien HENRY <julien.henry@sonarsource.com>
Thu, 17 Mar 2016 15:00:37 +0000 (16:00 +0100)
plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/CpdTokenizerSensorTest.java [new file with mode: 0644]
sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java
sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.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 (file)
index 0000000..c58f85a
--- /dev/null
@@ -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);
+  }
+
+}
index 46301c089ec9fbf1d7c66c562eb3fc25e5bb780d..82d425841411b682984a9d9ce8517e8ecb789e42 100644 (file)
@@ -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<TokensLine> cpdTokens(String componentKey) {
+    DefaultCpdTokens defaultCpdTokens = sensorStorage.cpdTokensByComponent.get(componentKey);
+    return defaultCpdTokens != null ? defaultCpdTokens.getTokenLines() : null;
+  }
+
   @Override
   public NewHighlighting newHighlighting() {
     return new DefaultHighlighting(sensorStorage);
index 4e70a3e293bf2664802a73f968ad2a98e2f3b9dc..7be3ecc5ba37af15ddebe387a829d8708f2ed13d 100644 (file)
@@ -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));
+  }
 }