diff options
author | Matteo Mara <matteo.mara@sonarsource.com> | 2023-12-20 12:34:14 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2024-01-04 20:02:48 +0000 |
commit | ed0ea157e2e474301e4b6c4480f4361742c8f95e (patch) | |
tree | b53785eb6a73ba5ef962311fc66be8671ff2f4ca /sonar-scanner-engine/src/test/java/org/sonar/scanner | |
parent | d89cd75c3bb2a69f4cf71f0cac55cf41d23a223f (diff) | |
download | sonarqube-ed0ea157e2e474301e4b6c4480f4361742c8f95e.tar.gz sonarqube-ed0ea157e2e474301e4b6c4480f4361742c8f95e.zip |
SONAR-21195 Address pull request feedback
Diffstat (limited to 'sonar-scanner-engine/src/test/java/org/sonar/scanner')
-rw-r--r-- | sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/LanguageDetectionTest.java | 34 |
1 files changed, 25 insertions, 9 deletions
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 a071c0869cf..e81120bee32 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; |