From 11b188d14623d47e0bcdfec36866749996060586 Mon Sep 17 00:00:00 2001 From: Aurora Lahtela <24460436+AuroraLS3@users.noreply.github.com> Date: Sat, 3 Dec 2022 10:49:47 +0200 Subject: [PATCH] Test TabCompleteCache to increase test coverage --- .../plan/commands/TabCompleteCache.java | 44 +++---- .../plan/commands/TabCompleteCacheTest.java | 113 ++++++++++++++++++ 2 files changed, 129 insertions(+), 28 deletions(-) create mode 100644 Plan/common/src/test/java/com/djrapitops/plan/commands/TabCompleteCacheTest.java diff --git a/Plan/common/src/main/java/com/djrapitops/plan/commands/TabCompleteCache.java b/Plan/common/src/main/java/com/djrapitops/plan/commands/TabCompleteCache.java index 8b4fb5979..b2e49236e 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/commands/TabCompleteCache.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/commands/TabCompleteCache.java @@ -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 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 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 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 getMatchingBackupFilenames(String searchFor) { - if (backupFileNames.size() >= 100) { - return Collections.emptyList(); - } - return backupFileNames.stream() + return findMatches(backupFileNames, searchFor); + } + + @NotNull + List findMatches(Collection searchList, String searchFor) { + List 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; } } diff --git a/Plan/common/src/test/java/com/djrapitops/plan/commands/TabCompleteCacheTest.java b/Plan/common/src/test/java/com/djrapitops/plan/commands/TabCompleteCacheTest.java new file mode 100644 index 000000000..f43738fb2 --- /dev/null +++ b/Plan/common/src/test/java/com/djrapitops/plan/commands/TabCompleteCacheTest.java @@ -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 . + */ +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 searchList = RandomData.pickMultiple(101, () -> RandomData.randomString(100)); + List matches = underTest.findMatches(searchList, null); + assertTrue(matches.isEmpty()); + } + + @Test + @DisplayName("Tab completion limit check: {limit} results returns empty") + void tooManyMatchesGetsEmptyTabCompletionAtLimit() { + Collection searchList = RandomData.pickMultiple(100, () -> RandomData.randomString(100)); + List matches = underTest.findMatches(searchList, null); + assertTrue(matches.isEmpty()); + } + + @Test + @DisplayName("Tab completion limit check: {limit} - 1 results returns results") + void tooManyMatchesGetsResultsTabCompletionOneUnder() { + Collection searchList = RandomData.pickMultiple(99, () -> RandomData.randomString(100)); + List matches = underTest.findMatches(searchList, null); + assertEquals(99, matches.size()); + } + + @Test + @DisplayName("Tab completion empty search string returns results") + void emptyStringReturnsAllResults() { + Collection searchList = RandomData.pickMultiple(99, () -> RandomData.randomString(100)); + List matches = underTest.findMatches(searchList, ""); + assertEquals(99, matches.size()); + } + + @Test + @DisplayName("Tab completion matches starts") + void searchGetsAllStarts() { + Collection searchList = new ArrayList<>(); + + for (int i = 0; i < 25; i++) { + searchList.add("start-" + i); + searchList.add("nope-" + i); + } + + List 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 playerNames = new ArrayList<>(); + for (int i = 0; i < 25; i++) { + playerNames.add("start-" + i); + } + when(serverSensor.getOnlinePlayerNames()).thenReturn(playerNames); + + List matches = underTest.getMatchingPlayerIdentifiers("start-"); + assertEquals(25, matches.size()); + } + +} \ No newline at end of file