Unit test refactor

This commit is contained in:
nossr50 2023-12-22 11:37:17 -08:00
parent 7973ccc848
commit 5f63faf65d
6 changed files with 179 additions and 47 deletions

View File

@ -211,8 +211,6 @@ public enum SuperAbilityType {
case SUPER_SHOTGUN -> Permissions.superShotgun(player); case SUPER_SHOTGUN -> Permissions.superShotgun(player);
case TREE_FELLER -> Permissions.treeFeller(player); case TREE_FELLER -> Permissions.treeFeller(player);
case TRIDENTS_SUPER_ABILITY -> Permissions.tridentsSuper(player); case TRIDENTS_SUPER_ABILITY -> Permissions.tridentsSuper(player);
default ->
throw new RuntimeException("Unhandled SuperAbilityType in getPermissions(), devs need to add definition for " + this + "!");
}; };
} }

View File

@ -52,7 +52,7 @@ public class SkillTools {
NON_CHILD_SKILLS = ImmutableList.copyOf(tempNonChildSkills); NON_CHILD_SKILLS = ImmutableList.copyOf(tempNonChildSkills);
} }
public SkillTools(@NotNull mcMMO pluginRef) { public SkillTools(@NotNull mcMMO pluginRef) throws InvalidSkillException {
this.pluginRef = pluginRef; this.pluginRef = pluginRef;
/* /*
@ -198,8 +198,7 @@ public class SkillTools {
case GIGA_DRILL_BREAKER -> PrimarySkillType.EXCAVATION; case GIGA_DRILL_BREAKER -> PrimarySkillType.EXCAVATION;
case SUPER_SHOTGUN -> PrimarySkillType.CROSSBOWS; case SUPER_SHOTGUN -> PrimarySkillType.CROSSBOWS;
case TRIDENTS_SUPER_ABILITY -> PrimarySkillType.TRIDENTS; case TRIDENTS_SUPER_ABILITY -> PrimarySkillType.TRIDENTS;
default -> case EXPLOSIVE_SHOT -> PrimarySkillType.ARCHERY;
throw new InvalidSkillException("No parent defined for super ability! " + superAbilityType.toString());
}; };
} }
@ -340,14 +339,10 @@ public class SkillTools {
// TODO: This is a little "hacky", we probably need to add something to distinguish child skills in the enum, or to use another enum for them // TODO: This is a little "hacky", we probably need to add something to distinguish child skills in the enum, or to use another enum for them
public static boolean isChildSkill(PrimarySkillType primarySkillType) { public static boolean isChildSkill(PrimarySkillType primarySkillType) {
switch (primarySkillType) { return switch (primarySkillType) {
case SALVAGE: case SALVAGE, SMELTING -> true;
case SMELTING: default -> false;
return true; };
default:
return false;
}
} }
/** /**
@ -412,34 +407,7 @@ public class SkillTools {
* @return true if the player has permissions, false otherwise * @return true if the player has permissions, false otherwise
*/ */
public boolean superAbilityPermissionCheck(SuperAbilityType superAbilityType, Player player) { public boolean superAbilityPermissionCheck(SuperAbilityType superAbilityType, Player player) {
switch (superAbilityType) { return superAbilityType.getPermissions(player);
case BERSERK:
return Permissions.berserk(player);
case BLAST_MINING:
return Permissions.remoteDetonation(player);
case GIGA_DRILL_BREAKER:
return Permissions.gigaDrillBreaker(player);
case GREEN_TERRA:
return Permissions.greenTerra(player);
case SERRATED_STRIKES:
return Permissions.serratedStrikes(player);
case SKULL_SPLITTER:
return Permissions.skullSplitter(player);
case SUPER_BREAKER:
return Permissions.superBreaker(player);
case TREE_FELLER:
return Permissions.treeFeller(player);
default:
return false;
}
} }
public @NotNull List<PrimarySkillType> getChildSkills() { public @NotNull List<PrimarySkillType> getChildSkills() {

View File

@ -0,0 +1,163 @@
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

@ -1,5 +1,6 @@
package com.gmail.nossr50; package com.gmail.nossr50;
import com.gmail.nossr50.api.exceptions.InvalidSkillException;
import com.gmail.nossr50.config.AdvancedConfig; import com.gmail.nossr50.config.AdvancedConfig;
import com.gmail.nossr50.config.ChatConfig; import com.gmail.nossr50.config.ChatConfig;
import com.gmail.nossr50.config.GeneralConfig; import com.gmail.nossr50.config.GeneralConfig;
@ -26,11 +27,11 @@ import org.mockito.MockedStatic;
import org.mockito.Mockito; import org.mockito.Mockito;
import java.util.UUID; import java.util.UUID;
import java.util.logging.Logger;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
public abstract class MMOTestEnvironment { public abstract class MMOTestEnvironment {
private final java.util.logging.Logger logger = java.util.logging.Logger.getLogger(MMOTestEnvironment.class.getName());
protected MockedStatic<mcMMO> mockedMcMMO; protected MockedStatic<mcMMO> mockedMcMMO;
protected MockedStatic<ChatConfig> mockedChatConfig; protected MockedStatic<ChatConfig> mockedChatConfig;
protected MockedStatic<ExperienceConfig> experienceConfig; protected MockedStatic<ExperienceConfig> experienceConfig;
@ -62,7 +63,7 @@ public abstract class MMOTestEnvironment {
protected ChunkManager chunkManager; protected ChunkManager chunkManager;
protected void mockBaseEnvironment() { protected void mockBaseEnvironment(Logger logger) throws InvalidSkillException {
mockedMcMMO = Mockito.mockStatic(mcMMO.class); mockedMcMMO = Mockito.mockStatic(mcMMO.class);
mcMMO.p = Mockito.mock(mcMMO.class); mcMMO.p = Mockito.mock(mcMMO.class);
Mockito.when(mcMMO.p.getLogger()).thenReturn(logger); Mockito.when(mcMMO.p.getLogger()).thenReturn(logger);

View File

@ -1,6 +1,7 @@
package com.gmail.nossr50.skills.tridents; package com.gmail.nossr50.skills.tridents;
import com.gmail.nossr50.MMOTestEnvironment; import com.gmail.nossr50.MMOTestEnvironment;
import com.gmail.nossr50.api.exceptions.InvalidSkillException;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -15,8 +16,8 @@ class TridentsTest extends MMOTestEnvironment {
TridentsManager tridentsManager; TridentsManager tridentsManager;
ItemStack trident; ItemStack trident;
@BeforeEach @BeforeEach
void setUp() { void setUp() throws InvalidSkillException {
mockBaseEnvironment(); mockBaseEnvironment(logger);
// setup player and player related mocks after everything else // setup player and player related mocks after everything else
this.player = Mockito.mock(Player.class); this.player = Mockito.mock(Player.class);

View File

@ -1,6 +1,7 @@
package com.gmail.nossr50.skills.woodcutting; package com.gmail.nossr50.skills.woodcutting;
import com.gmail.nossr50.MMOTestEnvironment; import com.gmail.nossr50.MMOTestEnvironment;
import com.gmail.nossr50.api.exceptions.InvalidSkillException;
import com.gmail.nossr50.config.experience.ExperienceConfig; import com.gmail.nossr50.config.experience.ExperienceConfig;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType; import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import com.gmail.nossr50.datatypes.skills.SubSkillType; import com.gmail.nossr50.datatypes.skills.SubSkillType;
@ -24,8 +25,8 @@ class WoodcuttingTest extends MMOTestEnvironment {
WoodcuttingManager woodcuttingManager; WoodcuttingManager woodcuttingManager;
@BeforeEach @BeforeEach
void setUp() { void setUp() throws InvalidSkillException {
mockBaseEnvironment(); mockBaseEnvironment(logger);
Mockito.when(rankConfig.getSubSkillUnlockLevel(SubSkillType.WOODCUTTING_HARVEST_LUMBER, 1)).thenReturn(1); Mockito.when(rankConfig.getSubSkillUnlockLevel(SubSkillType.WOODCUTTING_HARVEST_LUMBER, 1)).thenReturn(1);
// wire advanced config // wire advanced config