|
| 1 | +/** |
| 2 | + * |
| 3 | + */ |
| 4 | +package us.tastybento.bskyblock.listeners.flags; |
| 5 | + |
| 6 | +import static org.mockito.Mockito.mock; |
| 7 | +import static org.mockito.Mockito.when; |
| 8 | + |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.Map; |
| 11 | + |
| 12 | +import org.bukkit.Bukkit; |
| 13 | +import org.bukkit.Chunk; |
| 14 | +import org.bukkit.Material; |
| 15 | +import org.bukkit.World; |
| 16 | +import org.bukkit.block.Block; |
| 17 | +import org.bukkit.event.world.ChunkLoadEvent; |
| 18 | +import org.bukkit.inventory.ItemFactory; |
| 19 | +import org.bukkit.inventory.meta.ItemMeta; |
| 20 | +import org.junit.Before; |
| 21 | +import org.junit.Test; |
| 22 | +import org.junit.runner.RunWith; |
| 23 | +import org.mockito.Mockito; |
| 24 | +import org.powermock.api.mockito.PowerMockito; |
| 25 | +import org.powermock.core.classloader.annotations.PrepareForTest; |
| 26 | +import org.powermock.modules.junit4.PowerMockRunner; |
| 27 | +import org.powermock.reflect.Whitebox; |
| 28 | + |
| 29 | +import us.tastybento.bskyblock.BSkyBlock; |
| 30 | +import us.tastybento.bskyblock.api.configuration.WorldSettings; |
| 31 | +import us.tastybento.bskyblock.lists.Flags; |
| 32 | +import us.tastybento.bskyblock.managers.IslandWorldManager; |
| 33 | +import us.tastybento.bskyblock.util.Util; |
| 34 | + |
| 35 | +/** |
| 36 | + * @author tastybento |
| 37 | + * |
| 38 | + */ |
| 39 | +@RunWith(PowerMockRunner.class) |
| 40 | +@PrepareForTest({Bukkit.class, BSkyBlock.class, Util.class }) |
| 41 | +public class CleanSuperFlatListenerTest { |
| 42 | + |
| 43 | + private World world; |
| 44 | + private Block block; |
| 45 | + private Chunk chunk; |
| 46 | + private IslandWorldManager iwm; |
| 47 | + private BSkyBlock plugin; |
| 48 | + |
| 49 | + /** |
| 50 | + * @throws java.lang.Exception |
| 51 | + */ |
| 52 | + @Before |
| 53 | + public void setUp() throws Exception { |
| 54 | + |
| 55 | + // Set up plugin |
| 56 | + plugin = mock(BSkyBlock.class); |
| 57 | + Whitebox.setInternalState(BSkyBlock.class, "instance", plugin); |
| 58 | + |
| 59 | + when(plugin.isLoaded()).thenReturn(true); |
| 60 | + |
| 61 | + // World |
| 62 | + world = mock(World.class); |
| 63 | + when(world.getEnvironment()).thenReturn(World.Environment.NORMAL); |
| 64 | + |
| 65 | + PowerMockito.mockStatic(Util.class); |
| 66 | + when(Util.getWorld(Mockito.any())).thenReturn(world); |
| 67 | + |
| 68 | + // World Settings |
| 69 | + iwm = mock(IslandWorldManager.class); |
| 70 | + when(plugin.getIWM()).thenReturn(iwm); |
| 71 | + WorldSettings ws = mock(WorldSettings.class); |
| 72 | + when(iwm.getWorldSettings(Mockito.any())).thenReturn(ws); |
| 73 | + Map<String, Boolean> worldFlags = new HashMap<>(); |
| 74 | + when(ws.getWorldFlags()).thenReturn(worldFlags); |
| 75 | + when(iwm.isNetherGenerate(Mockito.any())).thenReturn(true); |
| 76 | + when(iwm.isEndGenerate(Mockito.any())).thenReturn(true); |
| 77 | + when(iwm.isNetherIslands(Mockito.any())).thenReturn(true); |
| 78 | + when(iwm.isEndIslands(Mockito.any())).thenReturn(true); |
| 79 | + |
| 80 | + |
| 81 | + PowerMockito.mockStatic(Bukkit.class); |
| 82 | + ItemFactory itemF = mock(ItemFactory.class); |
| 83 | + ItemMeta im = mock(ItemMeta.class); |
| 84 | + when(itemF.getItemMeta(Mockito.any())).thenReturn(im); |
| 85 | + when(Bukkit.getItemFactory()).thenReturn(itemF); |
| 86 | + |
| 87 | + Flags.CLEAN_SUPER_FLAT.setSetting(world, true); |
| 88 | + |
| 89 | + chunk = mock(Chunk.class); |
| 90 | + when(chunk.getWorld()).thenReturn(world); |
| 91 | + block = mock(Block.class); |
| 92 | + when(block.getType()).thenReturn(Material.BEDROCK); |
| 93 | + when(chunk.getBlock(Mockito.anyInt(), Mockito.anyInt(), Mockito.anyInt())).thenReturn(block); |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Test method for {@link us.tastybento.bskyblock.listeners.flags.CleanSuperFlatListener#onChunkLoad(org.bukkit.event.world.ChunkLoadEvent)}. |
| 99 | + */ |
| 100 | + @Test |
| 101 | + public void testOnChunkLoadNotBedrockNoFlsg() { |
| 102 | + when(block.getType()).thenReturn(Material.AIR); |
| 103 | + Flags.CLEAN_SUPER_FLAT.setSetting(world, false); |
| 104 | + |
| 105 | + ChunkLoadEvent e = new ChunkLoadEvent(chunk, false); |
| 106 | + new CleanSuperFlatListener().onChunkLoad(e); |
| 107 | + Mockito.verify(world, Mockito.never()).regenerateChunk(Mockito.anyInt(), Mockito.anyInt()); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Test method for {@link us.tastybento.bskyblock.listeners.flags.CleanSuperFlatListener#onChunkLoad(org.bukkit.event.world.ChunkLoadEvent)}. |
| 112 | + */ |
| 113 | + @Test |
| 114 | + public void testOnChunkLoadNotLoaded() { |
| 115 | + when(plugin.isLoaded()).thenReturn(false); |
| 116 | + ChunkLoadEvent e = new ChunkLoadEvent(chunk, false); |
| 117 | + new CleanSuperFlatListener().onChunkLoad(e); |
| 118 | + Mockito.verify(world, Mockito.never()).regenerateChunk(Mockito.anyInt(), Mockito.anyInt()); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Test method for {@link us.tastybento.bskyblock.listeners.flags.CleanSuperFlatListener#onChunkLoad(org.bukkit.event.world.ChunkLoadEvent)}. |
| 123 | + */ |
| 124 | + @Test |
| 125 | + public void testOnChunkLoadBedrock() { |
| 126 | + ChunkLoadEvent e = new ChunkLoadEvent(chunk, false); |
| 127 | + new CleanSuperFlatListener().onChunkLoad(e); |
| 128 | + Mockito.verify(world).regenerateChunk(Mockito.anyInt(), Mockito.anyInt()); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Test method for {@link us.tastybento.bskyblock.listeners.flags.CleanSuperFlatListener#onChunkLoad(org.bukkit.event.world.ChunkLoadEvent)}. |
| 133 | + */ |
| 134 | + @Test |
| 135 | + public void testOnChunkLoadBedrockNoClean() { |
| 136 | + Flags.CLEAN_SUPER_FLAT.setSetting(world, false); |
| 137 | + |
| 138 | + ChunkLoadEvent e = new ChunkLoadEvent(chunk, false); |
| 139 | + new CleanSuperFlatListener().onChunkLoad(e); |
| 140 | + Mockito.verify(world, Mockito.never()).regenerateChunk(Mockito.anyInt(), Mockito.anyInt()); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Test method for {@link us.tastybento.bskyblock.listeners.flags.CleanSuperFlatListener#onChunkLoad(org.bukkit.event.world.ChunkLoadEvent)}. |
| 145 | + */ |
| 146 | + @Test |
| 147 | + public void testOnChunkLoadBedrockNether() { |
| 148 | + when(world.getEnvironment()).thenReturn(World.Environment.NETHER); |
| 149 | + ChunkLoadEvent e = new ChunkLoadEvent(chunk, false); |
| 150 | + new CleanSuperFlatListener().onChunkLoad(e); |
| 151 | + Mockito.verify(world).regenerateChunk(Mockito.anyInt(), Mockito.anyInt()); |
| 152 | + when(iwm.isNetherGenerate(Mockito.any())).thenReturn(false); |
| 153 | + when(iwm.isNetherIslands(Mockito.any())).thenReturn(true); |
| 154 | + new CleanSuperFlatListener().onChunkLoad(e); |
| 155 | + Mockito.verify(world).regenerateChunk(Mockito.anyInt(), Mockito.anyInt()); // No more than once |
| 156 | + when(iwm.isNetherGenerate(Mockito.any())).thenReturn(true); |
| 157 | + when(iwm.isNetherIslands(Mockito.any())).thenReturn(false); |
| 158 | + new CleanSuperFlatListener().onChunkLoad(e); |
| 159 | + Mockito.verify(world).regenerateChunk(Mockito.anyInt(), Mockito.anyInt()); // No more than once |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Test method for {@link us.tastybento.bskyblock.listeners.flags.CleanSuperFlatListener#onChunkLoad(org.bukkit.event.world.ChunkLoadEvent)}. |
| 164 | + */ |
| 165 | + @Test |
| 166 | + public void testOnChunkLoadBedrockEnd() { |
| 167 | + when(world.getEnvironment()).thenReturn(World.Environment.THE_END); |
| 168 | + ChunkLoadEvent e = new ChunkLoadEvent(chunk, false); |
| 169 | + new CleanSuperFlatListener().onChunkLoad(e); |
| 170 | + Mockito.verify(world).regenerateChunk(Mockito.anyInt(), Mockito.anyInt()); |
| 171 | + when(iwm.isEndGenerate(Mockito.any())).thenReturn(false); |
| 172 | + when(iwm.isEndIslands(Mockito.any())).thenReturn(true); |
| 173 | + new CleanSuperFlatListener().onChunkLoad(e); |
| 174 | + Mockito.verify(world).regenerateChunk(Mockito.anyInt(), Mockito.anyInt()); // No more than once |
| 175 | + when(iwm.isEndGenerate(Mockito.any())).thenReturn(true); |
| 176 | + when(iwm.isEndIslands(Mockito.any())).thenReturn(false); |
| 177 | + new CleanSuperFlatListener().onChunkLoad(e); |
| 178 | + Mockito.verify(world).regenerateChunk(Mockito.anyInt(), Mockito.anyInt()); // No more than once |
| 179 | + } |
| 180 | + |
| 181 | +} |
0 commit comments