aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/LanguageDetection.java5
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/LanguageDetectionTest.java34
2 files changed, 28 insertions, 11 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/LanguageDetection.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/LanguageDetection.java
index 24128133824..5a52107a8bf 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/LanguageDetection.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/LanguageDetection.java
@@ -22,6 +22,7 @@ package org.sonar.scanner.scan.filesystem;
import java.nio.file.Path;
import java.text.MessageFormat;
import java.util.Arrays;
+import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -56,7 +57,7 @@ public class LanguageDetection {
private final List<Language> languagesToConsider;
private final Map<String, Language> languageCacheByPath;
- public LanguageDetection(Configuration settings, LanguagesRepository languages, Map<String, Language> languageCache) {
+ public LanguageDetection(Configuration settings, LanguagesRepository languages) {
Map<Language, PathPattern[]> patternsByLanguageBuilder = new LinkedHashMap<>();
for (Language language : languages.all()) {
String[] filePatterns = settings.getStringArray(getFileLangPatternPropKey(language.key()));
@@ -71,7 +72,7 @@ public class LanguageDetection {
languagesToConsider = List.copyOf(patternsByLanguageBuilder.keySet());
patternsByLanguage = unmodifiableMap(patternsByLanguageBuilder);
- languageCacheByPath = languageCache;
+ languageCacheByPath = new HashMap<>();
}
private static PathPattern[] getLanguagePatterns(Language language) {
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/LanguageDetectionTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/LanguageDetectionTest.java
index 2155106ac04..9aef7e49abf 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/LanguageDetectionTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/LanguageDetectionTest.java
@@ -23,6 +23,7 @@ import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.java.junit.dataprovider.UseDataProvider;
import java.io.File;
+import java.lang.reflect.Field;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
@@ -79,7 +80,7 @@ public class LanguageDetectionTest {
public void detectLanguageKey_shouldDetectByFileExtension() {
DefaultLanguagesRepository languages = new DefaultLanguagesRepository(new FakeLanguagesLoader(new Languages(new MockLanguage("java", "java", "jav"), new MockLanguage("cobol", "cbl", "cob"))));
languages.start();
- LanguageDetection detection = new LanguageDetection(settings.asConfig(), languages, new HashMap<>());
+ LanguageDetection detection = new LanguageDetection(settings.asConfig(), languages);
assertThat(detectLanguageKey(detection, "Foo.java")).isEqualTo("java");
assertThat(detectLanguageKey(detection, "src/Foo.java")).isEqualTo("java");
@@ -102,7 +103,7 @@ public class LanguageDetectionTest {
new MockLanguage("terraform", new String[] {"tf"}, new String[] {".tf"}),
new MockLanguage("java", new String[0], new String[] {"**/*Test.java"}))));
languages.start();
- LanguageDetection detection = new LanguageDetection(settings.asConfig(), languages, new HashMap<>());
+ LanguageDetection detection = new LanguageDetection(settings.asConfig(), languages);
assertThat(detectLanguageKey(detection, fileName)).isEqualTo(expectedLanguageKey);
}
@@ -125,7 +126,7 @@ public class LanguageDetectionTest {
@Test
public void detectLanguageKey_shouldNotFailIfNoLanguage() {
- LanguageDetection detection = spy(new LanguageDetection(settings.asConfig(), new DefaultLanguagesRepository(new FakeLanguagesLoader(new Languages())), new HashMap<>()));
+ LanguageDetection detection = spy(new LanguageDetection(settings.asConfig(), new DefaultLanguagesRepository(new FakeLanguagesLoader(new Languages()))));
assertThat(detectLanguageKey(detection, "Foo.java")).isNull();
}
@@ -134,7 +135,7 @@ public class LanguageDetectionTest {
DefaultLanguagesRepository languages = new DefaultLanguagesRepository(new FakeLanguagesLoader(new Languages(new MockLanguage("abap", "abap", "ABAP"))));
languages.start();
- LanguageDetection detection = new LanguageDetection(settings.asConfig(), languages, new HashMap<>());
+ LanguageDetection detection = new LanguageDetection(settings.asConfig(), languages);
assertThat(detectLanguageKey(detection, "abc.abap")).isEqualTo("abap");
}
@@ -142,7 +143,7 @@ public class LanguageDetectionTest {
public void detectLanguageKey_shouldFailIfConflictingLanguageSuffix() {
DefaultLanguagesRepository languages = new DefaultLanguagesRepository(new FakeLanguagesLoader(new Languages(new MockLanguage("xml", "xhtml"), new MockLanguage("web", "xhtml"))));
languages.start();
- LanguageDetection detection = new LanguageDetection(settings.asConfig(), languages, new HashMap<>());
+ LanguageDetection detection = new LanguageDetection(settings.asConfig(), languages);
assertThatThrownBy(() -> detectLanguageKey(detection, "abc.xhtml"))
.isInstanceOf(MessageException.class)
.hasMessageContaining("Language of file 'abc.xhtml' can not be decided as the file matches patterns of both ")
@@ -157,7 +158,7 @@ public class LanguageDetectionTest {
settings.setProperty("sonar.lang.patterns.xml", "xml/**");
settings.setProperty("sonar.lang.patterns.web", "web/**");
- LanguageDetection detection = new LanguageDetection(settings.asConfig(), languages, new HashMap<>());
+ LanguageDetection detection = new LanguageDetection(settings.asConfig(), languages);
assertThat(detectLanguageKey(detection, "xml/abc.xhtml")).isEqualTo("xml");
assertThat(detectLanguageKey(detection, "web/abc.xhtml")).isEqualTo("web");
}
@@ -169,7 +170,7 @@ public class LanguageDetectionTest {
settings.setProperty("sonar.lang.patterns.abap", "*.abap,*.txt");
settings.setProperty("sonar.lang.patterns.cobol", "*.cobol,*.txt");
- LanguageDetection detection = new LanguageDetection(settings.asConfig(), languages, new HashMap<>());
+ LanguageDetection detection = new LanguageDetection(settings.asConfig(), languages);
assertThat(detectLanguageKey(detection, "abc.abap")).isEqualTo("abap");
assertThat(detectLanguageKey(detection, "abc.cobol")).isEqualTo("cobol");
@@ -182,11 +183,13 @@ public class LanguageDetectionTest {
@Test
public void should_cache_detected_language_by_file_path() {
- Map<String, org.sonar.scanner.repository.language.Language> languageCacheSpy = spy(new HashMap<>());
DefaultLanguagesRepository languages = new DefaultLanguagesRepository(new FakeLanguagesLoader(new Languages(
new MockLanguage("java", "java", "jav"), new MockLanguage("cobol", "cbl", "cob"))));
languages.start();
- LanguageDetection detection = new LanguageDetection(settings.asConfig(), languages, languageCacheSpy);
+
+ LanguageDetection detection = new LanguageDetection(settings.asConfig(), languages);
+
+ var languageCacheSpy = spyInternalLanguageCache(detection);
assertThat(detectLanguageKey(detection, "Foo.java")).isEqualTo("java");
assertThat(detectLanguageKey(detection, "Foo.java")).isEqualTo("java");
@@ -194,6 +197,19 @@ public class LanguageDetectionTest {
verify(languageCacheSpy, times(2)).get(endsWith("/Foo.java"));
}
+ private static Map<String, org.sonar.scanner.repository.language.Language> spyInternalLanguageCache(LanguageDetection detection) {
+ Map<String, org.sonar.scanner.repository.language.Language> languageCacheSpy = spy(new HashMap<>());
+ Field field;
+ try {
+ field = LanguageDetection.class.getDeclaredField("languageCacheByPath");
+ field.setAccessible(true);
+ field.set(detection, languageCacheSpy);
+ } catch (NoSuchFieldException | IllegalAccessException e) {
+ throw new RuntimeException(e);
+ }
+ return languageCacheSpy;
+ }
+
private String detectLanguageKey(LanguageDetection detection, String path) {
org.sonar.scanner.repository.language.Language language = detection.language(new File(temp.getRoot(), path).toPath(), Paths.get(path));
return language != null ? language.key() : null;