From e1f893afe38883fbcecb4b40deeecb1cee817cee Mon Sep 17 00:00:00 2001 From: Gabriele C Date: Tue, 21 Jan 2020 11:27:28 +0100 Subject: [PATCH] Fix some unit testing --- .../fr/xephi/authme/service/AntiBotService.java | 6 +++--- ...cCounter.java => AtomicIntervalCounter.java} | 4 ++-- .../authme/listener/PlayerListenerTest.java | 17 ++++++++++------- 3 files changed, 15 insertions(+), 12 deletions(-) rename src/main/java/fr/xephi/authme/util/{AtomicCounter.java => AtomicIntervalCounter.java} (87%) diff --git a/src/main/java/fr/xephi/authme/service/AntiBotService.java b/src/main/java/fr/xephi/authme/service/AntiBotService.java index e3a1ab089..14e1fd51e 100644 --- a/src/main/java/fr/xephi/authme/service/AntiBotService.java +++ b/src/main/java/fr/xephi/authme/service/AntiBotService.java @@ -10,7 +10,7 @@ import fr.xephi.authme.permission.AdminPermission; import fr.xephi.authme.permission.PermissionsManager; import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.properties.ProtectionSettings; -import fr.xephi.authme.util.AtomicCounter; +import fr.xephi.authme.util.AtomicIntervalCounter; import org.bukkit.scheduler.BukkitTask; import javax.inject.Inject; @@ -35,7 +35,7 @@ public class AntiBotService implements SettingsDependent { private AntiBotStatus antiBotStatus; private boolean startup; private BukkitTask disableTask; - private AtomicCounter flaggedCounter; + private AtomicIntervalCounter flaggedCounter; @Inject AntiBotService(Settings settings, Messages messages, PermissionsManager permissionsManager, @@ -58,7 +58,7 @@ public class AntiBotService implements SettingsDependent { duration = settings.getProperty(ProtectionSettings.ANTIBOT_DURATION); int sensibility = settings.getProperty(ProtectionSettings.ANTIBOT_SENSIBILITY); int interval = settings.getProperty(ProtectionSettings.ANTIBOT_INTERVAL) * 1000; - flaggedCounter = new AtomicCounter(sensibility, interval); + flaggedCounter = new AtomicIntervalCounter(sensibility, interval); // Stop existing protection stopProtection(); diff --git a/src/main/java/fr/xephi/authme/util/AtomicCounter.java b/src/main/java/fr/xephi/authme/util/AtomicIntervalCounter.java similarity index 87% rename from src/main/java/fr/xephi/authme/util/AtomicCounter.java rename to src/main/java/fr/xephi/authme/util/AtomicIntervalCounter.java index a5b929e65..b7abcf850 100644 --- a/src/main/java/fr/xephi/authme/util/AtomicCounter.java +++ b/src/main/java/fr/xephi/authme/util/AtomicIntervalCounter.java @@ -1,12 +1,12 @@ package fr.xephi.authme.util; -public class AtomicCounter { +public class AtomicIntervalCounter { private final int threshold; private final int interval; private int count; private long lastInsert; - public AtomicCounter(int threshold, int interval) { + public AtomicIntervalCounter(int threshold, int interval) { this.threshold = threshold; this.interval = interval; reset(); diff --git a/src/test/java/fr/xephi/authme/listener/PlayerListenerTest.java b/src/test/java/fr/xephi/authme/listener/PlayerListenerTest.java index e72d48be6..04f45ab1c 100644 --- a/src/test/java/fr/xephi/authme/listener/PlayerListenerTest.java +++ b/src/test/java/fr/xephi/authme/listener/PlayerListenerTest.java @@ -646,7 +646,7 @@ public class PlayerListenerTest { } @Test - public void shouldNotInterfereWithUnrestrictedUser() { + public void shouldNotInterfereWithUnrestrictedUser() throws FailedVerificationException { // given String name = "Player01"; Player player = mockPlayerWithName(name); @@ -658,12 +658,13 @@ public class PlayerListenerTest { // then verify(validationService).isUnrestricted(name); + verify(onJoinVerifier).checkSingleSession(name); verifyNoModifyingCalls(event); - verifyNoInteractions(onJoinVerifier); + verifyNoMoreInteractions(onJoinVerifier); } @Test - public void shouldStopHandlingForFullServer() { + public void shouldStopHandlingForFullServer() throws FailedVerificationException { // given String name = "someone"; Player player = mockPlayerWithName(name); @@ -676,12 +677,14 @@ public class PlayerListenerTest { // then verify(validationService).isUnrestricted(name); - verify(onJoinVerifier, only()).refusePlayerForFullServer(event); + verify(onJoinVerifier).checkSingleSession(name); + verify(onJoinVerifier).refusePlayerForFullServer(event); + verifyNoMoreInteractions(onJoinVerifier); verifyNoModifyingCalls(event); } @Test - public void shouldStopHandlingEventForBadResult() { + public void shouldStopHandlingEventForBadResult() throws FailedVerificationException { // given String name = "someone"; Player player = mockPlayerWithName(name); @@ -696,7 +699,8 @@ public class PlayerListenerTest { // then verify(validationService).isUnrestricted(name); - verify(onJoinVerifier, only()).refusePlayerForFullServer(event); + verify(onJoinVerifier).checkSingleSession(name); + verify(onJoinVerifier).refusePlayerForFullServer(event); verifyNoModifyingCalls(event); } @@ -715,7 +719,6 @@ public class PlayerListenerTest { // then verify(validationService).isUnrestricted(name); - verify(onJoinVerifier).checkSingleSession(name); verify(onJoinVerifier).checkIsValidName(name); verifyNoInteractions(dataSource); verifyNoModifyingCalls(preLoginEvent);