diff --git a/src/main/java/fr/xephi/authme/AntiBot.java b/src/main/java/fr/xephi/authme/AntiBot.java index a608a5982..3b8d0afc8 100644 --- a/src/main/java/fr/xephi/authme/AntiBot.java +++ b/src/main/java/fr/xephi/authme/AntiBot.java @@ -3,6 +3,7 @@ package fr.xephi.authme; import fr.xephi.authme.settings.MessageKey; import fr.xephi.authme.settings.Messages; import fr.xephi.authme.settings.Settings; +import fr.xephi.authme.util.Wrapper; import org.bukkit.Bukkit; import org.bukkit.entity.Player; @@ -16,6 +17,7 @@ public class AntiBot { private static final AuthMe plugin = AuthMe.getInstance(); private static final Messages messages = plugin.getMessages(); + private static Wrapper wrapper = new Wrapper(plugin); private static final List antibotPlayers = new ArrayList<>(); private static AntiBotStatus antiBotStatus = AntiBotStatus.DISABLED; @@ -52,7 +54,7 @@ public class AntiBot { Bukkit.broadcastMessage(s); } - Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { + wrapper.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { @Override public void run() { if (antiBotStatus == AntiBotStatus.ACTIVE) { diff --git a/src/main/java/fr/xephi/authme/util/Wrapper.java b/src/main/java/fr/xephi/authme/util/Wrapper.java index 29f806e58..1505a203e 100644 --- a/src/main/java/fr/xephi/authme/util/Wrapper.java +++ b/src/main/java/fr/xephi/authme/util/Wrapper.java @@ -1,7 +1,9 @@ package fr.xephi.authme.util; import fr.xephi.authme.AuthMe; +import org.bukkit.Bukkit; import org.bukkit.Server; +import org.bukkit.scheduler.BukkitScheduler; import java.util.logging.Logger; @@ -29,6 +31,10 @@ public class Wrapper { return authMe.getLogger(); } + public BukkitScheduler getScheduler() { + return Bukkit.getScheduler(); + } + } diff --git a/src/test/java/fr/xephi/authme/AntiBotTest.java b/src/test/java/fr/xephi/authme/AntiBotTest.java new file mode 100644 index 000000000..e56e27762 --- /dev/null +++ b/src/test/java/fr/xephi/authme/AntiBotTest.java @@ -0,0 +1,36 @@ +package fr.xephi.authme; + +import fr.xephi.authme.settings.Settings; +import fr.xephi.authme.util.Wrapper; +import org.junit.Before; +import org.junit.Test; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; + +/** + * Test for {@link AntiBot}. + */ +public class AntiBotTest { + + private Wrapper wrapper; + + @Before + public void setUpMocks() { + AuthMeMockUtil.mockAuthMeInstance(); + wrapper = AuthMeMockUtil.insertMockWrapperInstance(AntiBot.class, "wrapper"); + } + + @Test + public void shouldNotEnableAntiBot() { + // given + Settings.enableAntiBot = false; + + // when + AntiBot.setupAntiBotService(); + + // then + verify(wrapper.getScheduler(), never()).scheduleSyncDelayedTask(any(AuthMe.class), any(Runnable.class)); + } +} diff --git a/src/test/java/fr/xephi/authme/WrapperMock.java b/src/test/java/fr/xephi/authme/WrapperMock.java index bfad67be5..7812c6d33 100644 --- a/src/test/java/fr/xephi/authme/WrapperMock.java +++ b/src/test/java/fr/xephi/authme/WrapperMock.java @@ -2,6 +2,7 @@ package fr.xephi.authme; import fr.xephi.authme.util.Wrapper; import org.bukkit.Server; +import org.bukkit.scheduler.BukkitScheduler; import org.mockito.Mockito; import java.util.HashMap; @@ -40,6 +41,11 @@ public class WrapperMock extends Wrapper { return getMock(AuthMe.class); } + @Override + public BukkitScheduler getScheduler() { + return getMock(BukkitScheduler.class); + } + @SuppressWarnings("unchecked") private static T getMock(Class clazz) { Object o = mocks.get(clazz);