|
| 1 | +/* |
| 2 | + * SonarLint Core - Medium Tests |
| 3 | + * Copyright (C) 2016-2025 SonarSource SA |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 3 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this program; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | + */ |
| 20 | +package mediumtest.analysis; |
| 21 | + |
| 22 | +import java.nio.file.Path; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.UUID; |
| 26 | +import java.util.concurrent.TimeUnit; |
| 27 | +import mediumtest.analysis.sensor.WaitingCancellationSensor; |
| 28 | +import org.junit.jupiter.api.io.TempDir; |
| 29 | +import org.sonarsource.sonarlint.core.rpc.protocol.backend.analysis.AnalyzeFilesAndTrackParams; |
| 30 | +import org.sonarsource.sonarlint.core.rpc.protocol.common.ClientFileDto; |
| 31 | +import org.sonarsource.sonarlint.core.rpc.protocol.common.Language; |
| 32 | +import org.sonarsource.sonarlint.core.test.utils.junit5.SonarLintTest; |
| 33 | +import org.sonarsource.sonarlint.core.test.utils.junit5.SonarLintTestHarness; |
| 34 | + |
| 35 | +import static mediumtest.analysis.sensor.WaitingCancellationSensor.CANCELLATION_FILE_PATH_PROPERTY_NAME; |
| 36 | +import static org.assertj.core.api.Assertions.assertThat; |
| 37 | +import static org.awaitility.Awaitility.await; |
| 38 | +import static org.sonarsource.sonarlint.core.test.utils.plugins.SonarPluginBuilder.newSonarPlugin; |
| 39 | +import static utils.AnalysisUtils.createFile; |
| 40 | + |
| 41 | +class AnalysisCancellationMediumTests { |
| 42 | + |
| 43 | + private static final String CONFIG_SCOPE_ID = "CONFIG_SCOPE_ID"; |
| 44 | + |
| 45 | + @SonarLintTest |
| 46 | + void it_should_analyze_file_on_open(SonarLintTestHarness harness, @TempDir Path baseDir) throws InterruptedException { |
| 47 | + var filePath = createFile(baseDir, "pom.xml", ""); |
| 48 | + var fileUri = filePath.toUri(); |
| 49 | + var client = harness.newFakeClient() |
| 50 | + .withInitialFs(CONFIG_SCOPE_ID, baseDir, List.of(new ClientFileDto(fileUri, baseDir.relativize(filePath), CONFIG_SCOPE_ID, false, |
| 51 | + null, filePath, null, null, true))) |
| 52 | + .build(); |
| 53 | + var plugin = newSonarPlugin("xml") |
| 54 | + .withSensor(WaitingCancellationSensor.class) |
| 55 | + .generate(baseDir); |
| 56 | + var backend = harness.newBackend() |
| 57 | + .withUnboundConfigScope(CONFIG_SCOPE_ID) |
| 58 | + .withStandaloneEmbeddedPlugin(plugin) |
| 59 | + .withEnabledLanguageInStandaloneMode(Language.XML) |
| 60 | + .start(client); |
| 61 | + var cancelationFilePath = baseDir.resolve("cancellation.result"); |
| 62 | + var future = backend.getAnalysisService() |
| 63 | + .analyzeFilesAndTrack(new AnalyzeFilesAndTrackParams(CONFIG_SCOPE_ID, UUID.randomUUID(), List.of(fileUri), |
| 64 | + Map.of(CANCELLATION_FILE_PATH_PROPERTY_NAME, cancelationFilePath.toString()), false, System.currentTimeMillis())); |
| 65 | + Thread.sleep(500); |
| 66 | + |
| 67 | + future.cancel(false); |
| 68 | + |
| 69 | + assertThat(future.isCancelled()).isTrue(); |
| 70 | + await().atMost(3, TimeUnit.SECONDS) |
| 71 | + .untilAsserted(() -> assertThat(cancelationFilePath).hasContent("CANCELED")); |
| 72 | + } |
| 73 | +} |
0 commit comments