bentobox/src/test/java/world/bentobox/bentobox/managers/PlaceholdersManagerTest.java

117 lines
3.9 KiB
Java
Raw Normal View History

package world.bentobox.bentobox.managers;
2019-08-05 06:44:46 +02:00
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
2019-08-05 06:44:46 +02:00
import java.util.Optional;
import org.eclipse.jdt.annotation.NonNull;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
2024-04-09 05:15:34 +02:00
import org.powermock.reflect.Whitebox;
import world.bentobox.bentobox.BentoBox;
2024-04-09 05:15:34 +02:00
import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.TestWorldSettings;
import world.bentobox.bentobox.api.addons.AddonDescription;
import world.bentobox.bentobox.api.addons.GameModeAddon;
2024-04-09 05:15:34 +02:00
import world.bentobox.bentobox.api.configuration.WorldSettings;
2019-08-05 06:44:46 +02:00
import world.bentobox.bentobox.api.hooks.Hook;
import world.bentobox.bentobox.hooks.placeholders.PlaceholderAPIHook;
import world.bentobox.bentobox.lists.GameModePlaceholder;
/**
* @author tastybento
* @since 1.5.0
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest( {BentoBox.class} )
public class PlaceholdersManagerTest {
2019-04-07 10:13:34 +02:00
@Mock
private BentoBox plugin;
2019-04-07 10:13:34 +02:00
@Mock
private GameModeAddon addon;
private PlaceholdersManager pm;
2019-08-05 06:44:46 +02:00
@Mock
private HooksManager hm;
@Mock
private PlaceholderAPIHook hook;
2024-04-09 05:15:34 +02:00
@Mock
private IslandWorldManager iwm;
private Settings settings;
2019-04-07 10:13:34 +02:00
@Before
public void setUp() throws Exception {
2024-04-09 05:15:34 +02:00
// Set up plugin
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
settings = new Settings();
when(plugin.getSettings()).thenReturn(settings);
2019-04-07 10:13:34 +02:00
// Addon
@NonNull
AddonDescription desc = new AddonDescription.Builder("main", "bskyblock", "1.0").build();
when(addon.getDescription()).thenReturn(desc);
2019-04-07 10:13:34 +02:00
when(plugin.getPlaceholdersManager()).thenReturn(pm);
// No placeholders registered yet
2019-08-05 06:44:46 +02:00
//when(pm.isPlaceholder(any(), any())).thenReturn(false);
// Hooks
when(plugin.getHooks()).thenReturn(hm);
Optional<Hook> optionalHook = Optional.of(hook);
when(hm.getHook(eq("PlaceholderAPI"))).thenReturn(optionalHook);
when(hook.isPlaceholder(any(), any())).thenReturn(false);
2024-04-09 05:15:34 +02:00
// World settings
@NonNull
WorldSettings ws = new TestWorldSettings();
when(iwm.getWorldSettings(any())).thenReturn(ws);
when(plugin.getIWM()).thenReturn(iwm);
2019-08-05 06:44:46 +02:00
// Placeholder manager
pm = new PlaceholdersManager(plugin);
2019-04-07 10:13:34 +02:00
}
@After
public void tearDown() {
Mockito.framework().clearInlineMocks();
}
2019-04-07 10:13:34 +02:00
/**
* Test method for {@link world.bentobox.bentobox.managers.PlaceholdersManager#registerDefaultPlaceholders(GameModeAddon)}.
2019-04-07 10:13:34 +02:00
*/
@Test
public void testRegisterGameModePlaceholdersAllDefaults() {
pm.registerDefaultPlaceholders(addon);
// + 300 because we register team member placeholders up to 50 members
2024-04-09 05:15:34 +02:00
verify(hook, times(GameModePlaceholder.values().length + 304)).registerPlaceholder(any(), anyString(), any());
2019-04-07 10:13:34 +02:00
}
2019-04-07 10:13:34 +02:00
/**
* Test method for {@link world.bentobox.bentobox.managers.PlaceholdersManager#registerDefaultPlaceholders(GameModeAddon)}.
2019-04-07 10:13:34 +02:00
*/
@Test
public void testRegisterDefaultPlaceholdersSomePreregistered() {
2019-04-07 10:13:34 +02:00
// Some duplicates
2019-08-05 06:44:46 +02:00
when(hook.isPlaceholder(any(), any())).thenReturn(false, true, true, false, false, true, false);
2019-04-07 10:13:34 +02:00
pm.registerDefaultPlaceholders(addon);
2019-04-07 10:13:34 +02:00
2019-08-05 06:44:46 +02:00
// 3 less registrations for this addon
2024-04-09 05:15:34 +02:00
verify(hook, times(GameModePlaceholder.values().length - 3 + 304)).registerPlaceholder(any(), anyString(),
2024-02-18 23:43:29 +01:00
any());
2019-04-07 10:13:34 +02:00
}
}