diff --git a/cxx-sensors/src/main/java/org/sonar/cxx/sensors/other/CxxOtherSensor.java b/cxx-sensors/src/main/java/org/sonar/cxx/sensors/other/CxxOtherSensor.java index 1b91259102..546d95d97a 100644 --- a/cxx-sensors/src/main/java/org/sonar/cxx/sensors/other/CxxOtherSensor.java +++ b/cxx-sensors/src/main/java/org/sonar/cxx/sensors/other/CxxOtherSensor.java @@ -22,6 +22,7 @@ import java.io.File; import java.io.IOException; import java.net.URISyntaxException; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; import javax.annotation.Nullable; @@ -88,24 +89,17 @@ public void processReport(final SensorContext context, File report) throws XMLSt URISyntaxException, TransformerException { LOG.debug("Parsing 'other' format"); - StaxParser parser = new StaxParser(new StaxParser.XmlStreamHandler() { + StaxParser parser = new StaxParser((SMHierarchicCursor rootCursor) -> { + rootCursor.advance(); - /** - * {@inheritDoc} - */ - @Override - public void stream(SMHierarchicCursor rootCursor) throws XMLStreamException { - rootCursor.advance(); + SMInputCursor errorCursor = rootCursor.childElementCursor("error"); + while (errorCursor.getNext() != null) { + String file = errorCursor.getAttrValue("file"); + String line = errorCursor.getAttrValue("line"); + String id = errorCursor.getAttrValue("id"); + String msg = errorCursor.getAttrValue("msg"); - SMInputCursor errorCursor = rootCursor.childElementCursor("error"); - while (errorCursor.getNext() != null) { - String file = errorCursor.getAttrValue("file"); - String line = errorCursor.getAttrValue("line"); - String id = errorCursor.getAttrValue("id"); - String msg = errorCursor.getAttrValue("msg"); - - saveUniqueViolation(context, CxxOtherRepository.KEY, file, line, id, msg); - } + saveUniqueViolation(context, CxxOtherRepository.KEY, file, line, id, msg); } }); @@ -118,45 +112,34 @@ protected String getSensorKey() { } public void transformFiles(final File baseDir, SensorContext context) { - boolean goOn = true; - for (int i = 1; (i < MAX_STYLESHEETS) && goOn; i++) { + for (int i = 1; i < MAX_STYLESHEETS; i++) { String stylesheetKey = this.language.getPluginProperty(OTHER_XSLT_KEY + i + STYLESHEET_KEY); String inputKey = this.language.getPluginProperty(OTHER_XSLT_KEY + i + INPUT_KEY); String outputKey = this.language.getPluginProperty(OTHER_XSLT_KEY + i + OUTPUT_KEY); - if (stylesheetKey == null) { - LOG.error("'{}' is not defined.", OTHER_XSLT_KEY + i + STYLESHEET_KEY); - break; + String stylesheet = stylesheetKey == null ? null : resolveFilename(baseDir.getAbsolutePath(), context.config().get(stylesheetKey).orElse(null)); + List inputs = inputKey == null ? new ArrayList<>() : getReports(context.config(), baseDir, inputKey); + String[] outputStrings = outputKey == null ? null : context.config().getStringArray(outputKey); + List outputs = outputStrings == null ? new ArrayList<>() : Arrays.asList(outputStrings); + + if ((stylesheet == null) && (inputs.isEmpty()) && (outputs.isEmpty())) { + break; // no or last item } - String stylesheet = resolveFilename(baseDir.getAbsolutePath(), context.config().get(stylesheetKey).orElse(null)); - List inputs = getReports(context.config(), baseDir, inputKey); - String[] outputStrings = null; - if (outputKey != null) { - outputStrings = context.config().getStringArray(outputKey); + if (stylesheet == null) { + LOG.error(stylesheetKey + " is not defined."); + break; } - List outputs = Arrays.asList((outputStrings != null) ? outputStrings : new String[]{}); - if (stylesheet == null && inputKey == null && outputKey == null) { - goOn = false; - } else { - if (stylesheet == null) { - LOG.error(stylesheetKey + " is not defined."); - goOn = false; - } else { - goOn = checkInput(inputKey, outputKey, inputs, outputs); - } + if (!checkInput(inputKey, outputKey, inputs, outputs)) { + break; } - if (goOn) { - if (LOG.isDebugEnabled()) { - LOG.debug("Converting " + stylesheet + " with " + inputs + " to " + outputs + "."); - } - File stylesheetFile = new File(stylesheet); - if (stylesheetFile.isAbsolute()) { - transformFileList(baseDir.getAbsolutePath(), stylesheetFile, inputs, outputs); - } + if (LOG.isDebugEnabled()) { + LOG.debug("Converting " + stylesheet + " with " + inputs + " to " + outputs + "."); } + + transformFileList(baseDir.getAbsolutePath(), stylesheet, inputs, outputs); } } @@ -213,11 +196,11 @@ private static boolean isValidInput(@Nullable String inputKey, @Nullable List inputs, List outputs) { + private void transformFileList(final String baseDir, String stylesheet, List inputs, List outputs) { for (int j = 0; j < inputs.size(); j++) { try { String normalizedOutputFilename = resolveFilename(baseDir, outputs.get(j)); - CxxUtils.transformFile(new StreamSource(stylesheetFile), inputs.get(j), new File(normalizedOutputFilename)); + CxxUtils.transformFile(new StreamSource(new File(stylesheet)), inputs.get(j), new File(normalizedOutputFilename)); } catch (TransformerException e) { String msg = new StringBuilder() .append("Cannot transform report files: '") diff --git a/cxx-sensors/src/test/java/org/sonar/cxx/sensors/other/CxxOtherSensorTest.java b/cxx-sensors/src/test/java/org/sonar/cxx/sensors/other/CxxOtherSensorTest.java index d8edfff9be..e805b151a0 100644 --- a/cxx-sensors/src/test/java/org/sonar/cxx/sensors/other/CxxOtherSensorTest.java +++ b/cxx-sensors/src/test/java/org/sonar/cxx/sensors/other/CxxOtherSensorTest.java @@ -147,6 +147,18 @@ public void shouldReportOnlyOneViolationAndRemoveDuplicates() { assertThat(context.allIssues()).hasSize(1); } + @Test + public void shouldReportNothing() { + SensorContextTester context = SensorContextTester.create(fs.baseDir()); + when(language.getPluginProperty("other.xslt.1.stylesheet")).thenReturn("other.xslt.1.stylesheet"); + when(language.getPluginProperty("other.xslt.1.inputs")).thenReturn("other.xslt.1.inputs"); + when(language.getPluginProperty("other.xslt.1.outputs")).thenReturn("other.xslt.1.outputs"); + + sensor = new CxxOtherSensor(language); + sensor.execute(context); + assertThat(logTester.logs(LoggerLevel.ERROR)).isEmpty(); + } + @Test public void shouldNotCreateMessage() { SensorContextTester context = SensorContextTester.create(fs.baseDir()); @@ -166,9 +178,10 @@ public void shouldCreateMissingStylesheetMessage() { logTester.clear(); SensorContextTester context = SensorContextTester.create(fs.baseDir()); when(language.getPluginProperty("other.xslt.1.stylesheet")).thenReturn("something"); - when(language.getPluginProperty("other.xslt.1.outputs")).thenReturn("something"); + when(language.getPluginProperty("other.xslt.1.outputs")).thenReturn("outputs"); settings.setProperty(language.getPluginProperty(CxxOtherSensor.REPORT_PATH_KEY), "externalrules-reports/externalrules-with-duplicates.xml"); + settings.setProperty("outputs", "outputs"); context.setSettings(settings); context.fileSystem().add(TestInputFileBuilder.create("ProjectKey", "sources/utils/code_chunks.cpp")