From 5c6163d8cd1f2e5c6143fd34d87be4798b1659ac Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Thu, 13 Feb 2020 19:20:53 -0500 Subject: [PATCH] Use more obvious/broken-down test cases for topological sort tests --- .../util/PluginDependencyUtilsTest.java | 95 +++++++++++-------- 1 file changed, 53 insertions(+), 42 deletions(-) diff --git a/proxy/src/test/java/com/velocitypowered/proxy/plugin/util/PluginDependencyUtilsTest.java b/proxy/src/test/java/com/velocitypowered/proxy/plugin/util/PluginDependencyUtilsTest.java index 99033c689..5709b2864 100644 --- a/proxy/src/test/java/com/velocitypowered/proxy/plugin/util/PluginDependencyUtilsTest.java +++ b/proxy/src/test/java/com/velocitypowered/proxy/plugin/util/PluginDependencyUtilsTest.java @@ -14,49 +14,64 @@ import org.junit.jupiter.api.Test; class PluginDependencyUtilsTest { - private static final PluginDescription NO_DEPENDENCY_1_EXAMPLE = testDescription("example"); - private static final PluginDescription NEVER_DEPENDED = testDescription("and-again"); - private static final PluginDescription SOFT_DEPENDENCY_EXISTS = testDescription("soft", - ImmutableList.of(new PluginDependency("example", "", true))); - private static final PluginDescription SOFT_DEPENDENCY_DOES_NOT_EXIST = testDescription("fluffy", - ImmutableList.of(new PluginDependency("i-dont-exist", "", false))); - private static final PluginDescription MULTI_DEPENDENCY = testDescription("multi-depend", - ImmutableList.of( - new PluginDependency("example", "", false) - ) - ); - private static final PluginDescription TEST_WITH_DUPLICATE_DEPEND = testDescription("dup-depend", - ImmutableList.of( - new PluginDependency("multi-depend", "", false) - ) - ); + private static final PluginDescription NO_DEPENDENCY = testDescription("trivial"); + private static final PluginDescription NO_DEPENDENCY_2 = testDescription("trivial2"); + private static final PluginDescription HAS_DEPENDENCY_1 = testDescription("dependent1", + new PluginDependency("trivial", null, false)); + private static final PluginDescription HAS_DEPENDENCY_2 = testDescription("dependent2", + new PluginDependency("dependent1", null, false)); + private static final PluginDescription HAS_DEPENDENCY_3 = testDescription("dependent3", + new PluginDependency("trivial", null, false)); private static final PluginDescription CIRCULAR_DEPENDENCY_1 = testDescription("circle", - ImmutableList.of(new PluginDependency("oval", "", false))); + new PluginDependency("oval", "", false)); private static final PluginDescription CIRCULAR_DEPENDENCY_2 = testDescription("oval", - ImmutableList.of(new PluginDependency("circle", "", false))); - - private static final ImmutableList EXPECTED = ImmutableList.of( - NEVER_DEPENDED, - NO_DEPENDENCY_1_EXAMPLE, - MULTI_DEPENDENCY, - TEST_WITH_DUPLICATE_DEPEND, - SOFT_DEPENDENCY_DOES_NOT_EXIST, - SOFT_DEPENDENCY_EXISTS - ); + new PluginDependency("circle", "", false)); @Test - void sortCandidates() throws Exception { + void sortCandidatesTrivial() throws Exception { List descriptionList = new ArrayList<>(); - descriptionList.add(NO_DEPENDENCY_1_EXAMPLE); - descriptionList.add(NEVER_DEPENDED); - descriptionList.add(SOFT_DEPENDENCY_DOES_NOT_EXIST); - descriptionList.add(SOFT_DEPENDENCY_EXISTS); - descriptionList.add(MULTI_DEPENDENCY); - descriptionList.add(TEST_WITH_DUPLICATE_DEPEND); - descriptionList.sort(Comparator.comparing(PluginDescription::getId)); + assertEquals(descriptionList, PluginDependencyUtils.sortCandidates(descriptionList)); + } - assertEquals(EXPECTED, PluginDependencyUtils.sortCandidates(descriptionList)); + @Test + void sortCandidatesSingleton() throws Exception { + List plugins = ImmutableList.of(NO_DEPENDENCY); + assertEquals(plugins, PluginDependencyUtils.sortCandidates(plugins)); + } + + @Test + void sortCandidatesBasicDependency() throws Exception { + List plugins = ImmutableList.of(HAS_DEPENDENCY_1, NO_DEPENDENCY); + List expected = ImmutableList.of(NO_DEPENDENCY, HAS_DEPENDENCY_1); + assertEquals(expected, PluginDependencyUtils.sortCandidates(plugins)); + } + + @Test + void sortCandidatesNestedDependency() throws Exception { + List plugins = ImmutableList.of(HAS_DEPENDENCY_1, HAS_DEPENDENCY_2, + NO_DEPENDENCY); + List expected = ImmutableList.of(NO_DEPENDENCY, HAS_DEPENDENCY_1, + HAS_DEPENDENCY_2); + assertEquals(expected, PluginDependencyUtils.sortCandidates(plugins)); + } + + @Test + void sortCandidatesTypical() throws Exception { + List plugins = ImmutableList.of(HAS_DEPENDENCY_2, NO_DEPENDENCY_2, + HAS_DEPENDENCY_1, NO_DEPENDENCY); + List expected = ImmutableList.of(NO_DEPENDENCY, HAS_DEPENDENCY_1, + HAS_DEPENDENCY_2, NO_DEPENDENCY_2); + assertEquals(expected, PluginDependencyUtils.sortCandidates(plugins)); + } + + @Test + void sortCandidatesMultiplePluginsDependentOnOne() throws Exception { + List plugins = ImmutableList.of(HAS_DEPENDENCY_3, HAS_DEPENDENCY_1, + NO_DEPENDENCY); + List expected = ImmutableList.of(NO_DEPENDENCY, HAS_DEPENDENCY_3, + HAS_DEPENDENCY_1); + assertEquals(expected, PluginDependencyUtils.sortCandidates(plugins)); } @Test @@ -65,14 +80,10 @@ class PluginDependencyUtilsTest { assertThrows(IllegalStateException.class, () -> PluginDependencyUtils.sortCandidates(descs)); } - private static PluginDescription testDescription(String id) { - return testDescription(id, ImmutableList.of()); - } - - private static PluginDescription testDescription(String id, List dependencies) { + private static PluginDescription testDescription(String id, PluginDependency... dependencies) { return new VelocityPluginDescription( id, "tuxed", "0.1", null, null, ImmutableList.of(), - dependencies, null + ImmutableList.copyOf(dependencies), null ); } }