diff options
author | Matteo Mara <matteo.mara@sonarsource.com> | 2023-12-22 10:28:17 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-12-22 20:03:00 +0000 |
commit | 007ef6cf95685696b373ca2a13060278ce2b4263 (patch) | |
tree | 447844a2b6c3b4eeaf5b5873c212f26a3a33700b /sonar-scanner-engine/src/test/java/org/sonar/scanner | |
parent | c93c5127dc667a49642bbdaab9d4a766b9bf32e4 (diff) | |
download | sonarqube-007ef6cf95685696b373ca2a13060278ce2b4263.tar.gz sonarqube-007ef6cf95685696b373ca2a13060278ce2b4263.zip |
Revert "SONAR-21195 Address pull request feedback"
This reverts commit f6f1db0ef46a1ce50790a104d429aaaa42889c91.
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, 9 insertions, 25 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 9aef7e49abf..2155106ac04 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,7 +23,6 @@ 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; @@ -80,7 +79,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); + LanguageDetection detection = new LanguageDetection(settings.asConfig(), languages, new HashMap<>()); assertThat(detectLanguageKey(detection, "Foo.java")).isEqualTo("java"); assertThat(detectLanguageKey(detection, "src/Foo.java")).isEqualTo("java"); @@ -103,7 +102,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); + LanguageDetection detection = new LanguageDetection(settings.asConfig(), languages, new HashMap<>()); assertThat(detectLanguageKey(detection, fileName)).isEqualTo(expectedLanguageKey); } @@ -126,7 +125,7 @@ public class LanguageDetectionTest { @Test public void detectLanguageKey_shouldNotFailIfNoLanguage() { - LanguageDetection detection = spy(new LanguageDetection(settings.asConfig(), new DefaultLanguagesRepository(new FakeLanguagesLoader(new Languages())))); + LanguageDetection detection = spy(new LanguageDetection(settings.asConfig(), new DefaultLanguagesRepository(new FakeLanguagesLoader(new Languages())), new HashMap<>())); assertThat(detectLanguageKey(detection, "Foo.java")).isNull(); } @@ -135,7 +134,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); + LanguageDetection detection = new LanguageDetection(settings.asConfig(), languages, new HashMap<>()); assertThat(detectLanguageKey(detection, "abc.abap")).isEqualTo("abap"); } @@ -143,7 +142,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); + LanguageDetection detection = new LanguageDetection(settings.asConfig(), languages, new HashMap<>()); 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 ") @@ -158,7 +157,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); + LanguageDetection detection = new LanguageDetection(settings.asConfig(), languages, new HashMap<>()); assertThat(detectLanguageKey(detection, "xml/abc.xhtml")).isEqualTo("xml"); assertThat(detectLanguageKey(detection, "web/abc.xhtml")).isEqualTo("web"); } @@ -170,7 +169,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); + LanguageDetection detection = new LanguageDetection(settings.asConfig(), languages, new HashMap<>()); assertThat(detectLanguageKey(detection, "abc.abap")).isEqualTo("abap"); assertThat(detectLanguageKey(detection, "abc.cobol")).isEqualTo("cobol"); @@ -183,13 +182,11 @@ 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); - - var languageCacheSpy = spyInternalLanguageCache(detection); + LanguageDetection detection = new LanguageDetection(settings.asConfig(), languages, languageCacheSpy); assertThat(detectLanguageKey(detection, "Foo.java")).isEqualTo("java"); assertThat(detectLanguageKey(detection, "Foo.java")).isEqualTo("java"); @@ -197,19 +194,6 @@ 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; |