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;
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");
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);
}
@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();
}
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");
}
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 ")
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");
}
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");
@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");
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;