From a6d885e0bb79527d97f988b2e12f474c60660173 Mon Sep 17 00:00:00 2001 From: ljacqu Date: Sun, 10 Jul 2016 23:00:58 +0200 Subject: [PATCH] Write tests for PlayerDataStorage --- .../cache/backup/PlayerDataStorage.java | 2 +- .../cache/backup/PlayerDataStorageTest.java | 125 ++++++++++++++++++ .../cache/backup/sample-folder/data.json | 15 +++ 3 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 src/test/java/fr/xephi/authme/cache/backup/PlayerDataStorageTest.java create mode 100644 src/test/resources/fr/xephi/authme/cache/backup/sample-folder/data.json diff --git a/src/main/java/fr/xephi/authme/cache/backup/PlayerDataStorage.java b/src/main/java/fr/xephi/authme/cache/backup/PlayerDataStorage.java index e2a94da53..c4472b04f 100644 --- a/src/main/java/fr/xephi/authme/cache/backup/PlayerDataStorage.java +++ b/src/main/java/fr/xephi/authme/cache/backup/PlayerDataStorage.java @@ -74,7 +74,7 @@ public class PlayerDataStorage { String str = Files.toString(file, Charsets.UTF_8); return gson.fromJson(str, PlayerData.class); } catch (IOException e) { - ConsoleLogger.writeStackTrace(e); + ConsoleLogger.logException("Could not read player data on disk for '" + player.getName() + "'", e); return null; } } diff --git a/src/test/java/fr/xephi/authme/cache/backup/PlayerDataStorageTest.java b/src/test/java/fr/xephi/authme/cache/backup/PlayerDataStorageTest.java new file mode 100644 index 000000000..ec4b79dac --- /dev/null +++ b/src/test/java/fr/xephi/authme/cache/backup/PlayerDataStorageTest.java @@ -0,0 +1,125 @@ +package fr.xephi.authme.cache.backup; + +import fr.xephi.authme.TestHelper; +import fr.xephi.authme.cache.limbo.PlayerData; +import fr.xephi.authme.initialization.DataFolder; +import fr.xephi.authme.permission.PermissionsManager; +import fr.xephi.authme.runner.BeforeInjecting; +import fr.xephi.authme.runner.DelayedInjectionRunner; +import fr.xephi.authme.runner.InjectDelayed; +import fr.xephi.authme.settings.SpawnLoader; +import fr.xephi.authme.util.BukkitService; +import fr.xephi.authme.util.StringUtils; +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 PlayerDataStorage}. + */ +@RunWith(DelayedInjectionRunner.class) +public class PlayerDataStorageTest { + + private static final UUID SAMPLE_UUID = UUID.nameUUIDFromBytes("PlayerDataStorageTest".getBytes()); + private static final String SOURCE_FOLDER = TestHelper.PROJECT_ROOT + "cache/backup/"; + + @InjectDelayed + private PlayerDataStorage playerDataStorage; + + @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, StringUtils.makePath("playerdata", SAMPLE_UUID.toString())); + if (!playerFolder.mkdirs()) { + throw new IllegalStateException("Cannot create '" + playerFolder.getAbsolutePath() + "'"); + } + Files.copy(TestHelper.getJarPath(StringUtils.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 + PlayerData data = playerDataStorage.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 + PlayerData data = playerDataStorage.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(playerDataStorage.hasData(player1), equalTo(true)); + assertThat(playerDataStorage.hasData(player2), equalTo(false)); + } + +} diff --git a/src/test/resources/fr/xephi/authme/cache/backup/sample-folder/data.json b/src/test/resources/fr/xephi/authme/cache/backup/sample-folder/data.json new file mode 100644 index 000000000..ee1ae20ad --- /dev/null +++ b/src/test/resources/fr/xephi/authme/cache/backup/sample-folder/data.json @@ -0,0 +1,15 @@ +{ + "location": { + "world": "nether", + "x": -113.219, + "y": 72.0, + "z": 130.637, + "yaw": -292.484, + "pitch": 24.15 + }, + "operator": true, + "can-fly": true, + "walk-speed": 0.2, + "fly-speed": 0.1, + "group": "players" +}