diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2014-09-11 13:30:27 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2014-09-11 13:30:55 +0200 |
commit | 01e4d872465235a0a504dbd5119d6e5b0ad3403c (patch) | |
tree | 56716ae7870da580b9c48fe7333c2475274b2bae /plugins/sonar-xoo-plugin/src | |
parent | 49bc20e4cd664c5f373f8d451bf636d1d880bf81 (diff) | |
download | sonarqube-01e4d872465235a0a504dbd5119d6e5b0ad3403c.tar.gz sonarqube-01e4d872465235a0a504dbd5119d6e5b0ad3403c.zip |
Fix some quality flaws
Diffstat (limited to 'plugins/sonar-xoo-plugin/src')
4 files changed, 156 insertions, 9 deletions
diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/CoveragePerTestSensor.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/CoveragePerTestSensor.java index aee8297811e..cf0ff9389bf 100644 --- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/CoveragePerTestSensor.java +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/CoveragePerTestSensor.java @@ -50,18 +50,18 @@ public class CoveragePerTestSensor implements Sensor { private void processCoveragePerTest(InputFile inputFile, SensorContext context) { File ioFile = inputFile.file(); - File testPlanFile = new File(ioFile.getParentFile(), ioFile.getName() + COVER_PER_TEST_EXTENSION); - if (testPlanFile.exists()) { - LOG.debug("Processing " + testPlanFile.getAbsolutePath()); + File coverPerTestFile = new File(ioFile.getParentFile(), ioFile.getName() + COVER_PER_TEST_EXTENSION); + if (coverPerTestFile.exists()) { + LOG.debug("Processing " + coverPerTestFile.getAbsolutePath()); try { - List<String> lines = FileUtils.readLines(testPlanFile, context.fileSystem().encoding().name()); + List<String> lines = FileUtils.readLines(coverPerTestFile, context.fileSystem().encoding().name()); int lineNumber = 0; for (String line : lines) { lineNumber++; if (StringUtils.isBlank(line) || line.startsWith("#")) { continue; } - processLine(testPlanFile, lineNumber, context, line, inputFile); + processLine(coverPerTestFile, lineNumber, context, line, inputFile); } } catch (IOException e) { throw new IllegalStateException(e); @@ -69,7 +69,7 @@ public class CoveragePerTestSensor implements Sensor { } } - private void processLine(File testplanFile, int lineNumber, SensorContext context, String line, InputFile testFile) { + private void processLine(File coverPerTest, int lineNumber, SensorContext context, String line, InputFile testFile) { try { Iterator<String> split = Splitter.on(":").split(line).iterator(); String testCaseName = split.next(); @@ -87,7 +87,7 @@ public class CoveragePerTestSensor implements Sensor { } context.saveCoveragePerTest(testCase, mainFile, coveredLines); } catch (Exception e) { - throw new IllegalStateException("Error processing line " + lineNumber + " of file " + testplanFile.getAbsolutePath(), e); + throw new IllegalStateException("Error processing line " + lineNumber + " of file " + coverPerTest.getAbsolutePath(), e); } } diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/TestCaseSensor.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/TestCaseSensor.java index c4238b51e18..96c7de1b045 100644 --- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/TestCaseSensor.java +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/TestCaseSensor.java @@ -80,8 +80,8 @@ public class TestCaseSensor implements Sensor { context.addTestCase(context.testCaseBuilder(testFile, name) .type(TestCase.Type.valueOf(type)) .status(TestCase.Status.valueOf(status)) - .message(message) - .stackTrace(stack) + .message(StringUtils.trimToNull(message)) + .stackTrace(StringUtils.trimToNull(stack)) .durationInMs(duration) .build()); } catch (Exception e) { diff --git a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/CoveragePerTestSensorTest.java b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/CoveragePerTestSensorTest.java new file mode 100644 index 00000000000..583911e4d76 --- /dev/null +++ b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/CoveragePerTestSensorTest.java @@ -0,0 +1,75 @@ +package org.sonar.xoo.lang; + +import org.apache.commons.io.FileUtils; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.sonar.api.batch.fs.InputFile.Type; +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.internal.DefaultSensorDescriptor; +import org.sonar.api.batch.sensor.test.TestCase; +import org.sonar.api.batch.sensor.test.internal.DefaultTestCaseBuilder; + +import java.io.File; +import java.io.IOException; +import java.util.Arrays; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class CoveragePerTestSensorTest { + + private CoveragePerTestSensor sensor; + private SensorContext context = mock(SensorContext.class); + private DefaultFileSystem fileSystem; + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + private File baseDir; + + @Before + public void prepare() throws IOException { + baseDir = temp.newFolder(); + sensor = new CoveragePerTestSensor(); + fileSystem = new DefaultFileSystem(); + when(context.fileSystem()).thenReturn(fileSystem); + } + + @Test + public void testDescriptor() { + sensor.describe(new DefaultSensorDescriptor()); + } + + @Test + public void testNoExecutionIfCoveragePerTestFile() { + DefaultInputFile testFile = new DefaultInputFile("foo", "test/fooTest.xoo").setAbsolutePath(new File(baseDir, "test/fooTest.xoo").getAbsolutePath()).setLanguage("xoo") + .setType(Type.TEST); + fileSystem.add(testFile); + sensor.execute(context); + } + + @Test + public void testExecution() throws IOException { + File coverPerTest = new File(baseDir, "test/fooTest.xoo.coveragePerTest"); + FileUtils.write(coverPerTest, "test1:src/foo.xoo:1,2,3,4\ntest2:src/foo.xoo:5,6,7\n\n#comment"); + DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setAbsolutePath(new File(baseDir, "src/foo.xoo").getAbsolutePath()).setLanguage("xoo"); + DefaultInputFile testFile = new DefaultInputFile("foo", "test/fooTest.xoo").setAbsolutePath(new File(baseDir, "test/fooTest.xoo").getAbsolutePath()).setLanguage("xoo") + .setType(Type.TEST); + fileSystem.add(inputFile); + fileSystem.add(testFile); + + TestCase test1 = new DefaultTestCaseBuilder(testFile, "test1").durationInMs(10).build(); + TestCase test2 = new DefaultTestCaseBuilder(testFile, "test2").durationInMs(10).build(); + when(context.getTestCase(testFile, "test1")).thenReturn(test1); + when(context.getTestCase(testFile, "test2")).thenReturn(test2); + + sensor.execute(context); + + verify(context).saveCoveragePerTest(test1, inputFile, Arrays.asList(1, 2, 3, 4)); + verify(context).saveCoveragePerTest(test2, inputFile, Arrays.asList(5, 6, 7)); + } +} diff --git a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/TestCaseSensorTest.java b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/TestCaseSensorTest.java new file mode 100644 index 00000000000..35967a58349 --- /dev/null +++ b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/TestCaseSensorTest.java @@ -0,0 +1,72 @@ +package org.sonar.xoo.lang; + +import org.apache.commons.io.FileUtils; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.sonar.api.batch.fs.InputFile.Type; +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.internal.DefaultSensorDescriptor; +import org.sonar.api.batch.sensor.test.TestCase; +import org.sonar.api.batch.sensor.test.internal.DefaultTestCaseBuilder; + +import java.io.File; +import java.io.IOException; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class TestCaseSensorTest { + + private TestCaseSensor sensor; + private SensorContext context = mock(SensorContext.class); + private DefaultFileSystem fileSystem; + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + private File baseDir; + + @Before + public void prepare() throws IOException { + baseDir = temp.newFolder(); + sensor = new TestCaseSensor(); + fileSystem = new DefaultFileSystem(); + when(context.fileSystem()).thenReturn(fileSystem); + } + + @Test + public void testDescriptor() { + sensor.describe(new DefaultSensorDescriptor()); + } + + @Test + public void testNoExecutionIfNoTestFile() { + DefaultInputFile testFile = new DefaultInputFile("foo", "test/fooTest.xoo").setAbsolutePath(new File(baseDir, "test/fooTest.xoo").getAbsolutePath()).setLanguage("xoo") + .setType(Type.TEST); + fileSystem.add(testFile); + sensor.execute(context); + } + + @Test + public void testExecution() throws IOException { + File testPlan = new File(baseDir, "test/fooTest.xoo.testplan"); + FileUtils.write(testPlan, "test1:UNIT:OK:::10\ntest2:INTEGRATION:ERROR:message:stack:15\n\n#comment"); + DefaultInputFile testFile = new DefaultInputFile("foo", "test/fooTest.xoo").setAbsolutePath(new File(baseDir, "test/fooTest.xoo").getAbsolutePath()).setLanguage("xoo") + .setType(Type.TEST); + fileSystem.add(testFile); + + when(context.testCaseBuilder(testFile, "test1")).thenReturn(new DefaultTestCaseBuilder(testFile, "test1")); + when(context.testCaseBuilder(testFile, "test2")).thenReturn(new DefaultTestCaseBuilder(testFile, "test2")); + + sensor.execute(context); + + verify(context).addTestCase(new DefaultTestCaseBuilder(testFile, "test1").durationInMs(10).build()); + verify(context).addTestCase( + new DefaultTestCaseBuilder(testFile, "test2").type(TestCase.Type.INTEGRATION).status(TestCase.Status.ERROR).message("message").stackTrace("stack").durationInMs(15).build()); + } + +} |