mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-11-27 04:35:12 +01:00
#806 Add tests and avoid sessions cleanup if they're disabled
This commit is contained in:
parent
f2d7fe908e
commit
b4ea396d08
@ -1,5 +1,6 @@
|
||||
package fr.xephi.authme.cache;
|
||||
|
||||
import fr.xephi.authme.ConsoleLogger;
|
||||
import fr.xephi.authme.initialization.HasCleanup;
|
||||
import fr.xephi.authme.initialization.SettingsDependent;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
@ -74,11 +75,15 @@ public class SessionManager implements SettingsDependent, HasCleanup {
|
||||
// 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) {
|
||||
return;
|
||||
}
|
||||
final long currentTime = System.currentTimeMillis();
|
||||
Iterator<Map.Entry<String, Long>> iterator = sessions.entrySet().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
|
@ -9,7 +9,9 @@ import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.hamcrest.Matchers.aMapWithSize;
|
||||
import static org.hamcrest.Matchers.anEmptyMap;
|
||||
import static org.hamcrest.Matchers.containsInAnyOrder;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
@ -122,6 +124,43 @@ public class SessionManagerTest {
|
||||
assertThat(getSessionsMap(manager), anEmptyMap());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPerformCleanup() {
|
||||
// given
|
||||
Settings settings = mockSettings(true, 1);
|
||||
SessionManager manager = new SessionManager(settings);
|
||||
Map<String, Long> sessions = getSessionsMap(manager);
|
||||
sessions.put("somebody", System.currentTimeMillis() - 123L);
|
||||
sessions.put("someone", System.currentTimeMillis() + 4040L);
|
||||
sessions.put("anyone", System.currentTimeMillis() - 1000L);
|
||||
sessions.put("everyone", System.currentTimeMillis() + 60000L);
|
||||
|
||||
// when
|
||||
manager.performCleanup();
|
||||
|
||||
// then
|
||||
assertThat(sessions, aMapWithSize(2));
|
||||
assertThat(sessions.keySet(), containsInAnyOrder("someone", "everyone"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotPerformCleanup() {
|
||||
// given
|
||||
Settings settings = mockSettings(false, 1);
|
||||
SessionManager manager = new SessionManager(settings);
|
||||
Map<String, Long> sessions = getSessionsMap(manager);
|
||||
sessions.put("somebody", System.currentTimeMillis() - 123L);
|
||||
sessions.put("someone", System.currentTimeMillis() + 4040L);
|
||||
sessions.put("anyone", System.currentTimeMillis() - 1000L);
|
||||
sessions.put("everyone", System.currentTimeMillis() + 60000L);
|
||||
|
||||
// when
|
||||
manager.performCleanup();
|
||||
|
||||
// then
|
||||
assertThat(sessions, aMapWithSize(4)); // map not changed -> no cleanup performed
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static Map<String, Long> getSessionsMap(SessionManager manager) {
|
||||
return (Map<String, Long>) ReflectionTestUtils
|
||||
|
46
src/test/java/fr/xephi/authme/task/CleanupTaskTest.java
Normal file
46
src/test/java/fr/xephi/authme/task/CleanupTaskTest.java
Normal file
@ -0,0 +1,46 @@
|
||||
package fr.xephi.authme.task;
|
||||
|
||||
import ch.jalu.injector.Injector;
|
||||
import fr.xephi.authme.initialization.HasCleanup;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.only;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Test for {@link CleanupTask}.
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class CleanupTaskTest {
|
||||
|
||||
@InjectMocks
|
||||
private CleanupTask cleanupTask;
|
||||
|
||||
@Mock
|
||||
private Injector injector;
|
||||
|
||||
@Test
|
||||
public void shouldPerformCleanup() {
|
||||
// given
|
||||
List<HasCleanup> services = asList(mock(HasCleanup.class), mock(HasCleanup.class), mock(HasCleanup.class));
|
||||
given(injector.retrieveAllOfType(HasCleanup.class)).willReturn(services);
|
||||
|
||||
// when
|
||||
cleanupTask.run();
|
||||
|
||||
// then
|
||||
verify(services.get(0)).performCleanup();
|
||||
verify(services.get(1)).performCleanup();
|
||||
verify(services.get(2)).performCleanup();
|
||||
verify(injector, only()).retrieveAllOfType(HasCleanup.class);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user