From 8bc9b652460487196ccb7639d7c6aae0c1a0d15f Mon Sep 17 00:00:00 2001 From: Bert Date: Mon, 6 Nov 2017 21:10:47 +0100 Subject: [PATCH] add unit tests #1277 --- .../cxx/sensors/other/CxxOtherSensor.java | 21 +++-- .../cxx/sensors/utils/CxxReportSensor.java | 25 +++--- .../cxx/sensors/other/CxxOtherSensorTest.java | 76 +++++++++++++++++++ 3 files changed, 104 insertions(+), 18 deletions(-) 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 a299ded359..af945eb015 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 @@ -181,9 +181,13 @@ private static boolean hasCorrectSize(List inputs, List outputs) { * @param outputs * @return */ - private static boolean isValidOutput(String outputKey, @Nullable List outputs) { - if ((outputs == null) || (outputs.isEmpty())) { - LOG.error(outputKey + " is not defined."); + private static boolean isValidOutput(@Nullable String outputKey, @Nullable List outputs) { + if ((outputKey==null) ||(outputs == null) || (outputs.isEmpty())) { + if (outputKey != null) { + LOG.error(outputKey + " file is not defined."); + } else { + LOG.error(" outputKey is not defined."); + } return false; } return true; @@ -193,9 +197,14 @@ private static boolean isValidOutput(String outputKey, @Nullable List ou * @param inputKey * @param inputs */ - private static boolean isValidInput(String inputKey, @Nullable List inputs) { - if ((inputs == null) || (inputs.isEmpty())) { - LOG.error(inputKey + " file is not defined."); + private static boolean isValidInput(@Nullable String inputKey, @Nullable List inputs) { + + if ((inputKey == null) || (inputs == null) || (inputs.isEmpty())) { + if (inputKey != null) { + LOG.error(inputKey + " file is not defined."); + } else { + LOG.error(" inputKey is not defined."); + } return false; } return true; diff --git a/cxx-sensors/src/main/java/org/sonar/cxx/sensors/utils/CxxReportSensor.java b/cxx-sensors/src/main/java/org/sonar/cxx/sensors/utils/CxxReportSensor.java index a5a2d33b8a..1ade794b69 100644 --- a/cxx-sensors/src/main/java/org/sonar/cxx/sensors/utils/CxxReportSensor.java +++ b/cxx-sensors/src/main/java/org/sonar/cxx/sensors/utils/CxxReportSensor.java @@ -156,19 +156,20 @@ public String toString() { @Nullable public static String resolveFilename(final String baseDir, final String filename) { - // Normalization can return null if path is null, is invalid, - // or is a path with back-ticks outside known directory structure - String normalizedPath = FilenameUtils.normalize(filename); - if ((normalizedPath != null) && (new File(normalizedPath).isAbsolute())) { - return normalizedPath; - } - - // Prefix with absolute module base directory, attempt normalization again -- can still get null here - normalizedPath = FilenameUtils.normalize(baseDir + File.separator + filename); - if (normalizedPath != null) { - return normalizedPath; + if (filename != null) { + // Normalization can return null if path is null, is invalid, + // or is a path with back-ticks outside known directory structure + String normalizedPath = FilenameUtils.normalize(filename); + if ((normalizedPath != null) && (new File(normalizedPath).isAbsolute())) { + return normalizedPath; + } + + // Prefix with absolute module base directory, attempt normalization again -- can still get null here + normalizedPath = FilenameUtils.normalize(baseDir + File.separator + filename); + if (normalizedPath != null) { + return normalizedPath; + } } - return null; } 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 ef696d1398..d6179be9cb 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 @@ -24,9 +24,13 @@ import org.sonar.api.batch.fs.internal.DefaultFileSystem; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import static org.mockito.Mockito.when; +import org.sonar.api.utils.log.LogTester; +import org.sonar.api.utils.log.LoggerLevel; + import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.sensor.internal.SensorContextTester; import org.sonar.api.config.Settings; @@ -39,6 +43,9 @@ public class CxxOtherSensorTest { private DefaultFileSystem fs; private CxxLanguage language; + @Rule + public LogTester logTester = new LogTester(); + @Before public void setUp() { fs = TestUtils.mockFileSystem(); @@ -145,4 +152,73 @@ public void shouldReportOnlyOneViolationAndRemoveDuplicates() { sensor.execute(context); assertThat(context.allIssues()).hasSize(1); } + + @Test + public void shouldNotCreateMessage() { + SensorContextTester context = SensorContextTester.create(fs.baseDir()); + when(language.getPluginProperty("other.xslt.1.stylesheet")).thenReturn("something"); + + Settings settings = new Settings(); + context.setSettings(settings); + + context.fileSystem().add(new DefaultInputFile("myProjectKey", "sources/utils/code_chunks.cpp").setLanguage("cpp").initMetadata("asd\nasdas\nasda\n")); + sensor = new CxxOtherSensor(language, settings); + sensor.execute(context); + assertThat(context.allIssues()).hasSize(0); + } + + @Test + 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"); + + Settings settings = new Settings(); + settings.setProperty(language.getPluginProperty(CxxOtherSensor.REPORT_PATH_KEY), "externalrules-reports/externalrules-with-duplicates.xml"); + context.setSettings(settings); + + context.fileSystem().add(new DefaultInputFile("myProjectKey", "sources/utils/code_chunks.cpp").setLanguage("cpp").initMetadata("asd\nasdas\nasda\n")); + sensor = new CxxOtherSensor(language, settings); + sensor.execute(context); + assertThat(logTester.logs(LoggerLevel.ERROR)).contains("something is not defined."); + + } + + @Test + public void shouldCreateMissingInputKeyMessage() { + 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"); + + Settings settings = new Settings(); + settings.setProperty(language.getPluginProperty(CxxOtherSensor.REPORT_PATH_KEY), "something"); + settings.setProperty("something", "something"); + context.setSettings(settings); + + context.fileSystem().add(new DefaultInputFile("myProjectKey", "sources/utils/code_chunks.cpp").setLanguage("cpp").initMetadata("asd\nasdas\nasda\n")); + sensor = new CxxOtherSensor(language, settings); + sensor.execute(context); + assertThat(logTester.logs(LoggerLevel.ERROR)).contains(" inputKey is not defined."); + } + + @Test + public void shouldCreateMissingEmptyInputsMessage() { + logTester.clear(); + SensorContextTester context = SensorContextTester.create(fs.baseDir()); + when(language.getPluginProperty("other.xslt.1.stylesheet")).thenReturn("something"); + when(language.getPluginProperty("other.xslt.1.inputs")).thenReturn("someInput"); + + Settings settings = new Settings(); + settings.setProperty(language.getPluginProperty(CxxOtherSensor.REPORT_PATH_KEY), "something"); + settings.setProperty("something", "something"); + context.setSettings(settings); + + context.fileSystem().add(new DefaultInputFile("myProjectKey", "sources/utils/code_chunks.cpp").setLanguage("cpp").initMetadata("asd\nasdas\nasda\n")); + sensor = new CxxOtherSensor(language, settings); + sensor.execute(context); + assertThat(logTester.logs(LoggerLevel.ERROR)).contains("someInput file is not defined."); + } + }