Skip to content

Commit

Permalink
SONARJAVA-4091 Use of Java 17 feature should not lead to a warning me…
Browse files Browse the repository at this point in the history
…ssage
  • Loading branch information
mpaladin authored and quentin-jaquier-sonarsource committed Nov 26, 2021
1 parent 21aa5a6 commit ab57ed8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

public abstract class JParserConfig {

public static final String MAXIMUM_SUPPORTED_JAVA_VERSION = "16";
public static final String MAXIMUM_SUPPORTED_JAVA_VERSION = Integer.toString(JavaVersionImpl.MAX_SUPPORTED);

private static final Logger LOG = Loggers.get(JParserConfig.class);

Expand Down
31 changes: 30 additions & 1 deletion java-frontend/src/test/java/org/sonar/java/JavaFrontendTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,13 @@
import org.sonar.plugins.java.api.JavaFileScanner;
import org.sonar.plugins.java.api.JavaFileScannerContext;
import org.sonar.plugins.java.api.JavaResourceLocator;
import org.sonar.plugins.java.api.JavaVersion;
import org.sonar.plugins.java.api.tree.CompilationUnitTree;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
Expand Down Expand Up @@ -267,6 +270,31 @@ void exceptions_outside_rules_as_batch_should_interrupt_analysis_if_fail_fast()
.hasMessage("Batch Mode failed, analysis of Java Files stopped.");
}

@Test
void test_preview_feature_log_message() throws IOException {
logTester.setLevel(LoggerLevel.DEBUG);
scan(new MapSettings().setProperty(JavaVersion.SOURCE_VERSION, "16"),
"sealed class Shape { } sealed class Circle extends Shape { }");
assertThat(sensorContext.allAnalysisErrors()).isEmpty();
assertTrue(logTester.logs(LoggerLevel.WARN).stream().anyMatch(l -> l.endsWith("Unresolved imports/types have been detected during analysis. Enable DEBUG mode to see them.")));
// We should keep this message or we won't have anything actionable in the debug logs to understand the warning
assertTrue(logTester.logs(LoggerLevel.DEBUG).stream().anyMatch(l -> l.replace("\r\n", "\n").endsWith("Unresolved imports/types:\n" +
"- Sealed Types is a preview feature and disabled by default. Use --enable-preview to enable")));
assertThat(mainCodeIssueScannerAndFilter.scanFileInvocationCount).isEqualTo(1);
assertThat(testCodeIssueScannerAndFilter.scanFileInvocationCount).isZero();
}

@Test
void test_java17_feature() throws IOException {
logTester.setLevel(LoggerLevel.DEBUG);
scan(new MapSettings().setProperty(JavaVersion.SOURCE_VERSION, "17"),
"sealed class Shape { } sealed class Circle extends Shape { }");
String allLogs = String.join("\n", logTester.logs());
assertFalse(allLogs.contains("Unresolved imports/types"));
assertThat(mainCodeIssueScannerAndFilter.scanFileInvocationCount).isEqualTo(1);
assertThat(testCodeIssueScannerAndFilter.scanFileInvocationCount).isZero();
}

private List<InputFile> scan(String... codeList) throws IOException {
return scan(new MapSettings(), codeList);
}
Expand Down Expand Up @@ -305,7 +333,8 @@ private List<InputFile> scan(MapSettings settings, List<InputFile> inputFiles) t
sonarComponents.setSensorContext(sensorContext);
sonarComponents.mainChecks().add(mainCodeIssueScannerAndFilter);
sonarComponents.testChecks().add(testCodeIssueScannerAndFilter);
JavaFrontend frontend = new JavaFrontend(new JavaVersionImpl(), sonarComponents, new Measurer(sensorContext, mock(NoSonarFilter.class)), mock(JavaResourceLocator.class),
JavaVersion javaVersion = JavaVersionImpl.fromString(settings.asConfig().get(JavaVersion.SOURCE_VERSION).orElse(null));
JavaFrontend frontend = new JavaFrontend(javaVersion, sonarComponents, new Measurer(sensorContext, mock(NoSonarFilter.class)), mock(JavaResourceLocator.class),
null, mainCodeIssueScannerAndFilter);
frontend.scan(inputFiles, Collections.emptyList(), Collections.emptyList());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class JParserConfigTest {

@Test
void effective_java_version() {
assertThat(effectiveJavaVersion(null)).isEqualTo("16");
assertThat(effectiveJavaVersion(new JavaVersionImpl())).isEqualTo("16");
assertThat(effectiveJavaVersion(null)).isEqualTo("17");
assertThat(effectiveJavaVersion(new JavaVersionImpl())).isEqualTo("17");
assertThat(effectiveJavaVersion(new JavaVersionImpl(10))).isEqualTo("10");
}

Expand Down

0 comments on commit ab57ed8

Please sign in to comment.