Search in sources :

Example 1 with ApiMismatchException

use of org.sonar.java.exceptions.ApiMismatchException in project sonar-java by SonarSource.

the class VisitorsBridgeTest method canSkipScanningOfUnchangedFiles_returns_based_on_context.

@Test
void canSkipScanningOfUnchangedFiles_returns_based_on_context() throws ApiMismatchException {
    SonarComponents sonarComponents = mock(SonarComponents.class);
    VisitorsBridge vb = new VisitorsBridge(Collections.emptyList(), Collections.emptyList(), sonarComponents);
    doReturn(true).when(sonarComponents).canSkipUnchangedFiles();
    assertThat(vb.canSkipScanningOfUnchangedFiles()).isTrue();
    doReturn(false).when(sonarComponents).canSkipUnchangedFiles();
    assertThat(vb.canSkipScanningOfUnchangedFiles()).isFalse();
    ApiMismatchException exception = new ApiMismatchException(new NoSuchMethodError());
    doThrow(exception).when(sonarComponents).canSkipUnchangedFiles();
    assertThat(vb.canSkipScanningOfUnchangedFiles()).isFalse();
}
Also used : SonarComponents(org.sonar.java.SonarComponents) ApiMismatchException(org.sonar.java.exceptions.ApiMismatchException) Test(org.junit.jupiter.api.Test)

Example 2 with ApiMismatchException

use of org.sonar.java.exceptions.ApiMismatchException in project sonar-java by SonarSource.

the class SonarComponentsTest method fileCanBeSkipped_returns_false_when_canSkipUnchangedFile_isFalse.

@Test
void fileCanBeSkipped_returns_false_when_canSkipUnchangedFile_isFalse() throws ApiMismatchException {
    SonarComponents sonarComponents = mock(SonarComponents.class, CALLS_REAL_METHODS);
    ApiMismatchException apiMismatchException = new ApiMismatchException(new NoSuchMethodError("API version mismatch :-("));
    doThrow(apiMismatchException).when(sonarComponents).canSkipUnchangedFiles();
    assertThat(sonarComponents.fileCanBeSkipped(mock(InputFile.class))).isFalse();
}
Also used : ApiMismatchException(org.sonar.java.exceptions.ApiMismatchException) InputFile(org.sonar.api.batch.fs.InputFile) Test(org.junit.jupiter.api.Test) ClasspathForTest(org.sonar.java.classpath.ClasspathForTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with ApiMismatchException

use of org.sonar.java.exceptions.ApiMismatchException in project sonar-java by SonarSource.

the class SonarComponentsTest method skipUnchangedFiles_throws_a_NoSuchMethodError_when_canSkipUnchangedFiles_not_in_API.

@Test
void skipUnchangedFiles_throws_a_NoSuchMethodError_when_canSkipUnchangedFiles_not_in_API() {
    SensorContextTester sensorContextTester = SensorContextTester.create(new File(""));
    SonarComponents sonarComponents = new SonarComponents(fileLinesContextFactory, sensorContextTester.fileSystem(), mock(ClasspathForMain.class), mock(ClasspathForTest.class), checkFactory);
    IncrementalAnalysisSensorContext context = mock(IncrementalAnalysisSensorContext.class);
    when(context.canSkipUnchangedFiles()).thenThrow(new NoSuchMethodError("API version mismatch :-("));
    sonarComponents.setSensorContext(context);
    ApiMismatchException error = assertThrows(ApiMismatchException.class, sonarComponents::canSkipUnchangedFiles);
    assertThat(error).hasCause(new NoSuchMethodError("API version mismatch :-("));
}
Also used : SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) ClasspathForTest(org.sonar.java.classpath.ClasspathForTest) ApiMismatchException(org.sonar.java.exceptions.ApiMismatchException) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) ClasspathForMain(org.sonar.java.classpath.ClasspathForMain) Test(org.junit.jupiter.api.Test) ClasspathForTest(org.sonar.java.classpath.ClasspathForTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with ApiMismatchException

use of org.sonar.java.exceptions.ApiMismatchException in project sonar-java by SonarSource.

the class SonarComponentsTest method canSkipUnchangedFiles.

@ParameterizedTest
@MethodSource("provideInputsFor_canSkipUnchangedFiles")
void canSkipUnchangedFiles(@CheckForNull Boolean overrideFlagVal, @CheckForNull Boolean apiResponseVal, @CheckForNull Boolean expectedResult) throws ApiMismatchException {
    SensorContextTester sensorContextTester = SensorContextTester.create(new File(""));
    SonarComponents sonarComponents = new SonarComponents(fileLinesContextFactory, sensorContextTester.fileSystem(), mock(ClasspathForMain.class), mock(ClasspathForTest.class), checkFactory);
    IncrementalAnalysisSensorContext context = mock(IncrementalAnalysisSensorContext.class);
    Configuration config = mock(Configuration.class);
    when(context.config()).thenReturn(config);
    when(config.getBoolean(any())).thenReturn(Optional.ofNullable(overrideFlagVal));
    if (apiResponseVal == null) {
        lenient().when(context.canSkipUnchangedFiles()).thenThrow(new NoSuchMethodError("API version mismatch :-("));
    } else {
        lenient().when(context.canSkipUnchangedFiles()).thenReturn(apiResponseVal);
    }
    sonarComponents.setSensorContext(context);
    if (expectedResult == null) {
        ApiMismatchException noSuchMethodError = assertThrows(ApiMismatchException.class, sonarComponents::canSkipUnchangedFiles);
        assertThat(noSuchMethodError).hasCause(new NoSuchMethodError("API version mismatch :-("));
    } else {
        assertThat(sonarComponents.canSkipUnchangedFiles()).isEqualTo(expectedResult);
    }
}
Also used : SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) Configuration(org.sonar.api.config.Configuration) ClasspathForTest(org.sonar.java.classpath.ClasspathForTest) ApiMismatchException(org.sonar.java.exceptions.ApiMismatchException) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) ClasspathForMain(org.sonar.java.classpath.ClasspathForMain) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 5 with ApiMismatchException

use of org.sonar.java.exceptions.ApiMismatchException in project sonar-java by SonarSource.

the class SonarComponentsTest method fileCanBeSkipped_only_logs_on_first_call_input.

private static Stream<Arguments> fileCanBeSkipped_only_logs_on_first_call_input() throws ApiMismatchException {
    ApiMismatchException apiMismatchException = new ApiMismatchException(new NoSuchMethodError("API version mismatch :-("));
    SonarComponents sonarComponentsThatCanSkipFiles = mock(SonarComponents.class, CALLS_REAL_METHODS);
    doReturn(true).when(sonarComponentsThatCanSkipFiles).canSkipUnchangedFiles();
    SonarComponents sonarComponentsThatCannotSkipFiles = mock(SonarComponents.class, CALLS_REAL_METHODS);
    doReturn(false).when(sonarComponentsThatCannotSkipFiles).canSkipUnchangedFiles();
    SonarComponents sonarComponentsWithApiMismatch = mock(SonarComponents.class, CALLS_REAL_METHODS);
    doThrow(apiMismatchException).when(sonarComponentsWithApiMismatch).canSkipUnchangedFiles();
    InputFile inputFile = mock(InputFile.class);
    doReturn(InputFile.Status.SAME).when(inputFile).status();
    return Stream.of(Arguments.of(sonarComponentsThatCanSkipFiles, inputFile, LOG_MESSAGE_FILES_CAN_BE_SKIPPED), Arguments.of(sonarComponentsThatCannotSkipFiles, mock(InputFile.class), LOG_MESSAGE_FILES_CANNOT_BE_SKIPPED), Arguments.of(sonarComponentsWithApiMismatch, mock(InputFile.class), LOG_MESSAGE_CANNOT_DETERMINE_IF_FILES_CAN_BE_SKIPPED));
}
Also used : ApiMismatchException(org.sonar.java.exceptions.ApiMismatchException) InputFile(org.sonar.api.batch.fs.InputFile)

Aggregations

ApiMismatchException (org.sonar.java.exceptions.ApiMismatchException)5 InputFile (org.sonar.api.batch.fs.InputFile)4 Test (org.junit.jupiter.api.Test)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 ClasspathForTest (org.sonar.java.classpath.ClasspathForTest)3 File (java.io.File)2 SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)2 ClasspathForMain (org.sonar.java.classpath.ClasspathForMain)2 MethodSource (org.junit.jupiter.params.provider.MethodSource)1 Configuration (org.sonar.api.config.Configuration)1 SonarComponents (org.sonar.java.SonarComponents)1