fix unit tests

This commit is contained in:
nossr50 2024-03-30 04:44:10 -07:00
parent fba6e8a961
commit ef3887a720
7 changed files with 41 additions and 350 deletions

View File

@ -55,22 +55,12 @@ public final class PartyManager {
* @return true if party is full and cannot be joined * @return true if party is full and cannot be joined
*/ */
public boolean isPartyFull(@NotNull Player player, @NotNull Party targetParty) { public boolean isPartyFull(@NotNull Player player, @NotNull Party targetParty) {
requireNonNull(player, "player cannot be null!");
requireNonNull(targetParty, "targetParty cannot be null!");
return !Permissions.partySizeBypass(player) && pluginRef.getGeneralConfig().getPartyMaxSize() >= 1 && targetParty.getOnlineMembers().size() >= pluginRef.getGeneralConfig().getPartyMaxSize(); return !Permissions.partySizeBypass(player) && pluginRef.getGeneralConfig().getPartyMaxSize() >= 1 && targetParty.getOnlineMembers().size() >= pluginRef.getGeneralConfig().getPartyMaxSize();
} }
public boolean areAllies(@NotNull Player firstPlayer, @NotNull Player secondPlayer) { public boolean areAllies(@NotNull Player firstPlayer, @NotNull Player secondPlayer) {
requireNonNull(firstPlayer, "firstPlayer cannot be null!"); //Profile is not loaded
requireNonNull(secondPlayer, "secondPlayer cannot be null!"); if (UserManager.getPlayer(firstPlayer) == null || UserManager.getPlayer(secondPlayer) == null) {
//Profile not loaded
if (UserManager.getPlayer(firstPlayer) == null) {
return false;
}
//Profile not loaded
if (UserManager.getPlayer(secondPlayer) == null) {
return false; return false;
} }
@ -110,7 +100,6 @@ public final class PartyManager {
} }
public @NotNull List<Player> getNearVisibleMembers(@NotNull McMMOPlayer mmoPlayer) { public @NotNull List<Player> getNearVisibleMembers(@NotNull McMMOPlayer mmoPlayer) {
requireNonNull(mmoPlayer, "mmoPlayer cannot be null!");
List<Player> nearMembers = new ArrayList<>(); List<Player> nearMembers = new ArrayList<>();
Party party = mmoPlayer.getParty(); Party party = mmoPlayer.getParty();
@ -137,7 +126,6 @@ public final class PartyManager {
* @return all the players in the player's party * @return all the players in the player's party
*/ */
public @NotNull LinkedHashMap<UUID, String> getAllMembers(@NotNull Player player) { public @NotNull LinkedHashMap<UUID, String> getAllMembers(@NotNull Player player) {
requireNonNull(player, "player cannot be null!");
Party party = getParty(player); Party party = getParty(player);
return party == null ? new LinkedHashMap<>() : party.getMembers(); return party == null ? new LinkedHashMap<>() : party.getMembers();
@ -150,7 +138,6 @@ public final class PartyManager {
* @return all online players in this party * @return all online players in this party
*/ */
public @NotNull List<Player> getOnlineMembers(@NotNull String partyName) { public @NotNull List<Player> getOnlineMembers(@NotNull String partyName) {
requireNonNull(partyName, "partyName cannot be null!");
return getOnlineMembers(getParty(partyName)); return getOnlineMembers(getParty(partyName));
} }
@ -161,7 +148,6 @@ public final class PartyManager {
* @return all online players in this party * @return all online players in this party
*/ */
public @NotNull List<Player> getOnlineMembers(@NotNull Player player) { public @NotNull List<Player> getOnlineMembers(@NotNull Player player) {
requireNonNull(player, "player cannot be null!");
return getOnlineMembers(getParty(player)); return getOnlineMembers(getParty(player));
} }
@ -176,7 +162,6 @@ public final class PartyManager {
* @return the existing party, null otherwise * @return the existing party, null otherwise
*/ */
public @Nullable Party getParty(@NotNull String partyName) { public @Nullable Party getParty(@NotNull String partyName) {
requireNonNull(partyName, "partyName cannot be null!");
for (Party party : parties) { for (Party party : parties) {
if (party.getName().equalsIgnoreCase(partyName)) { if (party.getName().equalsIgnoreCase(partyName)) {
return party; return party;
@ -194,7 +179,6 @@ public final class PartyManager {
*/ */
@Deprecated @Deprecated
public @Nullable Party getPlayerParty(@NotNull String playerName) { public @Nullable Party getPlayerParty(@NotNull String playerName) {
requireNonNull(playerName, "playerName cannot be null!");
for (Party party : parties) { for (Party party : parties) {
if (party.getMembers().containsValue(playerName)) { if (party.getMembers().containsValue(playerName)) {
return party; return party;
@ -211,8 +195,6 @@ public final class PartyManager {
* @return the existing party, null otherwise * @return the existing party, null otherwise
*/ */
public @Nullable Party getPlayerParty(@NotNull String playerName, @NotNull UUID uuid) { public @Nullable Party getPlayerParty(@NotNull String playerName, @NotNull UUID uuid) {
requireNonNull(playerName, "playerName cannot be null!");
requireNonNull(uuid, "uuid cannot be null!");
for (Party party : parties) { for (Party party : parties) {
LinkedHashMap<UUID, String> members = party.getMembers(); LinkedHashMap<UUID, String> members = party.getMembers();
if (members.containsKey(uuid) || members.containsValue(playerName)) { if (members.containsKey(uuid) || members.containsValue(playerName)) {
@ -236,8 +218,7 @@ public final class PartyManager {
* @return the existing party, null otherwise * @return the existing party, null otherwise
*/ */
public @Nullable Party getParty(@NotNull Player player) { public @Nullable Party getParty(@NotNull Player player) {
requireNonNull(player, "player cannot be null!"); //Profile is not loaded
//Profile not loaded
if (UserManager.getPlayer(player) == null) { if (UserManager.getPlayer(player) == null) {
return null; return null;
} }
@ -265,9 +246,6 @@ public final class PartyManager {
* @param party The party * @param party The party
*/ */
public void removeFromParty(@NotNull OfflinePlayer player, @NotNull Party party) { public void removeFromParty(@NotNull OfflinePlayer player, @NotNull Party party) {
requireNonNull(player, "player cannot be null!");
requireNonNull(party, "party cannot be null!");
LinkedHashMap<UUID, String> members = party.getMembers(); LinkedHashMap<UUID, String> members = party.getMembers();
String playerName = player.getName(); String playerName = player.getName();
@ -295,7 +273,6 @@ public final class PartyManager {
* @param mcMMOPlayer The player to remove * @param mcMMOPlayer The player to remove
*/ */
public void removeFromParty(@NotNull McMMOPlayer mcMMOPlayer) { public void removeFromParty(@NotNull McMMOPlayer mcMMOPlayer) {
requireNonNull(mcMMOPlayer, "mcMMOPlayer cannot be null!");
if (mcMMOPlayer.getParty() == null) { if (mcMMOPlayer.getParty() == null) {
return; return;
} }
@ -311,7 +288,6 @@ public final class PartyManager {
* @deprecated Use {@link #disbandParty(McMMOPlayer, Party)} * @deprecated Use {@link #disbandParty(McMMOPlayer, Party)}
*/ */
public void disbandParty(@NotNull Party party) { public void disbandParty(@NotNull Party party) {
requireNonNull(party, "party cannot be null!");
disbandParty(null, party); disbandParty(null, party);
} }
@ -322,7 +298,6 @@ public final class PartyManager {
* @param party The party to remove * @param party The party to remove
*/ */
public void disbandParty(@Nullable McMMOPlayer mcMMOPlayer, @NotNull Party party) { public void disbandParty(@Nullable McMMOPlayer mcMMOPlayer, @NotNull Party party) {
requireNonNull(party, "party cannot be null!");
//TODO: Potential issues with unloaded profile? //TODO: Potential issues with unloaded profile?
for (final Player member : party.getOnlineMembers()) { for (final Player member : party.getOnlineMembers()) {
//Profile not loaded //Profile not loaded
@ -352,9 +327,6 @@ public final class PartyManager {
* @param password The password for this party, null if there was no password * @param password The password for this party, null if there was no password
*/ */
public void createParty(@NotNull McMMOPlayer mcMMOPlayer, @NotNull String partyName, @Nullable String password) { public void createParty(@NotNull McMMOPlayer mcMMOPlayer, @NotNull String partyName, @Nullable String password) {
requireNonNull(mcMMOPlayer, "mcMMOPlayer cannot be null!");
requireNonNull(partyName, "partyName cannot be null!");
Player player = mcMMOPlayer.getPlayer(); Player player = mcMMOPlayer.getPlayer();
Party party = new Party(new PartyLeader(player.getUniqueId(), player.getName()), partyName.replace(".", ""), password); Party party = new Party(new PartyLeader(player.getUniqueId(), player.getName()), partyName.replace(".", ""), password);
@ -407,7 +379,6 @@ public final class PartyManager {
* @param mcMMOPlayer The player to add to the party * @param mcMMOPlayer The player to add to the party
*/ */
public void joinInvitedParty(@NotNull McMMOPlayer mcMMOPlayer) { public void joinInvitedParty(@NotNull McMMOPlayer mcMMOPlayer) {
requireNonNull(mcMMOPlayer, "mcMMOPlayer cannot be null!");
Party invite = mcMMOPlayer.getPartyInvite(); Party invite = mcMMOPlayer.getPartyInvite();
// Check if the party still exists, it might have been disbanded // Check if the party still exists, it might have been disbanded
@ -435,7 +406,6 @@ public final class PartyManager {
* @param mcMMOPlayer The player who accepts the alliance invite * @param mcMMOPlayer The player who accepts the alliance invite
*/ */
public void acceptAllianceInvite(@NotNull McMMOPlayer mcMMOPlayer) { public void acceptAllianceInvite(@NotNull McMMOPlayer mcMMOPlayer) {
requireNonNull(mcMMOPlayer, "mcMMOPlayer cannot be null!");
Party invite = mcMMOPlayer.getPartyAllianceInvite(); Party invite = mcMMOPlayer.getPartyAllianceInvite();
Player player = mcMMOPlayer.getPlayer(); Player player = mcMMOPlayer.getPlayer();
@ -456,9 +426,6 @@ public final class PartyManager {
} }
public void createAlliance(@NotNull Party firstParty, @NotNull Party secondParty) { public void createAlliance(@NotNull Party firstParty, @NotNull Party secondParty) {
requireNonNull(firstParty, "firstParty cannot be null!");
requireNonNull(secondParty, "secondParty cannot be null!");
firstParty.setAlly(secondParty); firstParty.setAlly(secondParty);
secondParty.setAlly(firstParty); secondParty.setAlly(firstParty);
@ -472,10 +439,6 @@ public final class PartyManager {
} }
public boolean disbandAlliance(@NotNull Player player, @NotNull Party firstParty, @NotNull Party secondParty) { public boolean disbandAlliance(@NotNull Player player, @NotNull Party firstParty, @NotNull Party secondParty) {
requireNonNull(player, "player cannot be null!");
requireNonNull(firstParty, "firstParty cannot be null!");
requireNonNull(secondParty, "secondParty cannot be null!");
if (!handlePartyChangeAllianceEvent(player, firstParty.getName(), secondParty.getName(), McMMOPartyAllianceChangeEvent.EventReason.DISBAND_ALLIANCE)) { if (!handlePartyChangeAllianceEvent(player, firstParty.getName(), secondParty.getName(), McMMOPartyAllianceChangeEvent.EventReason.DISBAND_ALLIANCE)) {
return false; return false;
} }
@ -485,8 +448,6 @@ public final class PartyManager {
} }
private void disbandAlliance(@NotNull Party firstParty, @NotNull Party secondParty) { private void disbandAlliance(@NotNull Party firstParty, @NotNull Party secondParty) {
requireNonNull(firstParty, "firstParty cannot be null!");
requireNonNull(secondParty, "secondParty cannot be null!");
firstParty.setAlly(null); firstParty.setAlly(null);
secondParty.setAlly(null); secondParty.setAlly(null);
@ -506,9 +467,6 @@ public final class PartyManager {
* @param party The party * @param party The party
*/ */
public void addToParty(@NotNull McMMOPlayer mcMMOPlayer, @NotNull Party party) { public void addToParty(@NotNull McMMOPlayer mcMMOPlayer, @NotNull Party party) {
requireNonNull(mcMMOPlayer, "mcMMOPlayer cannot be null!");
requireNonNull(party, "party cannot be null!");
Player player = mcMMOPlayer.getPlayer(); Player player = mcMMOPlayer.getPlayer();
String playerName = player.getName(); String playerName = player.getName();
@ -525,7 +483,6 @@ public final class PartyManager {
* @return the leader of the party * @return the leader of the party
*/ */
public @Nullable String getPartyLeaderName(@NotNull String partyName) { public @Nullable String getPartyLeaderName(@NotNull String partyName) {
requireNonNull(partyName, "partyName cannot be null!");
Party party = getParty(partyName); Party party = getParty(partyName);
return party == null ? null : party.getLeader().getPlayerName(); return party == null ? null : party.getLeader().getPlayerName();
@ -538,8 +495,6 @@ public final class PartyManager {
* @param party The party * @param party The party
*/ */
public void setPartyLeader(@NotNull UUID uuid, @NotNull Party party) { public void setPartyLeader(@NotNull UUID uuid, @NotNull Party party) {
requireNonNull(uuid, "uuid cannot be null!");
requireNonNull(party, "party cannot be null!");
OfflinePlayer player = pluginRef.getServer().getOfflinePlayer(uuid); OfflinePlayer player = pluginRef.getServer().getOfflinePlayer(uuid);
UUID leaderUniqueId = party.getLeader().getUniqueId(); UUID leaderUniqueId = party.getLeader().getUniqueId();
@ -564,7 +519,6 @@ public final class PartyManager {
* @return true if the player can invite * @return true if the player can invite
*/ */
public boolean canInvite(@NotNull McMMOPlayer mcMMOPlayer) { public boolean canInvite(@NotNull McMMOPlayer mcMMOPlayer) {
requireNonNull(mcMMOPlayer, "mcMMOPlayer cannot be null!");
Party party = mcMMOPlayer.getParty(); Party party = mcMMOPlayer.getParty();
return !party.isLocked() || party.getLeader().getUniqueId().equals(mcMMOPlayer.getPlayer().getUniqueId()); return !party.isLocked() || party.getLeader().getUniqueId().equals(mcMMOPlayer.getPlayer().getUniqueId());
@ -578,9 +532,6 @@ public final class PartyManager {
* @return true if a party with that name exists, false otherwise * @return true if a party with that name exists, false otherwise
*/ */
public boolean checkPartyExistence(@NotNull Player player, @NotNull String partyName) { public boolean checkPartyExistence(@NotNull Player player, @NotNull String partyName) {
requireNonNull(player, "player cannot be null!");
requireNonNull(partyName, "partyName cannot be null!");
if (getParty(partyName) == null) { if (getParty(partyName) == null) {
return false; return false;
} }
@ -597,9 +548,6 @@ public final class PartyManager {
* @return true if the party was joined successfully, false otherwise * @return true if the party was joined successfully, false otherwise
*/ */
public boolean changeOrJoinParty(@NotNull McMMOPlayer mmoPlayer, @NotNull String newPartyName) { public boolean changeOrJoinParty(@NotNull McMMOPlayer mmoPlayer, @NotNull String newPartyName) {
requireNonNull(mmoPlayer, "mmoPlayer cannot be null!");
requireNonNull(newPartyName, "newPartyName cannot be null!");
final Player player = mmoPlayer.getPlayer(); final Player player = mmoPlayer.getPlayer();
if (mmoPlayer.inParty()) { if (mmoPlayer.inParty()) {
@ -623,9 +571,6 @@ public final class PartyManager {
* @return true if they are in the same party, false otherwise * @return true if they are in the same party, false otherwise
*/ */
public boolean inSameParty(@NotNull Player firstPlayer, @NotNull Player secondPlayer) { public boolean inSameParty(@NotNull Player firstPlayer, @NotNull Player secondPlayer) {
requireNonNull(firstPlayer, "firstPlayer cannot be null!");
requireNonNull(secondPlayer, "secondPlayer cannot be null!");
//Profile not loaded //Profile not loaded
if (UserManager.getPlayer(firstPlayer) == null) { if (UserManager.getPlayer(firstPlayer) == null) {
return false; return false;

View File

@ -1,163 +0,0 @@
package com.gmail.nossr50;
import com.gmail.nossr50.config.AdvancedConfig;
import com.gmail.nossr50.config.ChatConfig;
import com.gmail.nossr50.config.GeneralConfig;
import com.gmail.nossr50.config.RankConfig;
import com.gmail.nossr50.config.experience.ExperienceConfig;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import com.gmail.nossr50.datatypes.skills.SubSkillType;
import com.gmail.nossr50.util.*;
import com.gmail.nossr50.util.blockmeta.ChunkManager;
import com.gmail.nossr50.util.player.UserManager;
import com.gmail.nossr50.util.skills.RankUtils;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import java.util.logging.Logger;
import static org.mockito.ArgumentMatchers.any;
public abstract class MMOMinimalPluginMock {
protected MockedStatic<mcMMO> mockedMcMMO;
protected MockedStatic<ChatConfig> mockedChatConfig;
protected MockedStatic<ExperienceConfig> experienceConfig;
protected MockedStatic<Permissions> mockedPermissions;
protected MockedStatic<RankUtils> mockedRankUtils;
protected MockedStatic<UserManager> mockedUserManager;
protected MockedStatic<Misc> mockedMisc;
protected MockedStatic<EventUtils> mockedEventUtils;
protected TransientEntityTracker transientEntityTracker;
protected AdvancedConfig advancedConfig;
protected GeneralConfig generalConfig;
protected RankConfig rankConfig;
protected Server server;
protected PluginManager pluginManager;
protected World world;
/* Mocks */
protected ChunkManager chunkManager;
protected void mockEnvironment(Logger logger) {
mockedMcMMO = Mockito.mockStatic(mcMMO.class);
mcMMO.p = Mockito.mock(mcMMO.class);
Mockito.when(mcMMO.p.getLogger()).thenReturn(logger);
// place store
chunkManager = Mockito.mock(ChunkManager.class);
Mockito.when(mcMMO.getPlaceStore()).thenReturn(chunkManager);
// shut off mod manager for woodcutting
Mockito.when(mcMMO.getModManager()).thenReturn(Mockito.mock(ModManager.class));
Mockito.when(mcMMO.getModManager().isCustomLog(any())).thenReturn(false);
// chat config
mockedChatConfig = Mockito.mockStatic(ChatConfig.class);
Mockito.when(ChatConfig.getInstance()).thenReturn(Mockito.mock(ChatConfig.class));
// general config
mockGeneralConfig();
// rank config
mockRankConfig();
// wire advanced config
mockAdvancedConfig();
// wire experience config
mockExperienceConfig();
this.transientEntityTracker = new TransientEntityTracker();
Mockito.when(mcMMO.getTransientEntityTracker()).thenReturn(transientEntityTracker);
mockPermissions();
mockedRankUtils = Mockito.mockStatic(RankUtils.class);
// wire server
this.server = Mockito.mock(Server.class);
Mockito.when(mcMMO.p.getServer()).thenReturn(server);
// wire plugin manager
this.pluginManager = Mockito.mock(PluginManager.class);
Mockito.when(server.getPluginManager()).thenReturn(pluginManager);
// wire world
this.world = Mockito.mock(World.class);
// wire Misc
this.mockedMisc = Mockito.mockStatic(Misc.class);
Mockito.when(Misc.getBlockCenter(any())).thenReturn(new Location(world, 0, 0, 0));
// wire user manager
this.mockedUserManager = Mockito.mockStatic(UserManager.class);
}
private void mockPermissions() {
mockedPermissions = Mockito.mockStatic(Permissions.class);
Mockito.when(Permissions.isSubSkillEnabled(any(Player.class), any(SubSkillType.class))).thenReturn(true);
Mockito.when(Permissions.canUseSubSkill(any(Player.class), any(SubSkillType.class))).thenReturn(true);
Mockito.when(Permissions.isSubSkillEnabled(any(Player.class), any(SubSkillType.class))).thenReturn(true);
Mockito.when(Permissions.canUseSubSkill(any(Player.class), any(SubSkillType.class))).thenReturn(true);
}
private void mockRankConfig() {
rankConfig = Mockito.mock(RankConfig.class);
}
private void mockAdvancedConfig() {
this.advancedConfig = Mockito.mock(AdvancedConfig.class);
Mockito.when(mcMMO.p.getAdvancedConfig()).thenReturn(advancedConfig);
}
private void mockGeneralConfig() {
generalConfig = Mockito.mock(GeneralConfig.class);
Mockito.when(generalConfig.getTreeFellerThreshold()).thenReturn(100);
Mockito.when(generalConfig.getDoubleDropsEnabled(PrimarySkillType.WOODCUTTING, Material.OAK_LOG)).thenReturn(true);
Mockito.when(generalConfig.getLocale()).thenReturn("en_US");
Mockito.when(mcMMO.p.getGeneralConfig()).thenReturn(generalConfig);
}
private void mockExperienceConfig() {
experienceConfig = Mockito.mockStatic(ExperienceConfig.class);
Mockito.when(ExperienceConfig.getInstance()).thenReturn(Mockito.mock(ExperienceConfig.class));
// Combat
Mockito.when(ExperienceConfig.getInstance().getCombatXP("Cow")).thenReturn(1D);
}
protected void cleanupBaseEnvironment() {
// Clean up resources here if needed.
if (mockedMcMMO != null) {
mockedMcMMO.close();
}
if (experienceConfig != null) {
experienceConfig.close();
}
if (mockedChatConfig != null) {
mockedChatConfig.close();
}
if (mockedPermissions != null) {
mockedPermissions.close();
}
if (mockedRankUtils != null) {
mockedRankUtils.close();
}
if (mockedUserManager != null) {
mockedUserManager.close();
}
if (mockedMisc != null) {
mockedMisc.close();
}
if (mockedEventUtils != null) {
mockedEventUtils.close();
}
}
}

View File

@ -6,6 +6,7 @@ import com.gmail.nossr50.config.ChatConfig;
import com.gmail.nossr50.config.GeneralConfig; import com.gmail.nossr50.config.GeneralConfig;
import com.gmail.nossr50.config.RankConfig; import com.gmail.nossr50.config.RankConfig;
import com.gmail.nossr50.config.experience.ExperienceConfig; import com.gmail.nossr50.config.experience.ExperienceConfig;
import com.gmail.nossr50.config.party.PartyConfig;
import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.datatypes.player.PlayerProfile; import com.gmail.nossr50.datatypes.player.PlayerProfile;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType; import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
@ -44,6 +45,7 @@ public abstract class MMOTestEnvironment {
protected MockedStatic<EventUtils> mockedEventUtils; protected MockedStatic<EventUtils> mockedEventUtils;
protected TransientEntityTracker transientEntityTracker; protected TransientEntityTracker transientEntityTracker;
protected AdvancedConfig advancedConfig; protected AdvancedConfig advancedConfig;
protected PartyConfig partyConfig;
protected GeneralConfig generalConfig; protected GeneralConfig generalConfig;
protected RankConfig rankConfig; protected RankConfig rankConfig;
protected SkillTools skillTools; protected SkillTools skillTools;
@ -85,6 +87,9 @@ public abstract class MMOTestEnvironment {
// general config // general config
mockGeneralConfig(); mockGeneralConfig();
// party config
mockPartyConfig();
// rank config // rank config
mockRankConfig(); mockRankConfig();
@ -166,6 +171,12 @@ public abstract class MMOTestEnvironment {
when(mcMMO.p.getGeneralConfig()).thenReturn(generalConfig); when(mcMMO.p.getGeneralConfig()).thenReturn(generalConfig);
} }
private void mockPartyConfig() {
partyConfig = Mockito.mock(PartyConfig.class);
when(partyConfig.isPartyEnabled()).thenReturn(false);
when(mcMMO.p.getPartyConfig()).thenReturn(partyConfig);
}
private void mockExperienceConfig() { private void mockExperienceConfig() {
experienceConfig = Mockito.mockStatic(ExperienceConfig.class); experienceConfig = Mockito.mockStatic(ExperienceConfig.class);

View File

@ -1,45 +1,46 @@
package com.gmail.nossr50.party; package com.gmail.nossr50.party;
import com.gmail.nossr50.MMOTestEnvironment;
import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
import org.bukkit.Server;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import java.util.UUID; import java.util.UUID;
import java.util.logging.Logger;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.when;
class PartyManagerTest { class PartyManagerTest extends MMOTestEnvironment {
private static final Logger logger = Logger.getLogger(PartyManagerTest.class.getName());
static mcMMO mockMcMMO; @BeforeEach
public void setUp() {
mockBaseEnvironment(logger);
@BeforeAll // currently unnecessary, but may be needed for future tests
public static void setup() { Mockito.when(partyConfig.isPartyEnabled()).thenReturn(true);
// create a static stub for LocaleLoader.class }
mockStatic(LocaleLoader.class);
when(LocaleLoader.getString(anyString())).thenReturn("");
mockMcMMO = mock(mcMMO.class); @AfterEach
final Server mockServer = mock(Server.class); public void tearDown() {
when(mockMcMMO.getServer()).thenReturn(mockServer); cleanupBaseEnvironment();
when(mockServer.getPluginManager()).thenReturn(mock(PluginManager.class));
// TODO: Add cleanup for static mock // disable parties in config for other tests
Mockito.when(partyConfig.isPartyEnabled()).thenReturn(false);
} }
@Test @Test
public void createPartyWithoutPasswordShouldSucceed() { public void createPartyWithoutPasswordShouldSucceed() {
// Given // Given
PartyManager partyManager = new PartyManager(mockMcMMO); PartyManager partyManager = new PartyManager(mcMMO.p);
String partyName = "TestParty"; String partyName = "TestParty";
// TODO: Update this with utils from the other dev branches in the future
Player player = mock(Player.class); Player player = mock(Player.class);
McMMOPlayer mmoPlayer = mock(McMMOPlayer.class); McMMOPlayer mmoPlayer = mock(McMMOPlayer.class);
when(mmoPlayer.getPlayer()).thenReturn(player); when(mmoPlayer.getPlayer()).thenReturn(player);
@ -52,11 +53,10 @@ class PartyManagerTest {
@Test @Test
public void createPartyWithPasswordShouldSucceed() { public void createPartyWithPasswordShouldSucceed() {
// Given // Given
PartyManager partyManager = new PartyManager(mockMcMMO); PartyManager partyManager = new PartyManager(mcMMO.p);
String partyName = "TestParty"; String partyName = "TestParty";
String partyPassword = "somePassword"; String partyPassword = "somePassword";
// TODO: Update this with utils from the other dev branches in the future
Player player = mock(Player.class); Player player = mock(Player.class);
McMMOPlayer mmoPlayer = mock(McMMOPlayer.class); McMMOPlayer mmoPlayer = mock(McMMOPlayer.class);
when(mmoPlayer.getPlayer()).thenReturn(player); when(mmoPlayer.getPlayer()).thenReturn(player);
@ -69,29 +69,28 @@ class PartyManagerTest {
@Test @Test
public void createPartyWithoutNameShouldFail() { public void createPartyWithoutNameShouldFail() {
// Given // Given
PartyManager partyManager = new PartyManager(mockMcMMO); PartyManager partyManager = new PartyManager(mcMMO.p);
String partyPassword = "somePassword"; String partyPassword = "somePassword";
// TODO: Update this with utils from the other dev branches in the future
Player player = mock(Player.class); Player player = mock(Player.class);
McMMOPlayer mmoPlayer = mock(McMMOPlayer.class); McMMOPlayer mmoPlayer = mock(McMMOPlayer.class);
when(mmoPlayer.getPlayer()).thenReturn(player); when(mmoPlayer.getPlayer()).thenReturn(player);
when(player.getUniqueId()).thenReturn(new UUID(0, 0)); when(player.getUniqueId()).thenReturn(new UUID(0, 0));
// When & Then // When & Then
assertThrows(NullPointerException.class, assertThrows(IllegalArgumentException.class,
() -> partyManager.createParty(mmoPlayer, null, partyPassword)); () -> partyManager.createParty(mmoPlayer, null, partyPassword));
} }
@Test @Test
public void createPartyWithoutPlayerShouldFail() { public void createPartyWithoutPlayerShouldFail() {
// Given // Given
PartyManager partyManager = new PartyManager(mockMcMMO); PartyManager partyManager = new PartyManager(mcMMO.p);
String partyName = "TestParty"; String partyName = "TestParty";
String partyPassword = "somePassword"; String partyPassword = "somePassword";
// When & Then // When & Then
assertThrows(NullPointerException.class, assertThrows(IllegalArgumentException.class,
() -> partyManager.createParty(null, partyName, partyPassword)); () -> partyManager.createParty(null, partyName, partyPassword));
} }

View File

@ -16,7 +16,6 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
class ProbabilityUtilTest { class ProbabilityUtilTest {
mcMMO mmoInstance; mcMMO mmoInstance;
AdvancedConfig advancedConfig; AdvancedConfig advancedConfig;

View File

@ -1,100 +0,0 @@
//package com.gmail.nossr50.util.random;
//
//import com.gmail.nossr50.datatypes.player.McMMOPlayer;
//import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
//import com.gmail.nossr50.datatypes.skills.SubSkillType;
//import com.gmail.nossr50.util.Permissions;
//import com.gmail.nossr50.util.player.UserManager;
//import org.bukkit.entity.Player;
//import org.jetbrains.annotations.NotNull;
//import org.junit.Assert;
//import org.junit.Before;
//import org.junit.Test;
//import org.junit.runner.RunWith;
//import org.mockito.Mockito;
//import org.powermock.api.mockito.PowerMockito;
//import org.powermock.core.classloader.annotations.PrepareForTest;
//import org.powermock.modules.junit4.PowerMockRunner;
//
//import static org.mockito.Mockito.mock;
//
////TODO: Rewrite the entire com.gmail.nossr50.util.random package, it was written in haste and it disgusts me
////TODO: Add more tests for the other types of random dice rolls
//@RunWith(PowerMockRunner.class)
//@PrepareForTest({RandomChanceUtil.class, UserManager.class, PrimarySkillType.class})
//public class RandomChanceTest {
//
// private Player luckyPlayer;
// private McMMOPlayer mmoPlayerLucky;
//
// private Player normalPlayer;
// private McMMOPlayer mmoPlayerNormal;
//
// private SubSkillType subSkillType;
// private PrimarySkillType primarySkillType;
//
// private final String testASCIIHeader = "---- mcMMO Tests ----";
//
// @Before
// public void setUpMock() {
// primarySkillType = PrimarySkillType.MINING;
// subSkillType = SubSkillType.MINING_MOTHER_LODE;
//
// //TODO: Likely needs to be changed per skill if more tests were added
// PowerMockito.stub(PowerMockito.method(RandomChanceUtil.class, "getMaximumProbability", subSkillType.getClass())).toReturn(10.0D);
// PowerMockito.stub(PowerMockito.method(RandomChanceUtil.class, "getMaxBonusLevelCap", subSkillType.getClass())).toReturn(10000D);
//
// normalPlayer = mock(Player.class);
// luckyPlayer = mock(Player.class);
//
// mmoPlayerNormal = mock(McMMOPlayer.class);
// mmoPlayerLucky = mock(McMMOPlayer.class);
//
// PowerMockito.mockStatic(UserManager.class);
// Mockito.when(UserManager.getPlayer(normalPlayer)).thenReturn(mmoPlayerNormal);
// Mockito.when(UserManager.getPlayer(luckyPlayer)).thenReturn(mmoPlayerLucky);
//
// Mockito.when(mmoPlayerNormal.getPlayer()).thenReturn(normalPlayer);
// Mockito.when(mmoPlayerLucky.getPlayer()).thenReturn(luckyPlayer);
//
// //Lucky player has the lucky permission
// //Normal player doesn't have any lucky permission
// Mockito.when(Permissions.lucky(luckyPlayer, primarySkillType)).thenReturn(true);
// Mockito.when(Permissions.lucky(normalPlayer, primarySkillType)).thenReturn(false);
//
// Mockito.when(mmoPlayerNormal.getSkillLevel(primarySkillType)).thenReturn(2150);
// Mockito.when(mmoPlayerLucky.getSkillLevel(primarySkillType)).thenReturn(2150);
// }
//
// @Test
// public void testLuckyChance() {
// System.out.println(testASCIIHeader);
// System.out.println("Testing success odds to fall within expected values...");
// assertEquals(2.15D, getSuccessChance(mmoPlayerNormal),0.00D);
// assertEquals(2.15D * RandomChanceUtil.LUCKY_MODIFIER, getSuccessChance(mmoPlayerLucky),0.00D);
// }
//
//// @Test
//// public void testNeverFailsSuccessLuckyPlayer() {
//// System.out.println(testASCIIHeader);
//// System.out.println("Test - Lucky Player with 80% base success should never fail (10,000 iterations)");
//// for(int x = 0; x < 10000; x++) {
//// Assert.assertTrue(RandomChanceUtil.checkRandomChanceExecutionSuccess(luckyPlayer, SubSkillType.HERBALISM_GREEN_THUMB, true));
//// if(x == 10000-1)
//// System.out.println("They never failed!");
//// }
//// }
//
//
//
//
//
// private double getSuccessChance(@NotNull McMMOPlayer mmoPlayer) {
// RandomChanceSkill randomChanceSkill = new RandomChanceSkill(mmoPlayer.getPlayer(), subSkillType, true);
// return RandomChanceUtil.calculateChanceOfSuccess(randomChanceSkill);
// }
//
// private void assertEquals(double expected, double actual, double delta) {
// Assert.assertEquals(expected, actual, delta);
// }
//}

View File

@ -7,7 +7,7 @@ import org.junit.jupiter.api.Test;
/** /**
* This Unit Test checks if Adventure was set up correctly and works as expected. * This Unit Test checks if Adventure was set up correctly and works as expected.
* Normally we can rely on this to be the case. However sometimes our dependencies * Normally, we can rely on this to be the case. However sometimes our dependencies
* lack so far behind that things stop working correctly. * lack so far behind that things stop working correctly.
* This test ensures that basic functionality is guaranteed to work as we would expect. * This test ensures that basic functionality is guaranteed to work as we would expect.
* *