Skip to content

Commit 8d7f87e

Browse files
authored
Merge pull request #1288 from Bertk/fixes2
add unit tests
2 parents b796ad0 + 8bc9b65 commit 8d7f87e

File tree

3 files changed

+104
-18
lines changed

3 files changed

+104
-18
lines changed

cxx-sensors/src/main/java/org/sonar/cxx/sensors/other/CxxOtherSensor.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,13 @@ private static boolean hasCorrectSize(List<File> inputs, List<String> outputs) {
180180
* @param outputs
181181
* @return
182182
*/
183-
private static boolean isValidOutput(String outputKey, @Nullable List<String> outputs) {
184-
if ((outputs == null) || (outputs.isEmpty())) {
185-
LOG.error(outputKey + " is not defined.");
183+
private static boolean isValidOutput(@Nullable String outputKey, @Nullable List<String> outputs) {
184+
if ((outputKey==null) ||(outputs == null) || (outputs.isEmpty())) {
185+
if (outputKey != null) {
186+
LOG.error(outputKey + " file is not defined.");
187+
} else {
188+
LOG.error(" outputKey is not defined.");
189+
}
186190
return false;
187191
}
188192
return true;
@@ -192,9 +196,14 @@ private static boolean isValidOutput(String outputKey, @Nullable List<String> ou
192196
* @param inputKey
193197
* @param inputs
194198
*/
195-
private static boolean isValidInput(String inputKey, @Nullable List<File> inputs) {
196-
if ((inputs == null) || (inputs.isEmpty())) {
197-
LOG.error(inputKey + " file is not defined.");
199+
private static boolean isValidInput(@Nullable String inputKey, @Nullable List<File> inputs) {
200+
201+
if ((inputKey == null) || (inputs == null) || (inputs.isEmpty())) {
202+
if (inputKey != null) {
203+
LOG.error(inputKey + " file is not defined.");
204+
} else {
205+
LOG.error(" inputKey is not defined.");
206+
}
198207
return false;
199208
}
200209
return true;

cxx-sensors/src/main/java/org/sonar/cxx/sensors/utils/CxxReportSensor.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -164,19 +164,20 @@ public String toString() {
164164
@Nullable
165165
public static String resolveFilename(final String baseDir, final String filename) {
166166

167-
// Normalization can return null if path is null, is invalid,
168-
// or is a path with back-ticks outside known directory structure
169-
String normalizedPath = FilenameUtils.normalize(filename);
170-
if ((normalizedPath != null) && (new File(normalizedPath).isAbsolute())) {
171-
return normalizedPath;
172-
}
173-
174-
// Prefix with absolute module base directory, attempt normalization again -- can still get null here
175-
normalizedPath = FilenameUtils.normalize(baseDir + File.separator + filename);
176-
if (normalizedPath != null) {
177-
return normalizedPath;
167+
if (filename != null) {
168+
// Normalization can return null if path is null, is invalid,
169+
// or is a path with back-ticks outside known directory structure
170+
String normalizedPath = FilenameUtils.normalize(filename);
171+
if ((normalizedPath != null) && (new File(normalizedPath).isAbsolute())) {
172+
return normalizedPath;
173+
}
174+
175+
// Prefix with absolute module base directory, attempt normalization again -- can still get null here
176+
normalizedPath = FilenameUtils.normalize(baseDir + File.separator + filename);
177+
if (normalizedPath != null) {
178+
return normalizedPath;
179+
}
178180
}
179-
180181
return null;
181182
}
182183

cxx-sensors/src/test/java/org/sonar/cxx/sensors/other/CxxOtherSensorTest.java

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@
2323
import org.sonar.api.batch.fs.internal.DefaultFileSystem;
2424

2525
import org.junit.Before;
26+
import org.junit.Rule;
2627
import org.junit.Test;
2728
import static org.mockito.Mockito.when;
2829

30+
import org.sonar.api.utils.log.LogTester;
31+
import org.sonar.api.utils.log.LoggerLevel;
32+
2933
import org.sonar.api.batch.fs.internal.DefaultInputFile;
3034
import org.sonar.api.batch.sensor.internal.SensorContextTester;
3135
import org.sonar.api.config.Settings;
@@ -38,6 +42,9 @@ public class CxxOtherSensorTest {
3842
private DefaultFileSystem fs;
3943
private CxxLanguage language;
4044

45+
@Rule
46+
public LogTester logTester = new LogTester();
47+
4148
@Before
4249
public void setUp() {
4350
fs = TestUtils.mockFileSystem();
@@ -144,4 +151,73 @@ public void shouldReportOnlyOneViolationAndRemoveDuplicates() {
144151
sensor.execute(context);
145152
assertThat(context.allIssues()).hasSize(1);
146153
}
154+
155+
@Test
156+
public void shouldNotCreateMessage() {
157+
SensorContextTester context = SensorContextTester.create(fs.baseDir());
158+
when(language.getPluginProperty("other.xslt.1.stylesheet")).thenReturn("something");
159+
160+
Settings settings = new Settings();
161+
context.setSettings(settings);
162+
163+
context.fileSystem().add(new DefaultInputFile("myProjectKey", "sources/utils/code_chunks.cpp").setLanguage("cpp").initMetadata("asd\nasdas\nasda\n"));
164+
sensor = new CxxOtherSensor(language, settings);
165+
sensor.execute(context);
166+
assertThat(context.allIssues()).hasSize(0);
167+
}
168+
169+
@Test
170+
public void shouldCreateMissingStylesheetMessage() {
171+
logTester.clear();
172+
SensorContextTester context = SensorContextTester.create(fs.baseDir());
173+
when(language.getPluginProperty("other.xslt.1.stylesheet")).thenReturn("something");
174+
when(language.getPluginProperty("other.xslt.1.outputs")).thenReturn("something");
175+
176+
Settings settings = new Settings();
177+
settings.setProperty(language.getPluginProperty(CxxOtherSensor.REPORT_PATH_KEY), "externalrules-reports/externalrules-with-duplicates.xml");
178+
context.setSettings(settings);
179+
180+
context.fileSystem().add(new DefaultInputFile("myProjectKey", "sources/utils/code_chunks.cpp").setLanguage("cpp").initMetadata("asd\nasdas\nasda\n"));
181+
sensor = new CxxOtherSensor(language, settings);
182+
sensor.execute(context);
183+
assertThat(logTester.logs(LoggerLevel.ERROR)).contains("something is not defined.");
184+
185+
}
186+
187+
@Test
188+
public void shouldCreateMissingInputKeyMessage() {
189+
logTester.clear();
190+
SensorContextTester context = SensorContextTester.create(fs.baseDir());
191+
when(language.getPluginProperty("other.xslt.1.stylesheet")).thenReturn("something");
192+
when(language.getPluginProperty("other.xslt.1.outputs")).thenReturn("something");
193+
194+
Settings settings = new Settings();
195+
settings.setProperty(language.getPluginProperty(CxxOtherSensor.REPORT_PATH_KEY), "something");
196+
settings.setProperty("something", "something");
197+
context.setSettings(settings);
198+
199+
context.fileSystem().add(new DefaultInputFile("myProjectKey", "sources/utils/code_chunks.cpp").setLanguage("cpp").initMetadata("asd\nasdas\nasda\n"));
200+
sensor = new CxxOtherSensor(language, settings);
201+
sensor.execute(context);
202+
assertThat(logTester.logs(LoggerLevel.ERROR)).contains(" inputKey is not defined.");
203+
}
204+
205+
@Test
206+
public void shouldCreateMissingEmptyInputsMessage() {
207+
logTester.clear();
208+
SensorContextTester context = SensorContextTester.create(fs.baseDir());
209+
when(language.getPluginProperty("other.xslt.1.stylesheet")).thenReturn("something");
210+
when(language.getPluginProperty("other.xslt.1.inputs")).thenReturn("someInput");
211+
212+
Settings settings = new Settings();
213+
settings.setProperty(language.getPluginProperty(CxxOtherSensor.REPORT_PATH_KEY), "something");
214+
settings.setProperty("something", "something");
215+
context.setSettings(settings);
216+
217+
context.fileSystem().add(new DefaultInputFile("myProjectKey", "sources/utils/code_chunks.cpp").setLanguage("cpp").initMetadata("asd\nasdas\nasda\n"));
218+
sensor = new CxxOtherSensor(language, settings);
219+
sensor.execute(context);
220+
assertThat(logTester.logs(LoggerLevel.ERROR)).contains("someInput file is not defined.");
221+
}
222+
147223
}

0 commit comments

Comments
 (0)