#1113 Attempt to merge new LimboPlayer with an existing one

- Extract some logic into LimboServiceHelper to keep LimboService slim
- Create LimboServiceHelper#merge to merge two LimboPlayers associated with a Player. E.g. if an admin unregisters an online player that has not logged in, the creation of a LimboPlayer is triggered while there already is one in LimboService
This commit is contained in:
ljacqu
2017-03-12 15:56:08 +01:00
parent 689e5eeccc
commit 1678901e02
4 changed files with 240 additions and 100 deletions
@@ -0,0 +1,82 @@
package fr.xephi.authme.data.limbo;
import org.bukkit.Location;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.junit.MockitoJUnitRunner;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyZeroInteractions;
/**
* Test for {@link LimboServiceHelper}.
* <p>
* Note: some methods are tested directly where they are used via {@link LimboServiceTest}.
*/
@RunWith(MockitoJUnitRunner.class)
public class LimboServiceHelperTest {
@InjectMocks
private LimboServiceHelper limboServiceHelper;
@Test
public void shouldMergeLimboPlayers() {
// given
Location newLocation = mock(Location.class);
LimboPlayer newLimbo = new LimboPlayer(newLocation, false, "grp-new", false, 0.0f, 0.0f);
Location oldLocation = mock(Location.class);
LimboPlayer oldLimbo = new LimboPlayer(oldLocation, true, "grp-old", true, 0.1f, 0.8f);
// when
LimboPlayer result = limboServiceHelper.merge(newLimbo, oldLimbo);
// then
assertThat(result.getLocation(), equalTo(oldLocation));
assertThat(result.isOperator(), equalTo(true));
assertThat(result.getGroup(), equalTo("grp-old"));
assertThat(result.isCanFly(), equalTo(true));
assertThat(result.getWalkSpeed(), equalTo(0.1f));
assertThat(result.getFlySpeed(), equalTo(0.8f));
}
@Test
public void shouldFallBackToNewLimboForMissingData() {
// given
Location newLocation = mock(Location.class);
LimboPlayer newLimbo = new LimboPlayer(newLocation, false, "grp-new", true, 0.3f, 0.0f);
Location oldLocation = mock(Location.class);
LimboPlayer oldLimbo = new LimboPlayer(oldLocation, false, "", false, 0.1f, 0.1f);
// when
LimboPlayer result = limboServiceHelper.merge(newLimbo, oldLimbo);
// then
assertThat(result.getLocation(), equalTo(oldLocation));
assertThat(result.isOperator(), equalTo(false));
assertThat(result.getGroup(), equalTo("grp-new"));
assertThat(result.isCanFly(), equalTo(true));
assertThat(result.getWalkSpeed(), equalTo(0.3f));
assertThat(result.getFlySpeed(), equalTo(0.1f));
}
@Test
public void shouldHandleNullInputs() {
// given
LimboPlayer limbo = mock(LimboPlayer.class);
// when
LimboPlayer result1 = limboServiceHelper.merge(limbo, null);
LimboPlayer result2 = limboServiceHelper.merge(null, limbo);
LimboPlayer result3 = limboServiceHelper.merge(null, null);
// then
verifyZeroInteractions(limbo);
assertThat(result1, equalTo(limbo));
assertThat(result2, equalTo(limbo));
assertThat(result3, nullValue());
}
}
@@ -1,5 +1,7 @@
package fr.xephi.authme.data.limbo;
import ch.jalu.injector.testing.DelayedInjectionRunner;
import ch.jalu.injector.testing.InjectDelayed;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.permission.PermissionsManager;
@@ -13,10 +15,8 @@ import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Map;
@@ -34,14 +34,17 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
/**
* Test for {@link LimboService}.
* Test for {@link LimboService}, and {@link LimboServiceHelper}.
*/
@RunWith(MockitoJUnitRunner.class)
@RunWith(DelayedInjectionRunner.class)
public class LimboServiceTest {
@InjectMocks
@InjectDelayed
private LimboService limboService;
@InjectDelayed
private LimboServiceHelper limboServiceHelper;
@Mock
private SpawnLoader spawnLoader;