|
| 1 | +package world.bentobox.aoneblock.listeners; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertNotNull; |
| 4 | +import static org.mockito.ArgumentMatchers.any; |
| 5 | +import static org.mockito.ArgumentMatchers.anyDouble; |
| 6 | +import static org.mockito.ArgumentMatchers.anyInt; |
| 7 | +import static org.mockito.ArgumentMatchers.anyLong; |
| 8 | +import static org.mockito.ArgumentMatchers.anyString; |
| 9 | +import static org.mockito.ArgumentMatchers.eq; |
| 10 | +import static org.mockito.ArgumentMatchers.isNull; |
| 11 | +import static org.mockito.Mockito.mock; |
| 12 | +import static org.mockito.Mockito.never; |
| 13 | +import static org.mockito.Mockito.times; |
| 14 | +import static org.mockito.Mockito.verify; |
| 15 | +import static org.mockito.Mockito.when; |
| 16 | + |
| 17 | +import java.util.Collection; |
| 18 | +import java.util.List; |
| 19 | +import java.util.UUID; |
| 20 | +import java.util.concurrent.CompletableFuture; |
| 21 | + |
| 22 | +import org.bukkit.Bukkit; |
| 23 | +import org.bukkit.Chunk; |
| 24 | +import org.bukkit.Location; |
| 25 | +import org.bukkit.World; |
| 26 | +import org.bukkit.entity.Entity; |
| 27 | +import org.bukkit.entity.EntityType; |
| 28 | +import org.bukkit.entity.Player; |
| 29 | +import org.bukkit.entity.TextDisplay; |
| 30 | +import org.bukkit.scheduler.BukkitScheduler; |
| 31 | +import org.bukkit.util.Vector; |
| 32 | +import org.eclipse.jdt.annotation.NonNull; |
| 33 | +import org.junit.After; |
| 34 | +import org.junit.Before; |
| 35 | +import org.junit.Test; |
| 36 | +import org.junit.runner.RunWith; |
| 37 | +import org.mockito.Mock; |
| 38 | +import org.mockito.Mockito; |
| 39 | +import org.powermock.api.mockito.PowerMockito; |
| 40 | +import org.powermock.core.classloader.annotations.PrepareForTest; |
| 41 | +import org.powermock.modules.junit4.PowerMockRunner; |
| 42 | +import org.powermock.reflect.Whitebox; |
| 43 | + |
| 44 | +import world.bentobox.aoneblock.AOneBlock; |
| 45 | +import world.bentobox.aoneblock.Settings; |
| 46 | +import world.bentobox.aoneblock.dataobjects.OneBlockIslands; |
| 47 | +import world.bentobox.aoneblock.oneblocks.OneBlockPhase; |
| 48 | +import world.bentobox.bentobox.BentoBox; |
| 49 | +import world.bentobox.bentobox.api.events.island.IslandDeleteEvent; |
| 50 | +import world.bentobox.bentobox.api.user.User; |
| 51 | +import world.bentobox.bentobox.database.objects.Island; |
| 52 | +import world.bentobox.bentobox.managers.LocalesManager; |
| 53 | +import world.bentobox.bentobox.managers.PlaceholdersManager; |
| 54 | +import world.bentobox.bentobox.util.Util; |
| 55 | + |
| 56 | +/** |
| 57 | + * @author tastybento |
| 58 | + * |
| 59 | + */ |
| 60 | +@RunWith(PowerMockRunner.class) |
| 61 | +@PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class }) |
| 62 | +public class HoloListenerTest { |
| 63 | + @Mock |
| 64 | + private BentoBox plugin; |
| 65 | + @Mock |
| 66 | + AOneBlock addon; |
| 67 | + private HoloListener hl; |
| 68 | + @Mock |
| 69 | + private Island island; |
| 70 | + private UUID uuid = UUID.randomUUID(); |
| 71 | + @Mock |
| 72 | + private Location location; |
| 73 | + @Mock |
| 74 | + private World world; |
| 75 | + @Mock |
| 76 | + private TextDisplay hologram; |
| 77 | + private Settings settings; |
| 78 | + @Mock |
| 79 | + private @NonNull OneBlockIslands is; |
| 80 | + @Mock |
| 81 | + private Player player; |
| 82 | + @Mock |
| 83 | + private LocalesManager lm; |
| 84 | + @Mock |
| 85 | + private PlaceholdersManager pm; |
| 86 | + @Mock |
| 87 | + private BukkitScheduler scheduler; |
| 88 | + @Mock |
| 89 | + private @NonNull OneBlockPhase phase; |
| 90 | + |
| 91 | + |
| 92 | + /** |
| 93 | + * @throws java.lang.Exception |
| 94 | + */ |
| 95 | + @Before |
| 96 | + public void setUp() throws Exception { |
| 97 | + // Set up plugin |
| 98 | + BentoBox plugin = mock(BentoBox.class); |
| 99 | + Whitebox.setInternalState(BentoBox.class, "instance", plugin); |
| 100 | + // Settings |
| 101 | + settings = new Settings(); |
| 102 | + when(addon.getSettings()).thenReturn(settings); |
| 103 | + |
| 104 | + // Island |
| 105 | + when(island.getWorld()).thenReturn(world); |
| 106 | + when(island.getCenter()).thenReturn(location); |
| 107 | + when(island.getOwner()).thenReturn(uuid); |
| 108 | + |
| 109 | + // World |
| 110 | + when(world.spawn(any(Location.class), eq(TextDisplay.class))).thenReturn(hologram); |
| 111 | + |
| 112 | + // OneBlock Island |
| 113 | + when(is.getHologram()).thenReturn("Hologram"); |
| 114 | + // Location |
| 115 | + when(location.getWorld()).thenReturn(world); |
| 116 | + when(location.clone()).thenReturn(location); |
| 117 | + when(location.add(any(Vector.class))).thenReturn(location); |
| 118 | + // Chunks |
| 119 | + PowerMockito.mockStatic(Util.class, Mockito.RETURNS_MOCKS); |
| 120 | + CompletableFuture<Chunk> future = CompletableFuture.completedFuture(null); |
| 121 | + when(Util.getChunkAtAsync(location)).thenReturn(future); // Load the chunk immediately |
| 122 | + |
| 123 | + Player nonHolo = mock(Player.class); |
| 124 | + |
| 125 | + Collection<Entity> entities = List.of(nonHolo , hologram); |
| 126 | + // Get entities |
| 127 | + when(hologram.getType()).thenReturn(EntityType.TEXT_DISPLAY); |
| 128 | + when(world.getNearbyEntities(any(Location.class), anyDouble(), anyDouble(), anyDouble())).thenReturn(entities); |
| 129 | + |
| 130 | + // Player and translations |
| 131 | + when(player.getUniqueId()).thenReturn(uuid); |
| 132 | + User.getInstance(player); |
| 133 | + User.setPlugin(plugin); |
| 134 | + |
| 135 | + when(plugin.getLocalesManager()).thenReturn(lm); |
| 136 | + when(plugin.getPlaceholdersManager()).thenReturn(pm); |
| 137 | + |
| 138 | + // Bukkit |
| 139 | + PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); |
| 140 | + when(Bukkit.getScheduler()).thenReturn(scheduler); |
| 141 | + |
| 142 | + // DUT |
| 143 | + hl = new HoloListener(addon); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * @throws java.lang.Exception |
| 148 | + */ |
| 149 | + @After |
| 150 | + public void tearDown() throws Exception { |
| 151 | + User.clearUsers(); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Test method for {@link world.bentobox.aoneblock.listeners.HoloListener#HoloListener(world.bentobox.aoneblock.AOneBlock)}. |
| 156 | + */ |
| 157 | + @Test |
| 158 | + public void testHoloListener() { |
| 159 | + assertNotNull(hl); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Test method for {@link world.bentobox.aoneblock.listeners.HoloListener#onDeletedIsland(world.bentobox.bentobox.api.events.island.IslandDeleteEvent)}. |
| 164 | + */ |
| 165 | + @Test |
| 166 | + public void testOnDeletedIsland() { |
| 167 | + IslandDeleteEvent event = new IslandDeleteEvent(island, uuid, false, location); |
| 168 | + this.hl.onDeletedIsland(event); |
| 169 | + verify(hologram).remove(); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Test method for {@link world.bentobox.aoneblock.listeners.HoloListener#onDisable()}. |
| 174 | + */ |
| 175 | + @Test |
| 176 | + public void testOnDisable() { |
| 177 | + hl.setUp(island, is, true); |
| 178 | + hl.onDisable(); |
| 179 | + verify(hologram, times(2)).remove(); |
| 180 | + } |
| 181 | + |
| 182 | + |
| 183 | + /** |
| 184 | + * Test method for {@link world.bentobox.aoneblock.listeners.HoloListener#setUp(world.bentobox.bentobox.database.objects.Island, world.bentobox.aoneblock.dataobjects.OneBlockIslands, boolean)}. |
| 185 | + */ |
| 186 | + @Test |
| 187 | + public void testSetUpNoHolograms() { |
| 188 | + settings.setUseHolograms(false); |
| 189 | + hl.setUp(island, is, true); |
| 190 | + verify(scheduler, never()).runTaskLater(isNull(), any(Runnable.class), anyLong()); |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * Test method for {@link world.bentobox.aoneblock.listeners.HoloListener#setUp(world.bentobox.bentobox.database.objects.Island, world.bentobox.aoneblock.dataobjects.OneBlockIslands, boolean)}. |
| 195 | + */ |
| 196 | + @Test |
| 197 | + public void testSetUpNoScheduling() { |
| 198 | + settings.setHologramDuration(0); |
| 199 | + hl.setUp(island, is, true); |
| 200 | + verify(scheduler, never()).runTaskLater(isNull(), any(Runnable.class), anyLong()); |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * Test method for {@link world.bentobox.aoneblock.listeners.HoloListener#setUp(world.bentobox.bentobox.database.objects.Island, world.bentobox.aoneblock.dataobjects.OneBlockIslands, boolean)}. |
| 205 | + */ |
| 206 | + @Test |
| 207 | + public void testSetUpNewIsland() { |
| 208 | + hl.setUp(island, is, true); |
| 209 | + verify(is).setHologram(""); |
| 210 | + verify(scheduler).runTaskLater(isNull(), any(Runnable.class), anyLong()); |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * Test method for {@link world.bentobox.aoneblock.listeners.HoloListener#setUp(world.bentobox.bentobox.database.objects.Island, world.bentobox.aoneblock.dataobjects.OneBlockIslands, boolean)}. |
| 215 | + */ |
| 216 | + @Test |
| 217 | + public void testSetUpNotNewIsland() { |
| 218 | + hl.setUp(island, is, false); |
| 219 | + verify(is, never()).setHologram(anyString()); |
| 220 | + verify(scheduler).runTaskLater(isNull(), any(Runnable.class), anyLong()); |
| 221 | + } |
| 222 | + |
| 223 | + |
| 224 | + /** |
| 225 | + * Test method for {@link world.bentobox.aoneblock.listeners.HoloListener#process(world.bentobox.bentobox.database.objects.Island, world.bentobox.aoneblock.dataobjects.OneBlockIslands, world.bentobox.aoneblock.oneblocks.OneBlockPhase)}. |
| 226 | + */ |
| 227 | + @Test |
| 228 | + public void testProcessNull() { |
| 229 | + when(phase.getHologramLine(anyInt())).thenReturn(null); |
| 230 | + when(is.getHologram()).thenReturn(""); // Return blank |
| 231 | + hl.process(island, is, phase); |
| 232 | + verify(scheduler, never()).runTaskLater(isNull(), any(Runnable.class), anyLong()); |
| 233 | + } |
| 234 | + |
| 235 | + /** |
| 236 | + * Test method for {@link world.bentobox.aoneblock.listeners.HoloListener#process(world.bentobox.bentobox.database.objects.Island, world.bentobox.aoneblock.dataobjects.OneBlockIslands, world.bentobox.aoneblock.oneblocks.OneBlockPhase)}. |
| 237 | + */ |
| 238 | + @Test |
| 239 | + public void testProcess() { |
| 240 | + when(phase.getHologramLine(anyInt())).thenReturn("my Holo"); |
| 241 | + hl.process(island, is, phase); |
| 242 | + verify(scheduler).runTaskLater(isNull(), any(Runnable.class), anyLong()); |
| 243 | + } |
| 244 | + |
| 245 | +} |
0 commit comments