Greenhouses/src/test/java/world/bentobox/greenhouses/greenhouse/WallsTest.java

260 lines
8.6 KiB
Java
Raw Normal View History

2020-10-11 18:03:13 +02:00
package world.bentobox.greenhouses.greenhouse;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
2020-10-11 18:03:13 +02:00
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
2020-10-11 18:03:13 +02:00
import static org.mockito.Mockito.when;
2021-01-16 23:27:29 +01:00
import java.util.concurrent.CompletableFuture;
import org.bukkit.Bukkit;
2020-10-11 18:03:13 +02:00
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Tag;
2020-10-11 18:03:13 +02:00
import org.bukkit.World;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
2021-01-16 23:27:29 +01:00
import world.bentobox.bentobox.BentoBox;
import world.bentobox.greenhouses.Greenhouses;
import world.bentobox.greenhouses.Settings;
import world.bentobox.greenhouses.greenhouse.Walls.WallFinder;
2021-01-16 23:27:29 +01:00
import world.bentobox.greenhouses.world.AsyncWorldCache;
2020-10-11 18:03:13 +02:00
/**
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
2021-01-16 23:27:29 +01:00
@PrepareForTest({Bukkit.class, Greenhouses.class})
2020-10-11 18:03:13 +02:00
public class WallsTest {
private Roof roof;
@Mock
private Location location;
@Mock
private World world;
/**
* Class under test
*/
private Walls walls;
@Mock
private Greenhouses addon;
private Settings s;
2021-01-16 23:27:29 +01:00
@Mock
private BentoBox plugin;
@Mock
private AsyncWorldCache cache;
private CompletableFuture<Walls> r;
2020-10-11 18:03:13 +02:00
@Before
2021-08-01 08:10:07 +02:00
public void setUp() {
2021-01-16 23:27:29 +01:00
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS);
when(Tag.TRAPDOORS.isTagged(Material.BIRCH_TRAPDOOR)).thenReturn(true);
// Declare mock after mocking Bukkit
roof = mock(Roof.class);
s = new Settings();
when(addon.getSettings()).thenReturn(s);
2021-01-16 23:27:29 +01:00
when(addon.getPlugin()).thenReturn(plugin);
when(addon.wallBlocks(any())).thenCallRealMethod();
2021-01-16 23:27:29 +01:00
walls = new Walls(cache);
2020-10-11 18:03:13 +02:00
when(world.getMaxHeight()).thenReturn(255);
when(location.getWorld()).thenReturn(world);
when(location.getBlockX()).thenReturn(10);
when(location.getBlockY()).thenReturn(10);
when(location.getBlockZ()).thenReturn(10);
when(location.clone()).thenReturn(location);
2021-01-16 23:27:29 +01:00
when(cache.getBlockType(any())).thenReturn(Material.GLASS);
when(cache.getBlockType(anyInt(),anyInt(),anyInt())).thenReturn(Material.GLASS);
2020-10-11 18:03:13 +02:00
when(roof.getHeight()).thenReturn(1);
when(roof.getLocation()).thenReturn(location);
2021-01-16 23:27:29 +01:00
r = new CompletableFuture<>();
2020-10-11 18:03:13 +02:00
}
/**
* Test method for {@link world.bentobox.greenhouses.greenhouse.Walls#findWalls(world.bentobox.greenhouses.greenhouse.Roof)}.
*/
@Test
public void testFindWalls() {
2021-01-16 23:27:29 +01:00
walls.findWalls(r, roof);
2020-10-11 18:03:13 +02:00
assertEquals("Walls [minX=-2, maxX=11, minZ=-2, maxZ=11, floor=0]", walls.toString());
}
/**
* Test method for {@link world.bentobox.greenhouses.greenhouse.Walls#lookAround(org.bukkit.Location, world.bentobox.greenhouses.greenhouse.Walls.WallFinder, world.bentobox.greenhouses.greenhouse.Roof)}.
*/
@Test
public void testLookAround() {
2021-09-18 19:33:48 +02:00
WallFinder wf = new WallFinder();
2020-10-11 18:03:13 +02:00
walls.lookAround(location, wf, roof);
assertTrue(wf.stopMaxX);
assertTrue(wf.stopMaxZ);
assertFalse(wf.stopMinX);
assertFalse(wf.stopMinZ);
assertEquals(1, wf.radiusMinX);
assertEquals(0, wf.radiusMaxX);
assertEquals(1, wf.radiusMinZ);
assertEquals(0, wf.radiusMaxZ);
}
/**
* Test method for {@link world.bentobox.greenhouses.greenhouse.Walls#analyzeFindings(world.bentobox.greenhouses.greenhouse.Walls.WallFinder, world.bentobox.greenhouses.greenhouse.Roof)}.
*/
@Test
public void testAnalyzeFindings() {
2021-09-18 19:33:48 +02:00
WallFinder wf = new WallFinder();
2020-10-11 18:03:13 +02:00
walls.analyzeFindings(wf, roof);
assertFalse(wf.stopMaxX);
assertFalse(wf.stopMaxZ);
assertFalse(wf.stopMinX);
assertFalse(wf.stopMinZ);
assertEquals(1, wf.radiusMinX);
assertEquals(1, wf.radiusMaxX);
assertEquals(1, wf.radiusMinZ);
assertEquals(1, wf.radiusMaxZ);
}
/**
* Test method for {@link world.bentobox.greenhouses.greenhouse.Walls#analyzeFindings(world.bentobox.greenhouses.greenhouse.Walls.WallFinder, world.bentobox.greenhouses.greenhouse.Roof)}.
*/
@Test
public void testAnalyzeFindingsStop() {
walls.minX = -1;
walls.maxX = 1;
walls.minZ = -1;
walls.maxZ = 1;
2021-09-18 19:33:48 +02:00
WallFinder wf = new WallFinder();
2020-10-11 18:03:13 +02:00
walls.analyzeFindings(wf, roof);
assertTrue(wf.stopMaxX);
assertTrue(wf.stopMaxZ);
assertTrue(wf.stopMinX);
assertTrue(wf.stopMinZ);
assertEquals(0, wf.radiusMinX);
assertEquals(0, wf.radiusMaxX);
assertEquals(0, wf.radiusMinZ);
assertEquals(0, wf.radiusMaxZ);
}
/**
2021-08-01 08:10:07 +02:00
* Test method for {@link world.bentobox.greenhouses.greenhouse.Walls#lookAtBlockFaces(WallFinder, int, int, int)}.
2020-10-11 18:03:13 +02:00
*/
@Test
public void testLookAtBlockFaces() {
2021-09-18 19:33:48 +02:00
WallFinder wf = new WallFinder();
2021-01-16 23:27:29 +01:00
walls.lookAtBlockFaces(wf, 0, 5, -1);
2020-10-11 18:03:13 +02:00
assertTrue(wf.stopMaxX);
assertTrue(wf.stopMaxZ);
assertTrue(wf.stopMinX);
assertTrue(wf.stopMinZ);
}
/**
2021-08-01 08:10:07 +02:00
* Test method for {@link world.bentobox.greenhouses.greenhouse.Walls#lookAtBlockFaces(WallFinder, int, int, int)}.
2020-10-11 18:03:13 +02:00
*/
@Test
public void testLookAtBlockFacesNoGlass() {
2021-01-16 23:27:29 +01:00
when(cache.getBlockType(anyInt(), anyInt(), anyInt())).thenReturn(Material.AIR);
2021-09-18 19:33:48 +02:00
WallFinder wf = new WallFinder();
2021-01-16 23:27:29 +01:00
walls.lookAtBlockFaces(wf, 0, 5, -1);
2020-10-11 18:03:13 +02:00
assertFalse(wf.stopMaxX);
assertFalse(wf.stopMaxZ);
assertFalse(wf.stopMinX);
assertFalse(wf.stopMinZ);
}
/**
* Test method for {@link world.bentobox.greenhouses.greenhouse.Walls#getFloorY(int, int, int, int, int, int)}.
2020-10-11 18:03:13 +02:00
*/
@Test
public void testGetFloorYZeroY() {
assertEquals(-64, walls.getFloorY(10, 0, 1, 0, 1, -64));
2020-10-11 18:03:13 +02:00
}
/**
* Test method for {@link world.bentobox.greenhouses.greenhouse.Walls#getFloorY(int, int, int, int, int, int)}.
2020-10-11 18:03:13 +02:00
*/
@Test
public void testGetFloorY() {
2021-01-16 23:27:29 +01:00
when(cache.getBlockType(anyInt(), anyInt(), anyInt())).thenReturn(Material.GLASS, Material.GLASS,
2020-10-11 18:03:13 +02:00
Material.GLASS, Material.GLASS,
Material.GLASS, Material.GLASS,
Material.AIR);
assertEquals(8, walls.getFloorY(10, 0, 1, 0, 1, -64));
2020-10-11 18:03:13 +02:00
}
/**
* Test method for {@link world.bentobox.greenhouses.greenhouse.Walls#wallBlocks(org.bukkit.Material)}.
*/
@Test
public void testWallBlocks() {
assertFalse(addon.wallBlocks(Material.ACACIA_BOAT));
assertTrue(addon.wallBlocks(Material.GLASS));
assertTrue(addon.wallBlocks(Material.GLOWSTONE));
assertTrue(addon.wallBlocks(Material.ACACIA_DOOR));
assertTrue(addon.wallBlocks(Material.HOPPER));
assertTrue(addon.wallBlocks(Material.PURPLE_STAINED_GLASS_PANE));
assertFalse(addon.wallBlocks(Material.BIRCH_TRAPDOOR));
2020-10-11 18:03:13 +02:00
}
/**
* Test method for {@link world.bentobox.greenhouses.greenhouse.Walls#wallBlocks(org.bukkit.Material)}.
*/
@Test
public void testWallBlocksNoGlowStoneNoPanes() {
s.setAllowGlowstone(false);
s.setAllowPanes(false);
assertFalse(addon.wallBlocks(Material.ACACIA_BOAT));
assertTrue(addon.wallBlocks(Material.GLASS));
assertFalse(addon.wallBlocks(Material.GLOWSTONE));
assertTrue(addon.wallBlocks(Material.ACACIA_DOOR));
assertTrue(addon.wallBlocks(Material.HOPPER));
assertFalse(addon.wallBlocks(Material.PURPLE_STAINED_GLASS_PANE));
assertFalse(addon.wallBlocks(Material.BIRCH_TRAPDOOR));
}
2020-10-11 18:03:13 +02:00
/**
* Test method for {@link world.bentobox.greenhouses.greenhouse.Walls#getFloor()}.
*/
@Test
public void testGetFloor() {
assertEquals(0, walls.getFloor());
}
/**
* Test method for {@link world.bentobox.greenhouses.greenhouse.Walls#getWidth()}.
*/
@Test
public void testGetWidth() {
assertEquals(0, walls.getWidth());
}
/**
* Test method for {@link world.bentobox.greenhouses.greenhouse.Walls#getLength()}.
*/
@Test
public void testGetLength() {
assertEquals(0, walls.getLength());
}
/**
* Test method for {@link world.bentobox.greenhouses.greenhouse.Walls#toString()}.
*/
@Test
public void testToString() {
assertEquals("Walls [minX=0, maxX=0, minZ=0, maxZ=0, floor=0]", walls.toString());
}
}