diff --git a/src/test/java/com/alttd/altiqueue/listeners/ConnectionListenerTest.java b/src/test/java/com/alttd/altiqueue/listeners/ConnectionListenerTest.java new file mode 100644 index 0000000..53d42bc --- /dev/null +++ b/src/test/java/com/alttd/altiqueue/listeners/ConnectionListenerTest.java @@ -0,0 +1,154 @@ +package com.alttd.altiqueue.listeners; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +import com.alttd.altitudetag.AltitudeTag; +import com.alttd.altitudetag.configuration.Lang; +import com.alttd.altitudetag.listeners.ConnectionListener; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerQuitEvent; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.reflect.Whitebox; + +import static org.junit.Assert.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; +import static org.powermock.api.mockito.PowerMockito.doReturn; +import static org.powermock.api.mockito.PowerMockito.*; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({ AltitudeTag.class, Bukkit.class }) +public class ConnectionListenerTest +{ + private ConnectionListener listener; + + private PlayerJoinEvent joinEvent; + + private PlayerQuitEvent quitEvent; + + private Player player; + private Player differentPlayer; + + private UUID playerUuid; + private UUID differentUuid; + + private List players; + + @Before + public void setup() + { + // create the uuids used + differentUuid = UUID.fromString("00000000-0000-0000-0000-000000000001"); + playerUuid = UUID.fromString("00000000-0000-0000-0000-000000000000"); + + listener = new ConnectionListener(); + + // set up the base player + player = mock(Player.class); + when(player.getUniqueId()).thenReturn(playerUuid); + + // set up the other player + differentPlayer = mock(Player.class); + when(differentPlayer.getUniqueId()).thenReturn(differentUuid); + + // create the list of players + players = new ArrayList<>(); + players.add(player); + players.add(differentPlayer); + + // Bukkit is mine now! + mockStatic(Bukkit.class); + + // AltitudeTag is... still mine! + mockStatic(AltitudeTag.class); + when(AltitudeTag.setTagger(any())).thenCallRealMethod(); + + // do return players when Bukkit.getOnlinePlayers() is called + doReturn(players).when(Bukkit.class); + Bukkit.getOnlinePlayers(); + + // set the instance of AltitudeTag + Whitebox.setInternalState(AltitudeTag.class, "instance", mock(AltitudeTag.class)); + + joinEvent = new PlayerJoinEvent(player, ""); + quitEvent = new PlayerQuitEvent(player, ""); + } + + @Test + public void test_join_no_tagger() + { + // test when there is no tagger + when(AltitudeTag.getTagger()).thenReturn(null).thenCallRealMethod(); + + listener.onJoin(joinEvent); + + ArgumentCaptor argument = ArgumentCaptor.forClass(String[].class); + verify(player, times(1)).sendMessage(argument.capture()); + assertArrayEquals(Lang.YOURE_IT.getMessage(), argument.getValue()); + + assertSame(playerUuid, AltitudeTag.getTagger()); + } + + @Test + public void test_join_active_tagger() + { + // test when there is a tagger + when(AltitudeTag.getTagger()).thenReturn(differentUuid); + + listener.onJoin(joinEvent); + + verify(player, never()).sendMessage(any(String.class)); + verify(player, never()).sendMessage(any(String[].class)); + assertNotSame(playerUuid, AltitudeTag.getTagger()); + verifyStatic(Bukkit.class, never()); + Bukkit.getOnlinePlayers(); + } + + @Test + public void test_quit_not_tagger() + { + // test when they leave and aren't the tagger + when(AltitudeTag.getTagger()).thenReturn(differentUuid); + + listener.onLeave(quitEvent); + + verifyStatic(Bukkit.class, never()); + Bukkit.getOnlinePlayers(); + } + + @Test + public void test_quit_active_tagger_no_others() + { + // set the tagger to be the event player + when(AltitudeTag.getTagger()).thenReturn(playerUuid); + // make the event player be the only one online + players.remove(differentPlayer); + + listener.onLeave(quitEvent); + + verifyStatic(AltitudeTag.class, times(1)); + AltitudeTag.setTagger(null); + } + + @Test + public void test_quit_active_tagger_are_others() + { + when(AltitudeTag.getTagger()).thenReturn(playerUuid); + + listener.onLeave(quitEvent); + + verifyStatic(AltitudeTag.class, times(1)); + AltitudeTag.setTagger(differentUuid); + } +} diff --git a/src/test/java/com/alttd/altiqueue/listeners/InteractListenerTest.java b/src/test/java/com/alttd/altiqueue/listeners/InteractListenerTest.java new file mode 100644 index 0000000..4a40325 --- /dev/null +++ b/src/test/java/com/alttd/altiqueue/listeners/InteractListenerTest.java @@ -0,0 +1,217 @@ +package com.alttd.altiqueue.listeners; + +import java.util.UUID; +import java.util.function.Consumer; + +import com.alttd.altitudetag.AltitudeTag; +import com.alttd.altitudetag.configuration.Lang; +import com.alttd.altitudetag.listeners.InteractListener; +import org.bukkit.entity.Player; +import org.bukkit.entity.Sheep; +import org.bukkit.entity.Wolf; +import org.bukkit.event.entity.EntityDamageByEntityEvent; +import org.bukkit.event.entity.EntityDamageEvent.DamageCause; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.reflect.Whitebox; + +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.powermock.api.mockito.PowerMockito.doNothing; +import static org.powermock.api.mockito.PowerMockito.mock; +import static org.powermock.api.mockito.PowerMockito.mockStatic; +import static org.powermock.api.mockito.PowerMockito.spy; +import static org.powermock.api.mockito.PowerMockito.verifyStatic; +import static org.powermock.api.mockito.PowerMockito.when; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({ AltitudeTag.class }) +public class InteractListenerTest +{ + private InteractListener listener; + + private EntityDamageByEntityEvent event; + + private Player attacker; + private UUID attackerUuid = UUID.fromString("00000000-0000-0000-0000-000000000001"); + + private Player victim; + private UUID victimUuid = UUID.fromString("00000000-0000-0000-0000-000000000000"); + + @Before + public void setup() + { + listener = new InteractListener(); + } + + private void setup_both_players() + { + attacker = mock(Player.class); + when(attacker.getUniqueId()).thenReturn(attackerUuid); + + victim = mock(Player.class); + when(victim.getUniqueId()).thenReturn(victimUuid); + + event = spy(new EntityDamageByEntityEvent(attacker, victim, DamageCause.ENTITY_ATTACK, 1d)); + + mockStatic(AltitudeTag.class); + when(AltitudeTag.getTagger()).thenCallRealMethod(); + when(AltitudeTag.setTagger(any())).thenCallRealMethod(); + + Whitebox.setInternalState(AltitudeTag.class, "instance", mock(AltitudeTag.class)); + } + + @Test + public void test_two_players_normal() + { + setup_both_players(); + + // override addTag + ArgumentCaptor runnableCaptor = ArgumentCaptor.forClass(Runnable.class); + + doNothing().when(AltitudeTag.class); + AltitudeTag.addTag(eq(attackerUuid), runnableCaptor.capture()); + + // override getTag + ArgumentCaptor consumerCaptor = ArgumentCaptor.forClass(Consumer.class); + + doNothing().when(AltitudeTag.class); + AltitudeTag.getTags(eq(attackerUuid), consumerCaptor.capture()); + + // they're always online + when(attacker.isOnline()).thenReturn(true); + + // call the event + listener.onHit(event); + + // capture the runnable and run it + Runnable addTagRunnable = runnableCaptor.getValue(); + addTagRunnable.run(); + + // capture the consumer and run it + Consumer getTagsConsumer = consumerCaptor.getValue(); + getTagsConsumer.accept(10); + + verify(attacker, times(1)).sendMessage(Lang.TAGGED.getMessage("{tags}", 10)); + assertEquals(victimUuid, AltitudeTag.getTagger()); + verify(victim, times(1)).sendMessage(Lang.YOURE_IT.getMessage()); + } + + @Test + public void test_two_players_tagger_leave_before_message() + { + setup_both_players(); + + // override addTag + ArgumentCaptor runnableCaptor = ArgumentCaptor.forClass(Runnable.class); + doNothing().when(AltitudeTag.class); + AltitudeTag.addTag(eq(attackerUuid), runnableCaptor.capture()); + + // override getTag + ArgumentCaptor consumerCaptor = ArgumentCaptor.forClass(Consumer.class); + doNothing().when(AltitudeTag.class); + AltitudeTag.getTags(eq(attackerUuid), consumerCaptor.capture()); + + // they're always online + when(attacker.isOnline()).thenReturn(true).thenReturn(false); + + // call the event + listener.onHit(event); + + // capture the runnable and run it + Runnable addTagRunnable = runnableCaptor.getValue(); + addTagRunnable.run(); + + // capture the consumer and run it + Consumer getTagsConsumer = consumerCaptor.getValue(); + getTagsConsumer.accept(10); + + // assertions + verify(attacker, times(2)).isOnline(); + + verify(attacker, never()).sendMessage((String) any()); + verify(attacker, never()).sendMessage((String[]) any()); + + assertEquals(victimUuid, AltitudeTag.getTagger()); + + verify(victim, times(1)).sendMessage(Lang.YOURE_IT.getMessage()); + } + + @Test + public void test_two_players_tagger_leave_before_get_tags() + { + setup_both_players(); + + // override addTag + ArgumentCaptor runnableCaptor = ArgumentCaptor.forClass(Runnable.class); + + doNothing().when(AltitudeTag.class); + AltitudeTag.addTag(eq(attackerUuid), runnableCaptor.capture()); + + // they're always offline + when(attacker.isOnline()).thenReturn(false); + + // call the event + listener.onHit(event); + + // capture the runnable and run it + Runnable addTagRunnable = runnableCaptor.getValue(); + addTagRunnable.run(); + + verify(attacker, never()).sendMessage((String) any()); + verify(attacker, never()).sendMessage((String[]) any()); + + verifyStatic(AltitudeTag.class, never()); + AltitudeTag.getTags(any(), any()); + + assertEquals(victimUuid, AltitudeTag.getTagger()); + verify(victim, times(1)).sendMessage(Lang.YOURE_IT.getMessage()); + } + + @Test + public void test_no_players() + { + Wolf attacker = mock(Wolf.class); + Sheep victim = mock(Sheep.class); + + event = spy(new EntityDamageByEntityEvent(attacker, victim, DamageCause.ENTITY_ATTACK, 1d)); + + listener.onHit(event); + + verify(event, never()).getEntity(); + } + + @Test + public void test_player_attacker_non_player_victim() + { + Player attacker = mock(Player.class); + Sheep victim = mock(Sheep.class); + + event = spy(new EntityDamageByEntityEvent(attacker, victim, DamageCause.ENTITY_ATTACK, 1d)); + + listener.onHit(event); + + verify(event, times(1)).getEntity(); + } + + @Test + public void test_non_player_attacker_player_victim() + { + Wolf attacker = mock(Wolf.class); + Player victim = mock(Player.class); + + event = spy(new EntityDamageByEntityEvent(attacker, victim, DamageCause.ENTITY_ATTACK, 1d)); + + listener.onHit(event); + + verify(event, never()).getEntity(); + } +}