]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-21195 Address pull request feedback
authorMatteo Mara <matteo.mara@sonarsource.com>
Wed, 20 Dec 2023 11:34:14 +0000 (12:34 +0100)
committersonartech <sonartech@sonarsource.com>
Thu, 4 Jan 2024 20:02:48 +0000 (20:02 +0000)
sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/LanguageDetection.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/LanguageDetectionTest.java

index 3beaea8c2555bb2e339d041faef0181bb2d66ad2..dba9523c6bffba4bce69a1500a2983957746bca3 100644 (file)
@@ -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) {
index a071c0869cf8d3381d26214cd2a5e42ec3442f12..e81120bee320ed309992ce37a9ca820dc37aab7c 100644 (file)
@@ -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;