Skip to content

Commit ca46a10

Browse files
SONARJAVA-4934 JavaSensor should be executed on jsp files even without java files (#4772)
1 parent 12b7ef1 commit ca46a10

3 files changed

Lines changed: 19 additions & 6 deletions

File tree

java-jsp/src/main/java/org/sonar/java/jsp/Jasper.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@
6363
@ScannerSide
6464
public class Jasper {
6565

66+
public static final String JSP_LANGUAGE_KEY = "jsp";
67+
6668
private static final String SONAR_EXCLUSIONS_PROPERTY = "sonar.exclusions";
6769

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

190192
private static List<InputFile> jspFiles(FileSystem fs) {
191-
Iterable<InputFile> inputFiles = fs.inputFiles(fs.predicates().hasLanguage("jsp"));
193+
Iterable<InputFile> inputFiles = fs.inputFiles(fs.predicates().hasLanguage(JSP_LANGUAGE_KEY));
192194
return StreamSupport.stream(inputFiles.spliterator(), false)
193195
.toList();
194196
}

sonar-java-plugin/src/main/java/org/sonar/plugins/java/JavaSensor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public JavaSensor(SonarComponents sonarComponents, FileSystem fs, JavaResourceLo
9696

9797
@Override
9898
public void describe(SensorDescriptor descriptor) {
99-
descriptor.onlyOnLanguage(Java.KEY).name("JavaSensor");
99+
descriptor.onlyOnLanguages(Java.KEY, Jasper.JSP_LANGUAGE_KEY).name("JavaSensor");
100100
}
101101

102102
@Override

sonar-java-plugin/src/test/java/org/sonar/plugins/java/JavaSensorTest.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.sonar.api.batch.rule.Checks;
4646
import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
4747
import org.sonar.api.batch.rule.internal.NewActiveRule;
48+
import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
4849
import org.sonar.api.batch.sensor.internal.SensorContextTester;
4950
import org.sonar.api.config.Configuration;
5051
import org.sonar.api.config.internal.MapSettings;
@@ -112,8 +113,8 @@ void test_toString() throws IOException {
112113

113114
@Test
114115
void test_issues_creation_on_main_file() throws IOException {
115-
// Expected issues : the number of methods violating BadMethodName rule. Currently, 17 tests.
116-
testIssueCreation(InputFile.Type.MAIN, 17);
116+
// Expected issues : the number of methods violating BadMethodName rule. Currently, 18 tests.
117+
testIssueCreation(InputFile.Type.MAIN, 18);
117118
}
118119

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

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

139140
settings.setProperty(JavaVersion.SOURCE_VERSION, "wrongFormat");
@@ -434,6 +435,16 @@ void filter_checks_when_autoscan_true() throws IOException {
434435
);
435436
}
436437

438+
@Test
439+
void test_describe_sensor() throws IOException {
440+
DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor();
441+
SonarComponents sonarComponents = createSonarComponentsMock(createContext(InputFile.Type.MAIN));
442+
var sensor = new JavaSensor(sonarComponents, null, null, null, null, null);
443+
sensor.describe(descriptor);
444+
assertThat(descriptor.name()).isEqualTo("JavaSensor");
445+
assertThat(descriptor.languages()).containsExactly("java", "jsp");
446+
}
447+
437448
private SensorContextTester analyzeTwoFilesWithIssues(MapSettings settings) throws IOException {
438449
SensorContextTester context = SensorContextTester.create(new File("src/test/files").getAbsoluteFile())
439450
.setSettings(settings)

0 commit comments

Comments
 (0)