Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Bertk committed Nov 6, 2017
1 parent 423222a commit 8bc9b65
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,13 @@ private static boolean hasCorrectSize(List<File> inputs, List<String> outputs) {
* @param outputs
* @return
*/
private static boolean isValidOutput(String outputKey, @Nullable List<String> outputs) {
if ((outputs == null) || (outputs.isEmpty())) {
LOG.error(outputKey + " is not defined.");
private static boolean isValidOutput(@Nullable String outputKey, @Nullable List<String> 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;
Expand All @@ -193,9 +197,14 @@ private static boolean isValidOutput(String outputKey, @Nullable List<String> ou
* @param inputKey
* @param inputs
*/
private static boolean isValidInput(String inputKey, @Nullable List<File> inputs) {
if ((inputs == null) || (inputs.isEmpty())) {
LOG.error(inputKey + " file is not defined.");
private static boolean isValidInput(@Nullable String inputKey, @Nullable List<File> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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.");
}

}

0 comments on commit 8bc9b65

Please sign in to comment.