-
Notifications
You must be signed in to change notification settings - Fork 1
[Tests] Add unit tests for HooksRegistrar and improve Sfun, ManagerRegistry coverage #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
245 changes: 245 additions & 0 deletions
245
src/test/java/com/diamonddagger590/mccore/bootstrap/registrar/HooksRegistrarTest.java
This file contains hidden or 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 | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,245 @@ | ||
| package com.diamonddagger590.mccore.bootstrap.registrar; | ||
|
|
||
| import com.diamonddagger590.mccore.CorePlugin; | ||
| import com.diamonddagger590.mccore.bootstrap.BootstrapContext; | ||
| import com.diamonddagger590.mccore.bootstrap.StartupProfile; | ||
| import com.diamonddagger590.mccore.external.citizens.CoreCitizensHook; | ||
| import com.diamonddagger590.mccore.external.cmi.CoreCMIHook; | ||
| import com.diamonddagger590.mccore.external.itemsadder.CoreItemsAdderHook; | ||
| import com.diamonddagger590.mccore.external.modelengine.CoreModelEngineHook; | ||
| import com.diamonddagger590.mccore.external.mythicmobs.CoreMythicMobsHook; | ||
| import com.diamonddagger590.mccore.external.nexo.CoreNexoHook; | ||
| import com.diamonddagger590.mccore.external.papi.CorePapiHook; | ||
| import com.diamonddagger590.mccore.registry.RegistryAccess; | ||
| import com.diamonddagger590.mccore.registry.RegistryKey; | ||
| import com.diamonddagger590.mccore.registry.plugin.CorePluginHookKey; | ||
| import com.diamonddagger590.mccore.registry.plugin.PluginHookRegistry; | ||
| import com.diamonddagger590.mccore.testing.RegistryResetExtension; | ||
| import org.bukkit.Bukkit; | ||
| import org.bukkit.plugin.PluginManager; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.mockito.Mock; | ||
| import org.mockito.MockedStatic; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
|
|
||
| import java.util.logging.Logger; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| import static org.mockito.ArgumentMatchers.anyString; | ||
| import static org.mockito.Mockito.mockStatic; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| @ExtendWith(MockitoExtension.class) | ||
| class HooksRegistrarTest { | ||
|
|
||
| @Mock | ||
| private CorePlugin mockPlugin; | ||
|
|
||
| @Mock | ||
| private PluginManager mockPluginManager; | ||
|
|
||
| private PluginHookRegistry hookRegistry; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| RegistryResetExtension.setupRegistry(); | ||
| hookRegistry = RegistryAccess.registryAccess().registry(RegistryKey.PLUGIN_HOOK); | ||
| when(mockPlugin.registryAccess()).thenReturn(RegistryAccess.registryAccess()); | ||
| when(mockPlugin.getLogger()).thenReturn(Logger.getLogger("HooksRegistrarTest")); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void tearDown() { | ||
| RegistryResetExtension.resetRegistry(); | ||
| } | ||
|
|
||
| @NotNull | ||
| private BootstrapContext<CorePlugin> context() { | ||
| return new BootstrapContext<>(mockPlugin, StartupProfile.PROD); | ||
| } | ||
|
|
||
| private void enablePlugin(@NotNull String name) { | ||
| when(mockPluginManager.isPluginEnabled(name)).thenReturn(true); | ||
| } | ||
|
|
||
| private void enableAllPlugins() { | ||
| enablePlugin("Nexo"); | ||
| enablePlugin("ItemsAdder"); | ||
| enablePlugin("PlaceholderAPI"); | ||
| enablePlugin("ModelEngine"); | ||
| enablePlugin("MythicMobs"); | ||
| enablePlugin("CMI"); | ||
| enablePlugin("Citizens"); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Given no plugins enabled, when registering hooks, then no hooks are registered") | ||
| void register_registersNoHooks_whenNoPluginsEnabled() { | ||
| try (MockedStatic<Bukkit> bukkit = mockStatic(Bukkit.class)) { | ||
| bukkit.when(Bukkit::getPluginManager).thenReturn(mockPluginManager); | ||
| when(mockPluginManager.isPluginEnabled(anyString())).thenReturn(false); | ||
|
|
||
| new HooksRegistrar<CorePlugin>().register(context()); | ||
|
|
||
| assertFalse(hookRegistry.pluginHook(CorePluginHookKey.CORE_NEXO).isPresent()); | ||
| assertFalse(hookRegistry.pluginHook(CorePluginHookKey.CORE_ITEMS_ADDER).isPresent()); | ||
| assertFalse(hookRegistry.pluginHook(CorePluginHookKey.CORE_PAPI).isPresent()); | ||
| assertFalse(hookRegistry.pluginHook(CorePluginHookKey.CORE_MODEL_ENGINE).isPresent()); | ||
| assertFalse(hookRegistry.pluginHook(CorePluginHookKey.CORE_MYTHIC_MOBS).isPresent()); | ||
| assertFalse(hookRegistry.pluginHook(CorePluginHookKey.CORE_CMI).isPresent()); | ||
| assertFalse(hookRegistry.pluginHook(CorePluginHookKey.CORE_CITIZENS).isPresent()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Given Nexo enabled, when registering hooks, then Nexo hook is registered") | ||
| void register_registersNexoHook_whenNexoEnabled() { | ||
| try (MockedStatic<Bukkit> bukkit = mockStatic(Bukkit.class)) { | ||
| bukkit.when(Bukkit::getPluginManager).thenReturn(mockPluginManager); | ||
| when(mockPluginManager.isPluginEnabled(anyString())).thenReturn(false); | ||
| enablePlugin("Nexo"); | ||
|
|
||
| new HooksRegistrar<CorePlugin>().register(context()); | ||
|
|
||
| assertTrue(hookRegistry.pluginHook(CorePluginHookKey.CORE_NEXO).isPresent()); | ||
| assertTrue(hookRegistry.pluginHook(CorePluginHookKey.CORE_NEXO).get() instanceof CoreNexoHook); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Given ItemsAdder enabled, when registering hooks, then ItemsAdder hook is registered") | ||
| void register_registersItemsAdderHook_whenItemsAdderEnabled() { | ||
| try (MockedStatic<Bukkit> bukkit = mockStatic(Bukkit.class)) { | ||
| bukkit.when(Bukkit::getPluginManager).thenReturn(mockPluginManager); | ||
| when(mockPluginManager.isPluginEnabled(anyString())).thenReturn(false); | ||
| enablePlugin("ItemsAdder"); | ||
|
|
||
| new HooksRegistrar<CorePlugin>().register(context()); | ||
|
|
||
| assertTrue(hookRegistry.pluginHook(CorePluginHookKey.CORE_ITEMS_ADDER).isPresent()); | ||
| assertTrue(hookRegistry.pluginHook(CorePluginHookKey.CORE_ITEMS_ADDER).get() instanceof CoreItemsAdderHook); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Given PlaceholderAPI enabled, when registering hooks, then PAPI hook is registered") | ||
| void register_registersPapiHook_whenPlaceholderApiEnabled() { | ||
| try (MockedStatic<Bukkit> bukkit = mockStatic(Bukkit.class)) { | ||
| bukkit.when(Bukkit::getPluginManager).thenReturn(mockPluginManager); | ||
| when(mockPluginManager.isPluginEnabled(anyString())).thenReturn(false); | ||
| enablePlugin("PlaceholderAPI"); | ||
|
|
||
| new HooksRegistrar<CorePlugin>().register(context()); | ||
|
|
||
| assertTrue(hookRegistry.pluginHook(CorePluginHookKey.CORE_PAPI).isPresent()); | ||
| assertTrue(hookRegistry.pluginHook(CorePluginHookKey.CORE_PAPI).get() instanceof CorePapiHook); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Given ModelEngine enabled, when registering hooks, then ModelEngine hook is registered") | ||
| void register_registersModelEngineHook_whenModelEngineEnabled() { | ||
| try (MockedStatic<Bukkit> bukkit = mockStatic(Bukkit.class)) { | ||
| bukkit.when(Bukkit::getPluginManager).thenReturn(mockPluginManager); | ||
| when(mockPluginManager.isPluginEnabled(anyString())).thenReturn(false); | ||
| enablePlugin("ModelEngine"); | ||
|
|
||
| new HooksRegistrar<CorePlugin>().register(context()); | ||
|
|
||
| assertTrue(hookRegistry.pluginHook(CorePluginHookKey.CORE_MODEL_ENGINE).isPresent()); | ||
| assertTrue(hookRegistry.pluginHook(CorePluginHookKey.CORE_MODEL_ENGINE).get() instanceof CoreModelEngineHook); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Given MythicMobs enabled, when registering hooks, then MythicMobs hook is registered") | ||
| void register_registersMythicMobsHook_whenMythicMobsEnabled() { | ||
| try (MockedStatic<Bukkit> bukkit = mockStatic(Bukkit.class)) { | ||
| bukkit.when(Bukkit::getPluginManager).thenReturn(mockPluginManager); | ||
| when(mockPluginManager.isPluginEnabled(anyString())).thenReturn(false); | ||
| enablePlugin("MythicMobs"); | ||
|
|
||
| new HooksRegistrar<CorePlugin>().register(context()); | ||
|
|
||
| assertTrue(hookRegistry.pluginHook(CorePluginHookKey.CORE_MYTHIC_MOBS).isPresent()); | ||
| assertTrue(hookRegistry.pluginHook(CorePluginHookKey.CORE_MYTHIC_MOBS).get() instanceof CoreMythicMobsHook); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Given CMI enabled, when registering hooks, then CMI hook is registered") | ||
| void register_registersCmiHook_whenCmiEnabled() { | ||
| try (MockedStatic<Bukkit> bukkit = mockStatic(Bukkit.class)) { | ||
| bukkit.when(Bukkit::getPluginManager).thenReturn(mockPluginManager); | ||
| when(mockPluginManager.isPluginEnabled(anyString())).thenReturn(false); | ||
| enablePlugin("CMI"); | ||
|
|
||
| new HooksRegistrar<CorePlugin>().register(context()); | ||
|
|
||
| assertTrue(hookRegistry.pluginHook(CorePluginHookKey.CORE_CMI).isPresent()); | ||
| assertTrue(hookRegistry.pluginHook(CorePluginHookKey.CORE_CMI).get() instanceof CoreCMIHook); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Given Citizens enabled, when registering hooks, then Citizens hook is registered") | ||
| void register_registersCitizensHook_whenCitizensEnabled() { | ||
| try (MockedStatic<Bukkit> bukkit = mockStatic(Bukkit.class)) { | ||
| bukkit.when(Bukkit::getPluginManager).thenReturn(mockPluginManager); | ||
| when(mockPluginManager.isPluginEnabled(anyString())).thenReturn(false); | ||
| enablePlugin("Citizens"); | ||
|
|
||
| new HooksRegistrar<CorePlugin>().register(context()); | ||
|
|
||
| assertTrue(hookRegistry.pluginHook(CorePluginHookKey.CORE_CITIZENS).isPresent()); | ||
| assertTrue(hookRegistry.pluginHook(CorePluginHookKey.CORE_CITIZENS).get() instanceof CoreCitizensHook); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Given all testable plugins enabled, when registering hooks, then all 7 hooks are registered") | ||
| void register_registersAllHooks_whenAllPluginsEnabled() { | ||
| try (MockedStatic<Bukkit> bukkit = mockStatic(Bukkit.class)) { | ||
| bukkit.when(Bukkit::getPluginManager).thenReturn(mockPluginManager); | ||
| when(mockPluginManager.isPluginEnabled(anyString())).thenReturn(false); | ||
| enableAllPlugins(); | ||
|
|
||
| new HooksRegistrar<CorePlugin>().register(context()); | ||
|
|
||
| assertTrue(hookRegistry.pluginHook(CorePluginHookKey.CORE_NEXO).isPresent()); | ||
| assertTrue(hookRegistry.pluginHook(CorePluginHookKey.CORE_ITEMS_ADDER).isPresent()); | ||
| assertTrue(hookRegistry.pluginHook(CorePluginHookKey.CORE_PAPI).isPresent()); | ||
| assertTrue(hookRegistry.pluginHook(CorePluginHookKey.CORE_MODEL_ENGINE).isPresent()); | ||
| assertTrue(hookRegistry.pluginHook(CorePluginHookKey.CORE_MYTHIC_MOBS).isPresent()); | ||
| assertTrue(hookRegistry.pluginHook(CorePluginHookKey.CORE_CMI).isPresent()); | ||
| assertTrue(hookRegistry.pluginHook(CorePluginHookKey.CORE_CITIZENS).isPresent()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Given only a subset of plugins enabled, when registering hooks, then only those hooks are registered") | ||
| void register_registersOnlyEnabledHooks_whenSubsetEnabled() { | ||
| try (MockedStatic<Bukkit> bukkit = mockStatic(Bukkit.class)) { | ||
| bukkit.when(Bukkit::getPluginManager).thenReturn(mockPluginManager); | ||
| when(mockPluginManager.isPluginEnabled(anyString())).thenReturn(false); | ||
| enablePlugin("Nexo"); | ||
| enablePlugin("Citizens"); | ||
|
|
||
| new HooksRegistrar<CorePlugin>().register(context()); | ||
|
|
||
| assertTrue(hookRegistry.pluginHook(CorePluginHookKey.CORE_NEXO).isPresent()); | ||
| assertTrue(hookRegistry.pluginHook(CorePluginHookKey.CORE_CITIZENS).isPresent()); | ||
| assertFalse(hookRegistry.pluginHook(CorePluginHookKey.CORE_ITEMS_ADDER).isPresent()); | ||
| assertFalse(hookRegistry.pluginHook(CorePluginHookKey.CORE_PAPI).isPresent()); | ||
| assertFalse(hookRegistry.pluginHook(CorePluginHookKey.CORE_MODEL_ENGINE).isPresent()); | ||
| assertFalse(hookRegistry.pluginHook(CorePluginHookKey.CORE_MYTHIC_MOBS).isPresent()); | ||
| assertFalse(hookRegistry.pluginHook(CorePluginHookKey.CORE_CMI).isPresent()); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.