ExcavationTest.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package com.gmail.nossr50.skills.excavation;
  2. import static org.mockito.ArgumentMatchers.any;
  3. import static org.mockito.ArgumentMatchers.eq;
  4. import static org.mockito.Mockito.atLeastOnce;
  5. import static org.mockito.Mockito.doReturn;
  6. import static org.mockito.Mockito.never;
  7. import static org.mockito.Mockito.verify;
  8. import static org.mockito.Mockito.when;
  9. import com.gmail.nossr50.MMOTestEnvironment;
  10. import com.gmail.nossr50.api.exceptions.InvalidSkillException;
  11. import com.gmail.nossr50.datatypes.player.McMMOPlayer;
  12. import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
  13. import com.gmail.nossr50.datatypes.skills.SubSkillType;
  14. import com.gmail.nossr50.datatypes.treasure.ExcavationTreasure;
  15. import com.gmail.nossr50.util.TestPlayerMock;
  16. import com.gmail.nossr50.util.skills.RankUtils;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import org.bukkit.Location;
  20. import org.bukkit.Material;
  21. import org.bukkit.block.Block;
  22. import org.bukkit.entity.Player;
  23. import org.bukkit.inventory.ItemStack;
  24. import org.junit.jupiter.api.AfterEach;
  25. import org.junit.jupiter.api.BeforeEach;
  26. import org.junit.jupiter.api.Test;
  27. import org.mockito.Mockito;
  28. class ExcavationTest extends MMOTestEnvironment {
  29. private static final java.util.logging.Logger logger = java.util.logging.Logger.getLogger(
  30. ExcavationTest.class.getName());
  31. @BeforeEach
  32. void setUp() throws InvalidSkillException {
  33. mockBaseEnvironment(logger);
  34. when(rankConfig.getSubSkillUnlockLevel(SubSkillType.EXCAVATION_ARCHAEOLOGY, 1)).thenReturn(
  35. 1);
  36. when(rankConfig.getSubSkillUnlockLevel(SubSkillType.EXCAVATION_GIGA_DRILL_BREAKER,
  37. 1)).thenReturn(1);
  38. // wire advanced config
  39. when(RankUtils.getRankUnlockLevel(SubSkillType.EXCAVATION_ARCHAEOLOGY, 1)).thenReturn(
  40. 1); // needed?
  41. when(RankUtils.getRankUnlockLevel(SubSkillType.EXCAVATION_GIGA_DRILL_BREAKER,
  42. 1)).thenReturn(1); // needed?
  43. when(RankUtils.hasReachedRank(eq(1), any(Player.class),
  44. eq(SubSkillType.EXCAVATION_ARCHAEOLOGY))).thenReturn(true);
  45. when(RankUtils.hasReachedRank(eq(1), any(Player.class),
  46. eq(SubSkillType.EXCAVATION_GIGA_DRILL_BREAKER))).thenReturn(true);
  47. }
  48. @AfterEach
  49. void tearDown() {
  50. cleanUpStaticMocks();
  51. }
  52. @Test
  53. void excavationShouldHaveTreasureDrops() {
  54. final TestPlayerMock testPlayerMock = mockPlayer();
  55. final McMMOPlayer mmoPlayer = testPlayerMock.mmoPlayer();
  56. when(testPlayerMock.playerInventory().getItemInMainHand())
  57. .thenReturn(new ItemStack(Material.DIAMOND_SHOVEL));
  58. mmoPlayer.modifySkill(PrimarySkillType.EXCAVATION, 1000);
  59. // Wire block
  60. Block block = Mockito.mock(Block.class);
  61. when(block.getType()).thenReturn(Material.SAND);
  62. when(block.getDrops(any())).thenReturn(null);
  63. ExcavationManager excavationManager = Mockito.spy(new ExcavationManager(mmoPlayer));
  64. doReturn(getGuaranteedTreasureDrops()).when(excavationManager).getTreasures(block);
  65. excavationManager.excavationBlockCheck(block);
  66. // verify ExcavationManager.processExcavationBonusesOnBlock was called
  67. verify(excavationManager, atLeastOnce()).processExcavationBonusesOnBlock(
  68. any(ExcavationTreasure.class), any(Location.class));
  69. }
  70. @Test
  71. void excavationShouldNotDropTreasure() {
  72. final TestPlayerMock testPlayerMock = mockPlayer();
  73. final McMMOPlayer mmoPlayer = testPlayerMock.mmoPlayer();
  74. when(testPlayerMock.playerInventory().getItemInMainHand())
  75. .thenReturn(new ItemStack(Material.DIAMOND_SHOVEL));
  76. mmoPlayer.modifySkill(PrimarySkillType.EXCAVATION, 1000);
  77. // Wire block
  78. Block block = Mockito.mock(Block.class);
  79. when(block.getType()).thenReturn(Material.SAND);
  80. when(block.getDrops(any())).thenReturn(null);
  81. ExcavationManager excavationManager = Mockito.spy(new ExcavationManager(mmoPlayer));
  82. doReturn(getImpossibleTreasureDrops()).when(excavationManager).getTreasures(block);
  83. excavationManager.excavationBlockCheck(block);
  84. // verify ExcavationManager.processExcavationBonusesOnBlock was called
  85. verify(excavationManager, never()).processExcavationBonusesOnBlock(
  86. any(ExcavationTreasure.class),
  87. any(Location.class));
  88. }
  89. private List<ExcavationTreasure> getGuaranteedTreasureDrops() {
  90. List<ExcavationTreasure> treasures = new ArrayList<>();
  91. treasures.add(new ExcavationTreasure(new ItemStack(Material.CAKE), 1, 100, 1));
  92. return treasures;
  93. }
  94. private List<ExcavationTreasure> getImpossibleTreasureDrops() {
  95. List<ExcavationTreasure> treasures = new ArrayList<>();
  96. treasures.add(new ExcavationTreasure(new ItemStack(Material.CAKE), 1, 0, 1));
  97. return treasures;
  98. }
  99. }