From bf8d820848a407692393ffb4275590597e01ef01 Mon Sep 17 00:00:00 2001 From: DmitriyMX Date: Sun, 26 Aug 2018 00:24:59 +0300 Subject: [PATCH] Hello, Mockito! --- build.gradle | 8 +- core/build.gradle | 3 - .../test/java/mc/core/TestEntityLocation.java | 32 +--- core/src/test/java/mc/core/TestLocation.java | 76 ++-------- .../proto_1_12_2/packets/DummyWorld.java | 138 ------------------ .../packets/TestChunkdataPacket.java | 58 +++++++- 6 files changed, 79 insertions(+), 236 deletions(-) delete mode 100644 proto_1.12.2/src/test/java/mc/core/network/proto_1_12_2/packets/DummyWorld.java diff --git a/build.gradle b/build.gradle index 8735a6b..380447c 100644 --- a/build.gradle +++ b/build.gradle @@ -36,8 +36,12 @@ subprojects { /* Components */ compile (group: 'org.projectlombok', name: 'lombok', version: '1.16.16') - /* Test */ + /* JUnit */ testCompile (group: 'junit', name: 'junit', version: '4.12') + /* Simple log */ + testCompile (group: 'org.slf4j', name: 'slf4j-simple', version: slf4j_version) + /* Mockito */ + testCompile (group: 'org.mockito', name: 'mockito-core', version: '1.9.5') } task copyDep(type: Copy) { @@ -63,7 +67,7 @@ task runApp(type: JavaExec) { /* Uncomment, if you used VM args */ //jvmArgs = [ - // "-DspringConfig=spring-flat.xml", + // "-DspringConfig=spring.xml", // "-Dlog4j.configurationFile=log4j2.xml" //] diff --git a/core/build.gradle b/core/build.gradle index d6d32d2..77e3054 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -12,7 +12,4 @@ dependencies { compile (group: 'com.google.guava', name: 'guava', version: '24.1-jre') /* Named Binary Tags */ compile (group: 'com.flowpowered', name: 'flow-nbt', version: '1.0.0') - - /* Simple log */ - testCompile (group: 'org.slf4j', name: 'slf4j-simple', version: slf4j_version) } diff --git a/core/src/test/java/mc/core/TestEntityLocation.java b/core/src/test/java/mc/core/TestEntityLocation.java index 344991d..d1dddf6 100644 --- a/core/src/test/java/mc/core/TestEntityLocation.java +++ b/core/src/test/java/mc/core/TestEntityLocation.java @@ -1,40 +1,16 @@ package mc.core; import mc.core.world.World; -import mc.core.world.WorldType; -import mc.core.world.chunk.Chunk; import org.junit.Assert; import org.junit.Test; +import org.mockito.Mockito; + +import static org.mockito.Mockito.mock; public class TestEntityLocation { @Test public void cloneTest() { - World dummyWorld = new World() { - @Override - public WorldType getWorldType() { - return null; - } - - @Override - public EntityLocation getSpawn() { - return null; - } - - @Override - public void setSpawn(EntityLocation location) { - - } - - @Override - public Chunk getChunk(int x, int z) { - return null; - } - - @Override - public void setChunk(int x, int z, Chunk chunk) { - - } - }; + World dummyWorld = mock(World.class); EntityLocation firstLocation = new EntityLocation(10, 20, 30, 40, 50, dummyWorld); Assert.assertSame("Lost world reference before cloning", dummyWorld, firstLocation.getWorld()); diff --git a/core/src/test/java/mc/core/TestLocation.java b/core/src/test/java/mc/core/TestLocation.java index 9b9cf61..239c2c4 100644 --- a/core/src/test/java/mc/core/TestLocation.java +++ b/core/src/test/java/mc/core/TestLocation.java @@ -1,80 +1,33 @@ package mc.core; -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import mc.core.world.Biome; import mc.core.world.World; -import mc.core.world.WorldType; import mc.core.world.chunk.Chunk; -import mc.core.world.chunk.ChunkSection; -import org.junit.Assert; +import org.junit.Before; import org.junit.Test; +import org.mockito.stubbing.Answer; import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.*; public class TestLocation { - private class DummyWorld implements World { - @RequiredArgsConstructor - private class DummyChunk implements Chunk { - @Getter - private final int x, z; + private World world; - @Override - public ChunkSection getChunkSection(int height) { - return null; - } + @Before + public void prepareWorld() { + this.world = mock(World.class); + when(world.getChunk(anyInt(), anyInt())).thenAnswer(invocation -> { + Object[] args = invocation.getArguments(); - @Override - public void setChunkSection(int height, ChunkSection chunkSection) { - } + Chunk chunk = mock(Chunk.class); + when(chunk.getX()).thenReturn((int) args[0]); + when(chunk.getZ()).thenReturn((int) args[1]); - @Override - public Biome getBiome(int localX, int localZ) { - return null; - } - - @Override - public void setBiome(int localX, int localZ, Biome biome) { - } - - @Override - public World getWorld() { - return null; - } - - @Override - public void setWorld(World world) { - } - } - - @Override - public WorldType getWorldType() { - return null; - } - - @Override - public EntityLocation getSpawn() { - return null; - } - - @Override - public void setSpawn(EntityLocation location) { - } - - @Override - public Chunk getChunk(int x, int z) { - return new DummyChunk(x, z); - } - - @Override - public void setChunk(int x, int z, Chunk chunkSection) { - - } + return chunk; + }); } @Test public void testGetBlockXZ() { - World world = new DummyWorld(); Location location; location = new Location(0d, 0, 0d, world); @@ -120,7 +73,6 @@ public class TestLocation { @Test public void testGetChunk() { - World world = new DummyWorld(); Location location; Chunk chunk; diff --git a/proto_1.12.2/src/test/java/mc/core/network/proto_1_12_2/packets/DummyWorld.java b/proto_1.12.2/src/test/java/mc/core/network/proto_1_12_2/packets/DummyWorld.java deleted file mode 100644 index 52e8a88..0000000 --- a/proto_1.12.2/src/test/java/mc/core/network/proto_1_12_2/packets/DummyWorld.java +++ /dev/null @@ -1,138 +0,0 @@ -package mc.core.network.proto_1_12_2.packets; - -import mc.core.EntityLocation; -import mc.core.world.Biome; -import mc.core.world.World; -import mc.core.world.WorldType; -import mc.core.world.block.Block; -import mc.core.world.block.BlockFactory; -import mc.core.world.block.BlockType; -import mc.core.world.chunk.Chunk; -import mc.core.world.chunk.ChunkSection; - -public class DummyWorld implements World { - private class DummyChunkSection implements ChunkSection { - @Override - public int getSkyLight(int x, int y, int z) { - if (y <= 3) return 0; - else return 15; - } - - @Override - public void setSkyLight(int x, int y, int z, int lightLevel) { - } - - @Override - public int getAddition(int x, int y, int z) { - return 0; - } - - @Override - public void setAddition(int x, int y, int z, int value) { - } - - @Override - public Biome getBiome(int localX, int localZ) { - return Biome.PLAINS; - } - - @Override - public int getX() { - return 0; - } - - @Override - public int getY() { - return 0; - } - - @Override - public int getZ() { - return 0; - } - - @Override - public void setBlock(Block block) { - } - - @Override - public Block getBlock(int x, int y, int z) { - BlockFactory blockFactory = new BlockFactory(); - - if (y == 0) return blockFactory.create(BlockType.BEDROCK, x, y, z, getWorld()); - else if (y >= 1 && y <= 2) return blockFactory.create(BlockType.DIRT, x, y, z, getWorld()); - else if (y == 3) return blockFactory.create(BlockType.GRASS, x, y, z, getWorld()); - else return blockFactory.create(BlockType.AIR, x, y, z, getWorld()); - } - - @Override - public World getWorld() { - return DummyWorld.this; - } - } - - private class DummyChunk implements Chunk { - @Override - public World getWorld() { - return DummyWorld.this; - } - - @Override - public void setWorld(World world) { - } - - @Override - public ChunkSection getChunkSection(int height) { - if (height < 1) return new DummyChunkSection(); - else return null; - } - - @Override - public void setChunkSection(int height, ChunkSection chunkSection) { - } - - @Override - public Biome getBiome(int localX, int localZ) { - return Biome.PLAINS; - } - - @Override - public void setBiome(int localX, int localZ, Biome biome) { - } - - @Override - public int getX() { - return 0; - } - - @Override - public int getZ() { - return 0; - } - } - - private final Chunk chunk = new DummyChunk(); - - @Override - public WorldType getWorldType() { - return WorldType.FLAT; - } - - @Override - public EntityLocation getSpawn() { - return null; - } - - @Override - public void setSpawn(EntityLocation location) { - } - - @Override - public Chunk getChunk(int x, int z) { - return chunk; - } - - @Override - public void setChunk(int x, int z, Chunk chunk) { - } -} diff --git a/proto_1.12.2/src/test/java/mc/core/network/proto_1_12_2/packets/TestChunkdataPacket.java b/proto_1.12.2/src/test/java/mc/core/network/proto_1_12_2/packets/TestChunkdataPacket.java index 05f8528..d4febe9 100644 --- a/proto_1.12.2/src/test/java/mc/core/network/proto_1_12_2/packets/TestChunkdataPacket.java +++ b/proto_1.12.2/src/test/java/mc/core/network/proto_1_12_2/packets/TestChunkdataPacket.java @@ -2,15 +2,27 @@ package mc.core.network.proto_1_12_2.packets; import com.google.common.io.ByteStreams; import mc.core.network.proto_1_12_2.ByteArrayOutputNetStream; +import mc.core.world.Biome; import mc.core.world.World; +import mc.core.world.WorldType; +import mc.core.world.block.BlockFactory; +import mc.core.world.block.BlockType; +import mc.core.world.chunk.Chunk; +import mc.core.world.chunk.ChunkSection; import org.junit.Assert; +import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import java.io.*; +import static org.mockito.Matchers.anyInt; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + public class TestChunkdataPacket { private static byte[] expectedPacketData; + private World world; @BeforeClass public static void beforeClassTest() throws IOException { @@ -18,8 +30,48 @@ public class TestChunkdataPacket { expectedPacketData = ByteStreams.toByteArray(inputStream); } - private World createDummyWorld() { - return new DummyWorld(); + @Before + public void prepareWorld() { + final ChunkSection chunkSection = mock(ChunkSection.class); + when(chunkSection.getSkyLight(anyInt(), anyInt(), anyInt())).thenAnswer(invocation -> { + int y = (int)invocation.getArguments()[1]; + + if (y <= 3) return 0; + else return 15; + }); + when(chunkSection.getBiome(anyInt(), anyInt())).thenReturn(Biome.PLAINS); + when(chunkSection.getBlock(anyInt(), anyInt(), anyInt())).thenAnswer(invocation -> { + Object[] args = invocation.getArguments(); + int x = (int) args[0]; + int y = (int) args[1]; + int z = (int) args[2]; + + BlockFactory blockFactory = new BlockFactory(); + + if (y == 0) return blockFactory.create(BlockType.BEDROCK, x, y, z, null); + else if (y >= 1 && y <= 2) return blockFactory.create(BlockType.DIRT, x, y, z, null); + else if (y == 3) return blockFactory.create(BlockType.GRASS, x, y, z, null); + else return blockFactory.create(BlockType.AIR, x, y, z, null); + }); + + world = mock(World.class); + when(world.getWorldType()).thenReturn(WorldType.FLAT); + when(world.getChunk(anyInt(), anyInt())).thenAnswer(invocation -> { + Object[] args = invocation.getArguments(); + + Chunk chunk = mock(Chunk.class); + when(chunk.getX()).thenReturn((int) args[0]); + when(chunk.getZ()).thenReturn((int) args[1]); + when(chunk.getBiome(anyInt(), anyInt())).thenReturn(Biome.PLAINS); + when(chunk.getChunkSection(anyInt())).thenAnswer(invocation1 -> { + int height = (int)invocation1.getArguments()[0]; + + if (height < 1) return chunkSection; + else return null; + }); + + return chunk; + }); } @Test @@ -27,7 +79,7 @@ public class TestChunkdataPacket { ChunkDataPacket packet = new ChunkDataPacket(); packet.setX(0); packet.setZ(0); - packet.setChunk(createDummyWorld().getChunk(0, 0)); + packet.setChunk(world.getChunk(0, 0)); packet.setInitChunk(true); ByteArrayOutputNetStream netStream = new ByteArrayOutputNetStream();