aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src/main/java/org/sonar
diff options
context:
space:
mode:
authorMatteo Mara <matteo.mara@sonarsource.com>2023-12-19 10:13:02 +0100
committersonartech <sonartech@sonarsource.com>2023-12-21 20:02:33 +0000
commit6e57d86a3edb938012009bdd47bcbaf77a07acc8 (patch)
treeabeb7cfee08a6be01a5a0dbdfdc703e820f1e5d0 /sonar-scanner-engine/src/main/java/org/sonar
parent43f2e06d649bd047963863637ad95bdb9fdfa04e (diff)
downloadsonarqube-6e57d86a3edb938012009bdd47bcbaf77a07acc8.tar.gz
sonarqube-6e57d86a3edb938012009bdd47bcbaf77a07acc8.zip
SONAR-21195 Make DefaultLanguagesRepository more suitable for tests
Diffstat (limited to 'sonar-scanner-engine/src/main/java/org/sonar')
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/SpringScannerContainer.java9
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakeLanguagesLoader.java (renamed from sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakeLanguagesRepository.java)24
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/language/DefaultLanguagesLoader.java96
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/language/DefaultLanguagesRepository.java63
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/language/LanguagesLoader.java28
5 files changed, 138 insertions, 82 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/SpringScannerContainer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/SpringScannerContainer.java
index 104e6958eaa..d36a9a37c84 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/SpringScannerContainer.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/SpringScannerContainer.java
@@ -85,8 +85,8 @@ import org.sonar.scanner.repository.DefaultQualityProfileLoader;
import org.sonar.scanner.repository.ProjectRepositoriesProvider;
import org.sonar.scanner.repository.QualityProfilesProvider;
import org.sonar.scanner.repository.ReferenceBranchSupplier;
+import org.sonar.scanner.repository.language.DefaultLanguagesLoader;
import org.sonar.scanner.repository.language.DefaultLanguagesRepository;
-import org.sonar.scanner.repository.language.LanguagesRepository;
import org.sonar.scanner.repository.settings.DefaultProjectSettingsLoader;
import org.sonar.scanner.rule.ActiveRulesProvider;
import org.sonar.scanner.rule.DefaultActiveRulesLoader;
@@ -302,10 +302,9 @@ public class SpringScannerContainer extends SpringComponentContainer {
add(DefaultProjectSettingsLoader.class,
DefaultActiveRulesLoader.class,
DefaultQualityProfileLoader.class,
- DefaultProjectRepositoriesLoader.class);
-
- addIfMissing(DefaultLanguagesRepository.class, LanguagesRepository.class);
-
+ DefaultProjectRepositoriesLoader.class,
+ DefaultLanguagesLoader.class,
+ DefaultLanguagesRepository.class);
}
static ExtensionMatcher getScannerProjectExtensionsFilter() {
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakeLanguagesRepository.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakeLanguagesLoader.java
index 7443db29fa3..15153900f80 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakeLanguagesRepository.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakeLanguagesLoader.java
@@ -19,43 +19,31 @@
*/
package org.sonar.scanner.mediumtest;
-import java.util.Collection;
-import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Priority;
-import org.jetbrains.annotations.Nullable;
import org.sonar.api.resources.Languages;
import org.sonar.scanner.repository.language.Language;
-import org.sonar.scanner.repository.language.LanguagesRepository;
+import org.sonar.scanner.repository.language.LanguagesLoader;
import org.sonar.scanner.repository.language.SupportedLanguageDto;
@Priority(1)
-public class FakeLanguagesRepository implements LanguagesRepository {
+public class FakeLanguagesLoader implements LanguagesLoader {
private final Map<String, Language> languageMap = new HashMap<>();
- public FakeLanguagesRepository() {
+ public FakeLanguagesLoader() {
languageMap.put("xoo", new Language(new FakeLanguage("xoo", "xoo", new String[] { ".xoo" }, new String[0], true)));
}
- public FakeLanguagesRepository(Languages languages) {
+ public FakeLanguagesLoader(Languages languages) {
for (org.sonar.api.resources.Language language : languages.all()) {
languageMap.put(language.getKey(), new Language(new FakeLanguage(language.getKey(), language.getName(), language.getFileSuffixes(), language.filenamePatterns(), true)));
}
}
-
- @Nullable
- @Override
- public Language get(String languageKey) {
- return languageMap.get(languageKey);
- }
-
@Override
- public Collection<Language> all() {
- return languageMap.values().stream()
- // sorted for test consistency
- .sorted(Comparator.comparing(Language::key)).toList();
+ public Map<String, Language> load() {
+ return languageMap;
}
public void addLanguage(String key, String name, String[] suffixes, String[] filenamePatterns) {
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/language/DefaultLanguagesLoader.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/language/DefaultLanguagesLoader.java
new file mode 100644
index 00000000000..91ba3a58248
--- /dev/null
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/language/DefaultLanguagesLoader.java
@@ -0,0 +1,96 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2023 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.scanner.repository.language;
+
+import com.google.gson.Gson;
+import java.io.Reader;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.sonar.api.config.Configuration;
+import org.sonar.scanner.bootstrap.DefaultScannerWsClient;
+import org.sonarqube.ws.client.GetRequest;
+
+public class DefaultLanguagesLoader implements LanguagesLoader {
+
+ private static final Logger LOG = LoggerFactory.getLogger(DefaultLanguagesLoader.class);
+ private static final String LANGUAGES_WS_URL = "/api/languages/list";
+ private static final Map<String, String> PROPERTY_FRAGMENT_MAP = Map.of(
+ "js", "javascript",
+ "ts", "typescript",
+ "py", "python",
+ "web", "html"
+ );
+
+ private final DefaultScannerWsClient wsClient;
+
+ private final Configuration properties;
+
+ public DefaultLanguagesLoader(DefaultScannerWsClient wsClient, Configuration properties) {
+ this.wsClient = wsClient;
+ this.properties = properties;
+ }
+
+ @Override
+ public Map<String, Language> load() {
+ GetRequest getRequest = new GetRequest(LANGUAGES_WS_URL);
+ LanguagesWSResponse response;
+ try (Reader reader = wsClient.call(getRequest).contentReader()) {
+ response = new Gson().fromJson(reader, LanguagesWSResponse.class);
+ } catch (Exception e) {
+ throw new IllegalStateException("Fail to parse response of " + LANGUAGES_WS_URL, e);
+ }
+
+ return response.languages.stream()
+ .map(this::populateFileSuffixesAndPatterns)
+ .collect(Collectors.toMap(Language::key, Function.identity()));
+
+ }
+
+ private Language populateFileSuffixesAndPatterns(SupportedLanguageDto lang) {
+ lang.setFileSuffixes(getFileSuffixes(lang.getKey()));
+ lang.setFilenamePatterns(getFilenamePatterns(lang.getKey()));
+ if (lang.filenamePatterns() == null && lang.getFileSuffixes() == null) {
+ LOG.debug("Language '{}' cannot be detected as it has neither suffixes nor patterns.", lang.getName());
+ }
+ return new Language(lang);
+ }
+
+
+ private String[] getFileSuffixes(String languageKey) {
+ return getPropertyForLanguage("sonar.%s.file.suffixes", languageKey);
+ }
+
+ private String[] getFilenamePatterns(String languageKey) {
+ return getPropertyForLanguage("sonar.%s.file.patterns", languageKey);
+ }
+
+ private String[] getPropertyForLanguage(String propertyPattern, String languageKey) {
+ String propName = String.format(propertyPattern, PROPERTY_FRAGMENT_MAP.getOrDefault(languageKey, languageKey));
+ return properties.getStringArray(propName);
+ }
+
+ private static class LanguagesWSResponse {
+ List<SupportedLanguageDto> languages;
+ }
+}
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/language/DefaultLanguagesRepository.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/language/DefaultLanguagesRepository.java
index c3505ebbfbf..f598cdd8817 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/language/DefaultLanguagesRepository.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/language/DefaultLanguagesRepository.java
@@ -19,23 +19,13 @@
*/
package org.sonar.scanner.repository.language;
-import com.google.gson.Gson;
-import java.io.Reader;
import java.util.Collection;
import java.util.HashMap;
-import java.util.List;
import java.util.Map;
-import java.util.function.Function;
-import java.util.stream.Collectors;
import javax.annotation.CheckForNull;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import javax.annotation.concurrent.Immutable;
import org.sonar.api.Startable;
-import org.sonar.api.config.Configuration;
import org.sonar.api.resources.Languages;
-import org.sonar.scanner.bootstrap.DefaultScannerWsClient;
-import org.sonarqube.ws.client.GetRequest;
/**
* Languages repository using {@link Languages}
@@ -43,59 +33,17 @@ import org.sonarqube.ws.client.GetRequest;
*/
@Immutable
public class DefaultLanguagesRepository implements LanguagesRepository, Startable {
- private static final Logger LOG = LoggerFactory.getLogger(DefaultLanguagesRepository.class);
- private static final String LANGUAGES_WS_URL = "/api/languages/list";
- private static final Map<String, String> PROPERTY_FRAGMENT_MAP = Map.of(
- "js", "javascript",
- "ts", "typescript",
- "py", "python",
- "web", "html"
- );
private final Map<String, Language> languages = new HashMap<>();
- private final DefaultScannerWsClient wsClient;
- private final Configuration properties;
+ private final LanguagesLoader languagesLoader;
- public DefaultLanguagesRepository(DefaultScannerWsClient wsClient, Configuration properties) {
- this.wsClient = wsClient;
- this.properties = properties;
+ public DefaultLanguagesRepository(LanguagesLoader languagesLoader) {
+ this.languagesLoader = languagesLoader;
}
@Override
public void start() {
- GetRequest getRequest = new GetRequest(LANGUAGES_WS_URL);
- LanguagesWSResponse response;
- try (Reader reader = wsClient.call(getRequest).contentReader()) {
- response = new Gson().fromJson(reader, LanguagesWSResponse.class);
- } catch (Exception e) {
- throw new IllegalStateException("Fail to parse response of " + LANGUAGES_WS_URL, e);
- }
-
- languages.putAll(response.languages.stream()
- .map(this::populateFileSuffixesAndPatterns)
- .collect(Collectors.toMap(Language::key, Function.identity())));
- }
-
- private Language populateFileSuffixesAndPatterns(SupportedLanguageDto lang) {
- lang.setFileSuffixes(getFileSuffixes(lang.getKey()));
- lang.setFilenamePatterns(getFilenamePatterns(lang.getKey()));
- if (lang.filenamePatterns() == null && lang.getFileSuffixes() == null) {
- LOG.debug("Language '{}' cannot be detected as it has neither suffixes nor patterns.", lang.getName());
- }
- return new Language(lang);
- }
-
- private String[] getFileSuffixes(String languageKey) {
- return getPropertyForLanguage("sonar.%s.file.suffixes", languageKey);
- }
-
- private String[] getFilenamePatterns(String languageKey) {
- return getPropertyForLanguage("sonar.%s.file.patterns", languageKey);
- }
-
- private String[] getPropertyForLanguage(String propertyPattern, String languageKey) {
- String propName = String.format(propertyPattern, PROPERTY_FRAGMENT_MAP.getOrDefault(languageKey, languageKey));
- return properties.getStringArray(propName);
+ languages.putAll(languagesLoader.load());
}
/**
@@ -120,8 +68,5 @@ public class DefaultLanguagesRepository implements LanguagesRepository, Startabl
// nothing to do
}
- private static class LanguagesWSResponse {
- List<SupportedLanguageDto> languages;
- }
}
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/language/LanguagesLoader.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/language/LanguagesLoader.java
new file mode 100644
index 00000000000..51be98d468c
--- /dev/null
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/language/LanguagesLoader.java
@@ -0,0 +1,28 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2023 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.scanner.repository.language;
+
+import java.util.Map;
+
+public interface LanguagesLoader {
+
+ Map<String, Language> load();
+
+}