diff options
author | OrlovAlexander <35396155+OrlovAlexander85@users.noreply.github.com> | 2024-04-09 13:35:47 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2024-04-10 20:02:56 +0000 |
commit | 65962904f7e8992d6c4abae3e5507abdfdd3ba31 (patch) | |
tree | bd69ca071192116f5c98c9f67c0e1ead07391dcf /sonar-plugin-api-impl/src | |
parent | 0072c50e15384396032de42e12f95d7449c35c22 (diff) | |
download | sonarqube-65962904f7e8992d6c4abae3e5507abdfdd3ba31.tar.gz sonarqube-65962904f7e8992d6c4abae3e5507abdfdd3ba31.zip |
SONAR-22017 Add versionEOL to GlobalAction
Diffstat (limited to 'sonar-plugin-api-impl/src')
-rw-r--r-- | sonar-plugin-api-impl/src/main/java/org/sonar/api/internal/MetadataLoader.java | 26 | ||||
-rw-r--r-- | sonar-plugin-api-impl/src/test/java/org/sonar/api/internal/MetadataLoaderTest.java | 29 |
2 files changed, 39 insertions, 16 deletions
diff --git a/sonar-plugin-api-impl/src/main/java/org/sonar/api/internal/MetadataLoader.java b/sonar-plugin-api-impl/src/main/java/org/sonar/api/internal/MetadataLoader.java index ad8e12b610a..64e6203fbd2 100644 --- a/sonar-plugin-api-impl/src/main/java/org/sonar/api/internal/MetadataLoader.java +++ b/sonar-plugin-api-impl/src/main/java/org/sonar/api/internal/MetadataLoader.java @@ -28,6 +28,7 @@ import org.sonar.api.SonarEdition; import org.sonar.api.utils.System2; import org.sonar.api.utils.Version; +import static java.lang.String.format; import static org.apache.commons.lang3.StringUtils.trimToEmpty; /** @@ -40,6 +41,8 @@ public class MetadataLoader { private static final String SQ_VERSION_FILE_PATH = "/sq-version.txt"; private static final String SONAR_API_VERSION_FILE_PATH = "/sonar-api-version.txt"; private static final String EDITION_FILE_PATH = "/sonar-edition.txt"; + private static final String SQ_VERSION_EOL_FILE_PATH = "/sq-version-eol.txt"; + public static final String CAN_NOT_LOAD_FROM_CLASSPATH = "Can not load %s from classpath"; private MetadataLoader() { // only static methods @@ -48,18 +51,25 @@ public class MetadataLoader { public static Version loadApiVersion(System2 system) { return getVersion(system, SONAR_API_VERSION_FILE_PATH); } + public static Version loadSQVersion(System2 system) { return getVersion(system, SQ_VERSION_FILE_PATH); } + public static String loadSqVersionEol(System2 system) { + return getParamFromFile(system, SQ_VERSION_EOL_FILE_PATH); + } + private static Version getVersion(System2 system, String versionFilePath) { - URL url = system.getResource(versionFilePath); + return Version.parse(getParamFromFile(system, versionFilePath)); + } - try (Scanner scanner = new Scanner(url.openStream(), StandardCharsets.UTF_8.name())) { - String versionInFile = scanner.nextLine(); - return Version.parse(versionInFile); + private static String getParamFromFile(System2 system, String filePath) { + URL url = system.getResource(filePath); + try (Scanner scanner = new Scanner(url.openStream(), StandardCharsets.UTF_8)) { + return scanner.nextLine(); } catch (IOException e) { - throw new IllegalStateException("Can not load " + versionFilePath + " from classpath ", e); + throw new IllegalStateException(format(CAN_NOT_LOAD_FROM_CLASSPATH, filePath), e); } } @@ -68,11 +78,11 @@ public class MetadataLoader { if (url == null) { return SonarEdition.COMMUNITY; } - try (Scanner scanner = new Scanner(url.openStream(), StandardCharsets.UTF_8.name())) { + try (Scanner scanner = new Scanner(url.openStream(), StandardCharsets.UTF_8)) { String editionInFile = scanner.nextLine(); return parseEdition(editionInFile); } catch (IOException e) { - throw new IllegalStateException("Can not load " + EDITION_FILE_PATH + " from classpath", e); + throw new IllegalStateException(format(CAN_NOT_LOAD_FROM_CLASSPATH, EDITION_FILE_PATH), e); } } @@ -81,7 +91,7 @@ public class MetadataLoader { try { return SonarEdition.valueOf(str); } catch (IllegalArgumentException e) { - throw new IllegalStateException(String.format("Invalid edition found in '%s': '%s'", EDITION_FILE_PATH, str)); + throw new IllegalStateException(format("Invalid edition found in '%s': '%s'", EDITION_FILE_PATH, str)); } } } diff --git a/sonar-plugin-api-impl/src/test/java/org/sonar/api/internal/MetadataLoaderTest.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/internal/MetadataLoaderTest.java index 3ee4ec5df9a..017bfca8777 100644 --- a/sonar-plugin-api-impl/src/test/java/org/sonar/api/internal/MetadataLoaderTest.java +++ b/sonar-plugin-api-impl/src/test/java/org/sonar/api/internal/MetadataLoaderTest.java @@ -21,7 +21,7 @@ package org.sonar.api.internal; import java.io.File; import java.net.MalformedURLException; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.sonar.api.SonarEdition; import org.sonar.api.utils.System2; import org.sonar.api.utils.Version; @@ -32,45 +32,45 @@ import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -public class MetadataLoaderTest { +class MetadataLoaderTest { private final System2 system = mock(System2.class); @Test - public void load_api_version_from_file_in_classpath() { + void load_api_version_from_file_in_classpath() { Version version = MetadataLoader.loadApiVersion(System2.INSTANCE); assertThat(version).isNotNull(); assertThat(version.major()).isGreaterThanOrEqualTo(5); } @Test - public void load_sq_version_from_file_in_classpath() { + void load_sq_version_from_file_in_classpath() { Version version = MetadataLoader.loadSQVersion(System2.INSTANCE); assertThat(version).isNotNull(); assertThat(version.major()).isGreaterThanOrEqualTo(5); } @Test - public void load_edition_from_file_in_classpath() { + void load_edition_from_file_in_classpath() { SonarEdition edition = MetadataLoader.loadEdition(System2.INSTANCE); assertThat(edition).isNotNull(); } @Test - public void load_edition_defaults_to_community_if_file_not_found() throws MalformedURLException { + void load_edition_defaults_to_community_if_file_not_found() throws MalformedURLException { when(system.getResource(anyString())).thenReturn(new File("target/unknown").toURI().toURL()); SonarEdition edition = MetadataLoader.loadEdition(System2.INSTANCE); assertThat(edition).isEqualTo(SonarEdition.COMMUNITY); } @Test - public void throw_ISE_if_edition_is_invalid() { + void throw_ISE_if_edition_is_invalid() { assertThatThrownBy(() -> MetadataLoader.parseEdition("trash")) .isInstanceOf(IllegalStateException.class) .hasMessage("Invalid edition found in '/sonar-edition.txt': 'TRASH'"); } @Test - public void throw_ISE_if_fail_to_load_version() throws Exception { + void throw_ISE_if_fail_to_load_version() throws Exception { when(system.getResource(anyString())).thenReturn(new File("target/unknown").toURI().toURL()); assertThatThrownBy(() -> MetadataLoader.loadApiVersion(system)) @@ -78,4 +78,17 @@ public class MetadataLoaderTest { .hasMessageContaining("Can not load /sonar-api-version.txt from classpath"); } + @Test + void loadSqVersionEol_shouldLoadCorrectEol() { + String eol = MetadataLoader.loadSqVersionEol(System2.INSTANCE); + assertThat(eol).isNotNull(); + } + + @Test + void loadSqVersionEol_whenFileNotFound_shouldThrowException() throws MalformedURLException { + when(system.getResource(anyString())).thenReturn(new File("target/unknown").toURI().toURL()); + assertThatThrownBy(() -> MetadataLoader.loadSqVersionEol(system)) + .isInstanceOf(IllegalStateException.class) + .hasMessageContaining("Can not load /sq-version-eol.txt from classpath"); + } } |