mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-02-21 06:42:00 +01:00
Test TabCompleteCache to increase test coverage
This commit is contained in:
parent
2cc6a1eb60
commit
11b188d146
@ -27,6 +27,7 @@ import com.djrapitops.plan.storage.database.queries.objects.ServerQueries;
|
||||
import com.djrapitops.plan.storage.database.queries.objects.UserIdentifierQueries;
|
||||
import com.djrapitops.plan.storage.database.queries.objects.WebUserQueries;
|
||||
import com.djrapitops.plan.storage.file.PlanFiles;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
@ -98,7 +99,8 @@ public class TabCompleteCache implements SubSystem {
|
||||
}
|
||||
|
||||
private void refreshBackupFileNames() {
|
||||
Arrays.stream(files.getDataFolder().list())
|
||||
Optional.ofNullable(files.getDataFolder().list()).stream()
|
||||
.flatMap(Arrays::stream)
|
||||
.filter(Objects::nonNull)
|
||||
.filter(fileName -> fileName.endsWith(".db")
|
||||
&& !fileName.equalsIgnoreCase("database.db"))
|
||||
@ -114,45 +116,31 @@ public class TabCompleteCache implements SubSystem {
|
||||
}
|
||||
|
||||
public List<String> getMatchingServerIdentifiers(String searchFor) {
|
||||
if (serverIdentifiers.size() >= 100) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return serverIdentifiers.stream()
|
||||
.filter(identifier -> searchFor == null || searchFor.isEmpty() || identifier.startsWith(searchFor))
|
||||
.sorted(String.CASE_INSENSITIVE_ORDER)
|
||||
.collect(Collectors.toList());
|
||||
return findMatches(serverIdentifiers, searchFor);
|
||||
}
|
||||
|
||||
public List<String> getMatchingPlayerIdentifiers(String searchFor) {
|
||||
if (playerIdentifiers.size() >= 100) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
playerIdentifiers.addAll(serverSensor.getOnlinePlayerNames());
|
||||
|
||||
return playerIdentifiers.stream()
|
||||
.filter(identifier -> searchFor == null || searchFor.isEmpty() || identifier.startsWith(searchFor))
|
||||
.sorted(String.CASE_INSENSITIVE_ORDER)
|
||||
.collect(Collectors.toList());
|
||||
return findMatches(playerIdentifiers, searchFor);
|
||||
}
|
||||
|
||||
public List<String> getMatchingUserIdentifiers(String searchFor) {
|
||||
if (userIdentifiers.size() >= 100) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return userIdentifiers.stream()
|
||||
.filter(identifier -> searchFor == null || searchFor.isEmpty() || identifier.startsWith(searchFor))
|
||||
.sorted(String.CASE_INSENSITIVE_ORDER)
|
||||
.collect(Collectors.toList());
|
||||
return findMatches(userIdentifiers, searchFor);
|
||||
}
|
||||
|
||||
public List<String> getMatchingBackupFilenames(String searchFor) {
|
||||
if (backupFileNames.size() >= 100) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return backupFileNames.stream()
|
||||
return findMatches(backupFileNames, searchFor);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
List<String> findMatches(Collection<String> searchList, String searchFor) {
|
||||
List<String> filtered = searchList.stream()
|
||||
.filter(identifier -> searchFor == null || searchFor.isEmpty() || identifier.startsWith(searchFor))
|
||||
.sorted(String.CASE_INSENSITIVE_ORDER)
|
||||
.collect(Collectors.toList());
|
||||
if (filtered.size() >= 100) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return filtered;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,113 @@
|
||||
/*
|
||||
* This file is part of Player Analytics (Plan).
|
||||
*
|
||||
* Plan is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License v3 as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Plan 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 Plan. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.djrapitops.plan.commands;
|
||||
|
||||
import com.djrapitops.plan.gathering.ServerSensor;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import utilities.RandomData;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class TabCompleteCacheTest {
|
||||
|
||||
@Mock
|
||||
ServerSensor<?> serverSensor;
|
||||
|
||||
@InjectMocks
|
||||
TabCompleteCache underTest;
|
||||
|
||||
@Test
|
||||
@DisplayName("Tab completion limit check: {limit} + 1 results returns empty")
|
||||
void tooManyMatchesGetsEmptyTabCompletionOneOver() {
|
||||
Collection<String> searchList = RandomData.pickMultiple(101, () -> RandomData.randomString(100));
|
||||
List<String> matches = underTest.findMatches(searchList, null);
|
||||
assertTrue(matches.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Tab completion limit check: {limit} results returns empty")
|
||||
void tooManyMatchesGetsEmptyTabCompletionAtLimit() {
|
||||
Collection<String> searchList = RandomData.pickMultiple(100, () -> RandomData.randomString(100));
|
||||
List<String> matches = underTest.findMatches(searchList, null);
|
||||
assertTrue(matches.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Tab completion limit check: {limit} - 1 results returns results")
|
||||
void tooManyMatchesGetsResultsTabCompletionOneUnder() {
|
||||
Collection<String> searchList = RandomData.pickMultiple(99, () -> RandomData.randomString(100));
|
||||
List<String> matches = underTest.findMatches(searchList, null);
|
||||
assertEquals(99, matches.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Tab completion empty search string returns results")
|
||||
void emptyStringReturnsAllResults() {
|
||||
Collection<String> searchList = RandomData.pickMultiple(99, () -> RandomData.randomString(100));
|
||||
List<String> matches = underTest.findMatches(searchList, "");
|
||||
assertEquals(99, matches.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Tab completion matches starts")
|
||||
void searchGetsAllStarts() {
|
||||
Collection<String> searchList = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < 25; i++) {
|
||||
searchList.add("start-" + i);
|
||||
searchList.add("nope-" + i);
|
||||
}
|
||||
|
||||
List<String> matches = underTest.findMatches(searchList, "start-");
|
||||
assertEquals(25, matches.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Tab completion coverage")
|
||||
void tabCompletionCommonMethodCoverage() {
|
||||
assertTrue(underTest.getMatchingServerIdentifiers("").isEmpty());
|
||||
assertTrue(underTest.getMatchingPlayerIdentifiers("").isEmpty());
|
||||
assertTrue(underTest.getMatchingUserIdentifiers("").isEmpty());
|
||||
assertTrue(underTest.getMatchingBackupFilenames("").isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Tab completion online players are searched")
|
||||
void tabCompletionOnlinePlayersAreListed() {
|
||||
List<String> playerNames = new ArrayList<>();
|
||||
for (int i = 0; i < 25; i++) {
|
||||
playerNames.add("start-" + i);
|
||||
}
|
||||
when(serverSensor.getOnlinePlayerNames()).thenReturn(playerNames);
|
||||
|
||||
List<String> matches = underTest.getMatchingPlayerIdentifiers("start-");
|
||||
assertEquals(25, matches.size());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user