]> source.dussan.org Git - sonarqube.git/blob
490b7cfd519644901c1b3fc935c1b54a5cdbe3ed
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2021 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 3 of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19  */
20 package org.sonar.server.component.index;
21
22 import com.google.common.collect.ImmutableSet;
23 import org.junit.Test;
24 import org.sonar.db.component.ComponentDto;
25 import org.sonar.db.component.ComponentTesting;
26 import org.sonar.server.es.textsearch.ComponentTextSearchFeatureRepertoire;
27
28 import static java.util.Arrays.asList;
29 import static org.sonar.api.resources.Qualifiers.FILE;
30 import static org.sonar.api.resources.Qualifiers.MODULE;
31 import static org.sonar.api.resources.Qualifiers.PROJECT;
32
33 public class ComponentIndexScoreTest extends ComponentIndexTest {
34
35   @Test
36   public void should_prefer_components_without_prefix() {
37     assertResultOrder("File.java",
38       "File.java",
39       "MyFile.java");
40   }
41
42   @Test
43   public void should_prefer_components_without_suffix() {
44     assertResultOrder("File",
45       "File",
46       "Filex");
47   }
48
49   @Test
50   public void should_prefer_key_matching_over_name_matching() {
51     ComponentDto project1 = indexProject("quality", "SonarQube");
52     ComponentDto project2 = indexProject("sonarqube", "Quality Product");
53
54     assertExactResults("sonarqube", project2, project1);
55   }
56
57   @Test
58   public void should_prefer_prefix_matching_over_partial_matching() {
59     assertResultOrder("corem",
60       "CoreMetrics.java",
61       "ScoreMatrix.java");
62   }
63
64   @Test
65   public void should_prefer_case_sensitive_prefix() {
66     assertResultOrder("caSe",
67       "caSeBla.java",
68       "CaseBla.java");
69   }
70
71   @Test
72   public void scoring_prefix_with_multiple_words() {
73     assertResultOrder("index java",
74       "IndexSomething.java",
75       "MyIndex.java");
76   }
77
78   @Test
79   public void scoring_prefix_with_multiple_words_and_case() {
80     assertResultOrder("Index JAVA",
81       "IndexSomething.java",
82       "index_java.js");
83   }
84
85   @Test
86   public void scoring_long_items() {
87     assertResultOrder("ThisIsAVeryLongNameToSearchForAndItExceeds15Characters.java",
88       "ThisIsAVeryLongNameToSearchForAndItExceeds15Characters.java",
89       "ThisIsAVeryLongNameToSearchForAndItEndsDifferently.java");
90   }
91
92   @Test
93   public void scoring_perfect_match() {
94     assertResultOrder("SonarQube",
95       "SonarQube",
96       "SonarQube SCM Git");
97   }
98
99   @Test
100   public void scoring_perfect_match_dispite_case_changes() {
101     assertResultOrder("sonarqube",
102       "SonarQube",
103       "SonarQube SCM Git");
104   }
105
106   @Test
107   public void scoring_perfect_match_with_matching_case_higher_than_without_matching_case() {
108     assertResultOrder("sonarqube",
109       "sonarqube",
110       "SonarQube");
111   }
112
113   @Test
114   public void should_prefer_favorite_over_recently_browsed() {
115     ComponentDto file1 = db.components().insertPrivateProject(c -> c.setName("File1"));
116     index(file1);
117
118     ComponentDto file2 = db.components().insertPrivateProject(c -> c.setName("File2"));
119     index(file2);
120
121     assertSearch(SuggestionQuery.builder()
122       .setQuery("File")
123       .setQualifiers(asList(PROJECT, MODULE, FILE))
124       .setRecentlyBrowsedKeys(ImmutableSet.of(file1.getDbKey()))
125       .setFavoriteKeys(ImmutableSet.of(file2.getDbKey()))
126       .build()).containsExactly(uuids(file2, file1));
127
128     assertSearch(SuggestionQuery.builder()
129       .setQuery("File")
130       .setQualifiers(asList(PROJECT, MODULE, FILE))
131       .setRecentlyBrowsedKeys(ImmutableSet.of(file2.getDbKey()))
132       .setFavoriteKeys(ImmutableSet.of(file1.getDbKey()))
133       .build()).containsExactly(uuids(file1, file2));
134   }
135
136   @Test
137   public void do_not_match_wrong_file_extension() {
138     ComponentDto file1 = indexFile("MyClass.java");
139     ComponentDto file2 = indexFile("ClassExample.java");
140     ComponentDto file3 = indexFile("Class.java");
141     indexFile("Class.cs");
142     indexFile("Class.js");
143     indexFile("Class.rb");
144
145     assertExactResults("Class java", file3, file2, file1);
146   }
147
148   @Test
149   public void if_relevancy_is_equal_fall_back_to_alphabetical_ordering() {
150     assertResultOrder("sonarqube",
151       "sonarqubeA",
152       "sonarqubeB");
153   }
154
155   @Test
156   public void scoring_test_DbTester() {
157     features.set(ComponentTextSearchFeatureRepertoire.PARTIAL);
158
159     ComponentDto project = indexProject("key-1", "Quality Product");
160
161     index(ComponentTesting.newFileDto(project)
162       .setName("DbTester.java")
163       .setDbKey("java/org/example/DbTester.java")
164       .setUuid("UUID-DbTester"));
165
166     index(ComponentTesting.newFileDto(project)
167       .setName("WebhookDbTesting.java")
168       .setDbKey("java/org/example/WebhookDbTesting.java")
169       .setUuid("UUID-WebhookDbTesting"));
170
171     assertSearch("dbt").containsExactly(
172
173       "UUID-DbTester",
174       "UUID-WebhookDbTesting"
175
176     );
177   }
178 }