Also avoid issues in this test by mocking instead of implementing an ever changing interface

This commit is contained in:
nossr50 2021-04-23 10:34:00 -07:00
parent 95c291d630
commit 9e7bb12dc3

View File

@ -13,6 +13,7 @@ import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.Statistic; import org.bukkit.Statistic;
import org.bukkit.block.Block;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -21,6 +22,7 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import java.io.*; import java.io.*;
import java.net.URI; import java.net.URI;
@ -34,6 +36,7 @@ import java.util.logging.LogRecord;
import java.util.logging.Logger; import java.util.logging.Logger;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
//This class uses JUnit5/Jupiter //This class uses JUnit5/Jupiter
public class FlatFileDatabaseManagerTest { public class FlatFileDatabaseManagerTest {
@ -395,16 +398,16 @@ public class FlatFileDatabaseManagerTest {
String playerName = "nossr50"; String playerName = "nossr50";
UUID uuid = UUID.fromString("588fe472-1c82-4c4e-9aa1-7eefccb277e3"); UUID uuid = UUID.fromString("588fe472-1c82-4c4e-9aa1-7eefccb277e3");
TestOfflinePlayer player = new TestOfflinePlayer(playerName, uuid); Player player = initMockPlayer(playerName, uuid);
PlayerProfile profile1 = db.loadPlayerProfile(player); PlayerProfile profile1 = db.loadPlayerProfile(player);
testHealthyDataProfileValues(playerName, uuid, profile1); testHealthyDataProfileValues(playerName, uuid, profile1);
String updatedName = "updatedName"; String updatedName = "updatedName";
TestOfflinePlayer updatedNamePlayer = new TestOfflinePlayer(updatedName, uuid); Player updatedNamePlayer = initMockPlayer(updatedName, uuid);
PlayerProfile updatedNameProfile = db.loadPlayerProfile(updatedNamePlayer); PlayerProfile updatedNameProfile = db.loadPlayerProfile(updatedNamePlayer);
testHealthyDataProfileValues(updatedName, uuid, updatedNameProfile); testHealthyDataProfileValues(updatedName, uuid, updatedNameProfile);
TestOfflinePlayer shouldNotExist = new TestOfflinePlayer("doesntexist", new UUID(0, 1)); Player shouldNotExist = initMockPlayer("doesntexist", new UUID(0, 1));
PlayerProfile profile3 = db.loadPlayerProfile(shouldNotExist); PlayerProfile profile3 = db.loadPlayerProfile(shouldNotExist);
assertFalse(profile3.isLoaded()); assertFalse(profile3.isLoaded());
} }
@ -833,180 +836,12 @@ public class FlatFileDatabaseManagerTest {
assertTrue(dataFlags.contains(flag)); assertTrue(dataFlags.contains(flag));
} }
private class TestOfflinePlayer implements OfflinePlayer { @NotNull
private Player initMockPlayer(@NotNull String name, @NotNull UUID uuid) {
private final @NotNull String name; Player mockPlayer = mock(Player.class);
private final @NotNull UUID uuid; Mockito.when(mockPlayer.getName()).thenReturn(name);
Mockito.when(mockPlayer.getUniqueId()).thenReturn(uuid);
private TestOfflinePlayer(@NotNull String name, @NotNull UUID uuid) { return mockPlayer;
this.name = name;
this.uuid = uuid;
}
@Override
public boolean isOnline() {
return false;
}
@Nullable
@Override
public String getName() {
return name;
}
@NotNull
@Override
public UUID getUniqueId() {
return uuid;
}
@Override
public boolean isBanned() {
return false;
}
@Override
public boolean isWhitelisted() {
return false;
}
@Override
public void setWhitelisted(boolean value) {
}
@Nullable
@Override
public Player getPlayer() {
return null;
}
@Override
public long getFirstPlayed() {
return 0;
}
@Override
public long getLastPlayed() {
return 0;
}
@Override
public boolean hasPlayedBefore() {
return false;
}
@Nullable
@Override
public Location getBedSpawnLocation() {
return null;
}
@Override
public void incrementStatistic(@NotNull Statistic statistic) throws IllegalArgumentException {
}
@Override
public void decrementStatistic(@NotNull Statistic statistic) throws IllegalArgumentException {
}
@Override
public void incrementStatistic(@NotNull Statistic statistic, int amount) throws IllegalArgumentException {
}
@Override
public void decrementStatistic(@NotNull Statistic statistic, int amount) throws IllegalArgumentException {
}
@Override
public void setStatistic(@NotNull Statistic statistic, int newValue) throws IllegalArgumentException {
}
@Override
public int getStatistic(@NotNull Statistic statistic) throws IllegalArgumentException {
return 0;
}
@Override
public void incrementStatistic(@NotNull Statistic statistic, @NotNull Material material) throws IllegalArgumentException {
}
@Override
public void decrementStatistic(@NotNull Statistic statistic, @NotNull Material material) throws IllegalArgumentException {
}
@Override
public int getStatistic(@NotNull Statistic statistic, @NotNull Material material) throws IllegalArgumentException {
return 0;
}
@Override
public void incrementStatistic(@NotNull Statistic statistic, @NotNull Material material, int amount) throws IllegalArgumentException {
}
@Override
public void decrementStatistic(@NotNull Statistic statistic, @NotNull Material material, int amount) throws IllegalArgumentException {
}
@Override
public void setStatistic(@NotNull Statistic statistic, @NotNull Material material, int newValue) throws IllegalArgumentException {
}
@Override
public void incrementStatistic(@NotNull Statistic statistic, @NotNull EntityType entityType) throws IllegalArgumentException {
}
@Override
public void decrementStatistic(@NotNull Statistic statistic, @NotNull EntityType entityType) throws IllegalArgumentException {
}
@Override
public int getStatistic(@NotNull Statistic statistic, @NotNull EntityType entityType) throws IllegalArgumentException {
return 0;
}
@Override
public void incrementStatistic(@NotNull Statistic statistic, @NotNull EntityType entityType, int amount) throws IllegalArgumentException {
}
@Override
public void decrementStatistic(@NotNull Statistic statistic, @NotNull EntityType entityType, int amount) {
}
@Override
public void setStatistic(@NotNull Statistic statistic, @NotNull EntityType entityType, int newValue) {
}
@NotNull
@Override
public Map<String, Object> serialize() {
return null;
}
@Override
public boolean isOp() {
return false;
}
@Override
public void setOp(boolean value) {
}
} }
private static class DebugFilter implements Filter { private static class DebugFilter implements Filter {