diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2015-02-20 14:57:39 +0100 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2015-02-20 15:05:18 +0100 |
commit | ee09741a99176e635b310ab295dd898533828d69 (patch) | |
tree | ccb54de1e5373e54e9a9feffc2adde4d81ac1a05 /plugins | |
parent | 38357bb24905509dd775e40bdf09e40aec6af39c (diff) | |
download | sonarqube-ee09741a99176e635b310ab295dd898533828d69.tar.gz sonarqube-ee09741a99176e635b310ab295dd898533828d69.zip |
SONAR-5931 Add a tester class for SensorContext
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/MeasureSensorTest.java | 38 | ||||
-rw-r--r-- | plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/SyntaxHighlightingSensorTest.java | 28 |
2 files changed, 19 insertions, 47 deletions
diff --git a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/MeasureSensorTest.java b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/MeasureSensorTest.java index 92169bdf42b..de18a8b9264 100644 --- a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/MeasureSensorTest.java +++ b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/MeasureSensorTest.java @@ -19,37 +19,30 @@ */ package org.sonar.xoo.lang; -import org.sonar.api.batch.sensor.internal.SensorStorage; - 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.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; -import org.sonar.api.batch.fs.internal.DefaultFileSystem; import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.measure.MetricFinder; -import org.sonar.api.batch.sensor.SensorContext; import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor; -import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure; +import org.sonar.api.batch.sensor.internal.SensorContextTester; import org.sonar.api.measures.CoreMetrics; import org.sonar.api.measures.Metric; import java.io.File; import java.io.IOException; +import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; public class MeasureSensorTest { private MeasureSensor sensor; - private SensorContext context = mock(SensorContext.class); - private DefaultFileSystem fileSystem; + private SensorContextTester context; @Rule public TemporaryFolder temp = new TemporaryFolder(); @@ -59,22 +52,13 @@ public class MeasureSensorTest { private File baseDir; private MetricFinder metricFinder; - private SensorStorage storage; @Before public void prepare() throws IOException { baseDir = temp.newFolder(); metricFinder = mock(MetricFinder.class); sensor = new MeasureSensor(metricFinder); - fileSystem = new DefaultFileSystem(baseDir.toPath()); - when(context.fileSystem()).thenReturn(fileSystem); - storage = mock(SensorStorage.class); - when(context.newMeasure()).then(new Answer<DefaultMeasure>() { - @Override - public DefaultMeasure answer(InvocationOnMock invocation) throws Throwable { - return new DefaultMeasure(storage); - } - }); + context = SensorContextTester.create(baseDir); } @Test @@ -85,7 +69,7 @@ public class MeasureSensorTest { @Test public void testNoExecutionIfNoMeasureFile() { DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setLanguage("xoo"); - fileSystem.add(inputFile); + context.fileSystem().add(inputFile); sensor.execute(context); } @@ -94,7 +78,7 @@ public class MeasureSensorTest { File measures = new File(baseDir, "src/foo.xoo.measures"); FileUtils.write(measures, "ncloc:12\nbranch_coverage:5.3\nsqale_index:300\nbool:true\n\n#comment"); DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setLanguage("xoo"); - fileSystem.add(inputFile); + context.fileSystem().add(inputFile); Metric<Boolean> booleanMetric = new Metric.Builder("bool", "Bool", Metric.ValueType.BOOL) .create(); @@ -106,10 +90,10 @@ public class MeasureSensorTest { sensor.execute(context); - verify(storage).store(new DefaultMeasure().forMetric(CoreMetrics.NCLOC).onFile(inputFile).withValue(12)); - verify(storage).store(new DefaultMeasure().forMetric(CoreMetrics.BRANCH_COVERAGE).onFile(inputFile).withValue(5.3)); - verify(storage).store(new DefaultMeasure().forMetric(CoreMetrics.TECHNICAL_DEBT).onFile(inputFile).withValue(300L)); - verify(storage).store(new DefaultMeasure().forMetric(booleanMetric).onFile(inputFile).withValue(true)); + assertThat(context.measure("foo:src/foo.xoo", CoreMetrics.NCLOC).value()).isEqualTo(12); + assertThat(context.measure("foo:src/foo.xoo", CoreMetrics.BRANCH_COVERAGE).value()).isEqualTo(5.3); + assertThat(context.measure("foo:src/foo.xoo", CoreMetrics.TECHNICAL_DEBT).value()).isEqualTo(300L); + assertThat(context.measure("foo:src/foo.xoo", booleanMetric).value()).isTrue(); } @Test @@ -117,7 +101,7 @@ public class MeasureSensorTest { File measures = new File(baseDir, "src/foo.xoo.measures"); FileUtils.write(measures, "unknow:12\n\n#comment"); DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setLanguage("xoo"); - fileSystem.add(inputFile); + context.fileSystem().add(inputFile); thrown.expect(IllegalStateException.class); diff --git a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/SyntaxHighlightingSensorTest.java b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/SyntaxHighlightingSensorTest.java index 4bd8aa2da61..9b9a7baca05 100644 --- a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/SyntaxHighlightingSensorTest.java +++ b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/SyntaxHighlightingSensorTest.java @@ -24,27 +24,20 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.internal.DefaultFileSystem; import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.api.batch.sensor.SensorContext; -import org.sonar.api.batch.sensor.highlighting.NewHighlighting; import org.sonar.api.batch.sensor.highlighting.TypeOfText; import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor; +import org.sonar.api.batch.sensor.internal.SensorContextTester; import java.io.File; import java.io.IOException; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import static org.assertj.core.api.Assertions.assertThat; public class SyntaxHighlightingSensorTest { private SyntaxHighlightingSensor sensor; - private SensorContext context = mock(SensorContext.class); - private DefaultFileSystem fileSystem; + private SensorContextTester context; @Rule public TemporaryFolder temp = new TemporaryFolder(); @@ -54,8 +47,7 @@ public class SyntaxHighlightingSensorTest { public void prepare() throws IOException { baseDir = temp.newFolder(); sensor = new SyntaxHighlightingSensor(); - fileSystem = new DefaultFileSystem(baseDir.toPath()); - when(context.fileSystem()).thenReturn(fileSystem); + context = SensorContextTester.create(baseDir); } @Test @@ -66,7 +58,7 @@ public class SyntaxHighlightingSensorTest { @Test public void testNoExecutionIfNoSyntaxFile() { DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setLanguage("xoo"); - fileSystem.add(inputFile); + context.fileSystem().add(inputFile); sensor.execute(context); } @@ -75,15 +67,11 @@ public class SyntaxHighlightingSensorTest { File symbol = new File(baseDir, "src/foo.xoo.highlighting"); FileUtils.write(symbol, "1:4:k\n12:15:cppd\n\n#comment"); DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setLanguage("xoo"); - fileSystem.add(inputFile); - NewHighlighting builder = mock(NewHighlighting.class); - when(context.newHighlighting()).thenReturn(builder); - when(builder.onFile(any(InputFile.class))).thenReturn(builder); + context.fileSystem().add(inputFile); sensor.execute(context); - verify(builder).highlight(1, 4, TypeOfText.KEYWORD); - verify(builder).highlight(12, 15, TypeOfText.CPP_DOC); - verify(builder).save(); + assertThat(context.highlightingTypeFor("foo:src/foo.xoo", 2)).containsOnly(TypeOfText.KEYWORD); + assertThat(context.highlightingTypeFor("foo:src/foo.xoo", 13)).containsOnly(TypeOfText.CPP_DOC); } } |