#1113 Handle LimboPlayers within LimboService (remove LimboCache) (work in progress)
- Delete LimboCache and LimboPlayerStorage: LimboService now handles all LimboPlayer actions - Revoke player rights when creating a LimboPlayer, within the LimboService - Various fixes and improvements
This commit is contained in:
@@ -1,215 +0,0 @@
|
||||
package fr.xephi.authme.data.limbo;
|
||||
|
||||
import fr.xephi.authme.ReflectionTestUtils;
|
||||
import fr.xephi.authme.permission.PermissionsManager;
|
||||
import fr.xephi.authme.settings.SpawnLoader;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.hamcrest.Matchers.aMapWithSize;
|
||||
import static org.hamcrest.Matchers.anEmptyMap;
|
||||
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.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
|
||||
/**
|
||||
* Test for {@link LimboCache}.
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class LimboCacheTest {
|
||||
|
||||
@InjectMocks
|
||||
private LimboCache limboCache;
|
||||
|
||||
@Mock
|
||||
private PermissionsManager permissionsManager;
|
||||
|
||||
@Mock
|
||||
private SpawnLoader spawnLoader;
|
||||
|
||||
@Mock
|
||||
private LimboPlayerStorage limboPlayerStorage;
|
||||
|
||||
@Test
|
||||
public void shouldAddPlayerData() {
|
||||
// given
|
||||
Player player = mock(Player.class);
|
||||
String name = "Bobby";
|
||||
given(player.getName()).willReturn(name);
|
||||
Location location = mock(Location.class);
|
||||
given(spawnLoader.getPlayerLocationOrSpawn(player)).willReturn(location);
|
||||
given(player.isOp()).willReturn(true);
|
||||
float walkSpeed = 2.1f;
|
||||
given(player.getWalkSpeed()).willReturn(walkSpeed);
|
||||
given(player.getAllowFlight()).willReturn(true);
|
||||
float flySpeed = 3.0f;
|
||||
given(player.getFlySpeed()).willReturn(flySpeed);
|
||||
given(permissionsManager.hasGroupSupport()).willReturn(true);
|
||||
String group = "test-group";
|
||||
given(permissionsManager.getPrimaryGroup(player)).willReturn(group);
|
||||
given(limboPlayerStorage.hasData(player)).willReturn(false);
|
||||
|
||||
// when
|
||||
limboCache.addPlayerData(player);
|
||||
|
||||
// then
|
||||
LimboPlayer limboPlayer = limboCache.getPlayerData(name);
|
||||
assertThat(limboPlayer.getLocation(), equalTo(location));
|
||||
assertThat(limboPlayer.isOperator(), equalTo(true));
|
||||
assertThat(limboPlayer.getWalkSpeed(), equalTo(walkSpeed));
|
||||
assertThat(limboPlayer.isCanFly(), equalTo(true));
|
||||
assertThat(limboPlayer.getFlySpeed(), equalTo(flySpeed));
|
||||
assertThat(limboPlayer.getGroup(), equalTo(group));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetPlayerDataFromDisk() {
|
||||
// given
|
||||
String name = "player01";
|
||||
Player player = mock(Player.class);
|
||||
given(player.getName()).willReturn(name);
|
||||
given(limboPlayerStorage.hasData(player)).willReturn(true);
|
||||
LimboPlayer limboPlayer = mock(LimboPlayer.class);
|
||||
given(limboPlayerStorage.readData(player)).willReturn(limboPlayer);
|
||||
float walkSpeed = 2.4f;
|
||||
given(limboPlayer.getWalkSpeed()).willReturn(walkSpeed);
|
||||
given(limboPlayer.isCanFly()).willReturn(true);
|
||||
float flySpeed = 1.0f;
|
||||
given(limboPlayer.getFlySpeed()).willReturn(flySpeed);
|
||||
String group = "primary-group";
|
||||
given(limboPlayer.getGroup()).willReturn(group);
|
||||
|
||||
// when
|
||||
limboCache.addPlayerData(player);
|
||||
|
||||
// then
|
||||
LimboPlayer result = limboCache.getPlayerData(name);
|
||||
assertThat(result.getWalkSpeed(), equalTo(walkSpeed));
|
||||
assertThat(result.isCanFly(), equalTo(true));
|
||||
assertThat(result.getFlySpeed(), equalTo(flySpeed));
|
||||
assertThat(result.getGroup(), equalTo(group));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRestorePlayerInfo() {
|
||||
// given
|
||||
String name = "Champ";
|
||||
Player player = mock(Player.class);
|
||||
given(player.getName()).willReturn(name);
|
||||
LimboPlayer limboPlayer = mock(LimboPlayer.class);
|
||||
given(limboPlayer.isOperator()).willReturn(true);
|
||||
float walkSpeed = 2.4f;
|
||||
given(limboPlayer.getWalkSpeed()).willReturn(walkSpeed);
|
||||
given(limboPlayer.isCanFly()).willReturn(true);
|
||||
float flySpeed = 1.0f;
|
||||
given(limboPlayer.getFlySpeed()).willReturn(flySpeed);
|
||||
getCache().put(name.toLowerCase(), limboPlayer);
|
||||
|
||||
// when
|
||||
limboCache.restoreData(player);
|
||||
|
||||
// then
|
||||
verify(player).setOp(true);
|
||||
verify(player).setWalkSpeed(walkSpeed);
|
||||
verify(player).setAllowFlight(true);
|
||||
verify(player).setFlySpeed(flySpeed);
|
||||
verify(limboPlayer).clearTasks();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldResetPlayerSpeed() {
|
||||
// given
|
||||
String name = "Champ";
|
||||
Player player = mock(Player.class);
|
||||
given(player.getName()).willReturn(name);
|
||||
LimboPlayer limboPlayer = mock(LimboPlayer.class);
|
||||
given(limboPlayer.isOperator()).willReturn(true);
|
||||
given(limboPlayer.getWalkSpeed()).willReturn(0f);
|
||||
given(limboPlayer.isCanFly()).willReturn(true);
|
||||
given(limboPlayer.getFlySpeed()).willReturn(0f);
|
||||
getCache().put(name.toLowerCase(), limboPlayer);
|
||||
|
||||
// when
|
||||
limboCache.restoreData(player);
|
||||
|
||||
// then
|
||||
verify(player).setWalkSpeed(LimboPlayer.DEFAULT_WALK_SPEED);
|
||||
verify(player).setFlySpeed(LimboPlayer.DEFAULT_FLY_SPEED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotInteractWithPlayerIfNoDataAvailable() {
|
||||
// given
|
||||
String name = "player";
|
||||
Player player = mock(Player.class);
|
||||
given(player.getName()).willReturn(name);
|
||||
|
||||
// when
|
||||
limboCache.restoreData(player);
|
||||
|
||||
// then
|
||||
verify(player).getName();
|
||||
verifyNoMoreInteractions(player);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRemoveAndClearTasks() {
|
||||
// given
|
||||
LimboPlayer limboPlayer = mock(LimboPlayer.class);
|
||||
String name = "abcdef";
|
||||
getCache().put(name, limboPlayer);
|
||||
Player player = mock(Player.class);
|
||||
given(player.getName()).willReturn(name);
|
||||
|
||||
// when
|
||||
limboCache.removeFromCache(player);
|
||||
|
||||
// then
|
||||
assertThat(getCache(), anEmptyMap());
|
||||
verify(limboPlayer).clearTasks();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDeleteFromCacheAndStorage() {
|
||||
// given
|
||||
LimboPlayer limboPlayer = mock(LimboPlayer.class);
|
||||
String name = "SomeName";
|
||||
getCache().put(name.toLowerCase(), limboPlayer);
|
||||
getCache().put("othername", mock(LimboPlayer.class));
|
||||
Player player = mock(Player.class);
|
||||
given(player.getName()).willReturn(name);
|
||||
|
||||
// when
|
||||
limboCache.deletePlayerData(player);
|
||||
|
||||
// then
|
||||
assertThat(getCache(), aMapWithSize(1));
|
||||
verify(limboPlayer).clearTasks();
|
||||
verify(limboPlayerStorage).removeData(player);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnIfHasData() {
|
||||
// given
|
||||
String name = "tester";
|
||||
getCache().put(name, mock(LimboPlayer.class));
|
||||
|
||||
// when / then
|
||||
assertThat(limboCache.hasPlayerData(name), equalTo(true));
|
||||
assertThat(limboCache.hasPlayerData("someone_else"), equalTo(false));
|
||||
}
|
||||
|
||||
private Map<String, LimboPlayer> getCache() {
|
||||
return ReflectionTestUtils.getFieldValue(LimboCache.class, limboCache, "cache");
|
||||
}
|
||||
}
|
||||
@@ -1,150 +0,0 @@
|
||||
package fr.xephi.authme.data.limbo;
|
||||
|
||||
import ch.jalu.injector.testing.BeforeInjecting;
|
||||
import ch.jalu.injector.testing.DelayedInjectionRunner;
|
||||
import ch.jalu.injector.testing.InjectDelayed;
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.initialization.DataFolder;
|
||||
import fr.xephi.authme.permission.PermissionsManager;
|
||||
import fr.xephi.authme.service.BukkitService;
|
||||
import fr.xephi.authme.settings.SpawnLoader;
|
||||
import fr.xephi.authme.util.FileUtils;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Test for {@link LimboPlayerStorage}.
|
||||
*/
|
||||
@RunWith(DelayedInjectionRunner.class)
|
||||
public class LimboPlayerStorageTest {
|
||||
|
||||
private static final UUID SAMPLE_UUID = UUID.nameUUIDFromBytes("PlayerDataStorageTest".getBytes());
|
||||
private static final String SOURCE_FOLDER = TestHelper.PROJECT_ROOT + "data/backup/";
|
||||
|
||||
@InjectDelayed
|
||||
private LimboPlayerStorage limboPlayerStorage;
|
||||
|
||||
@Mock
|
||||
private SpawnLoader spawnLoader;
|
||||
|
||||
@Mock
|
||||
private BukkitService bukkitService;
|
||||
|
||||
@Mock
|
||||
private PermissionsManager permissionsManager;
|
||||
|
||||
@DataFolder
|
||||
private File dataFolder;
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
|
||||
@BeforeInjecting
|
||||
public void copyTestFiles() throws IOException {
|
||||
dataFolder = temporaryFolder.newFolder();
|
||||
File playerFolder = new File(dataFolder, FileUtils.makePath("playerdata", SAMPLE_UUID.toString()));
|
||||
if (!playerFolder.mkdirs()) {
|
||||
throw new IllegalStateException("Cannot create '" + playerFolder.getAbsolutePath() + "'");
|
||||
}
|
||||
Files.copy(TestHelper.getJarPath(FileUtils.makePath(SOURCE_FOLDER, "sample-folder", "data.json")),
|
||||
new File(playerFolder, "data.json").toPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReadDataFromFile() {
|
||||
// given
|
||||
Player player = mock(Player.class);
|
||||
given(player.getUniqueId()).willReturn(SAMPLE_UUID);
|
||||
World world = mock(World.class);
|
||||
given(bukkitService.getWorld("nether")).willReturn(world);
|
||||
|
||||
// when
|
||||
LimboPlayer data = limboPlayerStorage.readData(player);
|
||||
|
||||
// then
|
||||
assertThat(data, not(nullValue()));
|
||||
assertThat(data.isOperator(), equalTo(true));
|
||||
assertThat(data.isCanFly(), equalTo(true));
|
||||
assertThat(data.getWalkSpeed(), equalTo(0.2f));
|
||||
assertThat(data.getFlySpeed(), equalTo(0.1f));
|
||||
assertThat(data.getGroup(), equalTo("players"));
|
||||
Location location = data.getLocation();
|
||||
assertThat(location.getX(), equalTo(-113.219));
|
||||
assertThat(location.getY(), equalTo(72.0));
|
||||
assertThat(location.getZ(), equalTo(130.637));
|
||||
assertThat(location.getWorld(), equalTo(world));
|
||||
assertThat(location.getPitch(), equalTo(24.15f));
|
||||
assertThat(location.getYaw(), equalTo(-292.484f));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnNullForUnavailablePlayer() {
|
||||
// given
|
||||
Player player = mock(Player.class);
|
||||
given(player.getUniqueId()).willReturn(UUID.nameUUIDFromBytes("other-player".getBytes()));
|
||||
|
||||
// when
|
||||
LimboPlayer data = limboPlayerStorage.readData(player);
|
||||
|
||||
// then
|
||||
assertThat(data, nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnIfHasData() {
|
||||
// given
|
||||
Player player1 = mock(Player.class);
|
||||
given(player1.getUniqueId()).willReturn(SAMPLE_UUID);
|
||||
Player player2 = mock(Player.class);
|
||||
given(player2.getUniqueId()).willReturn(UUID.nameUUIDFromBytes("not-stored".getBytes()));
|
||||
|
||||
// when / then
|
||||
assertThat(limboPlayerStorage.hasData(player1), equalTo(true));
|
||||
assertThat(limboPlayerStorage.hasData(player2), equalTo(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSavePlayerData() {
|
||||
// given
|
||||
Player player = mock(Player.class);
|
||||
UUID uuid = UUID.nameUUIDFromBytes("New player".getBytes());
|
||||
given(player.getUniqueId()).willReturn(uuid);
|
||||
given(permissionsManager.getPrimaryGroup(player)).willReturn("primary-grp");
|
||||
given(player.isOp()).willReturn(true);
|
||||
given(player.getWalkSpeed()).willReturn(1.2f);
|
||||
given(player.getFlySpeed()).willReturn(0.8f);
|
||||
given(player.getAllowFlight()).willReturn(true);
|
||||
|
||||
World world = mock(World.class);
|
||||
given(world.getName()).willReturn("player-world");
|
||||
Location location = new Location(world, 0.2, 102.25, -89.28, 3.02f, 90.13f);
|
||||
given(spawnLoader.getPlayerLocationOrSpawn(player)).willReturn(location);
|
||||
|
||||
// when
|
||||
limboPlayerStorage.saveData(player);
|
||||
|
||||
// then
|
||||
File playerFile = new File(dataFolder, FileUtils.makePath("playerdata", uuid.toString(), "data.json"));
|
||||
assertThat(playerFile.exists(), equalTo(true));
|
||||
// TODO ljacqu 20160711: Check contents of file
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user