Skip to content

Commit

Permalink
SONARJAVA-4934 JavaSensor should be executed on jsp files even withou…
Browse files Browse the repository at this point in the history
…t java files (#4772)
  • Loading branch information
alban-auzeill committed Apr 17, 2024
1 parent 12b7ef1 commit ca46a10
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
4 changes: 3 additions & 1 deletion java-jsp/src/main/java/org/sonar/java/jsp/Jasper.java
Expand Up @@ -63,6 +63,8 @@
@ScannerSide
public class Jasper {

public static final String JSP_LANGUAGE_KEY = "jsp";

private static final String SONAR_EXCLUSIONS_PROPERTY = "sonar.exclusions";

private static final Logger LOG = LoggerFactory.getLogger(Jasper.class);
Expand Down Expand Up @@ -188,7 +190,7 @@ private static Optional<Path> findWebInfParentDirectory(FileSystem fs) {
}

private static List<InputFile> jspFiles(FileSystem fs) {
Iterable<InputFile> inputFiles = fs.inputFiles(fs.predicates().hasLanguage("jsp"));
Iterable<InputFile> inputFiles = fs.inputFiles(fs.predicates().hasLanguage(JSP_LANGUAGE_KEY));
return StreamSupport.stream(inputFiles.spliterator(), false)
.toList();
}
Expand Down
Expand Up @@ -96,7 +96,7 @@ public JavaSensor(SonarComponents sonarComponents, FileSystem fs, JavaResourceLo

@Override
public void describe(SensorDescriptor descriptor) {
descriptor.onlyOnLanguage(Java.KEY).name("JavaSensor");
descriptor.onlyOnLanguages(Java.KEY, Jasper.JSP_LANGUAGE_KEY).name("JavaSensor");
}

@Override
Expand Down
Expand Up @@ -45,6 +45,7 @@
import org.sonar.api.batch.rule.Checks;
import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
import org.sonar.api.batch.rule.internal.NewActiveRule;
import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
import org.sonar.api.batch.sensor.internal.SensorContextTester;
import org.sonar.api.config.Configuration;
import org.sonar.api.config.internal.MapSettings;
Expand Down Expand Up @@ -112,8 +113,8 @@ void test_toString() throws IOException {

@Test
void test_issues_creation_on_main_file() throws IOException {
// Expected issues : the number of methods violating BadMethodName rule. Currently, 17 tests.
testIssueCreation(InputFile.Type.MAIN, 17);
// Expected issues : the number of methods violating BadMethodName rule. Currently, 18 tests.
testIssueCreation(InputFile.Type.MAIN, 18);
}

@Test
Expand All @@ -132,8 +133,8 @@ private void testIssueCreation(InputFile.Type onType, int expectedIssues) throws
JavaSensor jss = new JavaSensor(sonarComponents, fs, javaResourceLocator, settings.asConfig(), noSonarFilter, null);

jss.execute(context);
// argument 120 refers to the comment on line #120 in this file
verify(noSonarFilter, times(1)).noSonarInFile(fs.inputFiles().iterator().next(), Collections.singleton(120));
// argument 121 refers to the comment on line #121 in this file, each time this file changes, this argument should be updated
verify(noSonarFilter, times(1)).noSonarInFile(fs.inputFiles().iterator().next(), Collections.singleton(121));
verify(sonarComponents, times(expectedIssues)).reportIssue(any(AnalyzerMessage.class));

settings.setProperty(JavaVersion.SOURCE_VERSION, "wrongFormat");
Expand Down Expand Up @@ -434,6 +435,16 @@ void filter_checks_when_autoscan_true() throws IOException {
);
}

@Test
void test_describe_sensor() throws IOException {
DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor();
SonarComponents sonarComponents = createSonarComponentsMock(createContext(InputFile.Type.MAIN));
var sensor = new JavaSensor(sonarComponents, null, null, null, null, null);
sensor.describe(descriptor);
assertThat(descriptor.name()).isEqualTo("JavaSensor");
assertThat(descriptor.languages()).containsExactly("java", "jsp");
}

private SensorContextTester analyzeTwoFilesWithIssues(MapSettings settings) throws IOException {
SensorContextTester context = SensorContextTester.create(new File("src/test/files").getAbsoluteFile())
.setSettings(settings)
Expand Down

0 comments on commit ca46a10

Please sign in to comment.