aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api/src/test/java/org/sonar/api
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2015-02-20 14:57:39 +0100
committerJulien HENRY <julien.henry@sonarsource.com>2015-02-20 15:05:18 +0100
commitee09741a99176e635b310ab295dd898533828d69 (patch)
treeccb54de1e5373e54e9a9feffc2adde4d81ac1a05 /sonar-plugin-api/src/test/java/org/sonar/api
parent38357bb24905509dd775e40bdf09e40aec6af39c (diff)
downloadsonarqube-ee09741a99176e635b310ab295dd898533828d69.tar.gz
sonarqube-ee09741a99176e635b310ab295dd898533828d69.zip
SONAR-5931 Add a tester class for SensorContext
Diffstat (limited to 'sonar-plugin-api/src/test/java/org/sonar/api')
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java158
1 files changed, 158 insertions, 0 deletions
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
new file mode 100644
index 00000000000..090e487ddfe
--- /dev/null
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java
@@ -0,0 +1,158 @@
+package org.sonar.api.batch.sensor.internal;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.sonar.api.batch.fs.internal.DefaultFileSystem;
+import org.sonar.api.batch.fs.internal.DefaultInputDir;
+import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.rule.ActiveRules;
+import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
+import org.sonar.api.batch.sensor.highlighting.TypeOfText;
+import org.sonar.api.config.Settings;
+import org.sonar.api.measures.CoreMetrics;
+import org.sonar.api.rule.RuleKey;
+
+import java.io.File;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class SensorContextTesterTest {
+
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+ private SensorContextTester tester;
+ private File baseDir;
+
+ @Before
+ public void prepare() throws Exception {
+ baseDir = temp.newFolder();
+ tester = SensorContextTester.create(baseDir);
+ }
+
+ @Test
+ public void testSettings() {
+ Settings settings = new Settings();
+ settings.setProperty("foo", "bar");
+ tester.setSettings(settings);
+ assertThat(tester.settings().getString("foo")).isEqualTo("bar");
+ }
+
+ @Test
+ public void testActiveRules() {
+ ActiveRules activeRules = new ActiveRulesBuilder().create(RuleKey.of("repo", "rule")).activate().build();
+ tester.setActiveRules(activeRules);
+ assertThat(tester.activeRules().findAll()).hasSize(1);
+ }
+
+ @Test
+ public void testFs() throws Exception {
+ DefaultFileSystem fs = new DefaultFileSystem(temp.newFolder());
+ tester.setFileSystem(fs);
+ assertThat(tester.fileSystem().baseDir()).isNotEqualTo(baseDir);
+ }
+
+ @Test
+ public void testAnalysisMode() {
+ assertThat(tester.analysisMode().isIncremental()).isFalse();
+ assertThat(tester.analysisMode().isPreview()).isFalse();
+ tester.analysisMode().setIncremental(true);
+ assertThat(tester.analysisMode().isIncremental()).isTrue();
+ tester.analysisMode().setPreview(true);
+ assertThat(tester.analysisMode().isPreview()).isTrue();
+ }
+
+ @Test
+ public void testIssues() {
+ assertThat(tester.issues("foo:src/Foo.java")).isEmpty();
+ assertThat(tester.allIssues()).isEmpty();
+ tester.newIssue()
+ .onFile(new DefaultInputFile("foo", "src/Foo.java"))
+ .forRule(RuleKey.of("repo", "rule"))
+ .atLine(1)
+ .save();
+ tester.newIssue()
+ .onFile(new DefaultInputFile("foo", "src/Foo.java"))
+ .forRule(RuleKey.of("repo", "rule"))
+ .atLine(3)
+ .save();
+ assertThat(tester.issues("foo:src/Foo.java")).hasSize(2);
+ assertThat(tester.allIssues()).hasSize(2);
+ tester.newIssue()
+ .onDir(new DefaultInputDir("foo", "src"))
+ .forRule(RuleKey.of("repo", "rule"))
+ .save();
+ assertThat(tester.issues("foo:src")).hasSize(1);
+ assertThat(tester.allIssues()).hasSize(3);
+ tester.newIssue()
+ .onProject()
+ .forRule(RuleKey.of("repo", "rule"))
+ .save();
+ assertThat(tester.issues(null)).hasSize(1);
+ assertThat(tester.allIssues()).hasSize(4);
+ }
+
+ @Test
+ public void testMeasures() {
+ assertThat(tester.measures("foo:src/Foo.java")).isEmpty();
+ assertThat(tester.measure("foo:src/Foo.java", "ncloc")).isNull();
+ tester.<Integer>newMeasure()
+ .onFile(new DefaultInputFile("foo", "src/Foo.java"))
+ .forMetric(CoreMetrics.NCLOC)
+ .withValue(2)
+ .save();
+ assertThat(tester.measures("foo:src/Foo.java")).hasSize(1);
+ assertThat(tester.measure("foo:src/Foo.java", "ncloc")).isNotNull();
+ tester.<Integer>newMeasure()
+ .onFile(new DefaultInputFile("foo", "src/Foo.java"))
+ .forMetric(CoreMetrics.LINES)
+ .withValue(4)
+ .save();
+ assertThat(tester.measures("foo:src/Foo.java")).hasSize(2);
+ assertThat(tester.measure("foo:src/Foo.java", "ncloc")).isNotNull();
+ assertThat(tester.measure("foo:src/Foo.java", "lines")).isNotNull();
+ tester.<Integer>newMeasure()
+ .onProject()
+ .forMetric(CoreMetrics.DIRECTORIES)
+ .withValue(4)
+ .save();
+ assertThat(tester.measures(null)).hasSize(1);
+ assertThat(tester.measure(null, "directories")).isNotNull();
+ }
+
+ @Test
+ public void testHighlighting() {
+ assertThat(tester.highlightingTypeFor("foo:src/Foo.java", 3)).isEmpty();
+ tester.newHighlighting()
+ .onFile(new DefaultInputFile("foo", "src/Foo.java"))
+ .highlight(0, 4, TypeOfText.ANNOTATION)
+ .highlight(8, 10, TypeOfText.CONSTANT)
+ .highlight(9, 10, TypeOfText.COMMENT)
+ .save();
+ assertThat(tester.highlightingTypeFor("foo:src/Foo.java", 3)).containsExactly(TypeOfText.ANNOTATION);
+ assertThat(tester.highlightingTypeFor("foo:src/Foo.java", 9)).containsExactly(TypeOfText.CONSTANT, TypeOfText.COMMENT);
+ }
+
+ @Test
+ public void testDuplication() {
+ assertThat(tester.duplications()).isEmpty();
+ tester.newDuplication()
+ .originBlock(new DefaultInputFile("foo", "src/Foo.java").setLines(40), 1, 30)
+ .isDuplicatedBy(new DefaultInputFile("foo", "src/Foo2.java").setLines(40), 3, 33)
+ .isDuplicatedBy(new DefaultInputFile("foo", "src/Foo3.java").setLines(40), 4, 34)
+ .save();
+ assertThat(tester.duplications()).hasSize(1);
+ }
+
+ @Test
+ public void testDependencies() {
+ assertThat(tester.dependencies()).isEmpty();
+ tester.newDependency()
+ .from(new DefaultInputFile("foo", "src/Foo.java"))
+ .to(new DefaultInputFile("foo", "src/Foo2.java"))
+ .weight(3)
+ .save();
+ assertThat(tester.dependencies()).hasSize(1);
+ }
+}