diff --git a/excluded/SessionManager.java b/excluded/SessionManager.java deleted file mode 100644 index a443c0cf2..000000000 --- a/excluded/SessionManager.java +++ /dev/null @@ -1,79 +0,0 @@ -package fr.xephi.authme.data; - -import fr.xephi.authme.ConsoleLogger; -import fr.xephi.authme.initialization.HasCleanup; -import fr.xephi.authme.initialization.SettingsDependent; -import fr.xephi.authme.settings.Settings; -import fr.xephi.authme.settings.properties.PluginSettings; -import fr.xephi.authme.util.expiring.ExpiringSet; - -import javax.inject.Inject; -import java.util.concurrent.TimeUnit; - -/** - * Manages sessions, allowing players to be automatically logged in if they join again - * within a configurable amount of time. - */ -public class SessionManager implements SettingsDependent, HasCleanup { - - private final ExpiringSet sessions; - private boolean enabled; - - @Inject - SessionManager(Settings settings) { - long timeout = settings.getProperty(PluginSettings.SESSIONS_TIMEOUT); - sessions = new ExpiringSet<>(timeout, TimeUnit.MINUTES); - enabled = timeout > 0 && settings.getProperty(PluginSettings.SESSIONS_ENABLED); - } - - /** - * Check if a session is available for the given player. - * - * @param name The name to check. - * @return True if a session is found. - */ - private boolean hasSession(String name) { - return enabled && sessions.contains(name.toLowerCase()); - } - - /** - * Add a player session to the cache. - * - * @param name The name of the player. - */ - private void addSession(String name) { - if (enabled) { - sessions.add(name.toLowerCase()); - } - } - - /** - * Remove a player's session from the cache. - * - * @param name The name of the player. - */ - private void removeSession(String name) { - sessions.remove(name.toLowerCase()); - } - - @Override - public void reload(Settings settings) { - long timeoutInMinutes = settings.getProperty(PluginSettings.SESSIONS_TIMEOUT); - sessions.setExpiration(timeoutInMinutes, TimeUnit.MINUTES); - boolean oldEnabled = enabled; - enabled = timeoutInMinutes > 0 && settings.getProperty(PluginSettings.SESSIONS_ENABLED); - - // With this reload, the sessions feature has just been disabled, so clear all stored sessions - if (oldEnabled && !enabled) { - sessions.clear(); - ConsoleLogger.fine("Sessions disabled: cleared all sessions"); - } - } - - @Override - public void performCleanup() { - if (enabled) { - sessions.removeExpiredEntries(); - } - } -} diff --git a/excluded/SessionManagerTest.java b/excluded/SessionManagerTest.java deleted file mode 100644 index 738c1c158..000000000 --- a/excluded/SessionManagerTest.java +++ /dev/null @@ -1,156 +0,0 @@ -package fr.xephi.authme.data; - -import fr.xephi.authme.ReflectionTestUtils; -import fr.xephi.authme.settings.Settings; -import fr.xephi.authme.settings.properties.PluginSettings; -import fr.xephi.authme.util.expiring.ExpiringSet; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.junit.MockitoJUnitRunner; - -import static org.hamcrest.Matchers.equalTo; -import static org.junit.Assert.assertThat; -import static org.mockito.BDDMockito.given; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; - -/** - * Test for {@link SessionManager}. - */ -@RunWith(MockitoJUnitRunner.class) -public class SessionManagerTest { - - @Test - public void shouldHaveSession() { - // given - Settings settings = mockSettings(true, 10); - SessionManager manager = new SessionManager(settings); - String player = "playah"; - - // when - manager.addSession(player); - - // then - assertThat(manager.hasSession(player), equalTo(true)); - } - - @Test - public void shouldNotHaveSession() { - // given - Settings settings = mockSettings(true, 10); - SessionManager manager = new SessionManager(settings); - String player = "playah"; - - // when/then - assertThat(manager.hasSession(player), equalTo(false)); - } - - @Test - public void shouldNotAddSessionBecauseDisabled() { - // given - Settings settings = mockSettings(false, 10); - SessionManager manager = new SessionManager(settings); - String player = "playah"; - - // when - manager.addSession(player); - - // then - assertThat(manager.hasSession(player), equalTo(false)); - } - - @Test - public void shouldNotAddSessionBecauseTimeoutIsZero() { - // given - Settings settings = mockSettings(true, 0); - SessionManager manager = new SessionManager(settings); - String player = "playah"; - - // when - manager.addSession(player); - - // then - assertThat(manager.hasSession(player), equalTo(false)); - } - - @Test - public void shouldRemoveSession() { - // given - Settings settings = mockSettings(true, 10); - String player = "user"; - SessionManager manager = new SessionManager(settings); - manager.addSession(player); - - // when - manager.removeSession(player); - - // then - assertThat(manager.hasSession(player), equalTo(false)); - } - - @Test - public void shouldClearAllSessionsAfterDisable() { - // given - Settings settings = mockSettings(true, 10); - SessionManager manager = new SessionManager(settings); - manager.addSession("player01"); - manager.addSession("player02"); - - // when - manager.reload(mockSettings(false, 20)); - - // then - assertThat(getSessionsMap(manager).isEmpty(), equalTo(true)); - } - - @Test - public void shouldPerformCleanup() { - // given - Settings settings = mockSettings(true, 1); - SessionManager manager = new SessionManager(settings); - ExpiringSet expiringSet = mockExpiringSet(); - setSessionsMap(manager, expiringSet); - - // when - manager.performCleanup(); - - // then - verify(expiringSet).removeExpiredEntries(); - } - - @Test - public void shouldNotPerformCleanup() { - // given - Settings settings = mockSettings(false, 1); - SessionManager manager = new SessionManager(settings); - ExpiringSet expiringSet = mockExpiringSet(); - setSessionsMap(manager, expiringSet); - - // when - manager.performCleanup(); - - // then - verify(expiringSet, never()).removeExpiredEntries(); - } - - private static ExpiringSet getSessionsMap(SessionManager manager) { - return ReflectionTestUtils.getFieldValue(SessionManager.class, manager, "sessions"); - } - - private static void setSessionsMap(SessionManager manager, ExpiringSet sessionsMap) { - ReflectionTestUtils.setField(SessionManager.class, manager, "sessions", sessionsMap); - } - - @SuppressWarnings("unchecked") - private static ExpiringSet mockExpiringSet() { - return mock(ExpiringSet.class); - } - - private static Settings mockSettings(boolean isEnabled, int sessionTimeout) { - Settings settings = mock(Settings.class); - given(settings.getProperty(PluginSettings.SESSIONS_ENABLED)).willReturn(isEnabled); - given(settings.getProperty(PluginSettings.SESSIONS_TIMEOUT)).willReturn(sessionTimeout); - return settings; - } -} diff --git a/src/main/java/fr/xephi/authme/process/logout/ProcessSyncPlayerLogout.java b/src/main/java/fr/xephi/authme/process/logout/ProcessSyncPlayerLogout.java index 4ec9d3475..cecf023c5 100644 --- a/src/main/java/fr/xephi/authme/process/logout/ProcessSyncPlayerLogout.java +++ b/src/main/java/fr/xephi/authme/process/logout/ProcessSyncPlayerLogout.java @@ -1,7 +1,6 @@ package fr.xephi.authme.process.logout; import fr.xephi.authme.ConsoleLogger; -//import fr.xephi.authme.data.SessionManager; import fr.xephi.authme.data.limbo.LimboService; import fr.xephi.authme.events.LogoutEvent; import fr.xephi.authme.listener.protocollib.ProtocolLibService;