mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-11-06 18:49:39 +01:00
Minor: remove commented out code
This commit is contained in:
parent
e59df84a84
commit
37dc918ece
@ -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<String> 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();
|
||||
}
|
||||
}
|
||||
}
|
@ -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<String> 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<String> expiringSet = mockExpiringSet();
|
||||
setSessionsMap(manager, expiringSet);
|
||||
|
||||
// when
|
||||
manager.performCleanup();
|
||||
|
||||
// then
|
||||
verify(expiringSet, never()).removeExpiredEntries();
|
||||
}
|
||||
|
||||
private static ExpiringSet<String> getSessionsMap(SessionManager manager) {
|
||||
return ReflectionTestUtils.getFieldValue(SessionManager.class, manager, "sessions");
|
||||
}
|
||||
|
||||
private static void setSessionsMap(SessionManager manager, ExpiringSet<String> sessionsMap) {
|
||||
ReflectionTestUtils.setField(SessionManager.class, manager, "sessions", sessionsMap);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <T> ExpiringSet<T> 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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user