Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SONAR-9077 calculate matching characters for component
- Loading branch information
1 parent
3c452a0
commit d636a8b
Showing
6 changed files
with
143 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...ar-server/src/test/java/org/sonar/server/component/index/ComponentIndexHighlightTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,56 @@ | |||
/* | |||
* SonarQube | |||
* Copyright (C) 2009-2016 SonarSource SA | |||
* mailto:contact AT sonarsource DOT com | |||
* | |||
* This program is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public License | |||
* along with this program; if not, write to the Free Software Foundation, | |||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
*/ | |||
package org.sonar.server.component.index; | |||
|
|||
import java.util.Collections; | |||
import java.util.List; | |||
import java.util.Optional; | |||
import org.junit.Test; | |||
import org.sonar.api.resources.Qualifiers; | |||
|
|||
import static org.assertj.core.api.Assertions.assertThat; | |||
|
|||
public class ComponentIndexHighlightTest extends ComponentIndexTest { | |||
|
|||
@Test | |||
public void should_highlight_prefix() { | |||
assertHighlighting("quick brown fox", "brown", "quick <mark>brown</mark> fox"); | |||
} | |||
|
|||
@Test | |||
public void should_escape_html() { | |||
assertHighlighting("quick< brown fox", "brown", "quick< <mark>brown</mark> fox"); | |||
} | |||
|
|||
private void assertHighlighting(String fileName, String search, String expectedHighlighting) { | |||
indexFile(fileName); | |||
|
|||
ComponentIndexQuery query = ComponentIndexQuery.builder() | |||
.setQuery(search) | |||
.setQualifiers(Collections.singletonList(Qualifiers.FILE)) | |||
.build(); | |||
List<ComponentHitsPerQualifier> results = index.search(query, features.get()); | |||
|
|||
assertThat(results).flatExtracting(ComponentHitsPerQualifier::getHits) | |||
.extracting(ComponentHit::getHighlightedText) | |||
.extracting(Optional::get) | |||
.containsExactly(expectedHighlighting); | |||
} | |||
} |