Added tests to cover #99

This commit is contained in:
tastybento 2023-03-01 18:30:35 -08:00
parent a4fc49689b
commit e4bbb70acb
2 changed files with 80 additions and 9 deletions

View File

@ -132,15 +132,21 @@ public class EcoSystemManager {
} }
private void addMobs(Greenhouse gh) { /**
* Try to spawn mobs in a greenhouse
* @param gh greenhouse
* @return true if mobs were spawned, false if not
*/
boolean addMobs(Greenhouse gh) {
final BoundingBox bb = gh.getBoundingBox(); final BoundingBox bb = gh.getBoundingBox();
if(gh.getLocation() == null || gh.getLocation().getWorld() == null || gh.getWorld() == null if(gh.getLocation() == null || gh.getLocation().getWorld() == null || gh.getWorld() == null
|| !gh.getLocation().getWorld().isChunkLoaded(((int) bb.getMaxX()) >> 4, ((int) bb.getMaxZ()) >> 4) || !gh.getLocation().getWorld().isChunkLoaded(((int) bb.getMinX()) >> 4, ((int) bb.getMinZ()) >> 4)){ || !gh.getLocation().getWorld().isChunkLoaded(((int) bb.getMaxX()) >> 4, ((int) bb.getMaxZ()) >> 4)
|| !gh.getLocation().getWorld().isChunkLoaded(((int) bb.getMinX()) >> 4, ((int) bb.getMinZ()) >> 4)){
// Skipping addmobs for unloaded greenhouse // Skipping addmobs for unloaded greenhouse
return; return false;
} }
if (gh.getBiomeRecipe().noMobs()) { if (gh.getBiomeRecipe().noMobs()) {
return; return false;
} }
// Check greenhouse chunks are loaded // Check greenhouse chunks are loaded
for (double blockX = bb.getMinX(); blockX < bb.getMaxX(); blockX+=16) { for (double blockX = bb.getMinX(); blockX < bb.getMaxX(); blockX+=16) {
@ -148,7 +154,7 @@ public class EcoSystemManager {
int chunkX = (int)(blockX / 16); int chunkX = (int)(blockX / 16);
int chunkZ = (int)(blockZ / 16); int chunkZ = (int)(blockZ / 16);
if (!gh.getWorld().isChunkLoaded(chunkX, chunkZ)) { if (!gh.getWorld().isChunkLoaded(chunkX, chunkZ)) {
return; return false;
} }
} }
} }
@ -162,7 +168,7 @@ public class EcoSystemManager {
Iterator<GrowthBlock> it = list.iterator(); Iterator<GrowthBlock> it = list.iterator();
// Check if the greenhouse is full // Check if the greenhouse is full
if (sum >= gh.getBiomeRecipe().getMaxMob()) { if (sum >= gh.getBiomeRecipe().getMaxMob()) {
return; return false;
} }
while (it.hasNext() && (sum == 0 || gh.getArea() / sum >= gh.getBiomeRecipe().getMobLimit())) { while (it.hasNext() && (sum == 0 || gh.getArea() / sum >= gh.getBiomeRecipe().getMobLimit())) {
// Spawn something if chance says so // Spawn something if chance says so
@ -171,6 +177,7 @@ public class EcoSystemManager {
sum++; sum++;
} }
} }
return sum > 0;
} }
/** /**

View File

@ -1,6 +1,8 @@
package world.bentobox.greenhouses.managers; package world.bentobox.greenhouses.managers;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
@ -9,11 +11,11 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import java.util.List; import java.util.List;
import java.util.Optional;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Keyed; import org.bukkit.Keyed;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.Tag; import org.bukkit.Tag;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Block; import org.bukkit.block.Block;
@ -25,13 +27,13 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.stubbing.OngoingStubbing;
import org.powermock.api.mockito.PowerMockito; import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.modules.junit4.PowerMockRunner;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.greenhouses.data.Greenhouse; import world.bentobox.greenhouses.data.Greenhouse;
import world.bentobox.greenhouses.greenhouse.BiomeRecipe;
import world.bentobox.greenhouses.managers.EcoSystemManager.GrowthBlock; import world.bentobox.greenhouses.managers.EcoSystemManager.GrowthBlock;
/** /**
@ -39,7 +41,7 @@ import world.bentobox.greenhouses.managers.EcoSystemManager.GrowthBlock;
* *
*/ */
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BentoBox.class, Tag.class}) @PrepareForTest({Bukkit.class, BentoBox.class, Tag.class, RecipeManager.class})
public class EcoSystemManagerTest { public class EcoSystemManagerTest {
private Greenhouse gh; private Greenhouse gh;
@ -53,6 +55,8 @@ public class EcoSystemManagerTest {
private Block liquid; private Block liquid;
@Mock @Mock
private Block plant; private Block plant;
@Mock
private BiomeRecipe recipe;
// CUT // CUT
private EcoSystemManager eco; private EcoSystemManager eco;
@ -62,6 +66,7 @@ public class EcoSystemManagerTest {
public void setUp() { public void setUp() {
PowerMockito.mockStatic(Tag.class, Mockito.RETURNS_MOCKS); PowerMockito.mockStatic(Tag.class, Mockito.RETURNS_MOCKS);
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS);
@SuppressWarnings("unchecked")
Tag<Keyed> tag = mock(Tag.class); Tag<Keyed> tag = mock(Tag.class);
when(Bukkit.getTag(anyString(), any(), any())).thenReturn(tag); when(Bukkit.getTag(anyString(), any(), any())).thenReturn(tag);
@ -95,6 +100,12 @@ public class EcoSystemManagerTest {
// Liquid false // Liquid false
when(block.getRelative(eq(BlockFace.UP))).thenReturn(air); when(block.getRelative(eq(BlockFace.UP))).thenReturn(air);
// Recipe
when(recipe.noMobs()).thenReturn(true);
PowerMockito.mockStatic(RecipeManager.class, Mockito.RETURNS_MOCKS);
when(RecipeManager.getBiomeRecipies(any())).thenReturn(Optional.of(recipe));
eco = new EcoSystemManager(null, null); eco = new EcoSystemManager(null, null);
} }
@ -209,4 +220,57 @@ public class EcoSystemManagerTest {
assertEquals(liquid, value.block()); assertEquals(liquid, value.block());
} }
} }
/**
* Test method for {@link world.bentobox.greenhouses.managers.EcoSystemManager#addMobs(Greenhouse)}.
*/
@Test
public void testAddMobsChunkNotLoaded() {
assertFalse(eco.addMobs(gh));
}
/**
* Test method for {@link world.bentobox.greenhouses.managers.EcoSystemManager#addMobs(Greenhouse)}.
*/
@Test
public void testAddMobsChunkLoadedNoMobs() {
when(world.isChunkLoaded(anyInt(), anyInt())).thenReturn(true);
assertFalse(eco.addMobs(gh));
}
/**
* Test method for {@link world.bentobox.greenhouses.managers.EcoSystemManager#addMobs(Greenhouse)}.
*/
@Test
public void testAddMobsChunkLoadedWithMobsInRecipeMaxMobsZero() {
when(world.isChunkLoaded(anyInt(), anyInt())).thenReturn(true);
when(recipe.noMobs()).thenReturn(false);
assertFalse(eco.addMobs(gh));
}
/**
* Test method for {@link world.bentobox.greenhouses.managers.EcoSystemManager#addMobs(Greenhouse)}.
*/
@Test
public void testAddMobsChunkLoadedWithMobsInRecipeMaxMobsNotZero() {
// Nothing spawned here
when(world.isChunkLoaded(anyInt(), anyInt())).thenReturn(true);
when(recipe.noMobs()).thenReturn(false);
when(recipe.getMaxMob()).thenReturn(10);
assertFalse(eco.addMobs(gh));
}
/**
* Test method for {@link world.bentobox.greenhouses.managers.EcoSystemManager#addMobs(Greenhouse)}.
*/
@Test
public void testAddMobsSpawnMob() {
// Nothing spawned here
when(world.isChunkLoaded(anyInt(), anyInt())).thenReturn(true);
when(recipe.noMobs()).thenReturn(false);
when(recipe.getMaxMob()).thenReturn(10);
when(recipe.spawnMob(any())).thenReturn(true);
assertTrue(eco.addMobs(gh));
}
} }