Skip to content

Commit

Permalink
Also avoid issues in this test by mocking instead of implementing an …
Browse files Browse the repository at this point in the history
…ever changing interface
  • Loading branch information
nossr50 committed Apr 23, 2021
1 parent 95c291d commit 9e7bb12
Showing 1 changed file with 12 additions and 177 deletions.
Expand Up @@ -13,6 +13,7 @@
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.Statistic;
import org.bukkit.block.Block;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
Expand All @@ -21,6 +22,7 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.io.*;
import java.net.URI;
Expand All @@ -34,6 +36,7 @@
import java.util.logging.Logger;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;

//This class uses JUnit5/Jupiter
public class FlatFileDatabaseManagerTest {
Expand Down Expand Up @@ -395,16 +398,16 @@ public void testLoadByUUIDAndName() {
String playerName = "nossr50";
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);
testHealthyDataProfileValues(playerName, uuid, profile1);

String updatedName = "updatedName";
TestOfflinePlayer updatedNamePlayer = new TestOfflinePlayer(updatedName, uuid);
Player updatedNamePlayer = initMockPlayer(updatedName, uuid);
PlayerProfile updatedNameProfile = db.loadPlayerProfile(updatedNamePlayer);
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);
assertFalse(profile3.isLoaded());
}
Expand Down Expand Up @@ -833,180 +836,12 @@ private void overwriteDataAndCheckForFlag(@NotNull FlatFileDatabaseManager targe
assertTrue(dataFlags.contains(flag));
}

private class TestOfflinePlayer implements OfflinePlayer {

private final @NotNull String name;
private final @NotNull UUID uuid;

private TestOfflinePlayer(@NotNull String name, @NotNull UUID uuid) {
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) {

}
@NotNull
private Player initMockPlayer(@NotNull String name, @NotNull UUID uuid) {
Player mockPlayer = mock(Player.class);
Mockito.when(mockPlayer.getName()).thenReturn(name);
Mockito.when(mockPlayer.getUniqueId()).thenReturn(uuid);
return mockPlayer;
}

private static class DebugFilter implements Filter {
Expand Down

0 comments on commit 9e7bb12

Please sign in to comment.